#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;
}
struct friend {
char *name;
char *address;
- char *port;
+ uint16_t port;
struct sockaddr *saddr;
};
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);
}
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;
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);
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);