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>
41 GInetSocketAddress *saddr;
45 static GSocket *usock;
49 GSocketAddress *address;
50 GInetAddress *any_addr;
53 any_addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6);
54 usock = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, NULL);
55 address = g_inet_socket_address_new(any_addr, 17078);
56 if (!g_socket_bind(usock, address, TRUE, &error)) {
60 g_object_unref(address);
61 g_object_unref(any_addr);
65 g_object_unref(usock);
69 int friend_send_message(struct friend *friend, char *buffer, size_t len)
71 g_socket_send_to(usock, G_SOCKET_ADDRESS(friend->saddr), buffer, len, NULL, NULL);
75 void friend_timeout(struct friend *friend)
77 if (friend->state == STATE_PINGED) {
78 friend->state = STATE_OFFLINE;
82 void friend_got_message(struct friend *friend, char *buffer, size_t len)
84 if (len >= 4 && !strncmp(buffer, "PING", 4)) {
85 friend->state = STATE_ONLINE;
87 } else if (len >= 4 && !strncmp(buffer, "PONG", 4)) {
88 friend->state = STATE_ONLINE;
92 static void friend_list(gchar **args, GSocketAddress *address);
94 static void friend_add(gchar **args, GSocketAddress *address)
98 struct menu_item cmds[] = {
99 { "list", friend_list },
100 { "add", friend_add },
103 void friend_cmd(gchar **args, GSocketAddress *address)
108 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++) {
109 if (!strcmp(args[1], cmds[i].cmd)) {
110 cmds[i].func(args, address);
115 void friend_init(void)
117 struct menu_item *mi;
118 mi = g_malloc(sizeof(*mi));
120 mi->func = friend_cmd;
128 static struct cache *ucache;
130 static void friend_list(gchar **args, GSocketAddress *address)
134 for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
135 struct friend *friend = l->data;
136 buffer = g_strdup_printf("%s\n", friend->name);
137 g_socket_send_to(usock, address, buffer, strlen(buffer), NULL, NULL);
140 g_socket_send_to(usock, address, buffer, 0, NULL, NULL);
143 struct friend *friend_get_by_address(GInetAddress *address, uint16_t port)
146 for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
147 struct friend *friend = l->data;
148 if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address) &&
149 friend->port == port)
155 int create_cache(struct cache **cache)
157 ucache = *cache = g_slice_new0(struct cache);
158 (*cache)->friends = NULL;
162 static void destroy_friend(gpointer data)
164 struct friend *friend = data;
165 g_free(friend->name);
166 g_free(friend->address);
167 g_object_unref(friend->saddr);
168 g_slice_free(struct friend, friend);
171 int destroy_cache(struct cache *cache)
174 g_list_free_full(cache->friends, destroy_friend);
175 g_slice_free(struct cache, cache);
178 char * friend_get_name(struct friend *friend)
183 int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
185 struct friend *friend;
187 friend = g_slice_new0(struct friend);
188 friend->name = g_strdup(name);
189 friend->address = g_strdup(address);
191 addr = g_inet_address_new_from_string(address);
192 friend->saddr = G_INET_SOCKET_ADDRESS(g_inet_socket_address_new(addr, friend->port));
193 g_object_unref(addr);
194 cache->friends = g_list_append(cache->friends, friend);
196 friend->state = STATE_PINGED;
200 int load_cache(struct cache *cache, char *fname)
205 file = g_key_file_new();
206 g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
207 groups = g_key_file_get_groups(file, NULL);
208 for (group = groups; *group != NULL; group++) {
212 name = g_key_file_get_value(file, *group, "name", NULL);
213 address = g_key_file_get_value(file, *group, "address", NULL);
214 port = g_key_file_get_integer(file, *group, "port", NULL);
215 cache_add_friend(cache, name, address, port);
220 g_key_file_free(file);
224 int store_cache(struct cache *cache, char *fname)
230 file = g_key_file_new();
231 g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
232 for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
233 struct friend *friend = f->data;
234 g_key_file_set_value(file, friend->name, "name", friend->name);
235 g_key_file_set_value(file, friend->name, "address", friend->address);
236 g_key_file_set_integer(file, friend->name, "port", friend->port);
238 contents = g_key_file_to_data(file, &len, NULL);
239 g_file_set_contents(fname, contents, len, NULL);
241 g_key_file_free(file);