1 #ifndef TCP_INTERNAL_H |
|
2 #define TCP_INTERNAL_H |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * |
|
7 * Internal TCP interface for implementations |
|
8 */ |
|
9 #include "tcp.h" |
|
10 #include "resolve.h" |
|
11 #include "transport_fd.h" |
|
12 #include "transport_internal.h" |
|
13 #include "service_internal.h" |
|
14 #include "error.h" |
|
15 |
|
16 #include <event2/event.h> |
|
17 #include <event2/util.h> |
|
18 |
|
19 /** |
|
20 * Create a new socket() using the given addr's family/socktype/protocol and return it. |
|
21 * |
|
22 * In case of errors, this returns -err_t |
|
23 * |
|
24 * @param addr the addrinfo to create the socket for |
|
25 * @param err returned error info |
|
26 * @return new fd on success, -err_t on error |
|
27 */ |
|
28 int tcp_sock_create (const struct addrinfo *addr, error_t *err); |
|
29 |
|
30 /** |
|
31 * Return the socket's current error code via err->extra. |
|
32 * |
|
33 * In case getting the socket error code itself fails, this will return normal error code/info. |
|
34 * |
|
35 * Otherwise, this will return SUCCESS, with the errno value stored in err->extra. |
|
36 */ |
|
37 err_t tcp_sock_error (evutil_socket_t sock, error_t *err); |
|
38 |
|
39 |
|
40 /** |
|
41 * TCP transport type |
|
42 */ |
|
43 extern const struct transport_type tcp_transport_type; |
|
44 |
|
45 /** |
|
46 * Base TCP transport |
|
47 * |
|
48 * XXX: currently just the same as transport_fd, but this will probably change |
|
49 */ |
|
50 struct tcp_transport { |
|
51 /** Base FD-based implementation */ |
|
52 struct transport_fd base_fd; |
|
53 }; |
|
54 |
|
55 /** |
|
56 * Initialize the tcp_transport state. |
|
57 * |
|
58 * This initializes the transport_fd base using the global sock_ctx::ev_base and the given socket. |
|
59 */ |
|
60 void tcp_transport_init (struct tcp_transport *trans, evutil_socket_t sock); |
|
61 |
|
62 /** |
|
63 * Create a new tcp_transport with the given sock. |
|
64 * |
|
65 * For convenience, this will also make the sock nonblocking. |
|
66 * |
|
67 * In case of errors, this will the socket. |
|
68 * |
|
69 * @param trans_ptr returned tcp_transport |
|
70 * @param info the transport user settings |
|
71 * @param sock the unused TCP socket |
|
72 * @param err returned error info |
|
73 */ |
|
74 err_t tcp_transport_create (struct tcp_transport **trans_ptr, const struct transport_info *info, evutil_socket_t sock, error_t *err); |
|
75 |
|
76 /** |
|
77 * The transport as now connected, this sets up the intitial user settings, and invokes the callback. |
|
78 * |
|
79 * XXX: this does an 'indirect' call to transport_connected(). |
|
80 * |
|
81 * @param err returned error info |
|
82 */ |
|
83 err_t tcp_transport_connected (struct tcp_transport *trans, error_t *err); |
|
84 |
|
85 /** |
|
86 * Deinitialize the transport state, terminating the connection and releasing resources. |
|
87 */ |
|
88 void tcp_transport_deinit (struct tcp_transport *trans); |
|
89 |
|
90 /** |
|
91 * Deinitialize and free the given tcp_transport |
|
92 */ |
|
93 void tcp_transport_destroy (struct tcp_transport *trans); |
|
94 |
|
95 /** |
|
96 * TCP client transport type |
|
97 */ |
|
98 extern const struct transport_type tcp_client_type; |
|
99 |
|
100 /** |
|
101 * TCP client state |
|
102 */ |
|
103 struct tcp_client { |
|
104 /** Base transport stuff */ |
|
105 struct tcp_transport base_trans; |
|
106 |
|
107 /** The resolver lookup result for the async connect process */ |
|
108 struct resolve_result rr; |
|
109 }; |
|
110 |
|
111 /** |
|
112 * Initialize the tcp_client state |
|
113 */ |
|
114 void tcp_client_init (struct tcp_client *client); |
|
115 |
|
116 /** |
|
117 * Attempt to connect asyncronously to the given hostname/service. Once a connection has been established, this will |
|
118 * call transport_connected(), so you can register transport_methods::_connected if layering on top of TCP. |
|
119 * |
|
120 * In case of errors while starting the async connect process, an error code will be returned. If the connect fails |
|
121 * later on, transport_connected() will be called with the error code. |
|
122 * |
|
123 * The sock must have been initialized using sock_tcp_init(). |
|
124 * |
|
125 * @param client the unconnected TCP client socket to connect with |
|
126 * @param hostname the hostname to resolve |
|
127 * @param service the service to connect to |
|
128 * @param err returned error info for immediate errors |
|
129 */ |
|
130 err_t tcp_client_connect_async (struct tcp_client *client, const char *hostname, const char *service, error_t *err); |
|
131 |
|
132 /** |
|
133 * Deinitialize the tcp_client's state, including the tcp_transport state. |
|
134 */ |
|
135 void tcp_client_deinit (struct tcp_client *client); |
|
136 |
|
137 |
|
138 |
|
139 /** |
|
140 * TCP service type |
|
141 */ |
|
142 extern const struct service_type tcp_server_type; |
|
143 |
|
144 /** |
|
145 * TCP service state |
|
146 */ |
|
147 struct tcp_server { |
|
148 /** Base service state */ |
|
149 struct service base_service; |
|
150 |
|
151 /** The input event with our listen() socket */ |
|
152 struct event *ev; |
|
153 }; |
|
154 |
|
155 /** |
|
156 * The listen() backlog |
|
157 */ |
|
158 #define TCP_SERVER_BACKLOG 5 |
|
159 |
|
160 /** |
|
161 * Open the listening socket on the given interface/service. |
|
162 */ |
|
163 err_t tcp_server_listen (struct tcp_server *serv, const char *interface, const char *service, error_t *err); |
|
164 |
|
165 /** |
|
166 * Release the tcp_server's state, and cleanup the struct. |
|
167 */ |
|
168 void tcp_server_deinit (struct tcp_server *serv); |
|
169 |
|
170 #endif /* TCP_INTERNAL_H */ |
|