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, *tcpsock;
86 static wchar_t **parseadc(wchar_t *cmdline)
89 size_t retsize, retdata;
95 retsize = retdata = 0;
98 if((p2 = wcschr(p, L' ')) != NULL)
101 for(ep = p; len > 0; ep++, len--) {
105 } else if(ep[1] == L'n') {
107 } else if(ep[1] == L'\\') {
110 memmove(ep, ep + 2, (len -= 2) * sizeof(*ep));
114 memmove(ep, ep + 1, --len * sizeof(*ep));
119 addtobuf(ret, swcsdup(p));
126 static void sendeoc(struct socket *sk)
128 sockqueue(sk, "\n", 1);
131 static void sendadc1(struct socket *sk, int sep, wchar_t *arg)
135 size_t bufsize, bufdata;
138 bufsize = bufdata = 0;
141 for(; *arg != L'\0'; arg++) {
143 bufcat(buf, L"\\s", 2);
144 else if(*arg == L'\n')
145 bufcat(buf, L"\\n", 2);
146 else if(*arg == L'\\')
147 bufcat(buf, L"\\\\", 2);
151 addtobuf(buf, L'\0');
152 mbsbuf = icwcstombs(buf, "utf-8");
153 sockqueue(sk, mbsbuf, strlen(mbsbuf));
157 static void sendadc(struct socket *sk, int sep, ...)
165 for(i = 0; (arg = va_arg(args, wchar_t *)) != NULL; i++) {
168 } else if(arg == ns) {
170 } else if(arg == fmt) {
171 type = va_arg(args, wchar_t *);
172 if(!wcscmp(type, L"i")) {
173 buf = swprintf2(L"%i", va_arg(args, int));
174 } else if(!wcscmp(type, L"mi")) {
175 buf = swprintf2(L"%ji", va_arg(args, intmax_t));
177 sendadc1(sk, sep, buf);
181 sendadc1(sk, sep, arg);
188 static wchar_t *findkv(wchar_t **argv, wchar_t *name)
190 while(*argv != NULL) {
191 if(!wcsncmp(*argv, name, 2))
197 static struct qcmd *newqcmd(struct qcmdqueue *queue, wchar_t **args)
201 new = smalloc(sizeof(*new));
207 queue->l->next = new;
213 static struct qcmd *ulqcmd(struct qcmdqueue *queue)
217 if((ret = queue->f) == NULL)
219 if((queue->f = ret->next) == NULL)
225 static void freeqcmd(struct qcmd *qcmd)
227 freeparr(qcmd->args);
231 static void sendinf(struct fnetnode *fn)
233 struct adchub *hub = fn->data;
234 struct socket *sk = hub->sk;
235 struct sockaddr_in *a4;
238 sendadc(sk, 0, L"BINF", hub->sid, NULL);
239 sendadc(sk, 1, L"PD", ns, privid, L"ID", ns, cid, NULL);
240 sendadc(sk, 1, L"VEDolda ", ns, icsmbstowcs(VERSION, "us-ascii", NULL), NULL);
241 sendadc(sk, 1, L"NI", ns, fn->mynick, NULL);
242 sendadc(sk, 1, L"SS", ns, fmt, L"mi", (intmax_t)sharesize, L"SF", ns, fmt, L"i", sharedfiles, NULL);
243 if(sk->family == AF_INET)
244 sendadc(sk, 1, L"I40.0.0.0", NULL);
245 else if(sk->family == AF_INET6)
246 sendadc(sk, 1, L"I6::", NULL);
247 sendadc(sk, 1, L"SL", ns, fmt, L"i", confgetint("transfer", "slot"), NULL);
248 if(tcpsock != NULL) {
249 if((sk->family == AF_INET) && !sockgetremotename(udpsock, (struct sockaddr **)&a4, &alen)) {
250 sendadc(sk, 1, L"U4", ns, fmt, L"i", ntohs(a4->sin_port), NULL);
254 sendadc(sk, 1, eoc, NULL);
257 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
259 #define UNUSED __attribute__ ((unused))
264 struct adchub *hub UNUSED = fn->data; \
265 struct socket *sk UNUSED = hub->sk;
272 for(i = 1; i < argc; i++) {
273 if(wcslen(argv[i]) < 3)
275 for(o = 0, f = 0; hub->sup[o]; o++) {
276 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
281 if(!wcsncmp(argv[i], L"AD", 2)) {
284 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
285 hub->sup[o] = swcsdup(argv[i] + 2);
286 } else if(!wcsncmp(argv[i], L"RM", 2)) {
290 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
301 hub->sid = swcsdup(argv[1]);
302 if(hub->state == ADC_PROTOCOL) {
303 hub->state = ADC_IDENTIFY;
314 if((p = findkv(argv, L"NI")) != NULL)
319 static struct command hubcmds[] = {
320 {L"SUP", 1, 0, 0, cmd_sup},
321 {L"SID", 2, 0, 0, cmd_sid},
322 {L"INF", 1, 0, 0, cmd_inf},
323 {NULL, 0, 0, 0, NULL}
326 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
330 wchar_t *cmdnm, *sender, **argv;
332 if((argc = parrlen(argv = qcmd->args)) < 1)
336 if(wcslen(cmdnm) < 2)
339 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
344 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
351 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
352 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
353 if(argc < cmd->minargs)
355 cmd->func(fn, cmdnm, sender, argc, argv);
359 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
362 static void peeraccept(struct socket *sk, struct socket *newsk, void *uudata)
366 static void udpread(struct socket *sk, void *uudata)
370 static void hubread(struct socket *sk, struct fnetnode *fn)
374 char *newbuf, *p1, *p2;
376 size_t datalen, cblen;
379 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
381 if(hub->inbufdata > 1024)
383 bufcat(hub->inbuf, newbuf, datalen);
385 /* Memory eating protection */
386 if(hub->cbdata > 65536)
388 while(hub->inbufdata > 0) {
389 if(hub->cbdata == hub->cbsize)
390 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
392 p2 = (char *)(hub->cb + hub->cbdata);
393 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
394 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
395 memmove(hub->inbuf, p1, hub->inbufdata);
396 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
397 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
400 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
402 if(errno == EILSEQ) {
403 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
406 } else if(errno == EINVAL) {
408 } else if(errno == E2BIG) {
411 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
416 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
418 newqcmd(&hub->queue, parseadc(hub->cb));
419 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
423 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
428 static void hubconnect(struct fnetnode *fn, struct socket *sk)
432 sk->readcb = (void (*)(struct socket *, void *))hubread;
433 sk->errcb = (void (*)(struct socket *, int, void *))huberr;
436 hub = smalloc(sizeof(*hub));
437 memset(hub, 0, sizeof(*hub));
438 getsock(hub->sk = sk);
439 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
440 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
445 sendadc(sk, 0, L"HSUP", L"ADBASE", L"ADTIGR", eoc, NULL);
448 static void hubdestroy(struct fnetnode *fn)
453 iconv_close(hub->ich);
454 if(hub->inbuf != NULL)
461 static void hubkill(struct fnetnode *fn)
469 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
474 static int hubreqconn(struct fnetpeer *peer)
479 static struct fnet adcnet_store = {
480 .connect = hubconnect,
481 .destroy = hubdestroy,
483 .setnick = hubsetnick,
484 .reqconn = hubreqconn,
488 static struct fnet *adcnet = &adcnet_store;
493 struct fnetnode *fn, *nextfn;
498 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
500 if(fn->fnet != adcnet)
502 if((hub = fn->data) == NULL)
504 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
505 if((hub->sk != NULL) && (hub->sk->state == SOCK_EST))
515 static void preinit(int hup)
522 static void makepid(char *idbuf)
528 if((fd = open("/dev/urandom", O_RDONLY)) >= 0) {
529 for(i = 0; i < 24; i += ret) {
530 if((ret = read(fd, idbuf + i, 24 - i)) <= 0) {
531 flog(LOG_WARNING, "could not read random data from /dev/urandom for ADC PID: %s", (errno == 0)?"EOF":strerror(errno));
537 flog(LOG_WARNING, "could not open /dev/urandom: %s", strerror(errno));
540 for(i = 0; i < sizeof(idbuf); i++)
541 idbuf[i] = rand() % 256;
545 static int updateudpport(struct configvar *var, void *uudata)
547 struct sockaddr_in addr;
548 struct socket *newsock;
550 memset(&addr, 0, sizeof(addr));
551 addr.sin_family = AF_INET;
552 addr.sin_port = htons(var->val.num);
553 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
555 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
558 newsock->readcb = udpread;
565 static int updatetcpport(struct configvar *var, void *uudata)
567 struct sockaddr_in addr;
568 struct socket *newsock;
570 memset(&addr, 0, sizeof(addr));
571 addr.sin_family = AF_INET;
572 addr.sin_port = htons(var->val.num);
573 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
574 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
581 static int init(int hup)
583 char idbuf[24], *id32;
585 struct sockaddr_in addr;
592 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
594 id32 = base32encode(idbuf, sizeof(idbuf));
596 privid = icmbstowcs(id32, "us-ascii");
598 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
601 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
603 dotiger(&th, id32, 24);
606 restiger(&th, idbuf);
607 id32 = base32encode(idbuf, sizeof(idbuf));
609 cid = icmbstowcs(id32, "us-ascii");
612 memset(&addr, 0, sizeof(addr));
613 addr.sin_family = AF_INET;
614 addr.sin_port = htons(confgetint("adc", "udpport"));
615 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL) {
616 flog(LOG_CRIT, "could not create ADC UDP socket: %s", strerror(errno));
619 udpsock->readcb = udpread;
620 addr.sin_port = htons(confgetint("adc", "tcpport"));
621 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL) {
622 flog(LOG_CRIT, "could not create ADC TCP socket: %s", strerror(errno));
625 CBREG(confgetvar("adc", "udpport"), conf_update, updateudpport, NULL, NULL);
626 CBREG(confgetvar("adc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
627 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
632 static void terminate(void)
637 static struct configvar myvars[] = {
638 /** Specifies a specific UDP port to use for ADC search
639 * results. If left unspecified, a port is allocated
640 * dynamically. Useful for NAT routers (see also the
641 * net.visibleipv4 address for those cases). */
642 {CONF_VAR_INT, "udpport", {.num = 0}},
643 /** Specifies a specific TCP port to use for ADC peer
644 * connections. If left unspecified, a port is allocated
645 * dynamically. Useful for NAT routers (see also the
646 * net.visibleipv4 address for those cases). */
647 {CONF_VAR_INT, "tcpport", {.num = 0}},
651 static struct module me = {
658 .terminate = terminate,