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 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +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; |
| 8 | struct timeval tv; |
| 9 | struct hostent *server_hostent; |
| 10 | struct sockaddr_in server_addr; |
| 11 | int n; |
| 12 | int plen = 0; |
| 13 | char pkt[512]; |
| 14 | int opt = 1; |
M K | f243115 | 2011-09-25 10:34:35 +0100 | [diff] [blame] | 15 | #if defined(__APPLE__) |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 16 | struct protoent *tcp_proto; |
M K | f243115 | 2011-09-25 10:34:35 +0100 | [diff] [blame] | 17 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 18 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 19 | lwsl_client("__libwebsocket_client_connect_2\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 20 | |
| 21 | wsi->candidate_children_list = NULL; |
| 22 | |
| 23 | /* |
| 24 | * proxy? |
| 25 | */ |
| 26 | |
| 27 | if (context->http_proxy_port) { |
| 28 | plen = sprintf(pkt, "CONNECT %s:%u HTTP/1.0\x0d\x0a" |
| 29 | "User-agent: libwebsockets\x0d\x0a" |
| 30 | /*Proxy-authorization: basic aGVsbG86d29ybGQ= */ |
| 31 | "\x0d\x0a", wsi->c_address, wsi->c_port); |
| 32 | |
| 33 | /* OK from now on we talk via the proxy */ |
| 34 | |
| 35 | free(wsi->c_address); |
| 36 | wsi->c_address = strdup(context->http_proxy_address); |
| 37 | wsi->c_port = context->http_proxy_port; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * prepare the actual connection (to the proxy, if any) |
| 42 | */ |
| 43 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 44 | lwsl_client("__libwebsocket_client_connect_2: address %s", wsi->c_address); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 45 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 46 | server_hostent = gethostbyname(wsi->c_address); |
| 47 | if (server_hostent == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 48 | lwsl_err("Unable to get host name from %s\n", wsi->c_address); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 49 | goto oom4; |
| 50 | } |
| 51 | |
| 52 | wsi->sock = socket(AF_INET, SOCK_STREAM, 0); |
| 53 | |
| 54 | if (wsi->sock < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 55 | lwsl_warn("Unable to open socket\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 56 | goto oom4; |
| 57 | } |
| 58 | |
| 59 | server_addr.sin_family = AF_INET; |
| 60 | server_addr.sin_port = htons(wsi->c_port); |
| 61 | server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr); |
| 62 | bzero(&server_addr.sin_zero, 8); |
| 63 | |
| 64 | /* Disable Nagle */ |
M K | f243115 | 2011-09-25 10:34:35 +0100 | [diff] [blame] | 65 | #if !defined(__APPLE__) |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 66 | setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY, |
| 67 | (const void *)&opt, sizeof(opt)); |
M K | f243115 | 2011-09-25 10:34:35 +0100 | [diff] [blame] | 68 | #else |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 69 | tcp_proto = getprotobyname("TCP"); |
| 70 | setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY, |
| 71 | &opt, sizeof(opt)); |
M K | f243115 | 2011-09-25 10:34:35 +0100 | [diff] [blame] | 72 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 73 | |
| 74 | /* Set receiving timeout */ |
| 75 | tv.tv_sec = 0; |
| 76 | tv.tv_usec = 100 * 1000; |
| 77 | setsockopt(wsi->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv); |
| 78 | |
| 79 | if (connect(wsi->sock, (struct sockaddr *)&server_addr, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 80 | sizeof(struct sockaddr)) == -1) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 81 | lwsl_debug("Connect failed\n"); |
Tobias Maier | d1db83c | 2012-05-30 12:46:42 +0800 | [diff] [blame] | 82 | #ifdef WIN32 |
| 83 | closesocket(wsi->sock); |
| 84 | #else |
| 85 | close(wsi->sock); |
| 86 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 87 | goto oom4; |
| 88 | } |
| 89 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 90 | lwsl_client("connected\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 91 | |
| 92 | /* into fd -> wsi hashtable */ |
| 93 | |
| 94 | insert_wsi(context, wsi); |
| 95 | |
| 96 | /* into internal poll list */ |
| 97 | |
| 98 | context->fds[context->fds_count].fd = wsi->sock; |
| 99 | context->fds[context->fds_count].revents = 0; |
| 100 | context->fds[context->fds_count++].events = POLLIN; |
| 101 | |
| 102 | /* external POLL support via protocol 0 */ |
| 103 | context->protocols[0].callback(context, wsi, |
| 104 | LWS_CALLBACK_ADD_POLL_FD, |
| 105 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 106 | |
| 107 | /* we are connected to server, or proxy */ |
| 108 | |
| 109 | if (context->http_proxy_port) { |
| 110 | |
| 111 | n = send(wsi->sock, pkt, plen, 0); |
| 112 | if (n < 0) { |
| 113 | #ifdef WIN32 |
| 114 | closesocket(wsi->sock); |
| 115 | #else |
| 116 | close(wsi->sock); |
| 117 | #endif |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 118 | lwsl_debug("ERROR writing to proxy socket\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 119 | goto bail1; |
| 120 | } |
| 121 | |
| 122 | libwebsocket_set_timeout(wsi, |
David Galeano | c9f1ff8 | 2013-01-09 18:01:23 +0800 | [diff] [blame] | 123 | PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, AWAITING_TIMEOUT); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 124 | |
| 125 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY; |
| 126 | |
| 127 | return wsi; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * provoke service to issue the handshake directly |
| 132 | * we need to do it this way because in the proxy case, this is the |
| 133 | * next state and executed only if and when we get a good proxy |
| 134 | * response inside the state machine |
| 135 | */ |
| 136 | |
| 137 | wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE; |
| 138 | pfd.fd = wsi->sock; |
| 139 | pfd.revents = POLLIN; |
David Galeano | 36750b8 | 2013-01-09 16:17:04 +0800 | [diff] [blame] | 140 | |
| 141 | n = libwebsocket_service_fd(context, &pfd); |
| 142 | |
| 143 | if (n < 0) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 144 | goto oom4; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 145 | |
David Galeano | 36750b8 | 2013-01-09 16:17:04 +0800 | [diff] [blame] | 146 | if (n) /* returns 1 on failure after closing wsi */ |
| 147 | return NULL; |
| 148 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 149 | return wsi; |
| 150 | |
| 151 | oom4: |
| 152 | if (wsi->c_protocol) |
| 153 | free(wsi->c_protocol); |
| 154 | |
| 155 | if (wsi->c_origin) |
| 156 | free(wsi->c_origin); |
| 157 | |
| 158 | free(wsi->c_host); |
| 159 | free(wsi->c_path); |
| 160 | |
| 161 | bail1: |
| 162 | free(wsi); |
| 163 | |
| 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 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 186 | 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; |
| 198 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 199 | int m; |
| 200 | struct libwebsocket_extension *ext; |
| 201 | int handled; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 202 | #ifndef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 203 | if (ssl_connection) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 204 | lwsl_err("libwebsockets not configured for ssl\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | #endif |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 208 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 209 | wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket)); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 210 | if (wsi == NULL) |
| 211 | goto bail1; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 212 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 213 | memset(wsi, 0, sizeof *wsi); |
| 214 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 215 | /* -1 means just use latest supported */ |
| 216 | |
| 217 | if (ietf_version_or_minus_one == -1) |
Andy Green | 193306c | 2011-02-26 11:08:46 +0000 | [diff] [blame] | 218 | ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 219 | |
| 220 | wsi->ietf_spec_revision = ietf_version_or_minus_one; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 221 | wsi->name_buffer_pos = 0; |
| 222 | wsi->user_space = NULL; |
| 223 | wsi->state = WSI_STATE_CLIENT_UNCONNECTED; |
| 224 | wsi->pings_vs_pongs = 0; |
Andy Green | 927eb7b | 2011-02-01 08:52:55 +0000 | [diff] [blame] | 225 | wsi->protocol = NULL; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 226 | wsi->pending_timeout = NO_PENDING_TIMEOUT; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 227 | wsi->count_active_extensions = 0; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 228 | #ifdef LWS_OPENSSL_SUPPORT |
| 229 | wsi->use_ssl = ssl_connection; |
| 230 | #endif |
| 231 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 232 | wsi->c_port = port; |
| 233 | wsi->c_address = strdup(address); |
| 234 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 235 | /* copy parameters over so state machine has access */ |
| 236 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 237 | wsi->c_path = (char *)malloc(strlen(path) + 1); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 238 | if (wsi->c_path == NULL) |
| 239 | goto bail1; |
| 240 | strcpy(wsi->c_path, path); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 241 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 242 | wsi->c_host = (char *)malloc(strlen(host) + 1); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 243 | if (wsi->c_host == NULL) |
| 244 | goto oom1; |
| 245 | strcpy(wsi->c_host, host); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 246 | |
Andy Green | 08d3392 | 2011-02-26 10:22:49 +0000 | [diff] [blame] | 247 | if (origin) { |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 248 | wsi->c_origin = (char *)malloc(strlen(origin) + 1); |
Andy Green | 08d3392 | 2011-02-26 10:22:49 +0000 | [diff] [blame] | 249 | strcpy(wsi->c_origin, origin); |
| 250 | if (wsi->c_origin == NULL) |
| 251 | goto oom2; |
| 252 | } else |
| 253 | wsi->c_origin = NULL; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 254 | |
David Brooks | 993343b | 2012-04-20 12:16:52 +0800 | [diff] [blame] | 255 | wsi->c_callback = NULL; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 256 | if (protocol) { |
David Brooks | 993343b | 2012-04-20 12:16:52 +0800 | [diff] [blame] | 257 | const char *pc; |
| 258 | struct libwebsocket_protocols *pp; |
| 259 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 260 | wsi->c_protocol = (char *)malloc(strlen(protocol) + 1); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 261 | if (wsi->c_protocol == NULL) |
| 262 | goto oom3; |
David Brooks | 993343b | 2012-04-20 12:16:52 +0800 | [diff] [blame] | 263 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 264 | strcpy(wsi->c_protocol, protocol); |
David Brooks | 993343b | 2012-04-20 12:16:52 +0800 | [diff] [blame] | 265 | |
| 266 | pc = protocol; |
| 267 | while (*pc && *pc != ',') |
| 268 | pc++; |
| 269 | n = pc - protocol; |
| 270 | pp = context->protocols; |
| 271 | while (pp->name && !wsi->c_callback) { |
| 272 | if (!strncmp(protocol, pp->name, n)) |
| 273 | wsi->c_callback = pp->callback; |
| 274 | pp++; |
| 275 | } |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 276 | } else |
| 277 | wsi->c_protocol = NULL; |
| 278 | |
David Brooks | 993343b | 2012-04-20 12:16:52 +0800 | [diff] [blame] | 279 | if (!wsi->c_callback) |
| 280 | wsi->c_callback = context->protocols[0].callback; |
| 281 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 282 | /* set up appropriate masking */ |
| 283 | |
| 284 | wsi->xor_mask = xor_no_mask; |
| 285 | |
| 286 | switch (wsi->ietf_spec_revision) { |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 287 | case 0: |
| 288 | break; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 289 | case 4: |
| 290 | wsi->xor_mask = xor_mask_04; |
| 291 | break; |
| 292 | case 5: |
Andy Green | 9514bf8 | 2011-02-26 11:13:56 +0000 | [diff] [blame] | 293 | case 6: |
Andy Green | 33872cd | 2011-04-24 05:49:44 +0100 | [diff] [blame] | 294 | case 7: |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 295 | case 8: |
| 296 | case 13: |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 297 | wsi->xor_mask = xor_mask_05; |
| 298 | break; |
| 299 | default: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 300 | lwsl_parser("Client ietf version %d not supported\n", |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 301 | wsi->ietf_spec_revision); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 302 | goto oom4; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* force no mask if he asks for that though */ |
| 306 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 307 | if (context->options & LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK) |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 308 | wsi->xor_mask = xor_no_mask; |
| 309 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 310 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 311 | wsi->utf8_token[n].token = NULL; |
| 312 | wsi->utf8_token[n].token_len = 0; |
| 313 | } |
| 314 | |
| 315 | /* |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 316 | * Check with each extension if it is able to route and proxy this |
| 317 | * connection for us. For example, an extension like x-google-mux |
| 318 | * can handle this and then we don't need an actual socket for this |
| 319 | * connection. |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 320 | */ |
| 321 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 322 | handled = 0; |
| 323 | ext = context->extensions; |
| 324 | n = 0; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 325 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 326 | while (ext && ext->callback && !handled) { |
| 327 | m = ext->callback(context, ext, wsi, |
| 328 | LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION, |
| 329 | (void *)(long)n, (void *)address, port); |
| 330 | if (m) |
| 331 | handled = 1; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 332 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 333 | ext++; |
| 334 | n++; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 337 | if (handled) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 338 | lwsl_client("libwebsocket_client_connect: ext handling conn\n"); |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 339 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 340 | libwebsocket_set_timeout(wsi, |
David Galeano | c9f1ff8 | 2013-01-09 18:01:23 +0800 | [diff] [blame] | 341 | PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, AWAITING_TIMEOUT); |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 342 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 343 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 344 | return wsi; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame^] | 347 | lwsl_client("libwebsocket_client_connect: direct conn\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 348 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 349 | return __libwebsocket_client_connect_2(context, wsi); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 350 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 351 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 352 | oom4: |
| 353 | if (wsi->c_protocol) |
| 354 | free(wsi->c_protocol); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 355 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 356 | oom3: |
Andy Green | 08d3392 | 2011-02-26 10:22:49 +0000 | [diff] [blame] | 357 | if (wsi->c_origin) |
| 358 | free(wsi->c_origin); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 359 | |
| 360 | oom2: |
| 361 | free(wsi->c_host); |
| 362 | |
| 363 | oom1: |
| 364 | free(wsi->c_path); |
| 365 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 366 | bail1: |
| 367 | free(wsi); |
| 368 | |
| 369 | return NULL; |
| 370 | } |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 371 | |
| 372 | |
| 373 | /** |
| 374 | * libwebsocket_client_connect_extended() - Connect to another websocket server |
| 375 | * @context: Websocket context |
| 376 | * @address: Remote server address, eg, "myserver.com" |
| 377 | * @port: Port to connect to on the remote server, eg, 80 |
| 378 | * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self |
| 379 | * signed certs |
| 380 | * @path: Websocket path on server |
| 381 | * @host: Hostname on server |
| 382 | * @origin: Socket origin name |
| 383 | * @protocol: Comma-separated list of protocols being asked for from |
| 384 | * the server, or just one. The server will pick the one it |
| 385 | * likes best. |
| 386 | * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest |
| 387 | * protocol supported, or the specific protocol ordinal |
| 388 | * @userdata: Pre-allocated user data |
| 389 | * |
| 390 | * This function creates a connection to a remote server |
| 391 | */ |
| 392 | |
| 393 | struct libwebsocket * |
| 394 | libwebsocket_client_connect_extended(struct libwebsocket_context *context, |
| 395 | const char *address, |
| 396 | int port, |
| 397 | int ssl_connection, |
| 398 | const char *path, |
| 399 | const char *host, |
| 400 | const char *origin, |
| 401 | const char *protocol, |
| 402 | int ietf_version_or_minus_one, |
| 403 | void *userdata) |
| 404 | { |
| 405 | struct libwebsocket *ws = |
| 406 | libwebsocket_client_connect(context, address, port, ssl_connection, path, host, origin, protocol, ietf_version_or_minus_one) ; |
| 407 | |
| 408 | if (ws && !ws->user_space && userdata) |
| 409 | ws->user_space = userdata ; |
| 410 | |
| 411 | return ws ; |
| 412 | } |