Final remove support for legacy libwebsockets

This means libwebsockets < 2.4.0.
pull/1920/head
Roger A. Light 5 years ago
parent 730fc34539
commit b34dcd2a67

@ -45,6 +45,7 @@ Breaking changes:
- The mosquitto_sub, mosquitto_pub, and mosquitto_rr clients will now load - The mosquitto_sub, mosquitto_pub, and mosquitto_rr clients will now load
OS provided CA certificates by default if `-L mqtts://...` is used, or if OS provided CA certificates by default if `-L mqtts://...` is used, or if
the port is set to 8883 and no other CA certificates are loaded. the port is set to 8883 and no other CA certificates are loaded.
- Minimum support libwebsockets version is now 2.4.0
Broker: Broker:

@ -291,12 +291,7 @@ struct mosquitto {
int shared_sub_count; int shared_sub_count;
int pollfd_index; int pollfd_index;
# ifdef WITH_WEBSOCKETS # ifdef WITH_WEBSOCKETS
# if defined(LWS_LIBRARY_VERSION_NUMBER)
struct lws *wsi; struct lws *wsi;
# else
struct libwebsocket_context *ws_context;
struct libwebsocket *wsi;
# endif
# endif # endif
bool ws_want_write; bool ws_want_write;
bool assigned_id; bool assigned_id;

@ -229,7 +229,7 @@ int net__socket_close(struct mosquitto *mosq)
if(mosq->state != mosq_cs_disconnecting){ if(mosq->state != mosq_cs_disconnecting){
mosquitto__set_state(mosq, mosq_cs_disconnect_ws); mosquitto__set_state(mosq, mosq_cs_disconnect_ws);
} }
libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi); lws_callback_on_writable(mosq->wsi);
}else }else
#endif #endif
{ {

@ -69,7 +69,7 @@ int packet__alloc(struct mosquitto__packet *packet)
if(packet->remaining_count == 5) return MOSQ_ERR_PAYLOAD_SIZE; if(packet->remaining_count == 5) return MOSQ_ERR_PAYLOAD_SIZE;
packet->packet_length = packet->remaining_length + 1 + (uint8_t)packet->remaining_count; packet->packet_length = packet->remaining_length + 1 + (uint8_t)packet->remaining_count;
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length + LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING); packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length + LWS_PRE);
#else #else
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length); packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
#endif #endif
@ -159,7 +159,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet)
#ifdef WITH_BROKER #ifdef WITH_BROKER
# ifdef WITH_WEBSOCKETS # ifdef WITH_WEBSOCKETS
if(mosq->wsi){ if(mosq->wsi){
libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi); lws_callback_on_writable(mosq->wsi);
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
}else{ }else{
return packet__write(mosq); return packet__write(mosq);

@ -2145,16 +2145,12 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct
#endif #endif
}else if(!strcmp(token, "websockets_headers_size")){ }else if(!strcmp(token, "websockets_headers_size")){
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
# if defined(LWS_LIBRARY_VERSION_NUMBER) && LWS_LIBRARY_VERSION_NUMBER>=1007000
if(conf__parse_int(&token, "websockets_headers_size", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; if(conf__parse_int(&token, "websockets_headers_size", &tmp_int, saveptr)) return MOSQ_ERR_INVAL;
if(tmp_int < 0 || tmp_int > UINT16_MAX){ if(tmp_int < 0 || tmp_int > UINT16_MAX){
log__printf(NULL, MOSQ_LOG_WARNING, "Error: Websockets headers size must be between 0 and 65535 inclusive."); log__printf(NULL, MOSQ_LOG_WARNING, "Error: Websockets headers size must be between 0 and 65535 inclusive.");
return MOSQ_ERR_INVAL; return MOSQ_ERR_INVAL;
} }
config->websockets_headers_size = (uint16_t)tmp_int; config->websockets_headers_size = (uint16_t)tmp_int;
# else
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Websockets headers size require libwebsocket 1.7+");
# endif
#else #else
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Websockets support not available."); log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Websockets support not available.");
#endif #endif

@ -225,12 +225,12 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens
* citizens. */ * citizens. */
if(db.config->listeners[i].ws_context){ if(db.config->listeners[i].ws_context){
#if LWS_LIBRARY_VERSION_NUMBER > 3002000 #if LWS_LIBRARY_VERSION_NUMBER > 3002000
libwebsocket_service(db.config->listeners[i].ws_context, -1); lws_service(db.config->listeners[i].ws_context, -1);
#elif LWS_LIBRARY_VERSION_NUMBER == 3002000 #elif LWS_LIBRARY_VERSION_NUMBER == 3002000
lws_sul_schedule(db.config->listeners[i].ws_context, 0, &sul, lws__sul_callback, 10); lws_sul_schedule(db.config->listeners[i].ws_context, 0, &sul, lws__sul_callback, 10);
libwebsocket_service(db.config->listeners[i].ws_context, 0); lws_service(db.config->listeners[i].ws_context, 0);
#else #else
libwebsocket_service(db.config->listeners[i].ws_context, 0); lws_service(db.config->listeners[i].ws_context, 0);
#endif #endif
} }
@ -264,7 +264,7 @@ void do_disconnect(struct mosquitto *context, int reason)
mosquitto__set_state(context, mosq_cs_disconnect_ws); mosquitto__set_state(context, mosq_cs_disconnect_ws);
} }
if(context->wsi){ if(context->wsi){
libwebsocket_callback_on_writable(context->ws_context, context->wsi); lws_callback_on_writable(context->wsi);
} }
if(context->sock != INVALID_SOCKET){ if(context->sock != INVALID_SOCKET){
HASH_DELETE(hh_sock, db.contexts_by_sock, context); HASH_DELETE(hh_sock, db.contexts_by_sock, context);

@ -344,7 +344,7 @@ void listeners__stop(struct mosquitto__listener_sock *listensock, int listensock
for(i=0; i<db.config->listener_count; i++){ for(i=0; i<db.config->listener_count; i++){
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
if(db.config->listeners[i].ws_context){ if(db.config->listeners[i].ws_context){
libwebsocket_context_destroy(db.config->listeners[i].ws_context); lws_context_destroy(db.config->listeners[i].ws_context);
} }
mosquitto__free(db.config->listeners[i].ws_protocol); mosquitto__free(db.config->listeners[i].ws_protocol);
#endif #endif

@ -23,25 +23,6 @@ Contributors:
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
# include <libwebsockets.h> # include <libwebsockets.h>
# if defined(LWS_LIBRARY_VERSION_NUMBER)
# define libwebsocket_callback_on_writable(A, B) lws_callback_on_writable((B))
# define libwebsocket_service(A, B) lws_service((A), (B))
# define libwebsocket_create_context(A) lws_create_context((A))
# define libwebsocket_context_destroy(A) lws_context_destroy((A))
# define libwebsocket_write(A, B, C, D) lws_write((A), (B), (C), (D))
# define libwebsocket_get_socket_fd(A) lws_get_socket_fd((A))
# define libwebsockets_return_http_status(A, B, C, D) lws_return_http_status((B), (C), (D))
# define libwebsockets_get_protocol(A) lws_get_protocol((A))
# define libwebsocket_context lws_context
# define libwebsocket_protocols lws_protocols
# define libwebsocket_callback_reasons lws_callback_reasons
# define libwebsocket lws
# else
# define lws_pollfd pollfd
# define lws_service_fd(A, B) libwebsocket_service_fd((A), (B))
# define lws_pollargs libwebsocket_pollargs
# endif
#endif #endif
#include "mosquitto_internal.h" #include "mosquitto_internal.h"
@ -240,9 +221,9 @@ struct mosquitto__listener {
enum mosquitto__keyform tls_keyform; enum mosquitto__keyform tls_keyform;
#endif #endif
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
struct libwebsocket_context *ws_context; struct lws_context *ws_context;
char *http_dir; char *http_dir;
struct libwebsocket_protocols *ws_protocol; struct lws_protocols *ws_protocol;
#endif #endif
struct mosquitto__security_options security_options; struct mosquitto__security_options security_options;
#ifdef WITH_UNIX_SOCKETS #ifdef WITH_UNIX_SOCKETS
@ -841,11 +822,7 @@ DWORD WINAPI SigThreadProc(void* data);
* Websockets related functions * Websockets related functions
* ============================================================ */ * ============================================================ */
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
# if defined(LWS_LIBRARY_VERSION_NUMBER)
struct lws_context *mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf); struct lws_context *mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf);
# else
struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf);
# endif
#endif #endif
void do_disconnect(struct mosquitto *context, int reason); void do_disconnect(struct mosquitto *context, int reason);

@ -246,11 +246,7 @@ static void loop_handle_reads_writes(struct mosquitto *context, uint32_t events)
wspoll.fd = context->sock; wspoll.fd = context->sock;
wspoll.events = (int16_t)context->events; wspoll.events = (int16_t)context->events;
wspoll.revents = (int16_t)events; wspoll.revents = (int16_t)events;
#ifdef LWS_LIBRARY_VERSION_NUMBER
lws_service_fd(lws_get_context(context->wsi), &wspoll); lws_service_fd(lws_get_context(context->wsi), &wspoll);
#else
lws_service_fd(context->ws_context, &wspoll);
#endif
return; return;
} }
#endif #endif

@ -251,11 +251,7 @@ static void loop_handle_reads_writes(struct pollfd *pollfds)
wspoll.fd = pollfds[context->pollfd_index].fd; wspoll.fd = pollfds[context->pollfd_index].fd;
wspoll.events = pollfds[context->pollfd_index].events; wspoll.events = pollfds[context->pollfd_index].events;
wspoll.revents = pollfds[context->pollfd_index].revents; wspoll.revents = pollfds[context->pollfd_index].revents;
#ifdef LWS_LIBRARY_VERSION_NUMBER
lws_service_fd(lws_get_context(context->wsi), &wspoll); lws_service_fd(lws_get_context(context->wsi), &wspoll);
#else
lws_service_fd(context->ws_context, &wspoll);
#endif
continue; continue;
} }
#endif #endif

