2e50f3677177d33151c877e6c7bda305f75475e6
[cascardo/f2fchat.git] / friend.c
1 /*
2  *  Copyright (C) 2013  Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "friend.h"
20 #include <string.h>
21 #include <glib.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include "message.h"
29
30 enum {
31         STATE_OFFLINE,
32         STATE_PINGED,
33         STATE_ONLINE,
34 };
35
36 struct friend {
37         char *name;
38         char *address;
39         uint16_t port;
40         GInetSocketAddress *saddr;
41         int state;
42 };
43
44 static GSocket *usock;
45
46 int sock_init(void)
47 {
48         GSocketAddress *address;
49         GInetAddress *any_addr;
50         GError *error;
51         int err = 0;
52         any_addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6);
53         usock = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, NULL);
54         address = g_inet_socket_address_new(any_addr, 17078);
55         if (!g_socket_bind(usock, address, TRUE, &error)) {
56                 err = error->code;
57                 g_error_free(error);
58         }
59         g_object_unref(address);
60         g_object_unref(any_addr);
61         if (!err)
62                 message_init(usock);
63         else
64                 g_object_unref(usock);
65         return err;
66 }
67
68 int friend_send_message(struct friend *friend, char *buffer, size_t len)
69 {
70         g_socket_send_to(usock, G_SOCKET_ADDRESS(friend->saddr), buffer, len, NULL, NULL);
71         return 0;
72 }
73
74 void friend_timeout(struct friend *friend)
75 {
76         if (friend->state == STATE_PINGED) {
77                 friend->state = STATE_OFFLINE;
78         }
79 }
80
81 void friend_got_message(struct friend *friend, char *buffer, size_t len)
82 {
83         if (len >= 4 && !strncmp(buffer, "PING", 4)) {
84                 friend->state = STATE_ONLINE;
85                 pong(friend);
86         } else if (len >= 4 && !strncmp(buffer, "PONG", 4)) {
87                 friend->state = STATE_ONLINE;
88         }
89 }
90
91 struct cache {
92         GList *friends;
93 };
94
95 static struct cache *ucache;
96
97 struct friend *friend_get_by_address(GInetAddress *address, uint16_t port)
98 {
99         GList *l;
100         for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
101                 struct friend *friend = l->data;
102                 if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address) &&
103                     friend->port == port)
104                         return friend;
105         }
106         return NULL;
107 }
108
109 int create_cache(struct cache **cache)
110 {
111         ucache = *cache = g_slice_new0(struct cache);
112         (*cache)->friends = NULL;
113         return 0;
114 }
115
116 static void destroy_friend(gpointer data)
117 {
118         struct friend *friend = data;
119         g_free(friend->name);
120         g_free(friend->address);
121         g_object_unref(friend->saddr);
122         g_slice_free(struct friend, friend);
123 }
124
125 int destroy_cache(struct cache *cache)
126 {
127         if (cache->friends)
128                 g_list_free_full(cache->friends, destroy_friend);
129         g_slice_free(struct cache, cache);
130 }
131
132 char * friend_get_name(struct friend *friend)
133 {
134         return friend->name;
135 }
136
137 int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
138 {
139         struct friend *friend;
140         GInetAddress *addr;
141         friend = g_slice_new0(struct friend);
142         friend->name = g_strdup(name);
143         friend->address = g_strdup(address);
144         friend->port = port;
145         addr = g_inet_address_new_from_string(address);
146         friend->saddr = G_INET_SOCKET_ADDRESS(g_inet_socket_address_new(addr, friend->port));
147         g_object_unref(addr);
148         cache->friends = g_list_append(cache->friends, friend);
149         ping(friend);
150         friend->state = STATE_PINGED;
151         return 0;
152 }
153
154 int load_cache(struct cache *cache, char *fname)
155 {
156         GKeyFile *file;
157         gchar **groups;
158         gchar **group;
159         file = g_key_file_new();
160         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
161         groups = g_key_file_get_groups(file, NULL);
162         for (group = groups; *group != NULL; group++) {
163                 gchar *name;
164                 gchar *address;
165                 uint16_t port;
166                 name = g_key_file_get_value(file, *group, "name", NULL);
167                 address = g_key_file_get_value(file, *group, "address", NULL);
168                 port = g_key_file_get_integer(file, *group, "port", NULL);
169                 cache_add_friend(cache, name, address, port);
170                 g_free(name);
171                 g_free(address);
172         }
173         g_strfreev(groups);
174         g_key_file_free(file);
175         return 0;
176 }
177
178 int store_cache(struct cache *cache, char *fname)
179 {
180         GKeyFile *file;
181         GList *f;
182         gchar *contents;
183         gssize len;
184         file = g_key_file_new();
185         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
186         for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
187                 struct friend *friend = f->data;
188                 g_key_file_set_value(file, friend->name, "name", friend->name);
189                 g_key_file_set_value(file, friend->name, "address", friend->address);
190                 g_key_file_set_integer(file, friend->name, "port", friend->port);
191         }
192         contents = g_key_file_to_data(file, &len, NULL);
193         g_file_set_contents(fname, contents, len, NULL);
194         g_free(contents);
195         g_key_file_free(file);
196         return 0;
197 }