Improve error reporting in dc-qcmd.
[doldaconnect.git] / daemon / transfer.c
CommitLineData
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#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <signal.h>
25#include <pwd.h>
26#include <grp.h>
27#include <errno.h>
28#include <sys/wait.h>
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include "log.h"
34#include "utils.h"
35#include "sysevents.h"
36#include "auth.h"
37#include "transfer.h"
38#include "module.h"
429d35e5 39#include "client.h"
d3372da9 40
41static void killfilter(struct transfer *transfer);
42
7a5e9d41 43unsigned long long bytesupload = 0;
44unsigned long long bytesdownload = 0;
d3372da9 45struct transfer *transfers = NULL;
46int numtransfers = 0;
47GCBCHAIN(newtransfercb, struct transfer *);
48
49void freetransfer(struct transfer *transfer)
50{
d3372da9 51 if(transfer == transfers)
52 transfers = transfer->next;
53 if(transfer->next != NULL)
54 transfer->next->prev = transfer->prev;
55 if(transfer->prev != NULL)
56 transfer->prev->next = transfer->next;
57 CBCHAINDOCB(transfer, trans_destroy, transfer);
58 CBCHAINFREE(transfer, trans_ac);
59 CBCHAINFREE(transfer, trans_act);
60 CBCHAINFREE(transfer, trans_p);
61 CBCHAINFREE(transfer, trans_destroy);
62 CBCHAINFREE(transfer, trans_filterout);
9ec790e8 63 while(transfer->args != NULL)
64 freewcspair(transfer->args, &transfer->args);
d3372da9 65 if(transfer->filter != -1)
66 killfilter(transfer);
67 if(transfer->etimer != NULL)
68 canceltimer(transfer->etimer);
69 if(transfer->auth != NULL)
70 authputhandle(transfer->auth);
71 if(transfer->peerid != NULL)
72 free(transfer->peerid);
73 if(transfer->peernick != NULL)
74 free(transfer->peernick);
75 if(transfer->path != NULL)
76 free(transfer->path);
77 if(transfer->actdesc != NULL)
78 free(transfer->actdesc);
79 if(transfer->filterbuf != NULL)
80 free(transfer->filterbuf);
429d35e5 81 if(transfer->hash != NULL)
82 freehash(transfer->hash);
0309b83d 83 if(transfer->exitstatus != NULL)
84 free(transfer->exitstatus);
d3372da9 85 if(transfer->localend != NULL)
86 {
cab0b442 87 transfer->localend->readcb = NULL;
88 transfer->localend->writecb = NULL;
89 transfer->localend->errcb = NULL;
d3372da9 90 putsock(transfer->localend);
91 }
92 if(transfer->filterout != NULL)
93 {
cab0b442 94 transfer->filterout->readcb = NULL;
95 transfer->filterout->writecb = NULL;
96 transfer->filterout->errcb = NULL;
d3372da9 97 putsock(transfer->filterout);
98 }
99 if(transfer->fn != NULL)
100 putfnetnode(transfer->fn);
101 free(transfer);
102 numtransfers--;
103}
104
105struct transfer *newtransfer(void)
106{
107 struct transfer *new;
108 static int curid = 0;
109
110 new = smalloc(sizeof(*new));
111 memset(new, 0, sizeof(*new));
112 new->id = curid++;
113 new->size = -1;
114 new->endpos = -1;
115 new->filter = -1;
116 CBCHAININIT(new, trans_ac);
117 CBCHAININIT(new, trans_act);
118 CBCHAININIT(new, trans_p);
119 CBCHAININIT(new, trans_destroy);
120 CBCHAININIT(new, trans_filterout);
121 new->next = NULL;
122 new->prev = NULL;
123 time(&new->activity);
124 numtransfers++;
125 return(new);
126}
127
d3372da9 128void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
129{
130 if(transfer->iface != NULL)
131 transferdetach(transfer);
132 transfer->iface = iface;
133 transfer->ifacedata = data;
134}
135
136void transferdetach(struct transfer *transfer)
137{
138 if(transfer->iface != NULL)
139 {
140 transfer->iface->detach(transfer, transfer->ifacedata);
141 transfer->iface = NULL;
142 transfer->ifacedata = NULL;
143 }
144}
145
2c086721 146struct transfer *finddownload(wchar_t *peerid)
147{
148 struct transfer *transfer;
149
150 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
151 {
152 if((transfer->dir == TRNSD_DOWN) && (transfer->iface == NULL) && !wcscmp(peerid, transfer->peerid))
153 break;
154 }
155 return(transfer);
156}
157
6c6bc9ce 158struct transfer *hasupload(struct fnet *fnet, wchar_t *peerid)
159{
160 struct transfer *transfer;
161
162 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
163 {
c458597e 164 if((transfer->dir == TRNSD_UP) && (transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
6c6bc9ce 165 break;
166 }
167 return(transfer);
168}
169
d3372da9 170struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
171{
172 struct transfer *transfer;
173
174 transfer = newtransfer();
175 if(fnet != NULL)
176 transfer->fnet = fnet;
177 else
178 transfer->fnet = fn->fnet;
179 transfer->peerid = swcsdup(nickid);
180 transfer->state = TRNS_HS;
181 transfer->dir = TRNSD_UP;
182 if(fn != NULL)
183 getfnetnode(transfer->fn = fn);
184 transferattach(transfer, iface, data);
185 linktransfer(transfer);
186 bumptransfer(transfer);
187 return(transfer);
188}
189
190void linktransfer(struct transfer *transfer)
191{
192 transfer->next = transfers;
193 transfer->prev = NULL;
194 if(transfers != NULL)
195 transfers->prev = transfer;
196 transfers = transfer;
197 GCBCHAINDOCB(newtransfercb, transfer);
198}
199
200void resettransfer(struct transfer *transfer)
201{
202 if(transfer->dir == TRNSD_DOWN)
203 {
204 if(transfer->iface != NULL)
205 transferdetach(transfer);
206 killfilter(transfer);
207 transfersetstate(transfer, TRNS_WAITING);
208 transfersetactivity(transfer, L"reset");
209 return;
210 }
211}
212
213struct transfer *findtransfer(int id)
214{
215 struct transfer *transfer;
216
217 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
218 {
219 if(transfer->id == id)
220 break;
221 }
222 return(transfer);
223}
224
225static void transexpire(int cancelled, struct transfer *transfer)
226{
227 transfer->etimer = NULL;
228 if(!cancelled)
229 bumptransfer(transfer);
230 else
231 transfer->timeout = 0;
232}
233
cab0b442 234static void transferread(struct socket *sk, struct transfer *transfer)
d3372da9 235{
236 if(sockgetdatalen(sk) >= 65536)
237 sk->ignread = 1;
238 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
239 transfer->iface->gotdata(transfer, transfer->ifacedata);
240}
241
cab0b442 242static void transferwrite(struct socket *sk, struct transfer *transfer)
d3372da9 243{
244 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
245 transfer->iface->wantdata(transfer, transfer->ifacedata);
246}
247
cab0b442 248static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
d3372da9 249{
250 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
251 transfer->iface->endofdata(transfer, transfer->ifacedata);
252}
253
254void transferputdata(struct transfer *transfer, void *buf, size_t size)
255{
256 time(&transfer->activity);
257 sockqueue(transfer->localend, buf, size);
258 transfer->curpos += size;
7a5e9d41 259 bytesdownload += size;
d3372da9 260 CBCHAINDOCB(transfer, trans_p, transfer);
261}
262
263void transferendofdata(struct transfer *transfer)
264{
265 if(transfer->curpos >= transfer->size)
266 {
267 transfersetstate(transfer, TRNS_DONE);
cab0b442 268 transfer->localend->readcb = NULL;
269 transfer->localend->writecb = NULL;
270 transfer->localend->errcb = NULL;
d3372da9 271 putsock(transfer->localend);
272 transfer->localend = NULL;
273 } else {
274 resettransfer(transfer);
275 }
276}
277
278size_t transferdatasize(struct transfer *transfer)
279{
280 return(sockqueuesize(transfer->localend));
281}
282
283void *transfergetdata(struct transfer *transfer, size_t *size)
284{
285 void *buf;
286
287 if(transfer->localend == NULL)
288 return(NULL);
289 transfer->localend->ignread = 0;
290 time(&transfer->activity);
291 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
292 return(NULL);
293 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
294 {
e3f10dc2
FT
295 if((*size = transfer->endpos - transfer->curpos) == 0) {
296 free(buf);
297 buf = NULL;
298 } else {
299 buf = srealloc(buf, *size);
300 }
d3372da9 301 }
302 transfer->curpos += *size;
7a5e9d41 303 bytesupload += *size;
d3372da9 304 CBCHAINDOCB(transfer, trans_p, transfer);
305 return(buf);
306}
307
308void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
309{
310 transfersetsize(transfer, size);
311 transfer->curpos = start;
312 transfer->endpos = end;
313 lesk->ignread = 1;
314 transfersetlocalend(transfer, lesk);
315}
316
2c086721 317void transferstartdl(struct transfer *transfer, struct socket *sk)
318{
319 transfersetstate(transfer, TRNS_MAIN);
320 socksettos(sk, confgetint("transfer", "dltos"));
321}
322
d3372da9 323void transferstartul(struct transfer *transfer, struct socket *sk)
324{
325 transfersetstate(transfer, TRNS_MAIN);
326 socksettos(sk, confgetint("transfer", "ultos"));
327 if(transfer->localend != NULL)
328 transfer->localend->ignread = 0;
329}
330
331void transfersetlocalend(struct transfer *transfer, struct socket *sk)
332{
333 if(transfer->localend != NULL)
334 putsock(transfer->localend);
335 getsock(transfer->localend = sk);
cab0b442 336 sk->data = transfer;
337 sk->readcb = (void (*)(struct socket *, void *))transferread;
338 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
339 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
d3372da9 340}
341
18f56d13 342static int tryreq(struct transfer *transfer)
d3372da9 343{
344 struct fnetnode *fn;
345 struct fnetpeer *peer;
18f56d13 346
347 if((fn = transfer->fn) != NULL)
348 {
349 if(fn->state != FNN_EST)
350 {
351 transfer->close = 1;
352 return(1);
353 }
354 peer = fnetfindpeer(fn, transfer->peerid);
355 } else {
356 peer = NULL;
357 for(fn = fnetnodes; fn != NULL; fn = fn->next)
358 {
359 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
360 break;
361 }
362 }
363 if(peer != NULL)
6329b4ba 364 {
365 time(&transfer->lastreq);
18f56d13 366 return(fn->fnet->reqconn(peer));
6329b4ba 367 }
18f56d13 368 return(1);
369}
370
371void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
372{
373 struct transfer *transfer;
374
375 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
376 {
377 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
378 {
379 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
380 {
381 if(!tryreq(transfer))
382 return;
383 }
384 }
385 }
386}
387
388void bumptransfer(struct transfer *transfer)
389{
d3372da9 390 time_t now;
391
392 if((now = time(NULL)) < transfer->timeout)
393 {
18f56d13 394
d3372da9 395 if(transfer->etimer == NULL)
396 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
397 return;
398 }
399 if(transfer->etimer != NULL)
400 canceltimer(transfer->etimer);
401 switch(transfer->state)
402 {
403 case TRNS_WAITING:
d3372da9 404 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
405 if(now - transfer->lastreq > 30)
18f56d13 406 tryreq(transfer);
d3372da9 407 break;
408 case TRNS_HS:
409 if(transfer->dir == TRNSD_UP)
410 {
411 if(now - transfer->activity < 60)
412 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
413 else
414 transfer->close = 1;
415 } else if(transfer->dir == TRNSD_DOWN) {
416 if(now - transfer->activity < 60)
417 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
418 else
419 resettransfer(transfer);
420 }
421 break;
422 case TRNS_MAIN:
423 if(transfer->dir == TRNSD_UP)
424 {
425 if(now - transfer->activity < 300)
426 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
427 else
428 transfer->close = 1;
429 }
430 break;
431 }
432}
433
434void transfersetactivity(struct transfer *transfer, wchar_t *desc)
435{
436 time(&transfer->activity);
437 if(desc != NULL)
438 {
439 if(transfer->actdesc != NULL)
440 free(transfer->actdesc);
441 transfer->actdesc = swcsdup(desc);
442 }
443 bumptransfer(transfer);
444 CBCHAINDOCB(transfer, trans_act, transfer);
445}
446
447void transfersetstate(struct transfer *transfer, int newstate)
448{
449 transfer->state = newstate;
450 if(transfer->etimer != NULL)
451 canceltimer(transfer->etimer);
452 transfersetactivity(transfer, NULL);
453 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
454}
455
456void transfersetnick(struct transfer *transfer, wchar_t *newnick)
457{
458 if(transfer->peernick != NULL)
459 free(transfer->peernick);
460 transfer->peernick = swcsdup(newnick);
461 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
462}
463
464void transfersetsize(struct transfer *transfer, int newsize)
465{
466 transfer->size = newsize;
467 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
468}
469
470void transferseterror(struct transfer *transfer, int error)
471{
472 transfer->error = error;
473 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
474}
475
476void transfersetpath(struct transfer *transfer, wchar_t *path)
477{
478 if(transfer->path != NULL)
479 free(transfer->path);
480 transfer->path = swcsdup(path);
481 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
482}
483
2c086721 484void transfersethash(struct transfer *transfer, struct hash *hash)
485{
486 if(transfer->hash != NULL)
487 freehash(transfer->hash);
488 transfer->hash = hash;
489 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
490}
491
d3372da9 492int slotsleft(void)
493{
494 struct transfer *transfer;
495 int slots;
496
497 slots = confgetint("transfer", "slots");
498 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
499 {
500 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
501 slots--;
502 }
503 return(slots);
504}
505
506static void killfilter(struct transfer *transfer)
507{
508 if(transfer->filter != -1)
509 {
510 kill(-transfer->filter, SIGHUP);
511 transfer->filter = -1;
512 }
513 if(transfer->localend)
514 {
cab0b442 515 transfer->localend->readcb = NULL;
516 transfer->localend->writecb = NULL;
517 transfer->localend->errcb = NULL;
d3372da9 518 putsock(transfer->localend);
519 transfer->localend = NULL;
520 }
521 if(transfer->filterout)
522 {
cab0b442 523 transfer->filterout->readcb = NULL;
d3372da9 524 putsock(transfer->filterout);
525 transfer->filterout = NULL;
526 }
527 if(transfer->filterbuf)
528 {
529 free(transfer->filterbuf);
530 transfer->filterbuf = NULL;
531 }
532 transfer->filterbufsize = transfer->filterbufdata = 0;
533}
534
0309b83d 535static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
536{
537 if(!wcscmp(cmd, L"status")) {
538 if(arg == NULL)
539 arg = L"";
540 if(transfer->exitstatus != NULL)
541 free(transfer->exitstatus);
542 transfer->exitstatus = swcsdup(arg);
543 }
544}
545
cab0b442 546static void filterread(struct socket *sk, struct transfer *transfer)
d3372da9 547{
548 char *buf, *p, *p2;
549 size_t bufsize;
550 wchar_t *cmd, *arg;
551
552 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
cab0b442 553 return;
d3372da9 554 bufcat(transfer->filterbuf, buf, bufsize);
555 free(buf);
5b193db0 556 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
d3372da9 557 {
558 *(p++) = 0;
559 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
560 *(p2++) = 0;
561 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
562 {
563 arg = NULL;
564 if(p2 != NULL)
565 {
566 if((arg = icmbstowcs(p2, NULL)) == NULL)
13b303d4 567 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
d3372da9 568 }
0309b83d 569 handletranscmd(transfer, cmd, arg);
d3372da9 570 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
571 if(arg != NULL)
572 free(arg);
573 free(cmd);
574 } else {
575 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
576 }
577 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
578 }
579}
580
581static void filterexit(pid_t pid, int status, void *data)
582{
583 struct transfer *transfer;
734dc54e 584 struct fnet *fnet;
585 wchar_t *peerid;
d3372da9 586
587 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
588 {
589 if(transfer->filter == pid)
590 {
591 transfer->filter = -1;
592 killfilter(transfer);
734dc54e 593 fnet = transfer->fnet;
594 peerid = swcsdup(transfer->peerid);
d3372da9 595 if(WEXITSTATUS(status))
d3372da9 596 resettransfer(transfer);
734dc54e 597 else
d3372da9 598 freetransfer(transfer);
734dc54e 599 trytransferbypeer(fnet, peerid);
600 free(peerid);
d3372da9 601 break;
602 }
603 }
604}
605
606int forkfilter(struct transfer *transfer)
607{
608 char *filtername, *filename, *peerid, *buf;
609 wchar_t *wfilename;
610 struct passwd *pwent;
611 pid_t pid;
612 int inpipe, outpipe;
613 char **argv;
614 size_t argvsize, argvdata;
615 struct socket *insock, *outsock;
9ec790e8 616 struct wcspair *ta;
d3372da9 617 char *rec, *val;
618
619 wfilename = transfer->path;
620 if(transfer->fnet->filebasename != NULL)
621 wfilename = transfer->fnet->filebasename(wfilename);
622 if(transfer->auth == NULL)
623 {
624 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
625 errno = EACCES;
626 return(-1);
627 }
628 if((pwent = getpwuid(transfer->owner)) == NULL)
629 {
630 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
631 errno = EACCES;
632 return(-1);
633 }
645a4af7 634 filtername = findfile("dc-filter", pwent->pw_dir, 0);
81386042 635 if(filtername == NULL)
645a4af7 636 filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0);
81386042 637 if(filtername == NULL)
d3372da9 638 {
639 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
640 errno = ENOENT;
641 return(-1);
642 }
643 if((filename = icwcstombs(wfilename, NULL)) == NULL)
644 {
645 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
646 {
647 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
648 return(-1);
649 }
650 filename = sprintf2("utf8-%s", buf);
651 free(buf);
652 }
653 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
654 {
655 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
656 {
657 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
658 free(filename);
659 return(-1);
660 }
661 peerid = sprintf2("utf8-%s", buf);
662 free(buf);
663 }
664 if((pid = forksess(transfer->owner, transfer->auth, filterexit, NULL, FD_PIPE, 0, O_WRONLY, &inpipe, FD_PIPE, 1, O_RDONLY, &outpipe, FD_FILE, 2, O_RDWR, "/dev/null", FD_END)) < 0)
665 {
666 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
667 return(-1);
668 }
669 if(pid == 0)
670 {
671 argv = NULL;
672 argvsize = argvdata = 0;
529e3a4a 673 buf = sprintf2("%zi", transfer->size);
d3372da9 674 addtobuf(argv, filtername);
675 addtobuf(argv, filename);
676 addtobuf(argv, buf);
677 addtobuf(argv, peerid);
082b0419 678 if(transfer->hash)
679 {
680 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
681 {
682 /* XXX: I am very doubtful of this, but it can just as
683 * well be argued that all data should be presented as
684 * key-value pairs. */
685 addtobuf(argv, "hash");
686 addtobuf(argv, buf);
687 } else {
688 flog(LOG_WARNING, "could not convert hash to local charset");
689 }
690 }
d3372da9 691 for(ta = transfer->args; ta != NULL; ta = ta->next)
692 {
9ec790e8 693 if((rec = icwcstombs(ta->key, NULL)) == NULL)
d3372da9 694 continue;
695 if((val = icwcstombs(ta->val, NULL)) == NULL)
696 continue;
697 addtobuf(argv, rec);
698 addtobuf(argv, val);
699 }
700 addtobuf(argv, NULL);
701 execv(filtername, argv);
702 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
703 exit(127);
704 }
705 insock = wrapsock(inpipe);
706 outsock = wrapsock(outpipe);
707 /* Really, really strange thing here - sometimes the kernel would
708 * return POLLIN on insock, even though it's a write-side
709 * pipe. The corresponding read on the pipe naturally returns
710 * EBADF, causing doldacond to think there's something wrong with
711 * the fd, and thus it closes it. Until I can find out whyever the
712 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
713 * set ignread on insock for now. */
714 insock->ignread = 1;
715 transfer->filter = pid;
716 transfersetlocalend(transfer, insock);
717 getsock(transfer->filterout = outsock);
cab0b442 718 outsock->data = transfer;
719 outsock->readcb = (void (*)(struct socket *, void *))filterread;
d3372da9 720 putsock(insock);
721 putsock(outsock);
722 free(filtername);
723 free(filename);
724 free(peerid);
725 return(0);
726}
727
728static int run(void)
729{
730 struct transfer *transfer, *next;
731
732 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
733 {
734 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
735 {
736 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
737 transfer->iface->endofdata(transfer, transfer->ifacedata);
738 closesock(transfer->localend);
739 }
740 }
741 for(transfer = transfers; transfer != NULL; transfer = next)
742 {
743 next = transfer->next;
744 if(transfer->close)
745 {
746 transferdetach(transfer);
747 freetransfer(transfer);
748 continue;
749 }
750 }
751 return(0);
752}
753
754static struct configvar myvars[] =
755{
d9f89ef5 756 /** The maximum number of simultaneously permitted uploads. A
757 * common hub rule is that you will need at least as many slots as
758 * the number of hubs to which you are connected. */
d3372da9 759 {CONF_VAR_INT, "slots", {.num = 3}},
d9f89ef5 760 /** The TOS value to use for upload connections (see the TOS
761 * VALUES section). */
d3372da9 762 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
d9f89ef5 763 /** The TOS value to use for download connections (see the TOS
764 * VALUES section). */
d3372da9 765 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
d9f89ef5 766 /** The name of the filter script (see the FILES section for
767 * lookup information). */
d3372da9 768 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
d9f89ef5 769 /** If true, only one upload is allowed per remote peer. This
770 * option is still experimental, so it is recommended to leave it
771 * off. */
01663fd9 772 {CONF_VAR_BOOL, "ulquota", {.num = 0}},
d3372da9 773 {CONF_VAR_END}
774};
775
776static struct module me =
777{
778 .conf =
779 {
780 .vars = myvars
781 },
782 .name = "transfer",
783 .run = run
784};
785
786MODULE(me);