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)
477 /* Ewww... this really is soo ugly. I need to clean this up some day. */
478 static int pstate = 0;
479 static char inbuf[128];
480 static size_t inbufdata = 0;
481 static wchar_t *cbuf = NULL;
482 static size_t cbufsize = 0, cbufdata = 0;
483 static wchar_t *pptr = NULL;
484 static wchar_t **argv = NULL;
486 static size_t args = 0;
487 static wchar_t *cw = NULL;
488 static size_t cwsize = 0, cwdata = 0;
489 static struct dc_response *curresp = NULL;
491 static int unlink = 0;
498 optlen = sizeof(ret);
499 getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
503 struct sockaddr_storage addr;
504 struct sockaddr_in *ipv4;
505 struct sockaddr_in6 *ipv6;
507 for(curhost = curhost->ai_next; curhost != NULL; curhost = curhost->ai_next)
509 if((newfd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0)
518 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
519 memcpy(&addr, curhost->ai_addr, curhost->ai_addrlen);
520 if(addr.ss_family == AF_INET)
522 ipv4 = (struct sockaddr_in *)&addr;
523 ipv4->sin_port = htons(servport);
526 if(addr.ss_family == AF_INET6)
528 ipv6 = (struct sockaddr_in6 *)&addr;
529 ipv6->sin6_port = htons(servport);
532 if(connect(fd, (struct sockaddr *)&addr, curhost->ai_addrlen))
534 if(errno == EINPROGRESS)
560 dc_freeresp(curresp);
565 ret = read(fd, inbuf + inbufdata, 128 - inbufdata);
568 if((errno == EAGAIN) || (errno == EINTR))
574 } else if(ret == 0) {
583 if(cbufsize == cbufdata)
587 if((cbuf = realloc(cbuf, sizeof(wchar_t) * (cbufsize += 256))) == NULL)
597 p2 = (char *)(cbuf + cbufdata);
598 len = sizeof(wchar_t) * (cbufsize - cbufdata);
599 ret = iconv(ichandle, &p1, &inbufdata, &p2, &len);
600 memmove(inbuf, p1, inbufdata);
601 cbufdata = cbufsize - (len / sizeof(wchar_t));
607 /* XXX Is this really OK? */
629 while(!done && (pptr - cbuf < cbufdata))
638 if(pptr == cbuf + cbufdata - 1)
643 if(*(++pptr) == L'\n')
647 curresp = makeresp();
648 if((argc > 0) && ((curresp->code = wcstol(argv[0], NULL, 10)) >= 600))
650 curresp->cmdname = L".notify";
651 curresp->internal = commands->next;
655 if((curresp->cmdname = queue->cmd->name) == NULL)
656 curresp->cmdname = L".connect";
657 curresp->data = queue->data;
658 curresp->tag = queue->tag;
659 curresp->internal = (void *)(queue->cmd);
663 sizebuf(&curresp->rlines, &curresp->linessize, curresp->numlines + 1, sizeof(*(curresp->rlines)), 1);
664 curresp->rlines[curresp->numlines].argc = argc;
665 curresp->rlines[curresp->numlines].argv = argv;
671 if((curresp->code >= 600) || (queue == NULL) || (queue->callback == NULL))
674 ret = queue->callback(curresp);
677 if(respqueue == NULL)
679 respqueue = respqueueend = curresp;
681 curresp->next = NULL;
682 curresp->prev = respqueueend;
683 respqueueend->next = curresp;
684 respqueueend = curresp;
686 } else if(ret == 1) {
687 dc_freeresp(curresp);
693 wmemmove(cbuf, pptr, cbufdata -= (pptr - cbuf));
707 if(iswspace(*pptr) || ((argc == 0) && (*pptr == L'-')))
720 sizebuf(&argv, &args, argc + 1, sizeof(*argv), 1);
725 } else if(*pptr == L'\"') {
728 } else if(*pptr == L'\\') {
729 if(pptr == cbuf + cbufdata - 1)
734 addtobuf(cw, *(++pptr));
737 addtobuf(cw, *(pptr++));
744 } else if(*pptr == L'\\') {
745 addtobuf(cw, *(++pptr));
758 int dc_handlewrite(void)
766 if(queue->buflen > 0)
768 ret = send(fd, queue->buf, queue->buflen, MSG_NOSIGNAL | MSG_DONTWAIT);
771 if((errno == EAGAIN) || (errno == EINTR))
779 memmove(queue->buf, queue->buf + ret, queue->buflen -= ret);
787 static char *readname(unsigned char *msg, unsigned char *eom, unsigned char **p)
791 size_t namesize, namedata, len;
794 namesize = namedata = 0;
802 } else if(len == 0xc0) {
803 tp = msg + *((*p)++);
804 if((tname = readname(msg, eom, &tp)) == NULL)
810 bufcat(name, tname, strlen(tname));
814 } else if(*p + len >= eom) {
819 bufcat(name, *p, len);
825 static int skipname(unsigned char *msg, unsigned char *eom, unsigned char **p)
835 } else if(len == 0xc0) {
838 } else if(*p + len >= eom) {
845 static int getsrvrr(char *name, char **host, int *port)
848 char *name2, *rrname;
849 unsigned char *eom, *p;
850 unsigned char buf[1024];
851 int flags, num, class, type;
855 if(!(_res.options & RES_INIT))
860 /* res_querydomain doesn't work for some reason */
861 name2 = smalloc(strlen("_dolcon._tcp.") + strlen(name) + 2);
862 strcpy(name2, "_dolcon._tcp.");
865 if(name2[len - 1] != '.')
870 ret = res_query(name2, C_IN, T_SRV, buf, sizeof(buf));
877 flags = (buf[2] << 8) + buf[3];
878 if((flags & 0xfa0f) != 0x8000)
883 num = (buf[4] << 8) + buf[5];
885 for(i = 0; i < num; i++)
887 if(skipname(buf, eom, &p))
894 num = (buf[6] << 8) + buf[7];
895 for(i = 0; i < num; i++)
897 if((rrname = readname(buf, eom, &p)) == NULL)
909 if((class == C_IN) && (type == T_SRV) && !strcmp(rrname, name2))
913 /* Noone will want to have alternative DC servers, so
914 * don't care about priority and weigth */
925 if((rrname = readname(buf, eom, &p)) == NULL)
938 static int getsrvrr(char *name, char **host, int *port)
945 int dc_connect(char *host, int port)
947 struct addrinfo hint;
948 struct sockaddr_storage addr;
949 struct sockaddr_in *ipv4;
951 struct sockaddr_in6 *ipv6;
955 int getsrv, freehost;
969 memset(&hint, 0, sizeof(hint));
970 hint.ai_socktype = SOCK_STREAM;
973 if(!getsrvrr(host, &newhost, &port))
981 freeaddrinfo(hostlist);
982 if(getaddrinfo(host, NULL, &hint, &hostlist))
989 for(curhost = hostlist; curhost != NULL; curhost = curhost->ai_next)
991 if((fd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0)
999 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
1000 memcpy(&addr, curhost->ai_addr, curhost->ai_addrlen);
1001 if(addr.ss_family == AF_INET)
1003 ipv4 = (struct sockaddr_in *)&addr;
1004 ipv4->sin_port = htons(port);
1007 if(addr.ss_family == AF_INET6)
1009 ipv6 = (struct sockaddr_in6 *)&addr;
1010 ipv6->sin6_port = htons(port);
1013 if(connect(fd, (struct sockaddr *)&addr, curhost->ai_addrlen))
1015 if(errno == EINPROGRESS)
1027 qcmd = makeqcmd(NULL);
1029 if(dchostname != NULL)
1031 dchostname = sstrdup(host);
1037 struct dc_intresp *dc_interpret(struct dc_response *resp)
1040 struct dc_intresp *iresp;
1041 struct command *cmd;
1042 struct respclass *cls;
1046 if((resp->numlines == 0) || (resp->rlines[0].argc == 0) || (resp->curline >= resp->numlines))
1048 code = wcstol(resp->rlines[0].argv[0], NULL, 10);
1049 cmd = (struct command *)(resp->internal);
1050 for(cls = cmd->classes; cls != NULL; cls = cls->next)
1052 if(cls->code == code)
1057 if(cls->nwords >= resp->rlines[resp->curline].argc)
1059 iresp = smalloc(sizeof(*iresp));
1064 for(i = 0; i < cls->nwords; i++)
1066 switch(cls->wordt[i])
1071 sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1);
1072 iresp->argv[iresp->argc].val.str = swcsdup(resp->rlines[resp->curline].argv[i + 1]);
1073 iresp->argv[iresp->argc].type = cls->wordt[i];
1077 sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1);
1078 iresp->argv[iresp->argc].val.num = wcstol(resp->rlines[resp->curline].argv[i + 1], NULL, 0);
1079 iresp->argv[iresp->argc].type = cls->wordt[i];
1083 sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1);
1084 iresp->argv[iresp->argc].val.flnum = wcstod(resp->rlines[resp->curline].argv[i + 1], NULL);
1085 iresp->argv[iresp->argc].type = cls->wordt[i];
1094 void dc_freeires(struct dc_intresp *ires)
1098 for(i = 0; i < ires->argc; i++)
1100 if(ires->argv[i].type == RESP_STR)
1101 free(ires->argv[i].val.str);
1107 const char *dc_gethostname(void)