mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-16 00:00:07 +01:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
5775e4f338
21 changed files with 213 additions and 184 deletions
|
@ -277,6 +277,7 @@ set(SOURCES
|
|||
lib/parsers.c
|
||||
lib/context.c
|
||||
lib/sha-1.c
|
||||
lib/alloc.c
|
||||
)
|
||||
|
||||
if (NOT LWS_WITHOUT_CLIENT)
|
||||
|
|
30
lib/alloc.c
Normal file
30
lib/alloc.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "private-libwebsockets.h"
|
||||
|
||||
static void *_realloc(void *ptr, size_t size)
|
||||
{
|
||||
if (size)
|
||||
return realloc(ptr, size);
|
||||
else if (ptr)
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *(*_lws_realloc)(void *ptr, size_t size) = _realloc;
|
||||
|
||||
void *lws_realloc(void *ptr, size_t size)
|
||||
{
|
||||
return _lws_realloc(ptr, size);
|
||||
}
|
||||
|
||||
void *lws_zalloc(size_t size)
|
||||
{
|
||||
void *ptr = _lws_realloc(NULL, size);
|
||||
if (ptr)
|
||||
memset(ptr, 0, size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void lws_set_allocator(void *(*cb)(void *ptr, size_t size))
|
||||
{
|
||||
_lws_realloc = cb;
|
||||
}
|
|
@ -262,8 +262,8 @@ struct libwebsocket *libwebsocket_client_connect_2(
|
|||
return wsi;
|
||||
|
||||
oom4:
|
||||
free(wsi->u.hdr.ah);
|
||||
free(wsi);
|
||||
lws_free(wsi->u.hdr.ah);
|
||||
lws_free(wsi);
|
||||
return NULL;
|
||||
|
||||
failed:
|
||||
|
@ -305,11 +305,10 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
|
|||
{
|
||||
struct libwebsocket *wsi;
|
||||
|
||||
wsi = (struct libwebsocket *) malloc(sizeof(struct libwebsocket));
|
||||
wsi = lws_zalloc(sizeof(struct libwebsocket));
|
||||
if (wsi == NULL)
|
||||
goto bail;
|
||||
|
||||
memset(wsi, 0, sizeof(*wsi));
|
||||
wsi->sock = -1;
|
||||
|
||||
/* -1 means just use latest supported */
|
||||
|
@ -390,9 +389,9 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
|
|||
return libwebsocket_client_connect_2(context, wsi);
|
||||
|
||||
bail1:
|
||||
free(wsi->u.hdr.ah);
|
||||
lws_free(wsi->u.hdr.ah);
|
||||
bail:
|
||||
free(wsi);
|
||||
lws_free(wsi);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
16
lib/client.c
16
lib/client.c
|
@ -675,15 +675,12 @@ check_extensions:
|
|||
|
||||
wsi->active_extensions_user[
|
||||
wsi->count_active_extensions] =
|
||||
malloc(ext->per_session_data_size);
|
||||
lws_zalloc(ext->per_session_data_size);
|
||||
if (wsi->active_extensions_user[
|
||||
wsi->count_active_extensions] == NULL) {
|
||||
lwsl_err("Out of mem\n");
|
||||
goto bail2;
|
||||
}
|
||||
memset(wsi->active_extensions_user[
|
||||
wsi->count_active_extensions], 0,
|
||||
ext->per_session_data_size);
|
||||
wsi->active_extensions[
|
||||
wsi->count_active_extensions] = ext;
|
||||
|
||||
|
@ -744,8 +741,7 @@ check_accept:
|
|||
|
||||
/* free up his parsing allocations */
|
||||
|
||||
if (wsi->u.hdr.ah)
|
||||
free(wsi->u.hdr.ah);
|
||||
lws_free(wsi->u.hdr.ah);
|
||||
|
||||
lws_union_transition(wsi, LWS_CONNMODE_WS_CLIENT);
|
||||
wsi->state = WSI_STATE_ESTABLISHED;
|
||||
|
@ -762,7 +758,7 @@ check_accept:
|
|||
if (!n)
|
||||
n = LWS_MAX_SOCKET_IO_BUF;
|
||||
n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
|
||||
wsi->u.ws.rx_user_buffer = malloc(n);
|
||||
wsi->u.ws.rx_user_buffer = lws_malloc(n);
|
||||
if (!wsi->u.ws.rx_user_buffer) {
|
||||
lwsl_err("Out of Mem allocating rx buffer %d\n", n);
|
||||
goto bail2;
|
||||
|
@ -804,8 +800,7 @@ check_accept:
|
|||
return 0;
|
||||
|
||||
bail3:
|
||||
free(wsi->u.ws.rx_user_buffer);
|
||||
wsi->u.ws.rx_user_buffer = NULL;
|
||||
lws_free2(wsi->u.ws.rx_user_buffer);
|
||||
close_reason = LWS_CLOSE_STATUS_NOSTATUS;
|
||||
|
||||
bail2:
|
||||
|
@ -818,8 +813,7 @@ bail2:
|
|||
|
||||
/* free up his parsing allocations */
|
||||
|
||||
if (wsi->u.hdr.ah)
|
||||
free(wsi->u.hdr.ah);
|
||||
lws_free(wsi->u.hdr.ah);
|
||||
|
||||
libwebsocket_close_and_free_session(context, wsi, close_reason);
|
||||
|
||||
|
|
|
@ -101,13 +101,11 @@ libwebsocket_create_context(struct lws_context_creation_info *info)
|
|||
if (lws_plat_context_early_init())
|
||||
return NULL;
|
||||
|
||||
context = (struct libwebsocket_context *)
|
||||
malloc(sizeof(struct libwebsocket_context));
|
||||
context = lws_zalloc(sizeof(struct libwebsocket_context));
|
||||
if (!context) {
|
||||
lwsl_err("No memory for websocket context\n");
|
||||
return NULL;
|
||||
}
|
||||
memset(context, 0, sizeof(*context));
|
||||
|
||||
if (pid_daemon) {
|
||||
context->started_with_parent = pid_daemon;
|
||||
|
@ -138,33 +136,29 @@ libwebsocket_create_context(struct lws_context_creation_info *info)
|
|||
sizeof(struct libwebsocket *)) *
|
||||
context->max_fds));
|
||||
|
||||
context->fds = (struct libwebsocket_pollfd *)
|
||||
malloc(sizeof(struct libwebsocket_pollfd) *
|
||||
context->max_fds);
|
||||
context->fds = lws_zalloc(sizeof(struct libwebsocket_pollfd) *
|
||||
context->max_fds);
|
||||
if (context->fds == NULL) {
|
||||
lwsl_err("Unable to allocate fds array for %d connections\n",
|
||||
context->max_fds);
|
||||
free(context);
|
||||
lws_free(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
context->lws_lookup = (struct libwebsocket **)
|
||||
malloc(sizeof(struct libwebsocket *) * context->max_fds);
|
||||
context->lws_lookup = lws_zalloc(sizeof(struct libwebsocket *) * context->max_fds);
|
||||
if (context->lws_lookup == NULL) {
|
||||
lwsl_err(
|
||||
"Unable to allocate lws_lookup array for %d connections\n",
|
||||
context->max_fds);
|
||||
free(context->fds);
|
||||
free(context);
|
||||
lws_free(context->fds);
|
||||
lws_free(context);
|
||||
return NULL;
|
||||
}
|
||||
memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
|
||||
context->max_fds);
|
||||
|
||||
if (lws_plat_init_fd_tables(context)) {
|
||||
free(context->lws_lookup);
|
||||
free(context->fds);
|
||||
free(context);
|
||||
lws_free(context->lws_lookup);
|
||||
lws_free(context->fds);
|
||||
lws_free(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -334,12 +328,10 @@ libwebsocket_context_destroy(struct libwebsocket_context *context)
|
|||
|
||||
lws_ssl_context_destroy(context);
|
||||
|
||||
if (context->fds)
|
||||
free(context->fds);
|
||||
if (context->lws_lookup)
|
||||
free(context->lws_lookup);
|
||||
lws_free(context->fds);
|
||||
lws_free(context->lws_lookup);
|
||||
|
||||
lws_plat_context_late_destroy(context);
|
||||
|
||||
free(context);
|
||||
lws_free(context);
|
||||
}
|
||||
|
|
|
@ -76,8 +76,7 @@ static void lws_daemon_closing(int sigact)
|
|||
if (getpid() == pid_daemon)
|
||||
if (lock_path) {
|
||||
unlink(lock_path);
|
||||
free(lock_path);
|
||||
lock_path = NULL;
|
||||
lws_free2(lock_path);
|
||||
}
|
||||
|
||||
kill(getpid(), SIGKILL);
|
||||
|
@ -124,7 +123,7 @@ lws_daemonize(const char *_lock_path)
|
|||
}
|
||||
|
||||
n = strlen(_lock_path) + 1;
|
||||
lock_path = malloc(n);
|
||||
lock_path = lws_malloc(n);
|
||||
if (!lock_path) {
|
||||
fprintf(stderr, "Out of mem in lws_daemonize\n");
|
||||
return 1;
|
||||
|
|
|
@ -54,16 +54,14 @@ int lws_extension_callback_deflate_frame(
|
|||
conn->buf_out_length = sizeof(conn->buf_out);
|
||||
conn->compressed_out = 0;
|
||||
conn->buf_pre = NULL;
|
||||
conn->buf_in = (unsigned char *)
|
||||
malloc(LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_in_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
conn->buf_in = lws_malloc(LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_in_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
if (!conn->buf_in)
|
||||
goto bail;
|
||||
conn->buf_out = (unsigned char *)
|
||||
malloc(LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_out_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
conn->buf_out = lws_malloc(LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_out_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
if (!conn->buf_out)
|
||||
goto bail;
|
||||
lwsl_ext("zlibs constructed\n");
|
||||
|
@ -75,10 +73,9 @@ bail:
|
|||
return -1;
|
||||
|
||||
case LWS_EXT_CALLBACK_DESTROY:
|
||||
if (conn->buf_pre)
|
||||
free(conn->buf_pre);
|
||||
free(conn->buf_in);
|
||||
free(conn->buf_out);
|
||||
lws_free(conn->buf_pre);
|
||||
lws_free(conn->buf_in);
|
||||
lws_free(conn->buf_out);
|
||||
conn->buf_pre_used = 0;
|
||||
conn->buf_pre_length = 0;
|
||||
conn->buf_in_length = 0;
|
||||
|
@ -106,10 +103,8 @@ bail:
|
|||
|
||||
if (conn->buf_pre_length < total_payload) {
|
||||
conn->buf_pre_length = total_payload;
|
||||
if (conn->buf_pre)
|
||||
free(conn->buf_pre);
|
||||
conn->buf_pre =
|
||||
(unsigned char *)malloc(total_payload + 4);
|
||||
lws_free(conn->buf_pre);
|
||||
conn->buf_pre = lws_malloc(total_payload + 4);
|
||||
if (!conn->buf_pre) {
|
||||
lwsl_err("Out of memory\n");
|
||||
return -1;
|
||||
|
@ -180,10 +175,10 @@ bail:
|
|||
LWS_MAX_ZLIB_CONN_BUFFER);
|
||||
return -1;
|
||||
}
|
||||
conn->buf_in = (unsigned char *)realloc(conn->buf_in,
|
||||
LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_in_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
conn->buf_in = lws_realloc(conn->buf_in,
|
||||
LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_in_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
if (!conn->buf_in) {
|
||||
lwsl_err("Out of memory\n");
|
||||
return -1;
|
||||
|
@ -243,11 +238,10 @@ bail:
|
|||
LWS_MAX_ZLIB_CONN_BUFFER);
|
||||
return -1;
|
||||
}
|
||||
conn->buf_out = (unsigned char *)realloc(
|
||||
conn->buf_out,
|
||||
LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_out_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
conn->buf_out = lws_realloc(conn->buf_out,
|
||||
LWS_SEND_BUFFER_PRE_PADDING +
|
||||
conn->buf_out_length +
|
||||
LWS_SEND_BUFFER_POST_PADDING);
|
||||
if (!conn->buf_out) {
|
||||
lwsl_err("Out of memory\n");
|
||||
return -1;
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
#endif
|
||||
|
||||
#include "getifaddrs.h"
|
||||
#include "private-libwebsockets.h"
|
||||
|
||||
static int
|
||||
getifaddrs2(struct ifaddrs **ifap,
|
||||
|
@ -85,7 +86,7 @@ getifaddrs2(struct ifaddrs **ifap,
|
|||
|
||||
buf_size = 8192;
|
||||
for (;;) {
|
||||
buf = calloc(1, buf_size);
|
||||
buf = lws_zalloc(buf_size);
|
||||
if (buf == NULL) {
|
||||
ret = ENOMEM;
|
||||
goto error_out;
|
||||
|
@ -107,7 +108,7 @@ getifaddrs2(struct ifaddrs **ifap,
|
|||
|
||||
if (ifconf.ifc_len < (int)buf_size)
|
||||
break;
|
||||
free(buf);
|
||||
lws_free(buf);
|
||||
buf_size *= 2;
|
||||
}
|
||||
|
||||
|
@ -137,12 +138,12 @@ getifaddrs2(struct ifaddrs **ifap,
|
|||
goto error_out;
|
||||
}
|
||||
|
||||
*end = malloc(sizeof(**end));
|
||||
*end = lws_malloc(sizeof(**end));
|
||||
|
||||
(*end)->ifa_next = NULL;
|
||||
(*end)->ifa_name = strdup(ifr->ifr_name);
|
||||
(*end)->ifa_flags = ifreq.ifr_flags;
|
||||
(*end)->ifa_addr = malloc(salen);
|
||||
(*end)->ifa_addr = lws_malloc(salen);
|
||||
memcpy((*end)->ifa_addr, sa, salen);
|
||||
(*end)->ifa_netmask = NULL;
|
||||
|
||||
|
@ -150,11 +151,12 @@ getifaddrs2(struct ifaddrs **ifap,
|
|||
/* fix these when we actually need them */
|
||||
if (ifreq.ifr_flags & IFF_BROADCAST) {
|
||||
(*end)->ifa_broadaddr =
|
||||
malloc(sizeof(ifr->ifr_broadaddr));
|
||||
lws_malloc(sizeof(ifr->ifr_broadaddr));
|
||||
memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
|
||||
sizeof(ifr->ifr_broadaddr));
|
||||
} else if (ifreq.ifr_flags & IFF_POINTOPOINT) {
|
||||
(*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr));
|
||||
(*end)->ifa_dstaddr =
|
||||
lws_malloc(sizeof(ifr->ifr_dstaddr));
|
||||
memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
|
||||
sizeof(ifr->ifr_dstaddr));
|
||||
} else
|
||||
|
@ -169,12 +171,12 @@ getifaddrs2(struct ifaddrs **ifap,
|
|||
}
|
||||
*ifap = start;
|
||||
close(fd);
|
||||
free(buf);
|
||||
lws_free(buf);
|
||||
return 0;
|
||||
|
||||
error_out:
|
||||
close(fd);
|
||||
free(buf);
|
||||
lws_free(buf);
|
||||
errno = ret;
|
||||
|
||||
return -1;
|
||||
|
@ -209,18 +211,14 @@ freeifaddrs(struct ifaddrs *ifp)
|
|||
struct ifaddrs *p, *q;
|
||||
|
||||
for (p = ifp; p; ) {
|
||||
free(p->ifa_name);
|
||||
if (p->ifa_addr)
|
||||
free(p->ifa_addr);
|
||||
if (p->ifa_dstaddr)
|
||||
free(p->ifa_dstaddr);
|
||||
if (p->ifa_netmask)
|
||||
free(p->ifa_netmask);
|
||||
if (p->ifa_data)
|
||||
free(p->ifa_data);
|
||||
lws_free(p->ifa_name);
|
||||
lws_free(p->ifa_addr);
|
||||
lws_free(p->ifa_dstaddr);
|
||||
lws_free(p->ifa_netmask);
|
||||
lws_free(p->ifa_data);
|
||||
q = p;
|
||||
p = p->ifa_next;
|
||||
free(q);
|
||||
lws_free(q);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
17
lib/hpack.c
17
lib/hpack.c
|
@ -278,17 +278,16 @@ static int lws_hpack_add_dynamic_header(struct libwebsocket *wsi, int token, cha
|
|||
dyn = wsi->u.http2.hpack_dyn_table;
|
||||
|
||||
if (!dyn) {
|
||||
dyn = malloc(sizeof(*dyn));
|
||||
dyn = lws_zalloc(sizeof(*dyn));
|
||||
if (!dyn)
|
||||
return 1;
|
||||
memset(dyn, 0, sizeof(*dyn));
|
||||
wsi->u.http2.hpack_dyn_table = dyn;
|
||||
|
||||
dyn->args = malloc(1024);
|
||||
dyn->args = lws_malloc(1024);
|
||||
if (!dyn->args)
|
||||
goto bail1;
|
||||
dyn->args_length = 1024;
|
||||
dyn->entries = malloc(sizeof(dyn->entries[0]) * 20);
|
||||
dyn->entries = lws_malloc(sizeof(dyn->entries[0]) * 20);
|
||||
if (!dyn->entries)
|
||||
goto bail2;
|
||||
dyn->num_entries = 20;
|
||||
|
@ -312,13 +311,13 @@ static int lws_hpack_add_dynamic_header(struct libwebsocket *wsi, int token, cha
|
|||
dyn->next++;
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
bail2:
|
||||
free(dyn->args);
|
||||
lws_free(dyn->args);
|
||||
bail1:
|
||||
free(dyn);
|
||||
lws_free(dyn);
|
||||
wsi->u.http2.hpack_dyn_table = NULL;
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -681,4 +680,4 @@ int lws_add_http2_header_status(struct libwebsocket_context *context,
|
|||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -208,35 +208,26 @@ just_kill_connection:
|
|||
remove_wsi_socket_from_fds(context, wsi);
|
||||
|
||||
wsi->state = WSI_STATE_DEAD_SOCKET;
|
||||
|
||||
if (wsi->rxflow_buffer) {
|
||||
free(wsi->rxflow_buffer);
|
||||
wsi->rxflow_buffer = NULL;
|
||||
}
|
||||
|
||||
lws_free2(wsi->rxflow_buffer);
|
||||
|
||||
if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING && wsi->u.hdr.ah) {
|
||||
free(wsi->u.hdr.ah);
|
||||
wsi->u.hdr.ah = NULL;
|
||||
lws_free2(wsi->u.hdr.ah);
|
||||
}
|
||||
|
||||
|
||||
if ((old_state == WSI_STATE_ESTABLISHED ||
|
||||
wsi->mode == LWS_CONNMODE_WS_SERVING ||
|
||||
wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
|
||||
|
||||
if (wsi->u.ws.rx_user_buffer) {
|
||||
free(wsi->u.ws.rx_user_buffer);
|
||||
wsi->u.ws.rx_user_buffer = NULL;
|
||||
}
|
||||
lws_free2(wsi->u.ws.rx_user_buffer);
|
||||
|
||||
if (wsi->truncated_send_malloc) {
|
||||
/* not going to be completed... nuke it */
|
||||
free(wsi->truncated_send_malloc);
|
||||
wsi->truncated_send_malloc = NULL;
|
||||
lws_free2(wsi->truncated_send_malloc);
|
||||
wsi->truncated_send_len = 0;
|
||||
}
|
||||
if (wsi->u.ws.ping_payload_buf) {
|
||||
free(wsi->u.ws.ping_payload_buf);
|
||||
wsi->u.ws.ping_payload_buf = NULL;
|
||||
lws_free2(wsi->u.ws.ping_payload_buf);
|
||||
wsi->u.ws.ping_payload_alloc = 0;
|
||||
wsi->u.ws.ping_payload_len = 0;
|
||||
}
|
||||
|
@ -265,7 +256,7 @@ just_kill_connection:
|
|||
lwsl_warn("extension destruction failed\n");
|
||||
#ifndef LWS_NO_EXTENSIONS
|
||||
for (n = 0; n < wsi->count_active_extensions; n++)
|
||||
free(wsi->active_extensions_user[n]);
|
||||
lws_free(wsi->active_extensions_user[n]);
|
||||
#endif
|
||||
/*
|
||||
* inform all extensions in case they tracked this guy out of band
|
||||
|
@ -293,11 +284,11 @@ just_kill_connection:
|
|||
|
||||
if (wsi->protocol && wsi->protocol->per_session_data_size &&
|
||||
wsi->user_space && !wsi->user_space_externally_allocated)
|
||||
free(wsi->user_space);
|
||||
lws_free(wsi->user_space);
|
||||
|
||||
/* As a precaution, free the header table in case it lingered: */
|
||||
lws_free_header_table(wsi);
|
||||
free(wsi);
|
||||
lws_free(wsi);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -710,14 +701,11 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi)
|
|||
/* allocate the per-connection user memory (if any) */
|
||||
|
||||
if (wsi->protocol->per_session_data_size && !wsi->user_space) {
|
||||
wsi->user_space = malloc(
|
||||
wsi->protocol->per_session_data_size);
|
||||
wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
|
||||
if (wsi->user_space == NULL) {
|
||||
lwsl_err("Out of memory for conn user space\n");
|
||||
return 1;
|
||||
}
|
||||
memset(wsi->user_space, 0,
|
||||
wsi->protocol->per_session_data_size);
|
||||
} else
|
||||
lwsl_info("%s: %p protocol pss %u, user_space=%d\n", __func__, wsi, wsi->protocol->per_session_data_size, wsi->user_space);
|
||||
return 0;
|
||||
|
|
|
@ -160,6 +160,9 @@ LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
|
|||
/* you can call lws_get_peer_write_allowance */
|
||||
#define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
|
||||
|
||||
/* extra parameter introduced in 917f43ab821 */
|
||||
#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
|
||||
|
||||
enum libwebsocket_context_options {
|
||||
LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
|
||||
LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
|
||||
|
@ -1334,6 +1337,12 @@ libwebsocket_read(struct libwebsocket_context *context,
|
|||
LWS_VISIBLE LWS_EXTERN struct libwebsocket_extension *libwebsocket_get_internal_extensions();
|
||||
#endif
|
||||
|
||||
/*
|
||||
* custom allocator support
|
||||
*/
|
||||
LWS_VISIBLE LWS_EXTERN void
|
||||
lws_set_allocator(void *(*realloc)(void *ptr, size_t size));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -193,17 +193,16 @@ lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
|
|||
LWS_VISIBLE int
|
||||
lws_plat_init_fd_tables(struct libwebsocket_context *context)
|
||||
{
|
||||
context->events = (WSAEVENT *)malloc(sizeof(WSAEVENT) *
|
||||
(context->max_fds + 1));
|
||||
context->events = lws_malloc(sizeof(WSAEVENT) * (context->max_fds + 1));
|
||||
if (context->events == NULL) {
|
||||
lwsl_err("Unable to allocate events array for %d connections\n",
|
||||
context->max_fds);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
context->fds_count = 0;
|
||||
context->events[0] = WSACreateEvent();
|
||||
|
||||
|
||||
context->fd_random = 0;
|
||||
|
||||
return 0;
|
||||
|
@ -236,7 +235,7 @@ lws_plat_context_early_destroy(struct libwebsocket_context *context)
|
|||
{
|
||||
if (context->events) {
|
||||
WSACloseEvent(context->events[0]);
|
||||
free(context->events);
|
||||
lws_free(context->events);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -331,7 +330,7 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
|||
DWORD bufferlen = cnt;
|
||||
BOOL ok = FALSE;
|
||||
|
||||
buffer = malloc(bufferlen);
|
||||
buffer = lws_malloc(bufferlen);
|
||||
if (!buffer) {
|
||||
lwsl_err("Out of memory\n");
|
||||
return NULL;
|
||||
|
@ -366,6 +365,6 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
|||
ok = FALSE;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
lws_free(buffer);
|
||||
return ok ? dst : NULL;
|
||||
}
|
||||
|
|
|
@ -195,11 +195,10 @@ handle_truncated_send:
|
|||
*/
|
||||
if (!wsi->truncated_send_malloc ||
|
||||
real_len - n > wsi->truncated_send_allocation) {
|
||||
if (wsi->truncated_send_malloc)
|
||||
free(wsi->truncated_send_malloc);
|
||||
lws_free(wsi->truncated_send_malloc);
|
||||
|
||||
wsi->truncated_send_allocation = real_len - n;
|
||||
wsi->truncated_send_malloc = malloc(real_len - n);
|
||||
wsi->truncated_send_malloc = lws_malloc(real_len - n);
|
||||
if (!wsi->truncated_send_malloc) {
|
||||
lwsl_err("truncated send: unable to malloc %d\n",
|
||||
real_len - n);
|
||||
|
|
|
@ -63,7 +63,7 @@ int lws_allocate_header_table(struct libwebsocket *wsi)
|
|||
{
|
||||
/* Be sure to free any existing header data to avoid mem leak: */
|
||||
lws_free_header_table(wsi);
|
||||
wsi->u.hdr.ah = malloc(sizeof(*wsi->u.hdr.ah));
|
||||
wsi->u.hdr.ah = lws_malloc(sizeof(*wsi->u.hdr.ah));
|
||||
if (wsi->u.hdr.ah == NULL) {
|
||||
lwsl_err("Out of memory\n");
|
||||
return -1;
|
||||
|
@ -77,11 +77,9 @@ int lws_allocate_header_table(struct libwebsocket *wsi)
|
|||
|
||||
int lws_free_header_table(struct libwebsocket *wsi)
|
||||
{
|
||||
if (wsi->u.hdr.ah) {
|
||||
free(wsi->u.hdr.ah);
|
||||
wsi->u.hdr.ah = NULL;
|
||||
}
|
||||
return 0;
|
||||
lws_free2(wsi->u.hdr.ah);
|
||||
wsi->u.hdr.ah = NULL;
|
||||
return 0;
|
||||
};
|
||||
|
||||
LWS_VISIBLE int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h)
|
||||
|
@ -894,13 +892,13 @@ spill:
|
|||
/* 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) {
|
||||
free(wsi->u.ws.ping_payload_buf);
|
||||
wsi->u.ws.ping_payload_buf = NULL;
|
||||
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 = 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1157,6 +1157,19 @@ lws_ssl_capable_write_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int l
|
|||
#define lws_handshake_server(_a, _b, _c, _d) (0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* custom allocator
|
||||
*/
|
||||
LWS_EXTERN void*
|
||||
lws_realloc(void *ptr, size_t size);
|
||||
|
||||
LWS_EXTERN void*
|
||||
lws_zalloc(size_t size);
|
||||
|
||||
#define lws_malloc(S) lws_realloc(NULL, S)
|
||||
#define lws_free(P) lws_realloc(P, 0)
|
||||
#define lws_free2(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
|
||||
|
||||
/*
|
||||
* lws_plat_
|
||||
*/
|
||||
|
|
|
@ -124,15 +124,12 @@ lws_extension_server_handshake(struct libwebsocket_context *context,
|
|||
|
||||
wsi->active_extensions_user[
|
||||
wsi->count_active_extensions] =
|
||||
malloc(ext->per_session_data_size);
|
||||
lws_zalloc(ext->per_session_data_size);
|
||||
if (wsi->active_extensions_user[
|
||||
wsi->count_active_extensions] == NULL) {
|
||||
lwsl_err("Out of mem\n");
|
||||
return 1;
|
||||
}
|
||||
memset(wsi->active_extensions_user[
|
||||
wsi->count_active_extensions], 0,
|
||||
ext->per_session_data_size);
|
||||
|
||||
wsi->active_extensions[
|
||||
wsi->count_active_extensions] = ext;
|
||||
|
@ -154,7 +151,7 @@ lws_extension_server_handshake(struct libwebsocket_context *context,
|
|||
|
||||
n = 0;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
26
lib/server.c
26
lib/server.c
|
@ -102,7 +102,7 @@ int lws_context_init_server(struct lws_context_creation_info *info,
|
|||
compatible_close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
|
||||
lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
|
||||
else
|
||||
|
@ -110,13 +110,12 @@ int lws_context_init_server(struct lws_context_creation_info *info,
|
|||
|
||||
context->listen_port = info->port;
|
||||
|
||||
wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
|
||||
wsi = lws_zalloc(sizeof(struct libwebsocket));
|
||||
if (wsi == NULL) {
|
||||
lwsl_err("Out of mem\n");
|
||||
compatible_close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
memset(wsi, 0, sizeof(struct libwebsocket));
|
||||
wsi->sock = sockfd;
|
||||
wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
|
||||
|
||||
|
@ -128,7 +127,7 @@ int lws_context_init_server(struct lws_context_creation_info *info,
|
|||
|
||||
listen(sockfd, LWS_SOMAXCONN);
|
||||
lwsl_notice(" Listening on port %d\n", info->port);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -296,10 +295,7 @@ got_uri:
|
|||
}
|
||||
|
||||
/* now drop the header info we kept a pointer to */
|
||||
if (wsi->u.http.ah)
|
||||
free(wsi->u.http.ah);
|
||||
/* not possible to continue to use past here */
|
||||
wsi->u.http.ah = NULL;
|
||||
lws_free2(wsi->u.http.ah);
|
||||
|
||||
if (n) {
|
||||
lwsl_info("LWS_CALLBACK_HTTP closing\n");
|
||||
|
@ -322,11 +318,8 @@ got_uri:
|
|||
|
||||
bail_nuke_ah:
|
||||
/* drop the header info */
|
||||
if (wsi->u.hdr.ah) {
|
||||
free(wsi->u.hdr.ah);
|
||||
wsi->u.hdr.ah = NULL;
|
||||
}
|
||||
|
||||
lws_free2(wsi->u.hdr.ah);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -560,7 +553,7 @@ upgrade_ws:
|
|||
if (!n)
|
||||
n = LWS_MAX_SOCKET_IO_BUF;
|
||||
n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
|
||||
wsi->u.ws.rx_user_buffer = malloc(n);
|
||||
wsi->u.ws.rx_user_buffer = lws_malloc(n);
|
||||
if (!wsi->u.ws.rx_user_buffer) {
|
||||
lwsl_err("Out of Mem allocating rx buffer %d\n", n);
|
||||
return 1;
|
||||
|
@ -589,13 +582,12 @@ libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
|
|||
{
|
||||
struct libwebsocket *new_wsi;
|
||||
|
||||
new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
|
||||
new_wsi = lws_zalloc(sizeof(struct libwebsocket));
|
||||
if (new_wsi == NULL) {
|
||||
lwsl_err("Out of memory for new connection\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(new_wsi, 0, sizeof(struct libwebsocket));
|
||||
new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
|
||||
new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
|
||||
|
||||
|
@ -606,7 +598,7 @@ libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
|
|||
new_wsi->hdr_parsing_completed = 0;
|
||||
|
||||
if (lws_allocate_header_table(new_wsi)) {
|
||||
free(new_wsi);
|
||||
lws_free(new_wsi);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -302,7 +302,7 @@ int lws_rxflow_cache(struct libwebsocket *wsi, unsigned char *buf, int n, int le
|
|||
|
||||
/* a new rxflow, buffer it and warn caller */
|
||||
lwsl_info("new rxflow input buffer len %d\n", len - n);
|
||||
wsi->rxflow_buffer = (unsigned char *)malloc(len - n);
|
||||
wsi->rxflow_buffer = lws_malloc(len - n);
|
||||
wsi->rxflow_len = len - n;
|
||||
wsi->rxflow_pos = 0;
|
||||
memcpy(wsi->rxflow_buffer, buf + n, len - n);
|
||||
|
@ -564,8 +564,7 @@ drain:
|
|||
if (draining_flow && wsi->rxflow_buffer &&
|
||||
wsi->rxflow_pos == wsi->rxflow_len) {
|
||||
lwsl_info("flow buffer: drained\n");
|
||||
free(wsi->rxflow_buffer);
|
||||
wsi->rxflow_buffer = NULL;
|
||||
lws_free2(wsi->rxflow_buffer);
|
||||
/* having drained the rxflow buffer, can rearm POLLIN */
|
||||
_libwebsocket_rx_flow_control(wsi); /* n ignored, needed for NO_SERVER case */
|
||||
}
|
||||
|
|
|
@ -480,7 +480,7 @@ lws_server_socket_service_ssl(struct libwebsocket_context *context,
|
|||
ERR_error_string(SSL_get_error(
|
||||
new_wsi->ssl, 0), NULL));
|
||||
libwebsockets_decode_ssl_error();
|
||||
free(new_wsi);
|
||||
lws_free(new_wsi);
|
||||
compatible_close(accept_fd);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
#include "../lib/libwebsockets.h"
|
||||
|
||||
static volatile int force_exit = 0;
|
||||
static int versa, state;
|
||||
|
||||
#define MAX_ECHO_PAYLOAD 1400
|
||||
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
|
||||
|
@ -67,6 +68,7 @@ callback_echo(struct libwebsocket_context *context,
|
|||
/* when the callback is used for server operations --> */
|
||||
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
do_tx:
|
||||
n = libwebsocket_write(wsi, &pss->buf[LWS_SEND_BUFFER_PRE_PADDING], pss->len, LWS_WRITE_TEXT);
|
||||
if (n < 0) {
|
||||
lwsl_err("ERROR %d writing to socket, hanging up\n", n);
|
||||
|
@ -79,6 +81,7 @@ callback_echo(struct libwebsocket_context *context,
|
|||
break;
|
||||
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
do_rx:
|
||||
if (len > MAX_ECHO_PAYLOAD) {
|
||||
lwsl_err("Server received packet bigger than %u, hanging up\n", MAX_ECHO_PAYLOAD);
|
||||
return 1;
|
||||
|
@ -92,16 +95,31 @@ callback_echo(struct libwebsocket_context *context,
|
|||
#ifndef LWS_NO_CLIENT
|
||||
/* when the callback is used for client operations --> */
|
||||
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||
lwsl_info("closed\n");
|
||||
state = 0;
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
||||
lwsl_notice("Client has connected\n");
|
||||
pss->index = 0;
|
||||
state = 2;
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
#ifndef LWS_NO_SERVER
|
||||
if (versa)
|
||||
goto do_rx;
|
||||
#endif
|
||||
lwsl_notice("Client RX: %s", (char *)in);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
||||
#ifndef LWS_NO_SERVER
|
||||
if (versa)
|
||||
goto do_tx;
|
||||
#endif
|
||||
/* we will send our packet... */
|
||||
pss->len = sprintf((char *)&pss->buf[LWS_SEND_BUFFER_PRE_PADDING], "hello from libwebsockets-test-echo client pid %d index %d\n", getpid(), pss->index++);
|
||||
lwsl_notice("Client TX: %s", &pss->buf[LWS_SEND_BUFFER_PRE_PADDING]);
|
||||
|
@ -157,6 +175,8 @@ static struct option options[] = {
|
|||
{ "ratems", required_argument, NULL, 'r' },
|
||||
#endif
|
||||
{ "ssl", no_argument, NULL, 's' },
|
||||
{ "versa", no_argument, NULL, 'v' },
|
||||
{ "uri", required_argument, NULL, 'u' },
|
||||
{ "passphrase", required_argument, NULL, 'P' },
|
||||
{ "interface", required_argument, NULL, 'i' },
|
||||
#ifndef LWS_NO_DAEMONIZE
|
||||
|
@ -183,6 +203,7 @@ int main(int argc, char **argv)
|
|||
int listen_port = 80;
|
||||
struct lws_context_creation_info info;
|
||||
char passphrase[256];
|
||||
char uri[256] = "/";
|
||||
#ifndef LWS_NO_CLIENT
|
||||
char address[256], ads_port[256 + 30];
|
||||
int rate_us = 250000;
|
||||
|
@ -206,7 +227,7 @@ int main(int argc, char **argv)
|
|||
#endif
|
||||
|
||||
while (n >= 0) {
|
||||
n = getopt_long(argc, argv, "i:hsp:d:DC:k:P:"
|
||||
n = getopt_long(argc, argv, "i:hsp:d:DC:k:P:vu:"
|
||||
#ifndef LWS_NO_CLIENT
|
||||
"c:r:"
|
||||
#endif
|
||||
|
@ -228,7 +249,11 @@ int main(int argc, char **argv)
|
|||
strncpy(ssl_key, optarg, sizeof(ssl_key));
|
||||
ssl_key[sizeof(ssl_key) - 1] = '\0';
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
strncpy(uri, optarg, sizeof(uri));
|
||||
uri[sizeof(uri) - 1] = '\0';
|
||||
break;
|
||||
|
||||
#ifndef LWS_NO_DAEMONIZE
|
||||
case 'D':
|
||||
daemonize = 1;
|
||||
|
@ -257,6 +282,9 @@ int main(int argc, char **argv)
|
|||
case 'p':
|
||||
port = atoi(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
versa = 1;
|
||||
break;
|
||||
case 'i':
|
||||
strncpy(interface_name, optarg, sizeof interface_name);
|
||||
interface_name[(sizeof interface_name) - 1] = '\0';
|
||||
|
@ -356,24 +384,7 @@ int main(int argc, char **argv)
|
|||
return -1;
|
||||
}
|
||||
|
||||
#ifndef LWS_NO_CLIENT
|
||||
if (client) {
|
||||
lwsl_notice("Client connecting to %s:%u....\n", address, port);
|
||||
/* we are in client mode */
|
||||
|
||||
address[sizeof(address) - 1] = '\0';
|
||||
sprintf(ads_port, "%s:%u\n", address, port & 65535);
|
||||
|
||||
wsi = libwebsocket_client_connect(context, address,
|
||||
port, use_ssl, "/", ads_port,
|
||||
"origin", NULL, -1);
|
||||
if (!wsi) {
|
||||
lwsl_err("Client failed to connect to %s:%u\n", address, port);
|
||||
goto bail;
|
||||
}
|
||||
lwsl_notice("Client connected to %s:%u\n", address, port);
|
||||
}
|
||||
#endif
|
||||
|
||||
signal(SIGINT, sighandler);
|
||||
|
||||
n = 0;
|
||||
|
@ -381,7 +392,24 @@ int main(int argc, char **argv)
|
|||
#ifndef LWS_NO_CLIENT
|
||||
struct timeval tv;
|
||||
|
||||
if (client) {
|
||||
if (client && !state) {
|
||||
state = 1;
|
||||
lwsl_notice("Client connecting to %s:%u....\n", address, port);
|
||||
/* we are in client mode */
|
||||
|
||||
address[sizeof(address) - 1] = '\0';
|
||||
sprintf(ads_port, "%s:%u\n", address, port & 65535);
|
||||
|
||||
wsi = libwebsocket_client_connect(context, address,
|
||||
port, use_ssl, uri, ads_port,
|
||||
"origin", NULL, -1);
|
||||
if (!wsi) {
|
||||
lwsl_err("Client failed to connect to %s:%u\n", address, port);
|
||||
goto bail;
|
||||
}
|
||||
}
|
||||
|
||||
if (client && !versa) {
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
if (((unsigned int)tv.tv_usec - oldus) > (unsigned int)rate_us) {
|
||||
|
|
|
@ -190,7 +190,8 @@ callback_lws_mirror(struct libwebsocket_context * this,
|
|||
|
||||
if (!flood)
|
||||
fprintf(stderr, "%d bytes from %s: req=%ld "
|
||||
"time=(unknown)\n", (int)len, address, l);
|
||||
"time=(unknown)\n", (int)len, address,
|
||||
(long)l);
|
||||
else
|
||||
fprintf(stderr, "\b \b");
|
||||
|
||||
|
@ -211,7 +212,7 @@ callback_lws_mirror(struct libwebsocket_context * this,
|
|||
|
||||
if (!flood)
|
||||
fprintf(stderr, "%d bytes from %s: req=%ld "
|
||||
"time=%lu.%lums\n", (int)len, address, l,
|
||||
"time=%lu.%lums\n", (int)len, address, (long)l,
|
||||
(iv - psd->ringbuffer[n].issue_timestamp) / 1000,
|
||||
((iv - psd->ringbuffer[n].issue_timestamp) / 100) % 10);
|
||||
else
|
||||
|
|
Loading…
Add table
Reference in a new issue