Skip to content

Instantly share code, notes, and snippets.

@moritzmhmk
Created December 20, 2015 15:28
Show Gist options
  • Save moritzmhmk/8a9ed4fd22b777cd1255 to your computer and use it in GitHub Desktop.
Save moritzmhmk/8a9ed4fd22b777cd1255 to your computer and use it in GitHub Desktop.
Patch mosquitto for new libwebsocket API
diff -Naur a/lib/net_mosq.c a/lib/net_mosq.c
--- a/lib/net_mosq.c 2015-12-20 16:21:08.560283714 +0100
+++ a/lib/net_mosq.c 2015-12-20 16:13:17.750398153 +0100
@@ -166,7 +166,7 @@
#ifdef WITH_BROKER
# ifdef WITH_WEBSOCKETS
if(mosq->wsi){
- libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi);
+ lws_callback_on_writable(mosq->wsi);
return 0;
}else{
return _mosquitto_packet_write(mosq);
@@ -232,7 +232,7 @@
mosq->state = mosq_cs_disconnect_ws;
}
if(mosq->wsi){
- libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi);
+ lws_callback_on_writable(mosq->wsi);
}
mosq->sock = INVALID_SOCKET;
#endif
diff -Naur a/src/loop.c a/src/loop.c
--- a/src/loop.c 2015-12-20 15:51:10.370587702 +0100
+++ a/src/loop.c 2015-12-20 16:04:47.640550234 +0100
@@ -376,7 +376,7 @@
* will soon, so for now websockets clients are second class
* citizens. */
if(db->config->listeners[i].ws_context){
- libwebsocket_service(db->config->listeners[i].ws_context, 0);
+ lws_service(db->config->listeners[i].ws_context, 0);
}
}
if(db->config->have_websockets_listener){
@@ -402,7 +402,7 @@
context->state = mosq_cs_disconnect_ws;
}
if(context->wsi){
- libwebsocket_callback_on_writable(context->ws_context, context->wsi);
+ lws_callback_on_writable(context->wsi);
}
context->sock = INVALID_SOCKET;
}else
diff -Naur a/src/mosquitto.c a/src/mosquitto.c
--- a/src/mosquitto.c 2015-12-20 15:50:31.800588988 +0100
+++ a/src/mosquitto.c 2015-12-20 15:55:08.490578442 +0100
@@ -377,7 +377,7 @@
#ifdef WITH_WEBSOCKETS
for(i=0; i<int_db.config->listener_count; i++){
if(int_db.config->listeners[i].ws_context){
- libwebsocket_context_destroy(int_db.config->listeners[i].ws_context);
+ lws_context_destroy(int_db.config->listeners[i].ws_context);
}
if(int_db.config->listeners[i].ws_protocol){
_mosquitto_free(int_db.config->listeners[i].ws_protocol);
diff -Naur a/src/mosquitto_broker.h a/src/mosquitto_broker.h
--- a/src/mosquitto_broker.h 2015-12-20 15:39:33.430593266 +0100
+++ a/src/mosquitto_broker.h 2015-12-20 15:41:02.960595561 +0100
@@ -74,9 +74,9 @@
char *tls_version;
#endif
#ifdef WITH_WEBSOCKETS
- struct libwebsocket_context *ws_context;
+ struct lws_context *ws_context;
char *http_dir;
- struct libwebsocket_protocols *ws_protocol;
+ struct lws_protocols *ws_protocol;
#endif
};
@@ -482,7 +482,7 @@
* Websockets related functions
* ============================================================ */
#ifdef WITH_WEBSOCKETS
-struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener, int log_level);
+struct lws_context *mosq_websockets_init(struct _mqtt3_listener *listener, int log_level);
#endif
void do_disconnect(struct mosquitto_db *db, struct mosquitto *context);
diff -Naur a/src/websockets.c a/src/websockets.c
--- a/src/websockets.c 2015-12-20 15:34:24.350572583 +0100
+++ a/src/websockets.c 2015-12-20 15:38:30.010590795 +0100
@@ -49,15 +49,15 @@
#endif
extern struct mosquitto_db int_db;
-static int callback_mqtt(struct libwebsocket_context *context,
- struct libwebsocket *wsi,
- enum libwebsocket_callback_reasons reason,
+static int callback_mqtt(struct lws_context *context,
+ struct lws *wsi,
+ enum lws_callback_reasons reason,
void *user,
void *in,
size_t len);
-static int callback_http(struct libwebsocket_context *context,
- struct libwebsocket *wsi,
- enum libwebsocket_callback_reasons reason,
+static int callback_http(struct lws_context *context,
+ struct lws *wsi,
+ enum lws_callback_reasons reason,
void *user,
void *in,
size_t len);
@@ -81,10 +81,10 @@
# define HTTP_STATUS_METHOD_NOT_ALLOWED 405
# define HTTP_STATUS_REQ_URI_TOO_LONG 414
# define HTTP_STATUS_INTERNAL_SERVER_ERROR 500
-# define libwebsockets_return_http_status(A, B, C, D)
+# define lws_return_http_status(A, B, C, D)
#endif
-static struct libwebsocket_protocols protocols[] = {
+static struct lws_protocols protocols[] = {
/* first protocol must always be HTTP handler */
{
"http-only",
@@ -135,9 +135,9 @@
}
}
-static int callback_mqtt(struct libwebsocket_context *context,
- struct libwebsocket *wsi,
- enum libwebsocket_callback_reasons reason,
+static int callback_mqtt(struct lws_context *context,
+ struct lws *wsi,
+ enum lws_callback_reasons reason,
void *user,
void *in,
size_t len)
@@ -164,7 +164,7 @@
}else{
return -1;
}
- easy_address(libwebsocket_get_socket_fd(wsi), mosq);
+ easy_address(lws_get_socket_fd(wsi), mosq);
if(!mosq->address){
/* getpeername and inet_ntop failed and not a bridge */
_mosquitto_free(mosq);
@@ -214,7 +214,7 @@
memmove(&packet->payload[LWS_SEND_BUFFER_PRE_PADDING], packet->payload, packet->packet_length);
packet->pos += LWS_SEND_BUFFER_PRE_PADDING;
}
- count = libwebsocket_write(wsi, &packet->payload[packet->pos], packet->to_process, LWS_WRITE_BINARY);
+ count = lws_write(wsi, &packet->payload[packet->pos], packet->to_process, LWS_WRITE_BINARY);
#ifdef LWS_IS_OLD
/* lws < 1.3 doesn't return a valid count, assume everything sent. */
count = packet->to_process;
@@ -243,11 +243,11 @@
mosq->last_msg_out = mosquitto_time();
if(mosq->current_out_packet){
- libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi);
+ lws_callback_on_writable(mosq->wsi);
}
}
if(mosq->current_out_packet){
- libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi);
+ lws_callback_on_writable(mosq->wsi);
}
break;
@@ -342,9 +342,9 @@
}
-static int callback_http(struct libwebsocket_context *context,
- struct libwebsocket *wsi,
- enum libwebsocket_callback_reasons reason,
+static int callback_http(struct lws_context *context,
+ struct lws *wsi,
+ enum lws_callback_reasons reason,
void *user,
void *in,
size_t len)
@@ -368,7 +368,7 @@
return -1;
}
- hack = (struct libws_mqtt_hack *)libwebsocket_context_user(context);
+ hack = (struct libws_mqtt_hack *)lws_context_user(context);
if(!hack){
return -1;
}
@@ -382,7 +382,7 @@
#ifndef LWS_IS_OLD
/* Forbid POST */
if(lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL);
return -1;
}
#endif
@@ -394,7 +394,7 @@
}
filename = _mosquitto_malloc(slen);
if(!filename){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return -1;
}
if(!strcmp((char *)in, "/")){
@@ -409,7 +409,7 @@
filename_canonical = _fullpath(NULL, filename, 0);
if(!filename_canonical){
_mosquitto_free(filename);
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return -1;
}
#else
@@ -417,13 +417,13 @@
if(!filename_canonical){
_mosquitto_free(filename);
if(errno == EACCES){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
}else if(errno == EINVAL || errno == EIO || errno == ELOOP){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
}else if(errno == ENAMETOOLONG){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_REQ_URI_TOO_LONG, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_REQ_URI_TOO_LONG, NULL);
}else if(errno == ENOENT || errno == ENOTDIR){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_NOT_FOUND, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
}
return -1;
}
@@ -432,7 +432,7 @@
/* Requested file isn't within http_dir, deny access. */
free(filename_canonical);
_mosquitto_free(filename);
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
return -1;
}
free(filename_canonical);
@@ -441,11 +441,11 @@
u->fptr = fopen(filename, "rb");
_mosquitto_free(filename);
if(!u->fptr){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_NOT_FOUND, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
return -1;
}
if(fstat(fileno(u->fptr), &filestat) < 0){
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
fclose(u->fptr);
return -1;
}
@@ -454,7 +454,7 @@
#else
if(!S_ISREG(filestat.st_mode)){
#endif
- libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
+ lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
return -1;
}
@@ -462,11 +462,11 @@
"Server: mosquitto\r\n"
"Content-Length: %u\r\n\r\n",
(unsigned int)filestat.st_size);
- if(libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP) < 0){
+ if(lws_write(wsi, buf, buflen, LWS_WRITE_HTTP) < 0){
fclose(u->fptr);
return -1;
}
- libwebsocket_callback_on_writable(context, wsi);
+ lws_callback_on_writable(wsi);
break;
#ifndef LWS_IS_OLD
@@ -491,7 +491,7 @@
fclose(u->fptr);
return -1;
}
- wlen = libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP);
+ wlen = lws_write(wsi, buf, buflen, LWS_WRITE_HTTP);
if(wlen < buflen){
if(fseek(u->fptr, buflen-wlen, SEEK_CUR) < 0){
fclose(u->fptr);
@@ -505,7 +505,7 @@
}
}
}while(u->fptr && !lws_send_pipe_choked(wsi));
- libwebsocket_callback_on_writable(context, wsi);
+ lws_callback_on_writable(wsi);
}else{
return -1;
}
@@ -525,10 +525,10 @@
_mosquitto_log_printf(NULL, MOSQ_LOG_WEBSOCKETS, "%s", l);
}
-struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener, int log_level)
+struct lws_context *mosq_websockets_init(struct _mqtt3_listener *listener, int log_level)
{
struct lws_context_creation_info info;
- struct libwebsocket_protocols *p;
+ struct lws_protocols *p;
int protocol_count;
int i;
struct libws_mqtt_hack *user;
@@ -536,7 +536,7 @@
/* Count valid protocols */
for(protocol_count=0; protocols[protocol_count].name; protocol_count++);
- p = _mosquitto_calloc(protocol_count+1, sizeof(struct libwebsocket_protocols));
+ p = _mosquitto_calloc(protocol_count+1, sizeof(struct lws_protocols));
if(!p){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Out of memory.");
return NULL;
@@ -593,7 +593,7 @@
lws_set_log_level(log_level, log_wrap);
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Opening websockets listen socket on port %d.", listener->port);
- return libwebsocket_create_context(&info);
+ return lws_create_context(&info);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment