blob: f8d0b69274954b8576b98264e7daab544158a01e [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;
8 struct timeval tv;
9 struct hostent *server_hostent;
10 struct sockaddr_in server_addr;
11 int n;
12 int plen = 0;
13 char pkt[512];
14 int opt = 1;
M Kf2431152011-09-25 10:34:35 +010015#if defined(__APPLE__)
Andy Green6ee372f2012-04-09 15:09:01 +080016 struct protoent *tcp_proto;
M Kf2431152011-09-25 10:34:35 +010017#endif
Andy Greena41314f2011-05-23 10:00:03 +010018
Andy Greencc012472011-11-07 19:53:23 +080019 debug("__libwebsocket_client_connect_2\n");
Andy Greena41314f2011-05-23 10:00:03 +010020
21 wsi->candidate_children_list = NULL;
22
23 /*
24 * proxy?
25 */
26
27 if (context->http_proxy_port) {
28 plen = sprintf(pkt, "CONNECT %s:%u HTTP/1.0\x0d\x0a"
29 "User-agent: libwebsockets\x0d\x0a"
30/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
31 "\x0d\x0a", wsi->c_address, wsi->c_port);
32
33 /* OK from now on we talk via the proxy */
34
35 free(wsi->c_address);
36 wsi->c_address = strdup(context->http_proxy_address);
37 wsi->c_port = context->http_proxy_port;
38 }
39
40 /*
41 * prepare the actual connection (to the proxy, if any)
42 */
43
Andy Greencc012472011-11-07 19:53:23 +080044 debug("__libwebsocket_client_connect_2: address %s", wsi->c_address);
Andy Green66a16f32011-05-24 22:07:45 +010045
Andy Greena41314f2011-05-23 10:00:03 +010046 server_hostent = gethostbyname(wsi->c_address);
47 if (server_hostent == NULL) {
48 fprintf(stderr, "Unable to get host name from %s\n",
49 wsi->c_address);
50 goto oom4;
51 }
52
53 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
54
55 if (wsi->sock < 0) {
56 fprintf(stderr, "Unable to open socket\n");
57 goto oom4;
58 }
59
60 server_addr.sin_family = AF_INET;
61 server_addr.sin_port = htons(wsi->c_port);
62 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
63 bzero(&server_addr.sin_zero, 8);
64
65 /* Disable Nagle */
M Kf2431152011-09-25 10:34:35 +010066#if !defined(__APPLE__)
Andy Green6ee372f2012-04-09 15:09:01 +080067 setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY,
68 (const void *)&opt, sizeof(opt));
M Kf2431152011-09-25 10:34:35 +010069#else
Andy Green6ee372f2012-04-09 15:09:01 +080070 tcp_proto = getprotobyname("TCP");
71 setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY,
72 &opt, sizeof(opt));
M Kf2431152011-09-25 10:34:35 +010073#endif
Andy Greena41314f2011-05-23 10:00:03 +010074
75 /* Set receiving timeout */
76 tv.tv_sec = 0;
77 tv.tv_usec = 100 * 1000;
78 setsockopt(wsi->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv);
79
80 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
Andy Green6ee372f2012-04-09 15:09:01 +080081 sizeof(struct sockaddr)) == -1) {
Andy Greena41314f2011-05-23 10:00:03 +010082 fprintf(stderr, "Connect failed\n");
83 goto oom4;
84 }
85
Andy Greencc012472011-11-07 19:53:23 +080086 debug("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +010087
88 /* into fd -> wsi hashtable */
89
90 insert_wsi(context, wsi);
91
92 /* into internal poll list */
93
94 context->fds[context->fds_count].fd = wsi->sock;
95 context->fds[context->fds_count].revents = 0;
96 context->fds[context->fds_count++].events = POLLIN;
97
98 /* external POLL support via protocol 0 */
99 context->protocols[0].callback(context, wsi,
100 LWS_CALLBACK_ADD_POLL_FD,
101 (void *)(long)wsi->sock, NULL, POLLIN);
102
103 /* we are connected to server, or proxy */
104
105 if (context->http_proxy_port) {
106
107 n = send(wsi->sock, pkt, plen, 0);
108 if (n < 0) {
109#ifdef WIN32
110 closesocket(wsi->sock);
111#else
112 close(wsi->sock);
113#endif
114 fprintf(stderr, "ERROR writing to proxy socket\n");
115 goto bail1;
116 }
117
118 libwebsocket_set_timeout(wsi,
119 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, 5);
120
121 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
122
123 return wsi;
124 }
125
126 /*
127 * provoke service to issue the handshake directly
128 * we need to do it this way because in the proxy case, this is the
129 * next state and executed only if and when we get a good proxy
130 * response inside the state machine
131 */
132
133 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
134 pfd.fd = wsi->sock;
135 pfd.revents = POLLIN;
136 libwebsocket_service_fd(context, &pfd);
137
138 return wsi;
139
140oom4:
141 if (wsi->c_protocol)
142 free(wsi->c_protocol);
143
144 if (wsi->c_origin)
145 free(wsi->c_origin);
146
147 free(wsi->c_host);
148 free(wsi->c_path);
149
150bail1:
151 free(wsi);
152
153 return NULL;
154}
155
Andy Green90c7cbc2011-01-27 06:26:52 +0000156/**
157 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000158 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000159 * @address: Remote server address, eg, "myserver.com"
160 * @port: Port to connect to on the remote server, eg, 80
161 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
162 * signed certs
163 * @path: Websocket path on server
164 * @host: Hostname on server
165 * @origin: Socket origin name
166 * @protocol: Comma-separated list of protocols being asked for from
167 * the server, or just one. The server will pick the one it
168 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000169 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800170 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000171 *
172 * This function creates a connection to a remote server
173 */
174
Andy Green4739e5c2011-01-22 12:51:57 +0000175struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000176libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000177 const char *address,
178 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000179 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000180 const char *path,
181 const char *host,
182 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000183 const char *protocol,
184 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000185{
Andy Green4739e5c2011-01-22 12:51:57 +0000186 struct libwebsocket *wsi;
187 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100188 int m;
189 struct libwebsocket_extension *ext;
190 int handled;
Andy Greenbe93fef2011-02-14 20:25:43 +0000191#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000192 if (ssl_connection) {
193 fprintf(stderr, "libwebsockets not configured for ssl\n");
194 return NULL;
195 }
196#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000197
Andy Green6964bb52011-01-23 16:50:33 +0000198 wsi = malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000199 if (wsi == NULL)
200 goto bail1;
Andy Green4739e5c2011-01-22 12:51:57 +0000201
Darin Willitsc19456f2011-02-14 17:52:39 +0000202 memset(wsi, 0, sizeof *wsi);
203
Andy Greenbfb051f2011-02-09 08:49:14 +0000204 /* -1 means just use latest supported */
205
206 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000207 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000208
209 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000210 wsi->name_buffer_pos = 0;
211 wsi->user_space = NULL;
212 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
213 wsi->pings_vs_pongs = 0;
Andy Green927eb7b2011-02-01 08:52:55 +0000214 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000215 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Greend6e09112011-03-05 16:12:15 +0000216 wsi->count_active_extensions = 0;
Andy Greenbe93fef2011-02-14 20:25:43 +0000217#ifdef LWS_OPENSSL_SUPPORT
218 wsi->use_ssl = ssl_connection;
219#endif
220
Andy Greena41314f2011-05-23 10:00:03 +0100221 wsi->c_port = port;
222 wsi->c_address = strdup(address);
223
Andy Greenbe93fef2011-02-14 20:25:43 +0000224 /* copy parameters over so state machine has access */
225
226 wsi->c_path = malloc(strlen(path) + 1);
227 if (wsi->c_path == NULL)
228 goto bail1;
229 strcpy(wsi->c_path, path);
Andy Green6ee372f2012-04-09 15:09:01 +0800230
Andy Greenbe93fef2011-02-14 20:25:43 +0000231 wsi->c_host = malloc(strlen(host) + 1);
232 if (wsi->c_host == NULL)
233 goto oom1;
234 strcpy(wsi->c_host, host);
Andy Green6ee372f2012-04-09 15:09:01 +0800235
Andy Green08d33922011-02-26 10:22:49 +0000236 if (origin) {
237 wsi->c_origin = malloc(strlen(origin) + 1);
238 strcpy(wsi->c_origin, origin);
239 if (wsi->c_origin == NULL)
240 goto oom2;
241 } else
242 wsi->c_origin = NULL;
Andy Green6ee372f2012-04-09 15:09:01 +0800243
David Brooks993343b2012-04-20 12:16:52 +0800244 wsi->c_callback = NULL;
Andy Greenbe93fef2011-02-14 20:25:43 +0000245 if (protocol) {
David Brooks993343b2012-04-20 12:16:52 +0800246 const char *pc;
247 struct libwebsocket_protocols *pp;
248
Andy Greenbe93fef2011-02-14 20:25:43 +0000249 wsi->c_protocol = malloc(strlen(protocol) + 1);
250 if (wsi->c_protocol == NULL)
251 goto oom3;
David Brooks993343b2012-04-20 12:16:52 +0800252
Andy Greenbe93fef2011-02-14 20:25:43 +0000253 strcpy(wsi->c_protocol, protocol);
David Brooks993343b2012-04-20 12:16:52 +0800254
255 pc = protocol;
256 while (*pc && *pc != ',')
257 pc++;
258 n = pc - protocol;
259 pp = context->protocols;
260 while (pp->name && !wsi->c_callback) {
261 if (!strncmp(protocol, pp->name, n))
262 wsi->c_callback = pp->callback;
263 pp++;
264 }
Andy Greenbe93fef2011-02-14 20:25:43 +0000265 } else
266 wsi->c_protocol = NULL;
267
David Brooks993343b2012-04-20 12:16:52 +0800268 if (!wsi->c_callback)
269 wsi->c_callback = context->protocols[0].callback;
270
Andy Greenbfb051f2011-02-09 08:49:14 +0000271 /* set up appropriate masking */
272
273 wsi->xor_mask = xor_no_mask;
274
275 switch (wsi->ietf_spec_revision) {
Andy Greeneeaacb32011-03-01 20:44:24 +0000276 case 0:
277 break;
Andy Greenbfb051f2011-02-09 08:49:14 +0000278 case 4:
279 wsi->xor_mask = xor_mask_04;
280 break;
281 case 5:
Andy Green9514bf82011-02-26 11:13:56 +0000282 case 6:
Andy Green33872cd2011-04-24 05:49:44 +0100283 case 7:
Andy Greend85cb202011-09-25 09:32:54 +0100284 case 8:
285 case 13:
Andy Greenbfb051f2011-02-09 08:49:14 +0000286 wsi->xor_mask = xor_mask_05;
287 break;
288 default:
289 fprintf(stderr,
290 "Client ietf version %d not supported\n",
291 wsi->ietf_spec_revision);
Andy Greenbe93fef2011-02-14 20:25:43 +0000292 goto oom4;
Andy Greenbfb051f2011-02-09 08:49:14 +0000293 }
294
295 /* force no mask if he asks for that though */
296
Peter Hinz56885f32011-03-02 22:03:47 +0000297 if (context->options & LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK)
Andy Greenbfb051f2011-02-09 08:49:14 +0000298 wsi->xor_mask = xor_no_mask;
299
Andy Green4739e5c2011-01-22 12:51:57 +0000300 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
301 wsi->utf8_token[n].token = NULL;
302 wsi->utf8_token[n].token_len = 0;
303 }
304
305 /*
Andy Greena41314f2011-05-23 10:00:03 +0100306 * Check with each extension if it is able to route and proxy this
307 * connection for us. For example, an extension like x-google-mux
308 * can handle this and then we don't need an actual socket for this
309 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000310 */
311
Andy Greena41314f2011-05-23 10:00:03 +0100312 handled = 0;
313 ext = context->extensions;
314 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000315
Andy Greena41314f2011-05-23 10:00:03 +0100316 while (ext && ext->callback && !handled) {
317 m = ext->callback(context, ext, wsi,
318 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
319 (void *)(long)n, (void *)address, port);
320 if (m)
321 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000322
Andy Greena41314f2011-05-23 10:00:03 +0100323 ext++;
324 n++;
Andy Green9659f372011-01-27 22:01:43 +0000325 }
326
Andy Greena41314f2011-05-23 10:00:03 +0100327 if (handled) {
Andy Greencc012472011-11-07 19:53:23 +0800328 debug("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000329
Andy Greenbe93fef2011-02-14 20:25:43 +0000330 libwebsocket_set_timeout(wsi,
Andy Greena41314f2011-05-23 10:00:03 +0100331 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, 5);
Andy Green5b9a4c02011-01-28 09:39:29 +0000332
Andy Greena41314f2011-05-23 10:00:03 +0100333 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000334 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000335 }
336
Andy Greencc012472011-11-07 19:53:23 +0800337 debug("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000338
Andy Greena41314f2011-05-23 10:00:03 +0100339 return __libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000340
Andy Green4739e5c2011-01-22 12:51:57 +0000341
Andy Greenbe93fef2011-02-14 20:25:43 +0000342oom4:
343 if (wsi->c_protocol)
344 free(wsi->c_protocol);
Andy Green4739e5c2011-01-22 12:51:57 +0000345
Andy Greenbe93fef2011-02-14 20:25:43 +0000346oom3:
Andy Green08d33922011-02-26 10:22:49 +0000347 if (wsi->c_origin)
348 free(wsi->c_origin);
Andy Greenbe93fef2011-02-14 20:25:43 +0000349
350oom2:
351 free(wsi->c_host);
352
353oom1:
354 free(wsi->c_path);
355
Andy Green4739e5c2011-01-22 12:51:57 +0000356bail1:
357 free(wsi);
358
359 return NULL;
360}
David Brooks2c60d952012-04-20 12:19:01 +0800361
362
363/**
364 * libwebsocket_client_connect_extended() - Connect to another websocket server
365 * @context: Websocket context
366 * @address: Remote server address, eg, "myserver.com"
367 * @port: Port to connect to on the remote server, eg, 80
368 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
369 * signed certs
370 * @path: Websocket path on server
371 * @host: Hostname on server
372 * @origin: Socket origin name
373 * @protocol: Comma-separated list of protocols being asked for from
374 * the server, or just one. The server will pick the one it
375 * likes best.
376 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
377 * protocol supported, or the specific protocol ordinal
378 * @userdata: Pre-allocated user data
379 *
380 * This function creates a connection to a remote server
381 */
382
383struct libwebsocket *
384libwebsocket_client_connect_extended(struct libwebsocket_context *context,
385 const char *address,
386 int port,
387 int ssl_connection,
388 const char *path,
389 const char *host,
390 const char *origin,
391 const char *protocol,
392 int ietf_version_or_minus_one,
393 void *userdata)
394{
395 struct libwebsocket *ws =
396 libwebsocket_client_connect(context, address, port, ssl_connection, path, host, origin, protocol, ietf_version_or_minus_one) ;
397
398 if (ws && !ws->user_space && userdata)
399 ws->user_space = userdata ;
400
401 return ws ;
402 }