From 9bfbad1591814150338bb62050d28ad0e4033552 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 10 Oct 2013 08:07:12 -0300 Subject: [PATCH] Find friend by port. If multiple friend are behind the same NAT, they may use different source ports. --- friend.c | 5 +++-- friend.h | 2 +- message.c | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/friend.c b/friend.c index 2d317e7..fd79643 100644 --- a/friend.c +++ b/friend.c @@ -62,12 +62,13 @@ struct cache { static struct cache *ucache; -struct friend *friend_get_by_address(GInetAddress *address) +struct friend *friend_get_by_address(GInetAddress *address, uint16_t port) { GList *l; for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) { struct friend *friend = l->data; - if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address)) + if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address) && + friend->port == port) return friend; } return NULL; diff --git a/friend.h b/friend.h index 46f8ffe..76a22bd 100644 --- a/friend.h +++ b/friend.h @@ -34,7 +34,7 @@ int load_cache(struct cache *cache, char *fname); int store_cache(struct cache *cache, char *fname); int friend_send_message(struct friend *friend, char *buffer, size_t len); -struct friend *friend_get_by_address(GInetAddress *address); +struct friend *friend_get_by_address(GInetAddress *address, uint16_t port); char *friend_get_name(); diff --git a/message.c b/message.c index 8e3202d..25b5160 100644 --- a/message.c +++ b/message.c @@ -54,7 +54,8 @@ gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data) command(buffer, len); } else { struct friend *friend; - friend = friend_get_by_address(iaddress); + uint16_t port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(address)); + friend = friend_get_by_address(iaddress, port); if (friend) { printf("got message from %s\n", friend_get_name(friend)); g_source_remove_by_user_data(friend); -- 2.20.1