blob: 49a31bca3a9e8eb1603fcd42e6d8c3b82e53d241 [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001#include "private-libwebsockets.h"
2#include <netdb.h>
3
4
Andy Green90c7cbc2011-01-27 06:26:52 +00005/**
6 * libwebsocket_client_connect() - Connect to another websocket server
7 * @this: Websocket context
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 Greenbfb051f2011-02-09 08:49:14 +000018 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
19 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +000020 *
21 * This function creates a connection to a remote server
22 */
23
Andy Green4739e5c2011-01-22 12:51:57 +000024struct libwebsocket *
Andy Green90c7cbc2011-01-27 06:26:52 +000025libwebsocket_client_connect(struct libwebsocket_context *this,
Andy Green4739e5c2011-01-22 12:51:57 +000026 const char *address,
27 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +000028 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +000029 const char *path,
30 const char *host,
31 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +000032 const char *protocol,
33 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +000034{
35 struct hostent *server_hostent;
36 struct sockaddr_in server_addr;
Andy Greenbe93fef2011-02-14 20:25:43 +000037 char pkt[512];
Andy Green4739e5c2011-01-22 12:51:57 +000038 struct pollfd pfd;
Andy Green4739e5c2011-01-22 12:51:57 +000039 struct libwebsocket *wsi;
40 int n;
Andy Green9659f372011-01-27 22:01:43 +000041 int plen = 0;
Andy Greenbe93fef2011-02-14 20:25:43 +000042#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +000043 if (ssl_connection) {
44 fprintf(stderr, "libwebsockets not configured for ssl\n");
45 return NULL;
46 }
47#endif
Andy Green4739e5c2011-01-22 12:51:57 +000048
Andy Green6964bb52011-01-23 16:50:33 +000049 wsi = malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +000050 if (wsi == NULL)
51 goto bail1;
Andy Green4739e5c2011-01-22 12:51:57 +000052
Darin Willitsc19456f2011-02-14 17:52:39 +000053 memset(wsi, 0, sizeof *wsi);
54
Andy Greenbfb051f2011-02-09 08:49:14 +000055 /* -1 means just use latest supported */
56
57 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +000058 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +000059
60 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green4739e5c2011-01-22 12:51:57 +000061 wsi->name_buffer_pos = 0;
62 wsi->user_space = NULL;
63 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
64 wsi->pings_vs_pongs = 0;
Andy Green927eb7b2011-02-01 08:52:55 +000065 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +000066 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Greenbe93fef2011-02-14 20:25:43 +000067#ifdef LWS_OPENSSL_SUPPORT
68 wsi->use_ssl = ssl_connection;
69#endif
70
71 /* copy parameters over so state machine has access */
72
73 wsi->c_path = malloc(strlen(path) + 1);
74 if (wsi->c_path == NULL)
75 goto bail1;
76 strcpy(wsi->c_path, path);
77 wsi->c_host = malloc(strlen(host) + 1);
78 if (wsi->c_host == NULL)
79 goto oom1;
80 strcpy(wsi->c_host, host);
Andy Green08d33922011-02-26 10:22:49 +000081 if (origin) {
82 wsi->c_origin = malloc(strlen(origin) + 1);
83 strcpy(wsi->c_origin, origin);
84 if (wsi->c_origin == NULL)
85 goto oom2;
86 } else
87 wsi->c_origin = NULL;
Andy Greenbe93fef2011-02-14 20:25:43 +000088 if (protocol) {
89 wsi->c_protocol = malloc(strlen(protocol) + 1);
90 if (wsi->c_protocol == NULL)
91 goto oom3;
92 strcpy(wsi->c_protocol, protocol);
93 } else
94 wsi->c_protocol = NULL;
95
Andy Green4739e5c2011-01-22 12:51:57 +000096
Andy Greenbfb051f2011-02-09 08:49:14 +000097 /* set up appropriate masking */
98
99 wsi->xor_mask = xor_no_mask;
100
101 switch (wsi->ietf_spec_revision) {
102 case 4:
103 wsi->xor_mask = xor_mask_04;
104 break;
105 case 5:
106 wsi->xor_mask = xor_mask_05;
107 break;
108 default:
109 fprintf(stderr,
110 "Client ietf version %d not supported\n",
111 wsi->ietf_spec_revision);
Andy Greenbe93fef2011-02-14 20:25:43 +0000112 goto oom4;
Andy Greenbfb051f2011-02-09 08:49:14 +0000113 }
114
115 /* force no mask if he asks for that though */
116
117 if (this->options & LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK)
118 wsi->xor_mask = xor_no_mask;
119
Andy Green4739e5c2011-01-22 12:51:57 +0000120 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
121 wsi->utf8_token[n].token = NULL;
122 wsi->utf8_token[n].token_len = 0;
123 }
124
125 /*
Andy Green9659f372011-01-27 22:01:43 +0000126 * proxy?
127 */
128
129 if (this->http_proxy_port) {
130 plen = sprintf(pkt, "CONNECT %s:%u HTTP/1.0\x0d\x0a"
131 "User-agent: libwebsockets\x0d\x0a"
132/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
133 "\x0d\x0a", address, port);
134
135 /* OK from now on we talk via the proxy */
136
137 address = this->http_proxy_address;
138 port = this->http_proxy_port;
139 }
140
141 /*
142 * prepare the actual connection (to the proxy, if any)
Andy Green4739e5c2011-01-22 12:51:57 +0000143 */
144
145 server_hostent = gethostbyname(address);
146 if (server_hostent == NULL) {
147 fprintf(stderr, "Unable to get host name from %s\n", address);
Andy Greenbe93fef2011-02-14 20:25:43 +0000148 goto oom4;
Andy Green4739e5c2011-01-22 12:51:57 +0000149 }
150
151 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
Andy Green6964bb52011-01-23 16:50:33 +0000152
Andy Green4739e5c2011-01-22 12:51:57 +0000153 if (wsi->sock < 0) {
154 fprintf(stderr, "Unable to open socket\n");
Andy Greenbe93fef2011-02-14 20:25:43 +0000155 goto oom4;
Andy Green4739e5c2011-01-22 12:51:57 +0000156 }
157
Andy Green4739e5c2011-01-22 12:51:57 +0000158 server_addr.sin_family = AF_INET;
159 server_addr.sin_port = htons(port);
160 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
161 bzero(&server_addr.sin_zero, 8);
162
163 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
164 sizeof(struct sockaddr)) == -1) {
Andy Green90c7cbc2011-01-27 06:26:52 +0000165 fprintf(stderr, "Connect failed\n");
Andy Greenbe93fef2011-02-14 20:25:43 +0000166 goto oom4;
Andy Green6964bb52011-01-23 16:50:33 +0000167 }
Andy Green4739e5c2011-01-22 12:51:57 +0000168
Andy Greenbe93fef2011-02-14 20:25:43 +0000169 /* into fd -> wsi hashtable */
170
171 insert_wsi(this, wsi);
172
173 /* into internal poll list */
174
175 this->fds[this->fds_count].fd = wsi->sock;
176 this->fds[this->fds_count].revents = 0;
177 this->fds[this->fds_count++].events = POLLIN;
178
179 /* external POLL support via protocol 0 */
180 this->protocols[0].callback(this, wsi,
181 LWS_CALLBACK_ADD_POLL_FD,
182 (void *)(long)wsi->sock, NULL, POLLIN);
183
Andy Green9659f372011-01-27 22:01:43 +0000184 /* we are connected to server, or proxy */
185
Andy Green9659f372011-01-27 22:01:43 +0000186 if (this->http_proxy_port) {
187
188 n = send(wsi->sock, pkt, plen, 0);
189 if (n < 0) {
Andy Green5b9a4c02011-01-28 09:39:29 +0000190 close(wsi->sock);
191 fprintf(stderr, "ERROR writing to proxy socket\n");
192 goto bail1;
193 }
194
Andy Greenbe93fef2011-02-14 20:25:43 +0000195 libwebsocket_set_timeout(wsi,
196 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, 5);
Andy Green5b9a4c02011-01-28 09:39:29 +0000197
Andy Greenbe93fef2011-02-14 20:25:43 +0000198 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
Andy Green9659f372011-01-27 22:01:43 +0000199
Andy Greenbe93fef2011-02-14 20:25:43 +0000200 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000201 }
202
Andy Green6964bb52011-01-23 16:50:33 +0000203 /*
Andy Greenbe93fef2011-02-14 20:25:43 +0000204 * provoke service to issue the handshake directly
205 * we need to do it this way because in the proxy case, this is the
206 * next state and executed only if and when we get a good proxy
207 * response inside the state machine
Andy Green6964bb52011-01-23 16:50:33 +0000208 */
Andy Green4739e5c2011-01-22 12:51:57 +0000209
Andy Greenbe93fef2011-02-14 20:25:43 +0000210 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
Andy Green4739e5c2011-01-22 12:51:57 +0000211 pfd.fd = wsi->sock;
Andy Greenbe93fef2011-02-14 20:25:43 +0000212 pfd.revents = POLLIN;
213 libwebsocket_service_fd(this, &pfd);
Andy Green4739e5c2011-01-22 12:51:57 +0000214
Andy Green4739e5c2011-01-22 12:51:57 +0000215 return wsi;
216
Andy Greenbe93fef2011-02-14 20:25:43 +0000217oom4:
218 if (wsi->c_protocol)
219 free(wsi->c_protocol);
Andy Green4739e5c2011-01-22 12:51:57 +0000220
Andy Greenbe93fef2011-02-14 20:25:43 +0000221oom3:
Andy Green08d33922011-02-26 10:22:49 +0000222 if (wsi->c_origin)
223 free(wsi->c_origin);
Andy Greenbe93fef2011-02-14 20:25:43 +0000224
225oom2:
226 free(wsi->c_host);
227
228oom1:
229 free(wsi->c_path);
230
Andy Green4739e5c2011-01-22 12:51:57 +0000231bail1:
232 free(wsi);
233
234 return NULL;
235}