1 #ifndef EL__NETWORK_SOCKET_H
2 #define EL__NETWORK_SOCKET_H
5 #ifdef HAVE_SYS_SOCKET_H
6 #include <sys/socket.h> /* OS/2 needs this after sys/types.h */
9 #include "network/state.h"
17 /* Use internally for error return values. */
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. */
27 /* If a zero-byte message is read prematurely the connection will be
28 * retried with error state S_CANT_READ. */
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(). */
34 /* Used for signaling to protocols - via the read_buffer->done()
35 * callback - that a zero-byte message was read. */
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
;
57 /* A routine called *each time new data comes in*, therefore
58 * usually many times, not only when all the data arrives. */
64 unsigned char data
[1]; /* must be at end of struct */
68 /* The socket descriptor */
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
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
;
85 /* Callbacks to the connection management: */
86 struct socket_operations
*ops
;
88 /* Used by the request/response interface for saving the read_done
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 */
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. */
105 #define EL_PF_INET6 1
107 /* Socket management: */
109 /* Allocate and setup a socket. */
110 struct socket
*init_socket(void *conn
, struct socket_operations
*ops
);
112 /* Reset a socket so it can be reused. XXX: Does not free() it! */
113 void done_socket(struct socket
*socket
);
115 /* Closes socket if open and clear any associated handlers. */
116 void close_socket(struct socket
*socket
);
118 /* Timeout handler. Will try to reconnect if possible. */
119 void timeout_socket(struct socket
*socket
);
122 /* Connection establishing: */
124 /* End successful connect() attempt to socket. */
125 void complete_connect_socket(struct socket
*socket
, struct uri
*uri
,
126 socket_connect_T done
);
128 /* Establish connection with the host and port in @uri. Storing the socket
129 * descriptor in @socket. When the connection has been established the @done
130 * callback will be run. @no_cache specifies whether the DNS cache should be
132 void make_connection(struct socket
*socket
, struct uri
*uri
,
133 socket_connect_T connect_done
, int no_cache
);
135 /* Creates and returns a listening socket in the same IP family as the passed
136 * ctrl_socket and stores info about the created socket in @addr. @ctrl_socket
137 * is used for getting bind() information. */
138 int get_pasv_socket(struct socket
*ctrl_socket
, struct sockaddr_storage
*addr
);
140 /* Try to connect to the next available address or force the connection to retry
141 * if all has already been tried. Updates the connection state to
142 * @connection_state. */
143 void connect_socket(struct socket
*socket
, struct connection_state state
);
145 /* Used by the SSL layer when negotiating. */
146 void dns_exception(struct socket
*socket
);
149 /* Reading and writing to sockets: */
151 /* Reads data from @socket into @buffer. Calls @done each time new data is
153 void read_from_socket(struct socket
*socket
, struct read_buffer
*buffer
,
154 struct connection_state state
, socket_read_T done
);
156 /* Writes @datalen bytes from @data buffer to the passed @socket. When all data
157 * is written the @done callback will be called. */
158 void write_to_socket(struct socket
*socket
,
159 unsigned char *data
, int datalen
,
160 struct connection_state state
, socket_write_T write_done
);
162 /* Send request and get response. */
163 void request_from_socket(struct socket
*socket
, unsigned char *data
, int datalen
,
164 struct connection_state state
, enum socket_state sock_state
,
165 socket_read_T read_done
);
167 /* Initialize a read buffer. */
168 struct read_buffer
*alloc_read_buffer(struct socket
*socket
);
170 /* Remove @bytes number of bytes from @buffer. */
171 void kill_buffer_data(struct read_buffer
*buffer
, int bytes
);