Skip to content

Commit

Permalink
Add sock hash earlier to avoid crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Jun 23, 2014
1 parent 0fc0e13 commit 3577dbf
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge)
/* (possible from persistent db) */
}else{
/* id wasn't found, so generate a new context */
new_context = mqtt3_context_init(-1);
new_context = mqtt3_context_init(db, -1);
if(!new_context){
return MOSQ_ERR_NOMEM;
}
Expand Down
5 changes: 4 additions & 1 deletion src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ and the Eclipse Distribution License is available at

#include "uthash.h"

struct mosquitto *mqtt3_context_init(int sock)
struct mosquitto *mqtt3_context_init(struct mosquitto_db *db, int sock)
{
struct mosquitto *context;
char address[1024];
Expand Down Expand Up @@ -76,6 +76,9 @@ struct mosquitto *mqtt3_context_init(int sock)
context->ssl = NULL;
#endif

if(context->sock != INVALID_SOCKET){
HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context);
}
return context;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mosquitto_broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ int mqtt3_subs_clean_session(struct mosquitto_db *db, struct mosquitto *context,
/* ============================================================
* Context functions
* ============================================================ */
struct mosquitto *mqtt3_context_init(int sock);
struct mosquitto *mqtt3_context_init(struct mosquitto_db *db, int sock);
void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free);
void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context);

Expand Down
3 changes: 1 addition & 2 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int mqtt3_socket_accept(struct mosquitto_db *db, int listensock)
return -1;
}
#endif
new_context = mqtt3_context_init(new_sock);
new_context = mqtt3_context_init(db, new_sock);
if(!new_context){
COMPAT_CLOSE(new_sock);
return -1;
Expand Down Expand Up @@ -171,7 +171,6 @@ int mqtt3_socket_accept(struct mosquitto_db *db, int listensock)
#endif

_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "New connection from %s on port %d.", new_context->address, new_context->listener->port);
HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(new_context->sock), new_context);

return new_sock;
}
Expand Down
2 changes: 1 addition & 1 deletion src/persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static struct mosquitto *_db_find_or_add_context(struct mosquitto_db *db, const
context = NULL;
HASH_FIND(hh_id, db->contexts_by_id, client_id, strlen(client_id), context);
if(!context){
context = mqtt3_context_init(-1);
context = mqtt3_context_init(db, -1);
if(!context) return NULL;
context->id = _mosquitto_strdup(client_id);
if(!context){
Expand Down
2 changes: 1 addition & 1 deletion src/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static int callback_mqtt(struct libwebsocket_context *context,

switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
mosq = mqtt3_context_init(-1);
mosq = mqtt3_context_init(db, -1);
if(mosq){
mosq->ws_context = context;
mosq->wsi = wsi;
Expand Down

0 comments on commit 3577dbf

Please sign in to comment.