blob: 31099b8e5bd43099f3a3b9ef104369d4aef5c493 [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001#include "private-libwebsockets.h"
Andy Green4739e5c2011-01-22 12:51:57 +00002
Markus Elfring75212332013-10-26 20:23:00 +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
Markus Elfring75212332013-10-26 20:23:00 +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 */
Markus Elfring75212332013-10-26 20:23:00 +080038 lwsl_client("libwebsocket_client_connect_2: address %s\n", ads);
Andy Greene77fb802013-02-11 13:04:45 +080039
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
shys5efcb3f2013-10-25 15:49:11 +020096 if (errno != EISCONN) {
97
98 lwsl_debug("Connect failed errno=%d\n", errno);
99 goto failed;
100 }
Andy Greena41314f2011-05-23 10:00:03 +0100101 }
102
Andy Green43db0452013-01-10 19:50:35 +0800103 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +0100104
Andy Greena41314f2011-05-23 10:00:03 +0100105 /* we are connected to server, or proxy */
106
107 if (context->http_proxy_port) {
108
Andy Green36efd822013-10-25 22:07:57 +0800109 /* OK from now on we talk via the proxy, so connect to that */
110
111 /*
112 * (will overwrite existing pointer,
113 * leaving old string/frag there but unreferenced)
114 */
115 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
116 context->http_proxy_address))
117 goto failed;
118 wsi->u.hdr.ah->c_port = context->http_proxy_port;
119
Joachim Bauch8294c1f2013-06-29 10:22:09 +0800120 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
Andy Greena41314f2011-05-23 10:00:03 +0100121 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800122 lwsl_debug("ERROR writing to proxy socket\n");
Andy Green5dc62ea2013-09-20 20:26:12 +0800123 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100124 }
125
126 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800127 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
128 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100129
130 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
131
132 return wsi;
133 }
134
135 /*
136 * provoke service to issue the handshake directly
137 * we need to do it this way because in the proxy case, this is the
138 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800139 * response inside the state machine... but notice in SSL case this
140 * may not have sent anything yet with 0 return, and won't until some
141 * many retries from main loop. To stop that becoming endless,
142 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100143 */
144
Andy Green73abc252013-01-13 11:05:30 +0800145 libwebsocket_set_timeout(wsi,
146 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
147
Andy Greena41314f2011-05-23 10:00:03 +0100148 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
149 pfd.fd = wsi->sock;
150 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800151
152 n = libwebsocket_service_fd(context, &pfd);
153
154 if (n < 0)
Andy Green5dc62ea2013-09-20 20:26:12 +0800155 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100156
David Galeano36750b82013-01-09 16:17:04 +0800157 if (n) /* returns 1 on failure after closing wsi */
158 return NULL;
159
Andy Greena41314f2011-05-23 10:00:03 +0100160 return wsi;
161
162oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800163 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100164 free(wsi);
Andy Green5dc62ea2013-09-20 20:26:12 +0800165 return NULL;
Andy Greena41314f2011-05-23 10:00:03 +0100166
Andy Green5dc62ea2013-09-20 20:26:12 +0800167failed:
168 libwebsocket_close_and_free_session(context, wsi,
169 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena41314f2011-05-23 10:00:03 +0100170 return NULL;
171}
172
Andy Green90c7cbc2011-01-27 06:26:52 +0000173/**
174 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000175 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000176 * @address: Remote server address, eg, "myserver.com"
177 * @port: Port to connect to on the remote server, eg, 80
178 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
179 * signed certs
180 * @path: Websocket path on server
181 * @host: Hostname on server
182 * @origin: Socket origin name
183 * @protocol: Comma-separated list of protocols being asked for from
184 * the server, or just one. The server will pick the one it
185 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000186 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800187 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000188 *
189 * This function creates a connection to a remote server
190 */
191
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800192LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000193libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000194 const char *address,
195 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000196 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000197 const char *path,
198 const char *host,
199 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000200 const char *protocol,
201 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000202{
Andy Green4739e5c2011-01-22 12:51:57 +0000203 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800204#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800205 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100206 int m;
207 struct libwebsocket_extension *ext;
208 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800209#endif
210
Andy Greenbe93fef2011-02-14 20:25:43 +0000211#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000212 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800213 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000214 return NULL;
215 }
216#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000217
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800218 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000219 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800220 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000221
Andy Greenb5b23192013-02-11 17:13:32 +0800222 memset(wsi, 0, sizeof(*wsi));
Andy Green5dc62ea2013-09-20 20:26:12 +0800223 wsi->sock = -1;
Darin Willitsc19456f2011-02-14 17:52:39 +0000224
Andy Greenbfb051f2011-02-09 08:49:14 +0000225 /* -1 means just use latest supported */
226
227 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000228 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000229
230 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000231 wsi->user_space = NULL;
232 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000233 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000234 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800235#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000236 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800237#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000238#ifdef LWS_OPENSSL_SUPPORT
239 wsi->use_ssl = ssl_connection;
240#endif
241
Andy Green16ab3182013-02-10 18:02:31 +0800242 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800243 goto bail;
244
245 /*
246 * we're not necessarily in a position to action these right away,
247 * stash them... we only need during connect phase so u.hdr is fine
248 */
Andy Greena7521de2013-02-18 10:38:45 +0800249 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800250 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
251 goto bail1;
252
253 /* these only need u.hdr lifetime as well */
254
255 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
256 goto bail1;
257
258 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
259 goto bail1;
260
261 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800262 if (lws_hdr_simple_create(wsi,
263 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800264 goto bail1;
265 /*
266 * this is a list of protocols we tell the server we're okay with
267 * stash it for later when we compare server response with it
268 */
269 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800270 if (lws_hdr_simple_create(wsi,
271 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800272 goto bail1;
273
274 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000275
Andy Green3182ece2013-01-20 17:08:31 +0800276#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000277 /*
Andy Greena41314f2011-05-23 10:00:03 +0100278 * Check with each extension if it is able to route and proxy this
279 * connection for us. For example, an extension like x-google-mux
280 * can handle this and then we don't need an actual socket for this
281 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000282 */
283
Andy Greena41314f2011-05-23 10:00:03 +0100284 handled = 0;
285 ext = context->extensions;
286 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000287
Andy Greena41314f2011-05-23 10:00:03 +0100288 while (ext && ext->callback && !handled) {
289 m = ext->callback(context, ext, wsi,
290 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
291 (void *)(long)n, (void *)address, port);
292 if (m)
293 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000294
Andy Greena41314f2011-05-23 10:00:03 +0100295 ext++;
296 n++;
Andy Green9659f372011-01-27 22:01:43 +0000297 }
298
Andy Greena41314f2011-05-23 10:00:03 +0100299 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800300 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000301
Andy Greenbe93fef2011-02-14 20:25:43 +0000302 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800303 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
304 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000305
Andy Greena41314f2011-05-23 10:00:03 +0100306 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000307 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000308 }
Andy Green3182ece2013-01-20 17:08:31 +0800309#endif
Andy Green43db0452013-01-10 19:50:35 +0800310 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000311
Markus Elfring75212332013-10-26 20:23:00 +0800312 return libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000313
Andy Green4739e5c2011-01-22 12:51:57 +0000314bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800315 free(wsi->u.hdr.ah);
316bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000317 free(wsi);
318
319 return NULL;
320}
David Brooks2c60d952012-04-20 12:19:01 +0800321
322
323/**
324 * libwebsocket_client_connect_extended() - Connect to another websocket server
325 * @context: Websocket context
326 * @address: Remote server address, eg, "myserver.com"
327 * @port: Port to connect to on the remote server, eg, 80
328 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
329 * signed certs
330 * @path: Websocket path on server
331 * @host: Hostname on server
332 * @origin: Socket origin name
333 * @protocol: Comma-separated list of protocols being asked for from
334 * the server, or just one. The server will pick the one it
335 * likes best.
336 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800337 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800338 * @userdata: Pre-allocated user data
339 *
340 * This function creates a connection to a remote server
341 */
342
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800343LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800344libwebsocket_client_connect_extended(struct libwebsocket_context *context,
345 const char *address,
346 int port,
347 int ssl_connection,
348 const char *path,
349 const char *host,
350 const char *origin,
351 const char *protocol,
352 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800353 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800354{
355 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800356 libwebsocket_client_connect(context, address, port,
357 ssl_connection, path, host, origin, protocol,
358 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800359
360 if (ws && !ws->user_space && userdata)
361 ws->user_space = userdata ;
362
363 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800364}