Add socket and message.
[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 "message.h"
28
29 static int connect_friend(struct sockaddr **saddr, char *address, char *port)
30 {
31         struct addrinfo *addresses;
32         struct addrinfo *addr;
33         struct addrinfo hint;
34         int r;
35         memset(&hint, 0, sizeof(hint));
36         hint.ai_family = AF_UNSPEC;
37         hint.ai_socktype = SOCK_STREAM;
38         hint.ai_protocol = IPPROTO_TCP;
39         hint.ai_flags = AI_ADDRCONFIG;
40         r = getaddrinfo(address, port, &hint, &addresses);
41         if (r) {
42                 return r;
43         }
44         if (addresses != NULL) {
45                 *saddr = g_malloc(addresses->ai_addrlen);
46                 if (!*saddr) {
47                         r = -1;
48                 } else {
49                         memcpy(*saddr, addresses->ai_addr, addresses->ai_addrlen);
50                 }
51         }
52         freeaddrinfo(addresses);
53         if (r == -1)
54                 return EAI_SYSTEM;
55         return 0;
56 }
57
58 struct friend {
59         char *name;
60         char *address;
61         char *port;
62         struct sockaddr *saddr;
63 };
64
65 static int usock;
66
67 int sock_init(void)
68 {
69         struct sockaddr_in6 sa;
70         memset(&sa, 0, sizeof(sa));
71         sa.sin6_family = AF_INET6;
72         sa.sin6_port = htons(17078);
73         memcpy((void *) &sa.sin6_addr, (void *) &in6addr_any, sizeof(in6addr_any));
74         usock = socket(AF_INET6, SOCK_DGRAM, 0);
75         bind(usock, (struct sockaddr *) &sa, sizeof(sa));
76         message_init(usock);
77         return 0;
78 }
79
80 int friend_send_message(struct friend *friend, char *buffer, size_t len)
81 {
82         socklen_t sl;
83         if (friend->saddr->sa_family == AF_INET)
84                 sl = sizeof(struct sockaddr_in);
85         else if (friend->saddr->sa_family == AF_INET6)
86                 sl = sizeof(struct sockaddr_in6);
87         else
88                 return -EAFNOSUPPORT;
89         sendto(usock, buffer, len, 0, friend->saddr, sl);
90         return 0;
91 }
92
93 struct cache {
94         GList *friends;
95 };
96
97 int create_cache(struct cache **cache)
98 {
99         *cache = g_slice_new0(struct cache);
100         (*cache)->friends = NULL;
101         return 0;
102 }
103
104 static void destroy_friend(gpointer data)
105 {
106         struct friend *friend = data;
107         g_free(friend->name);
108         g_free(friend->address);
109         g_free(friend->port);
110         g_free(friend->saddr);
111         g_slice_free(struct friend, friend);
112 }
113
114 int destroy_cache(struct cache *cache)
115 {
116         if (cache->friends)
117                 g_list_free_full(cache->friends, destroy_friend);
118         g_slice_free(struct cache, cache);
119 }
120
121 int cache_add_friend(struct cache *cache, char *name, char *address, char *port)
122 {
123         struct friend *friend;
124         friend = g_slice_new0(struct friend);
125         friend->name = g_strdup(name);
126         friend->address = g_strdup(address);
127         friend->port = g_strdup(port);
128         connect_friend(&friend->saddr, friend->address, friend->port);
129         cache->friends = g_list_append(cache->friends, friend);
130         return 0;
131 }
132
133 int load_cache(struct cache *cache, char *fname)
134 {
135         GKeyFile *file;
136         gchar **groups;
137         gchar **group;
138         file = g_key_file_new();
139         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
140         groups = g_key_file_get_groups(file, NULL);
141         for (group = groups; *group != NULL; group++) {
142                 gchar *name;
143                 gchar *address;
144                 gchar *port;
145                 name = g_key_file_get_value(file, *group, "name", NULL);
146                 address = g_key_file_get_value(file, *group, "address", NULL);
147                 port = g_key_file_get_value(file, *group, "port", NULL);
148                 cache_add_friend(cache, name, address, port);
149                 g_free(name);
150                 g_free(address);
151                 g_free(port);
152         }
153         g_strfreev(groups);
154         g_key_file_free(file);
155         return 0;
156 }
157
158 int store_cache(struct cache *cache, char *fname)
159 {
160         GKeyFile *file;
161         GList *f;
162         gchar *contents;
163         gssize len;
164         file = g_key_file_new();
165         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
166         for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
167                 struct friend *friend = f->data;
168                 g_key_file_set_value(file, friend->name, "name", friend->name);
169                 g_key_file_set_value(file, friend->name, "address", friend->address);
170                 g_key_file_set_value(file, friend->name, "port", friend->port);
171         }
172         contents = g_key_file_to_data(file, &len, NULL);
173         g_file_set_contents(fname, contents, len, NULL);
174         g_free(contents);
175         g_key_file_free(file);
176         return 0;
177 }