Let users retry connection in case of error. Especially SSL error.
[elinks.git] / src / network / socket.h
blobe5ced365a6df3b6e0d0027057e4140a6849099cd
1 #ifndef EL__NETWORK_SOCKET_H
2 #define EL__NETWORK_SOCKET_H
4 #include <sys/types.h>
5 #ifdef HAVE_SYS_SOCKET_H
6 #include <sys/socket.h> /* OS/2 needs this after sys/types.h */
7 #endif
9 #include "network/state.h"
11 struct connect_info;
12 struct read_buffer;
13 struct socket;
14 struct uri;
17 /* Use internally for error return values. */
18 enum socket_error {
19 SOCKET_SYSCALL_ERROR = -1, /* Retry with connection_state_for_errno(errno). */
20 SOCKET_INTERNAL_ERROR = -2, /* Stop with connection_state(errno). */
21 SOCKET_SSL_WANT_READ = -3, /* Try to read some more. */
22 SOCKET_CANT_READ = -4, /* Retry with S_CANT_READ state. */
23 SOCKET_CANT_WRITE = -5, /* Retry with S_CANT_WRITE state. */
26 enum socket_state {
27 /* If a zero-byte message is read prematurely the connection will be
28 * retried with error state S_CANT_READ. */
29 SOCKET_RETRY_ONCLOSE,
30 /* If a zero-byte message is read flush the remaining bytes in the
31 * buffer and tell the protocol handler to end the reading by calling
32 * read_buffer->done(). */
33 SOCKET_END_ONCLOSE,
34 /* Used for signaling to protocols - via the read_buffer->done()
35 * callback - that a zero-byte message was read. */
36 SOCKET_CLOSED,
39 typedef void (*socket_read_T)(struct socket *, struct read_buffer *);
40 typedef void (*socket_write_T)(struct socket *);
41 typedef void (*socket_connect_T)(struct socket *);
42 typedef void (*socket_operation_T)(struct socket *, struct connection_state);
44 struct socket_operations {
45 /* Report change in the state of the socket. */
46 socket_operation_T set_state;
47 /* Reset the timeout for the socket. */
48 socket_operation_T set_timeout;
49 /* Some system related error occurred; advise to reconnect. */
50 socket_operation_T retry;
51 /* A fatal error occurred, like a memory allocation failure; advise to
52 * abort the connection. */
53 socket_operation_T done;
56 struct read_buffer {
57 /* A routine called *each time new data comes in*, therefore
58 * usually many times, not only when all the data arrives. */
59 socket_read_T done;
61 int length;
62 int freespace;
64 unsigned char data[1]; /* must be at end of struct */
67 struct socket {
68 /* The socket descriptor */
69 int fd;
71 /* Somewhat read-specific socket state management and signaling. */
72 enum socket_state state;
74 /* Information for resolving the connection with which the socket is
75 * associated. */
76 void *conn;
78 /* Information used during the connection establishing phase. */
79 struct connect_info *connect_info;
81 /* Use for read and write buffers. */
82 struct read_buffer *read_buffer;
83 void *write_buffer;
85 /* Callbacks to the connection management: */
86 struct socket_operations *ops;
88 /* Used by the request/response interface for saving the read_done
89 * operation. */
90 socket_read_T read_done;
92 /* For connections using SSL this is in fact (ssl_t *), but we don't
93 * want to know. Noone cares and inclusion of SSL header files costs a
94 * lot of compilation time. --pasky */
95 void *ssl;
97 unsigned int protocol_family:1; /* EL_PF_INET, EL_PF_INET6 */
98 unsigned int need_ssl:1; /* If the socket needs SSL support */
99 unsigned int no_tls:1; /* Internal SSL flag. */
100 unsigned int set_no_tls:1; /* Was the blacklist checked yet? */
101 unsigned int duplex:1; /* Allow simultaneous reads & writes. */
102 unsigned int verify:1; /* Whether to verify certificates */
105 #define EL_PF_INET 0
106 #define EL_PF_INET6 1
108 /* Socket management: */
110 /* Allocate and setup a socket. */
111 struct socket *init_socket(void *conn, struct socket_operations *ops);
113 /* Reset a socket so it can be reused. XXX: Does not free() it! */
114 void done_socket(struct socket *socket);
116 /* Closes socket if open and clear any associated handlers. */
117 void close_socket(struct socket *socket);
119 /* Timeout handler. Will try to reconnect if possible. */
120 void timeout_socket(struct socket *socket);
123 /* Connection establishing: */
125 /* End successful connect() attempt to socket. */
126 void complete_connect_socket(struct socket *socket, struct uri *uri,
127 socket_connect_T done);
129 /* Establish connection with the host and port in @uri. Storing the socket
130 * descriptor in @socket. When the connection has been established the @done
131 * callback will be run. @no_cache specifies whether the DNS cache should be
132 * ignored. */
133 void make_connection(struct socket *socket, struct uri *uri,
134 socket_connect_T connect_done, int no_cache);
136 /* Creates and returns a listening socket in the same IP family as the passed
137 * ctrl_socket and stores info about the created socket in @addr. @ctrl_socket
138 * is used for getting bind() information. */
139 int get_pasv_socket(struct socket *ctrl_socket, struct sockaddr_storage *addr);
141 /* Try to connect to the next available address or force the connection to retry
142 * if all has already been tried. Updates the connection state to
143 * @connection_state. */
144 void connect_socket(struct socket *socket, struct connection_state state);
146 /* Used by the SSL layer when negotiating. */
147 void dns_exception(struct socket *socket);
150 /* Reading and writing to sockets: */
152 /* Reads data from @socket into @buffer. Calls @done each time new data is
153 * ready. */
154 void read_from_socket(struct socket *socket, struct read_buffer *buffer,
155 struct connection_state state, socket_read_T done);
157 /* Writes @datalen bytes from @data buffer to the passed @socket. When all data
158 * is written the @done callback will be called. */
159 void write_to_socket(struct socket *socket,
160 unsigned char *data, int datalen,
161 struct connection_state state, socket_write_T write_done);
163 /* Send request and get response. */
164 void request_from_socket(struct socket *socket, unsigned char *data, int datalen,
165 struct connection_state state, enum socket_state sock_state,
166 socket_read_T read_done);
168 /* Initialize a read buffer. */
169 struct read_buffer *alloc_read_buffer(struct socket *socket);
171 /* Remove @bytes number of bytes from @buffer. */
172 void kill_buffer_data(struct read_buffer *buffer, int bytes);
174 #endif