blob: 1a0d362534d2da87df07fc0a7a2841cc6594682a [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001#include "private-libwebsockets.h"
Andy Green4739e5c2011-01-22 12:51:57 +00002
Andy Green6ee372f2012-04-09 15:09:01 +08003struct libwebsocket *__libwebsocket_client_connect_2(
Andy Greena41314f2011-05-23 10:00:03 +01004 struct libwebsocket_context *context,
5 struct libwebsocket *wsi
6) {
7 struct pollfd pfd;
Andy Greena41314f2011-05-23 10:00:03 +01008 struct hostent *server_hostent;
9 struct sockaddr_in server_addr;
10 int n;
11 int plen = 0;
Andy Greene77fb802013-02-11 13:04:45 +080012 const char *ads;
Andy Greena41314f2011-05-23 10:00:03 +010013
Andy Green43db0452013-01-10 19:50:35 +080014 lwsl_client("__libwebsocket_client_connect_2\n");
Andy Greena41314f2011-05-23 10:00:03 +010015
16 /*
17 * proxy?
18 */
19
20 if (context->http_proxy_port) {
Andy Greene48ba312013-02-10 15:34:59 +080021 plen = sprintf((char *)context->service_buffer,
Andy Greenf54a94b2013-02-10 15:19:39 +080022 "CONNECT %s:%u HTTP/1.0\x0d\x0a"
Andy Greena41314f2011-05-23 10:00:03 +010023 "User-agent: libwebsockets\x0d\x0a"
24/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
Andy Greene77fb802013-02-11 13:04:45 +080025 "\x0d\x0a",
26 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
27 wsi->u.hdr.c_port);
Andy Greena41314f2011-05-23 10:00:03 +010028
Andy Greene77fb802013-02-11 13:04:45 +080029 /* OK from now on we talk via the proxy, so connect to that */
Andy Greena41314f2011-05-23 10:00:03 +010030
Andy Greene77fb802013-02-11 13:04:45 +080031 /* (will overwrite existing pointer, leaving old string/frag there but unreferenced) */
32 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, context->http_proxy_address))
33 goto oom4;
34 wsi->u.hdr.c_port = context->http_proxy_port;
Andy Greena41314f2011-05-23 10:00:03 +010035 }
36
37 /*
38 * prepare the actual connection (to the proxy, if any)
39 */
40
Andy Greene77fb802013-02-11 13:04:45 +080041 ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
Andy Green66a16f32011-05-24 22:07:45 +010042
Andy Greene77fb802013-02-11 13:04:45 +080043 lwsl_client("__libwebsocket_client_connect_2: address %s\n", ads);
44
45 server_hostent = gethostbyname(ads);
Andy Greena41314f2011-05-23 10:00:03 +010046 if (server_hostent == NULL) {
Andy Greene77fb802013-02-11 13:04:45 +080047 lwsl_err("Unable to get host name from %s\n", ads);
Andy Greena41314f2011-05-23 10:00:03 +010048 goto oom4;
49 }
50
51 wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
52
53 if (wsi->sock < 0) {
Andy Green43db0452013-01-10 19:50:35 +080054 lwsl_warn("Unable to open socket\n");
Andy Greena41314f2011-05-23 10:00:03 +010055 goto oom4;
56 }
57
58 server_addr.sin_family = AF_INET;
Andy Greene77fb802013-02-11 13:04:45 +080059 server_addr.sin_port = htons(wsi->u.hdr.c_port);
Andy Greena41314f2011-05-23 10:00:03 +010060 server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
61 bzero(&server_addr.sin_zero, 8);
62
Andy Greena41314f2011-05-23 10:00:03 +010063 if (connect(wsi->sock, (struct sockaddr *)&server_addr,
Andy Green6ee372f2012-04-09 15:09:01 +080064 sizeof(struct sockaddr)) == -1) {
Andy Green43db0452013-01-10 19:50:35 +080065 lwsl_debug("Connect failed\n");
Andy Green3fc2c652013-01-14 15:35:02 +080066 compatible_close(wsi->sock);
Andy Greena41314f2011-05-23 10:00:03 +010067 goto oom4;
68 }
69
Andy Green43db0452013-01-10 19:50:35 +080070 lwsl_client("connected\n");
Andy Greena41314f2011-05-23 10:00:03 +010071
Andy Greena690cd02013-02-09 12:25:31 +080072 if (lws_set_socket_options(context, wsi->sock)) {
73 lwsl_err("Failed to set wsi socket options\n");
74 close(wsi->sock);
75 goto oom4;
76 }
77
Andy Greendfb23042013-01-17 12:26:48 +080078 insert_wsi_socket_into_fds(context, wsi);
Andy Greena41314f2011-05-23 10:00:03 +010079
80 /* we are connected to server, or proxy */
81
82 if (context->http_proxy_port) {
83
Andy Greenf54a94b2013-02-10 15:19:39 +080084 n = send(wsi->sock, context->service_buffer, plen, 0);
Andy Greena41314f2011-05-23 10:00:03 +010085 if (n < 0) {
Andy Green3fc2c652013-01-14 15:35:02 +080086 compatible_close(wsi->sock);
Andy Green43db0452013-01-10 19:50:35 +080087 lwsl_debug("ERROR writing to proxy socket\n");
Andy Greena41314f2011-05-23 10:00:03 +010088 goto bail1;
89 }
90
91 libwebsocket_set_timeout(wsi,
David Galeanoc9f1ff82013-01-09 18:01:23 +080092 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, AWAITING_TIMEOUT);
Andy Greena41314f2011-05-23 10:00:03 +010093
94 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
95
96 return wsi;
97 }
98
99 /*
100 * provoke service to issue the handshake directly
101 * we need to do it this way because in the proxy case, this is the
102 * next state and executed only if and when we get a good proxy
Andy Green73abc252013-01-13 11:05:30 +0800103 * response inside the state machine... but notice in SSL case this
104 * may not have sent anything yet with 0 return, and won't until some
105 * many retries from main loop. To stop that becoming endless,
106 * cover with a timeout.
Andy Greena41314f2011-05-23 10:00:03 +0100107 */
108
Andy Green73abc252013-01-13 11:05:30 +0800109 libwebsocket_set_timeout(wsi,
110 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
111
Andy Greena41314f2011-05-23 10:00:03 +0100112 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
113 pfd.fd = wsi->sock;
114 pfd.revents = POLLIN;
David Galeano36750b82013-01-09 16:17:04 +0800115
116 n = libwebsocket_service_fd(context, &pfd);
117
118 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +0800119 goto oom4;
Andy Greena41314f2011-05-23 10:00:03 +0100120
David Galeano36750b82013-01-09 16:17:04 +0800121 if (n) /* returns 1 on failure after closing wsi */
122 return NULL;
123
Andy Greena41314f2011-05-23 10:00:03 +0100124 return wsi;
125
126oom4:
Andy Greene77fb802013-02-11 13:04:45 +0800127 free(wsi->u.hdr.ah);
Andy Greena41314f2011-05-23 10:00:03 +0100128bail1:
129 free(wsi);
130
131 return NULL;
132}
133
Andy Green90c7cbc2011-01-27 06:26:52 +0000134/**
135 * libwebsocket_client_connect() - Connect to another websocket server
Peter Hinz56885f32011-03-02 22:03:47 +0000136 * @context: Websocket context
Andy Green90c7cbc2011-01-27 06:26:52 +0000137 * @address: Remote server address, eg, "myserver.com"
138 * @port: Port to connect to on the remote server, eg, 80
139 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
140 * signed certs
141 * @path: Websocket path on server
142 * @host: Hostname on server
143 * @origin: Socket origin name
144 * @protocol: Comma-separated list of protocols being asked for from
145 * the server, or just one. The server will pick the one it
146 * likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000147 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
Andy Green6ee372f2012-04-09 15:09:01 +0800148 * protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000149 *
150 * This function creates a connection to a remote server
151 */
152
Andy Green4739e5c2011-01-22 12:51:57 +0000153struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +0000154libwebsocket_client_connect(struct libwebsocket_context *context,
Andy Green4739e5c2011-01-22 12:51:57 +0000155 const char *address,
156 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000157 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000158 const char *path,
159 const char *host,
160 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000161 const char *protocol,
162 int ietf_version_or_minus_one)
Andy Green4739e5c2011-01-22 12:51:57 +0000163{
Andy Green4739e5c2011-01-22 12:51:57 +0000164 struct libwebsocket *wsi;
165 int n;
Andy Green3182ece2013-01-20 17:08:31 +0800166#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100167 int m;
168 struct libwebsocket_extension *ext;
169 int handled;
Andy Green3182ece2013-01-20 17:08:31 +0800170#endif
171
Andy Greenbe93fef2011-02-14 20:25:43 +0000172#ifndef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000173 if (ssl_connection) {
Andy Green43db0452013-01-10 19:50:35 +0800174 lwsl_err("libwebsockets not configured for ssl\n");
Andy Green90c7cbc2011-01-27 06:26:52 +0000175 return NULL;
176 }
177#endif
Andy Green4739e5c2011-01-22 12:51:57 +0000178
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800179 wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
Andy Greenbe93fef2011-02-14 20:25:43 +0000180 if (wsi == NULL)
181 goto bail1;
Andy Green4739e5c2011-01-22 12:51:57 +0000182
Darin Willitsc19456f2011-02-14 17:52:39 +0000183 memset(wsi, 0, sizeof *wsi);
184
Andy Greenbfb051f2011-02-09 08:49:14 +0000185 /* -1 means just use latest supported */
186
187 if (ietf_version_or_minus_one == -1)
Andy Green193306c2011-02-26 11:08:46 +0000188 ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
Andy Greenbfb051f2011-02-09 08:49:14 +0000189
190 wsi->ietf_spec_revision = ietf_version_or_minus_one;
Andy Green623a98d2013-01-21 11:04:23 +0800191 wsi->u.hdr.name_buffer_pos = 0;
Andy Green4739e5c2011-01-22 12:51:57 +0000192 wsi->user_space = NULL;
193 wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
Andy Green927eb7b2011-02-01 08:52:55 +0000194 wsi->protocol = NULL;
Andy Greena71eafc2011-02-14 17:59:43 +0000195 wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green3182ece2013-01-20 17:08:31 +0800196#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000197 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800198#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000199#ifdef LWS_OPENSSL_SUPPORT
200 wsi->use_ssl = ssl_connection;
201#endif
202
Andy Green16ab3182013-02-10 18:02:31 +0800203 if (lws_allocate_header_table(wsi))
Andy Greene77fb802013-02-11 13:04:45 +0800204 goto bail;
205
206 /*
207 * we're not necessarily in a position to action these right away,
208 * stash them... we only need during connect phase so u.hdr is fine
209 */
210 wsi->u.hdr.c_port = port;
211 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
212 goto bail1;
213
214 /* these only need u.hdr lifetime as well */
215
216 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
217 goto bail1;
218
219 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
220 goto bail1;
221
222 if (origin)
223 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_ORIGIN, origin))
224 goto bail1;
225 /*
226 * this is a list of protocols we tell the server we're okay with
227 * stash it for later when we compare server response with it
228 */
229 if (protocol)
230 if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
231 goto bail1;
232
233 wsi->protocol = &context->protocols[0];
Andy Green4739e5c2011-01-22 12:51:57 +0000234
Andy Green3182ece2013-01-20 17:08:31 +0800235#ifndef LWS_NO_EXTENSIONS
Andy Green4739e5c2011-01-22 12:51:57 +0000236 /*
Andy Greena41314f2011-05-23 10:00:03 +0100237 * Check with each extension if it is able to route and proxy this
238 * connection for us. For example, an extension like x-google-mux
239 * can handle this and then we don't need an actual socket for this
240 * connection.
Andy Green9659f372011-01-27 22:01:43 +0000241 */
242
Andy Greena41314f2011-05-23 10:00:03 +0100243 handled = 0;
244 ext = context->extensions;
245 n = 0;
Andy Green9659f372011-01-27 22:01:43 +0000246
Andy Greena41314f2011-05-23 10:00:03 +0100247 while (ext && ext->callback && !handled) {
248 m = ext->callback(context, ext, wsi,
249 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
250 (void *)(long)n, (void *)address, port);
251 if (m)
252 handled = 1;
Andy Green9659f372011-01-27 22:01:43 +0000253
Andy Greena41314f2011-05-23 10:00:03 +0100254 ext++;
255 n++;
Andy Green9659f372011-01-27 22:01:43 +0000256 }
257
Andy Greena41314f2011-05-23 10:00:03 +0100258 if (handled) {
Andy Green43db0452013-01-10 19:50:35 +0800259 lwsl_client("libwebsocket_client_connect: ext handling conn\n");
Andy Green5b9a4c02011-01-28 09:39:29 +0000260
Andy Greenbe93fef2011-02-14 20:25:43 +0000261 libwebsocket_set_timeout(wsi,
David Galeanoc9f1ff82013-01-09 18:01:23 +0800262 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, AWAITING_TIMEOUT);
Andy Green5b9a4c02011-01-28 09:39:29 +0000263
Andy Greena41314f2011-05-23 10:00:03 +0100264 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
Andy Greenbe93fef2011-02-14 20:25:43 +0000265 return wsi;
Andy Green9659f372011-01-27 22:01:43 +0000266 }
Andy Green3182ece2013-01-20 17:08:31 +0800267#endif
Andy Green43db0452013-01-10 19:50:35 +0800268 lwsl_client("libwebsocket_client_connect: direct conn\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000269
Andy Greena41314f2011-05-23 10:00:03 +0100270 return __libwebsocket_client_connect_2(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000271
Andy Green4739e5c2011-01-22 12:51:57 +0000272bail1:
Andy Greene77fb802013-02-11 13:04:45 +0800273 free(wsi->u.hdr.ah);
274bail:
Andy Green4739e5c2011-01-22 12:51:57 +0000275 free(wsi);
276
277 return NULL;
278}
David Brooks2c60d952012-04-20 12:19:01 +0800279
280
281/**
282 * libwebsocket_client_connect_extended() - Connect to another websocket server
283 * @context: Websocket context
284 * @address: Remote server address, eg, "myserver.com"
285 * @port: Port to connect to on the remote server, eg, 80
286 * @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
287 * signed certs
288 * @path: Websocket path on server
289 * @host: Hostname on server
290 * @origin: Socket origin name
291 * @protocol: Comma-separated list of protocols being asked for from
292 * the server, or just one. The server will pick the one it
293 * likes best.
294 * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
295 * protocol supported, or the specific protocol ordinal
296 * @userdata: Pre-allocated user data
297 *
298 * This function creates a connection to a remote server
299 */
300
301struct libwebsocket *
302libwebsocket_client_connect_extended(struct libwebsocket_context *context,
303 const char *address,
304 int port,
305 int ssl_connection,
306 const char *path,
307 const char *host,
308 const char *origin,
309 const char *protocol,
310 int ietf_version_or_minus_one,
311 void *userdata)
312{
313 struct libwebsocket *ws =
314 libwebsocket_client_connect(context, address, port, ssl_connection, path, host, origin, protocol, ietf_version_or_minus_one) ;
315
316 if (ws && !ws->user_space && userdata)
317 ws->user_space = userdata ;
318
319 return ws ;
320 }