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, { 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; } 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/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 c54bf02..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; @@ -80,6 +82,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 +97,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 +110,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 +140,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 { @@ -144,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); @@ -162,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); @@ -188,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 @@ -206,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..."); @@ -214,7 +214,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; } @@ -234,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) { @@ -290,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) { @@ -342,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) { @@ -387,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); @@ -397,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); @@ -408,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 @@ -427,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 @@ -446,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 6f85270..be4f27d 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; @@ -54,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 @@ -63,8 +63,15 @@ 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); + +/** + * 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); /** @@ -93,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() 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))) 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); } 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--; }