1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

introduce logging api and convert all library output to use it

- multiple debug context calls lwsl_ err, warn, debug, parser, ext, client

 - api added to set which contexts output to stderr using a bitfield log_level

 - --disable-debug on configure removes all code that is not err or warn severity

 - err and warn contexts always output to stderr unless disabled by log_level

 - err and warn enabled by default in log_level

Signed-off-by: Andy Green <andy@warmcat.com>
This commit is contained in:
Andy Green 2013-01-10 19:50:35 +08:00 committed by Andy Green
parent 4550f1d7b5
commit 43db045ff8
12 changed files with 375 additions and 300 deletions

View file

@ -109,6 +109,18 @@ AC_ARG_ENABLE(noping,
AM_CONDITIONAL(NOPING, test x$noping = xyes)
#
#
#
AC_ARG_ENABLE(debug,
[ --disable-debug Stops debug-related code from even being compiled in, useful for best speed],
[ disable_debug=yes
])
if test "x$disable_debug" != "xyes" ; then
CFLAGS="$CFLAGS -D_DEBUG"
fi
AM_CONDITIONAL(DISABLE_DEBUG, test x$disable_debug = xyes)
# Checks for header files.

View file

@ -41,6 +41,7 @@
#include <stdio.h>
#include <string.h>
#include <private-libwebsockets.h>
static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789+/";
@ -169,7 +170,7 @@ lws_b64_selftest(void)
n = lws_b64_encode_string(plaintext[test],
strlen(plaintext[test]), buf, sizeof buf);
if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
fprintf(stderr, "Failed lws_b64 encode selftest "
lwsl_err("Failed lws_b64 encode selftest "
"%d result '%s' %d\n", test, buf, n);
return -1;
}
@ -178,7 +179,7 @@ lws_b64_selftest(void)
n = lws_b64_decode_string(coded[test], buf, sizeof buf);
if (n != strlen(plaintext[test]) ||
strcmp(buf, plaintext[test])) {
fprintf(stderr, "Failed lws_b64 decode selftest "
lwsl_err("Failed lws_b64 decode selftest "
"%d result '%s' %d\n", test, buf, n);
return -1;
}

View file

@ -16,7 +16,7 @@ struct libwebsocket *__libwebsocket_client_connect_2(
struct protoent *tcp_proto;
#endif
debug("__libwebsocket_client_connect_2\n");
lwsl_client("__libwebsocket_client_connect_2\n");
wsi->candidate_children_list = NULL;
@ -41,19 +41,18 @@ struct libwebsocket *__libwebsocket_client_connect_2(
* prepare the actual connection (to the proxy, if any)
*/
debug("__libwebsocket_client_connect_2: address %s", wsi->c_address);
lwsl_client("__libwebsocket_client_connect_2: address %s", wsi->c_address);
server_hostent = gethostbyname(wsi->c_address);
if (server_hostent == NULL) {
fprintf(stderr, "Unable to get host name from %s\n",
wsi->c_address);
lwsl_err("Unable to get host name from %s\n", wsi->c_address);
goto oom4;
}
wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
if (wsi->sock < 0) {
fprintf(stderr, "Unable to open socket\n");
lwsl_warn("Unable to open socket\n");
goto oom4;
}
@ -79,7 +78,7 @@ struct libwebsocket *__libwebsocket_client_connect_2(
if (connect(wsi->sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) {
fprintf(stderr, "Connect failed\n");
lwsl_debug("Connect failed\n");
#ifdef WIN32
closesocket(wsi->sock);
#else
@ -88,7 +87,7 @@ struct libwebsocket *__libwebsocket_client_connect_2(
goto oom4;
}
debug("connected\n");
lwsl_client("connected\n");
/* into fd -> wsi hashtable */
@ -116,7 +115,7 @@ struct libwebsocket *__libwebsocket_client_connect_2(
#else
close(wsi->sock);
#endif
fprintf(stderr, "ERROR writing to proxy socket\n");
lwsl_debug("ERROR writing to proxy socket\n");
goto bail1;
}
@ -202,7 +201,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
int handled;
#ifndef LWS_OPENSSL_SUPPORT
if (ssl_connection) {
fprintf(stderr, "libwebsockets not configured for ssl\n");
lwsl_err("libwebsockets not configured for ssl\n");
return NULL;
}
#endif
@ -298,8 +297,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
wsi->xor_mask = xor_mask_05;
break;
default:
fprintf(stderr,
"Client ietf version %d not supported\n",
lwsl_parser("Client ietf version %d not supported\n",
wsi->ietf_spec_revision);
goto oom4;
}
@ -337,7 +335,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
}
if (handled) {
debug("libwebsocket_client_connect: ext handling conn\n");
lwsl_client("libwebsocket_client_connect: ext handling conn\n");
libwebsocket_set_timeout(wsi,
PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, AWAITING_TIMEOUT);
@ -346,7 +344,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
return wsi;
}
debug("libwebsocket_client_connect: direct conn\n");
lwsl_client("libwebsocket_client_connect: direct conn\n");
return __libwebsocket_client_connect_2(context, wsi);

View file

@ -36,7 +36,7 @@ int lws_extension_callback_deflate_frame(
conn->zs_in.opaque = conn->zs_out.opaque = Z_NULL;
n = inflateInit2(&conn->zs_in, -LWS_ZLIB_WINDOW_BITS);
if (n != Z_OK) {
fprintf(stderr, "deflateInit returned %d", n);
lwsl_ext("deflateInit returned %d", n);
return 1;
}
n = deflateInit2(&conn->zs_out,
@ -47,7 +47,7 @@ int lws_extension_callback_deflate_frame(
-LWS_ZLIB_WINDOW_BITS, LWS_ZLIB_MEMLEVEL,
Z_DEFAULT_STRATEGY);
if (n != Z_OK) {
fprintf(stderr, "deflateInit2 returned %d", n);
lwsl_ext("deflateInit2 returned %d", n);
return 1;
}
conn->buf_pre_used = 0;
@ -68,10 +68,10 @@ int lws_extension_callback_deflate_frame(
LWS_SEND_BUFFER_POST_PADDING);
if (!conn->buf_out)
goto bail;
fprintf(stderr, "zlibs constructed\n");
lwsl_ext("zlibs constructed\n");
break;
bail:
fprintf(stderr, "Out of mem\n");
lwsl_err("Out of mem\n");
(void)inflateEnd(&conn->zs_in);
(void)deflateEnd(&conn->zs_out);
return -1;
@ -88,7 +88,7 @@ bail:
conn->compressed_out = 0;
(void)inflateEnd(&conn->zs_in);
(void)deflateEnd(&conn->zs_out);
fprintf(stderr, "zlibs destructed\n");
lwsl_ext("zlibs destructed\n");
break;
case LWS_EXT_CALLBACK_PAYLOAD_RX:
@ -113,7 +113,7 @@ bail:
conn->buf_pre =
(unsigned char *)malloc(total_payload + 4);
if (!conn->buf_pre) {
fprintf(stderr, "Out of memory\n");
lwsl_err("Out of memory\n");
return -1;
}
}
@ -162,7 +162,7 @@ bail:
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
fprintf(stderr, "zlib error inflate %d: %s",
lwsl_ext("zlib error inflate %d: %s",
n, conn->zs_in.msg);
return -1;
}
@ -196,7 +196,7 @@ bail:
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
fprintf(stderr, "zlib error deflate");
lwsl_ext("zlib error deflate");
return -1;
}
@ -216,7 +216,7 @@ bail:
conn->buf_out_length +
LWS_SEND_BUFFER_POST_PADDING);
if (!new_buf) {
fprintf(stderr, "Out of memory\n");
lwsl_err("Out of memory\n");
return -1;
}
memcpy(new_buf + LWS_SEND_BUFFER_PRE_PADDING,

View file

@ -32,7 +32,7 @@ int lws_extension_callback_deflate_stream(
conn->zs_in.opaque = conn->zs_out.opaque = Z_NULL;
n = inflateInit2(&conn->zs_in, -LWS_ZLIB_WINDOW_BITS);
if (n != Z_OK) {
fprintf(stderr, "deflateInit returned %d\n", n);
lwsl_err("deflateInit returned %d\n", n);
return 1;
}
n = deflateInit2(&conn->zs_out,
@ -40,17 +40,17 @@ int lws_extension_callback_deflate_stream(
-LWS_ZLIB_WINDOW_BITS, LWS_ZLIB_MEMLEVEL,
Z_DEFAULT_STRATEGY);
if (n != Z_OK) {
fprintf(stderr, "deflateInit returned %d\n", n);
lwsl_err("deflateInit returned %d\n", n);
return 1;
}
debug("zlibs constructed\n");
lwsl_ext("zlibs constructed\n");
conn->remaining_in = 0;
break;
case LWS_EXT_CALLBACK_DESTROY:
(void)inflateEnd(&conn->zs_in);
(void)deflateEnd(&conn->zs_out);
debug("zlibs destructed\n");
lwsl_ext("zlibs destructed\n");
break;
case LWS_EXT_CALLBACK_PACKET_RX_PREPARSE:
@ -84,7 +84,7 @@ int lws_extension_callback_deflate_stream(
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
fprintf(stderr, "zlib error inflate %d\n", n);
lwsl_err("zlib error inflate %d\n", n);
return -1;
}
@ -136,7 +136,7 @@ int lws_extension_callback_deflate_stream(
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
fprintf(stderr, "zlib error deflate\n");
lwsl_ext("zlib error deflate\n");
return -1;
}

View file

@ -154,7 +154,7 @@ lws_extension_x_google_mux_parser(struct libwebsocket_context *context,
int n;
void *v;
// fprintf(stderr, "XRX: %02X %d %d\n", c, conn->state, conn->length);
// lwsl_debug("XRX: %02X %d %d\n", c, conn->state, conn->length);
/*
* [ <Channel ID b12.. b8> <Mux Opcode b2..b0> ]
@ -164,7 +164,7 @@ lws_extension_x_google_mux_parser(struct libwebsocket_context *context,
switch (conn->state) {
case LWS_EXT_XGM_STATE__MUX_BLOCK_1:
// fprintf(stderr, "LWS_EXT_XGM_STATE__MUX_BLOCK_1: opc=%d channel=%d\n", c & 7, c >> 3);
// lwsl_ext("LWS_EXT_XGM_STATE__MUX_BLOCK_1: opc=%d channel=%d\n", c & 7, c >> 3);
conn->block_subopcode = (enum lws_ext_x_goole_mux__mux_opcodes)(c & 7);
conn->block_subchannel = (c >> 3) & 0x1f;
conn->ignore_cmd = 0;
@ -184,7 +184,7 @@ lws_extension_x_google_mux_parser(struct libwebsocket_context *context,
conn->block_subchannel |= c;
interpret:
// fprintf(stderr, "LWS_EXT_XGM_STATE__MUX_BLOCK_3: subchannel=%d\n", conn->block_subchannel);
// lwsl_ext("LWS_EXT_XGM_STATE__MUX_BLOCK_3: subchannel=%d\n", conn->block_subchannel);
ongoing_subchannel = conn->block_subchannel;
/*
@ -229,7 +229,7 @@ interpret:
conn->state = LWS_EXT_XGM_STATE__FLOWCONTROL_1;
break;
default:
fprintf(stderr, "xgm: unknown subopcode\n");
lwsl_ext("xgm: unknown subopcode\n");
return -1;
}
break;
@ -285,14 +285,14 @@ interpret:
if (conn->block_subchannel == 0 || conn->block_subchannel >=
(sizeof(conn->wsi_children) /
sizeof(conn->wsi_children[0]))) {
fprintf(stderr, "Illegal subchannel %d in "
lwsl_ext("Illegal subchannel %d in "
"LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS, ignoring",
conn->block_subchannel);
conn->ignore_cmd = 1;
}
if (conn->block_subchannel == 1 && !conn->original_ch1_closed) {
fprintf(stderr, "illegal request to add ch1 when it's still live, ignoring\n");
lwsl_ext("illegal request to add ch1 when it's still live, ignoring\n");
conn->ignore_cmd = 1;
}
@ -362,7 +362,7 @@ interpret:
wsi_child->user_space = malloc(
wsi_child->protocol->per_session_data_size);
if (wsi_child->user_space == NULL) {
fprintf(stderr, "Out of memory for "
lwsl_ext("Out of memory for "
"conn user space\n");
goto bail2;
}
@ -447,7 +447,7 @@ bail2:
child_conn = (struct lws_ext_x_google_mux_conn *)lws_get_extension_user_matching_ext(wsi_child,
this_ext);
if (!child_conn) {
fprintf(stderr, "wsi_child %p has no child conn!", (void *)wsi_child);
lwsl_ext("wsi_child %p has no child conn!", (void *)wsi_child);
break;
}
child_conn->wsi_parent = wsi;
@ -468,7 +468,7 @@ bail2:
wsi_child->user_space = malloc(
wsi_child->protocol->per_session_data_size);
if (wsi_child->user_space == NULL) {
fprintf(stderr, "Out of memory for "
lwsl_err("Out of memory for "
"conn user space\n");
break;
}
@ -519,7 +519,7 @@ bail2:
case LWS_EXT_XGM_STATE__DATA:
// fprintf(stderr, "LWS_EXT_XGM_STATE__DATA in\n");
// lwsl_debug("LWS_EXT_XGM_STATE__DATA in\n");
/*
* we have cooked websocket frame content following just like
@ -533,15 +533,15 @@ bail2:
* afterwards
*/
if (conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET >= conn->highest_child_subchannel) {
fprintf(stderr, "Illegal subchannel %d\n", conn->block_subchannel);
lwsl_ext("Illegal subchannel %d\n", conn->block_subchannel);
return -1;
}
// fprintf(stderr, "LWS_EXT_XGM_STATE__DATA: ch %d\n", conn->block_subchannel);
// lwsl_debug("LWS_EXT_XGM_STATE__DATA: ch %d\n", conn->block_subchannel);
if (conn->block_subchannel == 1) {
if (conn->original_ch1_closed) {
fprintf(stderr, "data sent to closed ch1\n");
lwsl_debug("data sent to closed ch1\n");
return -1;
}
wsi_child = wsi;
@ -549,7 +549,7 @@ bail2:
wsi_child = conn->wsi_children[conn->block_subchannel - MUX_REAL_CHILD_INDEX_OFFSET];
if (!wsi_child) {
fprintf(stderr, "Bad subchannel %d\n", conn->block_subchannel);
lwsl_ext("Bad subchannel %d\n", conn->block_subchannel);
return -1;
}
@ -561,7 +561,7 @@ bail2:
case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
case LWS_CONNMODE_WS_CLIENT:
// fprintf(stderr, " client\n");
// lwsl_ext(" client\n");
if (libwebsocket_client_rx_sm(wsi_child, c) < 0) {
libwebsocket_close_and_free_session(
context,
@ -574,7 +574,7 @@ bail2:
/* server is receiving from client */
default:
// fprintf(stderr, " server\n");
// lwsl_ext(" server\n");
if (libwebsocket_rx_sm(wsi_child, c) < 0) {
muxdebug("probs\n");
libwebsocket_close_and_free_session(
@ -726,7 +726,7 @@ int lws_extension_callback_x_google_mux(
if (parent_conn->highest_child_subchannel >=
(sizeof(parent_conn->wsi_children) /
sizeof(parent_conn->wsi_children[0]))) {
fprintf(stderr, "Can't add any more children\n");
lwsl_ext("Can't add any more children\n");
continue;
}
/*
@ -786,7 +786,7 @@ int lws_extension_callback_x_google_mux(
conn->original_ch1_closed = 1;
fprintf(stderr, "original mux parent channel closing\n");
lwsl_ext("original mux parent channel closing\n");
parent_conn = conn;
} else {
@ -811,14 +811,14 @@ int lws_extension_callback_x_google_mux(
/* if he has children, don't let him close for real! */
if (done) {
fprintf(stderr, "VETO closure\n");
lwsl_ext("VETO closure\n");
return 1;
}
/* no children, ch1 is closed, let him destroy himself */
if (conn->subchannel == 1)
fprintf(stderr, "ALLOW closure of mux parent\n");
lwsl_ext("ALLOW closure of mux parent\n");
break;
@ -837,7 +837,7 @@ int lws_extension_callback_x_google_mux(
conn->original_ch1_closed = 1;
fprintf(stderr, "original mux parent channel closing\n");
lwsl_ext("original mux parent channel closing\n");
parent_conn = conn;
wsi_parent = wsi;
@ -1023,7 +1023,7 @@ handle_additions:
*/
if (conn->original_ch1_closed) {
fprintf(stderr, "Trying to send on dead original ch1\n");
lwsl_ext("Trying to send on dead original ch1\n");
return 0;
}
@ -1038,7 +1038,7 @@ handle_additions:
*/
if (!conn->wsi_parent) {
// fprintf(stderr, "conn %p has no parent\n", (void *)conn);
// lwsl_ext("conn %p has no parent\n", (void *)conn);
return 0;
}
@ -1058,7 +1058,7 @@ handle_additions:
*/
if (!parent_conn->sticky_mux_used) {
// fprintf(stderr, "parent in singular mode\n");
// lwsl_ext("parent in singular mode\n");
return 0;
}
}

View file

@ -65,7 +65,7 @@ interpret_key(const char *key, unsigned long *result)
}
if (rem) {
fprintf(stderr, "nonzero handshake remainder\n");
lwsl_warn("nonzero handshake remainder\n");
return -1;
}
@ -111,7 +111,7 @@ handshake_00(struct libwebsocket_context *context, struct libwebsocket *wsi)
wsi->utf8_token[WSI_TOKEN_GET_URI].token_len +
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
if (!response) {
fprintf(stderr, "Out of memory for response buffer\n");
lwsl_err("Out of memory for response buffer\n");
goto bail;
}
@ -174,14 +174,14 @@ handshake_00(struct libwebsocket_context *context, struct libwebsocket *wsi)
/* it's complete: go ahead and send it */
_debug("issuing response packet %d len\n", (int)(p - response));
lwsl_parser("issuing response packet %d len\n", (int)(p - response));
#ifdef _DEBUG
fwrite(response, 1, p - response, stderr);
#endif
n = libwebsocket_write(wsi, (unsigned char *)response,
p - response, LWS_WRITE_HTTP);
if (n < 0) {
fprintf(stderr, "ERROR writing to socket");
lwsl_debug("ERROR writing to socket");
goto bail;
}
@ -233,14 +233,14 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
if (!wsi->utf8_token[WSI_TOKEN_HOST].token_len ||
!wsi->utf8_token[WSI_TOKEN_KEY].token_len) {
_debug("handshake_04 missing pieces\n");
lwsl_parser("handshake_04 missing pieces\n");
/* completed header processing, but missing some bits */
goto bail;
}
if (wsi->utf8_token[WSI_TOKEN_KEY].token_len >=
MAX_WEBSOCKET_04_KEY_LEN) {
fprintf(stderr, "Client sent handshake key longer "
lwsl_warn("Client sent handshake key longer "
"than max supported %d\n", MAX_WEBSOCKET_04_KEY_LEN);
goto bail;
}
@ -256,7 +256,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
accept_len = lws_b64_encode_string((char *)hash, 20, accept_buf,
sizeof accept_buf);
if (accept_len < 0) {
fprintf(stderr, "Base64 encoded hash too long\n");
lwsl_warn("Base64 encoded hash too long\n");
goto bail;
}
@ -274,7 +274,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len +
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
if (!response) {
fprintf(stderr, "Out of memory for response buffer\n");
lwsl_err("Out of memory for response buffer\n");
goto bail;
}
@ -294,7 +294,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
n = libwebsockets_get_random(wsi->protocol->owning_server,
hash, 16);
if (n != 16) {
fprintf(stderr, "Unable to read random device %s %d\n",
lwsl_err("Unable to read random device %s %d\n",
SYSTEM_RANDOM_FILEPATH, n);
if (wsi->user_space)
free(wsi->user_space);
@ -306,7 +306,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
nonce_len = lws_b64_encode_string((const char *)hash, 16,
nonce_buf, sizeof nonce_buf);
if (nonce_len < 0) {
fprintf(stderr, "Failed to base 64 encode the nonce\n");
lwsl_err("Failed to base 64 encode the nonce\n");
if (wsi->user_space)
free(wsi->user_space);
goto bail;
@ -336,7 +336,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
*/
c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
debug("wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token = %s\n",
lwsl_parser("wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token = %s\n",
wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token);
wsi->count_active_extensions = 0;
n = 0;
@ -425,7 +425,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
wsi->count_active_extensions], NULL, 0);
wsi->count_active_extensions++;
debug("wsi->count_active_extensions <- %d",
lwsl_parser("wsi->count_active_extensions <- %d",
wsi->count_active_extensions);
ext++;
@ -469,14 +469,14 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
/* okay send the handshake response accepting the connection */
_debug("issuing response packet %d len\n", (int)(p - response));
lwsl_parser("issuing response packet %d len\n", (int)(p - response));
#ifdef DEBUG
fwrite(response, 1, p - response, stderr);
#endif
n = libwebsocket_write(wsi, (unsigned char *)response,
p - response, LWS_WRITE_HTTP);
if (n < 0) {
fprintf(stderr, "ERROR writing to socket");
lwsl_debug("ERROR writing to socket");
goto bail;
}
@ -549,9 +549,9 @@ libwebsocket_read(struct libwebsocket_context *context,
/* fallthru */
case WSI_STATE_HTTP_HEADERS:
_debug("issuing %d bytes to parser\n", (int)len);
lwsl_parser("issuing %d bytes to parser\n", (int)len);
#ifdef _DEBUG
fwrite(buf, 1, len, stderr);
//fwrite(buf, 1, len, stderr);
#endif
switch (wsi->mode) {
@ -576,9 +576,9 @@ libwebsocket_read(struct libwebsocket_context *context,
if (wsi->parser_state != WSI_PARSING_COMPLETE)
break;
debug("seem to be serving, mode is %d\n", wsi->mode);
lwsl_parser("seem to be serving, mode is %d\n", wsi->mode);
debug("libwebsocket_parse sees parsing complete\n");
lwsl_parser("libwebsocket_parse sees parsing complete\n");
/* is this websocket protocol or normal http 1.0? */
@ -595,7 +595,7 @@ libwebsocket_read(struct libwebsocket_context *context,
}
if (!wsi->protocol)
fprintf(stderr, "NULL protocol at libwebsocket_read\n");
lwsl_err("NULL protocol at libwebsocket_read\n");
/*
* It's websocket
@ -621,10 +621,10 @@ libwebsocket_read(struct libwebsocket_context *context,
if (wsi->protocol->callback == NULL) {
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL)
fprintf(stderr, "[no protocol] "
lwsl_err("[no protocol] "
"not supported (use NULL .name)\n");
else
fprintf(stderr, "Requested protocol %s "
lwsl_err("Requested protocol %s "
"not supported\n",
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
goto bail;
@ -647,7 +647,7 @@ libwebsocket_read(struct libwebsocket_context *context,
if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
&wsi->utf8_token[0], NULL, 0)) {
fprintf(stderr, "User code denied connection\n");
lwsl_warn("User code denied connection\n");
goto bail;
}
@ -665,7 +665,7 @@ libwebsocket_read(struct libwebsocket_context *context,
break;
case 4: /* 04 */
wsi->xor_mask = xor_mask_04;
_debug("libwebsocket_parse calling handshake_04\n");
lwsl_parser("libwebsocket_parse calling handshake_04\n");
if (handshake_0405(context, wsi))
goto bail;
break;
@ -675,18 +675,18 @@ libwebsocket_read(struct libwebsocket_context *context,
case 8:
case 13:
wsi->xor_mask = xor_mask_05;
_debug("libwebsocket_parse calling handshake_04\n");
lwsl_parser("libwebsocket_parse calling handshake_04\n");
if (handshake_0405(context, wsi))
goto bail;
break;
default:
fprintf(stderr, "Unknown client spec version %d\n",
lwsl_warn("Unknown client spec version %d\n",
wsi->ietf_spec_revision);
goto bail;
}
debug("accepted v%02d connection\n",
lwsl_parser("accepted v%02d connection\n",
wsi->ietf_spec_revision);
break;

View file

@ -43,6 +43,18 @@ int openssl_websocket_private_data_index;
#endif
#endif
static int log_level = LLL_ERR | LLL_WARN;
static const char *log_level_names[] = {
"ERR",
"WARN",
"INFO",
"DEBUG",
"PARSER",
"HEADER",
"EXTENSION",
"CLIENT",
};
/*
* In-place str to lower case
*/
@ -77,7 +89,7 @@ insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi)
int h = LWS_FD_HASH(wsi->sock);
if (context->fd_hashtable[h].length == MAX_CLIENTS - 1) {
fprintf(stderr, "hash table overflow\n");
lwsl_err("hash table overflow\n");
return 1;
}
@ -104,7 +116,7 @@ delete_from_fd(struct libwebsocket_context *context, int fd)
return 0;
}
fprintf(stderr, "Failed to find fd %d requested for "
lwsl_err("Failed to find fd %d requested for "
"delete in hashtable\n", fd);
return 1;
}
@ -118,7 +130,7 @@ libwebsockets_decode_ssl_error(void)
while ((err = ERR_get_error()) != 0) {
ERR_error_string_n(err, buf, sizeof(buf));
fprintf(stderr, "*** %s\n", buf);
lwsl_err("*** %s\n", buf);
}
}
#endif
@ -198,7 +210,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
*/
if (m) {
debug("extension vetoed close\n");
lwsl_ext("extension vetoed close\n");
return;
}
}
@ -228,8 +240,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
wsi->active_extensions_user[n], &eff_buf, 0);
if (m < 0) {
fprintf(stderr, "Extension reports "
"fatal error\n");
lwsl_ext("Extension reports fatal error\n");
goto just_kill_connection;
}
if (m)
@ -263,7 +274,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
if (old_state == WSI_STATE_ESTABLISHED &&
reason != LWS_CLOSE_STATUS_NOSTATUS) {
debug("sending close indication...\n");
lwsl_debug("sending close indication...\n");
n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
0, LWS_WRITE_CLOSE);
@ -280,7 +291,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
libwebsocket_set_timeout(wsi,
PENDING_TIMEOUT_CLOSE_ACK, AWAITING_TIMEOUT);
debug("sent close indication, awaiting ack\n");
lwsl_debug("sent close indication, awaiting ack\n");
return;
}
@ -290,7 +301,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
just_kill_connection:
debug("libwebsocket_close_and_free_session: just_kill_connection\n");
lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
/*
* we won't be servicing or receiving anything further from this guy
@ -327,11 +338,11 @@ just_kill_connection:
((old_state == WSI_STATE_ESTABLISHED) ||
(old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
(old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
debug("calling back CLOSED\n");
lwsl_debug("calling back CLOSED\n");
wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
wsi->user_space, NULL, 0);
} else
debug("not calling back closed, old_state=%d\n", old_state);
lwsl_debug("not calling back closed, old_state=%d\n", old_state);
/* deallocate any active extension contexts */
@ -369,7 +380,7 @@ just_kill_connection:
if (wsi->c_address)
free(wsi->c_address);
/* fprintf(stderr, "closing fd=%d\n", wsi->sock); */
/* lwsl_info("closing fd=%d\n", wsi->sock); */
#ifdef LWS_OPENSSL_SUPPORT
if (wsi->ssl) {
@ -635,7 +646,7 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
wsi->active_extensions_user[n], &eff_buf, 0);
if (m < 0) {
fprintf(stderr, "ext reports fatal error\n");
lwsl_err("ext reports fatal error\n");
return -1;
}
if (m)
@ -669,7 +680,7 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
/* no we could add more */
continue;
debug("choked in POLLOUT service\n");
lwsl_info("choked in POLLOUT service\n");
/*
* Yes, he's choked. Leave the POLLOUT masked on so we will
@ -735,7 +746,7 @@ libwebsocket_service_timeout_check(struct libwebsocket_context *context,
*/
if (sec > wsi->pending_timeout_limit) {
debug("TIMEDOUT WAITING\n");
lwsl_info("TIMEDOUT WAITING\n");
libwebsocket_close_and_free_session(context,
wsi, LWS_CLOSE_STATUS_NOSTATUS);
}
@ -749,7 +760,7 @@ libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
if (new_wsi == NULL) {
fprintf(stderr, "Out of memory for new connection\n");
lwsl_err("Out of memory for new connection\n");
return NULL;
}
@ -809,7 +820,7 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
n = libwebsockets_get_random(context, hash, 16);
if (n != 16) {
fprintf(stderr, "Unable to read from random dev %s\n",
lwsl_err("Unable to read from random dev %s\n",
SYSTEM_RANDOM_FILEPATH);
free(wsi->c_path);
free(wsi->c_host);
@ -986,7 +997,7 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
}
if (n) { /* an extension vetos us */
debug("ext %s vetoed\n", (char *)ext->name);
lwsl_ext("ext %s vetoed\n", (char *)ext->name);
ext++;
continue;
}
@ -1097,28 +1108,28 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
!wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
(!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
wsi->c_protocol != NULL)) {
debug("libwebsocket_client_handshake "
lwsl_parser("libwebsocket_client_handshake "
"missing required header(s)\n");
pkt[len] = '\0';
debug("%s", pkt);
lwsl_parser("%s", pkt);
goto bail3;
}
strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
fprintf(stderr, "libwebsocket_client_handshake "
lwsl_warn("libwebsocket_client_handshake "
"server sent bad HTTP response '%s'\n",
wsi->utf8_token[WSI_TOKEN_HTTP].token);
goto bail3;
}
if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) {
fprintf(stderr, "libwebsocket_client_handshake "
lwsl_parser("libwebsocket_client_handshake "
"challenge reply too short %d\n",
wsi->utf8_token[
WSI_TOKEN_CHALLENGE].token_len);
pkt[len] = '\0';
debug("%s", pkt);
lwsl_parser("%s", pkt);
goto bail3;
}
@ -1131,17 +1142,17 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
* Now let's confirm it sent all the necessary headers
*/
#if 0
fprintf(stderr, "WSI_TOKEN_HTTP: %d\n",
lwsl_parser("WSI_TOKEN_HTTP: %d\n",
wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n",
lwsl_parser("WSI_TOKEN_UPGRADE: %d\n",
wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n",
lwsl_parser("WSI_TOKEN_CONNECTION: %d\n",
wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n",
lwsl_parser("WSI_TOKEN_ACCEPT: %d\n",
wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
fprintf(stderr, "WSI_TOKEN_NONCE: %d\n",
lwsl_parser("WSI_TOKEN_NONCE: %d\n",
wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n",
lwsl_parser("WSI_TOKEN_PROTOCOL: %d\n",
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
#endif
if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
@ -1152,10 +1163,10 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
wsi->ietf_spec_revision == 4) ||
(!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
wsi->c_protocol != NULL)) {
debug("libwebsocket_client_handshake "
lwsl_parser("libwebsocket_client_handshake "
"missing required header(s)\n");
pkt[len] = '\0';
debug("%s", pkt);
lwsl_parser("%s", pkt);
goto bail3;
}
@ -1166,7 +1177,7 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
fprintf(stderr, "libwebsocket_client_handshake "
lwsl_warn("libwebsocket_client_handshake "
"server sent bad HTTP response '%s'\n",
wsi->utf8_token[WSI_TOKEN_HTTP].token);
goto bail3;
@ -1175,7 +1186,7 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
"websocket")) {
fprintf(stderr, "libwebsocket_client_handshake server "
lwsl_warn("libwebsocket_client_handshake server "
"sent bad Upgrade header '%s'\n",
wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
goto bail3;
@ -1184,7 +1195,7 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
"upgrade")) {
fprintf(stderr, "libwebsocket_client_handshake server "
lwsl_warn("libwebsocket_client_handshake server "
"sent bad Connection hdr '%s'\n",
wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
goto bail3;
@ -1193,10 +1204,10 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
select_protocol:
pc = wsi->c_protocol;
if (pc == NULL)
fprintf(stderr, "lws_client_interpret_server_handshake: "
lwsl_parser("lws_client_interpret_server_handshake: "
"NULL c_protocol\n");
else
debug("lws_client_interpret_server_handshake: "
lwsl_parser("lws_client_interpret_server_handshake: "
"cPprotocol='%s'\n", pc);
/*
@ -1206,7 +1217,7 @@ select_protocol:
if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
fprintf(stderr, "lws_client_interpret_server_handshake "
lwsl_warn("lws_client_interpret_server_handshake "
"WSI_TOKEN_PROTOCOL is null\n");
/*
* no protocol name to work from,
@ -1239,7 +1250,7 @@ select_protocol:
free(wsi->c_protocol);
if (!okay) {
fprintf(stderr, "libwebsocket_client_handshake server "
lwsl_err("libwebsocket_client_handshake server "
"sent bad protocol '%s'\n",
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
goto bail2;
@ -1260,7 +1271,7 @@ select_protocol:
}
if (wsi->protocol == NULL) {
fprintf(stderr, "libwebsocket_client_handshake server "
lwsl_err("libwebsocket_client_handshake server "
"requested protocol '%s', which we "
"said we supported but we don't!\n",
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
@ -1273,7 +1284,7 @@ check_extensions:
/* instantiate the accepted extensions */
if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
debug("no client extenstions allowed by server\n");
lwsl_ext("no client extenstions allowed by server\n");
goto check_accept;
}
@ -1303,7 +1314,7 @@ check_extensions:
/* check we actually support it */
debug("checking client ext %s\n", ext_name);
lwsl_ext("checking client ext %s\n", ext_name);
n = 0;
ext = wsi->protocol->owning_server->extensions;
@ -1316,7 +1327,7 @@ check_extensions:
n = 1;
debug("instantiating client ext %s\n", ext_name);
lwsl_ext("instantiating client ext %s\n", ext_name);
/* instantiate the extension on this conn */
@ -1344,7 +1355,7 @@ check_extensions:
}
if (n == 0) {
fprintf(stderr, "Server said we should use"
lwsl_warn("Server said we should use"
"an unknown extension '%s'!\n", ext_name);
goto bail2;
}
@ -1359,10 +1370,10 @@ check_accept:
if (memcmp(wsi->initial_handshake_hash_base64,
wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
fprintf(stderr, "libwebsocket_client_handshake "
lwsl_warn("libwebsocket_client_handshake "
"failed 00 challenge compare\n");
pkt[len] = '\0';
fprintf(stderr, "%s", pkt);
lwsl_warn("%s", pkt);
goto bail2;
}
@ -1375,7 +1386,7 @@ check_accept:
if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
wsi->initial_handshake_hash_base64)) {
fprintf(stderr, "libwebsocket_client_handshake server "
lwsl_warn("libwebsocket_client_handshake server "
"sent bad ACCEPT '%s' vs computed '%s'\n",
wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
wsi->initial_handshake_hash_base64);
@ -1411,7 +1422,7 @@ accept_ok:
wsi->state = WSI_STATE_ESTABLISHED;
wsi->mode = LWS_CONNMODE_WS_CLIENT;
_debug("handshake OK for protocol %s\n", wsi->protocol->name);
lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
/* call him back to inform him he is up */
@ -1534,7 +1545,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
break;
if (context->fds_count >= MAX_CLIENTS) {
fprintf(stderr, "too busy to accept new client\n");
lwsl_warn("too busy to accept new client\n");
break;
}
@ -1544,7 +1555,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
&clilen);
if (accept_fd < 0) {
debug("ERROR on accept: %d\n", strerror(errno));
lwsl_warn("ERROR on accept: %d\n", strerror(errno));
return -1;
}
@ -1562,7 +1573,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
if ((context->protocols[0].callback)(context, wsi,
LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
(void *)(long)accept_fd, NULL, 0)) {
debug("Callback denied network connection\n");
lwsl_debug("Callback denied network connection\n");
#ifdef WIN32
closesocket(accept_fd);
#else
@ -1593,7 +1604,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
new_wsi->ssl = SSL_new(context->ssl_ctx);
if (new_wsi->ssl == NULL) {
fprintf(stderr, "SSL_new failed: %s\n",
lwsl_err("SSL_new failed: %s\n",
ERR_error_string(SSL_get_error(
new_wsi->ssl, 0), NULL));
libwebsockets_decode_ssl_error();
@ -1615,7 +1626,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
* ssl params which fail then retry
* and succeed
*/
_debug("SSL_accept failed skt %u: %s\n",
lwsl_debug("SSL_accept failed skt %u: %s\n",
pollfd->fd,
ERR_error_string(SSL_get_error(
new_wsi->ssl, n), NULL));
@ -1630,14 +1641,14 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
break;
}
_debug("accepted new SSL conn "
lwsl_debug("accepted new SSL conn "
"port %u on fd=%d SSL ver %s\n",
ntohs(cli_addr.sin_port), accept_fd,
SSL_get_version(new_wsi->ssl));
} else
#endif
_debug("accepted new conn port %u on fd=%d\n",
lwsl_debug("accepted new conn port %u on fd=%d\n",
ntohs(cli_addr.sin_port), accept_fd);
insert_wsi(context, new_wsi);
@ -1672,12 +1683,12 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
&clilen);
if (accept_fd < 0) {
debug("ERROR on accept\n");
lwsl_warn("ERROR on accept %d\n", accept_fd);
return -1;
}
if (context->fds_count >= MAX_CLIENTS) {
fprintf(stderr, "too busy to accept new broadcast "
lwsl_err("too busy to accept new broadcast "
"proxy client\n");
#ifdef WIN32
closesocket(accept_fd);
@ -1719,7 +1730,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
if (pollfd->revents & (POLLERR | POLLHUP)) {
_debug("Session Socket %p (fd=%d) dead\n",
lwsl_debug("Session Socket %p (fd=%d) dead\n",
(void *)wsi, pollfd->fd);
libwebsocket_close_and_free_session(context, wsi,
@ -1750,7 +1761,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING,
MAX_BROADCAST_PAYLOAD);
if (len < 0) {
fprintf(stderr, "Error reading broadcast payload\n");
lwsl_err("Error reading broadcast payload\n");
break;
}
@ -1800,7 +1811,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
if (pollfd->revents & (POLLERR | POLLHUP)) {
fprintf(stderr, "Proxy connection %p (fd=%d) dead\n",
lwsl_warn("Proxy connection %p (fd=%d) dead\n",
(void *)wsi, pollfd->fd);
libwebsocket_close_and_free_session(context, wsi,
@ -1812,7 +1823,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
if (n < 0) {
libwebsocket_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
fprintf(stderr, "ERROR reading from proxy socket\n");
lwsl_err("ERROR reading from proxy socket\n");
return 1;
}
@ -1820,7 +1831,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
if (strcmp(pkt, "HTTP/1.0 200 ") != 0) {
libwebsocket_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
fprintf(stderr, "ERROR from proxy: %s\n", pkt);
lwsl_err("ERROR from proxy: %s\n", pkt);
return 1;
}
@ -1853,7 +1864,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
* run into the connection timeout or win
*/
fprintf(stderr, "SSL connect error %s\n",
lwsl_err("SSL connect error %s\n",
ERR_error_string(ERR_get_error(),
ssl_err_buf));
return 0;
@ -1864,7 +1875,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
wsi->use_ssl != 2)) {
fprintf(stderr, "server's cert didn't "
lwsl_err("server's cert didn't "
"look good %d\n", n);
libwebsocket_close_and_free_session(context,
wsi, LWS_CLOSE_STATUS_NOSTATUS);
@ -1888,7 +1899,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
n = send(wsi->sock, pkt, p - pkt, 0);
if (n < 0) {
fprintf(stderr, "ERROR writing to client socket\n");
lwsl_debug("ERROR writing to client socket\n");
libwebsocket_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
return 1;
@ -1907,7 +1918,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
if (pollfd->revents & (POLLERR | POLLHUP)) {
fprintf(stderr, "Server connection %p (fd=%d) dead\n",
lwsl_debug("Server connection %p (fd=%d) dead\n",
(void *)wsi, pollfd->fd);
goto bail3;
@ -1971,13 +1982,11 @@ bail3:
return 1;
case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
fprintf(stderr,
"LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
break;
case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
fprintf(stderr,
"LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
break;
@ -1988,7 +1997,7 @@ bail3:
if (pollfd->revents & (POLLERR | POLLHUP)) {
fprintf(stderr, "Session Socket %p (fd=%d) dead\n",
lwsl_debug("Session Socket %p (fd=%d) dead\n",
(void *)wsi, pollfd->fd);
libwebsocket_close_and_free_session(context, wsi,
@ -2023,7 +2032,7 @@ read_pending:
recv(pollfd->fd, buf, sizeof buf, 0);
if (eff_buf.token_len < 0) {
fprintf(stderr, "Socket read returned %d\n",
lwsl_debug("Socket read returned %d\n",
eff_buf.token_len);
if (errno != EINTR && errno != EAGAIN)
libwebsocket_close_and_free_session(context,
@ -2062,7 +2071,7 @@ read_pending:
wsi->active_extensions_user[n],
&eff_buf, 0);
if (m < 0) {
fprintf(stderr,
lwsl_ext(
"Extension reports fatal error\n");
libwebsocket_close_and_free_session(
context, wsi,
@ -2215,7 +2224,7 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
if (n < 0) {
/*
fprintf(stderr, "Listen Socket dead\n");
lwsl_err("Listen Socket dead\n");
*/
return -1;
}
@ -2312,7 +2321,7 @@ libwebsocket_callback_on_writable(struct libwebsocket_context *context,
}
if (n == context->fds_count)
fprintf(stderr, "libwebsocket_callback_on_writable: "
lwsl_err("libwebsocket_callback_on_writable: "
"failed to find socket %d\n", wsi->sock);
/* external POLL support via protocol 0 */
@ -2431,8 +2440,7 @@ libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
(void *)(long)wsi->sock, NULL, POLLIN);
#if 0
fprintf(stderr, "libwebsocket_rx_flow_control "
"unable to find socket\n");
lwsl_err("libwebsocket_rx_flow_control unable to find socket\n");
#endif
return 1;
}
@ -2573,6 +2581,8 @@ libwebsocket_create_context(int port, const char *interf,
char ssl_err_buf[512];
#endif
lwsl_info("Initial logging level %d\n", log_level);
#ifdef _WIN32
{
WORD wVersionRequested;
@ -2587,8 +2597,7 @@ libwebsocket_create_context(int port, const char *interf,
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
fprintf(stderr, "WSAStartup failed with error: %d\n",
err);
lwsl_err("WSAStartup failed with error: %d\n", err);
return NULL;
}
@ -2605,7 +2614,7 @@ libwebsocket_create_context(int port, const char *interf,
context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
if (!context) {
fprintf(stderr, "No memory for websocket context\n");
lwsl_err("No memory for websocket context\n");
return NULL;
}
context->protocols = protocols;
@ -2623,7 +2632,7 @@ libwebsocket_create_context(int port, const char *interf,
#else
context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
if (context->fd_random < 0) {
fprintf(stderr, "Unable to open random device %s %d\n",
lwsl_err("Unable to open random device %s %d\n",
SYSTEM_RANDOM_FILEPATH, context->fd_random);
return NULL;
}
@ -2654,7 +2663,7 @@ libwebsocket_create_context(int port, const char *interf,
if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
strcpy(sa.sa_data, hostname);
// fprintf(stderr, "my host name is %s\n", sa.sa_data);
// lwsl_debug("my host name is %s\n", sa.sa_data);
n = getnameinfo(&sa, sizeof(sa), hostname,
(sizeof hostname) - 1, NULL, 0, 0);
}
@ -2668,7 +2677,7 @@ libwebsocket_create_context(int port, const char *interf,
strncpy(context->canonical_hostname, hostname,
sizeof context->canonical_hostname - 1);
// fprintf(stderr, "context->canonical_hostname = %s\n",
// lwsl_debug("context->canonical_hostname = %s\n",
// context->canonical_hostname);
}
@ -2683,13 +2692,13 @@ libwebsocket_create_context(int port, const char *interf,
p = strchr(context->http_proxy_address, ':');
if (p == NULL) {
fprintf(stderr, "http_proxy needs to be ads:port\n");
lwsl_err("http_proxy needs to be ads:port\n");
return NULL;
}
*p = '\0';
context->http_proxy_port = atoi(p + 1);
fprintf(stderr, "Using proxy %s:%u\n",
lwsl_debug("Using proxy %s:%u\n",
context->http_proxy_address,
context->http_proxy_port);
}
@ -2700,19 +2709,17 @@ libwebsocket_create_context(int port, const char *interf,
context->use_ssl = ssl_cert_filepath != NULL &&
ssl_private_key_filepath != NULL;
if (context->use_ssl)
fprintf(stderr, " Compiled with SSL support, "
"using it\n");
lwsl_info(" Compiled with SSL support, using it\n");
else
fprintf(stderr, " Compiled with SSL support, "
"not using it\n");
lwsl_info(" Compiled with SSL support, not using it\n");
#else
if (ssl_cert_filepath != NULL &&
ssl_private_key_filepath != NULL) {
fprintf(stderr, " Not compiled for OpenSSl support!\n");
lwsl_info(" Not compiled for OpenSSl support!\n");
return NULL;
}
fprintf(stderr, " Compiled without SSL support, "
lwsl_info(" Compiled without SSL support, "
"serving unencrypted\n");
#endif
}
@ -2743,13 +2750,13 @@ libwebsocket_create_context(int port, const char *interf,
method = (SSL_METHOD *)SSLv23_server_method();
if (!method) {
fprintf(stderr, "problem creating ssl method: %s\n",
lwsl_err("problem creating ssl method: %s\n",
ERR_error_string(ERR_get_error(), ssl_err_buf));
return NULL;
}
context->ssl_ctx = SSL_CTX_new(method); /* create context */
if (!context->ssl_ctx) {
fprintf(stderr, "problem creating ssl context: %s\n",
lwsl_err("problem creating ssl context: %s\n",
ERR_error_string(ERR_get_error(), ssl_err_buf));
return NULL;
}
@ -2765,14 +2772,14 @@ libwebsocket_create_context(int port, const char *interf,
if (port == CONTEXT_PORT_NO_LISTEN) {
method = (SSL_METHOD *)SSLv23_client_method();
if (!method) {
fprintf(stderr, "problem creating ssl method: %s\n",
lwsl_err("problem creating ssl method: %s\n",
ERR_error_string(ERR_get_error(), ssl_err_buf));
return NULL;
}
/* create context */
context->ssl_client_ctx = SSL_CTX_new(method);
if (!context->ssl_client_ctx) {
fprintf(stderr, "problem creating ssl context: %s\n",
lwsl_err("problem creating ssl context: %s\n",
ERR_error_string(ERR_get_error(), ssl_err_buf));
return NULL;
}
@ -2788,7 +2795,7 @@ libwebsocket_create_context(int port, const char *interf,
if (!SSL_CTX_load_verify_locations(
context->ssl_client_ctx, NULL,
LWS_OPENSSL_CLIENT_CERTS))
fprintf(stderr,
lwsl_err(
"Unable to load SSL Client certs from %s "
"(set by --with-client-cert-dir= in configure) -- "
" client ssl isn't going to work",
@ -2797,7 +2804,7 @@ libwebsocket_create_context(int port, const char *interf,
if (!SSL_CTX_load_verify_locations(
context->ssl_client_ctx, ssl_ca_filepath,
NULL))
fprintf(stderr,
lwsl_err(
"Unable to load SSL Client certs "
"file from %s -- client ssl isn't "
"going to work", ssl_ca_filepath);
@ -2840,7 +2847,7 @@ libwebsocket_create_context(int port, const char *interf,
n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
ssl_cert_filepath);
if (n != 1) {
fprintf(stderr, "problem getting cert '%s': %s\n",
lwsl_err("problem getting cert '%s': %s\n",
ssl_cert_filepath,
ERR_error_string(ERR_get_error(), ssl_err_buf));
return NULL;
@ -2848,14 +2855,14 @@ libwebsocket_create_context(int port, const char *interf,
/* set the private key from KeyFile */
if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
fprintf(stderr, "ssl problem getting key '%s': %s\n",
lwsl_err("ssl problem getting key '%s': %s\n",
ssl_private_key_filepath,
ERR_error_string(ERR_get_error(), ssl_err_buf));
return NULL;
}
/* verify private key */
if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
fprintf(stderr, "Private SSL key doesn't match cert\n");
lwsl_err("Private SSL key doesn't match cert\n");
return NULL;
}
@ -2879,7 +2886,7 @@ libwebsocket_create_context(int port, const char *interf,
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
fprintf(stderr, "ERROR opening socket");
lwsl_err("ERROR opening socket");
return NULL;
}
@ -2904,7 +2911,7 @@ libwebsocket_create_context(int port, const char *interf,
n = bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr));
if (n < 0) {
fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
lwsl_err("ERROR on binding to port %d (%d %d)\n",
port, n, errno);
return NULL;
}
@ -2917,7 +2924,7 @@ libwebsocket_create_context(int port, const char *interf,
insert_wsi(context, wsi);
listen(sockfd, SOMAXCONN);
fprintf(stderr, " Listening on port %d\n", port);
lwsl_info(" Listening on port %d\n", port);
/* list in the internal poll array */
@ -2940,10 +2947,10 @@ libwebsocket_create_context(int port, const char *interf,
#else
if (gid != -1)
if (setgid(gid))
fprintf(stderr, "setgid: %s\n", strerror(errno));
lwsl_warn("setgid: %s\n", strerror(errno));
if (uid != -1)
if (setuid(uid))
fprintf(stderr, "setuid: %s\n", strerror(errno));
lwsl_warn("setuid: %s\n", strerror(errno));
#endif
/* set up our internal broadcast trigger sockets per-protocol */
@ -2952,7 +2959,8 @@ libwebsocket_create_context(int port, const char *interf,
protocols[context->count_protocols].callback;
context->count_protocols++) {
_debug(" Protocol: %s\n", protocols[context->count_protocols].name);
lwsl_parser(" Protocol: %s\n",
protocols[context->count_protocols].name);
protocols[context->count_protocols].owning_server = context;
protocols[context->count_protocols].protocol_index =
@ -2960,7 +2968,7 @@ libwebsocket_create_context(int port, const char *interf,
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "ERROR opening socket");
lwsl_err("ERROR opening socket");
return NULL;
}
@ -2975,7 +2983,7 @@ libwebsocket_create_context(int port, const char *interf,
n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (n < 0) {
fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
lwsl_err("ERROR on binding to port %d (%d %d)\n",
port, n, errno);
return NULL;
}
@ -2983,14 +2991,14 @@ libwebsocket_create_context(int port, const char *interf,
slen = sizeof cli_addr;
n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
if (n < 0) {
fprintf(stderr, "getsockname failed\n");
lwsl_err("getsockname failed\n");
return NULL;
}
protocols[context->count_protocols].broadcast_socket_port =
ntohs(cli_addr.sin_port);
listen(fd, 5);
_debug(" Protocol %s broadcast socket %d\n",
lwsl_debug(" Protocol %s broadcast socket %d\n",
protocols[context->count_protocols].name,
ntohs(cli_addr.sin_port));
@ -3030,7 +3038,7 @@ libwebsocket_create_context(int port, const char *interf,
if (extensions) {
while (extensions->callback) {
debug(" Extension: %s\n", extensions->name);
lwsl_ext(" Extension: %s\n", extensions->name);
extensions->callback(context, extensions, NULL,
(enum libwebsocket_extension_callback_reasons)m,
NULL, NULL, 0);
@ -3078,7 +3086,7 @@ libwebsockets_fork_service_loop(struct libwebsocket_context *context)
for (p = 0; p < context->count_protocols; p++) {
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "Unable to create socket\n");
lwsl_err("Unable to create socket\n");
return -1;
}
cli_addr.sin_family = AF_INET;
@ -3088,7 +3096,7 @@ libwebsockets_fork_service_loop(struct libwebsocket_context *context)
n = connect(fd, (struct sockaddr *)&cli_addr,
sizeof cli_addr);
if (n < 0) {
fprintf(stderr, "Unable to connect to "
lwsl_err("Unable to connect to "
"broadcast socket %d, %s\n",
n, strerror(errno));
return -1;
@ -3256,8 +3264,7 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi)
wsi->user_space = malloc(
wsi->protocol->per_session_data_size);
if (wsi->user_space == NULL) {
fprintf(stderr, "Out of memory for "
"conn user space\n");
lwsl_err("Out of memory for conn user space\n");
return NULL;
}
memset(wsi->user_space, 0,
@ -3265,3 +3272,36 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi)
}
return wsi->user_space;
}
void _lws_log(int filter, const char *format, ...)
{
va_list ap;
int n;
if (!(log_level & filter))
return;
for (n = 0; n < LLL_COUNT; n++)
if (filter == (1 << n)) {
fprintf(stderr, "%s: ", log_level_names[n]);
break;
}
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/**
* lws_set_log_level() - Set the logging bitfield
* @level: OR together the LLL_ debug contexts you want output from
*
*
* defaults to err and warn contexts enabled
*/
void lws_set_log_level(int level)
{
log_level = level;
}

View file

@ -62,6 +62,19 @@ typedef int ssize_t;
#define CONTEXT_PORT_NO_LISTEN 0
#define MAX_MUX_RECURSION 2
enum lws_log_levels {
LLL_ERR = 1 << 0,
LLL_WARN = 1 << 1,
LLL_INFO = 1 << 2,
LLL_DEBUG = 1 << 3,
LLL_PARSER = 1 << 4,
LLL_HEADER = 1 << 5,
LLL_EXT = 1 << 6,
LLL_CLIENT = 1 << 7,
LLL_COUNT = 8 /* set to count of valid flags */
};
enum libwebsocket_context_options {
LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK = 1,
LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
@ -641,7 +654,8 @@ struct libwebsocket_extension {
void * per_context_private_data;
};
LWS_EXTERN
void lws_set_log_level(int level);
LWS_EXTERN struct libwebsocket_context *
libwebsocket_create_context(int port, const char * interf,

View file

@ -77,7 +77,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
case WSI_TOKEN_HTTP:
case WSI_TOKEN_MUXURL:
_debug("WSI_TOKEN_(%d) '%c'\n", wsi->parser_state, c);
lwsl_parser("WSI_TOKEN_(%d) '%c'\n", wsi->parser_state, c);
/* collect into malloc'd buffers */
/* optional space swallow */
@ -88,7 +88,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
if (wsi->parser_state == WSI_TOKEN_GET_URI && c == ' ') {
wsi->utf8_token[wsi->parser_state].token[
wsi->utf8_token[wsi->parser_state].token_len] = '\0';
// debug("uri '%s'\n", wsi->utf8_token[wsi->parser_state].token);
// lwsl_parser("uri '%s'\n", wsi->utf8_token[wsi->parser_state].token);
wsi->parser_state = WSI_TOKEN_SKIPPING;
break;
}
@ -115,7 +115,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
wsi->utf8_token[wsi->parser_state].token[
wsi->utf8_token[wsi->parser_state].token_len] = '\0';
wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
_debug("*\n");
lwsl_parser("*\n");
break;
}
@ -156,7 +156,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
/* For any supported protocol we have enough payload */
_debug("Setting WSI_PARSING_COMPLETE\n");
lwsl_parser("Setting WSI_PARSING_COMPLETE\n");
wsi->parser_state = WSI_PARSING_COMPLETE;
break;
@ -171,7 +171,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
/* collecting and checking a name part */
case WSI_TOKEN_NAME_PART:
_debug("WSI_TOKEN_NAME_PART '%c'\n", c);
lwsl_parser("WSI_TOKEN_NAME_PART '%c'\n", c);
if (wsi->name_buffer_pos == sizeof(wsi->name_buffer) - 1) {
/* name bigger than we can handle, skip until next */
@ -186,7 +186,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
continue;
if (strcasecmp(lws_tokens[n].token, wsi->name_buffer))
continue;
_debug("known hdr '%s'\n", wsi->name_buffer);
lwsl_parser("known hdr '%s'\n", wsi->name_buffer);
/*
* WSORIGIN is protocol equiv to ORIGIN,
@ -213,7 +213,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
if (wsi->parser_state == WSI_TOKEN_NAME_PART) {
if (c == ':') {
debug("skipping unknown header '%s'\n",
lwsl_parser("skipping unknown header '%s'\n",
wsi->name_buffer);
wsi->parser_state = WSI_TOKEN_SKIPPING;
break;
@ -221,7 +221,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
if (c == ' ' &&
!wsi->utf8_token[WSI_TOKEN_GET_URI].token_len) {
debug("unknown method '%s'\n",
lwsl_parser("unknown method '%s'\n",
wsi->name_buffer);
wsi->parser_state = WSI_TOKEN_GET_URI;
wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
@ -238,7 +238,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len) {
/* they're HTTP headers, not websocket upgrade! */
_debug("Setting WSI_PARSING_COMPLETE "
lwsl_parser("Setting WSI_PARSING_COMPLETE "
"from http headers\n");
wsi->parser_state = WSI_PARSING_COMPLETE;
}
@ -247,7 +247,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) >= 4) {
_debug("04 header completed\n");
lwsl_parser("04 header completed\n");
wsi->parser_state = WSI_PARSING_COMPLETE;
wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len = 0;
free(wsi->utf8_token[WSI_TOKEN_CHALLENGE].token);
@ -257,7 +257,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
/* client parser? */
if (wsi->ietf_spec_revision >= 4) {
_debug("04 header completed\n");
lwsl_parser("04 header completed\n");
wsi->parser_state = WSI_PARSING_COMPLETE;
}
@ -265,12 +265,12 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
/* skipping arg part of a name we didn't recognize */
case WSI_TOKEN_SKIPPING:
_debug("WSI_TOKEN_SKIPPING '%c'\n", c);
lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
if (c == '\x0d')
wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
break;
case WSI_TOKEN_SKIPPING_SAW_CR:
_debug("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
if (c == '\x0a')
wsi->parser_state = WSI_TOKEN_NAME_PART;
else
@ -279,7 +279,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
break;
/* we're done, ignore anything else */
case WSI_PARSING_COMPLETE:
_debug("WSI_PARSING_COMPLETE '%c'\n", c);
lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
break;
default: /* keep gcc happy */
@ -323,7 +323,7 @@ libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c)
int m;
#if 0
fprintf(stderr, "RX: %02X ", c);
lwsl_debug("RX: %02X ", c);
#endif
switch (wsi->lws_rx_parse_state) {
@ -359,7 +359,7 @@ libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c)
goto handle_first;
default:
fprintf(stderr, "libwebsocket_rx_sm doesn't know "
lwsl_warn("libwebsocket_rx_sm doesn't know "
"about spec version %d\n", wsi->ietf_spec_revision);
break;
}
@ -498,7 +498,7 @@ handle_first:
wsi->opcode = LWS_WS_OPCODE_07__BINARY_FRAME;
break;
default:
fprintf(stderr, "reserved opcodes not "
lwsl_warn("reserved opcodes not "
"usable pre v7 protocol\n");
return -1;
}
@ -516,8 +516,7 @@ handle_first:
c = wsi->xor_mask(wsi, c);
if ((c & 0x80) && wsi->ietf_spec_revision < 7) {
fprintf(stderr, "Frame has extensions "
"set illegally 2\n");
lwsl_warn("Frame has extensions set illegally 2\n");
/* kill the connection */
return -1;
}
@ -576,7 +575,7 @@ handle_first:
if (wsi->ietf_spec_revision < 7)
c = wsi->xor_mask(wsi, c);
if (c & 0x80) {
fprintf(stderr, "b63 of length must be zero\n");
lwsl_warn("b63 of length must be zero\n");
/* kill the connection */
return -1;
}
@ -671,16 +670,16 @@ issue:
if (c)
break;
_debug("Seen that client is requesting "
lwsl_parser("Seen that client is requesting "
"a v76 close, sending ack\n");
buf[0] = 0xff;
buf[1] = 0;
n = libwebsocket_write(wsi, buf, 2, LWS_WRITE_HTTP);
if (n < 0) {
fprintf(stderr, "ERROR writing to socket");
lwsl_warn("ERROR writing to socket");
return -1;
}
_debug(" v76 close ack sent, server closing skt\n");
lwsl_parser(" v76 close ack sent, server closing skt\n");
/* returning < 0 will get it closed in parent */
return -1;
@ -741,7 +740,7 @@ spill:
* layer? If so service it and hide it from the user callback
*/
_debug("spill on %s\n", wsi->protocol->name);
lwsl_parser("spill on %s\n", wsi->protocol->name);
switch (wsi->opcode) {
case LWS_WS_OPCODE_07__CLOSE:
@ -751,10 +750,10 @@ spill:
* fine he has told us he is closing too, let's
* finish our close
*/
_debug("seen client close ack\n");
lwsl_parser("seen client close ack\n");
return -1;
}
debug("server sees client close packet\n");
lwsl_parser("server sees client close packet\n");
/* parrot the close packet payload back */
n = libwebsocket_write(wsi, (unsigned char *)
&wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
@ -786,7 +785,7 @@ spill:
default:
_debug("passing opcode %x up to exts\n", wsi->opcode);
lwsl_parser("passing opcode %x up to exts\n", wsi->opcode);
/*
* It's something special we can't understand here.
@ -811,7 +810,7 @@ spill:
}
if (!handled)
fprintf(stderr, "Unhandled extended opcode "
lwsl_ext("Unhandled extended opcode "
"0x%x - ignoring frame\n", wsi->opcode);
wsi->rx_user_buffer_head = 0;
@ -836,7 +835,7 @@ spill:
wsi->active_extensions_user[n],
&eff_buf, 0);
if (m < 0) {
fprintf(stderr,
lwsl_ext(
"Extension '%s' failed to handle payload!",
wsi->active_extensions[n]->name);
return -1;
@ -853,7 +852,7 @@ spill:
eff_buf.token,
eff_buf.token_len);
else
fprintf(stderr, "No callback on payload spill!");
lwsl_err("No callback on payload spill!");
}
wsi->rx_user_buffer_head = 0;
@ -864,7 +863,7 @@ spill:
illegal_ctl_length:
fprintf(stderr, "Control frame asking for "
lwsl_warn("Control frame asking for "
"extended length is illegal\n");
/* kill the connection */
return -1;
@ -880,7 +879,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c)
struct lws_tokens eff_buf;
int m;
_debug(" CRX: %02X %d\n", c, wsi->lws_rx_parse_state);
lwsl_parser(" CRX: %02X %d\n", c, wsi->lws_rx_parse_state);
switch (wsi->lws_rx_parse_state) {
case LWS_RXPS_NEW:
@ -977,7 +976,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c)
LWS_WS_OPCODE_07__BINARY_FRAME;
break;
default:
fprintf(stderr, "reserved opcodes not "
lwsl_warn("reserved opcodes not "
"usable pre v7 protocol\n");
return -1;
}
@ -990,7 +989,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c)
break;
default:
fprintf(stderr, "client_rx_sm doesn't know how "
lwsl_err("client_rx_sm doesn't know how "
"to handle spec version %02d\n",
wsi->ietf_spec_revision);
break;
@ -1001,8 +1000,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c)
case LWS_RXPS_04_FRAME_HDR_LEN:
if ((c & 0x80) && wsi->ietf_spec_revision < 7) {
fprintf(stderr,
"Frame has extensions set illegally 4\n");
lwsl_warn("Frame has extensions set illegally 4\n");
/* kill the connection */
return -1;
}
@ -1063,7 +1061,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c)
case LWS_RXPS_04_FRAME_HDR_LEN64_8:
if (c & 0x80) {
fprintf(stderr, "b63 of length must be zero\n");
lwsl_warn("b63 of length must be zero\n");
/* kill the connection */
return -1;
}
@ -1186,16 +1184,16 @@ issue:
if (c)
break;
_debug("Seen that client is requesting "
lwsl_parser("Seen that client is requesting "
"a v76 close, sending ack\n");
buf[0] = 0xff;
buf[1] = 0;
n = libwebsocket_write(wsi, buf, 2, LWS_WRITE_HTTP);
if (n < 0) {
fprintf(stderr, "ERROR writing to socket");
lwsl_warn("ERROR writing to socket");
return -1;
}
_debug(" v76 close ack sent, server closing skt\n");
lwsl_parser(" v76 close ack sent, server closing skt\n");
/* returning < 0 will get it closed in parent */
return -1;
@ -1234,15 +1232,15 @@ spill:
* fine he has told us he is closing too, let's
* finish our close
*/
debug("seen server's close ack\n");
lwsl_parser("seen server's close ack\n");
return -1;
}
_debug("client sees server close packet len = %d\n", wsi->rx_user_buffer_head);
lwsl_parser("client sees server close packet len = %d\n", wsi->rx_user_buffer_head);
/* parrot the close packet payload back */
n = libwebsocket_write(wsi, (unsigned char *)
&wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
wsi->rx_user_buffer_head, LWS_WRITE_CLOSE);
_debug("client writing close ack returned %d\n", n);
lwsl_parser("client writing close ack returned %d\n", n);
wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
/* close the connection */
return -1;
@ -1270,7 +1268,7 @@ spill:
default:
debug("Reserved opcode 0x%2X\n", wsi->opcode);
lwsl_parser("Reserved opcode 0x%2X\n", wsi->opcode);
/*
* It's something special we can't understand here.
* Pass the payload up to the extension's parsing
@ -1293,7 +1291,7 @@ spill:
}
if (!handled) {
fprintf(stderr, "Unhandled extended opcode "
lwsl_ext("Unhandled extended opcode "
"0x%x - ignoring frame\n", wsi->opcode);
wsi->rx_user_buffer_head = 0;
@ -1323,7 +1321,7 @@ spill:
wsi->active_extensions_user[n],
&eff_buf, 0);
if (m < 0) {
fprintf(stderr,
lwsl_ext(
"Extension '%s' failed to handle payload!",
wsi->active_extensions[n]->name);
return -1;
@ -1346,7 +1344,7 @@ already_done:
wsi->rx_user_buffer_head = 0;
break;
default:
fprintf(stderr, "client rx illegal state\n");
lwsl_err("client rx illegal state\n");
return 1;
}
@ -1354,7 +1352,7 @@ already_done:
illegal_ctl_length:
fprintf(stderr, "Control frame asking for "
lwsl_warn("Control frame asking for "
"extended length is illegal\n");
/* kill the connection */
return -1;
@ -1369,10 +1367,10 @@ int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
size_t n;
#ifdef DEBUG
fprintf(stderr, "received %d byte packet\n", (int)len);
lwsl_parser("received %d byte packet\n", (int)len);
for (n = 0; n < len; n++)
fprintf(stderr, "%02X ", buf[n]);
fprintf(stderr, "\n");
lwsl_parser("%02X ", buf[n]);
lwsl_parser("\n");
#endif
/* let the rx protocol state machine have as much as it needs */
@ -1397,7 +1395,7 @@ libwebsocket_0405_frame_mask_generate(struct libwebsocket *wsi)
n = libwebsockets_get_random(wsi->protocol->owning_server,
wsi->frame_masking_nonce_04, 4);
if (n != 4) {
fprintf(stderr, "Unable to read from random device %s %d\n",
lwsl_parser("Unable to read from random device %s %d\n",
SYSTEM_RANDOM_FILEPATH, n);
return 1;
}
@ -1432,32 +1430,32 @@ void lws_stderr_hexdump(unsigned char *buf, size_t len)
int m;
int start;
fprintf(stderr, "\n");
lwsl_parser("\n");
for (n = 0; n < len;) {
start = n;
fprintf(stderr, "%04X: ", start);
lwsl_debug("%04X: ", start);
for (m = 0; m < 16 && n < len; m++)
fprintf(stderr, "%02X ", buf[n++]);
lwsl_debug("%02X ", buf[n++]);
while (m++ < 16)
fprintf(stderr, " ");
lwsl_debug(" ");
fprintf(stderr, " ");
lwsl_debug(" ");
for (m = 0; m < 16 && (start + m) < len; m++) {
if (buf[start + m] >= ' ' && buf[start + m] <= 127)
fprintf(stderr, "%c", buf[start + m]);
lwsl_debug("%c", buf[start + m]);
else
fprintf(stderr, ".");
lwsl_debug(".");
}
while (m++ < 16)
fprintf(stderr, " ");
lwsl_debug(" ");
fprintf(stderr, "\n");
lwsl_debug("\n");
}
fprintf(stderr, "\n");
lwsl_debug("\n");
}
int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
@ -1480,24 +1478,24 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
wsi->active_extensions_user[n], &buf, len);
if (m < 0) {
fprintf(stderr, "Extension reports fatal error\n");
lwsl_ext("Extension reports fatal error\n");
return -1;
}
if (m) /* handled */ {
/* fprintf(stderr, "ext sent it\n"); */
/* lwsl_ext("ext sent it\n"); */
return 0;
}
}
if (!wsi->sock)
fprintf(stderr, "** error 0 sock but expected to send\n");
lwsl_warn("** error 0 sock but expected to send\n");
/*
* nope, send it on the socket directly
*/
#if 0
fprintf(stderr, " TX: ");
lwsl_debug(" TX: ");
lws_stderr_hexdump(buf, len);
#endif
@ -1505,16 +1503,14 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
if (wsi->ssl) {
n = SSL_write(wsi->ssl, buf, len);
if (n < 0) {
fprintf(stderr,
"ERROR writing to socket\n");
lwsl_debug("ERROR writing to socket\n");
return -1;
}
} else {
#endif
n = send(wsi->sock, buf, len, MSG_NOSIGNAL);
if (n < 0) {
fprintf(stderr,
"ERROR writing to socket\n");
lwsl_debug("ERROR writing to socket\n");
return -1;
}
#ifdef LWS_OPENSSL_SUPPORT
@ -1556,7 +1552,7 @@ lws_issue_raw_ext_access(struct libwebsocket *wsi,
LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
wsi->active_extensions_user[n], &eff_buf, 0);
if (m < 0) {
fprintf(stderr, "Extension: fatal error\n");
lwsl_ext("Extension: fatal error\n");
return -1;
}
if (m)
@ -1574,7 +1570,7 @@ lws_issue_raw_ext_access(struct libwebsocket *wsi,
eff_buf.token_len))
return -1;
_debug("written %d bytes to client\n", eff_buf.token_len);
lwsl_parser("written %d bytes to client\n", eff_buf.token_len);
/* no extension has more to spill */
@ -1594,7 +1590,7 @@ lws_issue_raw_ext_access(struct libwebsocket *wsi,
/* no we could add more */
continue;
debug("choked\n");
lwsl_debug("choked\n");
/*
* Yes, he's choked. Don't spill the rest now get a callback
@ -1649,7 +1645,7 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
int m;
if (len == 0 && protocol != LWS_WRITE_CLOSE) {
fprintf(stderr, "zero length libwebsocket_write attempt\n");
lwsl_warn("zero length libwebsocket_write attempt\n");
return 0;
}
@ -1768,13 +1764,13 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
switch (wsi->mode) {
case LWS_CONNMODE_WS_SERVING:
/*
fprintf(stderr, "LWS_WRITE_CLOSE S\n");
lwsl_debug("LWS_WRITE_CLOSE S\n");
*/
buf[0] = 'S';
break;
case LWS_CONNMODE_WS_CLIENT:
/*
fprintf(stderr, "LWS_WRITE_CLOSE C\n");
lwsl_debug("LWS_WRITE_CLOSE C\n");
*/
buf[0] = 'C';
break;
@ -1813,7 +1809,7 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
n = LWS_WS_OPCODE_07__PONG;
break;
default:
fprintf(stderr, "libwebsocket_write: unknown write "
lwsl_warn("libwebsocket_write: unknown write "
"opcode / protocol\n");
return -1;
}
@ -1873,7 +1869,7 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
wsi->xor_mask != xor_no_mask) {
if (libwebsocket_0405_frame_mask_generate(wsi)) {
fprintf(stderr, "libwebsocket_write: "
lwsl_err("libwebsocket_write: "
"frame mask generation failed\n");
return 1;
}
@ -1932,11 +1928,11 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
send_raw:
#if 0
fprintf(stderr, "send %ld: ", len + post);
lwsl_debug("send %ld: ", len + post);
for (n = -pre; n < ((int)len + post); n++)
fprintf(stderr, "%02X ", buf[n]);
lwsl_debug("%02X ", buf[n]);
fprintf(stderr, "\n");
lwsl_debug("\n");
#endif
if (protocol == LWS_WRITE_HTTP) {

View file

@ -78,35 +78,34 @@
#include <openssl/sha.h>
#endif
#include "libwebsockets.h"
#if 0
#define _DEBUG
#endif
extern void _lws_log(int filter, const char *format, ...);
/* warn and log are always compiled in */
#define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
#define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
/*
* weaker logging can be deselected at configure time using disable_debug
* that gets rid of the overhead of checking while keeping _warn and _err
* active
*/
#ifdef _DEBUG
#ifdef WIN32
#define _debug(...) lws_log(LWS_LOG_DEBUG, __VA_ARGS__)
#else
static inline
#define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
#define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
#define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
#define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
#define lwsl_ext(...) _lws_log(LLL_HEADER, __VA_ARGS__)
#define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
#else /* no debug */
#define lwsl_info(...)
#define lwsl_debug(...)
#define lwsl_parser(...)
#define lwsl_header(...)
#define lwsl_ext(...)
#define lwsl_client(...)
#endif
void debug(const char *format, ...)
{
va_list ap;
va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
}
#else
#ifdef WIN32
#define _debug(...)
#else
static inline
void _debug(const char *format, ...)
{
}
#endif
#endif
/*
* Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,

View file

@ -350,6 +350,21 @@ having to take any care about data visibility between the processes, it'll
"just work".
</blockquote>
<hr>
<h2>lws_set_log_level - Set the logging bitfield</h2>
<i>void</i>
<b>lws_set_log_level</b>
(<i>int</i> <b>level</b>)
<h3>Arguments</h3>
<dl>
<dt><b>level</b>
<dd>OR together the LLL_ debug contexts you want output from
</dl>
<h3>Description</h3>
<blockquote>
<p>
defaults to err and warn contexts enabled
</blockquote>
<hr>
<h2>libwebsocket_write - Apply protocol then write data to client</h2>
<i>int</i>
<b>libwebsocket_write</b>