blob: a501565690f0aa850d45f77a05e90dfaa983121c [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 Greena41314f2011-05-23 10:00:03 +010028
Andy Greene77fb802013-02-11 13:04:45 +080029 /* OK from now on we talk via the proxy, so connect to that */
Andy Greena41314f2011-05-23 10:00:03 +010030
Andy Greenb5b23192013-02-11 17:13:32 +080031 /*
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))
Andy Greene77fb802013-02-11 13:04:45 +080037 goto oom4;
Andy Greena7521de2013-02-18 10:38:45 +080038 wsi->u.hdr.ah->c_port = context->http_proxy_port;
Andy Greena41314f2011-05-23 10:00:03 +010039 }
40
41 /*
42 * prepare the actual connection (to the proxy, if any)
43 */
44
Andy Greene77fb802013-02-11 13:04:45 +080045 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
Andy Green66a16f32011-05-24 22:07:45 +010046
Andy Greene77fb802013-02-11 13:04:45 +080047 lwsl_client("__libwebsocket_client_connect_2: address %s\n", ads);
48
49 server_hostent = gethostbyname(ads);
Andy Greena41314f2011-05-23 10:00:03 +010050 if (server_hostent == NULL) {
Andy Greene77fb802013-02-11 13:04:45 +080051 lwsl_err("Unable to get host name from %s\n", ads);
Andy Greena41314f2011-05-23 10:00:03 +010052 goto oom4;
53 }
54
Andy Greena41314f2011-05-23 10:00:03 +010055 if (wsi->sock < 0) {
Andy Green5dc62ea2013-09-20 20:26:12 +080056
57 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
58
59 if (wsi->sock < 0) {
60 lwsl_warn("Unable to open socket\n");
61 goto oom4;
62 }
63
64 if (lws_set_socket_options(context, wsi->sock)) {
65 lwsl_err("Failed to set wsi socket options\n");
66 compatible_close(wsi->sock);
67 goto oom4;
68 }
69
70 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
71
72 insert_wsi_socket_into_fds(context, wsi);
73
74 libwebsocket_set_timeout(wsi,
75 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
76 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +010077 }
78
79 server_addr.sin_family = AF_INET;
Andy Greena7521de2013-02-18 10:38:45 +080080 server_addr.sin_port = htons(wsi->u.hdr.ah->c_port);
Andy Greena41314f2011-05-23 10:00:03 +010081 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
82 bzero(&server_addr.sin_zero, 8);
83
Andy Greena41314f2011-05-23 10:00:03 +010084 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
Andy Green6ee372f2012-04-09 15:09:01 +080085 sizeof(struct sockaddr)) == -1) {
Andy Green5dc62ea2013-09-20 20:26:12 +080086
87 if (errno == EALREADY || errno == EINPROGRESS) {
88 lwsl_client("nonblocking connect retry\n");
89
90 /*
91 * must do specifically a POLLOUT poll to hear
92 * about the connect completion
93 */
94
95 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
96
97 /* external POLL support via protocol 0 */
98 context->protocols[0].callback(context, wsi,
99 LWS_CALLBACK_SET_MODE_POLL_FD,
100 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
101
102 return wsi;
103 }
104
105 lwsl_debug("Connect failed errno=%d\n", errno);
106 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100107 }
108
Andy Green43db0452013-01-10 19:50:35 +0800109 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +0100110
Andy Greena41314f2011-05-23 10:00:03 +0100111 /* we are connected to server, or proxy */
112
113 if (context->http_proxy_port) {
114
Joachim Bauch8294c1f2013-06-29 10:22:09 +0800115 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
Andy Greena41314f2011-05-23 10:00:03 +0100116 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800117 lwsl_debug("ERROR writing to proxy socket\n");
Andy Green5dc62ea2013-09-20 20:26:12 +0800118 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100119 }
120
121 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800122 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
123 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100124
125 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
126
127 return wsi;
128 }
129
130 /*
131 * provoke service to issue the handshake directly
132 * we need to do it this way because in the proxy case, this is the
133 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800134 * response inside the state machine... but notice in SSL case this
135 * may not have sent anything yet with 0 return, and won't until some
136 * many retries from main loop. To stop that becoming endless,
137 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100138 */
139
Andy Green73abc252013-01-13 11:05:30 +0800140 libwebsocket_set_timeout(wsi,
141 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
142
Andy Greena41314f2011-05-23 10:00:03 +0100143 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
144 pfd.fd = wsi->sock;
145 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800146
147 n = libwebsocket_service_fd(context, &pfd);
148
149 if (n < 0)
Andy Green5dc62ea2013-09-20 20:26:12 +0800150 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100151
David Galeano36750b82013-01-09 16:17:04 +0800152 if (n) /* returns 1 on failure after closing wsi */
153 return NULL;
154
Andy Greena41314f2011-05-23 10:00:03 +0100155 return wsi;
156
157oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800158 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100159 free(wsi);
Andy Green5dc62ea2013-09-20 20:26:12 +0800160 return NULL;
Andy Greena41314f2011-05-23 10:00:03 +0100161
Andy Green5dc62ea2013-09-20 20:26:12 +0800162failed:
163 libwebsocket_close_and_free_session(context, wsi,
164 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena41314f2011-05-23 10:00:03 +0100165 return NULL;
166}
167
Andy Green90c7cbc2011-01-27 06:26:52 +0000168/**
169 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000170 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000171 * @address: Remote server address, eg, "myserver.com"
172 * @port: Port to connect to on the remote server, eg, 80
173 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
174 * signed certs
175 * @path: Websocket path on server
176 * @host: Hostname on server
177 * @origin: Socket origin name
178 * @protocol: Comma-separated list of protocols being asked for from
179 * the server, or just one. The server will pick the one it
180 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000181 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800182 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000183 *
184 * This function creates a connection to a remote server
185 */
186
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800187LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000188libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000189 const char *address,
190 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000191 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000192 const char *path,
193 const char *host,
194 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000195 const char *protocol,
196 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000197{
Andy Green4739e5c2011-01-22 12:51:57 +0000198 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800199#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800200 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100201 int m;
202 struct libwebsocket_extension *ext;
203 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800204#endif
205
Andy Greenbe93fef2011-02-14 20:25:43 +0000206#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000207 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800208 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000209 return NULL;
210 }
211#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000212
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800213 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000214 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800215 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000216
Andy Greenb5b23192013-02-11 17:13:32 +0800217 memset(wsi, 0, sizeof(*wsi));
Andy Green5dc62ea2013-09-20 20:26:12 +0800218 wsi->sock = -1;
Darin Willitsc19456f2011-02-14 17:52:39 +0000219
Andy Greenbfb051f2011-02-09 08:49:14 +0000220 /* -1 means just use latest supported */
221
222 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000223 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000224
225 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000226 wsi->user_space = NULL;
227 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000228 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000229 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800230#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000231 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800232#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000233#ifdef LWS_OPENSSL_SUPPORT
234 wsi->use_ssl = ssl_connection;
235#endif
236
Andy Green16ab3182013-02-10 18:02:31 +0800237 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800238 goto bail;
239
240 /*
241 * we're not necessarily in a position to action these right away,
242 * stash them... we only need during connect phase so u.hdr is fine
243 */
Andy Greena7521de2013-02-18 10:38:45 +0800244 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800245 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
246 goto bail1;
247
248 /* these only need u.hdr lifetime as well */
249
250 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
251 goto bail1;
252
253 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
254 goto bail1;
255
256 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800257 if (lws_hdr_simple_create(wsi,
258 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800259 goto bail1;
260 /*
261 * this is a list of protocols we tell the server we're okay with
262 * stash it for later when we compare server response with it
263 */
264 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800265 if (lws_hdr_simple_create(wsi,
266 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800267 goto bail1;
268
269 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000270
Andy Green3182ece2013-01-20 17:08:31 +0800271#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000272 /*
Andy Greena41314f2011-05-23 10:00:03 +0100273 * Check with each extension if it is able to route and proxy this
274 * connection for us. For example, an extension like x-google-mux
275 * can handle this and then we don't need an actual socket for this
276 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000277 */
278
Andy Greena41314f2011-05-23 10:00:03 +0100279 handled = 0;
280 ext = context->extensions;
281 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000282
Andy Greena41314f2011-05-23 10:00:03 +0100283 while (ext && ext->callback && !handled) {
284 m = ext->callback(context, ext, wsi,
285 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
286 (void *)(long)n, (void *)address, port);
287 if (m)
288 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000289
Andy Greena41314f2011-05-23 10:00:03 +0100290 ext++;
291 n++;
Andy Green9659f372011-01-27 22:01:43 +0000292 }
293
Andy Greena41314f2011-05-23 10:00:03 +0100294 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800295 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000296
Andy Greenbe93fef2011-02-14 20:25:43 +0000297 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800298 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
299 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000300
Andy Greena41314f2011-05-23 10:00:03 +0100301 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000302 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000303 }
Andy Green3182ece2013-01-20 17:08:31 +0800304#endif
Andy Green43db0452013-01-10 19:50:35 +0800305 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000306
Andy Greena41314f2011-05-23 10:00:03 +0100307 return __libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000308
Andy Green4739e5c2011-01-22 12:51:57 +0000309bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800310 free(wsi->u.hdr.ah);
311bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000312 free(wsi);
313
314 return NULL;
315}
David Brooks2c60d952012-04-20 12:19:01 +0800316
317
318/**
319 * libwebsocket_client_connect_extended() - Connect to another websocket server
320 * @context: Websocket context
321 * @address: Remote server address, eg, "myserver.com"
322 * @port: Port to connect to on the remote server, eg, 80
323 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
324 * signed certs
325 * @path: Websocket path on server
326 * @host: Hostname on server
327 * @origin: Socket origin name
328 * @protocol: Comma-separated list of protocols being asked for from
329 * the server, or just one. The server will pick the one it
330 * likes best.
331 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800332 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800333 * @userdata: Pre-allocated user data
334 *
335 * This function creates a connection to a remote server
336 */
337
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800338LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800339libwebsocket_client_connect_extended(struct libwebsocket_context *context,
340 const char *address,
341 int port,
342 int ssl_connection,
343 const char *path,
344 const char *host,
345 const char *origin,
346 const char *protocol,
347 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800348 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800349{
350 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800351 libwebsocket_client_connect(context, address, port,
352 ssl_connection, path, host, origin, protocol,
353 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800354
355 if (ws && !ws->user_space && userdata)
356 ws->user_space = userdata ;
357
358 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800359}