Find friend by port.
[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 struct friend {
31         char *name;
32         char *address;
33         uint16_t port;
34         GInetSocketAddress *saddr;
35 };
36
37 static GSocket *usock;
38
39 int sock_init(void)
40 {
41         GSocketAddress *address;
42         GInetAddress *any_addr;
43         any_addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6);
44         usock = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, NULL);
45         address = g_inet_socket_address_new(any_addr, 17078);
46         g_socket_bind(usock, address, TRUE, NULL);
47         g_object_unref(any_addr);
48         g_object_unref(address);
49         message_init(usock);
50         return 0;
51 }
52
53 int friend_send_message(struct friend *friend, char *buffer, size_t len)
54 {
55         g_socket_send_to(usock, G_SOCKET_ADDRESS(friend->saddr), buffer, len, NULL, NULL);
56         return 0;
57 }
58
59 struct cache {
60         GList *friends;
61 };
62
63 static struct cache *ucache;
64
65 struct friend *friend_get_by_address(GInetAddress *address, uint16_t port)
66 {
67         GList *l;
68         for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
69                 struct friend *friend = l->data;
70                 if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address) &&
71                     friend->port == port)
72                         return friend;
73         }
74         return NULL;
75 }
76
77 int create_cache(struct cache **cache)
78 {
79         ucache = *cache = g_slice_new0(struct cache);
80         (*cache)->friends = NULL;
81         return 0;
82 }
83
84 static void destroy_friend(gpointer data)
85 {
86         struct friend *friend = data;
87         g_free(friend->name);
88         g_free(friend->address);
89         g_object_unref(friend->saddr);
90         g_slice_free(struct friend, friend);
91 }
92
93 int destroy_cache(struct cache *cache)
94 {
95         if (cache->friends)
96                 g_list_free_full(cache->friends, destroy_friend);
97         g_slice_free(struct cache, cache);
98 }
99
100 char * friend_get_name(struct friend *friend)
101 {
102         return friend->name;
103 }
104
105 int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
106 {
107         struct friend *friend;
108         GInetAddress *addr;
109         friend = g_slice_new0(struct friend);
110         friend->name = g_strdup(name);
111         friend->address = g_strdup(address);
112         friend->port = port;
113         addr = g_inet_address_new_from_string(address);
114         friend->saddr = G_INET_SOCKET_ADDRESS(g_inet_socket_address_new(addr, friend->port));
115         g_object_unref(addr);
116         cache->friends = g_list_append(cache->friends, friend);
117         ping(friend);
118         return 0;
119 }
120
121 int load_cache(struct cache *cache, char *fname)
122 {
123         GKeyFile *file;
124         gchar **groups;
125         gchar **group;
126         file = g_key_file_new();
127         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
128         groups = g_key_file_get_groups(file, NULL);
129         for (group = groups; *group != NULL; group++) {
130                 gchar *name;
131                 gchar *address;
132                 uint16_t port;
133                 name = g_key_file_get_value(file, *group, "name", NULL);
134                 address = g_key_file_get_value(file, *group, "address", NULL);
135                 port = g_key_file_get_integer(file, *group, "port", NULL);
136                 cache_add_friend(cache, name, address, port);
137                 g_free(name);
138                 g_free(address);
139         }
140         g_strfreev(groups);
141         g_key_file_free(file);
142         return 0;
143 }
144
145 int store_cache(struct cache *cache, char *fname)
146 {
147         GKeyFile *file;
148         GList *f;
149         gchar *contents;
150         gssize len;
151         file = g_key_file_new();
152         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
153         for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
154                 struct friend *friend = f->data;
155                 g_key_file_set_value(file, friend->name, "name", friend->name);
156                 g_key_file_set_value(file, friend->name, "address", friend->address);
157                 g_key_file_set_integer(file, friend->name, "port", friend->port);
158         }
159         contents = g_key_file_to_data(file, &len, NULL);
160         g_file_set_contents(fname, contents, len, NULL);
161         g_free(contents);
162         g_key_file_free(file);
163         return 0;
164 }