blob: 2fec991c4fb0c0bb274d76d8a5437956bb8c2078 [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 Green055f2972014-03-24 16:09:25 +08008#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08009 struct sockaddr_in6 server_addr6;
10 struct sockaddr_in6 client_addr6;
11 struct addrinfo hints, *result;
12#endif
13 struct sockaddr_in server_addr4;
14 struct sockaddr_in client_addr4;
Andy Greena41314f2011-05-23 10:00:03 +010015 struct hostent *server_hostent;
James Devine3f13ea22014-03-24 16:09:25 +080016
17 struct sockaddr *v;
Andy Greena41314f2011-05-23 10:00:03 +010018 int n;
19 int plen = 0;
Andy Greene77fb802013-02-11 13:04:45 +080020 const char *ads;
Andy Greena41314f2011-05-23 10:00:03 +010021
Markus Elfring75212332013-10-26 20:23:00 +080022 lwsl_client("libwebsocket_client_connect_2\n");
Andy Greena41314f2011-05-23 10:00:03 +010023
24 /*
25 * proxy?
26 */
27
28 if (context->http_proxy_port) {
Andy Greene48ba312013-02-10 15:34:59 +080029 plen = sprintf((char *)context->service_buffer,
Andy Greenf54a94b2013-02-10 15:19:39 +080030 "CONNECT %s:%u HTTP/1.0\x0d\x0a"
Andy Greena41314f2011-05-23 10:00:03 +010031 "User-agent: libwebsockets\x0d\x0a"
32/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
Andy Greene77fb802013-02-11 13:04:45 +080033 "\x0d\x0a",
34 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
Andy Greena7521de2013-02-18 10:38:45 +080035 wsi->u.hdr.ah->c_port);
Andy Green36efd822013-10-25 22:07:57 +080036 ads = context->http_proxy_address;
James Devine3f13ea22014-03-24 16:09:25 +080037
Andy Green055f2972014-03-24 16:09:25 +080038#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +080039 if (LWS_IPV6_ENABLED(context))
40 server_addr6.sin6_port = htons(context->http_proxy_port);
41 else
42#endif
43 server_addr4.sin_port = htons(context->http_proxy_port);
44
Andy Green36efd822013-10-25 22:07:57 +080045 } else {
46 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
Andy Green7844d042014-03-25 14:08:21 +080047#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +080048 if (LWS_IPV6_ENABLED(context))
49 server_addr6.sin6_port = htons(wsi->u.hdr.ah->c_port);
50 else
51#endif
52 server_addr4.sin_port = htons(wsi->u.hdr.ah->c_port);
Andy Greena41314f2011-05-23 10:00:03 +010053 }
54
55 /*
56 * prepare the actual connection (to the proxy, if any)
57 */
Markus Elfring75212332013-10-26 20:23:00 +080058 lwsl_client("libwebsocket_client_connect_2: address %s\n", ads);
Andy Greene77fb802013-02-11 13:04:45 +080059
Andy Green055f2972014-03-24 16:09:25 +080060#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +080061 if (LWS_IPV6_ENABLED(context)) {
62 memset(&hints, 0, sizeof(struct addrinfo));
63 n = getaddrinfo(ads, NULL, &hints, &result);
64 if (n) {
65 lwsl_err("getaddrinfo: %s\n", gai_strerror(n));
66 goto oom4;
67 }
68
69 server_addr6.sin6_family = AF_INET6;
70 switch (result->ai_family) {
71 case AF_INET:
72 /* map IPv4 to IPv6 */
73 bzero((char *)&server_addr6.sin6_addr,
74 sizeof(struct in6_addr));
75 server_addr6.sin6_addr.s6_addr16[5] = 0xffff;
76 bcopy(&((struct sockaddr_in *)result->ai_addr)->sin_addr,
77 &server_addr6.sin6_addr.s6_addr16[6],
78 sizeof(struct in_addr));
79 break;
80 case AF_INET6:
81 memcpy(&server_addr6.sin6_addr,
82 &((struct sockaddr_in6 *)result->ai_addr)->sin6_addr,
83 sizeof(struct in6_addr));
84 break;
85 default:
86 lwsl_err("Unknown address family\n");
87 freeaddrinfo(result);
88 goto oom4;
89 }
90
91 freeaddrinfo(result);
92 } else
93#endif
94 {
95 server_hostent = gethostbyname(ads);
96 if (!server_hostent) {
97 lwsl_err("Unable to get host name from %s\n", ads);
98 goto oom4;
99 }
100
101 server_addr4.sin_family = AF_INET;
102 server_addr4.sin_addr =
103 *((struct in_addr *)server_hostent->h_addr);
104 bzero(&server_addr4.sin_zero, 8);
Andy Greena41314f2011-05-23 10:00:03 +0100105 }
106
Andy Greena41314f2011-05-23 10:00:03 +0100107 if (wsi->sock < 0) {
Andy Green5dc62ea2013-09-20 20:26:12 +0800108
Andy Green055f2972014-03-24 16:09:25 +0800109#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +0800110 if (LWS_IPV6_ENABLED(context))
111 wsi->sock = socket(AF_INET6, SOCK_STREAM, 0);
112 else
113#endif
114 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
Andy Green5dc62ea2013-09-20 20:26:12 +0800115
116 if (wsi->sock < 0) {
117 lwsl_warn("Unable to open socket\n");
118 goto oom4;
119 }
120
121 if (lws_set_socket_options(context, wsi->sock)) {
122 lwsl_err("Failed to set wsi socket options\n");
123 compatible_close(wsi->sock);
124 goto oom4;
125 }
126
127 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
128
129 insert_wsi_socket_into_fds(context, wsi);
130
131 libwebsocket_set_timeout(wsi,
132 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
133 AWAITING_TIMEOUT);
Andy Green055f2972014-03-24 16:09:25 +0800134#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +0800135 if (LWS_IPV6_ENABLED(context)) {
136 v = (struct sockaddr *)&client_addr6;
137 n = sizeof(client_addr6);
138 bzero((char *)v, n);
139 client_addr6.sin6_family = AF_INET6;
140 } else
141#endif
142 {
143 v = (struct sockaddr *)&client_addr4;
144 n = sizeof(client_addr4);
145 bzero((char *)v, n);
146 client_addr4.sin_family = AF_INET;
147 }
Mattias Lundberg03bb8f92014-02-18 10:06:57 +0100148
James Devine3f13ea22014-03-24 16:09:25 +0800149 if (context->iface) {
150 if (interface_to_sa(context, context->iface,
151 (struct sockaddr_in *)v, n) < 0) {
152 lwsl_err("Unable to find interface %s\n",
153 context->iface);
Mattias Lundberg03bb8f92014-02-18 10:06:57 +0100154 compatible_close(wsi->sock);
155 goto failed;
156 }
157
James Devine3f13ea22014-03-24 16:09:25 +0800158 if (bind(wsi->sock, v, n) < 0) {
159 lwsl_err("Error binding to interface %s",
160 context->iface);
Mattias Lundberg03bb8f92014-02-18 10:06:57 +0100161 compatible_close(wsi->sock);
162 goto failed;
163 }
164 }
Andy Greena41314f2011-05-23 10:00:03 +0100165 }
166
Andy Green055f2972014-03-24 16:09:25 +0800167#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +0800168 if (LWS_IPV6_ENABLED(context)) {
169 v = (struct sockaddr *)&server_addr6;
170 n = sizeof(struct sockaddr_in6);
171 } else
172#endif
173 {
174 v = (struct sockaddr *)&server_addr4;
175 n = sizeof(struct sockaddr);
176 }
Andy Green36efd822013-10-25 22:07:57 +0800177
James Devine3f13ea22014-03-24 16:09:25 +0800178 if (connect(wsi->sock, v, n) == -1 || LWS_ERRNO == LWS_EISCONN) {
Andy Green5dc62ea2013-09-20 20:26:12 +0800179
Patrick Gansterera6163002014-03-27 11:21:41 +0100180 if (LWS_ERRNO == LWS_EALREADY || LWS_ERRNO == LWS_EINPROGRESS
181 || LWS_ERRNO == LWS_EWOULDBLOCK) {
Andy Green5dc62ea2013-09-20 20:26:12 +0800182 lwsl_client("nonblocking connect retry\n");
183
184 /*
185 * must do specifically a POLLOUT poll to hear
186 * about the connect completion
187 */
Andy Green91f19d82013-12-21 11:18:34 +0800188 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green5dc62ea2013-09-20 20:26:12 +0800189
190 return wsi;
191 }
192
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100193 if (LWS_ERRNO != LWS_EISCONN) {
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100194 lwsl_debug("Connect failed errno=%d\n", LWS_ERRNO);
shys5efcb3f2013-10-25 15:49:11 +0200195 goto failed;
196 }
Andy Greena41314f2011-05-23 10:00:03 +0100197 }
198
Andy Green43db0452013-01-10 19:50:35 +0800199 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +0100200
Andy Greena41314f2011-05-23 10:00:03 +0100201 /* we are connected to server, or proxy */
202
203 if (context->http_proxy_port) {
204
Andy Green36efd822013-10-25 22:07:57 +0800205 /* OK from now on we talk via the proxy, so connect to that */
206
207 /*
208 * (will overwrite existing pointer,
209 * leaving old string/frag there but unreferenced)
210 */
211 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
212 context->http_proxy_address))
213 goto failed;
214 wsi->u.hdr.ah->c_port = context->http_proxy_port;
215
Joachim Bauch8294c1f2013-06-29 10:22:09 +0800216 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
Andy Greena41314f2011-05-23 10:00:03 +0100217 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800218 lwsl_debug("ERROR writing to proxy socket\n");
Andy Green5dc62ea2013-09-20 20:26:12 +0800219 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100220 }
221
222 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800223 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
224 AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100225
226 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
227
228 return wsi;
229 }
230
231 /*
232 * provoke service to issue the handshake directly
233 * we need to do it this way because in the proxy case, this is the
234 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800235 * response inside the state machine... but notice in SSL case this
236 * may not have sent anything yet with 0 return, and won't until some
237 * many retries from main loop. To stop that becoming endless,
238 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100239 */
240
Andy Green73abc252013-01-13 11:05:30 +0800241 libwebsocket_set_timeout(wsi,
242 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
243
Andy Greena41314f2011-05-23 10:00:03 +0100244 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
245 pfd.fd = wsi->sock;
246 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800247
248 n = libwebsocket_service_fd(context, &pfd);
249
250 if (n < 0)
Andy Green5dc62ea2013-09-20 20:26:12 +0800251 goto failed;
Andy Greena41314f2011-05-23 10:00:03 +0100252
David Galeano36750b82013-01-09 16:17:04 +0800253 if (n) /* returns 1 on failure after closing wsi */
254 return NULL;
255
Andy Greena41314f2011-05-23 10:00:03 +0100256 return wsi;
257
258oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800259 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100260 free(wsi);
Andy Green5dc62ea2013-09-20 20:26:12 +0800261 return NULL;
Andy Greena41314f2011-05-23 10:00:03 +0100262
Andy Green5dc62ea2013-09-20 20:26:12 +0800263failed:
264 libwebsocket_close_and_free_session(context, wsi,
265 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena41314f2011-05-23 10:00:03 +0100266 return NULL;
267}
268
Andy Green90c7cbc2011-01-27 06:26:52 +0000269/**
270 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000271 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000272 * @address: Remote server address, eg, "myserver.com"
273 * @port: Port to connect to on the remote server, eg, 80
274 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
275 * signed certs
276 * @path: Websocket path on server
277 * @host: Hostname on server
278 * @origin: Socket origin name
279 * @protocol: Comma-separated list of protocols being asked for from
280 * the server, or just one. The server will pick the one it
281 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000282 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800283 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000284 *
285 * This function creates a connection to a remote server
286 */
287
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800288LWS_VISIBLE struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000289libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000290 const char *address,
291 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000292 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000293 const char *path,
294 const char *host,
295 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000296 const char *protocol,
297 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000298{
Andy Green4739e5c2011-01-22 12:51:57 +0000299 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +0800300#ifndef LWS_NO_EXTENSIONS
Andy Green760c3d42013-02-18 10:43:18 +0800301 int n;
Andy Greena41314f2011-05-23 10:00:03 +0100302 int m;
303 struct libwebsocket_extension *ext;
304 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800305#endif
306
Andy Greenbe93fef2011-02-14 20:25:43 +0000307#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000308 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800309 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000310 return NULL;
311 }
312#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000313
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800314 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000315 if (wsi == NULL)
Andy Green224149a2013-02-11 21:43:41 +0800316 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000317
Andy Greenb5b23192013-02-11 17:13:32 +0800318 memset(wsi, 0, sizeof(*wsi));
Andy Green5dc62ea2013-09-20 20:26:12 +0800319 wsi->sock = -1;
Darin Willitsc19456f2011-02-14 17:52:39 +0000320
Andy Greenbfb051f2011-02-09 08:49:14 +0000321 /* -1 means just use latest supported */
322
323 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000324 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000325
326 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +0000327 wsi->user_space = NULL;
328 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000329 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000330 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800331#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000332 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800333#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000334#ifdef LWS_OPENSSL_SUPPORT
335 wsi->use_ssl = ssl_connection;
336#endif
337
Andy Green16ab3182013-02-10 18:02:31 +0800338 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800339 goto bail;
340
341 /*
342 * we're not necessarily in a position to action these right away,
343 * stash them... we only need during connect phase so u.hdr is fine
344 */
Andy Greena7521de2013-02-18 10:38:45 +0800345 wsi->u.hdr.ah->c_port = port;
Andy Greene77fb802013-02-11 13:04:45 +0800346 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
347 goto bail1;
348
349 /* these only need u.hdr lifetime as well */
350
351 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
352 goto bail1;
353
354 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
355 goto bail1;
356
357 if (origin)
Andy Greenb5b23192013-02-11 17:13:32 +0800358 if (lws_hdr_simple_create(wsi,
359 _WSI_TOKEN_CLIENT_ORIGIN, origin))
Andy Greene77fb802013-02-11 13:04:45 +0800360 goto bail1;
361 /*
362 * this is a list of protocols we tell the server we're okay with
363 * stash it for later when we compare server response with it
364 */
365 if (protocol)
Andy Greenb5b23192013-02-11 17:13:32 +0800366 if (lws_hdr_simple_create(wsi,
367 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
Andy Greene77fb802013-02-11 13:04:45 +0800368 goto bail1;
369
370 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000371
Andy Green3182ece2013-01-20 17:08:31 +0800372#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000373 /*
Andy Greena41314f2011-05-23 10:00:03 +0100374 * Check with each extension if it is able to route and proxy this
375 * connection for us. For example, an extension like x-google-mux
376 * can handle this and then we don't need an actual socket for this
377 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000378 */
379
Andy Greena41314f2011-05-23 10:00:03 +0100380 handled = 0;
381 ext = context->extensions;
382 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000383
Andy Greena41314f2011-05-23 10:00:03 +0100384 while (ext && ext->callback && !handled) {
385 m = ext->callback(context, ext, wsi,
386 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
387 (void *)(long)n, (void *)address, port);
388 if (m)
389 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000390
Andy Greena41314f2011-05-23 10:00:03 +0100391 ext++;
392 n++;
Andy Green9659f372011-01-27 22:01:43 +0000393 }
394
Andy Greena41314f2011-05-23 10:00:03 +0100395 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800396 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000397
Andy Greenbe93fef2011-02-14 20:25:43 +0000398 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800399 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
400 AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000401
Andy Greena41314f2011-05-23 10:00:03 +0100402 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000403 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000404 }
Andy Green3182ece2013-01-20 17:08:31 +0800405#endif
Andy Green43db0452013-01-10 19:50:35 +0800406 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000407
Markus Elfring75212332013-10-26 20:23:00 +0800408 return libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000409
Andy Green4739e5c2011-01-22 12:51:57 +0000410bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800411 free(wsi->u.hdr.ah);
412bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000413 free(wsi);
414
415 return NULL;
416}
David Brooks2c60d952012-04-20 12:19:01 +0800417
418
419/**
420 * libwebsocket_client_connect_extended() - Connect to another websocket server
421 * @context: Websocket context
422 * @address: Remote server address, eg, "myserver.com"
423 * @port: Port to connect to on the remote server, eg, 80
424 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
425 * signed certs
426 * @path: Websocket path on server
427 * @host: Hostname on server
428 * @origin: Socket origin name
429 * @protocol: Comma-separated list of protocols being asked for from
430 * the server, or just one. The server will pick the one it
431 * likes best.
432 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Greenb5b23192013-02-11 17:13:32 +0800433 * protocol supported, or the specific protocol ordinal
David Brooks2c60d952012-04-20 12:19:01 +0800434 * @userdata: Pre-allocated user data
435 *
436 * This function creates a connection to a remote server
437 */
438
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800439LWS_VISIBLE struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +0800440libwebsocket_client_connect_extended(struct libwebsocket_context *context,
441 const char *address,
442 int port,
443 int ssl_connection,
444 const char *path,
445 const char *host,
446 const char *origin,
447 const char *protocol,
448 int ietf_version_or_minus_one,
Andy Greenb5b23192013-02-11 17:13:32 +0800449 void *userdata)
David Brooks2c60d952012-04-20 12:19:01 +0800450{
451 struct libwebsocket *ws =
Andy Greenb5b23192013-02-11 17:13:32 +0800452 libwebsocket_client_connect(context, address, port,
453 ssl_connection, path, host, origin, protocol,
454 ietf_version_or_minus_one);
David Brooks2c60d952012-04-20 12:19:01 +0800455
456 if (ws && !ws->user_space && userdata)
457 ws->user_space = userdata ;
458
459 return ws ;
Andy Greenb5b23192013-02-11 17:13:32 +0800460}