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>
30 static int connect_friend(struct sockaddr **saddr, char *address, uint16_t port)
32 struct addrinfo *addresses;
33 struct addrinfo *addr;
37 snprintf(sport, sizeof(sport), "%us", port);
38 memset(&hint, 0, sizeof(hint));
39 hint.ai_family = AF_UNSPEC;
40 hint.ai_socktype = SOCK_STREAM;
41 hint.ai_protocol = IPPROTO_TCP;
42 hint.ai_flags = AI_ADDRCONFIG;
43 r = getaddrinfo(address, sport, &hint, &addresses);
47 if (addresses != NULL) {
48 *saddr = g_malloc(addresses->ai_addrlen);
52 memcpy(*saddr, addresses->ai_addr, addresses->ai_addrlen);
55 freeaddrinfo(addresses);
65 struct sockaddr *saddr;
72 struct sockaddr_in6 sa;
73 memset(&sa, 0, sizeof(sa));
74 sa.sin6_family = AF_INET6;
75 sa.sin6_port = htons(17078);
76 memcpy((void *) &sa.sin6_addr, (void *) &in6addr_any, sizeof(in6addr_any));
77 usock = socket(AF_INET6, SOCK_DGRAM, 0);
78 bind(usock, (struct sockaddr *) &sa, sizeof(sa));
83 int friend_send_message(struct friend *friend, char *buffer, size_t len)
86 if (friend->saddr->sa_family == AF_INET)
87 sl = sizeof(struct sockaddr_in);
88 else if (friend->saddr->sa_family == AF_INET6)
89 sl = sizeof(struct sockaddr_in6);
92 sendto(usock, buffer, len, 0, friend->saddr, sl);
100 int create_cache(struct cache **cache)
102 *cache = g_slice_new0(struct cache);
103 (*cache)->friends = NULL;
107 static void destroy_friend(gpointer data)
109 struct friend *friend = data;
110 g_free(friend->name);
111 g_free(friend->address);
112 g_free(friend->saddr);
113 g_slice_free(struct friend, friend);
116 int destroy_cache(struct cache *cache)
119 g_list_free_full(cache->friends, destroy_friend);
120 g_slice_free(struct cache, cache);
123 int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
125 struct friend *friend;
126 friend = g_slice_new0(struct friend);
127 friend->name = g_strdup(name);
128 friend->address = g_strdup(address);
130 connect_friend(&friend->saddr, friend->address, friend->port);
131 cache->friends = g_list_append(cache->friends, friend);
135 int load_cache(struct cache *cache, char *fname)
140 file = g_key_file_new();
141 g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
142 groups = g_key_file_get_groups(file, NULL);
143 for (group = groups; *group != NULL; group++) {
147 name = g_key_file_get_value(file, *group, "name", NULL);
148 address = g_key_file_get_value(file, *group, "address", NULL);
149 port = g_key_file_get_integer(file, *group, "port", NULL);
150 cache_add_friend(cache, name, address, port);
155 g_key_file_free(file);
159 int store_cache(struct cache *cache, char *fname)
165 file = g_key_file_new();
166 g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
167 for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
168 struct friend *friend = f->data;
169 g_key_file_set_value(file, friend->name, "name", friend->name);
170 g_key_file_set_value(file, friend->name, "address", friend->address);
171 g_key_file_set_integer(file, friend->name, "port", friend->port);
173 contents = g_key_file_to_data(file, &len, NULL);
174 g_file_set_contents(fname, contents, len, NULL);
176 g_key_file_free(file);