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);
63 size_t inbufdata, inbufsize;
66 size_t cbdata, cbsize;
70 struct wcspair *hubinf;
75 /* I've never understood why both of these are necessary, but... */
76 static wchar_t *privid, *cid;
78 static wchar_t **parseadc(wchar_t *cmdline)
81 size_t retsize, retdata;
87 retsize = retdata = 0;
90 if((p2 = wcschr(p, L' ')) != NULL)
93 for(ep = p; len > 0; ep++, len--) {
97 } else if(ep[1] == L'n') {
99 } else if(ep[1] == L'\\') {
102 memmove(ep, ep + 2, (len -= 2) * sizeof(*ep));
106 memmove(ep, ep + 1, --len * sizeof(*ep));
111 addtobuf(ret, swcsdup(p));
118 static void sendeoc(struct socket *sk)
120 sockqueue(sk, "\n", 1);
123 static void sendadc1(struct socket *sk, int sep, wchar_t *arg)
127 size_t bufsize, bufdata;
130 bufsize = bufdata = 0;
133 for(; *arg != L'\0'; arg++) {
135 bufcat(buf, L"\\s", 2);
136 else if(*arg == L'\n')
137 bufcat(buf, L"\\n", 2);
138 else if(*arg == L'\\')
139 bufcat(buf, L"\\\\", 2);
143 addtobuf(buf, L'\0');
144 mbsbuf = icwcstombs(buf, "utf-8");
145 sockqueue(sk, mbsbuf, strlen(mbsbuf));
149 static void sendadc(struct socket *sk, int sep, ...)
156 for(i = 0; (arg = va_arg(args, wchar_t *)) != NULL; i++) {
160 sendadc1(sk, (i == 0)?sep:1, arg);
165 static struct qcmd *newqcmd(struct qcmd **queue, wchar_t **args)
169 while(*queue != NULL)
170 queue = &((*queue)->next);
171 new = smalloc(sizeof(*new));
178 static struct qcmd *ulqcmd(struct qcmd **queue)
182 if((ret = *queue) == NULL)
188 static void freeqcmd(struct qcmd *qcmd)
190 freeparr(qcmd->args);
194 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
196 #define UNUSED __attribute__ ((unused))
201 struct socket *sk UNUSED = fn->sk; \
202 struct adchub *hub UNUSED = fn->data;
209 for(i = 1; i < argc; i++) {
210 if(wcslen(argv[i]) < 3)
212 for(o = 0, f = 0; hub->sup[o]; o++) {
213 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
218 if(!wcsncmp(argv[i], L"AD", 2)) {
221 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
222 hub->sup[o] = swcsdup(argv[i] + 2);
223 } else if(!wcsncmp(argv[i], L"RM", 2)) {
227 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
238 hub->sid = swcsdup(argv[1]);
239 if(hub->state == ADC_PROTOCOL) {
240 hub->state = ADC_IDENTIFY;
253 static struct command hubcmds[] = {
254 {L"SUP", 1, 0, cmd_sup},
255 {L"SID", 2, 0, cmd_sid},
256 {L"INF", 0, 0, cmd_inf},
260 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
264 wchar_t *cmdnm, *sender, **argv;
266 if((argc = parrlen(argv = qcmd->args)) < 1)
270 if(wcslen(cmdnm) < 2)
273 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
278 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
285 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
286 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
287 if(argc < cmd->minargs)
289 cmd->func(fn, cmdnm, sender, argc, qcmd->args);
293 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
296 static void hubread(struct socket *sk, struct fnetnode *fn)
300 char *newbuf, *p1, *p2;
302 size_t datalen, cblen;
305 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
307 if(hub->inbufdata > 1024)
309 bufcat(hub->inbuf, newbuf, datalen);
311 /* Memory eating protection */
312 if(hub->cbdata > 65536)
314 while(hub->inbufdata > 0) {
315 if(hub->cbdata == hub->cbsize)
316 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
318 p2 = (char *)(hub->cb + hub->cbdata);
319 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
320 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
321 memmove(hub->inbuf, p1, hub->inbufdata);
322 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
323 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
326 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
328 if(errno == EILSEQ) {
329 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
332 } else if(errno == EINVAL) {
334 } else if(errno == E2BIG) {
337 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
342 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
344 newqcmd(&hub->queue, parseadc(hub->cb));
345 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
349 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
354 static void hubconnect(struct fnetnode *fn)
358 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
359 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
363 hub = smalloc(sizeof(*hub));
364 memset(hub, 0, sizeof(*hub));
365 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
366 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
371 sendadc(fn->sk, 0, L"HSUP", L"ADBASE", eoc, NULL);
374 static void hubdestroy(struct fnetnode *fn)
378 if((hub = fn->data) == NULL)
380 iconv_close(hub->ich);
381 if(hub->inbuf != NULL)
388 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
393 static int hubreqconn(struct fnetpeer *peer)
398 static struct fnet adcnet_store = {
399 .connect = hubconnect,
400 .destroy = hubdestroy,
401 .setnick = hubsetnick,
402 .reqconn = hubreqconn,
406 static struct fnet *adcnet = &adcnet_store;
411 struct fnetnode *fn, *nextfn;
416 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
418 if(fn->fnet != adcnet)
420 if((hub = fn->data) == NULL)
422 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
423 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
433 static void preinit(int hup)
440 static int init(int hup)
443 char idbuf[24], *id32;
449 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
450 for(i = 0; i < sizeof(idbuf); i++)
451 idbuf[i] = rand() % 256;
452 id32 = base32encode(idbuf, sizeof(idbuf));
454 privid = icmbstowcs(id32, "us-ascii");
456 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
459 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
461 dotiger(&th, id32, 24);
464 restiger(&th, idbuf);
465 id32 = base32encode(idbuf, sizeof(idbuf));
467 cid = icmbstowcs(id32, "us-ascii");
473 static void terminate(void)
478 static struct configvar myvars[] = {
479 /** Specifies a specific UDP port to use for ADC search
480 * results. If left unspecified, a port is allocated
481 * dynamically. Useful for NAT routers (see also the
482 * net.visibleipv4 address for those cases). */
483 {CONF_VAR_INT, "udpport", {.num = 0}},
484 /** Specifies a specific TCP port to use for ADC peer
485 * connections. If left unspecified, a port is allocated
486 * dynamically. Useful for NAT routers (see also the
487 * net.visibleipv4 address for those cases). */
488 {CONF_VAR_INT, "tcpport", {.num = 0}},
492 static struct module me = {
499 .terminate = terminate,