Fix format mismatch.
[doldaconnect.git] / daemon / filenet.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19#include <string.h>
20#include <wchar.h>
21#include <sys/socket.h>
22#include <errno.h>
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27#include "filenet.h"
28#include "search.h"
29#include "module.h"
30#include "utils.h"
31#include "net.h"
32
33static struct fnet *networks = NULL;
34struct fnetnode *fnetnodes = NULL;
35int numfnetnodes = 0;
36GCBCHAIN(newfncb, struct fnetnode *);
37
38static struct fnetnode *newfn(struct fnet *fnet)
39{
40 static int curid = 0;
41 struct fnetnode *new;
42
43 new = smalloc(sizeof(*new));
44 memset(new, 0, sizeof(*new));
45 new->fnet = fnet;
46 new->refcount = 1;
47 new->id = curid++;
48 new->mynick = swcsdup(confgetstr("cli", "defnick"));
49 new->srchwait = confgetint("fnet", "srchwait");
50 new->state = FNN_SYN;
51 CBCHAININIT(new, fnetnode_ac);
52 CBCHAININIT(new, fnetnode_chat);
53 CBCHAININIT(new, fnetnode_unlink);
54 CBCHAININIT(new, fnetnode_destroy);
d9c95ea0 55 CBCHAININIT(new, fnetpeer_new);
56 CBCHAININIT(new, fnetpeer_del);
57 CBCHAININIT(new, fnetpeer_chdi);
d3372da9 58 new->next = NULL;
59 new->prev = NULL;
60 numfnetnodes++;
61 return(new);
62}
63
64void killfnetnode(struct fnetnode *fn)
65{
66 fnetsetstate(fn, FNN_DEAD);
67 if(fn->sk != NULL)
68 {
69 fn->sk->close = 1;
70 if(fn->sk->data == fn)
71 putfnetnode(fn);
72 putsock(fn->sk);
73 fn->sk = NULL;
74 }
75}
76
77void getfnetnode(struct fnetnode *fn)
78{
79 fn->refcount++;
80#ifdef DEBUG
81 fprintf(stderr, "getfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount);
82#endif
83}
84
85void putfnetnode(struct fnetnode *fn)
86{
87 struct fnetnode *cur;
88
89#ifdef DEBUG
90 fprintf(stderr, "putfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount - 1);
91#endif
92 if(--fn->refcount)
93 return;
94 for(cur = fnetnodes; cur != NULL; cur = cur->next)
95 {
96 if(cur == fn)
97 flog(LOG_CRIT, "BUG: fnetnode reached refcount 0 while still in list - id %i", fn->id);
98 }
99 CBCHAINDOCB(fn, fnetnode_destroy, fn);
100 CBCHAINFREE(fn, fnetnode_ac);
101 CBCHAINFREE(fn, fnetnode_chat);
102 CBCHAINFREE(fn, fnetnode_unlink);
103 CBCHAINFREE(fn, fnetnode_destroy);
d9c95ea0 104 CBCHAINFREE(fn, fnetpeer_new);
105 CBCHAINFREE(fn, fnetpeer_del);
106 CBCHAINFREE(fn, fnetpeer_chdi);
d3372da9 107 if(fn->fnet->destroy != NULL)
108 fn->fnet->destroy(fn);
3ea7528f 109 while(fn->args != NULL)
110 freewcspair(fn->args, &fn->args);
d3372da9 111 while(fn->peers != NULL)
112 fnetdelpeer(fn->peers);
113 if(fn->mynick != NULL)
114 free(fn->mynick);
115 if(fn->name != NULL)
116 free(fn->name);
117 if(fn->sk != NULL)
118 putsock(fn->sk);
119 free(fn);
120 numfnetnodes--;
121}
122
123struct fnetnode *findfnetnode(int id)
124{
125 struct fnetnode *fn;
126
127 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
128 return(fn);
129}
130
131void linkfnetnode(struct fnetnode *fn)
132{
133 if(fn->linked)
134 return;
135 getfnetnode(fn);
136 fn->next = fnetnodes;
137 if(fnetnodes != NULL)
138 fnetnodes->prev = fn;
139 fnetnodes = fn;
140 fn->linked = 1;
141 GCBCHAINDOCB(newfncb, fn);
142}
143
144void unlinkfnetnode(struct fnetnode *fn)
145{
146 if(!fn->linked)
147 return;
148 if(fnetnodes == fn)
149 fnetnodes = fn->next;
150 if(fn->next != NULL)
151 fn->next->prev = fn->prev;
152 if(fn->prev != NULL)
153 fn->prev->next = fn->next;
154 fn->linked = 0;
155 CBCHAINDOCB(fn, fnetnode_unlink, fn);
156 putfnetnode(fn);
157}
158
159static void conncb(struct socket *sk, int err, struct fnetnode *data)
160{
161 if(err != 0)
162 {
163 killfnetnode(data);
164 putfnetnode(data);
165 return;
166 }
167 data->sk = sk;
168 fnetsetstate(data, FNN_HS);
169 socksettos(sk, confgetint("fnet", "fntos"));
170 data->fnet->connect(data);
171 putfnetnode(data);
172}
173
174static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
175{
176 if(addr == NULL)
177 {
178 killfnetnode(data);
179 putfnetnode(data);
180 } else {
181 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
182 }
183}
184
185static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
186{
187 struct fnetpeerdatum *datum;
188
189 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
190 {
191 if(!wcscmp(datum->id, id))
192 break;
193 }
194 return(datum);
195}
196
197static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
198{
199 struct fnetpeerdatum *new;
200
201 new = smalloc(sizeof(*new));
202 new->refcount = 0;
203 new->id = swcsdup(id);
204 new->datatype = datatype;
205 new->prev = NULL;
206 new->next = fn->peerdata;
207 if(fn->peerdata != NULL)
208 fn->peerdata->prev = new;
209 fn->peerdata = new;
210 return(new);
211}
212
d9c95ea0 213static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
d3372da9 214{
215 int i;
216
217 for(i = 0; i < peer->dinum; i++)
218 {
219 if(peer->peerdi[i].datum == datum)
220 break;
221 }
222 if(i >= peer->dinum)
223 {
224 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
225 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
226 peer->peerdi[peer->dinum].datum = datum;
227 datum->refcount++;
d9c95ea0 228 if(isnew != NULL)
229 *isnew = 1;
d3372da9 230 return(&peer->peerdi[peer->dinum++]);
231 } else {
d9c95ea0 232 if(isnew != NULL)
233 *isnew = 0;
d3372da9 234 return(&peer->peerdi[i]);
235 }
236}
237
238void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
239{
240 struct fnetpeerdatum *datum;
241 struct fnetpeerdi *di;
d9c95ea0 242 int changed;
d3372da9 243
244 if((datum = finddatum(peer->fn, id)) == NULL)
245 datum = adddatum(peer->fn, id, FNPD_STR);
d9c95ea0 246 di = difindoradd(peer, datum, &changed);
247 if(di->data.str != NULL) {
378f34d2 248 changed = (changed || wcscmp(value, di->data.str));
d3372da9 249 free(di->data.str);
d9c95ea0 250 } else {
251 changed = 1;
252 }
d3372da9 253 di->data.str = swcsdup(value);
d9c95ea0 254 if(changed)
255 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
d3372da9 256}
257
258void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
259{
260 struct fnetpeerdatum *datum;
261 struct fnetpeerdi *di;
d9c95ea0 262 int changed;
d3372da9 263
264 if((datum = finddatum(peer->fn, id)) == NULL)
265 datum = adddatum(peer->fn, id, FNPD_INT);
d9c95ea0 266 di = difindoradd(peer, datum, &changed);
267 changed = (changed || (di->data.num != value));
d3372da9 268 di->data.num = value;
d9c95ea0 269 if(changed)
270 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
d3372da9 271}
272
273void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
274{
275 struct fnetpeerdatum *datum;
276 struct fnetpeerdi *di;
d9c95ea0 277 int changed;
d3372da9 278
279 if((datum = finddatum(peer->fn, id)) == NULL)
280 datum = adddatum(peer->fn, id, FNPD_LL);
d9c95ea0 281 di = difindoradd(peer, datum, &changed);
282 changed = (changed || (di->data.lnum != value));
d3372da9 283 di->data.lnum = value;
d9c95ea0 284 if(changed)
285 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
d3372da9 286}
287
288static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
289{
290 if(--datum->refcount > 0)
291 return;
292 if(datum->next != NULL)
293 datum->next->prev = datum->prev;
294 if(datum->prev != NULL)
295 datum->prev->next = datum->next;
296 if(datum == peer->fn->peerdata)
297 peer->fn->peerdata = datum->next;
298 free(datum->id);
299 free(datum);
300}
301
302void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
303{
304 int i;
305 struct fnetpeerdatum *datum;
306
307 if((datum = finddatum(peer->fn, id)) == NULL)
308 return;
309 for(i = 0; i < peer->dinum; i++)
310 {
311 if(peer->peerdi[i].datum == datum)
312 break;
313 }
314 if(i >= peer->dinum)
315 return;
316 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
317 free(peer->peerdi[i].data.str);
318 peer->dinum--;
319 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
320 putdatum(peer, datum);
321}
322
323struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
324{
325 struct fnetpeer *new;
326
327 new = smalloc(sizeof(*new));
328 new->fn = fn;
329 new->id = swcsdup(id);
330 new->nick = swcsdup(nick);
331 new->flags.w = 0;
332 new->dinum = 0;
333 new->peerdi = NULL;
334 new->next = fn->peers;
335 new->prev = NULL;
336 if(fn->peers != NULL)
337 fn->peers->prev = new;
338 fn->peers = new;
339 fn->numpeers++;
340 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
d9c95ea0 341 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
d3372da9 342 return(new);
343}
344
345void fnetdelpeer(struct fnetpeer *peer)
346{
347 int i;
348
349 if(peer->next != NULL)
350 peer->next->prev = peer->prev;
351 if(peer->prev != NULL)
352 peer->prev->next = peer->next;
353 if(peer->fn->peers == peer)
354 peer->fn->peers = peer->next;
355 peer->fn->numpeers--;
356 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
d9c95ea0 357 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
d3372da9 358 free(peer->id);
359 free(peer->nick);
360 for(i = 0; i < peer->dinum; i++)
361 {
362 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
363 free(peer->peerdi[i].data.str);
364 putdatum(peer, peer->peerdi[i].datum);
365 }
366 if(peer->peerdi != NULL)
367 free(peer->peerdi);
368 free(peer);
369}
370
371struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
372{
373 struct fnetpeer *cur;
374
375 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
376 return(cur);
377}
378
379int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
380{
381 int ret;
382
383 if(fn->fnet->setnick != NULL)
384 ret = fn->fnet->setnick(fn, newnick);
385 else
386 ret = 0;
387 if(!ret)
388 {
389 if(fn->mynick != NULL)
390 free(fn->mynick);
391 fn->mynick = swcsdup(newnick);
392 }
393 return(ret);
394}
395
396int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
397{
398 if(fn->fnet->sendchat == NULL)
399 {
400 errno = ENOTSUP;
401 return(-1);
402 }
403 return(fn->fnet->sendchat(fn, public, to, string));
404}
405
406int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
407{
408 if(fn->fnet->search == NULL)
409 {
410 errno = ENOTSUP;
411 return(-1);
412 }
413 return(fn->fnet->search(fn, srch, ln));
414}
415
416void fnetsetname(struct fnetnode *fn, wchar_t *newname)
417{
418 if(fn->name != NULL)
419 free(fn->name);
420 fn->name = swcsdup(newname);
421 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
422}
423
424void fnetsetstate(struct fnetnode *fn, int newstate)
425{
426 fn->state = newstate;
427 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
428}
429
430struct fnet *findfnet(wchar_t *name)
431{
432 struct fnet *fnet;
433
434 for(fnet = networks; fnet != NULL; fnet = fnet->next)
435 {
436 if(!wcscmp(name, fnet->name))
437 break;
438 }
439 return(fnet);
440}
441
efc05613 442struct fnetnode *fnetinitconnect(wchar_t *name, char *addr, struct wcspair *args)
d3372da9 443{
444 struct fnet *fnet;
445 struct fnetnode *fn;
efc05613 446 struct wcspair *arg;
d3372da9 447
448 if((fnet = findfnet(name)) == NULL)
449 {
450 errno = EPROTONOSUPPORT;
451 return(NULL);
452 }
453 fn = newfn(fnet);
54771774 454 fn->args = args;
efc05613 455 for(arg = fn->args; arg != NULL; arg = arg->next)
456 {
457 if(!wcscmp(arg->key, L"nick"))
458 fnetsetnick(fn, arg->val);
459 }
d3372da9 460 getfnetnode(fn);
461 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
462 return(NULL);
463 return(fn);
464}
465
466void regfnet(struct fnet *fnet)
467{
468 fnet->next = networks;
469 networks = fnet;
470}
471
472/*
473 * Note on the chat string: Must be in UNIX text file format - that
474 * is, LF line endings. The filenet-specific code must see to it that
475 * any other kind of format is converted into that. In the future,
476 * certain control characters and escape sequences will be parsed by
477 * the client. Make sure that any filenet-specific code strips any
478 * such that aren't supposed to be in the protocol.
479 *
480 * Note on "name": This is supposed to be an identifier for the
481 * source. If the chat is a public message, set "public" to non-zero
482 * and "name" to whatever "chat room" name is appropriate for the
483 * fnetnode, but not NULL. If there is a "default" channel in this
484 * filenet, set "name" to the empty string. If the chat is a private
485 * message, name is ignored.
486 */
487void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
488{
489 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
490}
491
492static struct configvar myvars[] =
493{
494 {CONF_VAR_INT, "srchwait", {.num = 15}},
495 {CONF_VAR_INT, "fntos", {.num = 0}},
496 {CONF_VAR_INT, "fnptos", {.num = 0}},
497 {CONF_VAR_END}
498};
499
500static struct module me =
501{
502 .conf =
503 {
504 .vars = myvars
505 },
506 .name = "fnet"
507};
508
509MODULE(me)