From d2ac22c27abf34d3cccb3a6fe1e2c6d92123f436 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Fri, 11 Dec 2015 10:45:35 +0800 Subject: [PATCH] make protocols const require explicit context API BREAK The user protocols struct has not been const until now. This has been painful for a while because the semantics of the protocols struct look like it's going to be treated as const. At context creation, the protocols struct has been getting marked with the context, and three apis exploited that to only need to be passed a pointer to a protocol to get access to the context. This patch removes the two writeable members in the context (these were never directly used by user code), changes all pointers to protocols to be const, and adds an explicit first argument to the three affected apis so they can have access to context. The three affected apis are these LWS_VISIBLE LWS_EXTERN int -lws_callback_on_writable_all_protocol(const struct lws_protocols *protocol); +lws_callback_on_writable_all_protocol(const struct lws_context *context, + const struct lws_protocols *protocol); LWS_VISIBLE LWS_EXTERN int -lws_callback_all_protocol(const struct lws_protocols *protocol, int reason); +lws_callback_all_protocol(struct lws_context *context, + const struct lws_protocols *protocol, int reason); LWS_VISIBLE LWS_EXTERN void -lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol); +lws_rx_flow_allow_all_protocol(const struct lws_context *context, + const struct lws_protocols *protocol); unfortunately the original apis can no longer be emulated and users of them must update. Signed-off-by: Andy Green --- lib/client.c | 4 ++-- lib/context.c | 2 +- lib/extension-deflate-frame.c | 2 +- lib/extension-deflate-frame.h | 2 +- lib/extension-deflate-stream.c | 2 +- lib/extension-deflate-stream.h | 2 +- lib/extension.c | 2 +- lib/libwebsockets.c | 32 +++++------------------------- lib/libwebsockets.h | 28 +++++++++++++++----------- lib/pollfd.c | 6 ++---- lib/private-libwebsockets.h | 10 +++++----- lib/server-handshake.c | 2 +- test-server/test-client.c | 2 +- test-server/test-echo.c | 3 ++- test-server/test-server-mirror.c | 4 ++-- test-server/test-server-pthreads.c | 2 +- test-server/test-server.c | 2 +- 17 files changed, 44 insertions(+), 63 deletions(-) diff --git a/lib/client.c b/lib/client.c index 9beb5bec..073b293a 100644 --- a/lib/client.c +++ b/lib/client.c @@ -498,7 +498,7 @@ lws_client_interpret_server_handshake(struct lws_context *context, const char *pc; char *p; #ifndef LWS_NO_EXTENSIONS - struct lws_extension *ext; + const struct lws_extension *ext; char ext_name[128]; const char *c; int more = 1; @@ -828,7 +828,7 @@ lws_generate_client_handshake(struct lws_context *context, char buf[128], hash[20], key_b64[40], *p = pkt; int n; #ifndef LWS_NO_EXTENSIONS - struct lws_extension *ext; + const struct lws_extension *ext; int ext_count = 0; #endif diff --git a/lib/context.c b/lib/context.c index d9c05e65..ef8414be 100644 --- a/lib/context.c +++ b/lib/context.c @@ -245,7 +245,7 @@ bail: LWS_VISIBLE void lws_context_destroy(struct lws_context *context) { - struct lws_protocols *protocol = NULL; + const struct lws_protocols *protocol = NULL; int n; lwsl_notice("%s\n", __func__); diff --git a/lib/extension-deflate-frame.c b/lib/extension-deflate-frame.c index 87eac8b4..77cb6602 100644 --- a/lib/extension-deflate-frame.c +++ b/lib/extension-deflate-frame.c @@ -9,7 +9,7 @@ int lws_extension_callback_deflate_frame( struct lws_context *context, - struct lws_extension *ext, + const struct lws_extension *ext, struct lws *wsi, enum lws_extension_callback_reasons reason, void *user, void *in, size_t len) diff --git a/lib/extension-deflate-frame.h b/lib/extension-deflate-frame.h index 5e5b3f1f..edc9de1a 100644 --- a/lib/extension-deflate-frame.h +++ b/lib/extension-deflate-frame.h @@ -19,7 +19,7 @@ struct lws_ext_deflate_frame_conn { extern int lws_extension_callback_deflate_frame( struct lws_context *context, - struct lws_extension *ext, + const struct lws_extension *ext, struct lws *wsi, enum lws_extension_callback_reasons reason, void *user, void *in, size_t len); diff --git a/lib/extension-deflate-stream.c b/lib/extension-deflate-stream.c index f69b12be..5a1f1582 100644 --- a/lib/extension-deflate-stream.c +++ b/lib/extension-deflate-stream.c @@ -9,7 +9,7 @@ int lws_extension_callback_deflate_stream( struct lws_context *context, - struct lws_extension *ext, + const struct lws_extension *ext, struct lws *wsi, enum lws_extension_callback_reasons reason, void *user, void *in, size_t len) diff --git a/lib/extension-deflate-stream.h b/lib/extension-deflate-stream.h index b4177906..80e40e9e 100644 --- a/lib/extension-deflate-stream.h +++ b/lib/extension-deflate-stream.h @@ -14,7 +14,7 @@ struct lws_ext_deflate_stream_conn { extern int lws_extension_callback_deflate_stream( struct lws_context *context, - struct lws_extension *ext, + const struct lws_extension *ext, struct lws *wsi, enum lws_extension_callback_reasons reason, void *user, void *in, size_t len); diff --git a/lib/extension.c b/lib/extension.c index 540be100..33fbd0c5 100644 --- a/lib/extension.c +++ b/lib/extension.c @@ -73,7 +73,7 @@ int lws_ext_callback_for_each_extension_type( int reason, void *arg, int len) { int n = 0, m, handled = 0; - struct lws_extension *ext = context->extensions; + const struct lws_extension *ext = context->extensions; while (ext && ext->callback && !handled) { m = ext->callback(context, ext, wsi, reason, diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 54fddbce..d26e11a1 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -481,9 +481,9 @@ lws_context_user(struct lws_context *context) */ LWS_VISIBLE int -lws_callback_all_protocol(const struct lws_protocols *protocol, int reason) +lws_callback_all_protocol(struct lws_context *context, + const struct lws_protocols *protocol, int reason) { - struct lws_context *context = protocol->owning_server; struct lws *wsi; int n; @@ -616,9 +616,9 @@ lws_rx_flow_control(struct lws *wsi, int enable) */ LWS_VISIBLE void -lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol) +lws_rx_flow_allow_all_protocol(const struct lws_context *context, + const struct lws_protocols *protocol) { - struct lws_context *context = protocol->owning_server; int n; struct lws *wsi; @@ -936,7 +936,7 @@ lws_get_fops(struct lws_context *context) } LWS_VISIBLE LWS_EXTERN struct lws_context * -lws_get_ctx(struct lws *wsi) +lws_get_ctx(const struct lws *wsi) { return wsi->context; } @@ -1087,13 +1087,6 @@ libwebsockets_get_protocol(struct lws *wsi) return lws_get_protocol(wsi); } -#undef libwebsocket_callback_on_writable_all_protocol -LWS_VISIBLE LWS_EXTERN int -libwebsocket_callback_on_writable_all_protocol( - const struct lws_protocols *protocol) -{ - return lws_callback_on_writable_all_protocol(protocol); -} #undef libwebsocket_callback_on_writable LWS_VISIBLE LWS_EXTERN int @@ -1103,14 +1096,6 @@ libwebsocket_callback_on_writable(struct lws_context *context, return lws_callback_on_writable(context, wsi); } -#undef libwebsocket_callback_all_protocol -LWS_VISIBLE LWS_EXTERN int -libwebsocket_callback_all_protocol( - const struct lws_protocols *protocol, int reason) -{ - return lws_callback_all_protocol(protocol, reason); -} - #undef libwebsocket_get_socket_fd LWS_VISIBLE LWS_EXTERN int libwebsocket_get_socket_fd(struct lws *wsi) @@ -1139,13 +1124,6 @@ libwebsocket_rx_flow_control(struct lws *wsi, int enable) return lws_rx_flow_control(wsi, enable); } -#undef libwebsocket_rx_flow_allow_all_protocol -LWS_VISIBLE LWS_EXTERN void -libwebsocket_rx_flow_allow_all_protocol(const struct lws_protocols *protocol) -{ - lws_rx_flow_allow_all_protocol(protocol); -} - #undef libwebsockets_remaining_packet_payload LWS_VISIBLE LWS_EXTERN size_t libwebsockets_remaining_packet_payload(struct lws *wsi) diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 2dd55aac..6da6d092 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1090,10 +1090,11 @@ struct lws_extension; * duration of wsi dereference from the other thread context. */ LWS_VISIBLE LWS_EXTERN int -callback(struct lws_context *context, struct lws *wsi, +callback(const struct lws_context *context, const struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); -typedef int (callback_function)(struct lws_context *context, struct lws *wsi, +typedef int (callback_function)(struct lws_context *context, + struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); @@ -1157,12 +1158,12 @@ typedef int (callback_function)(struct lws_context *context, struct lws *wsi, * set the lws_tokens token pointer to it. */ LWS_VISIBLE LWS_EXTERN int -extension_callback(struct lws_context *context, struct lws_extension *ext, +extension_callback(struct lws_context *context, const struct lws_extension *ext, struct lws *wsi, enum lws_extension_callback_reasons reason, void *user, void *in, size_t len); typedef int (extension_callback_function)(struct lws_context *context, - struct lws_extension *ext, struct lws *wsi, + const struct lws_extension *ext, struct lws *wsi, enum lws_extension_callback_reasons reason, void *user, void *in, size_t len); #endif @@ -1298,9 +1299,9 @@ struct lws_extension { struct lws_context_creation_info { int port; const char *iface; - struct lws_protocols *protocols; - struct lws_extension *extensions; - struct lws_token_limits *token_limits; + const struct lws_protocols *protocols; + const struct lws_extension *extensions; + const struct lws_token_limits *token_limits; const char *ssl_private_key_password; const char *ssl_cert_filepath; const char *ssl_private_key_filepath; @@ -1516,13 +1517,15 @@ LWS_VISIBLE LWS_EXTERN const struct lws_protocols * lws_get_protocol(struct lws *wsi); LWS_VISIBLE LWS_EXTERN int -lws_callback_on_writable(struct lws_context *context, struct lws *wsi); +lws_callback_on_writable(const struct lws_context *context, struct lws *wsi); LWS_VISIBLE LWS_EXTERN int -lws_callback_on_writable_all_protocol(const struct lws_protocols *protocol); +lws_callback_on_writable_all_protocol(const struct lws_context *context, + const struct lws_protocols *protocol); LWS_VISIBLE LWS_EXTERN int -lws_callback_all_protocol(const struct lws_protocols *protocol, int reason); +lws_callback_all_protocol(struct lws_context *context, + const struct lws_protocols *protocol, int reason); LWS_VISIBLE LWS_EXTERN int lws_get_socket_fd(struct lws *wsi); @@ -1537,7 +1540,8 @@ LWS_VISIBLE LWS_EXTERN int lws_rx_flow_control(struct lws *wsi, int enable); LWS_VISIBLE LWS_EXTERN void -lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol); +lws_rx_flow_allow_all_protocol(const struct lws_context *context, + const struct lws_protocols *protocol); LWS_VISIBLE LWS_EXTERN size_t lws_remaining_packet_payload(struct lws *wsi); @@ -1630,7 +1634,7 @@ LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * lws_get_fops(struct lws_context *context); LWS_VISIBLE LWS_EXTERN struct lws_context * -lws_get_ctx(struct lws *wsi); +lws_get_ctx(const struct lws *wsi); /* * File Operations access helpers diff --git a/lib/pollfd.c b/lib/pollfd.c index 6e47dcc8..a855bc3d 100644 --- a/lib/pollfd.c +++ b/lib/pollfd.c @@ -206,8 +206,7 @@ lws_change_pollfd(struct lws *wsi, int _and, int _or) */ LWS_VISIBLE int -lws_callback_on_writable(struct lws_context *context, - struct lws *wsi) +lws_callback_on_writable(const struct lws_context *context, struct lws *wsi) { #ifdef LWS_USE_HTTP2 struct lws *network_wsi, *wsi2; @@ -284,10 +283,9 @@ network_sock: */ LWS_VISIBLE int -lws_callback_on_writable_all_protocol( +lws_callback_on_writable_all_protocol(const struct lws_context *context, const struct lws_protocols *protocol) { - struct lws_context *context = protocol->owning_server; int n; struct lws *wsi; diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 063026f7..81773066 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -525,12 +525,12 @@ struct lws_context { #else #define lws_ssl_anybody_has_buffered_read(ctx) (0) #endif - struct lws_protocols *protocols; + const struct lws_protocols *protocols; int count_protocols; #ifndef LWS_NO_EXTENSIONS - struct lws_extension *extensions; + const struct lws_extension *extensions; #endif - struct lws_token_limits *token_limits; + const struct lws_token_limits *token_limits; void *user_space; struct lws_plat_file_ops fops; @@ -836,10 +836,10 @@ struct lws { struct lws_io_watcher w_read; struct lws_io_watcher w_write; #endif /* LWS_USE_LIBEV */ - const struct lws_context *context; + struct lws_context *context; const struct lws_protocols *protocol; #ifndef LWS_NO_EXTENSIONS - struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE]; + const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE]; void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE]; unsigned char count_active_extensions; unsigned int extension_data_pending:1; diff --git a/lib/server-handshake.c b/lib/server-handshake.c index f5627162..2d7441b2 100644 --- a/lib/server-handshake.c +++ b/lib/server-handshake.c @@ -30,7 +30,7 @@ lws_extension_server_handshake(struct lws_context *context, int n; char *c; char ext_name[128]; - struct lws_extension *ext; + const struct lws_extension *ext; int ext_count = 0; int more = 1; diff --git a/test-server/test-client.c b/test-server/test-client.c index 59e6f4f5..7ab9f586 100644 --- a/test-server/test-client.c +++ b/test-server/test-client.c @@ -72,7 +72,7 @@ enum demo_protocols { */ static int -callback_dumb_increment(struct lws_context *this, +callback_dumb_increment(struct lws_context *context, struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) diff --git a/test-server/test-echo.c b/test-server/test-echo.c index ccdc3c51..d9e65821 100644 --- a/test-server/test-echo.c +++ b/test-server/test-echo.c @@ -416,7 +416,8 @@ int main(int argc, char **argv) gettimeofday(&tv, NULL); if (((((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec) - oldus) > rate_us) { - lws_callback_on_writable_all_protocol(&protocols[0]); + lws_callback_on_writable_all_protocol(context, + &protocols[0]); oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec; } } diff --git a/test-server/test-server-mirror.c b/test-server/test-server-mirror.c index 0e95e4f5..3f7f2b2e 100644 --- a/test-server/test-server-mirror.c +++ b/test-server/test-server-mirror.c @@ -82,7 +82,7 @@ callback_lws_mirror(struct lws_context *context, if (((ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) - lws_rx_flow_allow_all_protocol( + lws_rx_flow_allow_all_protocol(context, lws_get_protocol(wsi)); if (lws_partial_buffered(wsi) || lws_send_pipe_choked(wsi)) { @@ -122,7 +122,7 @@ choke: lws_rx_flow_control(wsi, 0); done: - lws_callback_on_writable_all_protocol( + lws_callback_on_writable_all_protocol(context, lws_get_protocol(wsi)); break; diff --git a/test-server/test-server-pthreads.c b/test-server/test-server-pthreads.c index 3d3c3d66..c128eff9 100644 --- a/test-server/test-server-pthreads.c +++ b/test-server/test-server-pthreads.c @@ -123,7 +123,7 @@ void *thread_dumb_increment(void *threadid) * them is protected by the same lock */ pthread_mutex_lock(&lock_established_conns); - lws_callback_on_writable_all_protocol( + lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_DUMB_INCREMENT]); pthread_mutex_unlock(&lock_established_conns); usleep(100000); diff --git a/test-server/test-server.c b/test-server/test-server.c index 5620e4e7..0f55b006 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -314,7 +314,7 @@ int main(int argc, char **argv) ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); if ((ms - oldms) > 50) { - lws_callback_on_writable_all_protocol( + lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_DUMB_INCREMENT]); oldms = ms; }