blob: 6bce8fe52d3c3af1de0970e648e154a547073d03 [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001#include "private-libwebsockets.h"
Andy Green4739e5c2011-01-22 12:51:57 +00002
Markus Elfring75212332013-10-26 20:23:00 +08003struct libwebsocket *libwebsocket_client_connect_2(
Andy Greena41314f2011-05-23 10:00:03 +01004 struct libwebsocket_context *context,
5 struct libwebsocket *wsi
6) {
7 struct pollfd pfd;
Andy Greena41314f2011-05-23 10:00:03 +01008 struct hostent *server_hostent;
9 struct sockaddr_in server_addr;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +010010 struct sockaddr_in client_addr;
Andy Greena41314f2011-05-23 10:00:03 +010011 int n;
12 int plen = 0;
Andy Greene77fb802013-02-11 13:04:45 +080013 const char *ads;
Andy Greena41314f2011-05-23 10:00:03 +010014
Markus Elfring75212332013-10-26 20:23:00 +080015 lwsl_client("libwebsocket_client_connect_2\n");
Andy Greena41314f2011-05-23 10:00:03 +010016
17 /*
18 * proxy?
19 */
20
21 if (context->http_proxy_port) {
Andy Greene48ba312013-02-10 15:34:59 +080022 plen = sprintf((char *)context->service_buffer,
Andy Greenf54a94b2013-02-10 15:19:39 +080023 "CONNECT %s:%u HTTP/1.0\x0d\x0a"
Andy Greena41314f2011-05-23 10:00:03 +010024 "User-agent: libwebsockets\x0d\x0a"
25/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
Andy Greene77fb802013-02-11 13:04:45 +080026 "\x0d\x0a",
27 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
Andy Greena7521de2013-02-18 10:38:45 +080028 wsi->u.hdr.ah->c_port);
Andy Green36efd822013-10-25 22:07:57 +080029 ads = context->http_proxy_address;
30 server_addr.sin_port = htons(context->http_proxy_port);
31 } else {
32 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
33 server_addr.sin_port = htons(wsi->u.hdr.ah->c_port);
Andy Greena41314f2011-05-23 10:00:03 +010034 }
35
36 /*
37 * prepare the actual connection (to the proxy, if any)
38 */
Markus Elfring75212332013-10-26 20:23:00 +080039 lwsl_client("libwebsocket_client_connect_2: address %s\n", ads);
Andy Greene77fb802013-02-11 13:04:45 +080040
41 server_hostent = gethostbyname(ads);
Andy Greena41314f2011-05-23 10:00:03 +010042 if (server_hostent == NULL) {
Andy Greene77fb802013-02-11 13:04:45 +080043 lwsl_err("Unable to get host name from %s\n", ads);
Andy Greena41314f2011-05-23 10:00:03 +010044 goto oom4;
45 }
46
Andy Greena41314f2011-05-23 10:00:03 +010047 if (wsi->sock < 0) {
Andy Green5dc62ea2013-09-20 20:26:12 +080048
49 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
50
51 if (wsi->sock < 0) {
52 lwsl_warn("Unable to open socket\n");
53 goto oom4;
54 }
55
56 if (lws_set_socket_options(context, wsi->sock)) {
57 lwsl_err("Failed to set wsi socket options\n");
58 compatible_close(wsi->sock);
59 goto oom4;
60 }
61
62 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
63
64 insert_wsi_socket_into_fds(context, wsi);
65
66 libwebsocket_set_timeout(wsi,
67 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
68 AWAITING_TIMEOUT);
Mattias Lundberg03bb8f92014-02-18 10:06:57 +010069
70 bzero((char *) &client_addr, sizeof(client_addr));
71 client_addr.sin_family = AF_INET;
72
73 if (context->iface != NULL) {
74 if (interface_to_sa(context->iface, &client_addr,
75 sizeof(client_addr)) < 0) {
76 lwsl_err("Unable to find interface %s\n", context->iface);
77 compatible_close(wsi->sock);
78 goto failed;
79 }
80
81 if (bind(wsi->sock, (struct sockaddr *) &client_addr,
82 sizeof(client_addr)) < 0) {
83 lwsl_err("Error binding to interface %s", context->iface);
84 compatible_close(wsi->sock);
85 goto failed;
86 }
87 }
Andy Greena41314f2011-05-23 10:00:03 +010088 }
89
90 server_addr.sin_family = AF_INET;
Andy Greena41314f2011-05-23 10:00:03 +010091 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
Andy Green36efd822013-10-25 22:07:57 +080092
Andy Greena41314f2011-05-23 10:00:03 +010093 bzero(&server_addr.sin_zero, 8);
94
Andy Greena41314f2011-05-23 10:00:03 +010095 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
Andy Green68114572013-10-24 22:07:04 +080096 sizeof(struct sockaddr)) == -1 || errno == EISCONN) {
Andy Green5dc62ea2013-09-20 20:26:12 +080097
98 if (errno == EALREADY || errno == EINPROGRESS) {
99 lwsl_client("nonblocking connect retry\n");
100
101 /*
102 * must do specifically a POLLOUT poll to hear
103 * about the connect completion
104 */
Andy Green91f19d82013-12-21 11:18:34 +0800105 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green5dc62ea2013-09-20 20:26:12 +0800106
107 return wsi;
108 }
109
shys5efcb3f2013-10-25 15:49:11 +0200110 if (errno != EISCONN) {
111
112 lwsl_debug("Connect failed errno=%d\n", errno);
113 goto failed;
114 }
Andy Greena41314f2011-05-23 10:00:03 +0100115 }
116
Andy Green43db0452013-01-10 19:50:35 +0800117 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +0100118
Andy Greena41314f2011-05-23 10:00:03 +0100119 /* we are connected to server, or proxy */
120
121 if (context->http_proxy_port) {
122
Andy Green36efd822013-10-25 22:07:57 +0800123 /* OK from now on we talk via the proxy, so connect to that */
124
125 /*
126 * (will overwrite existing pointer,
127 * leaving old string/frag there but unreferenced)
128 */
129 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
130 context->http_proxy_address))
131 goto failed;
132 wsi->u.hdr.ah->c_port = context->http_proxy_port;
133
Joachim Bauch8294c1f2013-06-29 10:22:09 +0800134 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
Andy Greena41314f2011-05-23 10:00:03 +0100135 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800136 lwsl_debug("ERROR writing to proxy socket\n");
Andy Green5dc62ea2013-09-20 20:26:12 +0800137 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100138 }
139
140 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800141 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
142 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100143
144 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
145
146 return wsi;
147 }
148
149 /*
150 * provoke service to issue the handshake directly
151 * we need to do it this way because in the proxy case, this is the
152 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800153 * response inside the state machine... but notice in SSL case this
154 * may not have sent anything yet with 0 return, and won't until some
155 * many retries from main loop. To stop that becoming endless,
156 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100157 */
158
Andy Green73abc252013-01-13 11:05:30 +0800159 libwebsocket_set_timeout(wsi,
160 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
161
Andy Greena41314f2011-05-23 10:00:03 +0100162 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
163 pfd.fd = wsi->sock;
164 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800165
166 n = libwebsocket_service_fd(context, &pfd);
167
168 if (n < 0)
Andy Green5dc62ea2013-09-20 20:26:12 +0800169 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100170
David Galeano36750b82013-01-09 16:17:04 +0800171 if (n) /* returns 1 on failure after closing wsi */
172 return NULL;
173
Andy Greena41314f2011-05-23 10:00:03 +0100174 return wsi;
175
176oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800177 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100178 free(wsi);
Andy Green5dc62ea2013-09-20 20:26:12 +0800179 return NULL;
Andy Greena41314f2011-05-23 10:00:03 +0100180
Andy Green5dc62ea2013-09-20 20:26:12 +0800181failed:
182 libwebsocket_close_and_free_session(context, wsi,
183 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena41314f2011-05-23 10:00:03 +0100184 return NULL;
185}
186
Andy Green90c7cbc2011-01-27 06:26:52 +0000187/**
188 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000189 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000190 * @address: Remote server address, eg, "myserver.com"
191 * @port: Port to connect to on the remote server, eg, 80
192 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
193 * signed certs
194 * @path: Websocket path on server
195 * @host: Hostname on server
196 * @origin: Socket origin name
197 * @protocol: Comma-separated list of protocols being asked for from
198 * the server, or just one. The server will pick the one it
199 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000200 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800201 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000202 *
203 * This function creates a connection to a remote server
204 */
205
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800206LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000207libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000208 const char *address,
209 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000210 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000211 const char *path,
212 const char *host,
213 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000214 const char *protocol,
215 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000216{
Andy Green4739e5c2011-01-22 12:51:57 +0000217 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800218#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800219 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100220 int m;
221 struct libwebsocket_extension *ext;
222 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800223#endif
224
Andy Greenbe93fef2011-02-14 20:25:43 +0000225#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000226 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800227 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000228 return NULL;
229 }
230#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000231
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800232 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000233 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800234 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000235
Andy Greenb5b23192013-02-11 17:13:32 +0800236 memset(wsi, 0, sizeof(*wsi));
Andy Green5dc62ea2013-09-20 20:26:12 +0800237 wsi->sock = -1;
Darin Willitsc19456f2011-02-14 17:52:39 +0000238
Andy Greenbfb051f2011-02-09 08:49:14 +0000239 /* -1 means just use latest supported */
240
241 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000242 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000243
244 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000245 wsi->user_space = NULL;
246 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000247 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000248 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800249#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000250 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800251#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000252#ifdef LWS_OPENSSL_SUPPORT
253 wsi->use_ssl = ssl_connection;
254#endif
255
Andy Green16ab3182013-02-10 18:02:31 +0800256 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800257 goto bail;
258
259 /*
260 * we're not necessarily in a position to action these right away,
261 * stash them... we only need during connect phase so u.hdr is fine
262 */
Andy Greena7521de2013-02-18 10:38:45 +0800263 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800264 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
265 goto bail1;
266
267 /* these only need u.hdr lifetime as well */
268
269 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
270 goto bail1;
271
272 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
273 goto bail1;
274
275 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800276 if (lws_hdr_simple_create(wsi,
277 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800278 goto bail1;
279 /*
280 * this is a list of protocols we tell the server we're okay with
281 * stash it for later when we compare server response with it
282 */
283 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800284 if (lws_hdr_simple_create(wsi,
285 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800286 goto bail1;
287
288 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000289
Andy Green3182ece2013-01-20 17:08:31 +0800290#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000291 /*
Andy Greena41314f2011-05-23 10:00:03 +0100292 * Check with each extension if it is able to route and proxy this
293 * connection for us. For example, an extension like x-google-mux
294 * can handle this and then we don't need an actual socket for this
295 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000296 */
297
Andy Greena41314f2011-05-23 10:00:03 +0100298 handled = 0;
299 ext = context->extensions;
300 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000301
Andy Greena41314f2011-05-23 10:00:03 +0100302 while (ext && ext->callback && !handled) {
303 m = ext->callback(context, ext, wsi,
304 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
305 (void *)(long)n, (void *)address, port);
306 if (m)
307 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000308
Andy Greena41314f2011-05-23 10:00:03 +0100309 ext++;
310 n++;
Andy Green9659f372011-01-27 22:01:43 +0000311 }
312
Andy Greena41314f2011-05-23 10:00:03 +0100313 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800314 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000315
Andy Greenbe93fef2011-02-14 20:25:43 +0000316 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800317 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
318 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000319
Andy Greena41314f2011-05-23 10:00:03 +0100320 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000321 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000322 }
Andy Green3182ece2013-01-20 17:08:31 +0800323#endif
Andy Green43db0452013-01-10 19:50:35 +0800324 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000325
Markus Elfring75212332013-10-26 20:23:00 +0800326 return libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000327
Andy Green4739e5c2011-01-22 12:51:57 +0000328bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800329 free(wsi->u.hdr.ah);
330bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000331 free(wsi);
332
333 return NULL;
334}
David Brooks2c60d952012-04-20 12:19:01 +0800335
336
337/**
338 * libwebsocket_client_connect_extended() - Connect to another websocket server
339 * @context: Websocket context
340 * @address: Remote server address, eg, "myserver.com"
341 * @port: Port to connect to on the remote server, eg, 80
342 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
343 * signed certs
344 * @path: Websocket path on server
345 * @host: Hostname on server
346 * @origin: Socket origin name
347 * @protocol: Comma-separated list of protocols being asked for from
348 * the server, or just one. The server will pick the one it
349 * likes best.
350 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800351 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800352 * @userdata: Pre-allocated user data
353 *
354 * This function creates a connection to a remote server
355 */
356
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800357LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800358libwebsocket_client_connect_extended(struct libwebsocket_context *context,
359 const char *address,
360 int port,
361 int ssl_connection,
362 const char *path,
363 const char *host,
364 const char *origin,
365 const char *protocol,
366 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800367 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800368{
369 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800370 libwebsocket_client_connect(context, address, port,
371 ssl_connection, path, host, origin, protocol,
372 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800373
374 if (ws && !ws->user_space && userdata)
375 ws->user_space = userdata ;
376
377 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800378}