blob: fc8ffb76acfe5de9451eef3868caaeff81010fb9 [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;
10 int n;
11 int plen = 0;
Andy Greene77fb802013-02-11 13:04:45 +080012 const char *ads;
Andy Greena41314f2011-05-23 10:00:03 +010013
Markus Elfring75212332013-10-26 20:23:00 +080014 lwsl_client("libwebsocket_client_connect_2\n");
Andy Greena41314f2011-05-23 10:00:03 +010015
16 /*
17 * proxy?
18 */
19
20 if (context->http_proxy_port) {
Andy Greene48ba312013-02-10 15:34:59 +080021 plen = sprintf((char *)context->service_buffer,
Andy Greenf54a94b2013-02-10 15:19:39 +080022 "CONNECT %s:%u HTTP/1.0\x0d\x0a"
Andy Greena41314f2011-05-23 10:00:03 +010023 "User-agent: libwebsockets\x0d\x0a"
24/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
Andy Greene77fb802013-02-11 13:04:45 +080025 "\x0d\x0a",
26 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
Andy Greena7521de2013-02-18 10:38:45 +080027 wsi->u.hdr.ah->c_port);
Andy Green36efd822013-10-25 22:07:57 +080028 ads = context->http_proxy_address;
29 server_addr.sin_port = htons(context->http_proxy_port);
30 } else {
31 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
32 server_addr.sin_port = htons(wsi->u.hdr.ah->c_port);
Andy Greena41314f2011-05-23 10:00:03 +010033 }
34
35 /*
36 * prepare the actual connection (to the proxy, if any)
37 */
Markus Elfring75212332013-10-26 20:23:00 +080038 lwsl_client("libwebsocket_client_connect_2: address %s\n", ads);
Andy Greene77fb802013-02-11 13:04:45 +080039
40 server_hostent = gethostbyname(ads);
Andy Greena41314f2011-05-23 10:00:03 +010041 if (server_hostent == NULL) {
Andy Greene77fb802013-02-11 13:04:45 +080042 lwsl_err("Unable to get host name from %s\n", ads);
Andy Greena41314f2011-05-23 10:00:03 +010043 goto oom4;
44 }
45
Andy Greena41314f2011-05-23 10:00:03 +010046 if (wsi->sock < 0) {
Andy Green5dc62ea2013-09-20 20:26:12 +080047
48 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
49
50 if (wsi->sock < 0) {
51 lwsl_warn("Unable to open socket\n");
52 goto oom4;
53 }
54
55 if (lws_set_socket_options(context, wsi->sock)) {
56 lwsl_err("Failed to set wsi socket options\n");
57 compatible_close(wsi->sock);
58 goto oom4;
59 }
60
61 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
62
63 insert_wsi_socket_into_fds(context, wsi);
64
65 libwebsocket_set_timeout(wsi,
66 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
67 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +010068 }
69
70 server_addr.sin_family = AF_INET;
Andy Greena41314f2011-05-23 10:00:03 +010071 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
Andy Green36efd822013-10-25 22:07:57 +080072
Andy Greena41314f2011-05-23 10:00:03 +010073 bzero(&server_addr.sin_zero, 8);
74
Andy Greena41314f2011-05-23 10:00:03 +010075 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
Andy Green68114572013-10-24 22:07:04 +080076 sizeof(struct sockaddr)) == -1 || errno == EISCONN) {
Andy Green5dc62ea2013-09-20 20:26:12 +080077
78 if (errno == EALREADY || errno == EINPROGRESS) {
79 lwsl_client("nonblocking connect retry\n");
80
81 /*
82 * must do specifically a POLLOUT poll to hear
83 * about the connect completion
84 */
Andy Green91f19d82013-12-21 11:18:34 +080085 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green5dc62ea2013-09-20 20:26:12 +080086
87 return wsi;
88 }
89
shys5efcb3f2013-10-25 15:49:11 +020090 if (errno != EISCONN) {
91
92 lwsl_debug("Connect failed errno=%d\n", errno);
93 goto failed;
94 }
Andy Greena41314f2011-05-23 10:00:03 +010095 }
96
Andy Green43db0452013-01-10 19:50:35 +080097 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +010098
Andy Greena41314f2011-05-23 10:00:03 +010099 /* we are connected to server, or proxy */
100
101 if (context->http_proxy_port) {
102
Andy Green36efd822013-10-25 22:07:57 +0800103 /* OK from now on we talk via the proxy, so connect to that */
104
105 /*
106 * (will overwrite existing pointer,
107 * leaving old string/frag there but unreferenced)
108 */
109 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
110 context->http_proxy_address))
111 goto failed;
112 wsi->u.hdr.ah->c_port = context->http_proxy_port;
113
Joachim Bauch8294c1f2013-06-29 10:22:09 +0800114 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
Andy Greena41314f2011-05-23 10:00:03 +0100115 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800116 lwsl_debug("ERROR writing to proxy socket\n");
Andy Green5dc62ea2013-09-20 20:26:12 +0800117 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100118 }
119
120 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800121 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
122 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100123
124 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
125
126 return wsi;
127 }
128
129 /*
130 * provoke service to issue the handshake directly
131 * we need to do it this way because in the proxy case, this is the
132 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800133 * response inside the state machine... but notice in SSL case this
134 * may not have sent anything yet with 0 return, and won't until some
135 * many retries from main loop. To stop that becoming endless,
136 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100137 */
138
Andy Green73abc252013-01-13 11:05:30 +0800139 libwebsocket_set_timeout(wsi,
140 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
141
Andy Greena41314f2011-05-23 10:00:03 +0100142 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
143 pfd.fd = wsi->sock;
144 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800145
146 n = libwebsocket_service_fd(context, &pfd);
147
148 if (n < 0)
Andy Green5dc62ea2013-09-20 20:26:12 +0800149 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100150
David Galeano36750b82013-01-09 16:17:04 +0800151 if (n) /* returns 1 on failure after closing wsi */
152 return NULL;
153
Andy Greena41314f2011-05-23 10:00:03 +0100154 return wsi;
155
156oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800157 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100158 free(wsi);
Andy Green5dc62ea2013-09-20 20:26:12 +0800159 return NULL;
Andy Greena41314f2011-05-23 10:00:03 +0100160
Andy Green5dc62ea2013-09-20 20:26:12 +0800161failed:
162 libwebsocket_close_and_free_session(context, wsi,
163 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena41314f2011-05-23 10:00:03 +0100164 return NULL;
165}
166
Andy Green90c7cbc2011-01-27 06:26:52 +0000167/**
168 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000169 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000170 * @address: Remote server address, eg, "myserver.com"
171 * @port: Port to connect to on the remote server, eg, 80
172 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
173 * signed certs
174 * @path: Websocket path on server
175 * @host: Hostname on server
176 * @origin: Socket origin name
177 * @protocol: Comma-separated list of protocols being asked for from
178 * the server, or just one. The server will pick the one it
179 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000180 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800181 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000182 *
183 * This function creates a connection to a remote server
184 */
185
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800186LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000187libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000188 const char *address,
189 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000190 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000191 const char *path,
192 const char *host,
193 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000194 const char *protocol,
195 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000196{
Andy Green4739e5c2011-01-22 12:51:57 +0000197 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800198#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800199 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100200 int m;
201 struct libwebsocket_extension *ext;
202 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800203#endif
204
Andy Greenbe93fef2011-02-14 20:25:43 +0000205#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000206 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800207 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000208 return NULL;
209 }
210#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000211
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800212 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000213 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800214 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000215
Andy Greenb5b23192013-02-11 17:13:32 +0800216 memset(wsi, 0, sizeof(*wsi));
Andy Green5dc62ea2013-09-20 20:26:12 +0800217 wsi->sock = -1;
Darin Willitsc19456f2011-02-14 17:52:39 +0000218
Andy Greenbfb051f2011-02-09 08:49:14 +0000219 /* -1 means just use latest supported */
220
221 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000222 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000223
224 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000225 wsi->user_space = NULL;
226 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000227 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000228 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800229#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000230 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800231#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000232#ifdef LWS_OPENSSL_SUPPORT
233 wsi->use_ssl = ssl_connection;
234#endif
235
Andy Green16ab3182013-02-10 18:02:31 +0800236 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800237 goto bail;
238
239 /*
240 * we're not necessarily in a position to action these right away,
241 * stash them... we only need during connect phase so u.hdr is fine
242 */
Andy Greena7521de2013-02-18 10:38:45 +0800243 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800244 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
245 goto bail1;
246
247 /* these only need u.hdr lifetime as well */
248
249 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
250 goto bail1;
251
252 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
253 goto bail1;
254
255 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800256 if (lws_hdr_simple_create(wsi,
257 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800258 goto bail1;
259 /*
260 * this is a list of protocols we tell the server we're okay with
261 * stash it for later when we compare server response with it
262 */
263 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800264 if (lws_hdr_simple_create(wsi,
265 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800266 goto bail1;
267
268 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000269
Andy Green3182ece2013-01-20 17:08:31 +0800270#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000271 /*
Andy Greena41314f2011-05-23 10:00:03 +0100272 * Check with each extension if it is able to route and proxy this
273 * connection for us. For example, an extension like x-google-mux
274 * can handle this and then we don't need an actual socket for this
275 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000276 */
277
Andy Greena41314f2011-05-23 10:00:03 +0100278 handled = 0;
279 ext = context->extensions;
280 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000281
Andy Greena41314f2011-05-23 10:00:03 +0100282 while (ext && ext->callback && !handled) {
283 m = ext->callback(context, ext, wsi,
284 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
285 (void *)(long)n, (void *)address, port);
286 if (m)
287 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000288
Andy Greena41314f2011-05-23 10:00:03 +0100289 ext++;
290 n++;
Andy Green9659f372011-01-27 22:01:43 +0000291 }
292
Andy Greena41314f2011-05-23 10:00:03 +0100293 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800294 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000295
Andy Greenbe93fef2011-02-14 20:25:43 +0000296 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800297 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
298 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000299
Andy Greena41314f2011-05-23 10:00:03 +0100300 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000301 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000302 }
Andy Green3182ece2013-01-20 17:08:31 +0800303#endif
Andy Green43db0452013-01-10 19:50:35 +0800304 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000305
Markus Elfring75212332013-10-26 20:23:00 +0800306 return libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000307
Andy Green4739e5c2011-01-22 12:51:57 +0000308bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800309 free(wsi->u.hdr.ah);
310bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000311 free(wsi);
312
313 return NULL;
314}
David Brooks2c60d952012-04-20 12:19:01 +0800315
316
317/**
318 * libwebsocket_client_connect_extended() - Connect to another websocket server
319 * @context: Websocket context
320 * @address: Remote server address, eg, "myserver.com"
321 * @port: Port to connect to on the remote server, eg, 80
322 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
323 * signed certs
324 * @path: Websocket path on server
325 * @host: Hostname on server
326 * @origin: Socket origin name
327 * @protocol: Comma-separated list of protocols being asked for from
328 * the server, or just one. The server will pick the one it
329 * likes best.
330 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800331 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800332 * @userdata: Pre-allocated user data
333 *
334 * This function creates a connection to a remote server
335 */
336
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800337LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800338libwebsocket_client_connect_extended(struct libwebsocket_context *context,
339 const char *address,
340 int port,
341 int ssl_connection,
342 const char *path,
343 const char *host,
344 const char *origin,
345 const char *protocol,
346 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800347 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800348{
349 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800350 libwebsocket_client_connect(context, address, port,
351 ssl_connection, path, host, origin, protocol,
352 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800353
354 if (ws && !ws->user_space && userdata)
355 ws->user_space = userdata ;
356
357 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800358}