Simplify broker context sock init.

pull/2215/head
Roger Light 4 years ago
parent e82cee161f
commit 245b138aa5

@ -75,7 +75,7 @@ static struct mosquitto *bridge__new(struct mosquitto__bridge *bridge)
mosquitto__free(local_id);
}else{
/* id wasn't found, so generate a new context */
new_context = context__init(INVALID_SOCKET);
new_context = context__init();
if(!new_context){
mosquitto__free(local_id);
return NULL;

@ -32,11 +32,32 @@ Contributors:
#include "uthash.h"
struct mosquitto *context__init(mosq_sock_t sock)
int context__init_sock(struct mosquitto *context, mosq_sock_t sock)
{
struct mosquitto *context;
char address[1024];
context->sock = sock;
if((int)context->sock >= 0){
if(!net__socket_get_address(context->sock,
address, sizeof(address),
&context->remote_port)){
context->address = mosquitto__strdup(address);
}
if(!context->address){
/* getpeername and inet_ntop failed and not a bridge */
return MOSQ_ERR_NOMEM;
}
HASH_ADD(hh_sock, db.contexts_by_sock, sock, sizeof(context->sock), context);
}
return MOSQ_ERR_SUCCESS;
}
struct mosquitto *context__init(void)
{
struct mosquitto *context;
context = mosquitto__calloc(1, sizeof(struct mosquitto));
if(!context) return NULL;
@ -46,7 +67,7 @@ struct mosquitto *context__init(mosq_sock_t sock)
context->pollfd_index = -1;
#endif
mosquitto__set_state(context, mosq_cs_new);
context->sock = sock;
context->sock = INVALID_SOCKET;
context->last_msg_in = db.now_s;
context->next_msg_out = db.now_s + 60;
context->keepalive = 60; /* Default to 60s */
@ -71,16 +92,6 @@ struct mosquitto *context__init(mosq_sock_t sock)
context->current_out_packet = NULL;
context->address = NULL;
if((int)sock >= 0){
if(!net__socket_get_address(sock, address, 1024, &context->remote_port)){
context->address = mosquitto__strdup(address);
}
if(!context->address){
/* getpeername and inet_ntop failed and not a bridge */
mosquitto__free(context);
return NULL;
}
}
context->bridge = NULL;
context->msgs_in.inflight_maximum = db.config->max_inflight_messages;
context->msgs_out.inflight_maximum = db.config->max_inflight_messages;
@ -91,9 +102,6 @@ struct mosquitto *context__init(mosq_sock_t sock)
context->ssl = NULL;
#endif
if((int)context->sock >= 0){
HASH_ADD(hh_sock, db.contexts_by_sock, sock, sizeof(context->sock), context);
}
return context;
}

@ -53,8 +53,6 @@ Contributors:
#define MQTT3_LOG_DLT 0x20
#define MQTT3_LOG_ALL 0xFF
#define WEBSOCKET_CLIENT -2
#define CMD_PORT_LIMIT 10
#define TOPIC_HIERARCHY_LIMIT 200
@ -701,7 +699,8 @@ void sub__topic_tokens_free(struct sub__token *tokens);
/* ============================================================
* Context functions
* ============================================================ */
struct mosquitto *context__init(mosq_sock_t sock);
struct mosquitto *context__init(void);
int context__init_sock(struct mosquitto *context, mosq_sock_t sock);
void context__cleanup(struct mosquitto *context, bool force_free);
void context__disconnect(struct mosquitto *context);
void context__add_to_disused(struct mosquitto *context);

@ -182,11 +182,17 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock
}
}
new_context = context__init(new_sock);
new_context = context__init();
if(!new_context){
COMPAT_CLOSE(new_sock);
return NULL;
}
if(context__init_sock(new_context, new_sock) != MOSQ_ERR_SUCCESS){
context__cleanup(new_context, true);
COMPAT_CLOSE(new_sock);
return NULL;
}
new_context->listener = listensock->listener;
if(!new_context->listener){
context__cleanup(new_context, true);

@ -54,7 +54,7 @@ static struct mosquitto *persist__find_or_add_context(const char *client_id, uin
context = NULL;
HASH_FIND(hh_id, db.contexts_by_id, client_id, strlen(client_id), context);
if(!context){
context = context__init(INVALID_SOCKET);
context = context__init();
if(!context) return NULL;
context->id = mosquitto__strdup(client_id);
if(!context->id){

@ -137,7 +137,7 @@ static int callback_mqtt(
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
mosq = context__init(WEBSOCKET_CLIENT);
mosq = context__init();
if(mosq){
p = lws_get_protocol(wsi);
mosq->listener = p->user;

Loading…
Cancel
Save