blob: 76d4ce759b536e692ba5388191be9ee068f55a5c [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),
27 wsi->u.hdr.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;
38 wsi->u.hdr.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
55 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
56
57 if (wsi->sock < 0) {
Andy Green43db0452013-01-10 19:50:35 +080058 lwsl_warn("Unable to open socket\n");
Andy Greena41314f2011-05-23 10:00:03 +010059 goto oom4;
60 }
61
62 server_addr.sin_family = AF_INET;
Andy Greene77fb802013-02-11 13:04:45 +080063 server_addr.sin_port = htons(wsi->u.hdr.c_port);
Andy Greena41314f2011-05-23 10:00:03 +010064 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
65 bzero(&server_addr.sin_zero, 8);
66
Andy Greena41314f2011-05-23 10:00:03 +010067 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
Andy Green6ee372f2012-04-09 15:09:01 +080068 sizeof(struct sockaddr)) == -1) {
Andy Green43db0452013-01-10 19:50:35 +080069 lwsl_debug("Connect failed\n");
Andy Green3fc2c652013-01-14 15:35:02 +080070 compatible_close(wsi->sock);
Andy Greena41314f2011-05-23 10:00:03 +010071 goto oom4;
72 }
73
Andy Green43db0452013-01-10 19:50:35 +080074 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +010075
Andy Greena690cd02013-02-09 12:25:31 +080076 if (lws_set_socket_options(context, wsi->sock)) {
77 lwsl_err("Failed to set wsi socket options\n");
78 close(wsi->sock);
79 goto oom4;
80 }
81
Andy Greendfb23042013-01-17 12:26:48 +080082 insert_wsi_socket_into_fds(context, wsi);
Andy Greena41314f2011-05-23 10:00:03 +010083
84 /* we are connected to server, or proxy */
85
86 if (context->http_proxy_port) {
87
Andy Greenf54a94b2013-02-10 15:19:39 +080088 n = send(wsi->sock, context->service_buffer, plen, 0);
Andy Greena41314f2011-05-23 10:00:03 +010089 if (n < 0) {
Andy Green3fc2c652013-01-14 15:35:02 +080090 compatible_close(wsi->sock);
Andy Green43db0452013-01-10 19:50:35 +080091 lwsl_debug("ERROR writing to proxy socket\n");
Andy Greena41314f2011-05-23 10:00:03 +010092 goto bail1;
93 }
94
95 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +080096 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
97 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +010098
99 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
100
101 return wsi;
102 }
103
104 /*
105 * provoke service to issue the handshake directly
106 * we need to do it this way because in the proxy case, this is the
107 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800108 * response inside the state machine... but notice in SSL case this
109 * may not have sent anything yet with 0 return, and won't until some
110 * many retries from main loop. To stop that becoming endless,
111 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100112 */
113
Andy Green73abc252013-01-13 11:05:30 +0800114 libwebsocket_set_timeout(wsi,
115 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
116
Andy Greena41314f2011-05-23 10:00:03 +0100117 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
118 pfd.fd = wsi->sock;
119 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800120
121 n = libwebsocket_service_fd(context, &pfd);
122
123 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +0800124 goto oom4;
Andy Greena41314f2011-05-23 10:00:03 +0100125
David Galeano36750b82013-01-09 16:17:04 +0800126 if (n) /* returns 1 on failure after closing wsi */
127 return NULL;
128
Andy Greena41314f2011-05-23 10:00:03 +0100129 return wsi;
130
131oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800132 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100133bail1:
134 free(wsi);
135
136 return NULL;
137}
138
Andy Green90c7cbc2011-01-27 06:26:52 +0000139/**
140 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000141 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000142 * @address: Remote server address, eg, "myserver.com"
143 * @port: Port to connect to on the remote server, eg, 80
144 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
145 * signed certs
146 * @path: Websocket path on server
147 * @host: Hostname on server
148 * @origin: Socket origin name
149 * @protocol: Comma-separated list of protocols being asked for from
150 * the server, or just one. The server will pick the one it
151 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000152 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800153 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000154 *
155 * This function creates a connection to a remote server
156 */
157
Andy Green4739e5c2011-01-22 12:51:57 +0000158struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000159libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000160 const char *address,
161 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000162 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000163 const char *path,
164 const char *host,
165 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000166 const char *protocol,
167 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000168{
Andy Green4739e5c2011-01-22 12:51:57 +0000169 struct libwebsocket *wsi;
170 int n;
Andy Green3182ece2013-01-20 17:08:31 +0800171#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100172 int m;
173 struct libwebsocket_extension *ext;
174 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800175#endif
176
Andy Greenbe93fef2011-02-14 20:25:43 +0000177#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000178 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800179 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000180 return NULL;
181 }
182#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000183
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800184 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000185 if (wsi == NULL)
186 goto bail1;
Andy Green4739e5c2011-01-22 12:51:57 +0000187
Andy Greenb5b23192013-02-11 17:13:32 +0800188 memset(wsi, 0, sizeof(*wsi));
Darin Willitsc19456f2011-02-14 17:52:39 +0000189
Andy Greenbfb051f2011-02-09 08:49:14 +0000190 /* -1 means just use latest supported */
191
192 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000193 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000194
195 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green623a98d2013-01-21 11:04:23 +0800196 wsi->u.hdr.name_buffer_pos = 0;
Andy Green4739e5c2011-01-22 12:51:57 +0000197 wsi->user_space = NULL;
198 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000199 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000200 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800201#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000202 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800203#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000204#ifdef LWS_OPENSSL_SUPPORT
205 wsi->use_ssl = ssl_connection;
206#endif
207
Andy Green16ab3182013-02-10 18:02:31 +0800208 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800209 goto bail;
210
211 /*
212 * we're not necessarily in a position to action these right away,
213 * stash them... we only need during connect phase so u.hdr is fine
214 */
215 wsi->u.hdr.c_port = port;
216 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
217 goto bail1;
218
219 /* these only need u.hdr lifetime as well */
220
221 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
222 goto bail1;
223
224 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
225 goto bail1;
226
227 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800228 if (lws_hdr_simple_create(wsi,
229 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800230 goto bail1;
231 /*
232 * this is a list of protocols we tell the server we're okay with
233 * stash it for later when we compare server response with it
234 */
235 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800236 if (lws_hdr_simple_create(wsi,
237 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800238 goto bail1;
239
240 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000241
Andy Green3182ece2013-01-20 17:08:31 +0800242#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000243 /*
Andy Greena41314f2011-05-23 10:00:03 +0100244 * Check with each extension if it is able to route and proxy this
245 * connection for us. For example, an extension like x-google-mux
246 * can handle this and then we don't need an actual socket for this
247 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000248 */
249
Andy Greena41314f2011-05-23 10:00:03 +0100250 handled = 0;
251 ext = context->extensions;
252 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000253
Andy Greena41314f2011-05-23 10:00:03 +0100254 while (ext && ext->callback && !handled) {
255 m = ext->callback(context, ext, wsi,
256 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
257 (void *)(long)n, (void *)address, port);
258 if (m)
259 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000260
Andy Greena41314f2011-05-23 10:00:03 +0100261 ext++;
262 n++;
Andy Green9659f372011-01-27 22:01:43 +0000263 }
264
Andy Greena41314f2011-05-23 10:00:03 +0100265 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800266 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000267
Andy Greenbe93fef2011-02-14 20:25:43 +0000268 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800269 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
270 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000271
Andy Greena41314f2011-05-23 10:00:03 +0100272 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000273 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000274 }
Andy Green3182ece2013-01-20 17:08:31 +0800275#endif
Andy Green43db0452013-01-10 19:50:35 +0800276 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000277
Andy Greena41314f2011-05-23 10:00:03 +0100278 return __libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000279
Andy Green4739e5c2011-01-22 12:51:57 +0000280bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800281 free(wsi->u.hdr.ah);
282bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000283 free(wsi);
284
285 return NULL;
286}
David Brooks2c60d952012-04-20 12:19:01 +0800287
288
289/**
290 * libwebsocket_client_connect_extended() - Connect to another websocket server
291 * @context: Websocket context
292 * @address: Remote server address, eg, "myserver.com"
293 * @port: Port to connect to on the remote server, eg, 80
294 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
295 * signed certs
296 * @path: Websocket path on server
297 * @host: Hostname on server
298 * @origin: Socket origin name
299 * @protocol: Comma-separated list of protocols being asked for from
300 * the server, or just one. The server will pick the one it
301 * likes best.
302 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800303 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800304 * @userdata: Pre-allocated user data
305 *
306 * This function creates a connection to a remote server
307 */
308
309struct libwebsocket *
310libwebsocket_client_connect_extended(struct libwebsocket_context *context,
311 const char *address,
312 int port,
313 int ssl_connection,
314 const char *path,
315 const char *host,
316 const char *origin,
317 const char *protocol,
318 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800319 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800320{
321 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800322 libwebsocket_client_connect(context, address, port,
323 ssl_connection, path, host, origin, protocol,
324 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800325
326 if (ws && !ws->user_space && userdata)
327 ws->user_space = userdata ;
328
329 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800330}