diff --git a/autotest/test_inf.tcc b/autotest/test_inf.tcc index 42dc73f..d270820 100644 --- a/autotest/test_inf.tcc +++ b/autotest/test_inf.tcc @@ -42,8 +42,9 @@ static void inf_create_user() { if (inf_user) return; inf_user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user)); + inf_user->connection = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection)); inf_user->id.sid = 1; - inf_user->net.connection.sd = -1; + inf_user->connection->sd = -1; inf_user->limits.upload_slots = 1; } diff --git a/autotest/test_usermanager.tcc b/autotest/test_usermanager.tcc index 22d3201..dc430fc 100644 --- a/autotest/test_usermanager.tcc +++ b/autotest/test_usermanager.tcc @@ -4,6 +4,7 @@ static struct hub_info um_hub; static struct hub_user um_user[MAX_USERS]; +static struct net_connection um_cons[MAX_USERS]; EXO_TEST(um_test_setup, { int i = 0; @@ -11,9 +12,12 @@ EXO_TEST(um_test_setup, { for (i = 0; i < MAX_USERS; i++) { + memset(&um_cons[i], 0, sizeof(struct net_connection)); + um_cons[i].sd = -1; + memset(&um_user[i], 0, sizeof(struct hub_user)); um_user[i].id.sid = i+1; - um_user[i].net.connection.sd = -1; + um_user[i].connection = &um_cons[i]; } return 1; }); diff --git a/src/core/auth.c b/src/core/auth.c index bf6c4c8..1d2c82d 100644 --- a/src/core/auth.c +++ b/src/core/auth.c @@ -443,7 +443,7 @@ const char* acl_password_generate_challenge(struct acl_handle* acl, struct hub_u uint64_t tiger_res[3]; static char tiger_buf[MAX_CID_LEN+1]; - snprintf(buf, 32, "%d%d%d", (int) user->net.tm_connected, (int) user->id.sid, (int) user->net.connection.sd); + snprintf(buf, 32, "%d%d%d", (int) user->tm_connected, (int) user->id.sid, (int) user->connection->sd); tiger((uint64_t*) buf, strlen(buf), (uint64_t*) tiger_res); base32_encode((unsigned char*) tiger_res, TIGERSIZE, tiger_buf); diff --git a/src/core/commands.c b/src/core/commands.c index b238135..d10d065 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\"", ip_convert_to_string(&user->net.connection.ipaddr)); + snprintf(tmp, 128, "Your address is \"%s\"", net_con_get_peer_address(user->connection)); 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, ip_convert_to_string(&target->net.connection.ipaddr)); + snprintf(tmp, 128, "%s has address \"%s\"", nick, net_con_get_peer_address(user->connection)); 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, ip_convert_to_string(&u->net.connection.ipaddr)); + strcat(buffer, net_con_get_peer_address(u->connection)); strcat(buffer, ")\n"); u = (struct hub_user*) list_get_next(users); } diff --git a/src/core/hub.c b/src/core/hub.c index 4b300c9..0fb761a 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -140,7 +140,7 @@ int hub_handle_support(struct hub_info* hub, struct hub_user* u, struct adc_mess if (ok) { hub_send_handshake(hub, u); - net_con_set_timeout(&u->net.connection, TIMEOUT_HANDSHAKE); + net_con_set_timeout(u->connection, TIMEOUT_HANDSHAKE); } else { @@ -1018,8 +1018,8 @@ void hub_disconnect_user(struct hub_info* hub, struct hub_user* user, int reason } /* stop reading from user */ - net_shutdown_r(user->net.connection.sd); - net_con_close(&user->net.connection); + net_shutdown_r(user->connection->sd); + net_con_close(user->connection); LOG_TRACE("hub_disconnect_user(), user=%p, reason=%d, state=%d", user, reason, user->state); @@ -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->net.connection.ipaddr, sizeof(struct ip_addr_encap)); + memcpy(&loginfo->addr, &user->connection->ipaddr, 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 3b0c0bd..4ff44d4 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 = ip_convert_to_string(&u->net.connection.ipaddr); + const char* addr = net_con_get_peer_address(u->connection); 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 = ip_convert_to_string(&u->net.connection.ipaddr); + const char* addr = net_con_get_peer_address(u->connection); 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 = ip_convert_to_string(&u->net.connection.ipaddr); + const char* addr = net_con_get_peer_address(u->connection); 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 = ip_convert_to_string(&u->net.connection.ipaddr); + const char* addr = net_con_get_peer_address(u->connection); LOG_USER("NickChange %s/%s %s \"%s\" -> \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, nick); } @@ -69,7 +69,7 @@ void on_login_success(struct hub_info* hub, struct hub_user* u) hub_send_motd(hub, u); /* reset timeout */ - net_con_set_timeout(&u->net.connection, TIMEOUT_IDLE); + net_con_set_timeout(u->connection, TIMEOUT_IDLE); } void on_login_failure(struct hub_info* hub, struct hub_user* u, enum status_message msg) diff --git a/src/core/inf.c b/src/core/inf.c index b9218eb..d3d28b1 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 = ip_convert_to_string(&user->net.connection.ipaddr); + const char* address = net_con_get_peer_address(user->connection); /* 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 f9b212d..11dd999 100644 --- a/src/core/netevent.c +++ b/src/core/netevent.c @@ -55,7 +55,7 @@ void debug_sendq_recv(struct hub_user* user, int received, int max, const char* int net_user_send(void* ptr, const void* buf, size_t len) { struct hub_user* user = (struct hub_user*) ptr; - int ret = net_con_send(&user->net.connection, buf, len); + int ret = net_con_send(user->connection, buf, len); #ifdef DEBUG_SENDQ debug_sendq_send(user, ret, len); #endif @@ -70,7 +70,7 @@ int net_user_send(void* ptr, const void* buf, size_t len) int net_user_recv(void* ptr, void* buf, size_t len) { struct hub_user* user = (struct hub_user*) ptr; - int ret = net_con_recv(&user->net.connection, buf, len); + int ret = net_con_recv(user->connection, buf, len); #ifdef DEBUG_SENDQ debug_sendq_recv(user, ret, len, buf); #endif @@ -80,7 +80,7 @@ int net_user_recv(void* ptr, void* buf, size_t len) int handle_net_read(struct hub_user* user) { static char buf[MAX_RECV_BUF]; - struct hub_recvq* q = user->net.recv_queue; + struct hub_recvq* q = user->recv_queue; size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF); ssize_t size = net_user_recv(user, &buf[buf_size], MAX_RECV_BUF - buf_size); @@ -159,9 +159,9 @@ int handle_net_read(struct hub_user* user) int handle_net_write(struct hub_user* user) { int ret = 0; - while (hub_sendq_get_bytes(user->net.send_queue)) + while (hub_sendq_get_bytes(user->send_queue)) { - ret = hub_sendq_send(user->net.send_queue, net_user_send, user); + ret = hub_sendq_send(user->send_queue, net_user_send, user); if (ret <= 0) break; } @@ -171,7 +171,7 @@ int handle_net_write(struct hub_user* user) if (ret < 0) return quit_socket_error; - if (hub_sendq_get_bytes(user->net.send_queue)) + if (hub_sendq_get_bytes(user->send_queue)) { user_net_io_want_write(user); } diff --git a/src/core/route.c b/src/core/route.c index caea0a6..7923dd8 100644 --- a/src/core/route.c +++ b/src/core/route.c @@ -80,10 +80,10 @@ static inline int check_send_queue(struct hub_info* hub, struct hub_user* user, if (user_flag_get(user, flag_user_list)) return 1; - if ((user->net.send_queue->size + msg->length) > get_max_send_queue(hub)) + if ((user->send_queue->size + msg->length) > get_max_send_queue(hub)) return -1; - if (user->net.send_queue->size > get_max_send_queue_soft(hub) && msg->priority < 0) + if (user->send_queue->size > get_max_send_queue_soft(hub) && msg->priority < 0) return 0; return 1; @@ -97,17 +97,17 @@ int route_to_user(struct hub_info* hub, struct hub_user* user, struct adc_messag free(data); #endif - if (hub_sendq_is_empty(user->net.send_queue) && !user_flag_get(user, flag_pipeline)) + if (hub_sendq_is_empty(user->send_queue) && !user_flag_get(user, flag_pipeline)) { /* Perform oportunistic write */ - hub_sendq_add(user->net.send_queue, msg); + hub_sendq_add(user->send_queue, msg); handle_net_write(user); } else { if (check_send_queue(hub, user, msg) >= 0) { - hub_sendq_add(user->net.send_queue, msg); + hub_sendq_add(user->send_queue, msg); if (!user_flag_get(user, flag_pipeline)) user_net_io_want_write(user); } @@ -117,7 +117,7 @@ int route_to_user(struct hub_info* hub, struct hub_user* user, struct adc_messag int route_flush_pipeline(struct hub_info* hub, struct hub_user* u) { - if (hub_sendq_is_empty(u->net.send_queue)) + if (hub_sendq_is_empty(u->send_queue)) return 0; handle_net_write(u); @@ -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 = ip_convert_to_string(&u->net.connection.ipaddr); + const char* address = net_con_get_peer_address(u->connection); 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 8a433bb..cbc1624 100644 --- a/src/core/user.c +++ b/src/core/user.c @@ -46,12 +46,13 @@ struct hub_user* user_create(struct hub_info* hub, int sd, struct ip_addr_encap* if (user == NULL) return NULL; /* OOM */ - user->net.tm_connected = time(NULL); - user->net.send_queue = hub_sendq_create(); - user->net.recv_queue = hub_recvq_create(); + user->tm_connected = time(NULL); + user->send_queue = hub_sendq_create(); + user->recv_queue = hub_recvq_create(); - net_con_initialize(&user->net.connection, sd, addr, net_event, user, NET_EVENT_READ); - net_con_set_timeout(&user->net.connection, TIMEOUT_CONNECTED); + 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_set_timeout(user->connection, TIMEOUT_CONNECTED); user_set_state(user, state_protocol); return user; @@ -62,8 +63,8 @@ void user_destroy(struct hub_user* user) { LOG_TRACE("user_destroy(), user=%p", user); - hub_recvq_destroy(user->net.recv_queue); - hub_sendq_destroy(user->net.send_queue); + hub_recvq_destroy(user->recv_queue); + hub_sendq_destroy(user->send_queue); adc_msg_free(user->info); user_clear_feature_cast_support(user); @@ -314,12 +315,12 @@ int user_is_registered(struct hub_user* user) void user_net_io_want_write(struct hub_user* user) { - net_con_update(&user->net.connection, NET_EVENT_READ | NET_EVENT_WRITE); + net_con_update(user->connection, NET_EVENT_READ | NET_EVENT_WRITE); } void user_net_io_want_read(struct hub_user* user) { - net_con_update(&user->net.connection, NET_EVENT_READ); + net_con_update(user->connection, NET_EVENT_READ); } const char* user_get_quit_reason_string(enum user_quit_reason reason) diff --git a/src/core/user.h b/src/core/user.h index 602f7bb..0c22732 100644 --- a/src/core/user.h +++ b/src/core/user.h @@ -97,17 +97,8 @@ struct hub_user_limits size_t hub_count_total; /** The number of hubs connected to in total */ }; -struct hub_user_net_io -{ - struct net_connection connection; /** Connection data */ - struct hub_recvq* recv_queue; - struct hub_sendq* send_queue; - time_t tm_connected; /** time when user connected */ -}; - struct hub_user { - struct hub_user_net_io net; /** Network information data */ enum user_state state; /** see enum user_state */ enum user_credentials credentials; /** see enum user_credentials */ struct hub_user_info id; /** Contains nick name and CID */ @@ -116,6 +107,10 @@ struct hub_user struct linked_list* feature_cast; /** Features supported by feature cast */ struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */ struct hub_info* hub; /** The hub instance this user belong to */ + struct hub_recvq* recv_queue; + struct hub_sendq* send_queue; + struct net_connection* connection; /** Connection data */ + time_t tm_connected; /** time when user connected */ struct hub_user_limits limits; /** Data used for limitation */ enum user_quit_reason quit_reason; /** Quit reason (see user_quit_reason) */ diff --git a/src/core/usermanager.c b/src/core/usermanager.c index 2c7bf75..f4d7855 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->net.connection.ipaddr, range)) + if (ip_in_range(&user->connection->ipaddr, range)) { list_append(users, user); num++; diff --git a/src/network/connection.c b/src/network/connection.c index 951633b..70326c9 100644 --- a/src/network/connection.c +++ b/src/network/connection.c @@ -104,6 +104,11 @@ static void net_con_event(int fd, short ev, void *arg) #endif } +const char* net_con_get_peer_address(struct net_connection* con) +{ + return ip_convert_to_string(&con->ipaddr); +} + 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); diff --git a/src/network/connection.h b/src/network/connection.h index 1d022a1..754c69d 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -72,6 +72,12 @@ 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()