Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1 | #include "private-libwebsockets.h" |
| 2 | #include <netdb.h> |
| 3 | |
| 4 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 5 | /** |
| 6 | * libwebsocket_client_connect() - Connect to another websocket server |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 7 | * @context: Websocket context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 8 | * @address: Remote server address, eg, "myserver.com" |
| 9 | * @port: Port to connect to on the remote server, eg, 80 |
| 10 | * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self |
| 11 | * signed certs |
| 12 | * @path: Websocket path on server |
| 13 | * @host: Hostname on server |
| 14 | * @origin: Socket origin name |
| 15 | * @protocol: Comma-separated list of protocols being asked for from |
| 16 | * the server, or just one. The server will pick the one it |
| 17 | * likes best. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 18 | * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest |
| 19 | * protocol supported, or the specific protocol ordinal |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 20 | * |
| 21 | * This function creates a connection to a remote server |
| 22 | */ |
| 23 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 24 | struct libwebsocket * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 25 | libwebsocket_client_connect(struct libwebsocket_context *context, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 26 | const char *address, |
| 27 | int port, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 28 | int ssl_connection, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 29 | const char *path, |
| 30 | const char *host, |
| 31 | const char *origin, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 32 | const char *protocol, |
| 33 | int ietf_version_or_minus_one) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 34 | { |
| 35 | struct hostent *server_hostent; |
| 36 | struct sockaddr_in server_addr; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 37 | char pkt[512]; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 38 | struct pollfd pfd; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 39 | struct libwebsocket *wsi; |
| 40 | int n; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 41 | int plen = 0; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 42 | #ifndef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 43 | if (ssl_connection) { |
| 44 | fprintf(stderr, "libwebsockets not configured for ssl\n"); |
| 45 | return NULL; |
| 46 | } |
| 47 | #endif |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 48 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 49 | wsi = malloc(sizeof(struct libwebsocket)); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 50 | if (wsi == NULL) |
| 51 | goto bail1; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 52 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 53 | memset(wsi, 0, sizeof *wsi); |
| 54 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 55 | /* -1 means just use latest supported */ |
| 56 | |
| 57 | if (ietf_version_or_minus_one == -1) |
Andy Green | 193306c | 2011-02-26 11:08:46 +0000 | [diff] [blame] | 58 | ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 59 | |
| 60 | wsi->ietf_spec_revision = ietf_version_or_minus_one; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 61 | wsi->name_buffer_pos = 0; |
| 62 | wsi->user_space = NULL; |
| 63 | wsi->state = WSI_STATE_CLIENT_UNCONNECTED; |
| 64 | wsi->pings_vs_pongs = 0; |
Andy Green | 927eb7b | 2011-02-01 08:52:55 +0000 | [diff] [blame] | 65 | wsi->protocol = NULL; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 66 | wsi->pending_timeout = NO_PENDING_TIMEOUT; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame^] | 67 | wsi->count_active_extensions = 0; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 68 | #ifdef LWS_OPENSSL_SUPPORT |
| 69 | wsi->use_ssl = ssl_connection; |
| 70 | #endif |
| 71 | |
| 72 | /* copy parameters over so state machine has access */ |
| 73 | |
| 74 | wsi->c_path = malloc(strlen(path) + 1); |
| 75 | if (wsi->c_path == NULL) |
| 76 | goto bail1; |
| 77 | strcpy(wsi->c_path, path); |
| 78 | wsi->c_host = malloc(strlen(host) + 1); |
| 79 | if (wsi->c_host == NULL) |
| 80 | goto oom1; |
| 81 | strcpy(wsi->c_host, host); |
Andy Green | 08d3392 | 2011-02-26 10:22:49 +0000 | [diff] [blame] | 82 | if (origin) { |
| 83 | wsi->c_origin = malloc(strlen(origin) + 1); |
| 84 | strcpy(wsi->c_origin, origin); |
| 85 | if (wsi->c_origin == NULL) |
| 86 | goto oom2; |
| 87 | } else |
| 88 | wsi->c_origin = NULL; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 89 | if (protocol) { |
| 90 | wsi->c_protocol = malloc(strlen(protocol) + 1); |
| 91 | if (wsi->c_protocol == NULL) |
| 92 | goto oom3; |
| 93 | strcpy(wsi->c_protocol, protocol); |
| 94 | } else |
| 95 | wsi->c_protocol = NULL; |
| 96 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 97 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 98 | /* set up appropriate masking */ |
| 99 | |
| 100 | wsi->xor_mask = xor_no_mask; |
| 101 | |
| 102 | switch (wsi->ietf_spec_revision) { |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 103 | case 0: |
| 104 | break; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 105 | case 4: |
| 106 | wsi->xor_mask = xor_mask_04; |
| 107 | break; |
| 108 | case 5: |
Andy Green | 9514bf8 | 2011-02-26 11:13:56 +0000 | [diff] [blame] | 109 | case 6: |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 110 | wsi->xor_mask = xor_mask_05; |
| 111 | break; |
| 112 | default: |
| 113 | fprintf(stderr, |
| 114 | "Client ietf version %d not supported\n", |
| 115 | wsi->ietf_spec_revision); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 116 | goto oom4; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* force no mask if he asks for that though */ |
| 120 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 121 | if (context->options & LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK) |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 122 | wsi->xor_mask = xor_no_mask; |
| 123 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 124 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 125 | wsi->utf8_token[n].token = NULL; |
| 126 | wsi->utf8_token[n].token_len = 0; |
| 127 | } |
| 128 | |
| 129 | /* |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 130 | * proxy? |
| 131 | */ |
| 132 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 133 | if (context->http_proxy_port) { |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 134 | plen = sprintf(pkt, "CONNECT %s:%u HTTP/1.0\x0d\x0a" |
| 135 | "User-agent: libwebsockets\x0d\x0a" |
| 136 | /*Proxy-authorization: basic aGVsbG86d29ybGQ= */ |
| 137 | "\x0d\x0a", address, port); |
| 138 | |
| 139 | /* OK from now on we talk via the proxy */ |
| 140 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 141 | address = context->http_proxy_address; |
| 142 | port = context->http_proxy_port; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * prepare the actual connection (to the proxy, if any) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 147 | */ |
| 148 | |
| 149 | server_hostent = gethostbyname(address); |
| 150 | if (server_hostent == NULL) { |
| 151 | fprintf(stderr, "Unable to get host name from %s\n", address); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 152 | goto oom4; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | wsi->sock = socket(AF_INET, SOCK_STREAM, 0); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 156 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 157 | if (wsi->sock < 0) { |
| 158 | fprintf(stderr, "Unable to open socket\n"); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 159 | goto oom4; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 162 | server_addr.sin_family = AF_INET; |
| 163 | server_addr.sin_port = htons(port); |
| 164 | server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr); |
| 165 | bzero(&server_addr.sin_zero, 8); |
| 166 | |
| 167 | if (connect(wsi->sock, (struct sockaddr *)&server_addr, |
| 168 | sizeof(struct sockaddr)) == -1) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 169 | fprintf(stderr, "Connect failed\n"); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 170 | goto oom4; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 171 | } |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 172 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 173 | /* into fd -> wsi hashtable */ |
| 174 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 175 | insert_wsi(context, wsi); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 176 | |
| 177 | /* into internal poll list */ |
| 178 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 179 | context->fds[context->fds_count].fd = wsi->sock; |
| 180 | context->fds[context->fds_count].revents = 0; |
| 181 | context->fds[context->fds_count++].events = POLLIN; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 182 | |
| 183 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 184 | context->protocols[0].callback(context, wsi, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 185 | LWS_CALLBACK_ADD_POLL_FD, |
| 186 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 187 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 188 | /* we are connected to server, or proxy */ |
| 189 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 190 | if (context->http_proxy_port) { |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 191 | |
| 192 | n = send(wsi->sock, pkt, plen, 0); |
| 193 | if (n < 0) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 194 | #ifdef WIN32 |
| 195 | closesocket(wsi->sock); |
| 196 | #else |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 197 | close(wsi->sock); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 198 | #endif |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 199 | fprintf(stderr, "ERROR writing to proxy socket\n"); |
| 200 | goto bail1; |
| 201 | } |
| 202 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 203 | libwebsocket_set_timeout(wsi, |
| 204 | PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, 5); |
Andy Green | 5b9a4c0 | 2011-01-28 09:39:29 +0000 | [diff] [blame] | 205 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 206 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 207 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 208 | return wsi; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 211 | /* |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 212 | * provoke service to issue the handshake directly |
| 213 | * we need to do it this way because in the proxy case, this is the |
| 214 | * next state and executed only if and when we get a good proxy |
| 215 | * response inside the state machine |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 216 | */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 217 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 218 | wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 219 | pfd.fd = wsi->sock; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 220 | pfd.revents = POLLIN; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 221 | libwebsocket_service_fd(context, &pfd); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 222 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 223 | return wsi; |
| 224 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 225 | oom4: |
| 226 | if (wsi->c_protocol) |
| 227 | free(wsi->c_protocol); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 228 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 229 | oom3: |
Andy Green | 08d3392 | 2011-02-26 10:22:49 +0000 | [diff] [blame] | 230 | if (wsi->c_origin) |
| 231 | free(wsi->c_origin); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 232 | |
| 233 | oom2: |
| 234 | free(wsi->c_host); |
| 235 | |
| 236 | oom1: |
| 237 | free(wsi->c_path); |
| 238 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 239 | bail1: |
| 240 | free(wsi); |
| 241 | |
| 242 | return NULL; |
| 243 | } |