blob: a241c90ccc4af845c108abe5446d6aed149c4d81 [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001#include "private-libwebsockets.h"
Andy Green4739e5c2011-01-22 12:51:57 +00002
Andy Green6ee372f2012-04-09 15:09:01 +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
Andy Green43db0452013-01-10 19:50:35 +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 */
Andy Greene77fb802013-02-11 13:04:45 +080038 lwsl_client("__libwebsocket_client_connect_2: address %s\n", ads);
39
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 */
85
86 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
87
88 /* external POLL support via protocol 0 */
89 context->protocols[0].callback(context, wsi,
90 LWS_CALLBACK_SET_MODE_POLL_FD,
91 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
92
93 return wsi;
94 }
95
96 lwsl_debug("Connect failed errno=%d\n", errno);
97 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +010098 }
99
Andy Green43db0452013-01-10 19:50:35 +0800100 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +0100101
Andy Greena41314f2011-05-23 10:00:03 +0100102 /* we are connected to server, or proxy */
103
104 if (context->http_proxy_port) {
105
Andy Green36efd822013-10-25 22:07:57 +0800106 /* OK from now on we talk via the proxy, so connect to that */
107
108 /*
109 * (will overwrite existing pointer,
110 * leaving old string/frag there but unreferenced)
111 */
112 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
113 context->http_proxy_address))
114 goto failed;
115 wsi->u.hdr.ah->c_port = context->http_proxy_port;
116
Joachim Bauch8294c1f2013-06-29 10:22:09 +0800117 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
Andy Greena41314f2011-05-23 10:00:03 +0100118 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800119 lwsl_debug("ERROR writing to proxy socket\n");
Andy Green5dc62ea2013-09-20 20:26:12 +0800120 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100121 }
122
123 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800124 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
125 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100126
127 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
128
129 return wsi;
130 }
131
132 /*
133 * provoke service to issue the handshake directly
134 * we need to do it this way because in the proxy case, this is the
135 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800136 * response inside the state machine... but notice in SSL case this
137 * may not have sent anything yet with 0 return, and won't until some
138 * many retries from main loop. To stop that becoming endless,
139 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100140 */
141
Andy Green73abc252013-01-13 11:05:30 +0800142 libwebsocket_set_timeout(wsi,
143 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
144
Andy Greena41314f2011-05-23 10:00:03 +0100145 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
146 pfd.fd = wsi->sock;
147 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800148
149 n = libwebsocket_service_fd(context, &pfd);
150
151 if (n < 0)
Andy Green5dc62ea2013-09-20 20:26:12 +0800152 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100153
David Galeano36750b82013-01-09 16:17:04 +0800154 if (n) /* returns 1 on failure after closing wsi */
155 return NULL;
156
Andy Greena41314f2011-05-23 10:00:03 +0100157 return wsi;
158
159oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800160 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100161 free(wsi);
Andy Green5dc62ea2013-09-20 20:26:12 +0800162 return NULL;
Andy Greena41314f2011-05-23 10:00:03 +0100163
Andy Green5dc62ea2013-09-20 20:26:12 +0800164failed:
165 libwebsocket_close_and_free_session(context, wsi,
166 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena41314f2011-05-23 10:00:03 +0100167 return NULL;
168}
169
Andy Green90c7cbc2011-01-27 06:26:52 +0000170/**
171 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000172 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000173 * @address: Remote server address, eg, "myserver.com"
174 * @port: Port to connect to on the remote server, eg, 80
175 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
176 * signed certs
177 * @path: Websocket path on server
178 * @host: Hostname on server
179 * @origin: Socket origin name
180 * @protocol: Comma-separated list of protocols being asked for from
181 * the server, or just one. The server will pick the one it
182 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000183 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800184 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000185 *
186 * This function creates a connection to a remote server
187 */
188
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800189LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000190libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000191 const char *address,
192 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000193 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000194 const char *path,
195 const char *host,
196 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000197 const char *protocol,
198 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000199{
Andy Green4739e5c2011-01-22 12:51:57 +0000200 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800201#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800202 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100203 int m;
204 struct libwebsocket_extension *ext;
205 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800206#endif
207
Andy Greenbe93fef2011-02-14 20:25:43 +0000208#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000209 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800210 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000211 return NULL;
212 }
213#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000214
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800215 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000216 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800217 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000218
Andy Greenb5b23192013-02-11 17:13:32 +0800219 memset(wsi, 0, sizeof(*wsi));
Andy Green5dc62ea2013-09-20 20:26:12 +0800220 wsi->sock = -1;
Darin Willitsc19456f2011-02-14 17:52:39 +0000221
Andy Greenbfb051f2011-02-09 08:49:14 +0000222 /* -1 means just use latest supported */
223
224 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000225 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000226
227 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000228 wsi->user_space = NULL;
229 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000230 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000231 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800232#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000233 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800234#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000235#ifdef LWS_OPENSSL_SUPPORT
236 wsi->use_ssl = ssl_connection;
237#endif
238
Andy Green16ab3182013-02-10 18:02:31 +0800239 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800240 goto bail;
241
242 /*
243 * we're not necessarily in a position to action these right away,
244 * stash them... we only need during connect phase so u.hdr is fine
245 */
Andy Greena7521de2013-02-18 10:38:45 +0800246 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800247 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
248 goto bail1;
249
250 /* these only need u.hdr lifetime as well */
251
252 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
253 goto bail1;
254
255 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
256 goto bail1;
257
258 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800259 if (lws_hdr_simple_create(wsi,
260 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800261 goto bail1;
262 /*
263 * this is a list of protocols we tell the server we're okay with
264 * stash it for later when we compare server response with it
265 */
266 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800267 if (lws_hdr_simple_create(wsi,
268 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800269 goto bail1;
270
271 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000272
Andy Green3182ece2013-01-20 17:08:31 +0800273#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000274 /*
Andy Greena41314f2011-05-23 10:00:03 +0100275 * Check with each extension if it is able to route and proxy this
276 * connection for us. For example, an extension like x-google-mux
277 * can handle this and then we don't need an actual socket for this
278 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000279 */
280
Andy Greena41314f2011-05-23 10:00:03 +0100281 handled = 0;
282 ext = context->extensions;
283 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000284
Andy Greena41314f2011-05-23 10:00:03 +0100285 while (ext && ext->callback && !handled) {
286 m = ext->callback(context, ext, wsi,
287 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
288 (void *)(long)n, (void *)address, port);
289 if (m)
290 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000291
Andy Greena41314f2011-05-23 10:00:03 +0100292 ext++;
293 n++;
Andy Green9659f372011-01-27 22:01:43 +0000294 }
295
Andy Greena41314f2011-05-23 10:00:03 +0100296 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800297 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000298
Andy Greenbe93fef2011-02-14 20:25:43 +0000299 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800300 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
301 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000302
Andy Greena41314f2011-05-23 10:00:03 +0100303 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000304 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000305 }
Andy Green3182ece2013-01-20 17:08:31 +0800306#endif
Andy Green43db0452013-01-10 19:50:35 +0800307 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000308
Andy Greena41314f2011-05-23 10:00:03 +0100309 return __libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000310
Andy Green4739e5c2011-01-22 12:51:57 +0000311bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800312 free(wsi->u.hdr.ah);
313bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000314 free(wsi);
315
316 return NULL;
317}
David Brooks2c60d952012-04-20 12:19:01 +0800318
319
320/**
321 * libwebsocket_client_connect_extended() - Connect to another websocket server
322 * @context: Websocket context
323 * @address: Remote server address, eg, "myserver.com"
324 * @port: Port to connect to on the remote server, eg, 80
325 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
326 * signed certs
327 * @path: Websocket path on server
328 * @host: Hostname on server
329 * @origin: Socket origin name
330 * @protocol: Comma-separated list of protocols being asked for from
331 * the server, or just one. The server will pick the one it
332 * likes best.
333 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800334 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800335 * @userdata: Pre-allocated user data
336 *
337 * This function creates a connection to a remote server
338 */
339
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800340LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800341libwebsocket_client_connect_extended(struct libwebsocket_context *context,
342 const char *address,
343 int port,
344 int ssl_connection,
345 const char *path,
346 const char *host,
347 const char *origin,
348 const char *protocol,
349 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800350 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800351{
352 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800353 libwebsocket_client_connect(context, address, port,
354 ssl_connection, path, host, origin, protocol,
355 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800356
357 if (ws && !ws->user_space && userdata)
358 ws->user_space = userdata ;
359
360 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800361}