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.
This commit is contained in:
Jan Vidar Krey 2009-09-28 23:15:15 +02:00
parent a53c219f05
commit 69f6d811bf
11 changed files with 54 additions and 32 deletions

View File

@ -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) static int command_myip(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
{ {
char tmp[128]; 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); 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) if (!target)
return command_status_user_not_found(hub, user, cmd, nick); 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); 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, u->id.nick);
strcat(buffer, " ("); strcat(buffer, " (");
strcat(buffer, net_con_get_peer_address(u->connection)); strcat(buffer, user_get_address(u));
strcat(buffer, ")\n"); strcat(buffer, ")\n");
u = (struct hub_user*) list_get_next(users); u = (struct hub_user*) list_get_next(users);
} }

View File

@ -1047,7 +1047,7 @@ void hub_logout_log(struct hub_info* hub, struct hub_user* user)
loginfo->time = time(NULL); loginfo->time = time(NULL);
strcpy(loginfo->cid, user->id.cid); strcpy(loginfo->cid, user->id.cid);
strcpy(loginfo->nick, user->id.nick); 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; loginfo->reason = user->quit_reason;
list_append(hub->logout_info, loginfo); list_append(hub->logout_info, loginfo);

View File

@ -22,26 +22,26 @@
static void log_user_login(struct hub_user* u) static void log_user_login(struct hub_user* u)
{ {
const char* cred = get_user_credential_string(u->credentials); 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); 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) 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); 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); 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) 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); 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) 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); LOG_USER("NickChange %s/%s %s \"%s\" -> \"%s\"", sid_to_string(u->id.sid), u->id.cid, addr, u->id.nick, nick);
} }

View File

@ -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) 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 */ /* Check for NAT override address */
if (acl_is_ip_nat_override(hub->acl, address)) if (acl_is_ip_nat_override(hub->acl, address))

View File

@ -187,6 +187,14 @@ void net_event(struct net_connection* con, int event, void *arg)
struct hub_user* user = (struct hub_user*) arg; struct hub_user* user = (struct hub_user*) arg;
int flag_close = 0; 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 #ifdef DEBUG_SENDQ
LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", fd, (int) event, arg); LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", fd, (int) event, arg);
#endif #endif

View File

@ -197,7 +197,7 @@ int route_info_message(struct hub_info* hub, struct hub_user* u)
else else
{ {
struct adc_message* cmd = adc_msg_copy(u->info); 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; struct hub_user* user = 0;
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR); adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);

View File

@ -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->recv_queue = hub_recvq_create();
user->connection = (struct net_connection*) hub_malloc(sizeof(struct net_connection)); 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); net_con_set_timeout(user->connection, TIMEOUT_CONNECTED);
memcpy(&user->id.addr, addr, sizeof(struct ip_addr_encap));
user_set_state(user, state_protocol); user_set_state(user, state_protocol);
return user; return user;
} }
@ -344,4 +346,7 @@ const char* user_get_quit_reason_string(enum user_quit_reason reason)
return "unknown"; return "unknown";
} }
const char* user_get_address(struct hub_user* user)
{
return ip_convert_to_string(&user->id.addr);
}

View File

@ -79,6 +79,7 @@ struct hub_user_info
sid_t sid; /** session ID */ sid_t sid; /** session ID */
char cid[MAX_CID_LEN+1]; /** global client ID */ char cid[MAX_CID_LEN+1]; /** global client ID */
char nick[MAX_NICK_LEN+1]; /** User's nick name */ 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 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 * 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 * subnet as a natted hub to spoof their IP in order to use active mode

View File

