2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
21 #include <sys/socket.h>
33 static struct fnet *networks = NULL;
34 struct fnetnode *fnetnodes = NULL;
36 GCBCHAIN(newfncb, struct fnetnode *);
38 static struct fnetnode *newfn(struct fnet *fnet)
43 new = smalloc(sizeof(*new));
44 memset(new, 0, sizeof(*new));
48 new->mynick = swcsdup(confgetstr("cli", "defnick"));
49 new->srchwait = confgetint("fnet", "srchwait");
51 CBCHAININIT(new, fnetnode_ac);
52 CBCHAININIT(new, fnetnode_chat);
53 CBCHAININIT(new, fnetnode_unlink);
54 CBCHAININIT(new, fnetnode_destroy);
55 CBCHAININIT(new, fnetpeer_new);
56 CBCHAININIT(new, fnetpeer_del);
57 CBCHAININIT(new, fnetpeer_chdi);
64 void killfnetnode(struct fnetnode *fn)
66 fnetsetstate(fn, FNN_DEAD);
70 if(fn->sk->data == fn)
80 void getfnetnode(struct fnetnode *fn)
84 fprintf(stderr, "getfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount);
88 void putfnetnode(struct fnetnode *fn)
93 fprintf(stderr, "putfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount - 1);
97 for(cur = fnetnodes; cur != NULL; cur = cur->next)
100 flog(LOG_CRIT, "BUG: fnetnode reached refcount 0 while still in list - id %i", fn->id);
102 CBCHAINDOCB(fn, fnetnode_destroy, fn);
103 CBCHAINFREE(fn, fnetnode_ac);
104 CBCHAINFREE(fn, fnetnode_chat);
105 CBCHAINFREE(fn, fnetnode_unlink);
106 CBCHAINFREE(fn, fnetnode_destroy);
107 CBCHAINFREE(fn, fnetpeer_new);
108 CBCHAINFREE(fn, fnetpeer_del);
109 CBCHAINFREE(fn, fnetpeer_chdi);
110 if(fn->fnet->destroy != NULL)
111 fn->fnet->destroy(fn);
112 while(fn->args != NULL)
113 freewcspair(fn->args, &fn->args);
114 while(fn->peers != NULL)
115 fnetdelpeer(fn->peers);
116 if(fn->mynick != NULL)
118 if(fn->pubid != NULL)
124 if(fn->owner != NULL)
130 struct fnetnode *findfnetnode(int id)
134 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
138 void linkfnetnode(struct fnetnode *fn)
143 fn->next = fnetnodes;
144 if(fnetnodes != NULL)
145 fnetnodes->prev = fn;
148 GCBCHAINDOCB(newfncb, fn);
151 void unlinkfnetnode(struct fnetnode *fn)
156 fnetnodes = fn->next;
158 fn->next->prev = fn->prev;
160 fn->prev->next = fn->next;
162 CBCHAINDOCB(fn, fnetnode_unlink, fn);
166 static void conncb(struct socket *sk, int err, struct fnetnode *data)
175 fnetsetstate(data, FNN_HS);
176 socksettos(sk, confgetint("fnet", "fntos"));
177 data->fnet->connect(data);
181 static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
188 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
192 static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
194 struct fnetpeerdatum *datum;
196 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
198 if(!wcscmp(datum->id, id))
204 static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
206 struct fnetpeerdatum *new;
208 new = smalloc(sizeof(*new));
210 new->id = swcsdup(id);
211 new->datatype = datatype;
213 new->next = fn->peerdata;
214 if(fn->peerdata != NULL)
215 fn->peerdata->prev = new;
220 static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
224 for(i = 0; i < peer->dinum; i++)
226 if(peer->peerdi[i].datum == datum)
231 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
232 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
233 peer->peerdi[peer->dinum].datum = datum;
237 return(&peer->peerdi[peer->dinum++]);
241 return(&peer->peerdi[i]);
245 void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
247 struct fnetpeerdatum *datum;
248 struct fnetpeerdi *di;
251 if((datum = finddatum(peer->fn, id)) == NULL)
252 datum = adddatum(peer->fn, id, FNPD_STR);
253 di = difindoradd(peer, datum, &changed);
254 if(di->data.str != NULL) {
255 changed = (changed || wcscmp(value, di->data.str));
260 di->data.str = swcsdup(value);
262 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
265 void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
267 struct fnetpeerdatum *datum;
268 struct fnetpeerdi *di;
271 if((datum = finddatum(peer->fn, id)) == NULL)
272 datum = adddatum(peer->fn, id, FNPD_INT);
273 di = difindoradd(peer, datum, &changed);
274 changed = (changed || (di->data.num != value));
275 di->data.num = value;
277 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
280 void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
282 struct fnetpeerdatum *datum;
283 struct fnetpeerdi *di;
286 if((datum = finddatum(peer->fn, id)) == NULL)
287 datum = adddatum(peer->fn, id, FNPD_LL);
288 di = difindoradd(peer, datum, &changed);
289 changed = (changed || (di->data.lnum != value));
290 di->data.lnum = value;
292 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
295 static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
297 if(--datum->refcount > 0)
299 if(datum->next != NULL)
300 datum->next->prev = datum->prev;
301 if(datum->prev != NULL)
302 datum->prev->next = datum->next;
303 if(datum == peer->fn->peerdata)
304 peer->fn->peerdata = datum->next;
309 void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
312 struct fnetpeerdatum *datum;
314 if((datum = finddatum(peer->fn, id)) == NULL)
316 for(i = 0; i < peer->dinum; i++)
318 if(peer->peerdi[i].datum == datum)
323 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
324 free(peer->peerdi[i].data.str);
326 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
327 putdatum(peer, datum);
330 struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
332 struct fnetpeer *new;
334 new = smalloc(sizeof(*new));
336 new->id = swcsdup(id);
337 new->nick = swcsdup(nick);
341 new->next = fn->peers;
343 if(fn->peers != NULL)
344 fn->peers->prev = new;
347 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
348 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
352 void fnetdelpeer(struct fnetpeer *peer)
356 if(peer->next != NULL)
357 peer->next->prev = peer->prev;
358 if(peer->prev != NULL)
359 peer->prev->next = peer->next;
360 if(peer->fn->peers == peer)
361 peer->fn->peers = peer->next;
362 peer->fn->numpeers--;
363 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
364 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
367 for(i = 0; i < peer->dinum; i++)
369 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
370 free(peer->peerdi[i].data.str);
371 putdatum(peer, peer->peerdi[i].datum);
373 if(peer->peerdi != NULL)
378 struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
380 struct fnetpeer *cur;
382 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
386 int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
390 if(fn->fnet->setnick != NULL)
391 ret = fn->fnet->setnick(fn, newnick);
396 if(fn->mynick != NULL)
398 fn->mynick = swcsdup(newnick);
403 int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
405 if(fn->fnet->sendchat == NULL)
410 return(fn->fnet->sendchat(fn, public, to, string));
413 int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
415 if(fn->fnet->search == NULL)
420 return(fn->fnet->search(fn, srch, ln));
423 void fnetsetname(struct fnetnode *fn, wchar_t *newname)
427 fn->name = swcsdup(newname);
428 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
431 void fnetsetstate(struct fnetnode *fn, int newstate)
433 fn->state = newstate;
434 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
437 struct fnet *findfnet(wchar_t *name)
441 for(fnet = networks; fnet != NULL; fnet = fnet->next)
443 if(!wcscmp(name, fnet->name))
449 struct fnetnode *fnetinitconnect(wchar_t *name, wchar_t *owner, char *addr, struct wcspair *args)
455 if((fnet = findfnet(name)) == NULL)
457 errno = EPROTONOSUPPORT;
461 fn->owner = swcsdup(owner);
462 fn->pubid = icmbstowcs(addr, NULL);
463 if(fn->pubid == NULL)
464 fn->pubid = swcsdup(L"");
466 for(arg = fn->args; arg != NULL; arg = arg->next)
468 if(!wcscmp(arg->key, L"nick"))
469 fnetsetnick(fn, arg->val);
472 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
477 void regfnet(struct fnet *fnet)
479 fnet->next = networks;
484 * Note on the chat string: Must be in UNIX text file format - that
485 * is, LF line endings. The filenet-specific code must see to it that
486 * any other kind of format is converted into that. In the future,
487 * certain control characters and escape sequences will be parsed by
488 * the client. Make sure that any filenet-specific code strips any
489 * such that aren't supposed to be in the protocol.
491 * Note on "name": This is supposed to be an identifier for the
492 * source. If the chat is a public message, set "public" to non-zero
493 * and "name" to whatever "chat room" name is appropriate for the
494 * fnetnode, but not NULL. If there is a "default" channel in this
495 * filenet, set "name" to the empty string. If the chat is a private
496 * message, name is ignored.
498 void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
500 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
503 static struct configvar myvars[] =
505 /** The number of seconds to wait between searches. Most hubs
506 * require at least ten seconds, and quite often network lag will
507 * often make searches arrive to the hub more often than sent. It
508 * may be semi-dangerous to specify too low a value, since hubs
509 * will often kick users that search too often (even when the
510 * reason is network lag -- there is no way for the hub to know
511 * this), but it is quite annoying to set too high a value. 15 to
512 * 40 seconds are the recommended range (although the default is
513 * 15 seconds, it is recommended to set to 30 seconds). */
514 {CONF_VAR_INT, "srchwait", {.num = 15}},
515 /** The TOS value to use for hub connections (see the TOS VALUES
517 {CONF_VAR_INT, "fntos", {.num = 0}},
518 /** The TOS value to use for peer connections (see the TOS VALUES
520 {CONF_VAR_INT, "fnptos", {.num = 0}},
521 /** Specifies a maximum number of simultaneously connected
522 * hubs. Attempts to connect to new hubs beyond this limit will
523 * return an error. Set to zero to remove the limit. */
524 {CONF_VAR_INT, "maxnodes", {.num = 0}},
528 static struct module me =