#include "private-libwebsockets.h"
-struct lws *lws_client_connect_2(
- struct lws_context *context,
- struct lws *wsi
-) {
- struct lws_pollfd pfd;
+struct lws *
+lws_client_connect_2(struct lws_context *context, struct lws *wsi)
+{
#ifdef LWS_USE_IPV6
struct sockaddr_in6 server_addr6;
struct sockaddr_in6 client_addr6;
#endif
struct sockaddr_in server_addr4;
struct sockaddr_in client_addr4;
-
+ struct lws_pollfd pfd;
struct sockaddr *v;
- int n;
- int plen = 0;
+ int n, plen = 0;
const char *ads;
lwsl_client("lws_client_connect_2\n");
- /*
- * proxy?
- */
+ /* proxy? */
if (context->http_proxy_port) {
plen = sprintf((char *)context->service_buffer,
if (connect(wsi->sock, v, n) == -1 || LWS_ERRNO == LWS_EISCONN) {
- if (LWS_ERRNO == LWS_EALREADY
- || LWS_ERRNO == LWS_EINPROGRESS
- || LWS_ERRNO == LWS_EWOULDBLOCK
+ if (LWS_ERRNO == LWS_EALREADY ||
+ LWS_ERRNO == LWS_EINPROGRESS ||
+ LWS_ERRNO == LWS_EWOULDBLOCK
#ifdef _WIN32
|| LWS_ERRNO == WSAEINVAL
#endif
- )
- {
+ ) {
lwsl_client("nonblocking connect retry\n");
/*
if (context->http_proxy_port) {
- /* OK from now on we talk via the proxy, so connect to that */
-
/*
+ * OK from now on we talk via the proxy, so connect to that
+ *
* (will overwrite existing pointer,
* leaving old string/frag there but unreferenced)
*/
if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
- context->http_proxy_address))
+ context->http_proxy_address))
goto failed;
wsi->u.hdr.ah->c_port = context->http_proxy_port;
- n = send(wsi->sock, (char *)context->service_buffer, plen, MSG_NOSIGNAL);
+ n = send(wsi->sock, (char *)context->service_buffer, plen,
+ MSG_NOSIGNAL);
if (n < 0) {
lwsl_debug("ERROR writing to proxy socket\n");
goto failed;
}
- lws_set_timeout(wsi,
- PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
- AWAITING_TIMEOUT);
+ lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
+ AWAITING_TIMEOUT);
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
* cover with a timeout.
*/
- lws_set_timeout(wsi,
- PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, AWAITING_TIMEOUT);
+ lws_set_timeout(wsi, PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
+ AWAITING_TIMEOUT);
wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
pfd.fd = wsi->sock;
return NULL;
failed:
- lws_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NOSTATUS);
+ lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
+
return NULL;
}
*/
LWS_VISIBLE struct lws *
-lws_client_connect(struct lws_context *context,
- const char *address,
- int port,
- int ssl_connection,
- const char *path,
- const char *host,
- const char *origin,
- const char *protocol,
- int ietf_version_or_minus_one)
+lws_client_connect(struct lws_context *context, const char *address,
+ int port, int ssl_connection, const char *path,
+ const char *host, const char *origin,
+ const char *protocol, int ietf_version_or_minus_one)
{
struct lws *wsi;
goto bail1;
if (origin)
- if (lws_hdr_simple_create(wsi,
- _WSI_TOKEN_CLIENT_ORIGIN, origin))
+ if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_ORIGIN, origin))
goto bail1;
/*
* this is a list of protocols we tell the server we're okay with
* stash it for later when we compare server response with it
*/
if (protocol)
- if (lws_hdr_simple_create(wsi,
- _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, protocol))
+ if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
+ protocol))
goto bail1;
wsi->protocol = &context->protocols[0];
if (lws_ext_callback_for_each_extension_type(context, wsi,
LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
- (void *)address, port) > 0) {
+ (void *)address, port) > 0) {
lwsl_client("lws_client_connect: ext handling conn\n");
lws_set_timeout(wsi,
PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
- AWAITING_TIMEOUT);
+ AWAITING_TIMEOUT);
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
return wsi;
*/
LWS_VISIBLE struct lws *
-lws_client_connect_extended(struct lws_context *context,
- const char *address,
- int port,
- int ssl_connection,
- const char *path,
- const char *host,
- const char *origin,
- const char *protocol,
- int ietf_version_or_minus_one,
- void *userdata)
+lws_client_connect_extended(struct lws_context *context, const char *address,
+ int port, int ssl_connection, const char *path,
+ const char *host, const char *origin,
+ const char *protocol, int ietf_version_or_minus_one,
+ void *userdata)
{
- struct lws *ws =
- lws_client_connect(context, address, port,
- ssl_connection, path, host, origin, protocol,
- ietf_version_or_minus_one);
-
- if (ws && !ws->user_space && userdata) {
- ws->user_space_externally_allocated = 1;
- ws->user_space = userdata ;
+ struct lws *wsi;
+
+ wsi = lws_client_connect(context, address, port, ssl_connection, path,
+ host, origin, protocol,
+ ietf_version_or_minus_one);
+
+ if (wsi && !wsi->user_space && userdata) {
+ wsi->user_space_externally_allocated = 1;
+ wsi->user_space = userdata ;
}
- return ws ;
+ return wsi;
}
int lws_client_rx_sm(struct lws *wsi, unsigned char c)
{
int callback_action = LWS_CALLBACK_CLIENT_RECEIVE;
- int handled;
struct lws_tokens eff_buf;
- int m;
+ int handled, m;
switch (wsi->lws_rx_parse_state) {
case LWS_RXPS_NEW:
}
break;
-
case LWS_RXPS_04_FRAME_HDR_LEN:
wsi->u.ws.this_frame_masked = !!(c & 0x80);
case LWS_WS_OPCODE_07__PING:
lwsl_info("received %d byte ping, sending pong\n",
- wsi->u.ws.rx_user_buffer_head);
+ wsi->u.ws.rx_user_buffer_head);
if (wsi->u.ws.ping_pending_flag) {
/*
/* if existing buffer is too small, drop it */
if (wsi->u.ws.ping_payload_buf &&
- wsi->u.ws.ping_payload_alloc < wsi->u.ws.rx_user_buffer_head)
+ wsi->u.ws.ping_payload_alloc <
+ wsi->u.ws.rx_user_buffer_head)
lws_free2(wsi->u.ws.ping_payload_buf);
/* if no buffer, allocate it */
if (!wsi->u.ws.ping_payload_buf) {
- wsi->u.ws.ping_payload_buf = lws_malloc(wsi->u.ws.rx_user_buffer_head
- + LWS_SEND_BUFFER_PRE_PADDING);
+ wsi->u.ws.ping_payload_buf =
+ lws_malloc(wsi->u.ws.rx_user_buffer_head +
+ LWS_SEND_BUFFER_PRE_PADDING);
wsi->u.ws.ping_payload_alloc =
wsi->u.ws.rx_user_buffer_head;
}
case LWS_WS_OPCODE_07__PONG:
lwsl_info("client receied pong\n");
- lwsl_hexdump(&wsi->u.ws.rx_user_buffer[
- LWS_SEND_BUFFER_PRE_PADDING],
- wsi->u.ws.rx_user_buffer_head);
+ lwsl_hexdump(&wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
+ wsi->u.ws.rx_user_buffer_head);
/* issue it */
callback_action = LWS_CALLBACK_CLIENT_RECEIVE_PONG;
&eff_buf, 0) <= 0) { /* not handle or fail */
lwsl_ext("Unhandled ext opc 0x%x\n",
- wsi->u.ws.opcode);
+ wsi->u.ws.opcode);
wsi->u.ws.rx_user_buffer_head = 0;
return 0;
eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
if (lws_ext_callback_for_each_active(wsi,
- LWS_EXT_CALLBACK_PAYLOAD_RX,
- &eff_buf, 0) < 0) /* fail */
+ LWS_EXT_CALLBACK_PAYLOAD_RX,
+ &eff_buf, 0) < 0) /* fail */
return -1;
if (eff_buf.token_len <= 0 &&
- callback_action != LWS_CALLBACK_CLIENT_RECEIVE_PONG)
+ callback_action != LWS_CALLBACK_CLIENT_RECEIVE_PONG)
goto already_done;
eff_buf.token[eff_buf.token_len] = '\0';
if (callback_action == LWS_CALLBACK_CLIENT_RECEIVE_PONG)
lwsl_info("Client doing pong callback\n");
- m = wsi->protocol->callback(
- wsi->protocol->owning_server,
- wsi,
- (enum lws_callback_reasons)callback_action,
- wsi->user_space,
- eff_buf.token,
- eff_buf.token_len);
+ m = wsi->protocol->callback(wsi->protocol->owning_server,
+ wsi, (enum lws_callback_reasons)callback_action,
+ wsi->user_space, eff_buf.token, eff_buf.token_len);
/* if user code wants to close, let caller know */
if (m)
int lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len)
{
- int n;
+ unsigned int n;
switch (wsi->mode) {
case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
}
int lws_client_socket_service(struct lws_context *context,
- struct lws *wsi, struct lws_pollfd *pollfd)
+ struct lws *wsi, struct lws_pollfd *pollfd)
{
- int n;
char *p = (char *)&context->service_buffer[0];
- int len;
unsigned char c;
+ int n, len;
switch (wsi->mode) {
if (n < 0) {
if (LWS_ERRNO == LWS_EAGAIN) {
- lwsl_debug(
- "Proxy read returned EAGAIN... retrying\n");
+ lwsl_debug("Proxy read returned EAGAIN... retrying\n");
return 0;
}
lws_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NOSTATUS);
+ LWS_CLOSE_STATUS_NOSTATUS);
lwsl_err("ERROR reading from proxy socket\n");
return 0;
}
strcmp((char *)context->service_buffer, "HTTP/1.1 200 ")
) {
lws_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NOSTATUS);
+ LWS_CLOSE_STATUS_NOSTATUS);
lwsl_err("ERROR proxy: %s\n", context->service_buffer);
return 0;
}
int
lws_client_interpret_server_handshake(struct lws_context *context,
- struct lws *wsi)
+ struct lws *wsi)
{
+ int n, len, okay = 0, more = 1, isErrorCodeReceived = 0;
+ int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
const char *pc;
- int okay = 0;
char *p;
- int len;
- int isErrorCodeReceived = 0;
#ifndef LWS_NO_EXTENSIONS
- char ext_name[128];
struct lws_extension *ext;
- void *v;
- int more = 1;
+ char ext_name[128];
const char *c;
+ void *v;
#endif
- int n;
- int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
/*
* well, what the server sent looked reasonable for syntax.
wsi->protocol->callback(context, wsi,
LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
- wsi->user_space, NULL, 0);
+ wsi->user_space, NULL, 0);
/* clear his proxy connection timeout */
/* call him back to inform him he is up */
- wsi->protocol->callback(context, wsi,
- LWS_CALLBACK_CLIENT_ESTABLISHED,
- wsi->user_space, NULL, 0);
+ wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLIENT_ESTABLISHED,
+ wsi->user_space, NULL, 0);
#ifndef LWS_NO_EXTENSIONS
/*
* inform all extensions, not just active ones since they
if (isErrorCodeReceived && p) {
wsi->protocol->callback(context, wsi,
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
- wsi->user_space, p, (unsigned int)strlen(p));
+ wsi->user_space, p,
+ (unsigned int)strlen(p));
} else {
wsi->protocol->callback(context, wsi,
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
- wsi->user_space, NULL, 0);
+ wsi->user_space, NULL, 0);
}
}
lwsl_info("closing connection due to bail2 connection error\n");
/* free up his parsing allocations */
-
lws_free2(wsi->u.hdr.ah);
-
lws_close_and_free_session(context, wsi, close_reason);
return 1;
char *
lws_generate_client_handshake(struct lws_context *context,
- struct lws *wsi, char *pkt)
+ struct lws *wsi, char *pkt)
{
- char buf[128];
- char hash[20];
- char key_b64[40];
- char *p = pkt;
+ char buf[128], hash[20], key_b64[40], *p = pkt;
int n;
#ifndef LWS_NO_EXTENSIONS
struct lws_extension *ext;
/*
* create the random key
*/
-
n = lws_get_random(context, hash, 16);
if (n != 16) {
lwsl_err("Unable to read from random dev %s\n",
*/
p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a",
- lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
+ lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
- p += sprintf(p,
- "Pragma: no-cache\x0d\x0a""Cache-Control: no-cache\x0d\x0a");
+ p += sprintf(p, "Pragma: no-cache\x0d\x0a"
+ "Cache-Control: no-cache\x0d\x0a");
p += sprintf(p, "Host: %s\x0d\x0a",
- lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
- p += sprintf(p,
-"Upgrade: websocket\x0d\x0a""Connection: Upgrade\x0d\x0a""Sec-WebSocket-Key: ");
+ lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
+ p += sprintf(p, "Upgrade: websocket\x0d\x0a"
+ "Connection: Upgrade\x0d\x0a"
+ "Sec-WebSocket-Key: ");
strcpy(p, key_b64);
p += strlen(key_b64);
p += sprintf(p, "\x0d\x0a");
n = lws_ext_callback_for_each_extension_type(context, wsi,
LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
- (char *)ext->name, 0);
+ (char *)ext->name, 0);
if (n) { /* an extension vetos us */
lwsl_ext("ext %s vetoed\n", (char *)ext->name);
ext++;
if (wsi->ietf_spec_revision)
p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
- wsi->ietf_spec_revision);
+ wsi->ietf_spec_revision);
/* give userland a chance to append, eg, cookies */
context->protocols[0].callback(context, wsi,
- LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
- NULL, &p, (pkt + sizeof(context->service_buffer)) - p - 12);
+ LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
+ NULL, &p,
+ (pkt + sizeof(context->service_buffer)) - p - 12);
p += sprintf(p, "\x0d\x0a");
lws_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
lws_b64_encode_string(hash, 20,
- wsi->u.hdr.ah->initial_handshake_hash_base64,
- sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
+ wsi->u.hdr.ah->initial_handshake_hash_base64,
+ sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
return p;
}
lws_create_context(struct lws_context_creation_info *info)
{
struct lws_context *context = NULL;
- char *p;
#if LWS_POSIX
int pid_daemon = get_daemonize_pid();
#endif
+ char *p;
lwsl_notice("Initial logging level %d\n", log_level);
}
lws_plat_context_early_destroy(context);
-
lws_ssl_context_destroy(context);
if (context->fds)
static void
child_handler(int signum)
{
- int fd;
- int len;
- int sent;
+ int fd, len, sent;
char sz[20];
switch (signum) {
LWS_VISIBLE int
lws_daemonize(const char *_lock_path)
{
+ struct sigaction act;
pid_t sid, parent;
- int fd;
+ int n, fd, ret;
char buf[10];
- int n, ret;
- struct sigaction act;
/* already a daemon */
if (getppid() == 1)
if ((unsigned int)token >= ARRAY_SIZE(set))
return NULL;
+
return (unsigned char *)set[token];
}
-int lws_add_http_header_by_name(struct lws_context *context,
- struct lws *wsi,
+int
+lws_add_http_header_by_name(struct lws_context *context, struct lws *wsi,
const unsigned char *name,
- const unsigned char *value,
- int length,
- unsigned char **p,
- unsigned char *end)
+ const unsigned char *value, int length,
+ unsigned char **p, unsigned char *end)
{
#ifdef LWS_USE_HTTP2
if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING)
- return lws_add_http2_header_by_name(context, wsi, name, value, length, p, end);
+ return lws_add_http2_header_by_name(context, wsi, name,
+ value, length, p, end);
#else
(void)wsi;
(void)context;
*p += length;
*((*p)++) = '\x0d';
*((*p)++) = '\x0a';
+
return 0;
}
-int lws_finalize_http_header(struct lws_context *context,
- struct lws *wsi,
- unsigned char **p,
- unsigned char *end)
+int lws_finalize_http_header(struct lws_context *context, struct lws *wsi,
+ unsigned char **p, unsigned char *end)
{
(void)context;
#ifdef LWS_USE_HTTP2
return 1;
*((*p)++) = '\x0d';
*((*p)++) = '\x0a';
+
return 0;
}
-int lws_add_http_header_by_token(struct lws_context *context,
- struct lws *wsi,
- enum lws_token_indexes token,
- const unsigned char *value,
- int length,
- unsigned char **p,
- unsigned char *end)
+int
+lws_add_http_header_by_token(struct lws_context *context, struct lws *wsi,
+ enum lws_token_indexes token,
+ const unsigned char *value, int length,
+ unsigned char **p, unsigned char *end)
{
const unsigned char *name;
#ifdef LWS_USE_HTTP2
}
int lws_add_http_header_content_length(struct lws_context *context,
- struct lws *wsi,
- unsigned long content_length,
- unsigned char **p,
- unsigned char *end)
+ struct lws *wsi,
+ unsigned long content_length,
+ unsigned char **p, unsigned char *end)
{
char b[24];
int n;
"HTTP Version Not Supported"
};
-int lws_add_http_header_status(struct lws_context *context,
- struct lws *wsi,
- unsigned int code,
- unsigned char **p,
- unsigned char *end)
+int
+lws_add_http_header_status(struct lws_context *context, struct lws *wsi,
+ unsigned int code, unsigned char **p,
+ unsigned char *end)
{
unsigned char code_and_desc[60];
const char *description = "";
n = sprintf((char *)code_and_desc, "HTTP/1.0 %u %s", code, description);
- return lws_add_http_header_by_name(context, wsi, NULL, code_and_desc, n, p, end);
+ return lws_add_http_header_by_name(context, wsi, NULL, code_and_desc,
+ n, p, end);
}
/**
* Helper to report HTTP errors back to the client cleanly and
* consistently
*/
-LWS_VISIBLE int lws_return_http_status(
- struct lws_context *context, struct lws *wsi,
- unsigned int code, const char *html_body)
+LWS_VISIBLE int
+lws_return_http_status(struct lws_context *context, struct lws *wsi,
+ unsigned int code, const char *html_body)
{
int n, m;
- unsigned char *p = context->service_buffer + LWS_SEND_BUFFER_PRE_PADDING;
+ unsigned char *p = context->service_buffer +
+ LWS_SEND_BUFFER_PRE_PADDING;
unsigned char *start = p;
unsigned char *end = p + sizeof(context->service_buffer) -
- LWS_SEND_BUFFER_PRE_PADDING;
+ LWS_SEND_BUFFER_PRE_PADDING;
if (!html_body)
html_body = "";
if (lws_add_http_header_status(context, wsi, code, &p, end))
return 1;
- if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_SERVER, (unsigned char *)"libwebsockets", 13, &p, end))
+ if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_SERVER,
+ (unsigned char *)"libwebsockets", 13,
+ &p, end))
return 1;
- if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (unsigned char *)"text/html", 9, &p, end))
+ if (lws_add_http_header_by_token(context, wsi,
+ WSI_TOKEN_HTTP_CONTENT_TYPE,
+ (unsigned char *)"text/html", 9,
+ &p, end))
return 1;
if (lws_finalize_http_header(context, wsi, &p, end))
return 1;
if (m != (int)(p - start))
return 1;
- n = sprintf((char *)start, "<html><body><h1>%u</h1>%s</body></html>", code, html_body);
+ n = sprintf((char *)start, "<html><body><h1>%u</h1>%s</body></html>",
+ code, html_body);
m = lws_write(wsi, start, n, LWS_WRITE_HTTP);
return m != n;