Commit | Line | Data |
---|---|---|
d3372da9 | 1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client | |
302a2600 | 3 | * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com> |
d3372da9 | 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> | |
12383d48 | 41 | #include <sys/un.h> |
d3372da9 | 42 | #include <fcntl.h> |
43 | #include <netdb.h> | |
44 | #include <sys/poll.h> | |
12383d48 | 45 | #include <pwd.h> |
d3372da9 | 46 | #ifdef HAVE_RESOLVER |
47 | #include <arpa/nameser.h> | |
48 | #include <resolv.h> | |
49 | #endif | |
50 | ||
51 | #include <doldaconnect/uilib.h> | |
31c700d1 | 52 | #include <utils.h> |
d3372da9 | 53 | |
4d487bad | 54 | #define DOLCON_SRV_NAME "_dolcon._tcp" |
55 | ||
d3372da9 | 56 | #define RESP_END -1 |
57 | #define RESP_DSC 0 | |
58 | #define RESP_STR 1 | |
59 | #define RESP_INT 2 | |
60 | #define RESP_FLOAT 3 | |
8f623b36 | 61 | #define RESP_LNUM 4 |
d3372da9 | 62 | |
63 | struct respclass | |
64 | { | |
65 | struct respclass *next; | |
66 | int code; | |
67 | int nwords; | |
68 | int *wordt; | |
69 | }; | |
70 | ||
71 | struct command | |
72 | { | |
73 | struct command *next; | |
74 | wchar_t *name; | |
75 | struct respclass *classes; | |
76 | }; | |
77 | ||
78 | struct qcmd | |
79 | { | |
80 | struct qcmd *next; | |
81 | struct command *cmd; | |
82 | int tag; | |
83 | char *buf; | |
84 | size_t buflen; | |
85 | int (*callback)(struct dc_response *resp); | |
86 | void *data; | |
87 | }; | |
88 | ||
89 | void dc_uimisc_disconnected(void); | |
90 | ||
91 | /* The first command must be the nameless connect command and second | |
92 | * the notification command. */ | |
93 | static struct command *commands = NULL; | |
94 | static struct qcmd *queue = NULL, *queueend = NULL; | |
95 | static struct dc_response *respqueue = NULL, *respqueueend = NULL; | |
96 | static int state = -1; | |
97 | static int fd = -1; | |
98 | static iconv_t ichandle; | |
99 | static int resetreader = 1; | |
d3372da9 | 100 | static struct addrinfo *hostlist = NULL, *curhost = NULL; |
9e5f2b29 | 101 | struct { |
102 | char *hostname; | |
103 | int family; | |
104 | int sentcreds; | |
105 | } servinfo; | |
e4b88cc0 | 106 | char *dc_srv_local; |
d3372da9 | 107 | |
0538f877 | 108 | static void message(int bits, char *format, ...) |
109 | { | |
110 | static int hb = -1; | |
111 | char *v; | |
112 | va_list args; | |
113 | ||
114 | if(hb == -1) | |
115 | { | |
116 | hb = 0; | |
117 | if((v = getenv("LIBDCUI_MSG")) != NULL) | |
118 | hb = strtol(v, NULL, 0) & 65535; | |
119 | } | |
120 | if(hb & bits) | |
121 | { | |
122 | va_start(args, format); | |
123 | vfprintf(stderr, format, args); | |
124 | va_end(args); | |
125 | } | |
126 | } | |
127 | ||
128 | static char *formataddress(struct sockaddr *arg, socklen_t arglen) | |
129 | { | |
130 | struct sockaddr_in *ipv4; | |
131 | #ifdef HAVE_IPV6 | |
132 | struct sockaddr_in6 *ipv6; | |
133 | #endif | |
134 | static char *ret = NULL; | |
135 | char buf[1024]; | |
136 | ||
137 | if(ret != NULL) | |
138 | free(ret); | |
139 | ret = NULL; | |
140 | switch(arg->sa_family) | |
141 | { | |
142 | case AF_UNIX: | |
143 | ret = sprintf2("Unix socket (%s)", ((struct sockaddr_un *)arg)->sun_path); | |
144 | break; | |
145 | case AF_INET: | |
146 | ipv4 = (struct sockaddr_in *)arg; | |
147 | if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL) | |
148 | return(NULL); | |
149 | ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port)); | |
150 | break; | |
151 | #ifdef HAVE_IPV6 | |
152 | case AF_INET6: | |
153 | ipv6 = (struct sockaddr_in6 *)arg; | |
154 | if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL) | |
155 | return(NULL); | |
156 | ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port)); | |
157 | break; | |
158 | #endif | |
159 | default: | |
160 | errno = EPFNOSUPPORT; | |
161 | break; | |
162 | } | |
163 | return(ret); | |
164 | } | |
d3372da9 | 165 | static struct dc_response *makeresp(void) |
166 | { | |
167 | struct dc_response *new; | |
168 | ||
169 | new = smalloc(sizeof(*new)); | |
170 | new->next = NULL; | |
171 | new->prev = NULL; | |
172 | new->code = 0; | |
173 | new->cmdname = NULL; | |
174 | new->rlines = NULL; | |
175 | new->linessize = 0; | |
176 | new->numlines = 0; | |
177 | new->curline = 0; | |
178 | new->data = NULL; | |
179 | return(new); | |
180 | } | |
181 | ||
182 | static void freeqcmd(struct qcmd *qcmd) | |
183 | { | |
184 | if(qcmd->buf != NULL) | |
185 | free(qcmd->buf); | |
186 | free(qcmd); | |
187 | } | |
188 | ||
189 | static void unlinkqueue(void) | |
190 | { | |
191 | struct qcmd *qcmd; | |
192 | ||
193 | if((qcmd = queue) == NULL) | |
194 | return; | |
195 | queue = qcmd->next; | |
196 | if(queue == NULL) | |
197 | queueend = NULL; | |
198 | freeqcmd(qcmd); | |
199 | } | |
200 | ||
201 | static struct qcmd *makeqcmd(wchar_t *name) | |
202 | { | |
203 | struct qcmd *new; | |
204 | struct command *cmd; | |
205 | static int tag = 0; | |
206 | ||
207 | if(name == NULL) | |
208 | { | |
209 | cmd = commands; | |
210 | } else { | |
211 | for(cmd = commands; cmd != NULL; cmd = cmd->next) | |
212 | { | |
213 | if((cmd->name != NULL) && !wcscmp(cmd->name, name)) | |
214 | break; | |
215 | } | |
ee9bb4f3 FT |
216 | if(cmd == NULL) { |
217 | errno = ENOSYS; /* Bleh */ | |
d3372da9 | 218 | return(NULL); |
ee9bb4f3 | 219 | } |
d3372da9 | 220 | } |
221 | new = smalloc(sizeof(*new)); | |
222 | new->tag = tag++; | |
223 | new->cmd = cmd; | |
224 | new->buf = NULL; | |
225 | new->buflen = 0; | |
226 | new->callback = NULL; | |
227 | new->next = NULL; | |
228 | new->data = NULL; | |
229 | if(queueend == NULL) | |
230 | { | |
231 | queue = queueend = new; | |
232 | } else { | |
233 | queueend->next = new; | |
234 | queueend = new; | |
235 | } | |
236 | return(new); | |
237 | } | |
238 | ||
239 | static wchar_t *quoteword(wchar_t *word) | |
240 | { | |
241 | wchar_t *wp, *buf, *bp; | |
242 | int dq, numbs, numc; | |
243 | ||
244 | dq = 0; | |
245 | numbs = 0; | |
246 | numc = 0; | |
247 | if(*word == L'\0') | |
248 | { | |
249 | dq = 1; | |
250 | } else { | |
251 | for(wp = word; *wp != L'\0'; wp++) | |
252 | { | |
253 | if(!dq && iswspace(*wp)) | |
254 | dq = 1; | |
255 | if((*wp == L'\\') || (*wp == L'\"')) | |
256 | numbs++; | |
257 | numc++; | |
258 | } | |
259 | } | |
260 | if(!dq && !numbs) | |
261 | return(NULL); | |
262 | bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1)); | |
263 | if(dq) | |
264 | *(bp++) = L'\"'; | |
265 | for(wp = word; *wp != L'\0'; wp++) | |
266 | { | |
267 | if((*wp == L'\\') || (*wp == L'\"')) | |
268 | *(bp++) = L'\\'; | |
269 | *(bp++) = *wp; | |
270 | } | |
271 | if(dq) | |
272 | *(bp++) = L'\"'; | |
273 | *(bp++) = L'\0'; | |
274 | return(buf); | |
275 | } | |
276 | ||
277 | static struct command *makecmd(wchar_t *name) | |
278 | { | |
279 | struct command *new; | |
280 | ||
281 | new = smalloc(sizeof(*new)); | |
282 | new->name = name; | |
283 | new->classes = NULL; | |
284 | new->next = commands; | |
285 | commands = new; | |
286 | return(new); | |
287 | } | |
288 | ||
289 | static struct respclass *addresp(struct command *cmd, int code, ...) | |
290 | { | |
291 | struct respclass *new; | |
292 | int i; | |
293 | int resps[128]; | |
294 | va_list args; | |
295 | ||
296 | va_start(args, code); | |
297 | i = 0; | |
298 | while((resps[i++] = va_arg(args, int)) != RESP_END); | |
299 | i--; | |
300 | va_end(args); | |
301 | new = smalloc(sizeof(*new)); | |
302 | new->code = code; | |
303 | new->nwords = i; | |
304 | if(i > 0) | |
305 | { | |
306 | new->wordt = smalloc(sizeof(int) * i); | |
307 | memcpy(new->wordt, resps, sizeof(int) * i); | |
308 | } else { | |
309 | new->wordt = NULL; | |
310 | } | |
311 | new->next = cmd->classes; | |
312 | cmd->classes = new; | |
313 | return(new); | |
314 | } | |
315 | ||
316 | #include "initcmds.h" | |
317 | ||
318 | int dc_init(void) | |
319 | { | |
320 | if((ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) | |
321 | return(-1); | |
e4b88cc0 | 322 | dc_srv_local = sstrdup(""); |
d3372da9 | 323 | initcmds(); |
324 | return(0); | |
325 | } | |
326 | ||
327 | void dc_cleanup(void) | |
328 | { | |
329 | iconv_close(ichandle); | |
330 | } | |
331 | ||
332 | void dc_disconnect(void) | |
333 | { | |
334 | struct dc_response *resp; | |
335 | ||
336 | state = -1; | |
337 | if(fd >= 0) | |
338 | close(fd); | |
339 | fd = -1; | |
340 | while(queue != NULL) | |
341 | unlinkqueue(); | |
342 | while((resp = dc_getresp()) != NULL) | |
343 | dc_freeresp(resp); | |
344 | dc_uimisc_disconnected(); | |
9e5f2b29 | 345 | if(servinfo.hostname != NULL) |
346 | free(servinfo.hostname); | |
347 | memset(&servinfo, 0, sizeof(servinfo)); | |
d3372da9 | 348 | } |
349 | ||
350 | void dc_freeresp(struct dc_response *resp) | |
351 | { | |
352 | int i, o; | |
353 | ||
354 | for(i = 0; i < resp->numlines; i++) | |
355 | { | |
356 | for(o = 0; o < resp->rlines[i].argc; o++) | |
357 | free(resp->rlines[i].argv[o]); | |
358 | free(resp->rlines[i].argv); | |
359 | } | |
360 | free(resp->rlines); | |
361 | free(resp); | |
362 | } | |
363 | ||
364 | struct dc_response *dc_getresp(void) | |
365 | { | |
366 | struct dc_response *ret; | |
367 | ||
368 | if((ret = respqueue) == NULL) | |
369 | return(NULL); | |
370 | respqueue = ret->next; | |
371 | if(respqueue == NULL) | |
372 | respqueueend = NULL; | |
373 | else | |
374 | respqueue->prev = NULL; | |
375 | return(ret); | |
376 | } | |
377 | ||
378 | struct dc_response *dc_gettaggedresp(int tag) | |
379 | { | |
380 | struct dc_response *resp; | |
381 | ||
382 | for(resp = respqueue; resp != NULL; resp = resp->next) | |
383 | { | |
384 | if(resp->tag == tag) | |
385 | { | |
386 | if(resp->prev != NULL) | |
387 | resp->prev->next = resp->next; | |
388 | if(resp->next != NULL) | |
389 | resp->next->prev = resp->prev; | |
390 | if(resp == respqueue) | |
391 | respqueue = resp->next; | |
392 | if(resp == respqueueend) | |
393 | respqueueend = resp->prev; | |
394 | return(resp); | |
395 | } | |
396 | } | |
397 | return(NULL); | |
398 | } | |
399 | ||
400 | struct dc_response *dc_gettaggedrespsync(int tag) | |
401 | { | |
402 | struct pollfd pfd; | |
403 | struct dc_response *resp; | |
404 | ||
405 | while((resp = dc_gettaggedresp(tag)) == NULL) | |
406 | { | |
407 | pfd.fd = fd; | |
408 | pfd.events = POLLIN; | |
409 | if(dc_wantwrite()) | |
410 | pfd.events |= POLLOUT; | |
411 | if(poll(&pfd, 1, -1) < 0) | |
412 | return(NULL); | |
413 | if((pfd.revents & POLLIN) && dc_handleread()) | |
414 | return(NULL); | |
415 | if((pfd.revents & POLLOUT) && dc_handlewrite()) | |
416 | return(NULL); | |
417 | } | |
418 | return(resp); | |
419 | } | |
420 | ||
421 | int dc_wantwrite(void) | |
422 | { | |
423 | switch(state) | |
424 | { | |
425 | case 1: | |
426 | if((queue != NULL) && (queue->buflen > 0)) | |
427 | return(1); | |
428 | break; | |
429 | } | |
430 | return(0); | |
431 | } | |
432 | ||
433 | int dc_getstate(void) | |
434 | { | |
435 | return(state); | |
436 | } | |
437 | ||
438 | int dc_queuecmd(int (*callback)(struct dc_response *), void *data, ...) | |
439 | { | |
440 | struct qcmd *qcmd; | |
441 | int num, freepart; | |
442 | va_list al; | |
ccad23d3 | 443 | char *final, *sarg; |
d3372da9 | 444 | wchar_t **toks; |
445 | wchar_t *buf; | |
446 | wchar_t *part, *tpart; | |
447 | size_t bufsize, bufdata; | |
448 | ||
449 | buf = NULL; | |
450 | bufsize = bufdata = 0; | |
451 | num = 0; | |
452 | va_start(al, data); | |
453 | while((part = va_arg(al, wchar_t *)) != NULL) | |
454 | { | |
ccad23d3 | 455 | if(!wcscmp(part, L"%a")) |
d3372da9 | 456 | { |
457 | for(toks = va_arg(al, wchar_t **); *toks != NULL; toks++) | |
458 | { | |
459 | part = *toks; | |
460 | freepart = 0; | |
461 | if((tpart = quoteword(part)) != NULL) | |
462 | { | |
463 | freepart = 1; | |
464 | part = tpart; | |
465 | } | |
466 | addtobuf(buf, L' '); | |
467 | bufcat(buf, part, wcslen(part)); | |
468 | num++; | |
469 | if(freepart) | |
470 | free(part); | |
471 | } | |
472 | } else { | |
473 | if(*part == L'%') | |
474 | { | |
ccad23d3 | 475 | tpart = part + 1; |
476 | if(!wcscmp(tpart, L"i")) | |
d3372da9 | 477 | { |
ccad23d3 | 478 | freepart = 1; |
479 | part = swprintf2(L"%i", va_arg(al, int)); | |
480 | } else if(!wcscmp(tpart, L"s")) { | |
481 | freepart = 1; | |
482 | part = icmbstowcs(sarg = va_arg(al, char *), NULL); | |
483 | if(part == NULL) | |
d3372da9 | 484 | { |
ccad23d3 | 485 | if(buf != NULL) |
486 | free(buf); | |
487 | return(-1); | |
d3372da9 | 488 | } |
ccad23d3 | 489 | } else if(!wcscmp(tpart, L"ls")) { |
490 | part = va_arg(al, wchar_t *); | |
491 | } else if(!wcscmp(tpart, L"ll")) { | |
492 | freepart = 1; | |
493 | part = swprintf2(L"%lli", va_arg(al, long long)); | |
494 | } else if(!wcscmp(tpart, L"f")) { | |
495 | freepart = 1; | |
496 | part = swprintf2(L"%f", va_arg(al, double)); | |
497 | } else if(!wcscmp(tpart, L"x")) { | |
498 | freepart = 1; | |
499 | part = swprintf2(L"%x", va_arg(al, int)); | |
500 | } else { | |
501 | if(buf != NULL) | |
502 | free(buf); | |
ee9bb4f3 | 503 | errno = EINVAL; |
ccad23d3 | 504 | return(-1); |
d3372da9 | 505 | } |
d3372da9 | 506 | } else { |
507 | freepart = 0; | |
508 | } | |
509 | if((tpart = quoteword(part)) != NULL) | |
510 | { | |
511 | if(freepart) | |
512 | free(part); | |
513 | part = tpart; | |
514 | freepart = 1; | |
515 | } | |
516 | if(num > 0) | |
517 | addtobuf(buf, L' '); | |
518 | if(num == 0) | |
519 | { | |
520 | if((qcmd = makeqcmd(part)) == NULL) | |
521 | { | |
522 | if(freepart) | |
523 | free(part); | |
524 | if(buf != NULL) | |
525 | free(buf); | |
526 | return(-1); | |
527 | } else { | |
528 | qcmd->callback = callback; | |
529 | qcmd->data = data; | |
530 | } | |
531 | } | |
532 | bufcat(buf, part, wcslen(part)); | |
533 | num++; | |
534 | if(freepart) | |
535 | free(part); | |
536 | } | |
537 | } | |
538 | bufcat(buf, L"\r\n\0", 3); | |
539 | if((final = icwcstombs(buf, "utf-8")) == NULL) | |
540 | { | |
541 | free(buf); | |
542 | return(-1); | |
543 | } | |
544 | va_end(al); | |
545 | free(buf); | |
546 | qcmd->buf = final; | |
547 | qcmd->buflen = strlen(final); | |
548 | return(qcmd->tag); | |
549 | } | |
550 | ||
551 | int dc_handleread(void) | |
552 | { | |
553 | int ret, done; | |
554 | char *p1, *p2; | |
555 | size_t len; | |
26d72b0d | 556 | socklen_t optlen; |
d3372da9 | 557 | int errnobak; |
558 | /* Ewww... this really is soo ugly. I need to clean this up some day. */ | |
559 | static int pstate = 0; | |
560 | static char inbuf[128]; | |
561 | static size_t inbufdata = 0; | |
562 | static wchar_t *cbuf = NULL; | |
563 | static size_t cbufsize = 0, cbufdata = 0; | |
564 | static wchar_t *pptr = NULL; | |
565 | static wchar_t **argv = NULL; | |
26d72b0d | 566 | static int argc = 0; |
567 | static size_t args = 0; | |
d3372da9 | 568 | static wchar_t *cw = NULL; |
569 | static size_t cwsize = 0, cwdata = 0; | |
570 | static struct dc_response *curresp = NULL; | |
571 | static int cont = 0; | |
572 | static int unlink = 0; | |
573 | ||
574 | switch(state) | |
575 | { | |
576 | case -1: | |
577 | return(-1); | |
578 | case 0: | |
26d72b0d | 579 | optlen = sizeof(ret); |
580 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &optlen); | |
d3372da9 | 581 | if(ret) |
582 | { | |
583 | int newfd; | |
0538f877 | 584 | |
585 | message(2, "could not connect to %s: %s\n", formataddress(curhost->ai_addr, curhost->ai_addrlen), strerror(ret)); | |
d3372da9 | 586 | for(curhost = curhost->ai_next; curhost != NULL; curhost = curhost->ai_next) |
587 | { | |
588 | if((newfd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0) | |
589 | { | |
590 | errnobak = errno; | |
591 | dc_disconnect(); | |
592 | errno = errnobak; | |
593 | return(-1); | |
594 | } | |
595 | dup2(newfd, fd); | |
596 | close(newfd); | |
597 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); | |
0538f877 | 598 | message(4, "connecting to %s\n", formataddress(curhost->ai_addr, curhost->ai_addrlen)); |
12383d48 | 599 | if(connect(fd, (struct sockaddr *)curhost->ai_addr, curhost->ai_addrlen)) |
d3372da9 | 600 | { |
601 | if(errno == EINPROGRESS) | |
602 | return(0); | |
0538f877 | 603 | message(2, "could not connect to %s: %s\n", formataddress(curhost->ai_addr, curhost->ai_addrlen), strerror(ret)); |
d3372da9 | 604 | } else { |
605 | break; | |
606 | } | |
607 | } | |
608 | if(curhost == NULL) | |
609 | { | |
610 | dc_disconnect(); | |
611 | errno = ret; | |
612 | return(-1); | |
613 | } | |
614 | } | |
12383d48 | 615 | if(curhost->ai_canonname != NULL) |
9e5f2b29 | 616 | servinfo.hostname = sstrdup(curhost->ai_canonname); |
617 | servinfo.family = curhost->ai_family; | |
d3372da9 | 618 | state = 1; |
619 | resetreader = 1; | |
620 | break; | |
621 | case 1: | |
622 | if(resetreader) | |
623 | { | |
624 | inbufdata = 0; | |
625 | cbufdata = 0; | |
626 | pptr = NULL; | |
627 | pstate = 0; | |
628 | resetreader = 0; | |
629 | cont = 0; | |
630 | if(curresp != NULL) | |
631 | dc_freeresp(curresp); | |
632 | curresp = NULL; | |
633 | } | |
634 | if(inbufdata == 128) | |
635 | inbufdata = 0; | |
636 | ret = read(fd, inbuf + inbufdata, 128 - inbufdata); | |
637 | if(ret < 0) | |
638 | { | |
639 | if((errno == EAGAIN) || (errno == EINTR)) | |
640 | return(0); | |
641 | errnobak = errno; | |
642 | dc_disconnect(); | |
643 | errno = errnobak; | |
644 | return(-1); | |
645 | } else if(ret == 0) { | |
646 | dc_disconnect(); | |
647 | errno = 0; | |
648 | return(-1); | |
649 | } | |
650 | inbufdata += ret; | |
651 | done = 0; | |
652 | while(!done) | |
653 | { | |
654 | if(cbufsize == cbufdata) | |
655 | { | |
656 | if(pptr != NULL) | |
657 | len = pptr - cbuf; | |
658 | if((cbuf = realloc(cbuf, sizeof(wchar_t) * (cbufsize += 256))) == NULL) | |
659 | { | |
660 | dc_disconnect(); | |
661 | errno = ENOMEM; | |
662 | return(-1); | |
663 | } | |
664 | if(pptr != NULL) | |
665 | pptr = cbuf + len; | |
666 | } | |
667 | p1 = inbuf; | |
668 | p2 = (char *)(cbuf + cbufdata); | |
669 | len = sizeof(wchar_t) * (cbufsize - cbufdata); | |
670 | ret = iconv(ichandle, &p1, &inbufdata, &p2, &len); | |
671 | memmove(inbuf, p1, inbufdata); | |
672 | cbufdata = cbufsize - (len / sizeof(wchar_t)); | |
673 | if(ret < 0) | |
674 | { | |
675 | switch(errno) | |
676 | { | |
677 | case EILSEQ: | |
678 | /* XXX Is this really OK? */ | |
679 | inbufdata = 0; | |
680 | done = 1; | |
681 | break; | |
682 | case EINVAL: | |
683 | done = 1; | |
684 | break; | |
685 | case E2BIG: | |
686 | break; | |
687 | default: | |
688 | errnobak = errno; | |
689 | dc_disconnect(); | |
690 | errno = errnobak; | |
691 | return(-1); | |
692 | } | |
693 | } else { | |
694 | done = 1; | |
695 | } | |
696 | } | |
697 | if(pptr == NULL) | |
698 | pptr = cbuf; | |
699 | done = 0; | |
700 | while(!done && (pptr - cbuf < cbufdata)) | |
701 | { | |
702 | switch(pstate) | |
703 | { | |
704 | case 0: | |
705 | if(iswspace(*pptr)) | |
706 | { | |
707 | if(*pptr == L'\r') | |
708 | { | |
709 | if(pptr == cbuf + cbufdata - 1) | |
710 | { | |
711 | done = 1; | |
712 | break; | |
713 | } | |
714 | if(*(++pptr) == L'\n') | |
715 | { | |
716 | if(curresp == NULL) | |
717 | { | |
718 | curresp = makeresp(); | |
719 | if((argc > 0) && ((curresp->code = wcstol(argv[0], NULL, 10)) >= 600)) | |
720 | { | |
721 | curresp->cmdname = L".notify"; | |
722 | curresp->internal = commands->next; | |
723 | curresp->tag = -1; | |
724 | unlink = 0; | |
725 | } else { | |
726 | if((curresp->cmdname = queue->cmd->name) == NULL) | |
727 | curresp->cmdname = L".connect"; | |
728 | curresp->data = queue->data; | |
729 | curresp->tag = queue->tag; | |
730 | curresp->internal = (void *)(queue->cmd); | |
731 | unlink = 1; | |
732 | } | |
733 | } | |
734 | sizebuf(&curresp->rlines, &curresp->linessize, curresp->numlines + 1, sizeof(*(curresp->rlines)), 1); | |
735 | curresp->rlines[curresp->numlines].argc = argc; | |
736 | curresp->rlines[curresp->numlines].argv = argv; | |
737 | argv = NULL; | |
738 | argc = args = 0; | |
739 | curresp->numlines++; | |
740 | if(!cont) | |
741 | { | |
742 | if((curresp->code >= 600) || (queue == NULL) || (queue->callback == NULL)) | |
743 | ret = 0; | |
744 | else | |
745 | ret = queue->callback(curresp); | |
746 | if(ret == 0) | |
747 | { | |
748 | if(respqueue == NULL) | |
749 | { | |
750 | respqueue = respqueueend = curresp; | |
751 | } else { | |
752 | curresp->next = NULL; | |
753 | curresp->prev = respqueueend; | |
754 | respqueueend->next = curresp; | |
755 | respqueueend = curresp; | |
756 | } | |
757 | } else if(ret == 1) { | |
758 | dc_freeresp(curresp); | |
759 | } | |
760 | curresp = NULL; | |
761 | if(unlink) | |
762 | unlinkqueue(); | |
763 | } | |
764 | wmemmove(cbuf, pptr, cbufdata -= (pptr - cbuf)); | |
765 | pptr = cbuf; | |
766 | } else { | |
767 | pptr++; | |
768 | } | |
769 | } else { | |
770 | pptr++; | |
771 | } | |
772 | } else { | |
773 | pstate = 1; | |
774 | cwdata = 0; | |
775 | } | |
776 | break; | |
777 | case 1: | |
778 | if(iswspace(*pptr) || ((argc == 0) && (*pptr == L'-'))) | |
779 | { | |
780 | if(argc == 0) | |
781 | { | |
782 | if(*pptr == L'-') | |
783 | { | |
784 | cont = 1; | |
785 | pptr++; | |
786 | } else { | |
787 | cont = 0; | |
788 | } | |
789 | } | |
790 | addtobuf(cw, L'\0'); | |
791 | sizebuf(&argv, &args, argc + 1, sizeof(*argv), 1); | |
792 | argv[argc++] = cw; | |
793 | cw = NULL; | |
794 | cwsize = cwdata = 0; | |
795 | pstate = 0; | |
796 | } else if(*pptr == L'\"') { | |
797 | pstate = 2; | |
798 | pptr++; | |
799 | } else if(*pptr == L'\\') { | |
800 | if(pptr == cbuf + cbufdata - 1) | |
801 | { | |
802 | done = 1; | |
803 | break; | |
804 | } | |
805 | addtobuf(cw, *(++pptr)); | |
806 | pptr++; | |
807 | } else { | |
808 | addtobuf(cw, *(pptr++)); | |
809 | } | |
810 | break; | |
811 | case 2: | |
812 | if(*pptr == L'\"') | |
813 | { | |
814 | pstate = 1; | |
815 | } else if(*pptr == L'\\') { | |
816 | addtobuf(cw, *(++pptr)); | |
817 | } else { | |
818 | addtobuf(cw, *pptr); | |
819 | } | |
820 | pptr++; | |
821 | break; | |
822 | } | |
823 | } | |
824 | break; | |
825 | } | |
826 | return(0); | |
827 | } | |
828 | ||
a8c5ada6 | 829 | #if UNIX_AUTH_STYLE == 1 |
9e5f2b29 | 830 | static void mkcreds(struct msghdr *msg) |
831 | { | |
832 | struct ucred *ucred; | |
833 | static char buf[CMSG_SPACE(sizeof(*ucred))]; | |
834 | struct cmsghdr *cmsg; | |
835 | ||
836 | msg->msg_control = buf; | |
837 | msg->msg_controllen = sizeof(buf); | |
838 | cmsg = CMSG_FIRSTHDR(msg); | |
839 | cmsg->cmsg_level = SOL_SOCKET; | |
840 | cmsg->cmsg_type = SCM_CREDENTIALS; | |
841 | cmsg->cmsg_len = CMSG_LEN(sizeof(*ucred)); | |
842 | ucred = (struct ucred *)CMSG_DATA(cmsg); | |
843 | ucred->pid = getpid(); | |
844 | ucred->uid = getuid(); | |
845 | ucred->gid = getgid(); | |
846 | msg->msg_controllen = cmsg->cmsg_len; | |
847 | } | |
a8c5ada6 | 848 | #endif |
9e5f2b29 | 849 | |
d3372da9 | 850 | int dc_handlewrite(void) |
851 | { | |
852 | int ret; | |
853 | int errnobak; | |
9e5f2b29 | 854 | struct msghdr msg; |
855 | struct iovec bufvec; | |
d3372da9 | 856 | |
857 | switch(state) | |
858 | { | |
859 | case 1: | |
860 | if(queue->buflen > 0) | |
861 | { | |
9e5f2b29 | 862 | memset(&msg, 0, sizeof(msg)); |
863 | msg.msg_iov = &bufvec; | |
864 | msg.msg_iovlen = 1; | |
865 | bufvec.iov_base = queue->buf; | |
866 | bufvec.iov_len = queue->buflen; | |
a8c5ada6 | 867 | #if UNIX_AUTH_STYLE == 1 |
9e5f2b29 | 868 | if((servinfo.family == PF_UNIX) && !servinfo.sentcreds) |
869 | { | |
870 | mkcreds(&msg); | |
871 | servinfo.sentcreds = 1; | |
872 | } | |
a8c5ada6 | 873 | #endif |
9e5f2b29 | 874 | ret = sendmsg(fd, &msg, MSG_NOSIGNAL | MSG_DONTWAIT); |
d3372da9 | 875 | if(ret < 0) |
876 | { | |
877 | if((errno == EAGAIN) || (errno == EINTR)) | |
878 | return(0); | |
879 | errnobak = errno; | |
880 | dc_disconnect(); | |
881 | errno = errnobak; | |
882 | return(-1); | |
883 | } | |
884 | if(ret > 0) | |
885 | memmove(queue->buf, queue->buf + ret, queue->buflen -= ret); | |
886 | } | |
887 | break; | |
888 | } | |
889 | return(0); | |
890 | } | |
891 | ||
892 | #ifdef HAVE_RESOLVER | |
0c58c2a0 | 893 | /* |
894 | * It kind of sucks that libresolv doesn't have any DNS parsing | |
895 | * routines. We'll have to do it manually. | |
896 | */ | |
d3372da9 | 897 | static char *readname(unsigned char *msg, unsigned char *eom, unsigned char **p) |
898 | { | |
899 | char *name, *tname; | |
900 | unsigned char *tp; | |
901 | size_t namesize, namedata, len; | |
902 | ||
903 | name = NULL; | |
904 | namesize = namedata = 0; | |
905 | while(1) | |
906 | { | |
907 | len = *((*p)++); | |
908 | if(len == 0) | |
909 | { | |
910 | addtobuf(name, 0); | |
911 | return(name); | |
912 | } else if(len == 0xc0) { | |
913 | tp = msg + *((*p)++); | |
914 | if((tname = readname(msg, eom, &tp)) == NULL) | |
915 | { | |
916 | if(name != NULL) | |
917 | free(name); | |
918 | return(NULL); | |
919 | } | |
920 | bufcat(name, tname, strlen(tname)); | |
921 | addtobuf(name, 0); | |
922 | free(tname); | |
923 | return(name); | |
924 | } else if(*p + len >= eom) { | |
925 | if(name != NULL) | |
926 | free(name); | |
927 | return(NULL); | |
928 | } | |
929 | bufcat(name, *p, len); | |
930 | *p += len; | |
931 | addtobuf(name, '.'); | |
932 | } | |
933 | } | |
934 | ||
935 | static int skipname(unsigned char *msg, unsigned char *eom, unsigned char **p) | |
936 | { | |
937 | size_t len; | |
938 | ||
939 | while(1) | |
940 | { | |
941 | len = *((*p)++); | |
942 | if(len == 0) | |
943 | { | |
944 | return(0); | |
945 | } else if(len == 0xc0) { | |
946 | (*p)++; | |
947 | return(0); | |
948 | } else if(*p + len >= eom) { | |
949 | return(-1); | |
950 | } | |
951 | *p += len; | |
952 | } | |
953 | } | |
954 | ||
955 | static int getsrvrr(char *name, char **host, int *port) | |
956 | { | |
957 | int i; | |
958 | char *name2, *rrname; | |
959 | unsigned char *eom, *p; | |
960 | unsigned char buf[1024]; | |
961 | int flags, num, class, type; | |
962 | size_t len; | |
963 | int ret; | |
964 | ||
965 | if(!(_res.options & RES_INIT)) | |
966 | { | |
967 | if(res_init() < 0) | |
968 | return(-1); | |
969 | } | |
970 | /* res_querydomain doesn't work for some reason */ | |
4d487bad | 971 | if(name[strlen(name) - 1] == '.') |
972 | name2 = sprintf2("%s.%s", DOLCON_SRV_NAME, name); | |
973 | else | |
974 | name2 = sprintf2("%s.%s.", DOLCON_SRV_NAME, name); | |
d3372da9 | 975 | ret = res_query(name2, C_IN, T_SRV, buf, sizeof(buf)); |
976 | if(ret < 0) | |
977 | { | |
978 | free(name2); | |
979 | return(-1); | |
980 | } | |
981 | eom = buf + ret; | |
0c58c2a0 | 982 | /* |
983 | * Assume transaction ID is correct. | |
984 | * | |
985 | * Flags check: FA0F masks in request/response flag, opcode, | |
986 | * truncated flag and status code, and ignores authoritativeness, | |
987 | * recursion flags and DNSSEC and reserved bits. | |
988 | */ | |
d3372da9 | 989 | flags = (buf[2] << 8) + buf[3]; |
990 | if((flags & 0xfa0f) != 0x8000) | |
991 | { | |
992 | free(name2); | |
993 | return(-1); | |
994 | } | |
0c58c2a0 | 995 | /* Skip the query entries */ |
d3372da9 | 996 | num = (buf[4] << 8) + buf[5]; |
997 | p = buf + 12; | |
998 | for(i = 0; i < num; i++) | |
999 | { | |
1000 | if(skipname(buf, eom, &p)) | |
1001 | { | |
1002 | free(name2); | |
1003 | return(-1); | |
1004 | } | |
0c58c2a0 | 1005 | p += 4; /* Type and class */ |
d3372da9 | 1006 | } |
0c58c2a0 | 1007 | /* Parse answers */ |
d3372da9 | 1008 | num = (buf[6] << 8) + buf[7]; |
1009 | for(i = 0; i < num; i++) | |
1010 | { | |
1011 | if((rrname = readname(buf, eom, &p)) == NULL) | |
1012 | { | |
1013 | free(name2); | |
1014 | return(-1); | |
1015 | } | |
1016 | type = *(p++) << 8; | |
1017 | type += *(p++); | |
1018 | class = *(p++) << 8; | |
1019 | class += *(p++); | |
0c58c2a0 | 1020 | p += 4; /* TTL */ |
d3372da9 | 1021 | len = *(p++) << 8; |
1022 | len += *(p++); | |
1023 | if((class == C_IN) && (type == T_SRV) && !strcmp(rrname, name2)) | |
1024 | { | |
1025 | free(rrname); | |
1026 | free(name2); | |
1027 | /* Noone will want to have alternative DC servers, so | |
1028 | * don't care about priority and weigth */ | |
1029 | p += 4; | |
1030 | if(port == NULL) | |
1031 | { | |
1032 | p += 2; | |
1033 | } else { | |
1034 | *port = *(p++) << 8; | |
1035 | *port += *(p++); | |
1036 | } | |
1037 | if(host != NULL) | |
1038 | { | |
1039 | if((rrname = readname(buf, eom, &p)) == NULL) | |
1040 | return(-1); | |
1041 | *host = rrname; | |
1042 | } | |
1043 | return(0); | |
1044 | } | |
1045 | p += len; | |
1046 | free(rrname); | |
1047 | } | |
1048 | free(name2); | |
1049 | return(-1); | |
1050 | } | |
1051 | #else | |
1052 | static int getsrvrr(char *name, char **host, int *port) | |
1053 | { | |
c4c34936 | 1054 | errno = EOPNOTSUPP; |
d3372da9 | 1055 | return(-1); |
1056 | } | |
1057 | #endif | |
1058 | ||
12383d48 | 1059 | static struct addrinfo *gaicat(struct addrinfo *l1, struct addrinfo *l2) |
d3372da9 | 1060 | { |
12383d48 | 1061 | struct addrinfo *p; |
1062 | ||
1063 | if(l1 == NULL) | |
1064 | return(l2); | |
1065 | for(p = l1; p->ai_next != NULL; p = p->ai_next); | |
1066 | p->ai_next = l2; | |
1067 | return(l1); | |
1068 | } | |
1069 | ||
1070 | /* This isn't actually correct, in any sense of the word. It only | |
1071 | * works on systems whose getaddrinfo implementation saves the | |
1072 | * sockaddr in the same malloc block as the struct addrinfo. Those | |
1073 | * systems include at least FreeBSD and glibc-based systems, though, | |
1074 | * so it should not be any immediate threat, and it allows me to not | |
1075 | * implement a getaddrinfo wrapper. It can always be changed, should | |
1076 | * the need arise. */ | |
1077 | static struct addrinfo *unixgai(int type, char *path) | |
1078 | { | |
1079 | void *buf; | |
1080 | struct addrinfo *ai; | |
1081 | struct sockaddr_un *un; | |
1082 | ||
1083 | buf = smalloc(sizeof(*ai) + sizeof(*un)); | |
1084 | memset(buf, 0, sizeof(*ai) + sizeof(*un)); | |
1085 | ai = (struct addrinfo *)buf; | |
1086 | un = (struct sockaddr_un *)(buf + sizeof(*ai)); | |
1087 | ai->ai_flags = 0; | |
1088 | ai->ai_family = AF_UNIX; | |
1089 | ai->ai_socktype = type; | |
1090 | ai->ai_protocol = 0; | |
1091 | ai->ai_addrlen = sizeof(*un); | |
1092 | ai->ai_addr = (struct sockaddr *)un; | |
1093 | ai->ai_canonname = NULL; | |
1094 | ai->ai_next = NULL; | |
1095 | un->sun_family = PF_UNIX; | |
1096 | strncpy(un->sun_path, path, sizeof(un->sun_path) - 1); | |
1097 | return(ai); | |
1098 | } | |
1099 | ||
1100 | static struct addrinfo *resolvtcp(char *name, int port) | |
1101 | { | |
1102 | struct addrinfo hint, *ret; | |
1103 | char tmp[32]; | |
d3372da9 | 1104 | |
d3372da9 | 1105 | memset(&hint, 0, sizeof(hint)); |
1106 | hint.ai_socktype = SOCK_STREAM; | |
12383d48 | 1107 | hint.ai_flags = AI_NUMERICSERV | AI_CANONNAME; |
1108 | snprintf(tmp, sizeof(tmp), "%i", port); | |
1109 | if(!getaddrinfo(name, tmp, &hint, &ret)) | |
1110 | return(ret); | |
1111 | return(NULL); | |
1112 | } | |
1113 | ||
1114 | static struct addrinfo *resolvsrv(char *name) | |
1115 | { | |
1116 | struct addrinfo *ret; | |
1117 | char *realname; | |
1118 | int port; | |
1119 | ||
1120 | if(getsrvrr(name, &realname, &port)) | |
1121 | return(NULL); | |
0538f877 | 1122 | message(4, "SRV RR resolved: %s -> %s\n", name, realname); |
12383d48 | 1123 | ret = resolvtcp(realname, port); |
1124 | free(realname); | |
1125 | return(ret); | |
1126 | } | |
1127 | ||
1128 | static struct addrinfo *resolvhost(char *host) | |
1129 | { | |
1130 | char *p, *hp; | |
1131 | struct addrinfo *ret; | |
1132 | int port; | |
1133 | ||
1134 | if(strchr(host, '/')) | |
1135 | return(unixgai(SOCK_STREAM, host)); | |
1136 | if((strchr(host, ':') == NULL) && ((ret = resolvsrv(host)) != NULL)) | |
1137 | return(ret); | |
1138 | ret = NULL; | |
1139 | if((*host == '[') && ((p = strchr(host, ']')) != NULL)) | |
d3372da9 | 1140 | { |
12383d48 | 1141 | hp = memcpy(smalloc(p - host), host + 1, (p - host) - 1); |
1142 | hp[(p - host) - 1] = 0; | |
1143 | if(strchr(hp, ':') != NULL) { | |
1144 | port = 0; | |
1145 | if(*(++p) == ':') | |
1146 | port = atoi(p + 1); | |
1147 | if(port == 0) | |
1148 | port = 1500; | |
1149 | ret = resolvtcp(hp, port); | |
d3372da9 | 1150 | } |
12383d48 | 1151 | free(hp); |
1152 | } | |
1153 | if(ret != NULL) | |
1154 | return(ret); | |
1155 | hp = sstrdup(host); | |
1156 | port = 0; | |
1157 | if((p = strrchr(hp, ':')) != NULL) { | |
1158 | *(p++) = 0; | |
1159 | port = atoi(p); | |
1160 | } | |
1161 | if(port == 0) | |
1162 | port = 1500; | |
1163 | ret = resolvtcp(hp, port); | |
1164 | free(hp); | |
1165 | if(ret != NULL) | |
1166 | return(ret); | |
1167 | return(NULL); | |
1168 | } | |
1169 | ||
8affe2ff | 1170 | static struct addrinfo *getlocalai(void) |
12383d48 | 1171 | { |
1172 | struct addrinfo *ret; | |
1173 | struct passwd *pwd; | |
1174 | char *tmp; | |
8affe2ff | 1175 | |
12383d48 | 1176 | ret = NULL; |
1177 | if((getuid() != 0) && ((pwd = getpwuid(getuid())) != NULL)) | |
1178 | { | |
1179 | tmp = sprintf2("/tmp/doldacond-%s", pwd->pw_name); | |
8affe2ff | 1180 | ret = unixgai(SOCK_STREAM, tmp); |
12383d48 | 1181 | free(tmp); |
d3372da9 | 1182 | } |
12383d48 | 1183 | ret = gaicat(ret, unixgai(SOCK_STREAM, "/var/run/doldacond.sock")); |
8affe2ff | 1184 | return(ret); |
1185 | } | |
1186 | ||
1187 | static struct addrinfo *defaulthost(void) | |
1188 | { | |
1189 | struct addrinfo *ret; | |
1190 | char *tmp; | |
1191 | char dn[1024]; | |
1192 | ||
0538f877 | 1193 | if(((tmp = getenv("DCSERVER")) != NULL) && *tmp) { |
1194 | message(4, "using DCSERVER: %s\n", tmp); | |
8affe2ff | 1195 | return(resolvhost(tmp)); |
0538f877 | 1196 | } |
8affe2ff | 1197 | ret = getlocalai(); |
12383d48 | 1198 | ret = gaicat(ret, resolvtcp("localhost", 1500)); |
1199 | if(!getdomainname(dn, sizeof(dn)) && *dn && strcmp(dn, "(none)")) | |
1200 | ret = gaicat(ret, resolvsrv(dn)); | |
1201 | return(ret); | |
1202 | } | |
1203 | ||
e4b88cc0 | 1204 | static int dc_connectai(struct addrinfo *hosts, struct qcmd **cnctcmd) |
12383d48 | 1205 | { |
1206 | struct qcmd *qcmd; | |
1207 | int errnobak; | |
1208 | ||
1209 | if(fd >= 0) | |
1210 | dc_disconnect(); | |
1211 | state = -1; | |
d3372da9 | 1212 | if(hostlist != NULL) |
1213 | freeaddrinfo(hostlist); | |
8affe2ff | 1214 | hostlist = hosts; |
d3372da9 | 1215 | for(curhost = hostlist; curhost != NULL; curhost = curhost->ai_next) |
1216 | { | |
1217 | if((fd = socket(curhost->ai_family, curhost->ai_socktype, curhost->ai_protocol)) < 0) | |
1218 | { | |
1219 | errnobak = errno; | |
e4b88cc0 | 1220 | freeaddrinfo(hostlist); |
1221 | hostlist = NULL; | |
d3372da9 | 1222 | errno = errnobak; |
1223 | return(-1); | |
1224 | } | |
1225 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); | |
0538f877 | 1226 | message(4, "connecting to %s\n", formataddress(curhost->ai_addr, curhost->ai_addrlen)); |
12383d48 | 1227 | if(connect(fd, (struct sockaddr *)curhost->ai_addr, curhost->ai_addrlen)) |
d3372da9 | 1228 | { |
1229 | if(errno == EINPROGRESS) | |
1230 | { | |
1231 | state = 0; | |
1232 | break; | |
1233 | } | |
0538f877 | 1234 | message(2, "could not connect to %s: %s\n", formataddress(curhost->ai_addr, curhost->ai_addrlen), strerror(errno)); |
d3372da9 | 1235 | close(fd); |
1236 | fd = -1; | |
1237 | } else { | |
12383d48 | 1238 | if(curhost->ai_canonname != NULL) |
9e5f2b29 | 1239 | servinfo.hostname = sstrdup(curhost->ai_canonname); |
1240 | servinfo.family = curhost->ai_family; | |
d3372da9 | 1241 | state = 1; |
1242 | break; | |
1243 | } | |
1244 | } | |
e4b88cc0 | 1245 | if(state != -1) |
1246 | { | |
1247 | qcmd = makeqcmd(NULL); | |
1248 | if(cnctcmd != NULL) | |
1249 | *cnctcmd = qcmd; | |
1250 | resetreader = 1; | |
1251 | } else { | |
1252 | free(hostlist); | |
1253 | hostlist = NULL; | |
1254 | } | |
d3372da9 | 1255 | return(fd); |
1256 | } | |
1257 | ||
e4b88cc0 | 1258 | static int dc_connect2(char *host, struct qcmd **cnctcmd) |
8affe2ff | 1259 | { |
1260 | struct addrinfo *ai; | |
e4b88cc0 | 1261 | struct qcmd *qcmd; |
8affe2ff | 1262 | int ret; |
1263 | ||
0538f877 | 1264 | if(host == dc_srv_local) { |
1265 | message(4, "connect start: Unix\n"); | |
e4b88cc0 | 1266 | ai = getlocalai(); |
0538f877 | 1267 | } else if(!host || !*host) { |
1268 | message(4, "connect start: default\n"); | |
8affe2ff | 1269 | ai = defaulthost(); |
0538f877 | 1270 | } else { |
1271 | message(4, "connect start: host %s\n", host); | |
8affe2ff | 1272 | ai = resolvhost(host); |
0538f877 | 1273 | } |
8affe2ff | 1274 | if(ai == NULL) |
1275 | return(-1); | |
e4b88cc0 | 1276 | ret = dc_connectai(ai, &qcmd); |
1277 | if((ret >= 0) && (cnctcmd != NULL)) | |
1278 | *cnctcmd = qcmd; | |
8affe2ff | 1279 | return(ret); |
1280 | } | |
1281 | ||
e4b88cc0 | 1282 | int dc_connect(char *host) |
1283 | { | |
1284 | return(dc_connect2(host, NULL)); | |
1285 | } | |
1286 | ||
1287 | int dc_connectsync(char *host, struct dc_response **respbuf) | |
8affe2ff | 1288 | { |
8affe2ff | 1289 | int ret; |
e4b88cc0 | 1290 | struct qcmd *cc; |
1291 | struct dc_response *resp; | |
8affe2ff | 1292 | |
e4b88cc0 | 1293 | if((ret = dc_connect2(host, &cc)) < 0) |
1294 | return(-1); | |
1295 | resp = dc_gettaggedrespsync(cc->tag); | |
1296 | if(resp == NULL) { | |
1297 | dc_disconnect(); | |
1298 | return(-1); | |
1299 | } | |
1300 | if(respbuf == NULL) | |
1301 | dc_freeresp(resp); | |
1302 | else | |
1303 | *respbuf = resp; | |
1304 | return(ret); | |
1305 | } | |
1306 | ||
1307 | int dc_connectsync2(char *host, int rev) | |
1308 | { | |
1309 | int ret; | |
1310 | struct dc_response *resp; | |
1311 | ||
1312 | if((ret = dc_connectsync(host, &resp)) < 0) | |
1313 | return(-1); | |
1314 | if(dc_checkprotocol(resp, rev)) | |
1315 | { | |
1316 | dc_freeresp(resp); | |
1317 | dc_disconnect(); | |
a24f31ec | 1318 | errno = EPROTONOSUPPORT; |
e4b88cc0 | 1319 | return(-1); |
1320 | } | |
1321 | dc_freeresp(resp); | |
8affe2ff | 1322 | return(ret); |
1323 | } | |
1324 | ||
d3372da9 | 1325 | struct dc_intresp *dc_interpret(struct dc_response *resp) |
1326 | { | |
1327 | int i; | |
1328 | struct dc_intresp *iresp; | |
1329 | struct command *cmd; | |
1330 | struct respclass *cls; | |
1331 | int code; | |
26d72b0d | 1332 | size_t args; |
d3372da9 | 1333 | |
1334 | if((resp->numlines == 0) || (resp->rlines[0].argc == 0) || (resp->curline >= resp->numlines)) | |
1335 | return(NULL); | |
1336 | code = wcstol(resp->rlines[0].argv[0], NULL, 10); | |
1337 | cmd = (struct command *)(resp->internal); | |
1338 | for(cls = cmd->classes; cls != NULL; cls = cls->next) | |
1339 | { | |
1340 | if(cls->code == code) | |
1341 | break; | |
1342 | } | |
1343 | if(cls == NULL) | |
1344 | return(NULL); | |
1345 | if(cls->nwords >= resp->rlines[resp->curline].argc) | |
1346 | return(NULL); | |
1347 | iresp = smalloc(sizeof(*iresp)); | |
1348 | iresp->code = code; | |
1349 | iresp->argv = NULL; | |
1350 | iresp->argc = 0; | |
1351 | args = 0; | |
1352 | for(i = 0; i < cls->nwords; i++) | |
1353 | { | |
1354 | switch(cls->wordt[i]) | |
1355 | { | |
1356 | case RESP_DSC: | |
1357 | break; | |
1358 | case RESP_STR: | |
1359 | sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1); | |
1360 | iresp->argv[iresp->argc].val.str = swcsdup(resp->rlines[resp->curline].argv[i + 1]); | |
1361 | iresp->argv[iresp->argc].type = cls->wordt[i]; | |
1362 | iresp->argc++; | |
1363 | break; | |
1364 | case RESP_INT: | |
1365 | sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1); | |
1366 | iresp->argv[iresp->argc].val.num = wcstol(resp->rlines[resp->curline].argv[i + 1], NULL, 0); | |
1367 | iresp->argv[iresp->argc].type = cls->wordt[i]; | |
1368 | iresp->argc++; | |
1369 | break; | |
1370 | case RESP_FLOAT: | |
1371 | sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1); | |
1372 | iresp->argv[iresp->argc].val.flnum = wcstod(resp->rlines[resp->curline].argv[i + 1], NULL); | |
1373 | iresp->argv[iresp->argc].type = cls->wordt[i]; | |
1374 | iresp->argc++; | |
1375 | break; | |
8f623b36 FT |
1376 | case RESP_LNUM: |
1377 | sizebuf(&(iresp->argv), &args, iresp->argc + 1, sizeof(*(iresp->argv)), 1); | |
1378 | iresp->argv[iresp->argc].val.lnum = wcstoll(resp->rlines[resp->curline].argv[i + 1], NULL, 0); | |
1379 | iresp->argv[iresp->argc].type = cls->wordt[i]; | |
1380 | iresp->argc++; | |
1381 | break; | |
d3372da9 | 1382 | } |
1383 | } | |
1384 | resp->curline++; | |
1385 | return(iresp); | |
1386 | } | |
1387 | ||
1388 | void dc_freeires(struct dc_intresp *ires) | |
1389 | { | |
1390 | int i; | |
1391 | ||
1392 | for(i = 0; i < ires->argc; i++) | |
1393 | { | |
1394 | if(ires->argv[i].type == RESP_STR) | |
1395 | free(ires->argv[i].val.str); | |
1396 | } | |
1397 | free(ires->argv); | |
1398 | free(ires); | |
1399 | } | |
1400 | ||
9a0ef8e1 | 1401 | int dc_checkprotocol(struct dc_response *resp, int revision) |
1402 | { | |
1403 | struct dc_intresp *ires; | |
1404 | int low, high; | |
1405 | ||
1406 | if(resp->code != 201) | |
1407 | return(-1); | |
1408 | resp->curline = 0; | |
1409 | if((ires = dc_interpret(resp)) == NULL) | |
1410 | return(-1); | |
1411 | low = ires->argv[0].val.num; | |
e4b88cc0 | 1412 | high = ires->argv[1].val.num; |
9a0ef8e1 | 1413 | dc_freeires(ires); |
1414 | if((revision < low) || (revision > high)) | |
1415 | return(-1); | |
1416 | return(0); | |
1417 | } | |
1418 | ||
d3372da9 | 1419 | const char *dc_gethostname(void) |
1420 | { | |
9e5f2b29 | 1421 | return(servinfo.hostname); |
d3372da9 | 1422 | } |
e4b88cc0 | 1423 | |
1424 | int dc_getfd(void) | |
1425 | { | |
1426 | return(fd); | |
1427 | } |