Transfer from CVS at SourceForge
[doldaconnect.git] / lib / uilib.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
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.
9 *
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.
14 *
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
18*/
19
20/*
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.
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29#include <stdio.h>
30#include <unistd.h>
31#include <stdlib.h>
32#include <string.h>
33#include <iconv.h>
34#include <wchar.h>
35#include <wctype.h>
36#include <errno.h>
37#include <stdarg.h>
38#include <sys/socket.h>
39#include <netinet/in.h>
40#include <arpa/inet.h>
41#include <fcntl.h>
42#include <netdb.h>
43#include <sys/poll.h>
44#ifdef HAVE_RESOLVER
45#include <arpa/nameser.h>
46#include <resolv.h>
47#endif
48
49#include <doldaconnect/uilib.h>
50#include <doldaconnect/utils.h>
51
52#define RESP_END -1
53#define RESP_DSC 0
54#define RESP_STR 1
55#define RESP_INT 2
56#define RESP_FLOAT 3
57
58struct respclass
59{
60 struct respclass *next;
61 int code;
62 int nwords;
63 int *wordt;
64};
65
66struct command
67{
68 struct command *next;
69 wchar_t *name;
70 struct respclass *classes;
71};
72
73struct qcmd
74{
75 struct qcmd *next;
76 struct command *cmd;
77 int tag;
78 char *buf;
79 size_t buflen;
80 int (*callback)(struct dc_response *resp);
81 void *data;
82};
83
84void dc_uimisc_disconnected(void);
85
86/* The first command must be the nameless connect command and second
87 * the notification command. */
88static struct command *commands = NULL;
89static struct qcmd *queue = NULL, *queueend = NULL;
90static struct dc_response *respqueue = NULL, *respqueueend = NULL;
91static int state = -1;
92static int fd = -1;
93static iconv_t ichandle;
94static int resetreader = 1;
95static char *dchostname = NULL;
96static struct addrinfo *hostlist = NULL, *curhost = NULL;
97static int servport;
98
99static struct dc_response *makeresp(void)
100{
101 struct dc_response *new;
102
103 new = smalloc(sizeof(*new));
104 new->next = NULL;
105 new->prev = NULL;
106 new->code = 0;
107 new->cmdname = NULL;
108 new->rlines = NULL;
109 new->linessize = 0;
110 new->numlines = 0;
111 new->curline = 0;
112 new->data = NULL;
113 return(new);
114}
115
116static void freeqcmd(struct qcmd *qcmd)
117{
118 if(qcmd->buf != NULL)
119 free(qcmd->buf);
120 free(qcmd);
121}
122
123static void unlinkqueue(void)
124{
125 struct qcmd *qcmd;
126
127 if((qcmd = queue) == NULL)
128 return;
129 queue = qcmd->next;
130 if(queue == NULL)
131 queueend = NULL;
132 freeqcmd(qcmd);
133}
134
135static struct qcmd *makeqcmd(wchar_t *name)
136{
137 struct qcmd *new;
138 struct command *cmd;
139 static int tag = 0;
140
141 if(name == NULL)
142 {
143 cmd = commands;
144 } else {
145 for(cmd = commands; cmd != NULL; cmd = cmd->next)
146 {
147 if((cmd->name != NULL) && !wcscmp(cmd->name, name))
148 break;
149 }
150 if(cmd == NULL)
151 return(NULL);
152 }
153 new = smalloc(sizeof(*new));
154 new->tag = tag++;
155 new->cmd = cmd;
156 new->buf = NULL;
157 new->buflen = 0;
158 new->callback = NULL;
159 new->next = NULL;
160 new->data = NULL;
161 if(queueend == NULL)
162 {
163 queue = queueend = new;
164 } else {
165 queueend->next = new;
166 queueend = new;
167 }
168 return(new);
169}
170
171static wchar_t *quoteword(wchar_t *word)
172{
173 wchar_t *wp, *buf, *bp;
174 int dq, numbs, numc;
175
176 dq = 0;
177 numbs = 0;
178 numc = 0;
179 if(*word == L'\0')
180 {
181 dq = 1;
182 } else {
183 for(wp = word; *wp != L'\0'; wp++)
184 {
185 if(!dq && iswspace(*wp))
186 dq = 1;
187 if((*wp == L'\\') || (*wp == L'\"'))
188 numbs++;
189 numc++;
190 }
191 }
192 if(!dq && !numbs)
193 return(NULL);
194 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
195 if(dq)
196 *(bp++) = L'\"';
197 for(wp = word; *wp != L'\0'; wp++)
198 {
199 if((*wp == L'\\') || (*wp == L'\"'))
200 *(bp++) = L'\\';
201 *(bp++) = *wp;
202 }
203 if(dq)
204 *(bp++) = L'\"';
205 *(bp++) = L'\0';
206 return(buf);
207}
208
209static struct command *makecmd(wchar_t *name)
210{
211 struct command *new;
212
213 new = smalloc(sizeof(*new));
214 new->name = name;
215 new->classes = NULL;
216 new->next = commands;
217 commands = new;
218 return(new);
219}
220
221static struct respclass *addresp(struct command *cmd, int code, ...)
222{
223 struct respclass *new;
224 int i;
225 int resps[128];
226 va_list args;
227
228 va_start(args, code);
229 i = 0;
230 while((resps[i++] = va_arg(args, int)) != RESP_END);
231 i--;
232 va_end(args);
233 new = smalloc(sizeof(*new));
234 new->code = code;
235 new->nwords = i;
236 if(i > 0)
237 {
238 new->wordt = smalloc(sizeof(int) * i);
239 memcpy(new->wordt, resps, sizeof(int) * i);
240 } else {
241 new->wordt = NULL;
242 }
243 new->next = cmd->classes;
244 cmd->classes = new;
245 return(new);
246}
247
248#include "initcmds.h"
249
250int dc_init(void)
251{
252 if((ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
253 return(-1);
254 initcmds();
255 return(0);
256}
257
258void dc_cleanup(void)
259{
260 iconv_close(ichandle);
261}
262
263void dc_disconnect(void)
264{
265 struct dc_response *resp;
266
267 state = -1;
268 if(fd >= 0)
269 close(fd);
270 fd = -1;
271 while(queue != NULL)
272 unlinkqueue();
273 while((resp = dc_getresp()) != NULL)
274 dc_freeresp(resp);
275 dc_uimisc_disconnected();
276 if(dchostname != NULL)
277 free(dchostname);
278 dchostname = NULL;
279}
280
281void dc_freeresp(struct dc_response *resp)
282{
283 int i, o;
284
285 for(i = 0; i < resp->numlines; i++)
286 {
287 for(o = 0; o < resp->rlines[i].argc; o++)
288 free(resp->rlines[i].argv[o]);
289 free(resp->rlines[i].argv);
290 }
291 free(resp->rlines);
292 free(resp);
293}
294
295struct dc_response *dc_getresp(void)
296{
297 struct dc_response *ret;
298
299 if((ret = respqueue) == NULL)
300 return(NULL);
301 respqueue = ret->next;
302 if(respqueue == NULL)
303 respqueueend = NULL;
304 else
305 respqueue->prev = NULL;
306 return(ret);
307}
308
309struct dc_response *dc_gettaggedresp(int tag)
310{
311 struct dc_response *resp;
312
313 for(resp = respqueue; resp != NULL; resp = resp->next)
314 {
315 if(resp->tag == tag)
316 {
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;
325 return(resp);
326 }
327 }
328 return(NULL);
329}
330
331struct dc_response *dc_gettaggedrespsync(int tag)
332{
333 struct pollfd pfd;
334 struct dc_response *resp;
335
336 while((resp = dc_gettaggedresp(tag)) == NULL)
337 {
338 pfd.fd = fd;
339 pfd.events = POLLIN;
340 if(dc_wantwrite())
341 pfd.events |= POLLOUT;
342 if(poll(&pfd, 1, -1) < 0)
343 return(NULL);
344 if((pfd.revents & POLLIN) && dc_handleread())
345 return(NULL);
346 if((pfd.revents & POLLOUT) && dc_handlewrite())
347 return(NULL);
348 }
349 return(resp);
350}
351
352int dc_wantwrite(void)
353{
354 switch(state)
355 {
356 case 1:
357 if((queue != NULL) && (queue->buflen > 0))
358 return(1);
359 break;
360 }
361 return(0);
362}
363
364int dc_getstate(void)
365{
366 return(state);
367}
368
369int dc_queuecmd(int (*callback)(struct dc_response *), void *data, ...)
370{
371 struct qcmd *qcmd;
372 int num, freepart;
373 va_list al;
374 char *final;
375 wchar_t **toks;
376 wchar_t *buf;
377 wchar_t *part, *tpart;
378 size_t bufsize, bufdata;
379
380 buf = NULL;
381 bufsize = bufdata = 0;
382 num = 0;
383 va_start(al, data);
384 while((part = va_arg(al, wchar_t *)) != NULL)
385 {
386 if(!wcscmp(part, L"%%a"))
387 {
388 for(toks = va_arg(al, wchar_t **); *toks != NULL; toks++)
389 {
390 part = *toks;
391 freepart = 0;
392 if((tpart = quoteword(part)) != NULL)
393 {
394 freepart = 1;
395 part = tpart;
396 }
397 addtobuf(buf, L' ');
398 bufcat(buf, part, wcslen(part));
399 num++;
400 if(freepart)
401 free(part);
402 }
403 } else {
404 if(*part == L'%')
405 {
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
411 * it.
412 */
413 part = vswprintf2(tpart = (part + 1), al);
414 for(; *tpart != L'\0'; tpart++)
415 {
416 if(*tpart == L'%')
417 {
418 if(tpart[1] == L'%')
419 tpart++;
420 else
421 va_arg(al, int);
422 }
423 }
424 freepart = 1;
425 } else {
426 freepart = 0;
427 }
428 if((tpart = quoteword(part)) != NULL)
429 {
430 if(freepart)
431 free(part);
432 part = tpart;
433 freepart = 1;
434 }
435 if(num > 0)
436 addtobuf(buf, L' ');
437 if(num == 0)
438 {
439 if((qcmd = makeqcmd(part)) == NULL)
440 {
441 if(freepart)
442 free(part);
443 if(buf != NULL)
444 free(buf);
445 return(-1);
446 } else {
447 qcmd->callback = callback;
448 qcmd->data = data;
449 }
450 }
451 bufcat(buf, part, wcslen(part));
452 num++;
453 if(freepart)
454 free(part);
455 }
456 }
457 bufcat(buf, L"\r\n\0", 3);
458 if((final = icwcstombs(buf, "utf-8")) == NULL)
459 {
460 free(buf);
461 return(-1);
462 }
463 va_end(al);
464 free(buf);
465 qcmd->buf = final;
466 qcmd->buflen = strlen(final);
467 return(qcmd->tag);
468}
469
470int dc_handleread(void)
471{
472 int ret, done;
473 char *p1, *p2;
474 size_t len;
475 int errnobak;
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;
488 static int cont = 0;
489 static int unlink = 0;
490
491 switch(state)
492 {
493 case -1:
494 return(-1);
495 case 0:
496 len = sizeof(ret);
497 getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
498 if(ret)
499 {
500 int newfd;
501 struct sockaddr_storage addr;
502 struct sockaddr_in *ipv4;
503 struct sockaddr_in6 *ipv6;
504
505 for(curhost = curhost->ai_next; curhost != NULL; curhost = curhost->ai_next)
506 {
507 if((newfd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0)
508 {
509 errnobak = errno;
510 dc_disconnect();
511 errno = errnobak;
512 return(-1);
513 }
514 dup2(newfd, fd);
515 close(newfd);
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)
519 {
520 ipv4 = (struct sockaddr_in *)&addr;
521 ipv4->sin_port = htons(servport);
522 }
523#ifdef HAVE_IPV6
524 if(addr.ss_family == AF_INET6)
525 {
526 ipv6 = (struct sockaddr_in6 *)&addr;
527 ipv6->sin6_port = htons(servport);
528 }
529#endif
530 if(connect(fd, (struct sockaddr *)&addr, curhost->ai_addrlen))
531 {
532 if(errno == EINPROGRESS)
533 return(0);
534 } else {
535 break;
536 }
537 }
538 if(curhost == NULL)
539 {
540 dc_disconnect();
541 errno = ret;
542 return(-1);
543 }
544 }
545 state = 1;
546 resetreader = 1;
547 break;
548 case 1:
549 if(resetreader)
550 {
551 inbufdata = 0;
552 cbufdata = 0;
553 pptr = NULL;
554 pstate = 0;
555 resetreader = 0;
556 cont = 0;
557 if(curresp != NULL)
558 dc_freeresp(curresp);
559 curresp = NULL;
560 }
561 if(inbufdata == 128)
562 inbufdata = 0;
563 ret = read(fd, inbuf + inbufdata, 128 - inbufdata);
564 if(ret < 0)
565 {
566 if((errno == EAGAIN) || (errno == EINTR))
567 return(0);
568 errnobak = errno;
569 dc_disconnect();
570 errno = errnobak;
571 return(-1);
572 } else if(ret == 0) {
573 dc_disconnect();
574 errno = 0;
575 return(-1);
576 }
577 inbufdata += ret;
578 done = 0;
579 while(!done)
580 {
581 if(cbufsize == cbufdata)
582 {
583 if(pptr != NULL)
584 len = pptr - cbuf;
585 if((cbuf = realloc(cbuf, sizeof(wchar_t) * (cbufsize += 256))) == NULL)
586 {
587 dc_disconnect();
588 errno = ENOMEM;
589 return(-1);
590 }
591 if(pptr != NULL)
592 pptr = cbuf + len;
593 }
594 p1 = inbuf;
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));
600 if(ret < 0)
601 {
602 switch(errno)
603 {
604 case EILSEQ:
605 /* XXX Is this really OK? */
606 inbufdata = 0;
607 done = 1;
608 break;
609 case EINVAL:
610 done = 1;
611 break;
612 case E2BIG:
613 break;
614 default:
615 errnobak = errno;
616 dc_disconnect();
617 errno = errnobak;
618 return(-1);
619 }
620 } else {
621 done = 1;
622 }
623 }
624 if(pptr == NULL)
625 pptr = cbuf;
626 done = 0;
627 while(!done && (pptr - cbuf < cbufdata))
628 {
629 switch(pstate)
630 {
631 case 0:
632 if(iswspace(*pptr))
633 {
634 if(*pptr == L'\r')
635 {
636 if(pptr == cbuf + cbufdata - 1)
637 {
638 done = 1;
639 break;
640 }
641 if(*(++pptr) == L'\n')
642 {
643 if(curresp == NULL)
644 {
645 curresp = makeresp();
646 if((argc > 0) && ((curresp->code = wcstol(argv[0], NULL, 10)) >= 600))
647 {
648 curresp->cmdname = L".notify";
649 curresp->internal = commands->next;
650 curresp->tag = -1;
651 unlink = 0;
652 } else {
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);
658 unlink = 1;
659 }
660 }
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;
664 argv = NULL;
665 argc = args = 0;
666 curresp->numlines++;
667 if(!cont)
668 {
669 if((curresp->code >= 600) || (queue == NULL) || (queue->callback == NULL))
670 ret = 0;
671 else
672 ret = queue->callback(curresp);
673 if(ret == 0)
674 {
675 if(respqueue == NULL)
676 {
677 respqueue = respqueueend = curresp;
678 } else {
679 curresp->next = NULL;
680 curresp->prev = respqueueend;
681 respqueueend->next = curresp;
682 respqueueend = curresp;
683 }
684 } else if(ret == 1) {
685 dc_freeresp(curresp);
686 }
687 curresp = NULL;
688 if(unlink)
689 unlinkqueue();
690 }
691 wmemmove(cbuf, pptr, cbufdata -= (pptr - cbuf));
692 pptr = cbuf;
693 } else {
694 pptr++;
695 }
696 } else {
697 pptr++;
698 }
699 } else {
700 pstate = 1;
701 cwdata = 0;
702 }
703 break;
704 case 1:
705 if(iswspace(*pptr) || ((argc == 0) && (*pptr == L'-')))
706 {
707 if(argc == 0)
708 {
709 if(*pptr == L'-')
710 {
711 cont = 1;
712 pptr++;
713 } else {
714 cont = 0;
715 }
716 }
717 addtobuf(cw, L'\0');
718 sizebuf(&argv, &args, argc + 1, sizeof(*argv), 1);
719 argv[argc++] = cw;
720 cw = NULL;
721 cwsize = cwdata = 0;
722 pstate = 0;
723 } else if(*pptr == L'\"') {
724 pstate = 2;
725 pptr++;
726 } else if(*pptr == L'\\') {
727 if(pptr == cbuf + cbufdata - 1)
728 {
729 done = 1;
730 break;
731 }
732 addtobuf(cw, *(++pptr));
733 pptr++;
734 } else {
735 addtobuf(cw, *(pptr++));
736 }
737 break;
738 case 2:
739 if(*pptr == L'\"')
740 {
741 pstate = 1;
742 } else if(*pptr == L'\\') {
743 addtobuf(cw, *(++pptr));
744 } else {
745 addtobuf(cw, *pptr);
746 }
747 pptr++;
748 break;
749 }
750 }
751 break;
752 }
753 return(0);
754}
755
756int dc_handlewrite(void)
757{
758 int ret;
759 int errnobak;
760
761 switch(state)
762 {
763 case 1:
764 if(queue->buflen > 0)
765 {
766 ret = send(fd, queue->buf, queue->buflen, MSG_NOSIGNAL | MSG_DONTWAIT);
767 if(ret < 0)
768 {
769 if((errno == EAGAIN) || (errno == EINTR))
770 return(0);
771 errnobak = errno;
772 dc_disconnect();
773 errno = errnobak;
774 return(-1);
775 }
776 if(ret > 0)
777 memmove(queue->buf, queue->buf + ret, queue->buflen -= ret);
778 }
779 break;
780 }
781 return(0);
782}
783
784#ifdef HAVE_RESOLVER
785static char *readname(unsigned char *msg, unsigned char *eom, unsigned char **p)
786{
787 char *name, *tname;
788 unsigned char *tp;
789 size_t namesize, namedata, len;
790
791 name = NULL;
792 namesize = namedata = 0;
793 while(1)
794 {
795 len = *((*p)++);
796 if(len == 0)
797 {
798 addtobuf(name, 0);
799 return(name);
800 } else if(len == 0xc0) {
801 tp = msg + *((*p)++);
802 if((tname = readname(msg, eom, &tp)) == NULL)
803 {
804 if(name != NULL)
805 free(name);
806 return(NULL);
807 }
808 bufcat(name, tname, strlen(tname));
809 addtobuf(name, 0);
810 free(tname);
811 return(name);
812 } else if(*p + len >= eom) {
813 if(name != NULL)
814 free(name);
815 return(NULL);
816 }
817 bufcat(name, *p, len);
818 *p += len;
819 addtobuf(name, '.');
820 }
821}
822
823static int skipname(unsigned char *msg, unsigned char *eom, unsigned char **p)
824{
825 size_t len;
826
827 while(1)
828 {
829 len = *((*p)++);
830 if(len == 0)
831 {
832 return(0);
833 } else if(len == 0xc0) {
834 (*p)++;
835 return(0);
836 } else if(*p + len >= eom) {
837 return(-1);
838 }
839 *p += len;
840 }
841}
842
843static int getsrvrr(char *name, char **host, int *port)
844{
845 int i;
846 char *name2, *rrname;
847 unsigned char *eom, *p;
848 unsigned char buf[1024];
849 int flags, num, class, type;
850 size_t len;
851 int ret;
852
853 if(!(_res.options & RES_INIT))
854 {
855 if(res_init() < 0)
856 return(-1);
857 }
858 /* res_querydomain doesn't work for some reason */
859 name2 = smalloc(strlen("_dolcon._tcp.") + strlen(name) + 2);
860 strcpy(name2, "_dolcon._tcp.");
861 strcat(name2, name);
862 len = strlen(name2);
863 if(name2[len - 1] != '.')
864 {
865 name2[len] = '.';
866 name2[len + 1] = 0;
867 }
868 ret = res_query(name2, C_IN, T_SRV, buf, sizeof(buf));
869 if(ret < 0)
870 {
871 free(name2);
872 return(-1);
873 }
874 eom = buf + ret;
875 flags = (buf[2] << 8) + buf[3];
876 if((flags & 0xfa0f) != 0x8000)
877 {
878 free(name2);
879 return(-1);
880 }
881 num = (buf[4] << 8) + buf[5];
882 p = buf + 12;
883 for(i = 0; i < num; i++)
884 {
885 if(skipname(buf, eom, &p))
886 {
887 free(name2);
888 return(-1);
889 }
890 p += 4;
891 }
892 num = (buf[6] << 8) + buf[7];
893 for(i = 0; i < num; i++)
894 {
895 if((rrname = readname(buf, eom, &p)) == NULL)
896 {
897 free(name2);
898 return(-1);
899 }
900 type = *(p++) << 8;
901 type += *(p++);
902 class = *(p++) << 8;
903 class += *(p++);
904 p += 4;
905 len = *(p++) << 8;
906 len += *(p++);
907 if((class == C_IN) && (type == T_SRV) && !strcmp(rrname, name2))
908 {
909 free(rrname);
910 free(name2);
911 /* Noone will want to have alternative DC servers, so
912 * don't care about priority and weigth */
913 p += 4;
914 if(port == NULL)
915 {
916 p += 2;
917 } else {
918 *port = *(p++) << 8;
919 *port += *(p++);
920 }
921 if(host != NULL)
922 {
923 if((rrname = readname(buf, eom, &p)) == NULL)
924 return(-1);
925 *host = rrname;
926 }
927 return(0);
928 }
929 p += len;
930 free(rrname);
931 }
932 free(name2);
933 return(-1);
934}
935#else
936static int getsrvrr(char *name, char **host, int *port)
937{
938 errno = EOPNOTSUP;
939 return(-1);
940}
941#endif
942
943int dc_connect(char *host, int port)
944{
945 struct addrinfo hint;
946 struct sockaddr_storage addr;
947 struct sockaddr_in *ipv4;
948#ifdef HAVE_IPV6
949 struct sockaddr_in6 *ipv6;
950#endif
951 struct qcmd *qcmd;
952 char *newhost;
953 int getsrv, freehost;
954 int errnobak;
955
956 if(fd >= 0)
957 dc_disconnect();
958 state = -1;
959 freehost = 0;
960 if(port < 0)
961 {
962 port = 1500;
963 getsrv = 1;
964 } else {
965 getsrv = 0;
966 }
967 memset(&hint, 0, sizeof(hint));
968 hint.ai_socktype = SOCK_STREAM;
969 if(getsrv)
970 {
971 if(!getsrvrr(host, &newhost, &port))
972 {
973 host = newhost;
974 freehost = 1;
975 }
976 }
977 servport = port;
978 if(hostlist != NULL)
979 freeaddrinfo(hostlist);
980 if(getaddrinfo(host, NULL, &hint, &hostlist))
981 {
982 errno = ENONET;
983 if(freehost)
984 free(host);
985 return(-1);
986 }
987 for(curhost = hostlist; curhost != NULL; curhost = curhost->ai_next)
988 {
989 if((fd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0)
990 {
991 errnobak = errno;
992 if(freehost)
993 free(host);
994 errno = errnobak;
995 return(-1);
996 }
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)
1000 {
1001 ipv4 = (struct sockaddr_in *)&addr;
1002 ipv4->sin_port = htons(port);
1003 }
1004#ifdef HAVE_IPV6
1005 if(addr.ss_family == AF_INET6)
1006 {
1007 ipv6 = (struct sockaddr_in6 *)&addr;
1008 ipv6->sin6_port = htons(port);
1009 }
1010#endif
1011 if(connect(fd, (struct sockaddr *)&addr, curhost->ai_addrlen))
1012 {
1013 if(errno == EINPROGRESS)
1014 {
1015 state = 0;
1016 break;
1017 }
1018 close(fd);
1019 fd = -1;
1020 } else {
1021 state = 1;
1022 break;
1023 }
1024 }
1025 qcmd = makeqcmd(NULL);
1026 resetreader = 1;
1027 if(dchostname != NULL)
1028 free(dchostname);
1029 dchostname = sstrdup(host);
1030 if(freehost)
1031 free(host);
1032 return(fd);
1033}
1034
1035struct dc_intresp *dc_interpret(struct dc_response *resp)
1036{
1037 int i;
1038 struct dc_intresp *iresp;
1039 struct command *cmd;
1040 struct respclass *cls;
1041 int code;
1042 int args;
1043
1044 if((resp->numlines == 0) || (resp->rlines[0].argc == 0) || (resp->curline >= resp->numlines))
1045 return(NULL);
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)
1049 {
1050 if(cls->code == code)
1051 break;
1052 }
1053 if(cls == NULL)
1054 return(NULL);
1055 if(cls->nwords >= resp->rlines[resp->curline].argc)
1056 return(NULL);
1057 iresp = smalloc(sizeof(*iresp));
1058 iresp->code = code;
1059 iresp->argv = NULL;
1060 iresp->argc = 0;
1061 args = 0;
1062 for(i = 0; i < cls->nwords; i++)
1063 {
1064 switch(cls->wordt[i])
1065 {
1066 case RESP_DSC:
1067 break;
1068 case RESP_STR:
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];
1072 iresp->argc++;
1073 break;
1074 case RESP_INT:
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];
1078 iresp->argc++;
1079 break;
1080 case RESP_FLOAT:
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];
1084 iresp->argc++;
1085 break;
1086 }
1087 }
1088 resp->curline++;
1089 return(iresp);
1090}
1091
1092void dc_freeires(struct dc_intresp *ires)
1093{
1094 int i;
1095
1096 for(i = 0; i < ires->argc; i++)
1097 {
1098 if(ires->argv[i].type == RESP_STR)
1099 free(ires->argv[i].val.str);
1100 }
1101 free(ires->argv);
1102 free(ires);
1103}
1104
1105const char *dc_gethostname(void)
1106{
1107 return(dchostname);
1108}