1 #include "private-libwebsockets.h"
3 struct libwebsocket *libwebsocket_client_connect_2(
4 struct libwebsocket_context *context,
5 struct libwebsocket *wsi
7 struct libwebsocket_pollfd pfd;
9 struct sockaddr_in6 server_addr6;
10 struct sockaddr_in6 client_addr6;
11 struct addrinfo hints, *result;
13 struct sockaddr_in server_addr4;
14 struct sockaddr_in client_addr4;
15 struct hostent *server_hostent;
22 lwsl_client("libwebsocket_client_connect_2\n");
28 if (context->http_proxy_port) {
29 plen = sprintf((char *)context->service_buffer,
30 "CONNECT %s:%u HTTP/1.0\x0d\x0a"
31 "User-agent: libwebsockets\x0d\x0a"
32 /*Proxy-authorization: basic aGVsbG86d29ybGQ= */
34 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
35 wsi->u.hdr.ah->c_port);
36 ads = context->http_proxy_address;
39 if (LWS_IPV6_ENABLED(context))
40 server_addr6.sin6_port = htons(context->http_proxy_port);
43 server_addr4.sin_port = htons(context->http_proxy_port);
46 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
48 if (LWS_IPV6_ENABLED(context))
49 server_addr6.sin6_port = htons(wsi->u.hdr.ah->c_port);
52 server_addr4.sin_port = htons(wsi->u.hdr.ah->c_port);
56 * prepare the actual connection (to the proxy, if any)
58 lwsl_client("libwebsocket_client_connect_2: address %s\n", ads);
61 if (LWS_IPV6_ENABLED(context)) {
62 memset(&hints, 0, sizeof(struct addrinfo));
63 n = getaddrinfo(ads, NULL, &hints, &result);
65 lwsl_err("getaddrinfo: %s\n", gai_strerror(n));
69 server_addr6.sin6_family = AF_INET6;
70 switch (result->ai_family) {
72 /* map IPv4 to IPv6 */
73 bzero((char *)&server_addr6.sin6_addr,
74 sizeof(struct in6_addr));
75 server_addr6.sin6_addr.s6_addr[10] = 0xff;
76 server_addr6.sin6_addr.s6_addr[11] = 0xff;
77 memcpy(&server_addr6.sin6_addr.s6_addr[12],
78 &((struct sockaddr_in *)result->ai_addr)->sin_addr,
79 sizeof(struct in_addr));
82 memcpy(&server_addr6.sin6_addr,
83 &((struct sockaddr_in6 *)result->ai_addr)->sin6_addr,
84 sizeof(struct in6_addr));
87 lwsl_err("Unknown address family\n");
96 server_hostent = gethostbyname(ads);
97 if (!server_hostent) {
98 lwsl_err("Unable to get host name from %s\n", ads);
102 server_addr4.sin_family = AF_INET;
103 server_addr4.sin_addr =
104 *((struct in_addr *)server_hostent->h_addr);
105 bzero(&server_addr4.sin_zero, 8);
111 if (LWS_IPV6_ENABLED(context))
112 wsi->sock = socket(AF_INET6, SOCK_STREAM, 0);
115 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
118 lwsl_warn("Unable to open socket\n");
122 if (lws_plat_set_socket_options(context, wsi->sock)) {
123 lwsl_err("Failed to set wsi socket options\n");
124 compatible_close(wsi->sock);
128 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
130 insert_wsi_socket_into_fds(context, wsi);
132 libwebsocket_set_timeout(wsi,
133 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
136 if (LWS_IPV6_ENABLED(context)) {
137 v = (struct sockaddr *)&client_addr6;
138 n = sizeof(client_addr6);
140 client_addr6.sin6_family = AF_INET6;
144 v = (struct sockaddr *)&client_addr4;
145 n = sizeof(client_addr4);
147 client_addr4.sin_family = AF_INET;
150 if (context->iface) {
151 if (interface_to_sa(context, context->iface,
152 (struct sockaddr_in *)v, n) < 0) {
153 lwsl_err("Unable to find interface %s\n",
155 compatible_close(wsi->sock);
159 if (bind(wsi->sock, v, n) < 0) {
160 lwsl_err("Error binding to interface %s",
162 compatible_close(wsi->sock);
169 if (LWS_IPV6_ENABLED(context)) {
170 v = (struct sockaddr *)&server_addr6;
171 n = sizeof(struct sockaddr_in6);
175 v = (struct sockaddr *)&server_addr4;
176 n = sizeof(struct sockaddr);
179 if (connect(wsi->sock, v, n) == -1 || LWS_ERRNO == LWS_EISCONN) {
181 if (LWS_ERRNO == LWS_EALREADY || LWS_ERRNO == LWS_EINPROGRESS
182 || LWS_ERRNO == LWS_EWOULDBLOCK) {
183 lwsl_client("nonblocking connect retry\n");
186 * must do specifically a POLLOUT poll to hear
187 * about the connect completion
189 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
195 if (LWS_ERRNO != LWS_EISCONN) {
196 lwsl_debug("Connect failed errno=%d\n", LWS_ERRNO);
201 lwsl_client("connected\n");
203 /* we are connected to server, or proxy */
205 if (context->http_proxy_port) {
207 /* OK from now on we talk via the proxy, so connect to that */
210 * (will overwrite existing pointer,
211 * leaving old string/frag there but unreferenced)
213 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
214 context->http_proxy_address))
216 wsi->u.hdr.ah->c_port = context->http_proxy_port;
218 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
220 lwsl_debug("ERROR writing to proxy socket\n");
224 libwebsocket_set_timeout(wsi,
225 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
228 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
234 * provoke service to issue the handshake directly
235 * we need to do it this way because in the proxy case, this is the
236 * next state and executed only if and when we get a good proxy
237 * response inside the state machine... but notice in SSL case this
238 * may not have sent anything yet with 0 return, and won't until some
239 * many retries from main loop. To stop that becoming endless,
240 * cover with a timeout.
243 libwebsocket_set_timeout(wsi,
244 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
246 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
248 pfd.revents = LWS_POLLIN;
250 n = libwebsocket_service_fd(context, &pfd);
255 if (n) /* returns 1 on failure after closing wsi */
266 libwebsocket_close_and_free_session(context, wsi,
267 LWS_CLOSE_STATUS_NOSTATUS);
272 * libwebsocket_client_connect() - Connect to another websocket server
273 * @context: Websocket context
274 * @address: Remote server address, eg, "myserver.com"
275 * @port: Port to connect to on the remote server, eg, 80
276 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
278 * @path: Websocket path on server
279 * @host: Hostname on server
280 * @origin: Socket origin name
281 * @protocol: Comma-separated list of protocols being asked for from
282 * the server, or just one. The server will pick the one it
284 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
285 * protocol supported, or the specific protocol ordinal
287 * This function creates a connection to a remote server
290 LWS_VISIBLE struct libwebsocket *
291 libwebsocket_client_connect(struct libwebsocket_context *context,
298 const char *protocol,
299 int ietf_version_or_minus_one)
301 struct libwebsocket *wsi;
302 #ifndef LWS_NO_EXTENSIONS
305 struct libwebsocket_extension *ext;
309 #ifndef LWS_OPENSSL_SUPPORT
310 if (ssl_connection) {
311 lwsl_err("libwebsockets not configured for ssl\n");
316 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
320 memset(wsi, 0, sizeof(*wsi));
323 /* -1 means just use latest supported */
325 if (ietf_version_or_minus_one == -1)
326 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
328 wsi->ietf_spec_revision = ietf_version_or_minus_one;
329 wsi->user_space = NULL;
330 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
331 wsi->protocol = NULL;
332 wsi->pending_timeout = NO_PENDING_TIMEOUT;
333 #ifndef LWS_NO_EXTENSIONS
334 wsi->count_active_extensions = 0;
336 #ifdef LWS_OPENSSL_SUPPORT
337 wsi->use_ssl = ssl_connection;
340 if (lws_allocate_header_table(wsi))
344 * we're not necessarily in a position to action these right away,
345 * stash them... we only need during connect phase so u.hdr is fine
347 wsi->u.hdr.ah->c_port = port;
348 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
351 /* these only need u.hdr lifetime as well */
353 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
356 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
360 if (lws_hdr_simple_create(wsi,
361 _WSI_TOKEN_CLIENT_ORIGIN, origin))
364 * this is a list of protocols we tell the server we're okay with
365 * stash it for later when we compare server response with it
368 if (lws_hdr_simple_create(wsi,
369 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
372 wsi->protocol = &context->protocols[0];
374 #ifndef LWS_NO_EXTENSIONS
376 * Check with each extension if it is able to route and proxy this
377 * connection for us. For example, an extension like x-google-mux
378 * can handle this and then we don't need an actual socket for this
383 ext = context->extensions;
386 while (ext && ext->callback && !handled) {
387 m = ext->callback(context, ext, wsi,
388 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
389 (void *)(long)n, (void *)address, port);
398 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
400 libwebsocket_set_timeout(wsi,
401 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
404 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
408 lwsl_client("libwebsocket_client_connect: direct conn\n");
410 return libwebsocket_client_connect_2(context, wsi);
422 * libwebsocket_client_connect_extended() - Connect to another websocket server
423 * @context: Websocket context
424 * @address: Remote server address, eg, "myserver.com"
425 * @port: Port to connect to on the remote server, eg, 80
426 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
428 * @path: Websocket path on server
429 * @host: Hostname on server
430 * @origin: Socket origin name
431 * @protocol: Comma-separated list of protocols being asked for from
432 * the server, or just one. The server will pick the one it
434 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
435 * protocol supported, or the specific protocol ordinal
436 * @userdata: Pre-allocated user data
438 * This function creates a connection to a remote server
441 LWS_VISIBLE struct libwebsocket *
442 libwebsocket_client_connect_extended(struct libwebsocket_context *context,
449 const char *protocol,
450 int ietf_version_or_minus_one,
453 struct libwebsocket *ws =
454 libwebsocket_client_connect(context, address, port,
455 ssl_connection, path, host, origin, protocol,
456 ietf_version_or_minus_one);
458 if (ws && !ws->user_space && userdata)
459 ws->user_space = userdata ;