blob: de99a208eb6423b4d806bd256f1acebbd9ab55e7 [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
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 Greena7521de2013-02-18 10:38:45 +080063 server_addr.sin_port = htons(wsi->u.hdr.ah->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");
Tobias Maier37029d92013-03-14 13:02:23 +010078 compatible_close(wsi->sock);
Andy Greena690cd02013-02-09 12:25:31 +080079 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 Green224149a2013-02-11 21:43:41 +080092 goto oom4;
Andy Greena41314f2011-05-23 10:00:03 +010093 }
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 +0100133 free(wsi);
134
135 return NULL;
136}
137
Andy Green90c7cbc2011-01-27 06:26:52 +0000138/**
139 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000140 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000141 * @address: Remote server address, eg, "myserver.com"
142 * @port: Port to connect to on the remote server, eg, 80
143 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
144 * signed certs
145 * @path: Websocket path on server
146 * @host: Hostname on server
147 * @origin: Socket origin name
148 * @protocol: Comma-separated list of protocols being asked for from
149 * the server, or just one. The server will pick the one it
150 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000151 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800152 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000153 *
154 * This function creates a connection to a remote server
155 */
156
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800157LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000158libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000159 const char *address,
160 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000161 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000162 const char *path,
163 const char *host,
164 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000165 const char *protocol,
166 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000167{
Andy Green4739e5c2011-01-22 12:51:57 +0000168 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800169#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800170 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100171 int m;
172 struct libwebsocket_extension *ext;
173 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800174#endif
175
Andy Greenbe93fef2011-02-14 20:25:43 +0000176#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000177 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800178 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000179 return NULL;
180 }
181#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000182
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800183 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000184 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800185 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000186
Andy Greenb5b23192013-02-11 17:13:32 +0800187 memset(wsi, 0, sizeof(*wsi));
Darin Willitsc19456f2011-02-14 17:52:39 +0000188
Andy Greenbfb051f2011-02-09 08:49:14 +0000189 /* -1 means just use latest supported */
190
191 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000192 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000193
194 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000195 wsi->user_space = NULL;
196 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000197 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000198 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800199#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000200 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800201#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000202#ifdef LWS_OPENSSL_SUPPORT
203 wsi->use_ssl = ssl_connection;
204#endif
205
Andy Green16ab3182013-02-10 18:02:31 +0800206 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800207 goto bail;
208
209 /*
210 * we're not necessarily in a position to action these right away,
211 * stash them... we only need during connect phase so u.hdr is fine
212 */
Andy Greena7521de2013-02-18 10:38:45 +0800213 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800214 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
215 goto bail1;
216
217 /* these only need u.hdr lifetime as well */
218
219 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
220 goto bail1;
221
222 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
223 goto bail1;
224
225 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800226 if (lws_hdr_simple_create(wsi,
227 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800228 goto bail1;
229 /*
230 * this is a list of protocols we tell the server we're okay with
231 * stash it for later when we compare server response with it
232 */
233 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800234 if (lws_hdr_simple_create(wsi,
235 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800236 goto bail1;
237
238 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000239
Andy Green3182ece2013-01-20 17:08:31 +0800240#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000241 /*
Andy Greena41314f2011-05-23 10:00:03 +0100242 * Check with each extension if it is able to route and proxy this
243 * connection for us. For example, an extension like x-google-mux
244 * can handle this and then we don't need an actual socket for this
245 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000246 */
247
Andy Greena41314f2011-05-23 10:00:03 +0100248 handled = 0;
249 ext = context->extensions;
250 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000251
Andy Greena41314f2011-05-23 10:00:03 +0100252 while (ext && ext->callback && !handled) {
253 m = ext->callback(context, ext, wsi,
254 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
255 (void *)(long)n, (void *)address, port);
256 if (m)
257 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000258
Andy Greena41314f2011-05-23 10:00:03 +0100259 ext++;
260 n++;
Andy Green9659f372011-01-27 22:01:43 +0000261 }
262
Andy Greena41314f2011-05-23 10:00:03 +0100263 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800264 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000265
Andy Greenbe93fef2011-02-14 20:25:43 +0000266 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800267 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
268 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000269
Andy Greena41314f2011-05-23 10:00:03 +0100270 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000271 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000272 }
Andy Green3182ece2013-01-20 17:08:31 +0800273#endif
Andy Green43db0452013-01-10 19:50:35 +0800274 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000275
Andy Greena41314f2011-05-23 10:00:03 +0100276 return __libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000277
Andy Green4739e5c2011-01-22 12:51:57 +0000278bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800279 free(wsi->u.hdr.ah);
280bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000281 free(wsi);
282
283 return NULL;
284}
David Brooks2c60d952012-04-20 12:19:01 +0800285
286
287/**
288 * libwebsocket_client_connect_extended() - Connect to another websocket server
289 * @context: Websocket context
290 * @address: Remote server address, eg, "myserver.com"
291 * @port: Port to connect to on the remote server, eg, 80
292 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
293 * signed certs
294 * @path: Websocket path on server
295 * @host: Hostname on server
296 * @origin: Socket origin name
297 * @protocol: Comma-separated list of protocols being asked for from
298 * the server, or just one. The server will pick the one it
299 * likes best.
300 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800301 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800302 * @userdata: Pre-allocated user data
303 *
304 * This function creates a connection to a remote server
305 */
306
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800307LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800308libwebsocket_client_connect_extended(struct libwebsocket_context *context,
309 const char *address,
310 int port,
311 int ssl_connection,
312 const char *path,
313 const char *host,
314 const char *origin,
315 const char *protocol,
316 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800317 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800318{
319 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800320 libwebsocket_client_connect(context, address, port,
321 ssl_connection, path, host, origin, protocol,
322 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800323
324 if (ws && !ws->user_space && userdata)
325 ws->user_space = userdata ;
326
327 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800328}