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;
74 /* I've never understood why both of these are necessary, but... */
75 static wchar_t *privid, *cid;
77 static wchar_t **parseadc(wchar_t *cmdline)
80 size_t retsize, retdata;
86 retsize = retdata = 0;
89 if((p2 = wcschr(p, L' ')) != NULL)
92 for(ep = p; len > 0; ep++, len--) {
96 } else if(ep[1] == L'n') {
98 } else if(ep[1] == L'\\') {
101 memmove(ep, ep + 2, (len -= 2) * sizeof(*ep));
105 memmove(ep, ep + 1, --len * sizeof(*ep));
110 addtobuf(ret, swcsdup(p));
117 static void sendeoc(struct socket *sk)
119 sockqueue(sk, "\n", 1);
122 static void sendadc1(struct socket *sk, int sep, wchar_t *arg)
126 size_t bufsize, bufdata;
129 bufsize = bufdata = 0;
132 for(; *arg != L'\0'; arg++) {
134 bufcat(buf, L"\\s", 2);
135 else if(*arg == L'\n')
136 bufcat(buf, L"\\n", 2);
137 else if(*arg == L'\\')
138 bufcat(buf, L"\\\\", 2);
142 addtobuf(buf, L'\0');
143 mbsbuf = icwcstombs(buf, "utf-8");
144 sockqueue(sk, mbsbuf, strlen(mbsbuf));
148 static void sendadc(struct socket *sk, int sep, ...)
155 for(i = 0; (arg = va_arg(args, wchar_t *)) != NULL; i++) {
159 sendadc1(sk, (i == 0)?sep:1, arg);
164 static struct qcmd *newqcmd(struct qcmd **queue, wchar_t **args)
168 while(*queue != NULL)
169 queue = &((*queue)->next);
170 new = smalloc(sizeof(*new));
177 static struct qcmd *ulqcmd(struct qcmd **queue)
181 if((ret = *queue) == NULL)
187 static void freeqcmd(struct qcmd *qcmd)
189 freeparr(qcmd->args);
193 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
194 #define ADC_CMDCOM struct socket *sk = fn->sk; struct adchub *hub = fn->data;
201 for(i = 1; i < argc; i++) {
202 if(wcslen(argv[i]) < 3)
204 for(o = 0, f = 0; hub->sup[o]; o++) {
205 if(!wcscmp(argv[i] + 2, hub->sup[o])) {
210 if(!wcsncmp(argv[i], L"AD", 2)) {
213 hub->sup = srealloc(hub->sup, sizeof(*(hub->sup)) * (o + 1));
214 hub->sup[o] = swcsdup(argv[i] + 2);
215 } else if(!wcsncmp(argv[i], L"RM", 2)) {
218 memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
229 hub->sid = swcsdup(argv[1]);
230 if(hub->state == ADC_PROTOCOL) {
231 hub->state = ADC_IDENTIFY;
235 static struct command hubcmds[] = {
236 {L"SUP", 1, 0, cmd_sup},
237 {L"SID", 2, 0, cmd_sid},
241 static void dispatch(struct qcmd *qcmd, struct fnetnode *fn)
245 wchar_t *cmdnm, *sender, **argv;
247 if((argc = parrlen(argv = qcmd->args)) < 1)
251 if(wcslen(cmdnm) < 2)
254 if((*cmdnm == L'B') || (*cmdnm == L'F')) {
259 } else if((*cmdnm == L'D') || (*cmdnm == L'E')) {
266 for(cmd = hubcmds; cmd->func != NULL; cmd++) {
267 if(!wcscmp(cmd->name, qcmd->args[0] + 1)) {
268 if(argc < cmd->minargs)
270 cmd->func(fn, cmdnm, sender, argc, qcmd->args);
274 flog(LOG_DEBUG, "unknown adc command: %ls", qcmd->args[0]);
277 static void hubread(struct socket *sk, struct fnetnode *fn)
281 char *newbuf, *p1, *p2;
283 size_t datalen, cblen;
286 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
288 if(hub->inbufdata > 1024)
290 bufcat(hub->inbuf, newbuf, datalen);
292 /* Memory eating protection */
293 if(hub->cbdata > 65536)
295 while(hub->inbufdata > 0) {
296 if(hub->cbdata == hub->cbsize)
297 sizebuf2(hub->cb, hub->cbdata + datalen, 1);
299 p2 = (char *)(hub->cb + hub->cbdata);
300 cblen = sizeof(*(hub->cb)) * (hub->cbsize - hub->cbdata);
301 ret = iconv(hub->ich, &p1, &hub->inbufdata, &p2, &cblen);
302 memmove(hub->inbuf, p1, hub->inbufdata);
303 if(((p2 - ((char *)hub->cb)) % sizeof(*hub->cb)) != 0) {
304 flog(LOG_CRIT, "Spotted a dismembered wchar_t!");
307 hub->cbdata = hub->cbsize - (cblen / sizeof(*(hub->cb)));
309 if(errno == EILSEQ) {
310 flog(LOG_DEBUG, "adc fnetnode %i sent illegal utf-8 sequence", fn->id);
313 } else if(errno == EINVAL) {
315 } else if(errno == E2BIG) {
318 flog(LOG_WARNING, "bug? iconv returned unexpected error: %s", strerror(errno));
323 while((p = wmemchr(hub->cb, L'\n', hub->cbdata)) != NULL) {
325 newqcmd(&hub->queue, parseadc(hub->cb));
326 memmove(hub->cb, p, (hub->cbdata -= (p - hub->cb)) * sizeof(*(hub->cb)));
330 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
335 static void hubconnect(struct fnetnode *fn)
339 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
340 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
344 hub = smalloc(sizeof(*hub));
345 memset(hub, 0, sizeof(*hub));
346 if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
347 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
352 sendadc(fn->sk, 0, L"HSUP", L"ADBASE", eoc, NULL);
355 static void hubdestroy(struct fnetnode *fn)
359 if((hub = fn->data) == NULL)
361 iconv_close(hub->ich);
362 if(hub->inbuf != NULL)
369 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
374 static int hubreqconn(struct fnetpeer *peer)
379 static struct fnet adcnet_store = {
380 .connect = hubconnect,
381 .destroy = hubdestroy,
382 .setnick = hubsetnick,
383 .reqconn = hubreqconn,
387 static struct fnet *adcnet = &adcnet_store;
392 struct fnetnode *fn, *nextfn;
397 for(fn = fnetnodes; fn != NULL; fn = nextfn) {
399 if(fn->fnet != adcnet)
401 if((hub = fn->data) == NULL)
403 if((qcmd = ulqcmd(&hub->queue)) != NULL) {
404 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
414 static void preinit(int hup)
421 static int init(int hup)
424 char idbuf[24], *id32;
430 if((privid = fetchvar("adc.pid", NULL)) == NULL) {
431 for(i = 0; i < sizeof(idbuf); i++)
432 idbuf[i] = rand() % 256;
433 id32 = base32encode(idbuf, sizeof(idbuf));
435 privid = icmbstowcs(id32, "us-ascii");
437 storevar("adc.pid", privid, sizeof(*privid) * (wcslen(privid) + 1));
440 id32 = base32decode(icswcstombs(privid, "us-ascii", NULL), NULL);
442 dotiger(&th, id32, 24);
445 restiger(&th, idbuf);
446 id32 = base32encode(idbuf, sizeof(idbuf));
448 cid = icmbstowcs(id32, "us-ascii");
454 static void terminate(void)
459 static struct configvar myvars[] = {
460 {CONF_VAR_INT, "udpport", {.num = 0}},
461 {CONF_VAR_INT, "tcpport", {.num = 0}},
465 static struct module me = {
472 .terminate = terminate,