Use a GLib main loop
[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 <stdio.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.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 = 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 struct cache {
66         int nfriends;
67         struct friend *friends;
68 };
69
70 int create_cache(struct cache **cache)
71 {
72         *cache = malloc(sizeof(*cache));
73         if (!*cache)
74                 return -errno;
75         (*cache)->nfriends = 0;
76         (*cache)->friends = NULL;
77         return 0;
78 }
79
80 int destroy_cache(struct cache *cache)
81 {
82         if (cache->friends)
83                 free(cache->friends);
84         free(cache);
85 }
86
87 int cache_add_friend(struct cache *cache, char *friend, char *address, char *port)
88 {
89         struct friend *tfriend;
90         if (!cache->friends) {
91                 cache->friends = malloc(sizeof(struct friend));
92                 cache->nfriends = 1;
93         } else {
94                 struct friend *new_friends;
95                 cache->nfriends++;
96                 new_friends = realloc(cache->friends, cache->nfriends * sizeof(struct friend));
97                 if (!new_friends) {
98                         cache->nfriends--;
99                         return -errno;
100                 }
101                 cache->friends = new_friends;
102         }
103         tfriend = &cache->friends[cache->nfriends - 1];
104         tfriend->name = friend;
105         tfriend->address = address;
106         tfriend->port = port;
107         connect_friend(&tfriend->saddr, tfriend->address, tfriend->port);
108         return 0;
109 }
110
111 int load_cache(struct cache *cache, char *fname)
112 {
113         FILE *file;
114         int err = 0;
115         char *buffer = NULL;
116         size_t len = 0;
117         int r;
118         file = fopen(fname, "r");
119         if (!file)
120                 return -errno;
121         while ((r = getline(&buffer, &len, file)) > 0) {
122                 char *name;
123                 char *address;
124                 char *port;
125                 char *end;
126                 name = buffer;
127                 address = name;
128                 while (*++address != '\t');
129                 *address++ = '\0';
130                 port = address;
131                 while (*++port != '\t');
132                 *port++ = '\0';
133                 end = port;
134                 while (*++end != '\n');
135                 *end = '\0';
136                 fprintf(file, "%s\t%s\t%s\n", name, address, port);
137                 cache_add_friend(cache, strdup(name), strdup(address), strdup(port));
138         }
139 out:
140         fclose(file);
141         return err;
142 }
143
144 int store_cache(struct cache *cache, char *fname)
145 {
146         FILE *file;
147         int err = 0;
148         int i;
149         file = fopen(fname, "w");
150         if (!file)
151                 return -errno;
152         for (i = 0; i < cache->nfriends; i++) {
153                 struct friend *friend = &cache->friends[i];
154                 fprintf(file, "%s\t%s\t%s\n", friend->name, friend->address, friend->port);
155         }
156 out:
157         fclose(file);
158         return err;
159 }