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, needsid, 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;
81 static wchar_t *eoc, *ns, *fmt;
82 /* I've never understood why both of these are necessary, but... */
83 static wchar_t *privid, *cid;
84 struct socket *udpsock;
85 struct lport *tcpsock;
87 static wchar_t **parseadc(wchar_t *cmdline)
90 size_t retsize, retdata;
96 retsize = retdata = 0;
99 if((p2 = wcschr(p, L' ')) != NULL)
102 for(ep = p; len > 0; ep++, len--) {
106 } else if(ep[1] == L'n') {
108 } else if(ep[1] == L'\\') {
111 memmove(ep, ep + 2, (len -= 2) * sizeof(*ep));
115 memmove(ep, ep + 1, --len * sizeof(*ep));
120 addtobuf(ret, swcsdup(p));
127 static void sendeoc(struct socket *sk)
129 sockqueue(sk, "\n", 1);
132 static void sendadc1(struct socket *sk, int sep, wchar_t *arg)
136 size_t bufsize, bufdata;
139 bufsize = bufdata = 0;
142 for(; *arg != L'\0'; arg++) {
144 bufcat(buf, L"\\s", 2);
145 else if(*arg == L'\n')
146 bufcat(buf, L"\\n", 2);
147 else if(*arg == L'\\')
148 bufcat(buf, L"\\\\", 2);
152 addtobuf(buf, L'\0');
153 mbsbuf = icwcstombs(buf, "utf-8");
154 sockqueue(sk, mbsbuf, strlen(mbsbuf));
158 static void sendadc(struct socket *sk, int sep, ...)
166 for(i = 0; (arg = va_arg(args, wchar_t *)) != NULL; i++) {
169 } else if(arg == ns) {
171 } else if(arg == fmt) {
172 type = va_arg(args, wchar_t *);
173 if(!wcscmp(type, L"i")) {
174 buf = swprintf2(L"%i", va_arg(args, int));
175 } else if(!wcscmp(type, L"mi")) {
176 buf = swprintf2(L"%ji", va_arg(args, intmax_t));
178 sendadc1(sk, sep, buf);
182 sendadc1(sk, sep, arg);
189 static wchar_t *findkv(wchar_t **argv, wchar_t *name)
191 while(*argv != NULL) {
192 if(!wcsncmp(*argv, name, 2))
198 static struct qcmd *newqcmd(struct qcmdqueue *queue, wchar_t **args)
202 new = smalloc(sizeof(*new));
208 queue->l->next = new;
214 static struct qcmd *ulqcmd(struct qcmdqueue *queue)
218 if((ret = queue->f) == NULL)
220 if((queue->f = ret->next) == NULL)
226 static void freeqcmd(struct qcmd *qcmd)
228 freeparr(qcmd->args);
232 static void sendinf(struct fnetnode *fn)
234 struct adchub *hub = fn->data;
235 struct socket *sk = hub->sk;
236 struct sockaddr_in *a4;
239 sendadc(sk, 0, L"BINF", hub->sid, NULL);
240 sendadc(sk, 1, L"PD", ns, privid, L"ID", ns, cid, NULL);
241 sendadc(sk, 1, L"VEDolda ", ns, icsmbstowcs(VERSION, "us-ascii", NULL), NULL);
242 sendadc(sk, 1, L"NI", ns, fn->mynick, NULL);
243 sendadc(sk, 1, L"SS", ns, fmt, L"mi", (intmax_t)sharesize, L"SF", ns, fmt, L"i", sharedfiles, NULL);
244 if(sockfamily(sk) == AF_INET)
245 sendadc(sk, 1, L"I40.0.0.0", NULL);
246 else if(sockfamily(sk) == AF_INET6)
247 sendadc(sk, 1, L"I6::", NULL);
248 sendadc(sk, 1, L"SL", ns, fmt, L"i", confgetint("transfer", "slot"), NULL);
249 if(tcpsock != NULL) {
250 if((sockfamily(sk) == AF_INET) && !sockgetremotename(udpsock, (struct sockaddr **)&a4, &alen)) {
251 sendadc(sk, 1, L"U4", ns, fmt, L"i", ntohs(a4->sin_port), NULL);
255 sendadc(sk, 1, eoc, NULL);
258 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
260 #define UNUSED __attribute__ ((unused))
265 struct adchub *hub UNUSED = fn->data; \
266 struct socket *sk UNUSED = hub->sk;
273 for(i = 1; i < argc; i++) {
274 if(wcslen(argv[i]) < 3)
276 for(o = 0, f = 0; hub->sup[o]; o++) {
277 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
282 if(!wcsncmp(argv[i], L"AD", 2)) {
285 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
286 hub->sup[o] = swcsdup(argv[i] + 2);
287 } else if(!wcsncmp(argv[i], L"RM", 2)) {
291 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
302 hub->sid = swcsdup(argv[1]);
303 if(hub->state == ADC_PROTOCOL) {
304 hub->state = ADC_IDENTIFY;
315 if((p = findkv(argv, L"NI")) != NULL)
320 static struct command hubcmds[] = {
321 {L"SUP", 1, 0, 0, cmd_sup},
322 {L"SID", 2, 0, 0, cmd_sid},
323 {L"INF", 1, 0, 0, cmd_inf},
324 {NULL, 0, 0, 0, NULL}
327 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
331 wchar_t *cmdnm, *sender, **argv;
333 if((argc = parrlen(argv = qcmd->args)) < 1)
337 if(wcslen(cmdnm) < 2)
340 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
345 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
352 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
353 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
354 if(argc < cmd->minargs)
356 cmd->func(fn, cmdnm, sender, argc, argv);
360 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
363 static void peeraccept(struct lport *lp, struct socket *newsk, void *uudata)
367 static void udpread(struct socket *sk, void *uudata)
371 static void hubread(struct socket *sk, struct fnetnode *fn)
375 char *newbuf, *p1, *p2;
377 size_t datalen, cblen;
380 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
382 if(hub->inbufdata > 1024)
384 bufcat(hub->inbuf, newbuf, datalen);
386 /* Memory eating protection */
387 if(hub->cbdata > 65536)
389 while(hub->inbufdata > 0) {
390 if(hub->cbdata == hub->cbsize)
391 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
393 p2 = (char *)(hub->cb + hub->cbdata);
394 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
395 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
396 memmove(hub->inbuf, p1, hub->inbufdata);
397 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
398 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
401 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
403 if(errno == EILSEQ) {
404 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
407 } else if(errno == EINVAL) {
409 } else if(errno == E2BIG) {
412 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
417 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
419 newqcmd(&hub->queue, parseadc(hub->cb));
420 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
424 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
429 static void hubconnect(struct fnetnode *fn, struct socket *sk)
433 sk->readcb = (void (*)(struct socket *, void *))hubread;
434 sk->errcb = (void (*)(struct socket *, int, void *))huberr;
437 hub = smalloc(sizeof(*hub));
438 memset(hub, 0, sizeof(*hub));
439 getsock(hub->sk = sk);
440 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
441 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
446 sendadc(sk, 0, L"HSUP", L"ADBASE", L"ADTIGR", eoc, NULL);
449 static void hubdestroy(struct fnetnode *fn)
454 iconv_close(hub->ich);
455 if(hub->inbuf != NULL)
462 static void hubkill(struct fnetnode *fn)
470 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
475 static int hubreqconn(struct fnetpeer *peer)
480 static struct fnet adcnet_store = {
481 .connect = hubconnect,
482 .destroy = hubdestroy,
484 .setnick = hubsetnick,
485 .reqconn = hubreqconn,
489 static struct fnet *adcnet = &adcnet_store;
494 struct fnetnode *fn, *nextfn;
499 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
501 if(fn->fnet != adcnet)
503 if((hub = fn->data) == NULL)
505 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
506 if((hub->sk != NULL) && (hub->sk->state == SOCK_EST))
516 static void preinit(int hup)
523 static void makepid(char *idbuf)
529 if((fd = open("/dev/urandom", O_RDONLY)) >= 0) {
530 for(i = 0; i < 24; i += ret) {
531 if((ret = read(fd, idbuf + i, 24 - i)) <= 0) {
532 flog(LOG_WARNING, "could not read random data from /dev/urandom for ADC PID: %s", (errno == 0)?"EOF":strerror(errno));
538 flog(LOG_WARNING, "could not open /dev/urandom: %s", strerror(errno));
541 for(i = 0; i < sizeof(idbuf); i++)
542 idbuf[i] = rand() % 256;
546 static int updateudpport(struct configvar *var, void *uudata)
548 struct sockaddr_in addr;
549 struct socket *newsock;
551 memset(&addr, 0, sizeof(addr));
552 addr.sin_family = AF_INET;
553 addr.sin_port = htons(var->val.num);
554 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
556 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
559 newsock->readcb = udpread;
566 static int updatetcpport(struct configvar *var, void *uudata)
568 struct sockaddr_in addr;
569 struct lport *newsock;
571 memset(&addr, 0, sizeof(addr));
572 addr.sin_family = AF_INET;
573 addr.sin_port = htons(var->val.num);
574 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
575 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
582 static int init(int hup)
584 char idbuf[24], *id32;
586 struct sockaddr_in addr;
593 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
595 id32 = base32encode(idbuf, sizeof(idbuf));
597 privid = icmbstowcs(id32, "us-ascii");
599 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
602 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
604 dotiger(&th, id32, 24);
607 restiger(&th, idbuf);
608 id32 = base32encode(idbuf, sizeof(idbuf));
610 cid = icmbstowcs(id32, "us-ascii");
613 memset(&addr, 0, sizeof(addr));
614 addr.sin_family = AF_INET;
615 addr.sin_port = htons(confgetint("adc", "udpport"));
616 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL) {
617 flog(LOG_CRIT, "could not create ADC UDP socket: %s", strerror(errno));
620 udpsock->readcb = udpread;
621 addr.sin_port = htons(confgetint("adc", "tcpport"));
622 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL) {
623 flog(LOG_CRIT, "could not create ADC TCP socket: %s", strerror(errno));
626 CBREG(confgetvar("adc", "udpport"), conf_update, updateudpport, NULL, NULL);
627 CBREG(confgetvar("adc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
628 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
633 static void terminate(void)
638 static struct configvar myvars[] = {
639 /** Specifies a specific UDP port to use for ADC search
640 * results. If left unspecified, a port is allocated
641 * dynamically. Useful for NAT routers (see also the
642 * net.visibleipv4 address for those cases). */
643 {CONF_VAR_INT, "udpport", {.num = 0}},
644 /** Specifies a specific TCP port to use for ADC peer
645 * connections. If left unspecified, a port is allocated
646 * dynamically. Useful for NAT routers (see also the
647 * net.visibleipv4 address for those cases). */
648 {CONF_VAR_INT, "tcpport", {.num = 0}},
652 static struct module me = {
659 .terminate = terminate,