Find friend by port.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 10 Oct 2013 11:07:12 +0000 (08:07 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 10 Oct 2013 11:07:12 +0000 (08:07 -0300)
If multiple friend are behind the same NAT, they may use different
source ports.

friend.c
friend.h
message.c

index 2d317e7..fd79643 100644 (file)
--- 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;
index 46f8ffe..76a22bd 100644 (file)
--- 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();
 
index 8e3202d..25b5160 100644 (file)
--- 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);