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
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
39 #include "sysevents.h"
44 #define ADC_PROTOCOL 0
45 #define ADC_IDENTIFY 1
52 int minargs, needsender;
53 void (*func)(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv);
64 size_t inbufdata, inbufsize;
67 size_t cbdata, cbsize;
71 struct wcspair *hubinf;
76 /* I've never understood why both of these are necessary, but... */
77 static wchar_t *privid, *cid;
79 static wchar_t **parseadc(wchar_t *cmdline)
82 size_t retsize, retdata;
88 retsize = retdata = 0;
91 if((p2 = wcschr(p, L' ')) != NULL)
94 for(ep = p; len > 0; ep++, len--) {
98 } else if(ep[1] == L'n') {
100 } else if(ep[1] == L'\\') {
103 memmove(ep, ep + 2, (len -= 2) * sizeof(*ep));
107 memmove(ep, ep + 1, --len * sizeof(*ep));
112 addtobuf(ret, swcsdup(p));
119 static void sendeoc(struct socket *sk)
121 sockqueue(sk, "\n", 1);
124 static void sendadc1(struct socket *sk, int sep, wchar_t *arg)
128 size_t bufsize, bufdata;
131 bufsize = bufdata = 0;
134 for(; *arg != L'\0'; arg++) {
136 bufcat(buf, L"\\s", 2);
137 else if(*arg == L'\n')
138 bufcat(buf, L"\\n", 2);
139 else if(*arg == L'\\')
140 bufcat(buf, L"\\\\", 2);
144 addtobuf(buf, L'\0');
145 mbsbuf = icwcstombs(buf, "utf-8");
146 sockqueue(sk, mbsbuf, strlen(mbsbuf));
150 static void sendadc(struct socket *sk, int sep, ...)
157 for(i = 0; (arg = va_arg(args, wchar_t *)) != NULL; i++) {
161 sendadc1(sk, (i == 0)?sep:1, arg);
166 static struct qcmd *newqcmd(struct qcmd **queue, wchar_t **args)
170 while(*queue != NULL)
171 queue = &((*queue)->next);
172 new = smalloc(sizeof(*new));
179 static struct qcmd *ulqcmd(struct qcmd **queue)
183 if((ret = *queue) == NULL)
189 static void freeqcmd(struct qcmd *qcmd)
191 freeparr(qcmd->args);
195 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
197 #define UNUSED __attribute__ ((unused))
202 struct adchub *hub UNUSED = fn->data; \
203 struct socket *sk UNUSED = hub->sk;
210 for(i = 1; i < argc; i++) {
211 if(wcslen(argv[i]) < 3)
213 for(o = 0, f = 0; hub->sup[o]; o++) {
214 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
219 if(!wcsncmp(argv[i], L"AD", 2)) {
222 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
223 hub->sup[o] = swcsdup(argv[i] + 2);
224 } else if(!wcsncmp(argv[i], L"RM", 2)) {
228 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
239 hub->sid = swcsdup(argv[1]);
240 if(hub->state == ADC_PROTOCOL) {
241 hub->state = ADC_IDENTIFY;
254 static struct command hubcmds[] = {
255 {L"SUP", 1, 0, cmd_sup},
256 {L"SID", 2, 0, cmd_sid},
257 {L"INF", 0, 0, cmd_inf},
261 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
265 wchar_t *cmdnm, *sender, **argv;
267 if((argc = parrlen(argv = qcmd->args)) < 1)
271 if(wcslen(cmdnm) < 2)
274 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
279 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
286 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
287 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
288 if(argc < cmd->minargs)
290 cmd->func(fn, cmdnm, sender, argc, qcmd->args);
294 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
297 static void hubread(struct socket *sk, struct fnetnode *fn)
301 char *newbuf, *p1, *p2;
303 size_t datalen, cblen;
306 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
308 if(hub->inbufdata > 1024)
310 bufcat(hub->inbuf, newbuf, datalen);
312 /* Memory eating protection */
313 if(hub->cbdata > 65536)
315 while(hub->inbufdata > 0) {
316 if(hub->cbdata == hub->cbsize)
317 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
319 p2 = (char *)(hub->cb + hub->cbdata);
320 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
321 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
322 memmove(hub->inbuf, p1, hub->inbufdata);
323 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
324 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
327 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
329 if(errno == EILSEQ) {
330 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
333 } else if(errno == EINVAL) {
335 } else if(errno == E2BIG) {
338 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
343 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
345 newqcmd(&hub->queue, parseadc(hub->cb));
346 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
350 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
355 static void hubconnect(struct fnetnode *fn, struct socket *sk)
359 sk->readcb = (void (*)(struct socket *, void *))hubread;
360 sk->errcb = (void (*)(struct socket *, int, void *))huberr;
363 hub = smalloc(sizeof(*hub));
364 memset(hub, 0, sizeof(*hub));
365 getsock(hub->sk = sk);
366 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
367 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
372 sendadc(sk, 0, L"HSUP", L"ADBASE", eoc, NULL);
375 static void hubdestroy(struct fnetnode *fn)
380 iconv_close(hub->ich);
381 if(hub->inbuf != NULL)
388 static void hubkill(struct fnetnode *fn)
396 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
401 static int hubreqconn(struct fnetpeer *peer)
406 static struct fnet adcnet_store = {
407 .connect = hubconnect,
408 .destroy = hubdestroy,
410 .setnick = hubsetnick,
411 .reqconn = hubreqconn,
415 static struct fnet *adcnet = &adcnet_store;
420 struct fnetnode *fn, *nextfn;
425 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
427 if(fn->fnet != adcnet)
429 if((hub = fn->data) == NULL)
431 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
432 if((hub->sk != NULL) && (hub->sk->state == SOCK_EST))
442 static void preinit(int hup)
449 static int init(int hup)
452 char idbuf[24], *id32;
458 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
459 for(i = 0; i < sizeof(idbuf); i++)
460 idbuf[i] = rand() % 256;
461 id32 = base32encode(idbuf, sizeof(idbuf));
463 privid = icmbstowcs(id32, "us-ascii");
465 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
468 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
470 dotiger(&th, id32, 24);
473 restiger(&th, idbuf);
474 id32 = base32encode(idbuf, sizeof(idbuf));
476 cid = icmbstowcs(id32, "us-ascii");
482 static void terminate(void)
487 static struct configvar myvars[] = {
488 /** Specifies a specific UDP port to use for ADC search
489 * results. If left unspecified, a port is allocated
490 * dynamically. Useful for NAT routers (see also the
491 * net.visibleipv4 address for those cases). */
492 {CONF_VAR_INT, "udpport", {.num = 0}},
493 /** Specifies a specific TCP port to use for ADC peer
494 * connections. If left unspecified, a port is allocated
495 * dynamically. Useful for NAT routers (see also the
496 * net.visibleipv4 address for those cases). */
497 {CONF_VAR_INT, "tcpport", {.num = 0}},
501 static struct module me = {
508 .terminate = terminate,