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 -errno state. */
20 SOCKET_INTERNAL_ERROR
= -2, /* Stop with -errno state. */
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
*, enum connection_state 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 duplex
:1; /* Allow simultaneous reads & writes. */
104 #define EL_PF_INET6 1
106 /* Socket management: */
108 /* Allocate and setup a socket. */
109 struct socket
*init_socket(void *conn
, struct socket_operations
*ops
);
111 /* Reset a socket so it can be reused. XXX: Does not free() it! */
112 void done_socket(struct socket
*socket
);
114 /* Closes socket if open and clear any associated handlers. */
115 void close_socket(struct socket
*socket
);
117 /* Timeout handler. Will try to reconnect if possible. */
118 void timeout_socket(struct socket
*socket
);
121 /* Connection establishing: */
123 /* End successful connect() attempt to socket. */
124 void complete_connect_socket(struct socket
*socket
, struct uri
*uri
,
125 socket_connect_T done
);
127 /* Establish connection with the host and port in @uri. Storing the socket
128 * descriptor in @socket. When the connection has been established the @done
129 * callback will be run. @no_cache specifies whether the DNS cache should be
131 void make_connection(struct socket
*socket
, struct uri
*uri
,
132 socket_connect_T connect_done
, int no_cache
);
134 /* Creates and returns a listening socket in the same IP family as the passed
135 * ctrl_socket and stores info about the created socket in @addr. @ctrl_socket
136 * is used for getting bind() information. */
137 int get_pasv_socket(struct socket
*ctrl_socket
, struct sockaddr_storage
*addr
);
139 /* Try to connect to the next available address or force the connection to retry
140 * if all has already been tried. Updates the connection state to
141 * @connection_state. */
142 void connect_socket(struct socket
*socket
, enum connection_state state
);
144 /* Used by the SSL layer when negotiating. */
145 void dns_exception(struct socket
*socket
);
148 /* Reading and writing to sockets: */
150 /* Reads data from @socket into @buffer. Calls @done each time new data is
152 void read_from_socket(struct socket
*socket
, struct read_buffer
*buffer
,
153 enum connection_state state
, socket_read_T done
);
155 /* Writes @datalen bytes from @data buffer to the passed @socket. When all data
156 * is written the @done callback will be called. */
157 void write_to_socket(struct socket
*socket
,
158 unsigned char *data
, int datalen
,
159 enum connection_state state
, socket_write_T write_done
);
161 /* Send request and get response. */
162 void request_from_socket(struct socket
*socket
, unsigned char *data
, int datalen
,
163 enum connection_state state
, enum socket_state sock_state
,
164 socket_read_T read_done
);
166 /* Initialize a read buffer. */
167 struct read_buffer
*alloc_read_buffer(struct socket
*socket
);
169 /* Remove @bytes number of bytes from @buffer. */
170 void kill_buffer_data(struct read_buffer
*buffer
, int bytes
);