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