Use uint16_t for port instead of a string
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Wed, 9 Oct 2013 11:22:02 +0000 (08:22 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Wed, 9 Oct 2013 11:22:02 +0000 (08:22 -0300)
friend.c
friend.h

index de5ffdb..b7c3229 100644 (file)
--- a/friend.c
+++ b/friend.c
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <errno.h>
+#include <stdio.h>
 #include "message.h"
 
-static int connect_friend(struct sockaddr **saddr, char *address, char *port)
+static int connect_friend(struct sockaddr **saddr, char *address, uint16_t port)
 {
        struct addrinfo *addresses;
        struct addrinfo *addr;
        struct addrinfo hint;
        int r;
+       char sport[6];
+       snprintf(sport, sizeof(sport), "%us", port);
        memset(&hint, 0, sizeof(hint));
        hint.ai_family = AF_UNSPEC;
        hint.ai_socktype = SOCK_STREAM;
        hint.ai_protocol = IPPROTO_TCP;
        hint.ai_flags = AI_ADDRCONFIG;
-       r = getaddrinfo(address, port, &hint, &addresses);
+       r = getaddrinfo(address, sport, &hint, &addresses);
        if (r) {
                return r;
        }
@@ -58,7 +61,7 @@ static int connect_friend(struct sockaddr **saddr, char *address, char *port)
 struct friend {
        char *name;
        char *address;
-       char *port;
+       uint16_t port;
        struct sockaddr *saddr;
 };
 
@@ -106,7 +109,6 @@ static void destroy_friend(gpointer data)
        struct friend *friend = data;
        g_free(friend->name);
        g_free(friend->address);
-       g_free(friend->port);
        g_free(friend->saddr);
        g_slice_free(struct friend, friend);
 }
@@ -118,13 +120,13 @@ int destroy_cache(struct cache *cache)
        g_slice_free(struct cache, cache);
 }
 
-int cache_add_friend(struct cache *cache, char *name, char *address, char *port)
+int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
 {
        struct friend *friend;
        friend = g_slice_new0(struct friend);
        friend->name = g_strdup(name);
        friend->address = g_strdup(address);
-       friend->port = g_strdup(port);
+       friend->port = port;
        connect_friend(&friend->saddr, friend->address, friend->port);
        cache->friends = g_list_append(cache->friends, friend);
        return 0;
@@ -141,14 +143,13 @@ int load_cache(struct cache *cache, char *fname)
        for (group = groups; *group != NULL; group++) {
                gchar *name;
                gchar *address;
-               gchar *port;
+               uint16_t port;
                name = g_key_file_get_value(file, *group, "name", NULL);
                address = g_key_file_get_value(file, *group, "address", NULL);
-               port = g_key_file_get_value(file, *group, "port", NULL);
+               port = g_key_file_get_integer(file, *group, "port", NULL);
                cache_add_friend(cache, name, address, port);
                g_free(name);
                g_free(address);
-               g_free(port);
        }
        g_strfreev(groups);
        g_key_file_free(file);
@@ -167,7 +168,7 @@ int store_cache(struct cache *cache, char *fname)
                struct friend *friend = f->data;
                g_key_file_set_value(file, friend->name, "name", friend->name);
                g_key_file_set_value(file, friend->name, "address", friend->address);
-               g_key_file_set_value(file, friend->name, "port", friend->port);
+               g_key_file_set_integer(file, friend->name, "port", friend->port);
        }
        contents = g_key_file_to_data(file, &len, NULL);
        g_file_set_contents(fname, contents, len, NULL);
index 3c70741..b120fa1 100644 (file)
--- a/friend.h
+++ b/friend.h
@@ -19,7 +19,8 @@
 #ifndef _FRIEND_H
 #define _FRIEND_H
 
-#include <stdlib.h>
+#include <sys/types.h>
+#include <stdint.h>
 
 int sock_init(void);
 
@@ -27,7 +28,7 @@ struct friend;
 struct cache;
 int create_cache(struct cache **cache);
 int destroy_cache(struct cache *cache);
-int cache_add_friend(struct cache *cache, char *friend, char *address, char *port);
+int cache_add_friend(struct cache *cache, char *friend, char *address, uint16_t port);
 int load_cache(struct cache *cache, char *fname);
 int store_cache(struct cache *cache, char *fname);