1 #include "private-libwebsockets.h"
4 lws_getaddrinfo46(struct lws *wsi, const char *ads, struct addrinfo **result)
8 memset(&hints, 0, sizeof(hints));
14 #if !defined(__ANDROID__)
15 hints.ai_family = AF_INET6;
16 hints.ai_flags = AI_V4MAPPED;
21 hints.ai_family = PF_UNSPEC;
22 hints.ai_socktype = SOCK_STREAM;
23 hints.ai_flags = AI_CANONNAME;
26 return getaddrinfo(ads, NULL, &hints, result);
30 lws_client_connect_2(struct lws *wsi)
33 struct addrinfo *result;
34 struct lws_context *context = wsi->context;
35 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
36 struct lws_pollfd pfd;
37 const char *cce = "", *iface;
38 int n, plen = 0, port;
41 char ipv6only = lws_check_opt(wsi->vhost->options,
42 LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY |
43 LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE);
45 #if defined(__ANDROID__)
50 lwsl_client("%s\n", __func__);
53 cce = "ah was NULL at cc2";
54 lwsl_err("%s\n", cce);
59 * start off allowing ipv6 on connection if vhost allows it
61 wsi->ipv6 = LWS_IPV6_ENABLED(wsi->vhost);
63 /* Decide what it is we need to connect to:
65 * Priority 1: connect to http proxy */
67 if (wsi->vhost->http_proxy_port) {
68 plen = sprintf((char *)pt->serv_buf,
69 "CONNECT %s:%u HTTP/1.0\x0d\x0a"
70 "User-agent: libwebsockets\x0d\x0a",
71 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
74 if (wsi->vhost->proxy_basic_auth_token[0])
75 plen += sprintf((char *)pt->serv_buf + plen,
76 "Proxy-authorization: basic %s\x0d\x0a",
77 wsi->vhost->proxy_basic_auth_token);
79 plen += sprintf((char *)pt->serv_buf + plen, "\x0d\x0a");
80 ads = wsi->vhost->http_proxy_address;
81 port = wsi->vhost->http_proxy_port;
83 #if defined(LWS_WITH_SOCKS5)
85 /* Priority 2: Connect to SOCK5 Proxy */
87 } else if (wsi->vhost->socks_proxy_port) {
88 socks_generate_msg(wsi, SOCKS_MSG_GREETING, (size_t *)&plen);
89 lwsl_client("%s\n", "Sending SOCKS Greeting.");
91 ads = wsi->vhost->socks_proxy_address;
92 port = wsi->vhost->socks_proxy_port;
96 /* Priority 3: Connect directly */
98 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
103 * prepare the actual connection
104 * to whatever we decided to connect to
107 lwsl_notice("%s: %p: address %s\n", __func__, wsi, ads);
109 n = lws_getaddrinfo46(wsi, ads, &result);
114 memset(&sa46, 0, sizeof(sa46));
116 sa46.sa6.sin6_family = AF_INET6;
117 switch (result->ai_family) {
121 /* map IPv4 to IPv6 */
122 bzero((char *)&sa46.sa6.sin6_addr,
123 sizeof(sa46.sa6.sin6_addr));
124 sa46.sa6.sin6_addr.s6_addr[10] = 0xff;
125 sa46.sa6.sin6_addr.s6_addr[11] = 0xff;
126 memcpy(&sa46.sa6.sin6_addr.s6_addr[12],
127 &((struct sockaddr_in *)result->ai_addr)->sin_addr,
128 sizeof(struct in_addr));
129 lwsl_notice("uplevelling AF_INET to AF_INET6\n");
133 memcpy(&sa46.sa6.sin6_addr,
134 &((struct sockaddr_in6 *)result->ai_addr)->sin6_addr,
135 sizeof(struct in6_addr));
136 sa46.sa6.sin6_scope_id = ((struct sockaddr_in6 *)result->ai_addr)->sin6_scope_id;
137 sa46.sa6.sin6_flowinfo = ((struct sockaddr_in6 *)result->ai_addr)->sin6_flowinfo;
140 lwsl_err("Unknown address family\n");
141 freeaddrinfo(result);
142 cce = "unknown address family";
146 #endif /* use ipv6 */
153 struct addrinfo *res = result;
155 /* pick the first AF_INET (IPv4) result */
158 switch (res->ai_family) {
160 p = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
166 #if defined(LWS_FALLBACK_GETHOSTBYNAME)
167 } else if (n == EAI_SYSTEM) {
168 struct hostent *host;
170 lwsl_info("getaddrinfo (ipv4) failed, trying gethostbyname\n");
171 host = gethostbyname(ads);
175 lwsl_err("gethostbyname failed\n");
176 cce = "gethostbyname (ipv4) failed";
181 lwsl_err("getaddrinfo failed\n");
182 cce = "getaddrinfo failed";
188 freeaddrinfo(result);
189 lwsl_err("Couldn't identify address\n");
190 cce = "unable to lookup address";
194 sa46.sa4.sin_family = AF_INET;
195 sa46.sa4.sin_addr = *((struct in_addr *)p);
196 bzero(&sa46.sa4.sin_zero, 8);
200 freeaddrinfo(result);
202 /* now we decided on ipv4 or ipv6, set the port */
204 if (!lws_socket_is_valid(wsi->desc.sockfd)) {
206 #if defined(LWS_USE_LIBUV)
207 if (LWS_LIBUV_ENABLED(context))
208 if (lws_libuv_check_watcher_active(wsi)) {
209 lwsl_warn("Waiting for libuv watcher to close\n");
210 cce = "waiting for libuv watcher to close";
217 sa46.sa6.sin6_port = htons(port);
218 wsi->desc.sockfd = socket(AF_INET6, SOCK_STREAM, 0);
222 sa46.sa4.sin_port = htons(port);
223 wsi->desc.sockfd = socket(AF_INET, SOCK_STREAM, 0);
226 if (!lws_socket_is_valid(wsi->desc.sockfd)) {
227 lwsl_warn("Unable to open socket\n");
228 cce = "unable to open socket";
232 if (lws_plat_set_socket_options(wsi->vhost, wsi->desc.sockfd)) {
233 lwsl_err("Failed to set wsi socket options\n");
234 compatible_close(wsi->desc.sockfd);
235 cce = "set socket opts failed";
239 wsi->mode = LWSCM_WSCL_WAITING_CONNECT;
241 lws_libev_accept(wsi, wsi->desc);
242 lws_libuv_accept(wsi, wsi->desc);
243 lws_libevent_accept(wsi, wsi->desc);
245 if (insert_wsi_socket_into_fds(context, wsi)) {
246 compatible_close(wsi->desc.sockfd);
247 cce = "insert wsi failed";
251 lws_change_pollfd(wsi, 0, LWS_POLLIN);
254 * past here, we can't simply free the structs as error
255 * handling as oom4 does. We have to run the whole close flow.
259 wsi->protocol = &wsi->vhost->protocols[0];
261 wsi->protocol->callback(wsi, LWS_CALLBACK_WSI_CREATE,
262 wsi->user_space, NULL, 0);
264 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
267 iface = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_IFACE);
270 n = lws_socket_bind(wsi->vhost, wsi->desc.sockfd, 0, iface);
272 cce = "unable to bind socket";
280 n = sizeof(struct sockaddr_in6);
283 n = sizeof(struct sockaddr);
285 if (connect(wsi->desc.sockfd, (const struct sockaddr *)&sa46, n) == -1 ||
286 LWS_ERRNO == LWS_EISCONN) {
287 if (LWS_ERRNO == LWS_EALREADY ||
288 LWS_ERRNO == LWS_EINPROGRESS ||
289 LWS_ERRNO == LWS_EWOULDBLOCK
291 || LWS_ERRNO == WSAEINVAL
294 lwsl_client("nonblocking connect retry (errno = %d)\n",
297 if (lws_plat_check_connection_error(wsi)) {
298 cce = "socket connect failed";
303 * must do specifically a POLLOUT poll to hear
304 * about the connect completion
306 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT)) {
307 cce = "POLLOUT set failed";
314 if (LWS_ERRNO != LWS_EISCONN) {
315 lwsl_notice("Connect failed errno=%d\n", LWS_ERRNO);
316 cce = "connect failed";
321 lwsl_client("connected\n");
323 /* we are connected to server, or proxy */
326 if (wsi->vhost->http_proxy_port) {
329 * OK from now on we talk via the proxy, so connect to that
331 * (will overwrite existing pointer,
332 * leaving old string/frag there but unreferenced)
334 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
335 wsi->vhost->http_proxy_address))
337 wsi->c_port = wsi->vhost->http_proxy_port;
339 n = send(wsi->desc.sockfd, (char *)pt->serv_buf, plen,
342 lwsl_debug("ERROR writing to proxy socket\n");
343 cce = "proxy write failed";
347 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
350 wsi->mode = LWSCM_WSCL_WAITING_PROXY_REPLY;
354 #if defined(LWS_WITH_SOCKS5)
356 else if (wsi->vhost->socks_proxy_port) {
357 n = send(wsi->desc.sockfd, (char *)pt->serv_buf, plen,
360 lwsl_debug("ERROR writing greeting to socks proxy"
362 cce = "socks write failed";
366 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SOCKS_GREETING_REPLY,
369 wsi->mode = LWSCM_WSCL_WAITING_SOCKS_GREETING_REPLY;
376 * provoke service to issue the handshake directly
377 * we need to do it this way because in the proxy case, this is the
378 * next state and executed only if and when we get a good proxy
379 * response inside the state machine... but notice in SSL case this
380 * may not have sent anything yet with 0 return, and won't until some
381 * many retries from main loop. To stop that becoming endless,
382 * cover with a timeout.
385 lws_set_timeout(wsi, PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
388 wsi->mode = LWSCM_WSCL_ISSUE_HANDSHAKE;
389 pfd.fd = wsi->desc.sockfd;
390 pfd.events = LWS_POLLIN;
391 pfd.revents = LWS_POLLIN;
393 n = lws_service_fd(context, &pfd);
395 cce = "first service failed";
398 if (n) /* returns 1 on failure after closing wsi */
404 /* we're closing, losing some rx is OK */
406 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
407 if (wsi->mode == LWSCM_HTTP_CLIENT ||
408 wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED ||
409 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) {
410 wsi->vhost->protocols[0].callback(wsi,
411 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
412 wsi->user_space, (void *)cce, strlen(cce));
413 wsi->already_did_cce = 1;
415 /* take care that we might be inserted in fds already */
416 if (wsi->position_in_fds_table != -1)
418 lws_remove_from_timeout_list(wsi);
419 lws_header_table_detach(wsi, 0);
425 wsi->vhost->protocols[0].callback(wsi,
426 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
427 wsi->user_space, (void *)cce, strlen(cce));
428 wsi->already_did_cce = 1;
430 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
436 * lws_client_reset() - retarget a connected wsi to start over with a new connection (ie, redirect)
437 * this only works if still in HTTP, ie, not upgraded yet
438 * wsi: connection to reset
439 * address: network address of the new server
440 * port: port to connect to
441 * path: uri path to connect to on the new server
442 * host: host header to send to the new server
444 LWS_VISIBLE struct lws *
445 lws_client_reset(struct lws **pwsi, int ssl, const char *address, int port,
446 const char *path, const char *host)
448 char origin[300] = "", protocol[300] = "", method[32] = "", iface[16] = "", *p;
449 struct lws *wsi = *pwsi;
451 if (wsi->redirects == 3) {
452 lwsl_err("%s: Too many redirects\n", __func__);
457 #ifdef LWS_OPENSSL_SUPPORT
461 lwsl_err("%s: not configured for ssl\n", __func__);
466 p = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN);
468 strncpy(origin, p, sizeof(origin) - 1);
470 p = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
472 strncpy(protocol, p, sizeof(protocol) - 1);
474 p = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
476 strncpy(method, p, sizeof(method) - 1);
478 p = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_IFACE);
480 strncpy(method, p, sizeof(iface) - 1);
482 lwsl_debug("redirect ads='%s', port=%d, path='%s', ssl = %d\n",
483 address, port, path, ssl);
485 /* close the connection by hand */
488 if (LWS_LIBUV_ENABLED(wsi->context)) {
489 lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
491 * libuv has to do his own close handle processing asynchronously
492 * but once it starts we can do everything else synchronously,
493 * including trash wsi->desc.sockfd since it took a copy.
495 * When it completes it will call compatible_close()
497 lws_libuv_closehandle_manually(wsi);
500 compatible_close(wsi->desc.sockfd);
503 remove_wsi_socket_from_fds(wsi);
505 wsi->desc.sockfd = LWS_SOCK_INVALID;
506 wsi->state = LWSS_CLIENT_UNCONNECTED;
507 wsi->protocol = NULL;
508 wsi->pending_timeout = NO_PENDING_TIMEOUT;
510 wsi->hdr_parsing_completed = 0;
511 _lws_header_table_reset(wsi->u.hdr.ah);
513 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
516 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
520 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_ORIGIN,
524 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
528 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_METHOD,
533 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_IFACE,
538 strncpy(&origin[1], path, sizeof(origin) - 2);
539 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, origin))
542 *pwsi = lws_client_connect_2(wsi);
547 #ifdef LWS_WITH_HTTP_PROXY
549 html_parser_cb(const hubbub_token *token, void *pw)
551 struct lws_rewrite *r = (struct lws_rewrite *)pw;
552 char buf[1024], *start = buf + LWS_PRE, *p = start,
553 *end = &buf[sizeof(buf) - 1];
556 switch (token->type) {
557 case HUBBUB_TOKEN_DOCTYPE:
559 p += lws_snprintf(p, end - p, "<!DOCTYPE %.*s %s ",
560 (int) token->data.doctype.name.len,
561 token->data.doctype.name.ptr,
562 token->data.doctype.force_quirks ?
563 "(force-quirks) " : "");
565 if (token->data.doctype.public_missing)
566 lwsl_debug("\tpublic: missing\n");
568 p += lws_snprintf(p, end - p, "PUBLIC \"%.*s\"\n",
569 (int) token->data.doctype.public_id.len,
570 token->data.doctype.public_id.ptr);
572 if (token->data.doctype.system_missing)
573 lwsl_debug("\tsystem: missing\n");
575 p += lws_snprintf(p, end - p, " \"%.*s\">\n",
576 (int) token->data.doctype.system_id.len,
577 token->data.doctype.system_id.ptr);
580 case HUBBUB_TOKEN_START_TAG:
581 p += lws_snprintf(p, end - p, "<%.*s", (int)token->data.tag.name.len,
582 token->data.tag.name.ptr);
584 /* (token->data.tag.self_closing) ?
585 "(self-closing) " : "",
586 (token->data.tag.n_attributes > 0) ?
589 for (i = 0; i < token->data.tag.n_attributes; i++) {
590 if (!hstrcmp(&token->data.tag.attributes[i].name, "href", 4) ||
591 !hstrcmp(&token->data.tag.attributes[i].name, "action", 6) ||
592 !hstrcmp(&token->data.tag.attributes[i].name, "src", 3)) {
593 const char *pp = (const char *)token->data.tag.attributes[i].value.ptr;
594 int plen = (int) token->data.tag.attributes[i].value.len;
596 if (strncmp(pp, "http:", 5) && strncmp(pp, "https:", 6)) {
598 if (!hstrcmp(&token->data.tag.attributes[i].value,
599 r->from, r->from_len)) {
603 p += lws_snprintf(p, end - p, " %.*s=\"%s/%.*s\"",
604 (int) token->data.tag.attributes[i].name.len,
605 token->data.tag.attributes[i].name.ptr,
611 p += lws_snprintf(p, end - p, " %.*s=\"%.*s\"",
612 (int) token->data.tag.attributes[i].name.len,
613 token->data.tag.attributes[i].name.ptr,
614 (int) token->data.tag.attributes[i].value.len,
615 token->data.tag.attributes[i].value.ptr);
617 p += lws_snprintf(p, end - p, ">");
619 case HUBBUB_TOKEN_END_TAG:
620 p += lws_snprintf(p, end - p, "</%.*s", (int) token->data.tag.name.len,
621 token->data.tag.name.ptr);
623 (token->data.tag.self_closing) ?
624 "(self-closing) " : "",
625 (token->data.tag.n_attributes > 0) ?
628 for (i = 0; i < token->data.tag.n_attributes; i++) {
629 p += lws_snprintf(p, end - p, " %.*s='%.*s'\n",
630 (int) token->data.tag.attributes[i].name.len,
631 token->data.tag.attributes[i].name.ptr,
632 (int) token->data.tag.attributes[i].value.len,
633 token->data.tag.attributes[i].value.ptr);
635 p += lws_snprintf(p, end - p, ">");
637 case HUBBUB_TOKEN_COMMENT:
638 p += lws_snprintf(p, end - p, "<!-- %.*s -->\n",
639 (int) token->data.comment.len,
640 token->data.comment.ptr);
642 case HUBBUB_TOKEN_CHARACTER:
643 if (token->data.character.len == 1) {
644 if (*token->data.character.ptr == '<') {
645 p += lws_snprintf(p, end - p, "<");
648 if (*token->data.character.ptr == '>') {
649 p += lws_snprintf(p, end - p, ">");
652 if (*token->data.character.ptr == '&') {
653 p += lws_snprintf(p, end - p, "&");
658 p += lws_snprintf(p, end - p, "%.*s", (int) token->data.character.len,
659 token->data.character.ptr);
661 case HUBBUB_TOKEN_EOF:
662 p += lws_snprintf(p, end - p, "\n");
666 if (user_callback_handle_rxflow(r->wsi->protocol->callback,
667 r->wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
668 r->wsi->user_space, start, p - start))
675 LWS_VISIBLE struct lws *
676 lws_client_connect_via_info(struct lws_client_connect_info *i)
679 int v = SPEC_LATEST_SUPPORTED;
680 const struct lws_protocols *p;
682 if (i->context->requested_kill)
685 if (!i->context->protocol_init_done)
686 lws_protocol_init(i->context);
688 wsi = lws_zalloc(sizeof(struct lws));
692 wsi->context = i->context;
693 /* assert the mode and union status (hdr) clearly */
694 lws_union_transition(wsi, LWSCM_HTTP_CLIENT);
695 wsi->desc.sockfd = LWS_SOCK_INVALID;
697 /* 1) fill up the wsi with stuff from the connect_info as far as it
698 * can go. It's because not only is our connection async, we might
699 * not even be able to get ahold of an ah at this point.
702 /* -1 means just use latest supported */
703 if (i->ietf_version_or_minus_one != -1 && i->ietf_version_or_minus_one)
704 v = i->ietf_version_or_minus_one;
706 wsi->ietf_spec_revision = v;
707 wsi->user_space = NULL;
708 wsi->state = LWSS_CLIENT_UNCONNECTED;
709 wsi->pending_timeout = NO_PENDING_TIMEOUT;
710 wsi->position_in_fds_table = -1;
711 wsi->c_port = i->port;
712 wsi->vhost = i->vhost;
714 wsi->vhost = i->context->vhost_list;
716 wsi->protocol = &wsi->vhost->protocols[0];
718 /* for http[s] connection, allow protocol selection by name */
720 if (i->method && i->vhost && i->protocol) {
721 p = lws_vhost_name_to_protocol(i->vhost, i->protocol);
726 if (wsi && !wsi->user_space && i->userdata) {
727 wsi->user_space_externally_allocated = 1;
728 wsi->user_space = i->userdata;
730 /* if we stay in http, we can assign the user space now,
731 * otherwise do it after the protocol negotiated
734 if (lws_ensure_user_space(wsi))
737 #ifdef LWS_OPENSSL_SUPPORT
738 wsi->use_ssl = i->ssl_connection;
740 if (i->ssl_connection) {
741 lwsl_err("libwebsockets not configured for ssl\n");
746 /* 2) stash the things from connect_info that we can't process without
747 * an ah. Because if no ah, we will go on the ah waiting list and
748 * process those things later (after the connect_info and maybe the
749 * things pointed to have gone out of scope.
752 wsi->u.hdr.stash = lws_malloc(sizeof(*wsi->u.hdr.stash));
753 if (!wsi->u.hdr.stash) {
754 lwsl_err("%s: OOM\n", __func__);
758 wsi->u.hdr.stash->origin[0] = '\0';
759 wsi->u.hdr.stash->protocol[0] = '\0';
760 wsi->u.hdr.stash->method[0] = '\0';
761 wsi->u.hdr.stash->iface[0] = '\0';
763 strncpy(wsi->u.hdr.stash->address, i->address,
764 sizeof(wsi->u.hdr.stash->address) - 1);
765 strncpy(wsi->u.hdr.stash->path, i->path,
766 sizeof(wsi->u.hdr.stash->path) - 1);
767 strncpy(wsi->u.hdr.stash->host, i->host,
768 sizeof(wsi->u.hdr.stash->host) - 1);
770 strncpy(wsi->u.hdr.stash->origin, i->origin,
771 sizeof(wsi->u.hdr.stash->origin) - 1);
773 strncpy(wsi->u.hdr.stash->protocol, i->protocol,
774 sizeof(wsi->u.hdr.stash->protocol) - 1);
776 strncpy(wsi->u.hdr.stash->method, i->method,
777 sizeof(wsi->u.hdr.stash->method) - 1);
779 strncpy(wsi->u.hdr.stash->iface, i->iface,
780 sizeof(wsi->u.hdr.stash->iface) - 1);
782 wsi->u.hdr.stash->address[sizeof(wsi->u.hdr.stash->address) - 1] = '\0';
783 wsi->u.hdr.stash->path[sizeof(wsi->u.hdr.stash->path) - 1] = '\0';
784 wsi->u.hdr.stash->host[sizeof(wsi->u.hdr.stash->host) - 1] = '\0';
785 wsi->u.hdr.stash->origin[sizeof(wsi->u.hdr.stash->origin) - 1] = '\0';
786 wsi->u.hdr.stash->protocol[sizeof(wsi->u.hdr.stash->protocol) - 1] = '\0';
787 wsi->u.hdr.stash->method[sizeof(wsi->u.hdr.stash->method) - 1] = '\0';
788 wsi->u.hdr.stash->iface[sizeof(wsi->u.hdr.stash->iface) - 1] = '\0';
793 /* if we went on the waiting list, no probs just return the wsi
794 * when we get the ah, now or later, he will call
795 * lws_client_connect_via_info2() below.
797 if (lws_header_table_attach(wsi, 0) < 0) {
799 * if we failed here, the connection is already closed
806 lwsl_info("%s: created child %p of parent %p\n", __func__,
808 wsi->parent = i->parent_wsi;
809 wsi->sibling_list = i->parent_wsi->child_list;
810 i->parent_wsi->child_list = wsi;
812 #ifdef LWS_WITH_HTTP_PROXY
813 if (i->uri_replace_to)
814 wsi->rw = lws_rewrite_create(wsi, html_parser_cb,
832 lws_client_connect_via_info2(struct lws *wsi)
834 struct client_info_stash *stash = wsi->u.hdr.stash;
840 * we're not necessarily in a position to action these right away,
841 * stash them... we only need during connect phase so u.hdr is fine
843 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
847 /* these only need u.hdr lifetime as well */
849 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, stash->path))
852 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, stash->host))
855 if (stash->origin[0])
856 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_ORIGIN,
860 * this is a list of protocols we tell the server we're okay with
861 * stash it for later when we compare server response with it
863 if (stash->protocol[0])
864 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
867 if (stash->method[0])
868 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_METHOD,
872 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_IFACE,
876 #if defined(LWS_WITH_SOCKS5)
877 if (!wsi->vhost->socks_proxy_port)
878 lws_free_set_NULL(wsi->u.hdr.stash);
882 * Check with each extension if it is able to route and proxy this
883 * connection for us. For example, an extension like x-google-mux
884 * can handle this and then we don't need an actual socket for this
888 if (lws_ext_cb_all_exts(wsi->context, wsi,
889 LWS_EXT_CB_CAN_PROXY_CLIENT_CONNECTION,
890 (void *)stash->address,
892 lwsl_client("lws_client_connect: ext handling conn\n");
895 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
898 wsi->mode = LWSCM_WSCL_WAITING_EXTENSION_CONNECT;
901 lwsl_client("lws_client_connect: direct conn\n");
902 wsi->context->count_wsi_allocated++;
904 return lws_client_connect_2(wsi);
907 #if defined(LWS_WITH_SOCKS5)
908 if (!wsi->vhost->socks_proxy_port)
909 lws_free_set_NULL(wsi->u.hdr.stash);
915 LWS_VISIBLE struct lws *
916 lws_client_connect_extended(struct lws_context *context, const char *address,
917 int port, int ssl_connection, const char *path,
918 const char *host, const char *origin,
919 const char *protocol, int ietf_version_or_minus_one,
922 struct lws_client_connect_info i;
924 memset(&i, 0, sizeof(i));
929 i.ssl_connection = ssl_connection;
933 i.protocol = protocol;
934 i.ietf_version_or_minus_one = ietf_version_or_minus_one;
935 i.userdata = userdata;
937 return lws_client_connect_via_info(&i);
940 LWS_VISIBLE struct lws *
941 lws_client_connect(struct lws_context *context, const char *address,
942 int port, int ssl_connection, const char *path,
943 const char *host, const char *origin,
944 const char *protocol, int ietf_version_or_minus_one)
946 struct lws_client_connect_info i;
948 memset(&i, 0, sizeof(i));
953 i.ssl_connection = ssl_connection;
957 i.protocol = protocol;
958 i.ietf_version_or_minus_one = ietf_version_or_minus_one;
961 return lws_client_connect_via_info(&i);
964 #if defined(LWS_WITH_SOCKS5)
965 void socks_generate_msg(struct lws *wsi, enum socks_msg_type type,
968 struct lws_context *context = wsi->context;
969 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
972 if (type == SOCKS_MSG_GREETING) {
973 /* socks version, version 5 only */
974 pt->serv_buf[len++] = SOCKS_VERSION_5;
975 /* number of methods */
976 pt->serv_buf[len++] = 2;
977 /* username password method */
978 pt->serv_buf[len++] = SOCKS_AUTH_USERNAME_PASSWORD;
979 /* no authentication method */
980 pt->serv_buf[len++] = SOCKS_AUTH_NO_AUTH;
982 else if (type == SOCKS_MSG_USERNAME_PASSWORD) {
984 size_t passwd_len = 0;
986 user_len = strlen(wsi->vhost->socks_user);
987 passwd_len = strlen(wsi->vhost->socks_password);
989 /* the subnegotiation version */
990 pt->serv_buf[len++] = SOCKS_SUBNEGOTIATION_VERSION_1;
991 /* length of the user name */
992 pt->serv_buf[len++] = user_len;
994 strncpy((char *)&pt->serv_buf[len], wsi->vhost->socks_user,
995 context->pt_serv_buf_size - len);
997 /* length of the password */
998 pt->serv_buf[len++] = passwd_len;
1000 strncpy((char *)&pt->serv_buf[len], wsi->vhost->socks_password,
1001 context->pt_serv_buf_size - len);
1004 else if (type == SOCKS_MSG_CONNECT) {
1005 size_t len_index = 0;
1007 char *net_buf = (char*)&net_num;
1010 pt->serv_buf[len++] = SOCKS_VERSION_5;
1012 pt->serv_buf[len++] = SOCKS_COMMAND_CONNECT;
1014 pt->serv_buf[len++] = 0;
1016 pt->serv_buf[len++] = SOCKS_ATYP_DOMAINNAME;
1019 /* the address we tell SOCKS proxy to connect to */
1020 strncpy((char *)&(pt->serv_buf[len]), wsi->u.hdr.stash->address,
1021 context->pt_serv_buf_size - len);
1022 len += strlen(wsi->u.hdr.stash->address);
1023 net_num = htons((short)wsi->c_port);
1024 /* the port we tell SOCKS proxy to connect to */
1025 pt->serv_buf[len++] = net_buf[0];
1026 pt->serv_buf[len++] = net_buf[1];
1027 /* the length of the address, excluding port */
1028 pt->serv_buf[len_index] = strlen(wsi->u.hdr.stash->address);