Do not override CFLAGS and LIBS.
[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
27 static int connect_friend(struct sockaddr **saddr, char *address, char *port)
28 {
29         struct addrinfo *addresses;
30         struct addrinfo *addr;
31         struct addrinfo hint;
32         int r;
33         memset(&hint, 0, sizeof(hint));
34         hint.ai_family = AF_UNSPEC;
35         hint.ai_socktype = SOCK_STREAM;
36         hint.ai_protocol = IPPROTO_TCP;
37         hint.ai_flags = AI_ADDRCONFIG;
38         r = getaddrinfo(address, port, &hint, &addresses);
39         if (r) {
40                 return r;
41         }
42         if (addresses != NULL) {
43                 *saddr = g_malloc(addresses->ai_addrlen);
44                 if (!*saddr) {
45                         r = -1;
46                 } else {
47                         memcpy(*saddr, addresses->ai_addr, addresses->ai_addrlen);
48                 }
49         }
50         freeaddrinfo(addresses);
51         if (r == -1)
52                 return EAI_SYSTEM;
53         return 0;
54 }
55
56 struct friend {
57         char *name;
58         char *address;
59         char *port;
60         struct sockaddr *saddr;
61 };
62
63 struct cache {
64         GList *friends;
65 };
66
67 int create_cache(struct cache **cache)
68 {
69         *cache = g_slice_new0(struct cache);
70         (*cache)->friends = NULL;
71         return 0;
72 }
73
74 static void destroy_friend(gpointer data)
75 {
76         struct friend *friend = data;
77         g_free(friend->name);
78         g_free(friend->address);
79         g_free(friend->port);
80         g_free(friend->saddr);
81         g_slice_free(struct friend, friend);
82 }
83
84 int destroy_cache(struct cache *cache)
85 {
86         if (cache->friends)
87                 g_list_free_full(cache->friends, destroy_friend);
88         g_slice_free(struct cache, cache);
89 }
90
91 int cache_add_friend(struct cache *cache, char *name, char *address, char *port)
92 {
93         struct friend *friend;
94         friend = g_slice_new0(struct friend);
95         friend->name = g_strdup(name);
96         friend->address = g_strdup(address);
97         friend->port = g_strdup(port);
98         connect_friend(&friend->saddr, friend->address, friend->port);
99         g_list_append(cache->friends, friend);
100         return 0;
101 }
102
103 int load_cache(struct cache *cache, char *fname)
104 {
105         GKeyFile *file;
106         gchar **groups;
107         gchar **group;
108         file = g_key_file_new();
109         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
110         groups = g_key_file_get_groups(file, NULL);
111         for (group = groups; *group != NULL; group++) {
112                 gchar *name;
113                 gchar *address;
114                 gchar *port;
115                 name = g_key_file_get_value(file, *group, "name", NULL);
116                 address = g_key_file_get_value(file, *group, "address", NULL);
117                 port = g_key_file_get_value(file, *group, "port", NULL);
118                 cache_add_friend(cache, name, address, port);
119                 g_free(name);
120                 g_free(address);
121                 g_free(port);
122         }
123         g_strfreev(groups);
124         g_key_file_free(file);
125         return 0;
126 }
127
128 int store_cache(struct cache *cache, char *fname)
129 {
130         GKeyFile *file;
131         GList *f;
132         file = g_key_file_new();
133         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
134         for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
135                 struct friend *friend = f->data;
136                 g_key_file_set_value(file, friend->name, "name", friend->name);
137                 g_key_file_set_value(file, friend->name, "address", friend->address);
138                 g_key_file_set_value(file, friend->name, "port", friend->port);
139         }
140         g_key_file_free(file);
141         return 0;
142 }