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
21 * Note: This code is still ugly, since I copied it almost verbatim
22 * from the daemon's parser. It would need serious cleanups, but at
23 * least it works for now.
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #include <arpa/nameser.h>
49 #include <doldaconnect/uilib.h>
50 #include <doldaconnect/utils.h>
60 struct respclass *next;
70 struct respclass *classes;
80 int (*callback)(struct dc_response *resp);
84 void dc_uimisc_disconnected(void);
86 /* The first command must be the nameless connect command and second
87 * the notification command. */
88 static struct command *commands = NULL;
89 static struct qcmd *queue = NULL, *queueend = NULL;
90 static struct dc_response *respqueue = NULL, *respqueueend = NULL;
91 static int state = -1;
93 static iconv_t ichandle;
94 static int resetreader = 1;
95 static char *dchostname = NULL;
96 static struct addrinfo *hostlist = NULL, *curhost = NULL;
99 static struct dc_response *makeresp(void)
101 struct dc_response *new;
103 new = smalloc(sizeof(*new));
116 static void freeqcmd(struct qcmd *qcmd)
118 if(qcmd->buf != NULL)
123 static void unlinkqueue(void)
127 if((qcmd = queue) == NULL)
135 static struct qcmd *makeqcmd(wchar_t *name)
145 for(cmd = commands; cmd != NULL; cmd = cmd->next)
147 if((cmd->name != NULL) && !wcscmp(cmd->name, name))
153 new = smalloc(sizeof(*new));
158 new->callback = NULL;
163 queue = queueend = new;
165 queueend->next = new;
171 static wchar_t *quoteword(wchar_t *word)
173 wchar_t *wp, *buf, *bp;
183 for(wp = word; *wp != L'\0'; wp++)
185 if(!dq && iswspace(*wp))
187 if((*wp == L'\\') || (*wp == L'\"'))
194 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
197 for(wp = word; *wp != L'\0'; wp++)
199 if((*wp == L'\\') || (*wp == L'\"'))
209 static struct command *makecmd(wchar_t *name)
213 new = smalloc(sizeof(*new));
216 new->next = commands;
221 static struct respclass *addresp(struct command *cmd, int code, ...)
223 struct respclass *new;
228 va_start(args, code);
230 while((resps[i++] = va_arg(args, int)) != RESP_END);
233 new = smalloc(sizeof(*new));
238 new->wordt = smalloc(sizeof(int) * i);
239 memcpy(new->wordt, resps, sizeof(int) * i);
243 new->next = cmd->classes;
248 #include "initcmds.h"
252 if((ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
258 void dc_cleanup(void)
260 iconv_close(ichandle);
263 void dc_disconnect(void)
265 struct dc_response *resp;
273 while((resp = dc_getresp()) != NULL)
275 dc_uimisc_disconnected();
276 if(dchostname != NULL)
281 void dc_freeresp(struct dc_response *resp)
285 for(i = 0; i < resp->numlines; i++)
287 for(o = 0; o < resp->rlines[i].argc; o++)
288 free(resp->rlines[i].argv[o]);
289 free(resp->rlines[i].argv);
295 struct dc_response *dc_getresp(void)
297 struct dc_response *ret;
299 if((ret = respqueue) == NULL)
301 respqueue = ret->next;
302 if(respqueue == NULL)
305 respqueue->prev = NULL;
309 struct dc_response *dc_gettaggedresp(int tag)
311 struct dc_response *resp;
313 for(resp = respqueue; resp != NULL; resp = resp->next)
317 if(resp->prev != NULL)
318 resp->prev->next = resp->next;
319 if(resp->next != NULL)
320 resp->next->prev = resp->prev;
321 if(resp == respqueue)
322 respqueue = resp->next;
323 if(resp == respqueueend)
324 respqueueend = resp->prev;
331 struct dc_response *dc_gettaggedrespsync(int tag)
334 struct dc_response *resp;
336 while((resp = dc_gettaggedresp(tag)) == NULL)
341 pfd.events |= POLLOUT;
342 if(poll(&pfd, 1, -1) < 0)
344 if((pfd.revents & POLLIN) && dc_handleread())
346 if((pfd.revents & POLLOUT) && dc_handlewrite())
352 int dc_wantwrite(void)
357 if((queue != NULL) && (queue->buflen > 0))
364 int dc_getstate(void)
369 int dc_queuecmd(int (*callback)(struct dc_response *), void *data, ...)
377 wchar_t *part, *tpart;
378 size_t bufsize, bufdata;
381 bufsize = bufdata = 0;
384 while((part = va_arg(al, wchar_t *)) != NULL)
386 if(!wcscmp(part, L"%%a"))
388 for(toks = va_arg(al, wchar_t **); *toks != NULL; toks++)
392 if((tpart = quoteword(part)) != NULL)
398 bufcat(buf, part, wcslen(part));
406 /* This demands that all arguments that are passed to the
407 * function are of equal length, that of an int. I know
408 * that GCC does that on IA32 platforms, but I do not know
409 * which other platforms and compilers that it applies
410 * to. If this breaks your platform, please mail me about
413 part = vswprintf2(tpart = (part + 1), al);
414 for(; *tpart != L'\0'; tpart++)
428 if((tpart = quoteword(part)) != NULL)
439 if((qcmd = makeqcmd(part)) == NULL)
447 qcmd->callback = callback;
451 bufcat(buf, part, wcslen(part));
457 bufcat(buf, L"\r\n\0", 3);
458 if((final = icwcstombs(buf, "utf-8")) == NULL)
466 qcmd->buflen = strlen(final);
470 int dc_handleread(void)
476 /* Ewww... this really is soo ugly. I need to clean this up some day. */
477 static int pstate = 0;
478 static char inbuf[128];
479 static size_t inbufdata = 0;
480 static wchar_t *cbuf = NULL;
481 static size_t cbufsize = 0, cbufdata = 0;
482 static wchar_t *pptr = NULL;
483 static wchar_t **argv = NULL;
484 static int argc = 0, args = 0;
485 static wchar_t *cw = NULL;
486 static size_t cwsize = 0, cwdata = 0;
487 static struct dc_response *curresp = NULL;
489 static int unlink = 0;
497 getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
501 struct sockaddr_storage addr;
502 struct sockaddr_in *ipv4;
503 struct sockaddr_in6 *ipv6;
505 for(curhost = curhost->ai_next; curhost != NULL; curhost = curhost->ai_next)
507 if((newfd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0)
516 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
517 memcpy(&addr, curhost->ai_addr, curhost->ai_addrlen);
518 if(addr.ss_family == AF_INET)
520 ipv4 = (struct sockaddr_in *)&addr;
521 ipv4->sin_port = htons(servport);
524 if(addr.ss_family == AF_INET6)
526 ipv6 = (struct sockaddr_in6 *)&addr;
527 ipv6->sin6_port = htons(servport);
530 if(connect(fd, (struct sockaddr *)&addr, curhost->ai_addrlen))
532 if(errno == EINPROGRESS)
558 dc_freeresp(curresp);
563 ret = read(fd, inbuf + inbufdata, 128 - inbufdata);
566 if((errno == EAGAIN) || (errno == EINTR))
572 } else if(ret == 0) {
581 if(cbufsize == cbufdata)
585 if((cbuf = realloc(cbuf, sizeof(wchar_t) * (cbufsize += 256))) == NULL)
595 p2 = (char *)(cbuf + cbufdata);
596 len = sizeof(wchar_t) * (cbufsize - cbufdata);
597 ret = iconv(ichandle, &p1, &inbufdata, &p2, &len);
598 memmove(inbuf, p1, inbufdata);
599 cbufdata = cbufsize - (len / sizeof(wchar_t));
605 /* XXX Is this really OK? */
627 while(!done && (pptr - cbuf < cbufdata))
636 if(pptr == cbuf + cbufdata - 1)
641 if(*(++pptr) == L'\n')
645 curresp = makeresp();
646 if((argc > 0) && ((curresp->code = wcstol(argv[0], NULL, 10)) >= 600))
648 curresp->cmdname = L".notify";
649 curresp->internal = commands->next;
653 if((curresp->cmdname = queue->cmd->name) == NULL)
654 curresp->cmdname = L".connect";
655 curresp->data = queue->data;
656 curresp->tag = queue->tag;
657 curresp->internal = (void *)(queue->cmd);
661 sizebuf(&curresp->rlines, &curresp->linessize, curresp->numlines + 1, sizeof(*(curresp->rlines)), 1);
662 curresp->rlines[curresp->numlines].argc = argc;
663 curresp->rlines[curresp->numlines].argv = argv;
669 if((curresp->code >= 600) || (queue == NULL) || (queue->callback == NULL))
672 ret = queue->callback(curresp);
675 if(respqueue == NULL)
677 respqueue = respqueueend = curresp;
679 curresp->next = NULL;
680 curresp->prev = respqueueend;
681 respqueueend->next = curresp;
682 respqueueend = curresp;
684 } else if(ret == 1) {
685 dc_freeresp(curresp);
691 wmemmove(cbuf, pptr, cbufdata -= (pptr - cbuf));
705 if(iswspace(*pptr) || ((argc == 0) && (*pptr == L'-')))
718 sizebuf(&argv, &args, argc + 1, sizeof(*argv), 1);
723 } else if(*pptr == L'\"') {
726 } else if(*pptr == L'\\') {
727 if(pptr == cbuf + cbufdata - 1)
732 addtobuf(cw, *(++pptr));
735 addtobuf(cw, *(pptr++));
742 } else if(*pptr == L'\\') {
743 addtobuf(cw, *(++pptr));
756 int dc_handlewrite(void)
764 if(queue->buflen > 0)
766 ret = send(fd, queue->buf, queue->buflen, MSG_NOSIGNAL | MSG_DONTWAIT);
769 if((errno == EAGAIN) || (errno == EINTR))
777 memmove(queue->buf, queue->buf + ret, queue->buflen -= ret);
785 static char *readname(unsigned char *msg, unsigned char *eom, unsigned char **p)
789 size_t namesize, namedata, len;
792 namesize = namedata = 0;
800 } else if(len == 0xc0) {
801 tp = msg + *((*p)++);
802 if((tname = readname(msg, eom, &tp)) == NULL)
808 bufcat(name, tname, strlen(tname));
812 } else if(*p + len >= eom) {
817 bufcat(name, *p, len);
823 static int skipname(unsigned char *msg, unsigned char *eom, unsigned char **p)
833 } else if(len == 0xc0) {
836 } else if(*p + len >= eom) {
843 static int getsrvrr(char *name, char **host, int *port)
846 char *name2, *rrname;
847 unsigned char *eom, *p;
848 unsigned char buf[1024];
849 int flags, num, class, type;
853 if(!(_res.options & RES_INIT))
858 /* res_querydomain doesn't work for some reason */
859 name2 = smalloc(strlen("_dolcon._tcp.") + strlen(name) + 2);
860 strcpy(name2, "_dolcon._tcp.");
863 if(name2[len - 1] != '.')
868 ret = res_query(name2, C_IN, T_SRV, buf, sizeof(buf));
875 flags = (buf[2] << 8) + buf[3];
876 if((flags & 0xfa0f) != 0x8000)
881 num = (buf[4] << 8) + buf[5];
883 for(i = 0; i < num; i++)
885 if(skipname(buf, eom, &p))
892 num = (buf[6] << 8) + buf[7];
893 for(i = 0; i < num; i++)
895 if((rrname = readname(buf, eom, &p)) == NULL)
907 if((class == C_IN) && (type == T_SRV) && !strcmp(rrname, name2))
911 /* Noone will want to have alternative DC servers, so
912 * don't care about priority and weigth */
923 if((rrname = readname(buf, eom, &p)) == NULL)
936 static int getsrvrr(char *name, char **host, int *port)
943 int dc_connect(char *host, int port)
945 struct addrinfo hint;
946 struct sockaddr_storage addr;
947 struct sockaddr_in *ipv4;
949 struct sockaddr_in6 *ipv6;
953 int getsrv, freehost;
967 memset(&hint, 0, sizeof(hint));
968 hint.ai_socktype = SOCK_STREAM;
971 if(!getsrvrr(host, &newhost, &port))
979 freeaddrinfo(hostlist);
980 if(getaddrinfo(host, NULL, &hint, &hostlist))
987 for(curhost = hostlist; curhost != NULL; curhost = curhost->ai_next)
989 if((fd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0)
997 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
998 memcpy(&addr, curhost->ai_addr, curhost->ai_addrlen);
999 if(addr.ss_family == AF_INET)
1001 ipv4 = (struct sockaddr_in *)&addr;
1002 ipv4->sin_port = htons(port);
1005 if(addr.ss_family == AF_INET6)
1007 ipv6 = (struct sockaddr_in6 *)&addr;
1008 ipv6->sin6_port = htons(port);
1011 if(connect(fd, (struct sockaddr *)&addr, curhost->ai_addrlen))
1013 if(errno == EINPROGRESS)
1025 qcmd = makeqcmd(NULL);
1027 if(dchostname != NULL)
1029 dchostname = sstrdup(host);
1035 struct dc_intresp *dc_interpret(struct dc_response *resp)
1038 struct dc_intresp *iresp;
1039 struct command *cmd;
1040 struct respclass *cls;
1044 if((resp->numlines == 0) || (resp->rlines[0].argc == 0) || (resp->curline >= resp->numlines))
1046 code = wcstol(resp->rlines[0].argv[0], NULL, 10);
1047 cmd = (struct command *)(resp->internal);
1048 for(cls = cmd->classes; cls != NULL; cls = cls->next)
1050 if(cls->code == code)
1055 if(cls->nwords >= resp->rlines[resp->curline].argc)
1057 iresp = smalloc(sizeof(*iresp));
1062 for(i = 0; i < cls->nwords; i++)
1064 switch(cls->wordt[i])
1069 sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1);
1070 iresp->argv[iresp->argc].val.str = swcsdup(resp->rlines[resp->curline].argv[i + 1]);
1071 iresp->argv[iresp->argc].type = cls->wordt[i];
1075 sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1);
1076 iresp->argv[iresp->argc].val.num = wcstol(resp->rlines[resp->curline].argv[i + 1], NULL, 0);
1077 iresp->argv[iresp->argc].type = cls->wordt[i];
1081 sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1);
1082 iresp->argv[iresp->argc].val.flnum = wcstod(resp->rlines[resp->curline].argv[i + 1], NULL);
1083 iresp->argv[iresp->argc].type = cls->wordt[i];
1092 void dc_freeires(struct dc_intresp *ires)
1096 for(i = 0; i < ires->argc; i++)
1098 if(ires->argv[i].type == RESP_STR)
1099 free(ires->argv[i].val.str);
1105 const char *dc_gethostname(void)