packaging: support smack manifest and cleanup
[profile/ivi/libwebsockets.git] / lib / client-handshake.c
1 #include "private-libwebsockets.h"
2
3 struct libwebsocket *__libwebsocket_client_connect_2(
4         struct libwebsocket_context *context,
5         struct libwebsocket *wsi
6 ) {
7         struct pollfd pfd;
8         struct hostent *server_hostent;
9         struct sockaddr_in server_addr;
10         int n;
11         int plen = 0;
12         const char *ads;
13
14         lwsl_client("__libwebsocket_client_connect_2\n");
15
16         /*
17          * proxy?
18          */
19
20         if (context->http_proxy_port) {
21                 plen = sprintf((char *)context->service_buffer,
22                         "CONNECT %s:%u HTTP/1.0\x0d\x0a"
23                         "User-agent: libwebsockets\x0d\x0a"
24 /*Proxy-authorization: basic aGVsbG86d29ybGQ= */
25                         "\x0d\x0a",
26                         lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
27                         wsi->u.hdr.ah->c_port);
28
29                 /* OK from now on we talk via the proxy, so connect to that */
30
31                 /*
32                  * (will overwrite existing pointer,
33                  * leaving old string/frag there but unreferenced)
34                  */
35                 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
36                                                    context->http_proxy_address))
37                         goto oom4;
38                 wsi->u.hdr.ah->c_port = context->http_proxy_port;
39         }
40
41         /*
42          * prepare the actual connection (to the proxy, if any)
43          */
44
45         ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
46
47         lwsl_client("__libwebsocket_client_connect_2: address %s\n", ads);
48
49         server_hostent = gethostbyname(ads);
50         if (server_hostent == NULL) {
51                 lwsl_err("Unable to get host name from %s\n", ads);
52                 goto oom4;
53         }
54
55         wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
56
57         if (wsi->sock < 0) {
58                 lwsl_warn("Unable to open socket\n");
59                 goto oom4;
60         }
61
62         server_addr.sin_family = AF_INET;
63         server_addr.sin_port = htons(wsi->u.hdr.ah->c_port);
64         server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
65         bzero(&server_addr.sin_zero, 8);
66
67         if (connect(wsi->sock, (struct sockaddr *)&server_addr,
68                                              sizeof(struct sockaddr)) == -1)  {
69                 lwsl_debug("Connect failed\n");
70                 compatible_close(wsi->sock);
71                 goto oom4;
72         }
73
74         lwsl_client("connected\n");
75
76         if (lws_set_socket_options(context, wsi->sock)) {
77                 lwsl_err("Failed to set wsi socket options\n");
78                 close(wsi->sock);
79                 goto oom4;
80         }
81
82         insert_wsi_socket_into_fds(context, wsi);
83
84         /* we are connected to server, or proxy */
85
86         if (context->http_proxy_port) {
87
88                 n = send(wsi->sock, context->service_buffer, plen, 0);
89                 if (n < 0) {
90                         compatible_close(wsi->sock);
91                         lwsl_debug("ERROR writing to proxy socket\n");
92                         goto oom4;
93                 }
94
95                 libwebsocket_set_timeout(wsi,
96                         PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
97                                                               AWAITING_TIMEOUT);
98
99                 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
100
101                 return wsi;
102         }
103
104         /*
105          * provoke service to issue the handshake directly
106          * we need to do it this way because in the proxy case, this is the
107          * next state and executed only if and when we get a good proxy
108          * response inside the state machine... but notice in SSL case this
109          * may not have sent anything yet with 0 return, and won't until some
110          * many retries from main loop.  To stop that becoming endless,
111          * cover with a timeout.
112          */
113
114         libwebsocket_set_timeout(wsi,
115                 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
116
117         wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
118         pfd.fd = wsi->sock;
119         pfd.revents = POLLIN;
120
121         n = libwebsocket_service_fd(context, &pfd);
122
123         if (n < 0)
124                 goto oom4;
125
126         if (n) /* returns 1 on failure after closing wsi */
127                 return NULL;
128
129         return wsi;
130
131 oom4:
132         free(wsi->u.hdr.ah);
133         free(wsi);
134
135         return NULL;
136 }
137
138 /**
139  * libwebsocket_client_connect() - Connect to another websocket server
140  * @context:    Websocket context
141  * @address:    Remote server address, eg, "myserver.com"
142  * @port:       Port to connect to on the remote server, eg, 80
143  * @ssl_connection:     0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
144  *                      signed certs
145  * @path:       Websocket path on server
146  * @host:       Hostname on server
147  * @origin:     Socket origin name
148  * @protocol:   Comma-separated list of protocols being asked for from
149  *              the server, or just one.  The server will pick the one it
150  *              likes best.
151  * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
152  *              protocol supported, or the specific protocol ordinal
153  *
154  *      This function creates a connection to a remote server
155  */
156
157 struct libwebsocket *
158 libwebsocket_client_connect(struct libwebsocket_context *context,
159                               const char *address,
160                               int port,
161                               int ssl_connection,
162                               const char *path,
163                               const char *host,
164                               const char *origin,
165                               const char *protocol,
166                               int ietf_version_or_minus_one)
167 {
168         struct libwebsocket *wsi;
169 #ifndef LWS_NO_EXTENSIONS
170         int n;
171         int m;
172         struct libwebsocket_extension *ext;
173         int handled;
174 #endif
175
176 #ifndef LWS_OPENSSL_SUPPORT
177         if (ssl_connection) {
178                 lwsl_err("libwebsockets not configured for ssl\n");
179                 return NULL;
180         }
181 #endif
182
183         wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
184         if (wsi == NULL)
185                 goto bail;
186
187         memset(wsi, 0, sizeof(*wsi));
188
189         /* -1 means just use latest supported */
190
191         if (ietf_version_or_minus_one == -1)
192                 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
193
194         wsi->ietf_spec_revision = ietf_version_or_minus_one;
195         wsi->user_space = NULL;
196         wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
197         wsi->protocol = NULL;
198         wsi->pending_timeout = NO_PENDING_TIMEOUT;
199 #ifndef LWS_NO_EXTENSIONS
200         wsi->count_active_extensions = 0;
201 #endif
202 #ifdef LWS_OPENSSL_SUPPORT
203         wsi->use_ssl = ssl_connection;
204 #endif
205
206         if (lws_allocate_header_table(wsi))
207                 goto bail;
208
209         /*
210          * we're not necessarily in a position to action these right away,
211          * stash them... we only need during connect phase so u.hdr is fine
212          */
213         wsi->u.hdr.ah->c_port = port;
214         if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
215                 goto bail1;
216
217         /* these only need u.hdr lifetime as well */
218
219         if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
220                 goto bail1;
221
222         if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
223                 goto bail1;
224
225         if (origin)
226                 if (lws_hdr_simple_create(wsi,
227                                 _WSI_TOKEN_CLIENT_ORIGIN, origin))
228                         goto bail1;
229         /*
230          * this is a list of protocols we tell the server we're okay with
231          * stash it for later when we compare server response with it
232          */
233         if (protocol)
234                 if (lws_hdr_simple_create(wsi,
235                                 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
236                         goto bail1;
237
238         wsi->protocol = &context->protocols[0];
239
240 #ifndef LWS_NO_EXTENSIONS
241         /*
242          * Check with each extension if it is able to route and proxy this
243          * connection for us.  For example, an extension like x-google-mux
244          * can handle this and then we don't need an actual socket for this
245          * connection.
246          */
247
248         handled = 0;
249         ext = context->extensions;
250         n = 0;
251
252         while (ext && ext->callback && !handled) {
253                 m = ext->callback(context, ext, wsi,
254                         LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
255                                  (void *)(long)n, (void *)address, port);
256                 if (m)
257                         handled = 1;
258
259                 ext++;
260                 n++;
261         }
262
263         if (handled) {
264                 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
265
266                 libwebsocket_set_timeout(wsi,
267                         PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
268                                                               AWAITING_TIMEOUT);
269
270                 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
271                 return wsi;
272         }
273 #endif
274         lwsl_client("libwebsocket_client_connect: direct conn\n");
275
276         return __libwebsocket_client_connect_2(context, wsi);
277
278 bail1:
279         free(wsi->u.hdr.ah);
280 bail:
281         free(wsi);
282
283         return NULL;
284 }
285
286
287 /**
288  * libwebsocket_client_connect_extended() - Connect to another websocket server
289  * @context:    Websocket context
290  * @address:    Remote server address, eg, "myserver.com"
291  * @port:       Port to connect to on the remote server, eg, 80
292  * @ssl_connection:     0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
293  *                      signed certs
294  * @path:       Websocket path on server
295  * @host:       Hostname on server
296  * @origin:     Socket origin name
297  * @protocol:   Comma-separated list of protocols being asked for from
298  *              the server, or just one.  The server will pick the one it
299  *              likes best.
300  * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
301  *              protocol supported, or the specific protocol ordinal
302  * @userdata: Pre-allocated user data
303  *
304  *      This function creates a connection to a remote server
305  */
306
307 struct libwebsocket *
308 libwebsocket_client_connect_extended(struct libwebsocket_context *context,
309                               const char *address,
310                               int port,
311                               int ssl_connection,
312                               const char *path,
313                               const char *host,
314                               const char *origin,
315                               const char *protocol,
316                               int ietf_version_or_minus_one,
317                               void *userdata)
318 {
319         struct libwebsocket *ws =
320                 libwebsocket_client_connect(context, address, port,
321                         ssl_connection, path, host, origin, protocol,
322                                                      ietf_version_or_minus_one);
323
324         if (ws && !ws->user_space && userdata)
325                 ws->user_space = userdata ;
326
327         return ws ;
328 }