From 6952f9b2fb6203aab63c63ef74c2da9b90f75f4b Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Wed, 9 Oct 2013 08:22:02 -0300 Subject: [PATCH] Use uint16_t for port instead of a string --- friend.c | 21 +++++++++++---------- friend.h | 5 +++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/friend.c b/friend.c index de5ffdb..b7c3229 100644 --- a/friend.c +++ b/friend.c @@ -24,20 +24,23 @@ #include #include #include +#include #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); diff --git a/friend.h b/friend.h index 3c70741..b120fa1 100644 --- a/friend.h +++ b/friend.h @@ -19,7 +19,8 @@ #ifndef _FRIEND_H #define _FRIEND_H -#include +#include +#include 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); -- 2.20.1