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
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
40 #include "sysevents.h"
45 #define ADC_PROTOCOL 0
46 #define ADC_IDENTIFY 1
53 int minargs, needsender;
54 void (*func)(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv);
70 size_t inbufdata, inbufsize;
73 size_t cbdata, cbsize;
77 struct wcspair *hubinf;
78 struct qcmdqueue queue;
82 /* I've never understood why both of these are necessary, but... */
83 static wchar_t *privid, *cid;
85 static wchar_t **parseadc(wchar_t *cmdline)
88 size_t retsize, retdata;
94 retsize = retdata = 0;
97 if((p2 = wcschr(p, L' ')) != NULL)
100 for(ep = p; len > 0; ep++, len--) {
104 } else if(ep[1] == L'n') {
106 } else if(ep[1] == L'\\') {
109 memmove(ep, ep + 2, (len -= 2) * sizeof(*ep));
113 memmove(ep, ep + 1, --len * sizeof(*ep));
118 addtobuf(ret, swcsdup(p));
125 static void sendeoc(struct socket *sk)
127 sockqueue(sk, "\n", 1);
130 static void sendadc1(struct socket *sk, int sep, wchar_t *arg)
134 size_t bufsize, bufdata;
137 bufsize = bufdata = 0;
140 for(; *arg != L'\0'; arg++) {
142 bufcat(buf, L"\\s", 2);
143 else if(*arg == L'\n')
144 bufcat(buf, L"\\n", 2);
145 else if(*arg == L'\\')
146 bufcat(buf, L"\\\\", 2);
150 addtobuf(buf, L'\0');
151 mbsbuf = icwcstombs(buf, "utf-8");
152 sockqueue(sk, mbsbuf, strlen(mbsbuf));
156 static void sendadc(struct socket *sk, int sep, ...)
163 for(i = 0; (arg = va_arg(args, wchar_t *)) != NULL; i++) {
167 sendadc1(sk, (i == 0)?sep:1, arg);
172 static struct qcmd *newqcmd(struct qcmdqueue *queue, wchar_t **args)
176 new = smalloc(sizeof(*new));
182 queue->l->next = new;
188 static struct qcmd *ulqcmd(struct qcmdqueue *queue)
192 if((ret = queue->f) == NULL)
194 if((queue->f = ret->next) == NULL)
200 static void freeqcmd(struct qcmd *qcmd)
202 freeparr(qcmd->args);
206 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
208 #define UNUSED __attribute__ ((unused))
213 struct adchub *hub UNUSED = fn->data; \
214 struct socket *sk UNUSED = hub->sk;
221 for(i = 1; i < argc; i++) {
222 if(wcslen(argv[i]) < 3)
224 for(o = 0, f = 0; hub->sup[o]; o++) {
225 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
230 if(!wcsncmp(argv[i], L"AD", 2)) {
233 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
234 hub->sup[o] = swcsdup(argv[i] + 2);
235 } else if(!wcsncmp(argv[i], L"RM", 2)) {
239 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
250 hub->sid = swcsdup(argv[1]);
251 if(hub->state == ADC_PROTOCOL) {
252 hub->state = ADC_IDENTIFY;
265 static struct command hubcmds[] = {
266 {L"SUP", 1, 0, cmd_sup},
267 {L"SID", 2, 0, cmd_sid},
268 {L"INF", 0, 0, cmd_inf},
272 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
276 wchar_t *cmdnm, *sender, **argv;
278 if((argc = parrlen(argv = qcmd->args)) < 1)
282 if(wcslen(cmdnm) < 2)
285 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
290 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
297 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
298 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
299 if(argc < cmd->minargs)
301 cmd->func(fn, cmdnm, sender, argc, argv);
305 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
308 static void hubread(struct socket *sk, struct fnetnode *fn)
312 char *newbuf, *p1, *p2;
314 size_t datalen, cblen;
317 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
319 if(hub->inbufdata > 1024)
321 bufcat(hub->inbuf, newbuf, datalen);
323 /* Memory eating protection */
324 if(hub->cbdata > 65536)
326 while(hub->inbufdata > 0) {
327 if(hub->cbdata == hub->cbsize)
328 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
330 p2 = (char *)(hub->cb + hub->cbdata);
331 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
332 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
333 memmove(hub->inbuf, p1, hub->inbufdata);
334 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
335 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
338 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
340 if(errno == EILSEQ) {
341 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
344 } else if(errno == EINVAL) {
346 } else if(errno == E2BIG) {
349 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
354 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
356 newqcmd(&hub->queue, parseadc(hub->cb));
357 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
361 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
366 static void hubconnect(struct fnetnode *fn, struct socket *sk)
370 sk->readcb = (void (*)(struct socket *, void *))hubread;
371 sk->errcb = (void (*)(struct socket *, int, void *))huberr;
374 hub = smalloc(sizeof(*hub));
375 memset(hub, 0, sizeof(*hub));
376 getsock(hub->sk = sk);
377 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
378 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
383 sendadc(sk, 0, L"HSUP", L"ADBASE", L"ADTIGR", eoc, NULL);
386 static void hubdestroy(struct fnetnode *fn)
391 iconv_close(hub->ich);
392 if(hub->inbuf != NULL)
399 static void hubkill(struct fnetnode *fn)
407 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
412 static int hubreqconn(struct fnetpeer *peer)
417 static struct fnet adcnet_store = {
418 .connect = hubconnect,
419 .destroy = hubdestroy,
421 .setnick = hubsetnick,
422 .reqconn = hubreqconn,
426 static struct fnet *adcnet = &adcnet_store;
431 struct fnetnode *fn, *nextfn;
436 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
438 if(fn->fnet != adcnet)
440 if((hub = fn->data) == NULL)
442 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
443 if((hub->sk != NULL) && (hub->sk->state == SOCK_EST))
453 static void preinit(int hup)
460 static void makepid(char *idbuf)
466 if((fd = open("/dev/urandom", O_RDONLY)) >= 0) {
467 for(i = 0; i < 24; i += ret) {
468 if((ret = read(fd, idbuf + i, 24 - i)) <= 0) {
469 flog(LOG_WARNING, "could not read random data from /dev/urandom for ADC PID: %s", (errno == 0)?"EOF":strerror(errno));
475 flog(LOG_WARNING, "could not open /dev/urandom: %s", strerror(errno));
478 for(i = 0; i < sizeof(idbuf); i++)
479 idbuf[i] = rand() % 256;
483 static int init(int hup)
485 char idbuf[24], *id32;
491 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
493 id32 = base32encode(idbuf, sizeof(idbuf));
495 privid = icmbstowcs(id32, "us-ascii");
497 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
500 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
502 dotiger(&th, id32, 24);
505 restiger(&th, idbuf);
506 id32 = base32encode(idbuf, sizeof(idbuf));
508 cid = icmbstowcs(id32, "us-ascii");
514 static void terminate(void)
519 static struct configvar myvars[] = {
520 /** Specifies a specific UDP port to use for ADC search
521 * results. If left unspecified, a port is allocated
522 * dynamically. Useful for NAT routers (see also the
523 * net.visibleipv4 address for those cases). */
524 {CONF_VAR_INT, "udpport", {.num = 0}},
525 /** Specifies a specific TCP port to use for ADC peer
526 * connections. If left unspecified, a port is allocated
527 * dynamically. Useful for NAT routers (see also the
528 * net.visibleipv4 address for those cases). */
529 {CONF_VAR_INT, "tcpport", {.num = 0}},
533 static struct module me = {
540 .terminate = terminate,