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);
71 void getfnetnode(struct fnetnode *fn)
75 fprintf(stderr, "getfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount);
79 void putfnetnode(struct fnetnode *fn)
84 fprintf(stderr, "putfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount - 1);
88 for(cur = fnetnodes; cur != NULL; cur = cur->next)
91 flog(LOG_CRIT, "BUG: fnetnode reached refcount 0 while still in list - id %i", fn->id);
93 CBCHAINDOCB(fn, fnetnode_destroy, fn);
94 CBCHAINFREE(fn, fnetnode_ac);
95 CBCHAINFREE(fn, fnetnode_chat);
96 CBCHAINFREE(fn, fnetnode_unlink);
97 CBCHAINFREE(fn, fnetnode_destroy);
98 CBCHAINFREE(fn, fnetpeer_new);
99 CBCHAINFREE(fn, fnetpeer_del);
100 CBCHAINFREE(fn, fnetpeer_chdi);
101 if((fn->fnet->destroy != NULL) && fn->connected)
102 fn->fnet->destroy(fn);
103 while(fn->args != NULL)
104 freewcspair(fn->args, &fn->args);
105 while(fn->peers != NULL)
106 fnetdelpeer(fn->peers);
107 if(fn->mynick != NULL)
109 if(fn->pubid != NULL)
113 if(fn->owner != NULL)
119 struct fnetnode *findfnetnode(int id)
123 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
127 void linkfnetnode(struct fnetnode *fn)
132 fn->next = fnetnodes;
133 if(fnetnodes != NULL)
134 fnetnodes->prev = fn;
137 GCBCHAINDOCB(newfncb, fn);
140 void unlinkfnetnode(struct fnetnode *fn)
145 fnetnodes = fn->next;
147 fn->next->prev = fn->prev;
149 fn->prev->next = fn->next;
151 CBCHAINDOCB(fn, fnetnode_unlink, fn);
155 static void conncb(struct socket *sk, int err, struct fnetnode *data)
163 fnetsetstate(data, FNN_HS);
164 socksettos(sk, confgetint("fnet", "fntos"));
165 data->fnet->connect(data, sk);
171 static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
178 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
182 static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
184 struct fnetpeerdatum *datum;
186 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
188 if(!wcscmp(datum->id, id))
194 static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
196 struct fnetpeerdatum *new;
198 new = smalloc(sizeof(*new));
200 new->id = swcsdup(id);
201 new->datatype = datatype;
203 new->next = fn->peerdata;
204 if(fn->peerdata != NULL)
205 fn->peerdata->prev = new;
210 static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
214 for(i = 0; i < peer->dinum; i++)
216 if(peer->peerdi[i].datum == datum)
221 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
222 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
223 peer->peerdi[peer->dinum].datum = datum;
227 return(&peer->peerdi[peer->dinum++]);
231 return(&peer->peerdi[i]);
235 void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
237 struct fnetpeerdatum *datum;
238 struct fnetpeerdi *di;
241 if((datum = finddatum(peer->fn, id)) == NULL)
242 datum = adddatum(peer->fn, id, FNPD_STR);
243 di = difindoradd(peer, datum, &changed);
244 if(di->data.str != NULL) {
245 changed = (changed || wcscmp(value, di->data.str));
250 di->data.str = swcsdup(value);
252 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
255 void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
257 struct fnetpeerdatum *datum;
258 struct fnetpeerdi *di;
261 if((datum = finddatum(peer->fn, id)) == NULL)
262 datum = adddatum(peer->fn, id, FNPD_INT);
263 di = difindoradd(peer, datum, &changed);
264 changed = (changed || (di->data.num != value));
265 di->data.num = value;
267 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
270 void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
272 struct fnetpeerdatum *datum;
273 struct fnetpeerdi *di;
276 if((datum = finddatum(peer->fn, id)) == NULL)
277 datum = adddatum(peer->fn, id, FNPD_LL);
278 di = difindoradd(peer, datum, &changed);
279 changed = (changed || (di->data.lnum != value));
280 di->data.lnum = value;
282 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
285 static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
287 if(--datum->refcount > 0)
289 if(datum->next != NULL)
290 datum->next->prev = datum->prev;
291 if(datum->prev != NULL)
292 datum->prev->next = datum->next;
293 if(datum == peer->fn->peerdata)
294 peer->fn->peerdata = datum->next;
299 void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
302 struct fnetpeerdatum *datum;
304 if((datum = finddatum(peer->fn, id)) == NULL)
306 for(i = 0; i < peer->dinum; i++)
308 if(peer->peerdi[i].datum == datum)
313 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
314 free(peer->peerdi[i].data.str);
316 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
317 putdatum(peer, datum);
320 struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
322 struct fnetpeer *new;
324 new = smalloc(sizeof(*new));
326 new->id = swcsdup(id);
327 new->nick = swcsdup(nick);
331 new->next = fn->peers;
333 if(fn->peers != NULL)
334 fn->peers->prev = new;
337 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
338 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
342 void fnetdelpeer(struct fnetpeer *peer)
346 if(peer->next != NULL)
347 peer->next->prev = peer->prev;
348 if(peer->prev != NULL)
349 peer->prev->next = peer->next;
350 if(peer->fn->peers == peer)
351 peer->fn->peers = peer->next;
352 peer->fn->numpeers--;
353 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
354 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
357 for(i = 0; i < peer->dinum; i++)
359 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
360 free(peer->peerdi[i].data.str);
361 putdatum(peer, peer->peerdi[i].datum);
363 if(peer->peerdi != NULL)
368 struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
370 struct fnetpeer *cur;
372 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
376 int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
380 if((fn->fnet->setnick != NULL) && fn->connected)
381 ret = fn->fnet->setnick(fn, newnick);
386 if(fn->mynick != NULL)
388 fn->mynick = swcsdup(newnick);
393 int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
395 if((fn->fnet->sendchat == NULL) || !fn->connected)
400 return(fn->fnet->sendchat(fn, public, to, string));
403 int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
405 if(fn->fnet->search == NULL)
410 return(fn->fnet->search(fn, srch, ln));
413 void fnetsetname(struct fnetnode *fn, wchar_t *newname)
417 fn->name = swcsdup(newname);
418 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
421 void fnetsetstate(struct fnetnode *fn, int newstate)
423 fn->state = newstate;
424 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
427 struct fnet *findfnet(wchar_t *name)
431 for(fnet = networks; fnet != NULL; fnet = fnet->next)
433 if(!wcscmp(name, fnet->name))
439 struct fnetnode *fnetinitconnect(wchar_t *name, wchar_t *owner, char *addr, struct wcspair *args)
445 if((fnet = findfnet(name)) == NULL)
447 errno = EPROTONOSUPPORT;
451 fn->owner = swcsdup(owner);
452 fn->pubid = icmbstowcs(addr, NULL);
453 if(fn->pubid == NULL)
454 fn->pubid = swcsdup(L"");
456 for(arg = fn->args; arg != NULL; arg = arg->next)
458 if(!wcscmp(arg->key, L"nick"))
459 fnetsetnick(fn, arg->val);
462 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
467 void regfnet(struct fnet *fnet)
469 fnet->next = networks;
474 * Note on the chat string: Must be in UNIX text file format - that
475 * is, LF line endings. The filenet-specific code must see to it that
476 * any other kind of format is converted into that. In the future,
477 * certain control characters and escape sequences will be parsed by
478 * the client. Make sure that any filenet-specific code strips any
479 * such that aren't supposed to be in the protocol.
481 * Note on "name": This is supposed to be an identifier for the
482 * source. If the chat is a public message, set "public" to non-zero
483 * and "name" to whatever "chat room" name is appropriate for the
484 * fnetnode, but not NULL. If there is a "default" channel in this
485 * filenet, set "name" to the empty string. If the chat is a private
486 * message, name is ignored.
488 void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
490 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
493 static struct configvar myvars[] =
495 /** The number of seconds to wait between searches. Most hubs
496 * require at least ten seconds, and quite often network lag will
497 * often make searches arrive to the hub more often than sent. It
498 * may be semi-dangerous to specify too low a value, since hubs
499 * will often kick users that search too often (even when the
500 * reason is network lag -- there is no way for the hub to know
501 * this), but it is quite annoying to set too high a value. 15 to
502 * 40 seconds are the recommended range (although the default is
503 * 15 seconds, it is recommended to set to 30 seconds). */
504 {CONF_VAR_INT, "srchwait", {.num = 15}},
505 /** The TOS value to use for hub connections (see the TOS VALUES
507 {CONF_VAR_INT, "fntos", {.num = 0}},
508 /** The TOS value to use for peer connections (see the TOS VALUES
510 {CONF_VAR_INT, "fnptos", {.num = 0}},
511 /** Specifies a maximum number of simultaneously connected
512 * hubs. Attempts to connect to new hubs beyond this limit will
513 * return an error. Set to zero to remove the limit. */
514 {CONF_VAR_INT, "maxnodes", {.num = 0}},
518 static struct module me =