2 * Copyright (C) 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
27 static int connect_friend(struct sockaddr **saddr, char *address, char *port)
29 struct addrinfo *addresses;
30 struct addrinfo *addr;
33 memset(&hint, 0, sizeof(hint));
34 hint.ai_family = AF_UNSPEC;
35 hint.ai_socktype = SOCK_STREAM;
36 hint.ai_protocol = IPPROTO_TCP;
37 hint.ai_flags = AI_ADDRCONFIG;
38 r = getaddrinfo(address, port, &hint, &addresses);
42 if (addresses != NULL) {
43 *saddr = g_malloc(addresses->ai_addrlen);
47 memcpy(*saddr, addresses->ai_addr, addresses->ai_addrlen);
50 freeaddrinfo(addresses);
60 struct sockaddr *saddr;
67 int create_cache(struct cache **cache)
69 *cache = g_slice_new0(struct cache);
70 (*cache)->friends = NULL;
74 static void destroy_friend(gpointer data)
76 struct friend *friend = data;
78 g_free(friend->address);
80 g_free(friend->saddr);
81 g_slice_free(struct friend, friend);
84 int destroy_cache(struct cache *cache)
87 g_list_free_full(cache->friends, destroy_friend);
88 g_slice_free(struct cache, cache);
91 int cache_add_friend(struct cache *cache, char *name, char *address, char *port)
93 struct friend *friend;
94 friend = g_slice_new0(struct friend);
95 friend->name = g_strdup(name);
96 friend->address = g_strdup(address);
97 friend->port = g_strdup(port);
98 connect_friend(&friend->saddr, friend->address, friend->port);
99 cache->friends = g_list_append(cache->friends, friend);
103 int load_cache(struct cache *cache, char *fname)
108 file = g_key_file_new();
109 g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
110 groups = g_key_file_get_groups(file, NULL);
111 for (group = groups; *group != NULL; group++) {
115 name = g_key_file_get_value(file, *group, "name", NULL);
116 address = g_key_file_get_value(file, *group, "address", NULL);
117 port = g_key_file_get_value(file, *group, "port", NULL);
118 cache_add_friend(cache, name, address, port);
124 g_key_file_free(file);
128 int store_cache(struct cache *cache, char *fname)
134 file = g_key_file_new();
135 g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
136 for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
137 struct friend *friend = f->data;
138 g_key_file_set_value(file, friend->name, "name", friend->name);
139 g_key_file_set_value(file, friend->name, "address", friend->address);
140 g_key_file_set_value(file, friend->name, "port", friend->port);
142 contents = g_key_file_to_data(file, &len, NULL);
143 g_file_set_contents(fname, contents, len, NULL);
145 g_key_file_free(file);