Skip to content

Commit

Permalink
Extend socket activation to the web sockets
Browse files Browse the repository at this point in the history
Disable socket creation during lws_create_context().
Use the generic mosquitto socket() functionality and
hand over an accept()ed websocket file descriptor via
lws_adopt_socket()

Signed-off-by: Christian Hohnstaedt <[email protected]>
  • Loading branch information
chris2511 committed Dec 17, 2021
1 parent fc1365b commit 4c7cd33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,23 +347,22 @@ static int listeners__start(void)
}

for(i=0; i<db.config->listener_count; i++){
if(db.config->listeners[i].protocol == mp_mqtt){
if(listeners__start_single_mqtt(&db.config->listeners[i])){
db__close();
if(db.config->pid_file){
(void)remove(db.config->pid_file);
}
return 1;
if(listeners__start_single_mqtt(&db.config->listeners[i])){
db__close();
if(db.config->pid_file){
(void)remove(db.config->pid_file);
}
}else if(db.config->listeners[i].protocol == mp_websockets){
return 1;
}
#ifdef WITH_WEBSOCKETS
if(db.config->listeners[i].protocol == mp_websockets){
mosq_websockets_init(&db.config->listeners[i], db.config);
if(!db.config->listeners[i].ws_context){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to create websockets listener on port %d.", db.config->listeners[i].port);
return 1;
}
#endif
}
#endif
}
if(listensock == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to start any listening sockets, exiting.");
Expand Down
24 changes: 17 additions & 7 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#include "net_mosq.h"
#include "util_mosq.h"

#ifdef WITH_WEBSOCKETS
# include <libwebsockets.h>
#endif
#ifdef WITH_TLS
#include "tls_mosq.h"
#include <openssl/err.h>
Expand Down Expand Up @@ -148,6 +151,12 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock
}
return NULL;
}
#if WITH_WEBSOCKETS
if(listensock->listener->ws_context){
lws_adopt_socket(listensock->listener->ws_context, new_sock);
return NULL;
}
#endif

G_SOCKET_CONNECTIONS_INC();

Expand Down Expand Up @@ -710,13 +719,12 @@ static int net__socket_listen_tcp(struct mosquitto__listener *listener)
listener->socks = NULL;

for(rp = ainfo; rp; rp = rp->ai_next){
if(rp->ai_family == AF_INET){
log__printf(NULL, MOSQ_LOG_INFO, "Opening ipv4 listen socket on port %d.", ntohs(((struct sockaddr_in *)rp->ai_addr)->sin_port));
}else if(rp->ai_family == AF_INET6){
log__printf(NULL, MOSQ_LOG_INFO, "Opening ipv6 listen socket on port %d.", ntohs(((struct sockaddr_in6 *)rp->ai_addr)->sin6_port));
}else{
const char *sname = listener->protocol == mp_websockets ?
"web" : "";
int proto = rp->ai_family == AF_INET ? 4 : 6;
if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6)
continue;
}
log__printf(NULL, MOSQ_LOG_INFO, "Opening ipv%d listen %ssocket on port %d.", proto, sname, ntohs(((struct sockaddr_in6 *)rp->ai_addr)->sin6_port));

sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock == INVALID_SOCKET){
Expand Down Expand Up @@ -912,7 +920,9 @@ static int set_svc_mgr_fd(struct mosquitto__listener *listener, int sock)
listener->socks[listener->sock_count-1] = sock;
if (listener->port)
log__printf(NULL, MOSQ_LOG_NOTICE,
"IP socket for %s:%u provided by service manager",
"%s for %s:%u provided by service manager",
listener->protocol == mp_websockets ?
"WebSocket" : "IP socket",
listener->host, listener->port);
#ifdef WITH_UNIX_SOCKETS
else
Expand Down
4 changes: 2 additions & 2 deletions src/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ void mosq_websockets_init(struct mosquitto__listener *listener, const struct mos

memset(&info, 0, sizeof(info));
info.iface = listener->host;
info.port = listener->port;
info.port = CONTEXT_PORT_NO_LISTEN_SERVER;
info.protocols = p;
info.gid = -1;
info.uid = -1;
Expand Down Expand Up @@ -738,7 +738,7 @@ void mosq_websockets_init(struct mosquitto__listener *listener, const struct mos

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, "Initializing websockets context");
listener->ws_in_init = true;
listener->ws_context = lws_create_context(&info);
listener->ws_in_init = false;
Expand Down

0 comments on commit 4c7cd33

Please sign in to comment.