@ -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 */ struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */
while (user) while (user)
{ {
if (ip_in_range(&user->connection->ipaddr, range)) if (ip_in_range(&user->id.addr, range))
{ {
list_append(users, user); list_append(users, user);
num++; num++;

View File

@ -71,6 +71,8 @@ static void net_con_event(int fd, short ev, void *arg);
void net_con_set(struct net_connection* con) void net_con_set(struct net_connection* con)
{ {
uhub_assert(con);
int ev = 0; 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_READ | NET_WANT_SSL_READ)) ev |= EV_READ;
if (net_con_flag_get(con, NET_WANT_WRITE | NET_WANT_SSL_WRITE)) ev |= EV_WRITE; 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); 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_READ) net_con_flag_set(con, NET_WANT_READ);
if (ev & NET_EVENT_WRITE) net_con_flag_set(con, NET_WANT_WRITE); 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_send = time(0);
con->last_recv = con->last_send; con->last_recv = con->last_send;
/** IP address of peer */
if (addr)
{
memcpy(&con->ipaddr, addr, sizeof(struct ip_addr_encap));
}
if (ev) if (ev)
{ {
event_set(&con->event, con->sd, events, net_con_event, con); 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) void net_con_update(struct net_connection* con, int ev)
{ {
uhub_assert(con);
if (ev & NET_EVENT_READ) if (ev & NET_EVENT_READ)
net_con_flag_set(con, NET_EVENT_READ); net_con_flag_set(con, NET_EVENT_READ);
else else
@ -209,6 +204,8 @@ void net_con_update(struct net_connection* con, int ev)
void net_con_close(struct net_connection* con) void net_con_close(struct net_connection* con)
{ {
uhub_assert(con);
if (net_con_flag_get(con, NET_CLEANUP)) if (net_con_flag_get(con, NET_CLEANUP))
{ {
LOG_INFO("Running net_con_close, but we already have closed..."); 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 #ifdef SSL_SUPPORT
static int handle_openssl_error(struct net_connection* con, int ret) static int handle_openssl_error(struct net_connection* con, int ret)
{ {
uhub_assert(con);
int error = SSL_get_error(con->ssl, ret); int error = SSL_get_error(con->ssl, ret);
switch (error) 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) ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len)
{ {
uhub_assert(con);
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
if (!con->ssl) 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) ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
{ {
uhub_assert(con);
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
if (!con->ssl) 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) void net_con_set_timeout(struct net_connection* con, int seconds)
{ {
uhub_assert(con);
struct timeval timeout = { seconds, 0 }; struct timeval timeout = { seconds, 0 };
net_con_clear_timeout(con); 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) void net_con_clear_timeout(struct net_connection* con)
{ {
uhub_assert(con);
if (net_con_flag_get(con, NET_TIMER_ENABLED)) if (net_con_flag_get(con, NET_TIMER_ENABLED))
{ {
evtimer_del(&con->timeout); evtimer_del(&con->timeout);
@ -412,6 +419,8 @@ void net_con_clear_timeout(struct net_connection* con)
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
ssize_t net_con_ssl_accept(struct net_connection* con) ssize_t net_con_ssl_accept(struct net_connection* con)
{ {
uhub_assert(con);
net_con_flag_set(con, NET_WANT_SSL_ACCEPT); net_con_flag_set(con, NET_WANT_SSL_ACCEPT);
ssize_t ret = SSL_accept(con->ssl); ssize_t ret = SSL_accept(con->ssl);
#ifdef NETWORK_DUMP_DEBUG #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) ssize_t net_con_ssl_connect(struct net_connection* con)
{ {
uhub_assert(con);
net_con_flag_set(con, NET_WANT_SSL_CONNECT); net_con_flag_set(con, NET_WANT_SSL_CONNECT);
ssize_t ret = SSL_connect(con->ssl); ssize_t ret = SSL_connect(con->ssl);
#ifdef NETWORK_DUMP_DEBUG #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) ssize_t net_con_ssl_handshake(struct net_connection* con, int ssl_mode)
{ {
uhub_assert(con);
if (ssl_mode == NET_CON_SSL_MODE_SERVER) if (ssl_mode == NET_CON_SSL_MODE_SERVER)
{ {
con->ssl = SSL_new(g_hub->ssl_ctx); con->ssl = SSL_new(g_hub->ssl_ctx);

View File

@ -55,7 +55,6 @@ struct net_connection
net_connection_cb callback; /** Callback function */ net_connection_cb callback; /** Callback function */
struct event event; /** libevent struct for read/write events */ struct event event; /** libevent struct for read/write events */
struct event timeout; /** Used for internal timeout handling */ 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_recv; /** Timestamp for last recv() */
time_t last_send; /** Timestamp for last send() */ time_t last_send; /** Timestamp for last send() */
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
@ -64,7 +63,7 @@ struct net_connection
#endif /* SSL_SUPPORT */ #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); 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_set_timeout(struct net_connection* con, int seconds);
extern void net_con_clear_timeout(struct net_connection* con); 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 #ifdef SSL_SUPPORT
/** /**
* Start SSL_accept() * Start SSL_accept()