@ -54,24 +54,16 @@ POSSIBILITY OF SUCH DAMAGE.
#define WS_SERV_BUF_SIZE 4096 #define WS_SERV_BUF_SIZE 4096
#define WS_TX_BUF_SIZE (WS_SERV_BUF_SIZE*2) #define WS_TX_BUF_SIZE (WS_SERV_BUF_SIZE*2)
#if defined(LWS_LIBRARY_VERSION_NUMBER)
static int callback_mqtt( static int callback_mqtt(
#else struct lws *wsi,
static int callback_mqtt(struct libwebsocket_context *context, enum lws_callback_reasons reason,
#endif
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *user,
void *in, void *in,
size_t len); size_t len);
#if defined(LWS_LIBRARY_VERSION_NUMBER)
static int callback_http( static int callback_http(
#else struct lws *wsi,
static int callback_http(struct libwebsocket_context *context, enum lws_callback_reasons reason,
#endif
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *user,
void *in, void *in,
size_t len); size_t len);
@ -86,79 +78,43 @@ struct libws_http_data {
FILE *fptr; FILE *fptr;
}; };
static struct libwebsocket_protocols protocols[] = { static struct lws_protocols protocols[] = {
/* first protocol must always be HTTP handler */ /* first protocol must always be HTTP handler */
{ {
"http-only", /* name */ "http-only", /* name */
callback_http, /* lws_callback_function */ callback_http, /* lws_callback_function */
sizeof (struct libws_http_data), /* per_session_data_size */ sizeof (struct libws_http_data), /* per_session_data_size */
0, /* rx_buffer_size */ 0, /* rx_buffer_size */
#ifndef LWS_LIBRARY_VERSION_NUMBER
0, /* no_buffer_all_partial_tx v1.3 only */
#endif
#ifdef LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
0, /* id */ 0, /* id */
#endif
#ifdef LWS_LIBRARY_VERSION_NUMBER
NULL, /* user v1.4 on */ NULL, /* user v1.4 on */
# if LWS_LIBRARY_VERSION_NUMBER >= 2003000
WS_TX_BUF_SIZE /* tx_packet_size v2.3.0 */ WS_TX_BUF_SIZE /* tx_packet_size v2.3.0 */
# endif
#endif
}, },
{ {
"mqtt", "mqtt",
callback_mqtt, callback_mqtt,
sizeof(struct libws_mqtt_data), sizeof(struct libws_mqtt_data),
0, /* rx_buffer_size */ 0, /* rx_buffer_size */
#ifndef LWS_LIBRARY_VERSION_NUMBER
0, /* no_buffer_all_partial_tx v1.3 only */
#endif
#ifdef LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
1, /* id */ 1, /* id */
#endif
#ifdef LWS_LIBRARY_VERSION_NUMBER
NULL, /* user v1.4 on */ NULL, /* user v1.4 on */
# if LWS_LIBRARY_VERSION_NUMBER >= 2003000
WS_TX_BUF_SIZE /* tx_packet_size v2.3.0 */ WS_TX_BUF_SIZE /* tx_packet_size v2.3.0 */
# endif
#endif
}, },
{ {
"mqttv3.1", "mqttv3.1",
callback_mqtt, callback_mqtt,
sizeof(struct libws_mqtt_data), sizeof(struct libws_mqtt_data),
0, /* rx_buffer_size */ 0, /* rx_buffer_size */
#ifndef LWS_LIBRARY_VERSION_NUMBER
0, /* no_buffer_all_partial_tx v1.3 only */
#endif
#ifdef LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
2, /* id */ 2, /* id */
#endif
#ifdef LWS_LIBRARY_VERSION_NUMBER
NULL, /* user v1.4 on */ NULL, /* user v1.4 on */
# if LWS_LIBRARY_VERSION_NUMBER >= 2003000
WS_TX_BUF_SIZE /* tx_packet_size v2.3.0 */ WS_TX_BUF_SIZE /* tx_packet_size v2.3.0 */
# endif
#endif
}, },
{ {
NULL, NULL,
NULL, NULL,
0, 0,
0, /* rx_buffer_size */ 0, /* rx_buffer_size */
#ifndef LWS_LIBRARY_VERSION_NUMBER
0, /* no_buffer_all_partial_tx v1.3 only */
#endif
#ifdef LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
0, /* id */ 0, /* id */
#endif
#ifdef LWS_LIBRARY_VERSION_NUMBER
NULL, /* user v1.4 on */ NULL, /* user v1.4 on */
# if LWS_LIBRARY_VERSION_NUMBER >= 2003000
0 /* tx_packet_size v2.3.0 */ 0 /* tx_packet_size v2.3.0 */
# endif
#endif
} }
}; };
@ -171,13 +127,9 @@ static void easy_address(int sock, struct mosquitto *mosq)
} }
} }
#if defined(LWS_LIBRARY_VERSION_NUMBER)
static int callback_mqtt( static int callback_mqtt(
#else struct lws *wsi,
static int callback_mqtt(struct libwebsocket_context *context, enum lws_callback_reasons reason,
#endif
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *user,
void *in, void *in,
size_t len) size_t len)
@ -187,7 +139,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
size_t txlen; size_t txlen;
int count; int count;
unsigned int ucount; unsigned int ucount;
const struct libwebsocket_protocols *p; const struct lws_protocols *p;
struct libws_mqtt_data *u = (struct libws_mqtt_data *)user; struct libws_mqtt_data *u = (struct libws_mqtt_data *)user;
size_t pos; size_t pos;
uint8_t *buf; uint8_t *buf;
@ -198,15 +150,12 @@ static int callback_mqtt(struct libwebsocket_context *context,
case LWS_CALLBACK_ESTABLISHED: case LWS_CALLBACK_ESTABLISHED:
mosq = context__init(WEBSOCKET_CLIENT); mosq = context__init(WEBSOCKET_CLIENT);
if(mosq){ if(mosq){
p = libwebsockets_get_protocol(wsi); p = lws_get_protocol(wsi);
mosq->listener = p->user; mosq->listener = p->user;
if(!mosq->listener){ if(!mosq->listener){
mosquitto__free(mosq); mosquitto__free(mosq);
return -1; return -1;
} }
#if !defined(LWS_LIBRARY_VERSION_NUMBER)
mosq->ws_context = context;
#endif
mosq->wsi = wsi; mosq->wsi = wsi;
#ifdef WITH_TLS #ifdef WITH_TLS
if(in){ if(in){
@ -220,7 +169,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
}else{ }else{
return -1; return -1;
} }
easy_address(libwebsocket_get_socket_fd(wsi), mosq); easy_address(lws_get_socket_fd(wsi), mosq);
if(!mosq->address){ if(!mosq->address){
/* getpeername and inet_ntop failed and not a bridge */ /* getpeername and inet_ntop failed and not a bridge */
mosquitto__free(mosq); mosquitto__free(mosq);
@ -236,7 +185,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
u->mosq = NULL; u->mosq = NULL;
return -1; return -1;
} }
mosq->sock = libwebsocket_get_socket_fd(wsi); mosq->sock = lws_get_socket_fd(wsi);
HASH_ADD(hh_sock, db.contexts_by_sock, sock, sizeof(mosq->sock), mosq); HASH_ADD(hh_sock, db.contexts_by_sock, sock, sizeof(mosq->sock), mosq);
mux__add_in(mosq); mux__add_in(mosq);
break; break;
@ -289,19 +238,19 @@ static int callback_mqtt(struct libwebsocket_context *context,
if(packet->pos == 0 && packet->to_process == packet->packet_length){ if(packet->pos == 0 && packet->to_process == packet->packet_length){
/* First time this packet has been dealt with. /* First time this packet has been dealt with.
* libwebsockets requires that the payload has * libwebsockets requires that the payload has
* LWS_SEND_BUFFER_PRE_PADDING space available before the * LWS_PRE space available before the
* actual data and LWS_SEND_BUFFER_POST_PADDING afterwards. * actual data.
* We've already made the payload big enough to allow this, * We've already made the payload big enough to allow this,
* but need to move it into position here. */ * but need to move it into position here. */
memmove(&packet->payload[LWS_SEND_BUFFER_PRE_PADDING], packet->payload, packet->packet_length); memmove(&packet->payload[LWS_PRE], packet->payload, packet->packet_length);
packet->pos += LWS_SEND_BUFFER_PRE_PADDING; packet->pos += LWS_PRE;
} }
if(packet->to_process > WS_TX_BUF_SIZE){ if(packet->to_process > WS_TX_BUF_SIZE){
txlen = WS_TX_BUF_SIZE; txlen = WS_TX_BUF_SIZE;
}else{ }else{
txlen = packet->to_process; txlen = packet->to_process;
} }
count = libwebsocket_write(wsi, &packet->payload[packet->pos], txlen, LWS_WRITE_BINARY); count = lws_write(wsi, &packet->payload[packet->pos], txlen, LWS_WRITE_BINARY);
if(count < 0){ if(count < 0){
if (mosq->state == mosq_cs_disconnect_ws if (mosq->state == mosq_cs_disconnect_ws
|| mosq->state == mosq_cs_disconnecting || mosq->state == mosq_cs_disconnecting
@ -355,7 +304,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
return -1; return -1;
} }
if(mosq->current_out_packet){ if(mosq->current_out_packet){
libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi); lws_callback_on_writable(mosq->wsi);
} }
break; break;
@ -438,7 +387,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
if(mosq->state != mosq_cs_disconnecting){ if(mosq->state != mosq_cs_disconnecting){
mosquitto__set_state(mosq, mosq_cs_disconnect_ws); mosquitto__set_state(mosq, mosq_cs_disconnect_ws);
} }
libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi); lws_callback_on_writable(mosq->wsi);
} else if (rc) { } else if (rc) {
do_disconnect(mosq, MOSQ_ERR_CONN_LOST); do_disconnect(mosq, MOSQ_ERR_CONN_LOST);
return -1; return -1;
@ -455,10 +404,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
static char *http__canonical_filename( static char *http__canonical_filename(
#ifndef LWS_LIBRARY_VERSION_NUMBER struct lws *wsi,
struct libwebsocket_context *context,
#endif
struct libwebsocket *wsi,
const char *in, const char *in,
const char *http_dir) const char *http_dir)
{ {
@ -473,7 +419,7 @@ static char *http__canonical_filename(
} }
filename = mosquitto__malloc(slen); filename = mosquitto__malloc(slen);
if(!filename){ 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 NULL; return NULL;
} }
if(((char *)in)[inlen-1] == '/'){ if(((char *)in)[inlen-1] == '/'){
@ -488,7 +434,7 @@ static char *http__canonical_filename(
filename_canonical = _fullpath(NULL, filename, 0); filename_canonical = _fullpath(NULL, filename, 0);
mosquitto__free(filename); mosquitto__free(filename);
if(!filename_canonical){ if(!filename_canonical){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL); lws_return_http_status(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return NULL; return NULL;
} }
#else #else
@ -496,13 +442,13 @@ static char *http__canonical_filename(
mosquitto__free(filename); mosquitto__free(filename);
if(!filename_canonical){ if(!filename_canonical){
if(errno == EACCES){ 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){ }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){ }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){ }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 NULL; return NULL;
} }
@ -510,7 +456,7 @@ static char *http__canonical_filename(
if(strncmp(http_dir, filename_canonical, strlen(http_dir))){ if(strncmp(http_dir, filename_canonical, strlen(http_dir))){
/* Requested file isn't within http_dir, deny access. */ /* Requested file isn't within http_dir, deny access. */
free(filename_canonical); free(filename_canonical);
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL); lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
return NULL; return NULL;
} }
@ -518,13 +464,9 @@ static char *http__canonical_filename(
} }
#if defined(LWS_LIBRARY_VERSION_NUMBER)
static int callback_http( static int callback_http(
#else struct lws *wsi,
static int callback_http(struct libwebsocket_context *context, enum lws_callback_reasons reason,
#endif
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *user,
void *in, void *in,
size_t len) size_t len)
@ -549,11 +491,7 @@ static int callback_http(struct libwebsocket_context *context,
return -1; return -1;
} }
#if defined(LWS_LIBRARY_VERSION_NUMBER)
hack = (struct libws_mqtt_hack *)lws_context_user(lws_get_context(wsi)); hack = (struct libws_mqtt_hack *)lws_context_user(lws_get_context(wsi));
#else
hack = (struct libws_mqtt_hack *)libwebsocket_context_user(context);
#endif
if(!hack){ if(!hack){
return -1; return -1;
} }
@ -566,26 +504,22 @@ static int callback_http(struct libwebsocket_context *context,
/* Forbid POST */ /* Forbid POST */
if(lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)){ 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; return -1;
} }
#if defined(LWS_LIBRARY_VERSION_NUMBER)
filename_canonical = http__canonical_filename(wsi, (char *)in, http_dir); filename_canonical = http__canonical_filename(wsi, (char *)in, http_dir);
#else
filename_canonical = http__canonical_filename(context, wsi, (char *)in, http_dir);
#endif
if(!filename_canonical) return -1; if(!filename_canonical) return -1;
u->fptr = fopen(filename_canonical, "rb"); u->fptr = fopen(filename_canonical, "rb");
if(!u->fptr){ if(!u->fptr){
free(filename_canonical); free(filename_canonical);
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_NOT_FOUND, NULL); lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
return -1; return -1;
} }
if(fstat(fileno(u->fptr), &filestat) < 0){ if(fstat(fileno(u->fptr), &filestat) < 0){
free(filename_canonical); free(filename_canonical);
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); fclose(u->fptr);
u->fptr = NULL; u->fptr = NULL;
return -1; return -1;
@ -601,11 +535,11 @@ static int callback_http(struct libwebsocket_context *context,
buflen = (size_t)snprintf((char *)buf, 4096, "HTTP/1.0 302 OK\r\n" buflen = (size_t)snprintf((char *)buf, 4096, "HTTP/1.0 302 OK\r\n"
"Location: %s/\r\n\r\n", "Location: %s/\r\n\r\n",
(char *)in); (char *)in);
return libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP); return lws_write(wsi, buf, buflen, LWS_WRITE_HTTP);
} }
if((filestat.st_mode & S_IFREG) != S_IFREG){ if((filestat.st_mode & S_IFREG) != S_IFREG){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL); lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
fclose(u->fptr); fclose(u->fptr);
u->fptr = NULL; u->fptr = NULL;
free(filename_canonical); free(filename_canonical);
@ -619,12 +553,12 @@ static int callback_http(struct libwebsocket_context *context,
"Server: mosquitto\r\n" "Server: mosquitto\r\n"
"Content-Length: %u\r\n\r\n", "Content-Length: %u\r\n\r\n",
(unsigned int)filestat.st_size); (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); fclose(u->fptr);
u->fptr = NULL; u->fptr = NULL;
return -1; return -1;
} }
libwebsocket_callback_on_writable(context, wsi); lws_callback_on_writable(wsi);
break; break;
case LWS_CALLBACK_HTTP_BODY: case LWS_CALLBACK_HTTP_BODY:
@ -649,7 +583,7 @@ static int callback_http(struct libwebsocket_context *context,
u->fptr = NULL; u->fptr = NULL;
return -1; return -1;
} }
rc = libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP); rc = lws_write(wsi, buf, buflen, LWS_WRITE_HTTP);
if(rc < 0){ if(rc < 0){
return -1; return -1;
} }
@ -667,7 +601,7 @@ static int callback_http(struct libwebsocket_context *context,
} }
} }
}while(u->fptr && !lws_send_pipe_choked(wsi)); }while(u->fptr && !lws_send_pipe_choked(wsi));
libwebsocket_callback_on_writable(context, wsi); lws_callback_on_writable(wsi);
}else{ }else{
return -1; return -1;
} }
@ -718,10 +652,10 @@ static void log_wrap(int level, const char *line)
log__printf(NULL, MOSQ_LOG_WEBSOCKETS, "%s", l); log__printf(NULL, MOSQ_LOG_WEBSOCKETS, "%s", l);
} }
struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf) struct lws_context *mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf)
{ {
struct lws_context_creation_info info; struct lws_context_creation_info info;
struct libwebsocket_protocols *p; struct lws_protocols *p;
size_t protocol_count; size_t protocol_count;
int i; int i;
struct libws_mqtt_hack *user; struct libws_mqtt_hack *user;
@ -729,7 +663,7 @@ struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *li
/* Count valid protocols */ /* Count valid protocols */
for(protocol_count=0; protocols[protocol_count].name; protocol_count++); 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){ if(!p){
log__printf(NULL, MOSQ_LOG_ERR, "Out of memory."); log__printf(NULL, MOSQ_LOG_ERR, "Out of memory.");
return NULL; return NULL;
@ -754,22 +688,18 @@ struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *li
info.ssl_private_key_filepath = listener->keyfile; info.ssl_private_key_filepath = listener->keyfile;
info.ssl_cipher_list = listener->ciphers; info.ssl_cipher_list = listener->ciphers;
#if defined(WITH_WEBSOCKETS) && LWS_LIBRARY_VERSION_NUMBER>=3001000 #if defined(WITH_WEBSOCKETS) && LWS_LIBRARY_VERSION_NUMBER>=3001000
info->tls_1_3_plus_cipher_list = listener->ciphers_tls13; info.tls1_3_plus_cipher_list = listener->ciphers_tls13;
#endif #endif
if(listener->require_certificate){ if(listener->require_certificate){
info.options |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT; info.options |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
} }
#endif #endif
#if LWS_LIBRARY_VERSION_MAJOR>1
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
#endif
if(listener->socket_domain == AF_INET){ if(listener->socket_domain == AF_INET){
info.options |= LWS_SERVER_OPTION_DISABLE_IPV6; info.options |= LWS_SERVER_OPTION_DISABLE_IPV6;
} }
#if defined(LWS_LIBRARY_VERSION_NUMBER) && LWS_LIBRARY_VERSION_NUMBER>=1007000
info.max_http_header_data = conf->websockets_headers_size; info.max_http_header_data = conf->websockets_headers_size;
#endif
user = mosquitto__calloc(1, sizeof(struct libws_mqtt_hack)); user = mosquitto__calloc(1, sizeof(struct libws_mqtt_hack));
if(!user){ if(!user){
@ -793,15 +723,13 @@ struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *li
} }
info.user = user; info.user = user;
#if defined(LWS_LIBRARY_VERSION_NUMBER) && LWS_LIBRARY_VERSION_NUMBER>=2004000
info.pt_serv_buf_size = WS_SERV_BUF_SIZE; info.pt_serv_buf_size = WS_SERV_BUF_SIZE;
#endif
listener->ws_protocol = p; listener->ws_protocol = p;
lws_set_log_level(conf->websockets_log_level, log_wrap); lws_set_log_level(conf->websockets_log_level, log_wrap);
log__printf(NULL, MOSQ_LOG_INFO, "Opening websockets listen socket on port %d.", listener->port); 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);
} }

Loading…
Cancel
Save