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);
66 lwsl_err("getaddrinfo: %ls\n", gai_strerrorW(n));
68 lwsl_err("getaddrinfo: %s\n", gai_strerror(n));
73 server_addr6.sin6_family = AF_INET6;
74 switch (result->ai_family) {
76 /* map IPv4 to IPv6 */
77 bzero((char *)&server_addr6.sin6_addr,
78 sizeof(struct in6_addr));
79 server_addr6.sin6_addr.s6_addr[10] = 0xff;
80 server_addr6.sin6_addr.s6_addr[11] = 0xff;
81 memcpy(&server_addr6.sin6_addr.s6_addr[12],
82 &((struct sockaddr_in *)result->ai_addr)->sin_addr,
83 sizeof(struct in_addr));
86 memcpy(&server_addr6.sin6_addr,
87 &((struct sockaddr_in6 *)result->ai_addr)->sin6_addr,
88 sizeof(struct in6_addr));
91 lwsl_err("Unknown address family\n");
100 server_hostent = gethostbyname(ads);
101 if (!server_hostent) {
102 lwsl_err("Unable to get host name from %s\n", ads);
106 server_addr4.sin_family = AF_INET;
107 server_addr4.sin_addr =
108 *((struct in_addr *)server_hostent->h_addr);
109 bzero(&server_addr4.sin_zero, 8);
115 if (LWS_IPV6_ENABLED(context))
116 wsi->sock = socket(AF_INET6, SOCK_STREAM, 0);
119 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
122 lwsl_warn("Unable to open socket\n");
126 if (lws_plat_set_socket_options(context, wsi->sock)) {
127 lwsl_err("Failed to set wsi socket options\n");
128 compatible_close(wsi->sock);
132 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
134 insert_wsi_socket_into_fds(context, wsi);
136 libwebsocket_set_timeout(wsi,
137 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
140 if (LWS_IPV6_ENABLED(context)) {
141 v = (struct sockaddr *)&client_addr6;
142 n = sizeof(client_addr6);
144 client_addr6.sin6_family = AF_INET6;
148 v = (struct sockaddr *)&client_addr4;
149 n = sizeof(client_addr4);
151 client_addr4.sin_family = AF_INET;
154 if (context->iface) {
155 if (interface_to_sa(context, context->iface,
156 (struct sockaddr_in *)v, n) < 0) {
157 lwsl_err("Unable to find interface %s\n",
159 compatible_close(wsi->sock);
163 if (bind(wsi->sock, v, n) < 0) {
164 lwsl_err("Error binding to interface %s",
166 compatible_close(wsi->sock);
173 if (LWS_IPV6_ENABLED(context)) {
174 v = (struct sockaddr *)&server_addr6;
175 n = sizeof(struct sockaddr_in6);
179 v = (struct sockaddr *)&server_addr4;
180 n = sizeof(struct sockaddr);
183 if (connect(wsi->sock, v, n) == -1 || LWS_ERRNO == LWS_EISCONN) {
185 if (LWS_ERRNO == LWS_EALREADY || LWS_ERRNO == LWS_EINPROGRESS
186 || LWS_ERRNO == LWS_EWOULDBLOCK) {
187 lwsl_client("nonblocking connect retry\n");
190 * must do specifically a POLLOUT poll to hear
191 * about the connect completion
193 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
199 if (LWS_ERRNO != LWS_EISCONN) {
200 lwsl_debug("Connect failed errno=%d\n", LWS_ERRNO);
205 lwsl_client("connected\n");
207 /* we are connected to server, or proxy */
209 if (context->http_proxy_port) {
211 /* OK from now on we talk via the proxy, so connect to that */
214 * (will overwrite existing pointer,
215 * leaving old string/frag there but unreferenced)
217 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
218 context->http_proxy_address))
220 wsi->u.hdr.ah->c_port = context->http_proxy_port;
222 n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
224 lwsl_debug("ERROR writing to proxy socket\n");
228 libwebsocket_set_timeout(wsi,
229 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
232 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
238 * provoke service to issue the handshake directly
239 * we need to do it this way because in the proxy case, this is the
240 * next state and executed only if and when we get a good proxy
241 * response inside the state machine... but notice in SSL case this
242 * may not have sent anything yet with 0 return, and won't until some
243 * many retries from main loop. To stop that becoming endless,
244 * cover with a timeout.
247 libwebsocket_set_timeout(wsi,
248 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
250 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
252 pfd.revents = LWS_POLLIN;
254 n = libwebsocket_service_fd(context, &pfd);
259 if (n) /* returns 1 on failure after closing wsi */
270 libwebsocket_close_and_free_session(context, wsi,
271 LWS_CLOSE_STATUS_NOSTATUS);
276 * libwebsocket_client_connect() - Connect to another websocket server
277 * @context: Websocket context
278 * @address: Remote server address, eg, "myserver.com"
279 * @port: Port to connect to on the remote server, eg, 80
280 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
282 * @path: Websocket path on server
283 * @host: Hostname on server
284 * @origin: Socket origin name
285 * @protocol: Comma-separated list of protocols being asked for from
286 * the server, or just one. The server will pick the one it
288 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
289 * protocol supported, or the specific protocol ordinal
291 * This function creates a connection to a remote server
294 LWS_VISIBLE struct libwebsocket *
295 libwebsocket_client_connect(struct libwebsocket_context *context,
302 const char *protocol,
303 int ietf_version_or_minus_one)
305 struct libwebsocket *wsi;
307 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
311 memset(wsi, 0, sizeof(*wsi));
314 /* -1 means just use latest supported */
316 if (ietf_version_or_minus_one == -1)
317 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
319 wsi->ietf_spec_revision = ietf_version_or_minus_one;
320 wsi->user_space = NULL;
321 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
322 wsi->protocol = NULL;
323 wsi->pending_timeout = NO_PENDING_TIMEOUT;
325 #ifdef LWS_OPENSSL_SUPPORT
326 wsi->use_ssl = ssl_connection;
328 if (ssl_connection) {
329 lwsl_err("libwebsockets not configured for ssl\n");
334 if (lws_allocate_header_table(wsi))
338 * we're not necessarily in a position to action these right away,
339 * stash them... we only need during connect phase so u.hdr is fine
341 wsi->u.hdr.ah->c_port = port;
342 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
345 /* these only need u.hdr lifetime as well */
347 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
350 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
354 if (lws_hdr_simple_create(wsi,
355 _WSI_TOKEN_CLIENT_ORIGIN, origin))
358 * this is a list of protocols we tell the server we're okay with
359 * stash it for later when we compare server response with it
362 if (lws_hdr_simple_create(wsi,
363 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
366 wsi->protocol = &context->protocols[0];
369 * Check with each extension if it is able to route and proxy this
370 * connection for us. For example, an extension like x-google-mux
371 * can handle this and then we don't need an actual socket for this
375 if (lws_ext_callback_for_each_extension_type(context, wsi,
376 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
377 (void *)address, port) > 0) {
378 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
380 libwebsocket_set_timeout(wsi,
381 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
384 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
387 lwsl_client("libwebsocket_client_connect: direct conn\n");
389 return libwebsocket_client_connect_2(context, wsi);
401 * libwebsocket_client_connect_extended() - Connect to another websocket server
402 * @context: Websocket context
403 * @address: Remote server address, eg, "myserver.com"
404 * @port: Port to connect to on the remote server, eg, 80
405 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
407 * @path: Websocket path on server
408 * @host: Hostname on server
409 * @origin: Socket origin name
410 * @protocol: Comma-separated list of protocols being asked for from
411 * the server, or just one. The server will pick the one it
413 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
414 * protocol supported, or the specific protocol ordinal
415 * @userdata: Pre-allocated user data
417 * This function creates a connection to a remote server
420 LWS_VISIBLE struct libwebsocket *
421 libwebsocket_client_connect_extended(struct libwebsocket_context *context,
428 const char *protocol,
429 int ietf_version_or_minus_one,
432 struct libwebsocket *ws =
433 libwebsocket_client_connect(context, address, port,
434 ssl_connection, path, host, origin, protocol,
435 ietf_version_or_minus_one);
437 if (ws && !ws->user_space && userdata) {
438 ws->user_space_externally_allocated = 1;
439 ws->user_space = userdata ;