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()