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)
195 #define ADC_CMDCOM struct socket *sk = fn->sk; struct adchub *hub = fn->data;
202 for(i = 1; i < argc; i++) {
203 if(wcslen(argv[i]) < 3)
205 for(o = 0, f = 0; hub->sup[o]; o++) {
206 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
211 if(!wcsncmp(argv[i], L"AD", 2)) {
214 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
215 hub->sup[o] = swcsdup(argv[i] + 2);
216 } else if(!wcsncmp(argv[i], L"RM", 2)) {
220 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
231 hub->sid = swcsdup(argv[1]);
232 if(hub->state == ADC_PROTOCOL) {
233 hub->state = ADC_IDENTIFY;
246 static struct command hubcmds[] = {
247 {L"SUP", 1, 0, cmd_sup},
248 {L"SID", 2, 0, cmd_sid},
249 {L"INF", 0, 0, cmd_inf},
253 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
257 wchar_t *cmdnm, *sender, **argv;
259 if((argc = parrlen(argv = qcmd->args)) < 1)
263 if(wcslen(cmdnm) < 2)
266 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
271 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
278 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
279 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
280 if(argc < cmd->minargs)
282 cmd->func(fn, cmdnm, sender, argc, qcmd->args);
286 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
289 static void hubread(struct socket *sk, struct fnetnode *fn)
293 char *newbuf, *p1, *p2;
295 size_t datalen, cblen;
298 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
300 if(hub->inbufdata > 1024)
302 bufcat(hub->inbuf, newbuf, datalen);
304 /* Memory eating protection */
305 if(hub->cbdata > 65536)
307 while(hub->inbufdata > 0) {
308 if(hub->cbdata == hub->cbsize)
309 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
311 p2 = (char *)(hub->cb + hub->cbdata);
312 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
313 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
314 memmove(hub->inbuf, p1, hub->inbufdata);
315 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
316 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
319 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
321 if(errno == EILSEQ) {
322 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
325 } else if(errno == EINVAL) {
327 } else if(errno == E2BIG) {
330 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
335 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
337 newqcmd(&hub->queue, parseadc(hub->cb));
338 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
342 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
347 static void hubconnect(struct fnetnode *fn)
351 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
352 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
356 hub = smalloc(sizeof(*hub));
357 memset(hub, 0, sizeof(*hub));
358 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
359 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
364 sendadc(fn->sk, 0, L"HSUP", L"ADBASE", eoc, NULL);
367 static void hubdestroy(struct fnetnode *fn)
371 if((hub = fn->data) == NULL)
373 iconv_close(hub->ich);
374 if(hub->inbuf != NULL)
381 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
386 static int hubreqconn(struct fnetpeer *peer)
391 static struct fnet adcnet_store = {
392 .connect = hubconnect,
393 .destroy = hubdestroy,
394 .setnick = hubsetnick,
395 .reqconn = hubreqconn,
399 static struct fnet *adcnet = &adcnet_store;
404 struct fnetnode *fn, *nextfn;
409 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
411 if(fn->fnet != adcnet)
413 if((hub = fn->data) == NULL)
415 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
416 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
426 static void preinit(int hup)
433 static int init(int hup)
436 char idbuf[24], *id32;
442 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
443 for(i = 0; i < sizeof(idbuf); i++)
444 idbuf[i] = rand() % 256;
445 id32 = base32encode(idbuf, sizeof(idbuf));
447 privid = icmbstowcs(id32, "us-ascii");
449 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
452 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
454 dotiger(&th, id32, 24);
457 restiger(&th, idbuf);
458 id32 = base32encode(idbuf, sizeof(idbuf));
460 cid = icmbstowcs(id32, "us-ascii");
466 static void terminate(void)
471 static struct configvar myvars[] = {
472 {CONF_VAR_INT, "udpport", {.num = 0}},
473 {CONF_VAR_INT, "tcpport", {.num = 0}},
477 static struct module me = {
484 .terminate = terminate,