Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1 | #include "private-libwebsockets.h" |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2 | |
Markus Elfring | 7521233 | 2013-10-26 20:23:00 +0800 | [diff] [blame] | 3 | struct libwebsocket *libwebsocket_client_connect_2( |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 4 | struct libwebsocket_context *context, |
| 5 | struct libwebsocket *wsi |
| 6 | ) { |
| 7 | struct pollfd pfd; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 8 | struct hostent *server_hostent; |
| 9 | struct sockaddr_in server_addr; |
| 10 | int n; |
| 11 | int plen = 0; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 12 | const char *ads; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 13 | |
Markus Elfring | 7521233 | 2013-10-26 20:23:00 +0800 | [diff] [blame] | 14 | lwsl_client("libwebsocket_client_connect_2\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * proxy? |
| 18 | */ |
| 19 | |
| 20 | if (context->http_proxy_port) { |
Andy Green | e48ba31 | 2013-02-10 15:34:59 +0800 | [diff] [blame] | 21 | plen = sprintf((char *)context->service_buffer, |
Andy Green | f54a94b | 2013-02-10 15:19:39 +0800 | [diff] [blame] | 22 | "CONNECT %s:%u HTTP/1.0\x0d\x0a" |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 23 | "User-agent: libwebsockets\x0d\x0a" |
| 24 | /*Proxy-authorization: basic aGVsbG86d29ybGQ= */ |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 25 | "\x0d\x0a", |
| 26 | lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS), |
Andy Green | a7521de | 2013-02-18 10:38:45 +0800 | [diff] [blame] | 27 | wsi->u.hdr.ah->c_port); |
Andy Green | 36efd82 | 2013-10-25 22:07:57 +0800 | [diff] [blame] | 28 | 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 Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* |
| 36 | * prepare the actual connection (to the proxy, if any) |
| 37 | */ |
Markus Elfring | 7521233 | 2013-10-26 20:23:00 +0800 | [diff] [blame] | 38 | lwsl_client("libwebsocket_client_connect_2: address %s\n", ads); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 39 | |
| 40 | server_hostent = gethostbyname(ads); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 41 | if (server_hostent == NULL) { |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 42 | lwsl_err("Unable to get host name from %s\n", ads); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 43 | goto oom4; |
| 44 | } |
| 45 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 46 | if (wsi->sock < 0) { |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 47 | |
| 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 Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | server_addr.sin_family = AF_INET; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 71 | server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr); |
Andy Green | 36efd82 | 2013-10-25 22:07:57 +0800 | [diff] [blame] | 72 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 73 | bzero(&server_addr.sin_zero, 8); |
| 74 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 75 | if (connect(wsi->sock, (struct sockaddr *)&server_addr, |
Andy Green | 6811457 | 2013-10-24 22:07:04 +0800 | [diff] [blame] | 76 | sizeof(struct sockaddr)) == -1 || errno == EISCONN) { |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 77 | |
| 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 | */ |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame^] | 85 | lws_change_pollfd(wsi, 0, POLLOUT); |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 86 | |
| 87 | return wsi; |
| 88 | } |
| 89 | |
shys | 5efcb3f | 2013-10-25 15:49:11 +0200 | [diff] [blame] | 90 | if (errno != EISCONN) { |
| 91 | |
| 92 | lwsl_debug("Connect failed errno=%d\n", errno); |
| 93 | goto failed; |
| 94 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 97 | lwsl_client("connected\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 98 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 99 | /* we are connected to server, or proxy */ |
| 100 | |
| 101 | if (context->http_proxy_port) { |
| 102 | |
Andy Green | 36efd82 | 2013-10-25 22:07:57 +0800 | [diff] [blame] | 103 | /* OK from now on we talk via the proxy, so connect to that */ |
| 104 | |
| 105 | /* |
| 106 | * (will overwrite existing pointer, |
| 107 | * leaving old string/frag there but unreferenced) |
| 108 | */ |
| 109 | if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, |
| 110 | context->http_proxy_address)) |
| 111 | goto failed; |
| 112 | wsi->u.hdr.ah->c_port = context->http_proxy_port; |
| 113 | |
Joachim Bauch | 8294c1f | 2013-06-29 10:22:09 +0800 | [diff] [blame] | 114 | n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 115 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 116 | lwsl_debug("ERROR writing to proxy socket\n"); |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 117 | goto failed; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | libwebsocket_set_timeout(wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 121 | PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, |
| 122 | AWAITING_TIMEOUT); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 123 | |
| 124 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY; |
| 125 | |
| 126 | return wsi; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * provoke service to issue the handshake directly |
| 131 | * we need to do it this way because in the proxy case, this is the |
| 132 | * next state and executed only if and when we get a good proxy |
Andy Green | 73abc25 | 2013-01-13 11:05:30 +0800 | [diff] [blame] | 133 | * response inside the state machine... but notice in SSL case this |
| 134 | * may not have sent anything yet with 0 return, and won't until some |
| 135 | * many retries from main loop. To stop that becoming endless, |
| 136 | * cover with a timeout. |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 137 | */ |
| 138 | |
Andy Green | 73abc25 | 2013-01-13 11:05:30 +0800 | [diff] [blame] | 139 | libwebsocket_set_timeout(wsi, |
| 140 | PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT); |
| 141 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 142 | wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE; |
| 143 | pfd.fd = wsi->sock; |
| 144 | pfd.revents = POLLIN; |
David Galeano | 36750b8 | 2013-01-09 16:17:04 +0800 | [diff] [blame] | 145 | |
| 146 | n = libwebsocket_service_fd(context, &pfd); |
| 147 | |
| 148 | if (n < 0) |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 149 | goto failed; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 150 | |
David Galeano | 36750b8 | 2013-01-09 16:17:04 +0800 | [diff] [blame] | 151 | if (n) /* returns 1 on failure after closing wsi */ |
| 152 | return NULL; |
| 153 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 154 | return wsi; |
| 155 | |
| 156 | oom4: |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 157 | free(wsi->u.hdr.ah); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 158 | free(wsi); |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 159 | return NULL; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 160 | |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 161 | failed: |
| 162 | libwebsocket_close_and_free_session(context, wsi, |
| 163 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 164 | return NULL; |
| 165 | } |
| 166 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 167 | /** |
| 168 | * libwebsocket_client_connect() - Connect to another websocket server |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 169 | * @context: Websocket context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 170 | * @address: Remote server address, eg, "myserver.com" |
| 171 | * @port: Port to connect to on the remote server, eg, 80 |
| 172 | * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self |
| 173 | * signed certs |
| 174 | * @path: Websocket path on server |
| 175 | * @host: Hostname on server |
| 176 | * @origin: Socket origin name |
| 177 | * @protocol: Comma-separated list of protocols being asked for from |
| 178 | * the server, or just one. The server will pick the one it |
| 179 | * likes best. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 180 | * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 181 | * protocol supported, or the specific protocol ordinal |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 182 | * |
| 183 | * This function creates a connection to a remote server |
| 184 | */ |
| 185 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 186 | LWS_VISIBLE struct libwebsocket * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 187 | libwebsocket_client_connect(struct libwebsocket_context *context, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 188 | const char *address, |
| 189 | int port, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 190 | int ssl_connection, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 191 | const char *path, |
| 192 | const char *host, |
| 193 | const char *origin, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 194 | const char *protocol, |
| 195 | int ietf_version_or_minus_one) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 196 | { |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 197 | struct libwebsocket *wsi; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 198 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 760c3d4 | 2013-02-18 10:43:18 +0800 | [diff] [blame] | 199 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 200 | int m; |
| 201 | struct libwebsocket_extension *ext; |
| 202 | int handled; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 203 | #endif |
| 204 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 205 | #ifndef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 206 | if (ssl_connection) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 207 | lwsl_err("libwebsockets not configured for ssl\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 208 | return NULL; |
| 209 | } |
| 210 | #endif |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 211 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 212 | wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket)); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 213 | if (wsi == NULL) |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 214 | goto bail; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 215 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 216 | memset(wsi, 0, sizeof(*wsi)); |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 217 | wsi->sock = -1; |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 218 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 219 | /* -1 means just use latest supported */ |
| 220 | |
| 221 | if (ietf_version_or_minus_one == -1) |
Andy Green | 193306c | 2011-02-26 11:08:46 +0000 | [diff] [blame] | 222 | ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 223 | |
| 224 | wsi->ietf_spec_revision = ietf_version_or_minus_one; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 225 | wsi->user_space = NULL; |
| 226 | wsi->state = WSI_STATE_CLIENT_UNCONNECTED; |
Andy Green | 927eb7b | 2011-02-01 08:52:55 +0000 | [diff] [blame] | 227 | wsi->protocol = NULL; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 228 | wsi->pending_timeout = NO_PENDING_TIMEOUT; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 229 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 230 | wsi->count_active_extensions = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 231 | #endif |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 232 | #ifdef LWS_OPENSSL_SUPPORT |
| 233 | wsi->use_ssl = ssl_connection; |
| 234 | #endif |
| 235 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 236 | if (lws_allocate_header_table(wsi)) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 237 | goto bail; |
| 238 | |
| 239 | /* |
| 240 | * we're not necessarily in a position to action these right away, |
| 241 | * stash them... we only need during connect phase so u.hdr is fine |
| 242 | */ |
Andy Green | a7521de | 2013-02-18 10:38:45 +0800 | [diff] [blame] | 243 | wsi->u.hdr.ah->c_port = port; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 244 | if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address)) |
| 245 | goto bail1; |
| 246 | |
| 247 | /* these only need u.hdr lifetime as well */ |
| 248 | |
| 249 | if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path)) |
| 250 | goto bail1; |
| 251 | |
| 252 | if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host)) |
| 253 | goto bail1; |
| 254 | |
| 255 | if (origin) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 256 | if (lws_hdr_simple_create(wsi, |
| 257 | _WSI_TOKEN_CLIENT_ORIGIN, origin)) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 258 | goto bail1; |
| 259 | /* |
| 260 | * this is a list of protocols we tell the server we're okay with |
| 261 | * stash it for later when we compare server response with it |
| 262 | */ |
| 263 | if (protocol) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 264 | if (lws_hdr_simple_create(wsi, |
| 265 | _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol)) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 266 | goto bail1; |
| 267 | |
| 268 | wsi->protocol = &context->protocols[0]; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 269 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 270 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 271 | /* |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 272 | * Check with each extension if it is able to route and proxy this |
| 273 | * connection for us. For example, an extension like x-google-mux |
| 274 | * can handle this and then we don't need an actual socket for this |
| 275 | * connection. |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 276 | */ |
| 277 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 278 | handled = 0; |
| 279 | ext = context->extensions; |
| 280 | n = 0; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 281 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 282 | while (ext && ext->callback && !handled) { |
| 283 | m = ext->callback(context, ext, wsi, |
| 284 | LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION, |
| 285 | (void *)(long)n, (void *)address, port); |
| 286 | if (m) |
| 287 | handled = 1; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 288 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 289 | ext++; |
| 290 | n++; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 293 | if (handled) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 294 | lwsl_client("libwebsocket_client_connect: ext handling conn\n"); |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 295 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 296 | libwebsocket_set_timeout(wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 297 | PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, |
| 298 | AWAITING_TIMEOUT); |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 299 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 300 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 301 | return wsi; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 302 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 303 | #endif |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 304 | lwsl_client("libwebsocket_client_connect: direct conn\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 305 | |
Markus Elfring | 7521233 | 2013-10-26 20:23:00 +0800 | [diff] [blame] | 306 | return libwebsocket_client_connect_2(context, wsi); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 307 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 308 | bail1: |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 309 | free(wsi->u.hdr.ah); |
| 310 | bail: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 311 | free(wsi); |
| 312 | |
| 313 | return NULL; |
| 314 | } |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 315 | |
| 316 | |
| 317 | /** |
| 318 | * libwebsocket_client_connect_extended() - Connect to another websocket server |
| 319 | * @context: Websocket context |
| 320 | * @address: Remote server address, eg, "myserver.com" |
| 321 | * @port: Port to connect to on the remote server, eg, 80 |
| 322 | * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self |
| 323 | * signed certs |
| 324 | * @path: Websocket path on server |
| 325 | * @host: Hostname on server |
| 326 | * @origin: Socket origin name |
| 327 | * @protocol: Comma-separated list of protocols being asked for from |
| 328 | * the server, or just one. The server will pick the one it |
| 329 | * likes best. |
| 330 | * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 331 | * protocol supported, or the specific protocol ordinal |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 332 | * @userdata: Pre-allocated user data |
| 333 | * |
| 334 | * This function creates a connection to a remote server |
| 335 | */ |
| 336 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 337 | LWS_VISIBLE struct libwebsocket * |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 338 | libwebsocket_client_connect_extended(struct libwebsocket_context *context, |
| 339 | const char *address, |
| 340 | int port, |
| 341 | int ssl_connection, |
| 342 | const char *path, |
| 343 | const char *host, |
| 344 | const char *origin, |
| 345 | const char *protocol, |
| 346 | int ietf_version_or_minus_one, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 347 | void *userdata) |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 348 | { |
| 349 | struct libwebsocket *ws = |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 350 | libwebsocket_client_connect(context, address, port, |
| 351 | ssl_connection, path, host, origin, protocol, |
| 352 | ietf_version_or_minus_one); |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 353 | |
| 354 | if (ws && !ws->user_space && userdata) |
| 355 | ws->user_space = userdata ; |
| 356 | |
| 357 | return ws ; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 358 | } |