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