Fixed HTTP-client query-string handling bug.
[doldaconnect.git] / daemon / filenet.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
302a2600 3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
d3372da9 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
0e227c1d
FT
33static void freepeer(struct fnetpeer *peer);
34
d3372da9 35static struct fnet *networks = NULL;
36struct fnetnode *fnetnodes = NULL;
37int numfnetnodes = 0;
38GCBCHAIN(newfncb, struct fnetnode *);
39
40static struct fnetnode *newfn(struct fnet *fnet)
41{
42 static int curid = 0;
43 struct fnetnode *new;
44
45 new = smalloc(sizeof(*new));
46 memset(new, 0, sizeof(*new));
47 new->fnet = fnet;
48 new->refcount = 1;
49 new->id = curid++;
50 new->mynick = swcsdup(confgetstr("cli", "defnick"));
51 new->srchwait = confgetint("fnet", "srchwait");
52 new->state = FNN_SYN;
53 CBCHAININIT(new, fnetnode_ac);
54 CBCHAININIT(new, fnetnode_chat);
55 CBCHAININIT(new, fnetnode_unlink);
56 CBCHAININIT(new, fnetnode_destroy);
d9c95ea0 57 CBCHAININIT(new, fnetpeer_new);
58 CBCHAININIT(new, fnetpeer_del);
59 CBCHAININIT(new, fnetpeer_chdi);
d3372da9 60 new->next = NULL;
61 new->prev = NULL;
62 numfnetnodes++;
63 return(new);
64}
65
66void killfnetnode(struct fnetnode *fn)
67{
68 fnetsetstate(fn, FNN_DEAD);
ffa81d5f
FT
69 if(fn->connected)
70 fn->fnet->kill(fn);
d3372da9 71}
72
cab0b442 73void getfnetnode(struct fnetnode *fn)
d3372da9 74{
75 fn->refcount++;
76#ifdef DEBUG
77 fprintf(stderr, "getfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount);
78#endif
79}
80
0e227c1d
FT
81static void freepeers(struct btree *n)
82{
83 if(n == NULL)
84 return;
85 freepeers(n->l);
86 freepeers(n->r);
87 freepeer(n->d);
88 free(n);
89}
90
d3372da9 91void putfnetnode(struct fnetnode *fn)
92{
93 struct fnetnode *cur;
94
95#ifdef DEBUG
96 fprintf(stderr, "putfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount - 1);
97#endif
98 if(--fn->refcount)
99 return;
100 for(cur = fnetnodes; cur != NULL; cur = cur->next)
101 {
102 if(cur == fn)
103 flog(LOG_CRIT, "BUG: fnetnode reached refcount 0 while still in list - id %i", fn->id);
104 }
105 CBCHAINDOCB(fn, fnetnode_destroy, fn);
106 CBCHAINFREE(fn, fnetnode_ac);
107 CBCHAINFREE(fn, fnetnode_chat);
108 CBCHAINFREE(fn, fnetnode_unlink);
109 CBCHAINFREE(fn, fnetnode_destroy);
d9c95ea0 110 CBCHAINFREE(fn, fnetpeer_new);
111 CBCHAINFREE(fn, fnetpeer_del);
112 CBCHAINFREE(fn, fnetpeer_chdi);
ffa81d5f 113 if((fn->fnet->destroy != NULL) && fn->connected)
d3372da9 114 fn->fnet->destroy(fn);
3ea7528f 115 while(fn->args != NULL)
116 freewcspair(fn->args, &fn->args);
0e227c1d 117 freepeers(fn->peers);
d3372da9 118 if(fn->mynick != NULL)
119 free(fn->mynick);
d1e8b9fd 120 if(fn->pubid != NULL)
121 free(fn->pubid);
d3372da9 122 if(fn->name != NULL)
123 free(fn->name);
a3a4a3a7 124 if(fn->owner != NULL)
125 free(fn->owner);
d3372da9 126 free(fn);
127 numfnetnodes--;
128}
129
130struct fnetnode *findfnetnode(int id)
131{
132 struct fnetnode *fn;
133
134 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
135 return(fn);
136}
137
138void linkfnetnode(struct fnetnode *fn)
139{
140 if(fn->linked)
141 return;
142 getfnetnode(fn);
143 fn->next = fnetnodes;
144 if(fnetnodes != NULL)
145 fnetnodes->prev = fn;
146 fnetnodes = fn;
147 fn->linked = 1;
148 GCBCHAINDOCB(newfncb, fn);
149}
150
151void unlinkfnetnode(struct fnetnode *fn)
152{
153 if(!fn->linked)
154 return;
155 if(fnetnodes == fn)
156 fnetnodes = fn->next;
157 if(fn->next != NULL)
158 fn->next->prev = fn->prev;
159 if(fn->prev != NULL)
160 fn->prev->next = fn->next;
161 fn->linked = 0;
162 CBCHAINDOCB(fn, fnetnode_unlink, fn);
163 putfnetnode(fn);
164}
165
cab0b442 166static void conncb(struct socket *sk, int err, struct fnetnode *data)
d3372da9 167{
168 if(err != 0)
169 {
170 killfnetnode(data);
171 putfnetnode(data);
cab0b442 172 return;
d3372da9 173 }
d3372da9 174 fnetsetstate(data, FNN_HS);
175 socksettos(sk, confgetint("fnet", "fntos"));
ffa81d5f
FT
176 data->fnet->connect(data, sk);
177 data->connected = 1;
d3372da9 178 putfnetnode(data);
179}
180
181static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
182{
198e4c08
FT
183 struct socket *sk;
184
185 sk = NULL;
186 if(addr != NULL)
187 sk = netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
188 if(sk == NULL)
d3372da9 189 {
190 killfnetnode(data);
191 putfnetnode(data);
192 } else {
198e4c08 193 putsock(sk);
d3372da9 194 }
195}
196
197static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
198{
199 struct fnetpeerdatum *datum;
200
201 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
202 {
203 if(!wcscmp(datum->id, id))
204 break;
205 }
206 return(datum);
207}
208
209static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
210{
211 struct fnetpeerdatum *new;
212
213 new = smalloc(sizeof(*new));
214 new->refcount = 0;
215 new->id = swcsdup(id);
216 new->datatype = datatype;
217 new->prev = NULL;
218 new->next = fn->peerdata;
219 if(fn->peerdata != NULL)
220 fn->peerdata->prev = new;
221 fn->peerdata = new;
222 return(new);
223}
224
d9c95ea0 225static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
d3372da9 226{
227 int i;
228
229 for(i = 0; i < peer->dinum; i++)
230 {
231 if(peer->peerdi[i].datum == datum)
232 break;
233 }
234 if(i >= peer->dinum)
235 {
236 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
237 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
238 peer->peerdi[peer->dinum].datum = datum;
239 datum->refcount++;
d9c95ea0 240 if(isnew != NULL)
241 *isnew = 1;
d3372da9 242 return(&peer->peerdi[peer->dinum++]);
243 } else {
d9c95ea0 244 if(isnew != NULL)
245 *isnew = 0;
d3372da9 246 return(&peer->peerdi[i]);
247 }
248}
249
250void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
251{
252 struct fnetpeerdatum *datum;
253 struct fnetpeerdi *di;
d9c95ea0 254 int changed;
d3372da9 255
256 if((datum = finddatum(peer->fn, id)) == NULL)
257 datum = adddatum(peer->fn, id, FNPD_STR);
d9c95ea0 258 di = difindoradd(peer, datum, &changed);
259 if(di->data.str != NULL) {
378f34d2 260 changed = (changed || wcscmp(value, di->data.str));
d3372da9 261 free(di->data.str);
d9c95ea0 262 } else {
263 changed = 1;
264 }
d3372da9 265 di->data.str = swcsdup(value);
d9c95ea0 266 if(changed)
267 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
d3372da9 268}
269
270void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
271{
272 struct fnetpeerdatum *datum;
273 struct fnetpeerdi *di;
d9c95ea0 274 int changed;
d3372da9 275
276 if((datum = finddatum(peer->fn, id)) == NULL)
277 datum = adddatum(peer->fn, id, FNPD_INT);
d9c95ea0 278 di = difindoradd(peer, datum, &changed);
279 changed = (changed || (di->data.num != value));
d3372da9 280 di->data.num = value;
d9c95ea0 281 if(changed)
282 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
d3372da9 283}
284
285void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
286{
287 struct fnetpeerdatum *datum;
288 struct fnetpeerdi *di;
d9c95ea0 289 int changed;
d3372da9 290
291 if((datum = finddatum(peer->fn, id)) == NULL)
292 datum = adddatum(peer->fn, id, FNPD_LL);
d9c95ea0 293 di = difindoradd(peer, datum, &changed);
294 changed = (changed || (di->data.lnum != value));
d3372da9 295 di->data.lnum = value;
d9c95ea0 296 if(changed)
297 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
d3372da9 298}
299
300static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
301{
302 if(--datum->refcount > 0)
303 return;
304 if(datum->next != NULL)
305 datum->next->prev = datum->prev;
306 if(datum->prev != NULL)
307 datum->prev->next = datum->next;
308 if(datum == peer->fn->peerdata)
309 peer->fn->peerdata = datum->next;
310 free(datum->id);
311 free(datum);
312}
313
314void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
315{
316 int i;
317 struct fnetpeerdatum *datum;
318
319 if((datum = finddatum(peer->fn, id)) == NULL)
320 return;
321 for(i = 0; i < peer->dinum; i++)
322 {
323 if(peer->peerdi[i].datum == datum)
324 break;
325 }
326 if(i >= peer->dinum)
327 return;
328 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
329 free(peer->peerdi[i].data.str);
330 peer->dinum--;
331 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
332 putdatum(peer, datum);
333}
334
0e227c1d
FT
335static int peercmpid(void *a, void *b)
336{
337 return(wcscmp(((struct fnetpeer *)a)->id, ((struct fnetpeer *)b)->id));
338}
339
d3372da9 340struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
341{
342 struct fnetpeer *new;
343
344 new = smalloc(sizeof(*new));
345 new->fn = fn;
346 new->id = swcsdup(id);
347 new->nick = swcsdup(nick);
348 new->flags.w = 0;
349 new->dinum = 0;
350 new->peerdi = NULL;
0e227c1d 351 bbtreeput(&fn->peers, new, peercmpid);
d3372da9 352 fn->numpeers++;
353 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
d9c95ea0 354 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
d3372da9 355 return(new);
356}
357
0e227c1d 358static void freepeer(struct fnetpeer *peer)
d3372da9 359{
360 int i;
361
d3372da9 362 peer->fn->numpeers--;
d3372da9 363 free(peer->id);
364 free(peer->nick);
365 for(i = 0; i < peer->dinum; i++)
366 {
367 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
368 free(peer->peerdi[i].data.str);
369 putdatum(peer, peer->peerdi[i].datum);
370 }
371 if(peer->peerdi != NULL)
372 free(peer->peerdi);
373 free(peer);
374}
375
0e227c1d
FT
376void fnetdelpeer(struct fnetpeer *peer)
377{
378 bbtreedel(&peer->fn->peers, peer, peercmpid);
379 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
380 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
381 freepeer(peer);
382}
383
384void fnetpeerdm(struct fnetnode *fn)
385{
386 struct btree *new;
387 struct fnetpeer *peer;
388 int intact;
389
390 new = NULL;
391 intact = 1;
392 for(peer = btreeiter(fn->peers); peer != NULL; peer = btreeiter(NULL)) {
393 if(!peer->flags.b.delete) {
394 bbtreeput(&new, peer, peercmpid);
395 } else {
396 intact = 0;
397 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
398 freepeer(peer);
399 }
400 }
401 btreefree(fn->peers);
402 fn->peers = new;
403 if(!intact)
404 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
405}
406
d3372da9 407struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
408{
0e227c1d 409 struct fnetpeer key;
d3372da9 410
0e227c1d
FT
411 key.id = id;
412 return(btreeget(fn->peers, &key, peercmpid));
d3372da9 413}
414
415int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
416{
417 int ret;
418
ffa81d5f 419 if((fn->fnet->setnick != NULL) && fn->connected)
d3372da9 420 ret = fn->fnet->setnick(fn, newnick);
421 else
422 ret = 0;
423 if(!ret)
424 {
425 if(fn->mynick != NULL)
426 free(fn->mynick);
427 fn->mynick = swcsdup(newnick);
428 }
429 return(ret);
430}
431
432int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
433{
ffa81d5f 434 if((fn->fnet->sendchat == NULL) || !fn->connected)
d3372da9 435 {
436 errno = ENOTSUP;
437 return(-1);
438 }
439 return(fn->fnet->sendchat(fn, public, to, string));
440}
441
442int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
443{
444 if(fn->fnet->search == NULL)
445 {
446 errno = ENOTSUP;
447 return(-1);
448 }
449 return(fn->fnet->search(fn, srch, ln));
450}
451
452void fnetsetname(struct fnetnode *fn, wchar_t *newname)
453{
454 if(fn->name != NULL)
455 free(fn->name);
456 fn->name = swcsdup(newname);
457 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
458}
459
460void fnetsetstate(struct fnetnode *fn, int newstate)
461{
462 fn->state = newstate;
463 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
464}
465
9c161e77
FT
466wchar_t *fnfilebasename(wchar_t *path)
467{
468 wchar_t *p;
469
470 if((p = wcsrchr(path, L'/')) != NULL)
471 return(p + 1);
472 return(path);
473}
474
d3372da9 475struct fnet *findfnet(wchar_t *name)
476{
477 struct fnet *fnet;
478
479 for(fnet = networks; fnet != NULL; fnet = fnet->next)
480 {
481 if(!wcscmp(name, fnet->name))
482 break;
483 }
484 return(fnet);
485}
486
a3a4a3a7 487struct fnetnode *fnetinitconnect(wchar_t *name, wchar_t *owner, char *addr, struct wcspair *args)
d3372da9 488{
489 struct fnet *fnet;
490 struct fnetnode *fn;
efc05613 491 struct wcspair *arg;
d3372da9 492
493 if((fnet = findfnet(name)) == NULL)
494 {
495 errno = EPROTONOSUPPORT;
496 return(NULL);
497 }
498 fn = newfn(fnet);
a3a4a3a7 499 fn->owner = swcsdup(owner);
d1e8b9fd 500 fn->pubid = icmbstowcs(addr, NULL);
501 if(fn->pubid == NULL)
502 fn->pubid = swcsdup(L"");
54771774 503 fn->args = args;
efc05613 504 for(arg = fn->args; arg != NULL; arg = arg->next)
505 {
506 if(!wcscmp(arg->key, L"nick"))
507 fnetsetnick(fn, arg->val);
508 }
d3372da9 509 getfnetnode(fn);
510 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
511 return(NULL);
512 return(fn);
513}
514
515void regfnet(struct fnet *fnet)
516{
517 fnet->next = networks;
518 networks = fnet;
519}
520
521/*
522 * Note on the chat string: Must be in UNIX text file format - that
523 * is, LF line endings. The filenet-specific code must see to it that
524 * any other kind of format is converted into that. In the future,
525 * certain control characters and escape sequences will be parsed by
526 * the client. Make sure that any filenet-specific code strips any
527 * such that aren't supposed to be in the protocol.
528 *
529 * Note on "name": This is supposed to be an identifier for the
530 * source. If the chat is a public message, set "public" to non-zero
531 * and "name" to whatever "chat room" name is appropriate for the
532 * fnetnode, but not NULL. If there is a "default" channel in this
533 * filenet, set "name" to the empty string. If the chat is a private
534 * message, name is ignored.
535 */
536void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
537{
538 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
539}
540
541static struct configvar myvars[] =
542{
d9f89ef5 543 /** The number of seconds to wait between searches. Most hubs
544 * require at least ten seconds, and quite often network lag will
545 * often make searches arrive to the hub more often than sent. It
546 * may be semi-dangerous to specify too low a value, since hubs
547 * will often kick users that search too often (even when the
548 * reason is network lag -- there is no way for the hub to know
549 * this), but it is quite annoying to set too high a value. 15 to
550 * 40 seconds are the recommended range (although the default is
551 * 15 seconds, it is recommended to set to 30 seconds). */
d3372da9 552 {CONF_VAR_INT, "srchwait", {.num = 15}},
d9f89ef5 553 /** The TOS value to use for hub connections (see the TOS VALUES
554 * section). */
d3372da9 555 {CONF_VAR_INT, "fntos", {.num = 0}},
d9f89ef5 556 /** The TOS value to use for peer connections (see the TOS VALUES
557 * section). */
d3372da9 558 {CONF_VAR_INT, "fnptos", {.num = 0}},
d9f89ef5 559 /** Specifies a maximum number of simultaneously connected
560 * hubs. Attempts to connect to new hubs beyond this limit will
561 * return an error. Set to zero to remove the limit. */
cd340dab 562 {CONF_VAR_INT, "maxnodes", {.num = 0}},
d3372da9 563 {CONF_VAR_END}
564};
565
566static struct module me =
567{
568 .conf =
569 {
570 .vars = myvars
571 },
572 .name = "fnet"
573};
574
575MODULE(me)