From 1e380ecf0ffb1463f7156153dac01e4fd394dbc5 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 31 Aug 2009 19:39:09 +0200 Subject: [PATCH 1/7] Fix bug #77 - Error in ACL command parsing. --- src/core/auth.c | 18 +++++------------- src/util/misc.c | 11 +++++++---- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/core/auth.c b/src/core/auth.c index 1d2c82d..9cb7660 100644 --- a/src/core/auth.c +++ b/src/core/auth.c @@ -179,26 +179,18 @@ static int acl_parse_line(char* line, int line_count, void* ptr_data) char* pos; struct acl_handle* handle = (struct acl_handle*) ptr_data; int ret; - + if ((pos = strchr(line, '#')) != NULL) { pos[0] = 0; } - + + line = strip_white_space(line); if (!*line) return 0; - LOG_DEBUG("acl_parse_line(): '%s'", line); - line = strip_white_space(line); - - if (!*line) - { - LOG_FATAL("ACL parse error on line %d", line_count); - return -1; - } - LOG_DEBUG("acl_parse_line: '%s'", line); - + ACL_ADD_USER("bot", handle->users, cred_bot); ACL_ADD_USER("user_admin", handle->users, cred_admin); ACL_ADD_USER("user_super", handle->users, cred_super); @@ -210,7 +202,7 @@ static int acl_parse_line(char* line, int line_count, void* ptr_data) ACL_ADD_BOOL("ban_cid", handle->cids); ACL_ADD_ADDR("deny_ip", handle->networks); ACL_ADD_ADDR("nat_ip", handle->nat_override); - + LOG_ERROR("Unknown ACL command on line %d: '%s'", line_count, line); return -1; } diff --git a/src/util/misc.c b/src/util/misc.c index 63ec011..5f88272 100644 --- a/src/util/misc.c +++ b/src/util/misc.c @@ -45,12 +45,15 @@ static int is_printable(unsigned char c) char* strip_white_space(char* string) { char* pos; - + + if (!string) + return ""; + while (string[0] && is_white_space(string[0])) string++; - + if (!*string) - return 0; - + return string; + /* Strip appending whitespace */ pos = &string[strlen(string)-1]; while (&string[0] < &pos[0] && is_white_space(pos[0])) { pos[0] = 0; pos--; } From 993d9ae33b03e1a9db3408682d917af3354fb784 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 31 Aug 2009 19:45:47 +0200 Subject: [PATCH 2/7] Fix some issues regarding header extraction. --- src/adc/message.c | 23 +++++++++++++- src/adc/message.h | 16 ++++++++++ src/tools/adcclient.c | 73 ++++++++++++++++++++----------------------- src/tools/adcclient.h | 2 +- 4 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/adc/message.c b/src/adc/message.c index e1b5dc0..c40b7dc 100644 --- a/src/adc/message.c +++ b/src/adc/message.c @@ -521,7 +521,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length) struct adc_message* adc_msg_create(const char* line) { - return adc_msg_parse_verify(NULL, line, strlen(line)); + return adc_msg_parse(line, strlen(line)); } @@ -737,6 +737,27 @@ int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], co return ret; } +int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix[2], const char* string) +{ + char* escaped = adc_msg_escape(string); + int ret = adc_msg_add_named_argument(cmd, prefix, escaped); + hub_free(escaped); + return ret; +} + +int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int num) +{ + char* s = uhub_itoa(num); + int ret = adc_msg_add_named_argument(cmd, prefix, s); + return ret; +} + +int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num) +{ + char* s = uhub_ulltoa(num); + int ret = adc_msg_add_named_argument(cmd, prefix, s); + return ret; +} int adc_msg_add_argument(struct adc_message* cmd, const char* string) { diff --git a/src/adc/message.h b/src/adc/message.h index a3a8876..72665e3 100644 --- a/src/adc/message.h +++ b/src/adc/message.h @@ -173,6 +173,22 @@ extern int adc_msg_add_argument(struct adc_message* cmd, const char* string); */ extern int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], const char* string); +/** + * Append a string as a named argument. + * The string will automatcally be escaped, if you do not wish to escape th string use adc_msg_add_named_argument() instead. + * + * @arg prefix a 2 character argument prefix + * @arg string must NOT be escaped + * @return 0 if successful, or -1 if an error occured (out of memory). + */ +extern int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix[2], const char* string); + +/** + * Append an integer as a named argument. + */ +extern int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int integer); +extern int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num); + /** * Convert a ADC command escaped string to a regular string. * @return string or NULL if out of memory diff --git a/src/tools/adcclient.c b/src/tools/adcclient.c index ac6d9fd..af6cb38 100644 --- a/src/tools/adcclient.c +++ b/src/tools/adcclient.c @@ -59,19 +59,16 @@ static void adc_cid_pid(struct ADC_client* client) /* create cid+pid pair */ memset(seed, 0, 64); snprintf(seed, 64, VERSION "%p", client); - + tiger((uint64_t*) seed, strlen(seed), tiger_res1); base32_encode((unsigned char*) tiger_res1, TIGERSIZE, pid); tiger((uint64_t*) tiger_res1, TIGERSIZE, tiger_res2); base32_encode((unsigned char*) tiger_res2, TIGERSIZE, cid); - cid[ADC_CID_SIZE] = 0; pid[ADC_CID_SIZE] = 0; - - strcat(client->info, " PD"); - strcat(client->info, pid); - strcat(client->info, " ID"); - strcat(client->info, cid); + + adc_msg_add_named_argument(client->info, ADC_INF_FLAG_PRIVATE_ID, pid); + adc_msg_add_named_argument(client->info, ADC_INF_FLAG_CLIENT_ID, cid); } @@ -118,14 +115,23 @@ static void event_callback(struct net_connection* con, int events, void *arg) } } -#define EXTRACT(MSG, NAME, TARGET) \ +#define UNESCAPE_ARG(TMP, TARGET) \ + if (TMP) \ + TARGET = adc_msg_unescape(TMP); \ + else \ + TARGET = NULL; \ + hub_free(TMP); + +#define EXTRACT_NAMED_ARG(MSG, NAME, TARGET) \ do { \ char* tmp = adc_msg_get_named_argument(MSG, NAME); \ - if (tmp) \ - TARGET = adc_msg_unescape(tmp); \ - else \ - TARGET = NULL; \ - hub_free(tmp); \ + UNESCAPE_ARG(tmp, TARGET); \ + } while (0) + +#define EXTRACT_POS_ARG(MSG, POS, TARGET) \ + do { \ + char* tmp = adc_msg_get_argument(MSG, POS); \ + UNESCAPE_ARG(tmp, TARGET); \ } while (0) @@ -170,13 +176,11 @@ static void ADC_client_on_recv_line(struct ADC_client* client, const char* line, case ADC_CMD_IMSG: { struct ADC_chat_message chat; + struct ADC_client_callback_data data; chat.from_sid = msg->source; chat.to_sid = msg->target; - char* message = adc_msg_get_argument(msg, 0); - chat.message = adc_msg_unescape(message); - hub_free(message); - struct ADC_client_callback_data data; data.chat = &chat; + EXTRACT_POS_ARG(msg, 0, chat.message); client->callback(client, ADC_CLIENT_MESSAGE, &data); hub_free(chat.message); break; @@ -185,9 +189,9 @@ static void ADC_client_on_recv_line(struct ADC_client* client, const char* line, case ADC_CMD_IINF: { struct ADC_hub_info hubinfo; - EXTRACT(msg, "NI", hubinfo.name); - EXTRACT(msg, "DE", hubinfo.description); - EXTRACT(msg, "VE", hubinfo.version); + EXTRACT_NAMED_ARG(msg, "NI", hubinfo.name); + EXTRACT_NAMED_ARG(msg, "DE", hubinfo.description); + EXTRACT_NAMED_ARG(msg, "VE", hubinfo.version); struct ADC_client_callback_data data; data.hubinfo = &hubinfo; @@ -299,30 +303,21 @@ void ADC_client_send(struct ADC_client* client, char* msg) void ADC_client_send_info(struct ADC_client* client) { - client->info[0] = 0; - strcat(client->info, "BINF "); - strcat(client->info, sid_to_string(client->sid)); - strcat(client->info, " NI"); - strcat(client->info, client->nick); /* FIXME: no escaping */ - strcat(client->info, "_"); - strcat(client->info, uhub_itoa(client->sid)); - strcat(client->info, " VE" VERSION); + char binf[11]; + snprintf(binf, 11, "BINF %s\n", sid_to_string(client->sid)); + client->info = adc_msg_create(binf); + + adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_NICK, client->nick); + if (client->desc) { - strcat(client->info, " DE"); - strcat(client->info, client->desc); /* FIXME: no escaping */ - + adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_DESCRIPTION, client->desc); } - strcat(client->info, " I40.0.0.0"); - strcat(client->info, " EMuhub@extatic.org"); - strcat(client->info, " SL3"); - strcat(client->info, " HN1"); - strcat(client->info, " HR1"); - strcat(client->info, " HO1"); + + adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_USER_AGENT, PRODUCT "/" VERSION); adc_cid_pid(client); - strcat(client->info, "\n"); - ADC_client_send(client, client->info); + ADC_client_send(client, client->info->cache); } int ADC_client_create(struct ADC_client* client, const char* nickname, const char* description) diff --git a/src/tools/adcclient.h b/src/tools/adcclient.h index d8de8c9..b9b21b0 100644 --- a/src/tools/adcclient.h +++ b/src/tools/adcclient.h @@ -92,7 +92,7 @@ struct ADC_client { sid_t sid; enum ADC_client_state state; - char info[ADC_BUFSIZE]; + struct adc_message* info; char recvbuf[ADC_BUFSIZE]; char sendbuf[ADC_BUFSIZE]; adc_client_cb callback; From 4fd182c55ec73f3ec7aeeec945f6f6d65fc84a6c Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 31 Aug 2009 20:36:25 +0200 Subject: [PATCH 3/7] Fix compile warning. --- src/adc/message.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adc/message.c b/src/adc/message.c index c40b7dc..5343123 100644 --- a/src/adc/message.c +++ b/src/adc/message.c @@ -747,14 +747,14 @@ int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int num) { - char* s = uhub_itoa(num); + const char* s = uhub_itoa(num); int ret = adc_msg_add_named_argument(cmd, prefix, s); return ret; } int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num) { - char* s = uhub_ulltoa(num); + const char* s = uhub_ulltoa(num); int ret = adc_msg_add_named_argument(cmd, prefix, s); return ret; } From a3d6646b9963687ce239c598f332d7afba252e73 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 31 Aug 2009 20:37:47 +0200 Subject: [PATCH 4/7] Fix a failing autotest. --- autotest/test_message.tcc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autotest/test_message.tcc b/autotest/test_message.tcc index 7569992..fef366c 100644 --- a/autotest/test_message.tcc +++ b/autotest/test_message.tcc @@ -31,7 +31,9 @@ EXO_TEST(adc_message_parse_1, { EXO_TEST(adc_message_parse_2, { struct adc_message* msg = adc_msg_create(test_string2); - return msg == NULL; + int ok = (msg != NULL); + adc_msg_free(msg); + return ok; }); EXO_TEST(adc_message_parse_3, { From 96ce64ee0797fe9df4246b13520b49e5fe21a40a Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Fri, 4 Sep 2009 17:18:38 +0200 Subject: [PATCH 5/7] Fix memory issues in ADC client lib. --- src/network/connection.c | 22 +++++++++++++--------- src/network/connection.h | 8 ++++++++ src/tools/adcclient.c | 17 +++++++++++++---- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/network/connection.c b/src/network/connection.c index c54bf02..e292d1c 100644 --- a/src/network/connection.c +++ b/src/network/connection.c @@ -80,6 +80,9 @@ void net_con_set(struct net_connection* con) net_con_flag_set(con, NET_INITIALIZED); } +#define CALLBACK(CON, EVENTS) \ + if (CON->callback) \ + CON->callback(con, EVENTS, CON->ptr); static void net_con_event(int fd, short ev, void *arg) { @@ -92,7 +95,7 @@ static void net_con_event(int fd, short ev, void *arg) if (!con->ssl) { #endif - con->callback(con, events, con->ptr); + CALLBACK(con, events); #ifdef SSL_SUPPORT } else @@ -105,29 +108,29 @@ static void net_con_event(int fd, short ev, void *arg) if (net_con_flag_get(con, NET_WANT_SSL_ACCEPT)) { if (net_con_ssl_accept(con) < 0) - con->callback(con, NET_EVENT_SOCKERROR, con->ptr); + CALLBACK(con, NET_EVENT_SOCKERROR); } else if (net_con_flag_get(con, NET_WANT_SSL_CONNECT)) { if (net_con_ssl_connect(con) < 0) - con->callback(con, NET_EVENT_SOCKERROR, con->ptr); + CALLBACK(con, NET_EVENT_SOCKERROR); } else if (ev == EV_READ && net_con_flag_get(con, NET_WANT_SSL_READ)) { - con->callback(con, NET_EVENT_WRITE, con->ptr); + CALLBACK(con, NET_EVENT_WRITE); } else if (ev == EV_WRITE && net_con_flag_get(con, NET_WANT_SSL_WRITE)) { - con->callback(con, events & NET_EVENT_READ, con->ptr); + CALLBACK(con, events & NET_EVENT_READ); } else { - con->callback(con, events, con->ptr); + CALLBACK((con, events); } } else { - con->callback(con, events, con->ptr); + CALLBACK(con, events); } } #endif @@ -135,8 +138,8 @@ static void net_con_event(int fd, short ev, void *arg) if (net_con_flag_get(con, NET_CLEANUP)) { - printf("SHOULD SCHEDULE SHUTTING DOWN SOCKET."); net_con_flag_unset(con, NET_INITIALIZED); + CALLBACK(con, NET_EVENT_DESTROYED); } else { @@ -214,7 +217,8 @@ void net_con_close(struct net_connection* con) if (net_con_flag_get(con, NET_PROCESSING_BUSY)) { - LOG_INFO("Trying to close socket while processing it, will need to post a message about it..."); + LOG_INFO("Trying to close socket while processing it"); + net_con_flag_set(con, NET_CLEANUP); return; } diff --git a/src/network/connection.h b/src/network/connection.h index 6f85270..40b58f0 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -27,6 +27,7 @@ #define NET_EVENT_WRITE 0x0004 #define NET_EVENT_SOCKERROR 0x1000 /* Socket error, closed */ #define NET_EVENT_CLOSED 0x2000 /* Socket closed */ +#define NET_EVENT_DESTROYED 0x4000 /* Struct is invalid and can be cleaned up */ struct net_connection; struct net_timer; @@ -65,6 +66,13 @@ struct net_connection extern void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap*, net_connection_cb callback, const void* ptr, int events); extern void net_con_update(struct net_connection* con, int events); + +/** + * Close the connection. + * This will ensure a connection is closed properly and will generate a NET_EVENT_DESTROYED event which indicates + * that the con can safely be deleted (or set to NULL) + * NOTE: Do not dele + */ extern void net_con_close(struct net_connection* con); /** diff --git a/src/tools/adcclient.c b/src/tools/adcclient.c index af6cb38..b3d5977 100644 --- a/src/tools/adcclient.c +++ b/src/tools/adcclient.c @@ -80,8 +80,18 @@ static void timer_callback(struct net_timer* t, void* arg) static void event_callback(struct net_connection* con, int events, void *arg) { struct ADC_client* client = (struct ADC_client*) arg; + + if (events == NET_EVENT_DESTROYED) + { + printf("NET_EVENT_DESTROYED\n"); + hub_free(client->con); + client->con = 0; + return; + } + if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED) { + printf("NET_EVENT_SOCKERROR || NET_EVENT_CLOSED\n"); client->callback(client, ADC_CLIENT_DISCONNECTED, 0); return; } @@ -343,12 +353,13 @@ void ADC_client_destroy(struct ADC_client* client) { ADC_client_disconnect(client); net_timer_shutdown(client->timer); + hub_free(client->timer); + adc_msg_free(client->info); hub_free(client->nick); hub_free(client->desc); hub_free(client->hub_address); } - int ADC_client_connect(struct ADC_client* client, const char* address) { if (!client->hub_address) @@ -391,8 +402,6 @@ static void ADC_client_on_connected(struct ADC_client* client) static void ADC_client_on_disconnected(struct ADC_client* client) { net_con_close(client->con); - hub_free(client->con); - client->con = 0; ADC_client_set_state(client, ps_none); } @@ -404,7 +413,7 @@ static void ADC_client_on_login(struct ADC_client* client) void ADC_client_disconnect(struct ADC_client* client) { - if (client->con->sd != -1) + if (client->con && client->con->sd != -1) { net_con_close(client->con); } From a53c219f05b7b4c0beb19abb2508bd375865245a Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 14 Sep 2009 12:24:18 +0200 Subject: [PATCH 6/7] Fix bug #82 - Crash writing a log larger than 2GB on a 32 bit system as _FILE_OFFSET_BITS was not declared to be 64. --- src/system.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/system.h b/src/system.h index ee3fe29..93c155c 100644 --- a/src/system.h +++ b/src/system.h @@ -20,6 +20,8 @@ #ifndef HAVE_UHUB_SYSTEM_H #define HAVE_UHUB_SYSTEM_H +#define _FILE_OFFSET_BITS 64 + #if USE_REGPARM && __GNUC__ >= 3 #define REGPRM1 __attribute__((regparm(1))) #define REGPRM2 __attribute__((regparm(2))) From 69f6d811bf308a57a1c36cd452fd30ad182cb199 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 28 Sep 2009 23:15:15 +0200 Subject: [PATCH 7/7] Added lots of asserts, and split out the ip address from the connection object. Still a lot of work remains and a massive network connection cleanup is needed. --- src/core/commands.c | 6 +++--- src/core/hub.c | 2 +- src/core/hubevent.c | 8 ++++---- src/core/inf.c | 2 +- src/core/netevent.c | 8 ++++++++ src/core/route.c | 2 +- src/core/user.c | 9 +++++++-- src/core/user.h | 3 +++ src/core/usermanager.c | 2 +- src/network/connection.c | 35 ++++++++++++++++++++++++----------- src/network/connection.h | 9 +-------- 11 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/core/commands.c b/src/core/commands.c index 11ab60a..59fde80 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -290,7 +290,7 @@ static int command_version(struct hub_info* hub, struct hub_user* user, struct h static int command_myip(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd) { char tmp[128]; - snprintf(tmp, 128, "Your address is \"%s\"", net_con_get_peer_address(user->connection)); + snprintf(tmp, 128, "Your address is \"%s\"", user_get_address(user)); return command_status(hub, user, cmd, tmp); } @@ -304,7 +304,7 @@ static int command_getip(struct hub_info* hub, struct hub_user* user, struct hub if (!target) return command_status_user_not_found(hub, user, cmd, nick); - snprintf(tmp, 128, "%s has address \"%s\"", nick, net_con_get_peer_address(user->connection)); + snprintf(tmp, 128, "%s has address \"%s\"", nick, user_get_address(user)); return command_status(hub, user, cmd, tmp); } @@ -342,7 +342,7 @@ static int command_whoip(struct hub_info* hub, struct hub_user* user, struct hub { strcat(buffer, u->id.nick); strcat(buffer, " ("); - strcat(buffer, net_con_get_peer_address(u->connection)); + strcat(buffer, user_get_address(u)); strcat(buffer, ")\n"); u = (struct hub_user*) list_get_next(users); } diff --git a/src/core/hub.c b/src/core/hub.c index 0fb761a..902ab7c 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -1047,7 +1047,7 @@ void hub_logout_log(struct hub_info* hub, struct hub_user* user) loginfo->time = time(NULL); strcpy(loginfo->cid, user->id.cid); strcpy(loginfo->nick, user->id.nick); - memcpy(&loginfo->addr, &user->connection->ipaddr, sizeof(struct ip_addr_encap)); + memcpy(&loginfo->addr, &user->id.addr, sizeof(struct ip_addr_encap)); loginfo->reason = user->quit_reason; list_append(hub->logout_info, loginfo); diff --git a/src/core/hubevent.c b/src/core/hubevent.c index 4ff44d4..4078b45 100644 --- a/src/core/hubevent.c +++ b/src/core/hubevent.c @@ -22,26 +22,26 @@ static void log_user_login(struct hub_user* u) { const char* cred = get_user_credential_string(u->credentials); - const char* addr = net_con_get_peer_address(u->connection); + const char* addr = user_get_address(u); LOG_USER("LoginOK %s/%s %s \"%s\" (%s) \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, cred, u->user_agent); } static void log_user_login_error(struct hub_user* u, enum status_message msg) { - const char* addr = net_con_get_peer_address(u->connection); + const char* addr = user_get_address(u); const char* message = hub_get_status_message_log(u->hub, msg); LOG_USER("LoginError %s/%s %s \"%s\" (%s) \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, message, u->user_agent); } static void log_user_logout(struct hub_user* u, const char* message) { - const char* addr = net_con_get_peer_address(u->connection); + const char* addr = user_get_address(u); LOG_USER("Logout %s/%s %s \"%s\" (%s)", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, message); } static void log_user_nick_change(struct hub_user* u, const char* nick) { - const char* addr = net_con_get_peer_address(u->connection); + const char* addr = user_get_address(u); LOG_USER("NickChange %s/%s %s \"%s\" -> \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, nick); } diff --git a/src/core/inf.c b/src/core/inf.c index d3d28b1..7c2142d 100644 --- a/src/core/inf.c +++ b/src/core/inf.c @@ -186,7 +186,7 @@ static int check_required_login_flags(struct hub_info* hub, struct hub_user* use */ int check_network(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd) { - const char* address = net_con_get_peer_address(user->connection); + const char* address = user_get_address(user); /* Check for NAT override address */ if (acl_is_ip_nat_override(hub->acl, address)) diff --git a/src/core/netevent.c b/src/core/netevent.c index 11dd999..24eba8d 100644 --- a/src/core/netevent.c +++ b/src/core/netevent.c @@ -187,6 +187,14 @@ void net_event(struct net_connection* con, int event, void *arg) struct hub_user* user = (struct hub_user*) arg; int flag_close = 0; + if (event == NET_EVENT_DESTROYED) + { + printf("NET_EVENT_DESTROYED\n"); + hub_free(user->connection); + user->connection = 0; + return; + } + #ifdef DEBUG_SENDQ LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", fd, (int) event, arg); #endif diff --git a/src/core/route.c b/src/core/route.c index 7923dd8..c9c366b 100644 --- a/src/core/route.c +++ b/src/core/route.c @@ -197,7 +197,7 @@ int route_info_message(struct hub_info* hub, struct hub_user* u) else { struct adc_message* cmd = adc_msg_copy(u->info); - const char* address = net_con_get_peer_address(u->connection); + const char* address = user_get_address(u); struct hub_user* user = 0; adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR); diff --git a/src/core/user.c b/src/core/user.c index cbc1624..8e92185 100644 --- a/src/core/user.c +++ b/src/core/user.c @@ -51,9 +51,11 @@ struct hub_user* user_create(struct hub_info* hub, int sd, struct ip_addr_encap* user->recv_queue = hub_recvq_create(); user->connection = (struct net_connection*) hub_malloc(sizeof(struct net_connection)); - net_con_initialize(user->connection, sd, addr, net_event, user, NET_EVENT_READ); + net_con_initialize(user->connection, sd, net_event, user, NET_EVENT_READ); net_con_set_timeout(user->connection, TIMEOUT_CONNECTED); + memcpy(&user->id.addr, addr, sizeof(struct ip_addr_encap)); + user_set_state(user, state_protocol); return user; } @@ -344,4 +346,7 @@ const char* user_get_quit_reason_string(enum user_quit_reason reason) return "unknown"; } - +const char* user_get_address(struct hub_user* user) +{ + return ip_convert_to_string(&user->id.addr); +} diff --git a/src/core/user.h b/src/core/user.h index 0c22732..236b70e 100644 --- a/src/core/user.h +++ b/src/core/user.h @@ -79,6 +79,7 @@ struct hub_user_info sid_t sid; /** session ID */ char cid[MAX_CID_LEN+1]; /** global client ID */ char nick[MAX_NICK_LEN+1]; /** User's nick name */ + struct ip_addr_encap addr; /** User's IP address */ }; /** @@ -209,6 +210,8 @@ extern void user_support_add(struct hub_user* user, int fourcc); */ extern void user_support_remove(struct hub_user* user, int fourcc); +extern const char* user_get_address(struct hub_user* user); + /** * Sets the nat override flag for a user, this allows users on the same * subnet as a natted hub to spoof their IP in order to use active mode diff --git a/src/core/usermanager.c b/src/core/usermanager.c index 4e7f952..63e206f 100644 --- a/src/core/usermanager.c +++ b/src/core/usermanager.c @@ -221,7 +221,7 @@ size_t uman_get_user_by_addr(struct hub_info* hub, struct linked_list* users, st struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */ while (user) { - if (ip_in_range(&user->connection->ipaddr, range)) + if (ip_in_range(&user->id.addr, range)) { list_append(users, user); num++; diff --git a/src/network/connection.c b/src/network/connection.c index e292d1c..3108f69 100644 --- a/src/network/connection.c +++ b/src/network/connection.c @@ -71,6 +71,8 @@ static void net_con_event(int fd, short ev, void *arg); void net_con_set(struct net_connection* con) { + uhub_assert(con); + int ev = 0; if (net_con_flag_get(con, NET_WANT_READ | NET_WANT_SSL_READ)) ev |= EV_READ; if (net_con_flag_get(con, NET_WANT_WRITE | NET_WANT_SSL_WRITE)) ev |= EV_WRITE; @@ -147,13 +149,10 @@ static void net_con_event(int fd, short ev, void *arg) } } -const char* net_con_get_peer_address(struct net_connection* con) +void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int ev) { - return ip_convert_to_string(&con->ipaddr); -} + uhub_assert(con); -void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap* addr, net_connection_cb callback, const void* ptr, int ev) -{ int events = net_con_convert_to_libevent_mask(ev); if (ev & NET_EVENT_READ) net_con_flag_set(con, NET_WANT_READ); if (ev & NET_EVENT_WRITE) net_con_flag_set(con, NET_WANT_WRITE); @@ -165,12 +164,6 @@ void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap con->last_send = time(0); con->last_recv = con->last_send; - /** IP address of peer */ - if (addr) - { - memcpy(&con->ipaddr, addr, sizeof(struct ip_addr_encap)); - } - if (ev) { event_set(&con->event, con->sd, events, net_con_event, con); @@ -191,6 +184,8 @@ void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap void net_con_update(struct net_connection* con, int ev) { + uhub_assert(con); + if (ev & NET_EVENT_READ) net_con_flag_set(con, NET_EVENT_READ); else @@ -209,6 +204,8 @@ void net_con_update(struct net_connection* con, int ev) void net_con_close(struct net_connection* con) { + uhub_assert(con); + if (net_con_flag_get(con, NET_CLEANUP)) { LOG_INFO("Running net_con_close, but we already have closed..."); @@ -238,6 +235,8 @@ void net_con_close(struct net_connection* con) #ifdef SSL_SUPPORT static int handle_openssl_error(struct net_connection* con, int ret) { + uhub_assert(con); + int error = SSL_get_error(con->ssl, ret); switch (error) { @@ -294,6 +293,8 @@ static int handle_openssl_error(struct net_connection* con, int ret) ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len) { + uhub_assert(con); + #ifdef SSL_SUPPORT if (!con->ssl) { @@ -346,6 +347,8 @@ ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len) ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len) { + uhub_assert(con); + #ifdef SSL_SUPPORT if (!con->ssl) { @@ -391,6 +394,8 @@ ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len) void net_con_set_timeout(struct net_connection* con, int seconds) { + uhub_assert(con); + struct timeval timeout = { seconds, 0 }; net_con_clear_timeout(con); @@ -401,6 +406,8 @@ void net_con_set_timeout(struct net_connection* con, int seconds) void net_con_clear_timeout(struct net_connection* con) { + uhub_assert(con); + if (net_con_flag_get(con, NET_TIMER_ENABLED)) { evtimer_del(&con->timeout); @@ -412,6 +419,8 @@ void net_con_clear_timeout(struct net_connection* con) #ifdef SSL_SUPPORT ssize_t net_con_ssl_accept(struct net_connection* con) { + uhub_assert(con); + net_con_flag_set(con, NET_WANT_SSL_ACCEPT); ssize_t ret = SSL_accept(con->ssl); #ifdef NETWORK_DUMP_DEBUG @@ -431,6 +440,8 @@ ssize_t net_con_ssl_accept(struct net_connection* con) ssize_t net_con_ssl_connect(struct net_connection* con) { + uhub_assert(con); + net_con_flag_set(con, NET_WANT_SSL_CONNECT); ssize_t ret = SSL_connect(con->ssl); #ifdef NETWORK_DUMP_DEBUG @@ -450,6 +461,8 @@ ssize_t net_con_ssl_connect(struct net_connection* con) ssize_t net_con_ssl_handshake(struct net_connection* con, int ssl_mode) { + uhub_assert(con); + if (ssl_mode == NET_CON_SSL_MODE_SERVER) { con->ssl = SSL_new(g_hub->ssl_ctx); diff --git a/src/network/connection.h b/src/network/connection.h index 40b58f0..be4f27d 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -55,7 +55,6 @@ struct net_connection net_connection_cb callback; /** Callback function */ struct event event; /** libevent struct for read/write events */ struct event timeout; /** Used for internal timeout handling */ - struct ip_addr_encap ipaddr; /** IP address of peer */ time_t last_recv; /** Timestamp for last recv() */ time_t last_send; /** Timestamp for last send() */ #ifdef SSL_SUPPORT @@ -64,7 +63,7 @@ struct net_connection #endif /* SSL_SUPPORT */ }; -extern void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap*, net_connection_cb callback, const void* ptr, int events); +extern void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int events); extern void net_con_update(struct net_connection* con, int events); /** @@ -101,12 +100,6 @@ extern ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len); extern void net_con_set_timeout(struct net_connection* con, int seconds); extern void net_con_clear_timeout(struct net_connection* con); -/** - * Returns a string representation of the ipaddr member. - * NOTE: Static buffer. - */ -extern const char* net_con_get_peer_address(struct net_connection* con); - #ifdef SSL_SUPPORT /** * Start SSL_accept()