New dc_connect implementation
[doldaconnect.git] / daemon / transfer.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#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 {
295 *size = transfer->endpos - transfer->curpos;
296 buf = srealloc(buf, *size);
297 }
298 transfer->curpos += *size;
7a5e9d41 299 bytesupload += *size;
d3372da9 300 CBCHAINDOCB(transfer, trans_p, transfer);
301 return(buf);
302}
303
304void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
305{
306 transfersetsize(transfer, size);
307 transfer->curpos = start;
308 transfer->endpos = end;
309 lesk->ignread = 1;
310 transfersetlocalend(transfer, lesk);
311}
312
2c086721 313void transferstartdl(struct transfer *transfer, struct socket *sk)
314{
315 transfersetstate(transfer, TRNS_MAIN);
316 socksettos(sk, confgetint("transfer", "dltos"));
317}
318
d3372da9 319void transferstartul(struct transfer *transfer, struct socket *sk)
320{
321 transfersetstate(transfer, TRNS_MAIN);
322 socksettos(sk, confgetint("transfer", "ultos"));
323 if(transfer->localend != NULL)
324 transfer->localend->ignread = 0;
325}
326
327void transfersetlocalend(struct transfer *transfer, struct socket *sk)
328{
329 if(transfer->localend != NULL)
330 putsock(transfer->localend);
331 getsock(transfer->localend = sk);
cab0b442 332 sk->data = transfer;
333 sk->readcb = (void (*)(struct socket *, void *))transferread;
334 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
335 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
d3372da9 336}
337
18f56d13 338static int tryreq(struct transfer *transfer)
d3372da9 339{
340 struct fnetnode *fn;
341 struct fnetpeer *peer;
18f56d13 342
343 if((fn = transfer->fn) != NULL)
344 {
345 if(fn->state != FNN_EST)
346 {
347 transfer->close = 1;
348 return(1);
349 }
350 peer = fnetfindpeer(fn, transfer->peerid);
351 } else {
352 peer = NULL;
353 for(fn = fnetnodes; fn != NULL; fn = fn->next)
354 {
355 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
356 break;
357 }
358 }
359 if(peer != NULL)
6329b4ba 360 {
361 time(&transfer->lastreq);
18f56d13 362 return(fn->fnet->reqconn(peer));
6329b4ba 363 }
18f56d13 364 return(1);
365}
366
367void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
368{
369 struct transfer *transfer;
370
371 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
372 {
373 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
374 {
375 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
376 {
377 if(!tryreq(transfer))
378 return;
379 }
380 }
381 }
382}
383
384void bumptransfer(struct transfer *transfer)
385{
d3372da9 386 time_t now;
387
388 if((now = time(NULL)) < transfer->timeout)
389 {
18f56d13 390
d3372da9 391 if(transfer->etimer == NULL)
392 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
393 return;
394 }
395 if(transfer->etimer != NULL)
396 canceltimer(transfer->etimer);
397 switch(transfer->state)
398 {
399 case TRNS_WAITING:
d3372da9 400 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
401 if(now - transfer->lastreq > 30)
18f56d13 402 tryreq(transfer);
d3372da9 403 break;
404 case TRNS_HS:
405 if(transfer->dir == TRNSD_UP)
406 {
407 if(now - transfer->activity < 60)
408 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
409 else
410 transfer->close = 1;
411 } else if(transfer->dir == TRNSD_DOWN) {
412 if(now - transfer->activity < 60)
413 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
414 else
415 resettransfer(transfer);
416 }
417 break;
418 case TRNS_MAIN:
419 if(transfer->dir == TRNSD_UP)
420 {
421 if(now - transfer->activity < 300)
422 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
423 else
424 transfer->close = 1;
425 }
426 break;
427 }
428}
429
430void transfersetactivity(struct transfer *transfer, wchar_t *desc)
431{
432 time(&transfer->activity);
433 if(desc != NULL)
434 {
435 if(transfer->actdesc != NULL)
436 free(transfer->actdesc);
437 transfer->actdesc = swcsdup(desc);
438 }
439 bumptransfer(transfer);
440 CBCHAINDOCB(transfer, trans_act, transfer);
441}
442
443void transfersetstate(struct transfer *transfer, int newstate)
444{
445 transfer->state = newstate;
446 if(transfer->etimer != NULL)
447 canceltimer(transfer->etimer);
448 transfersetactivity(transfer, NULL);
449 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
450}
451
452void transfersetnick(struct transfer *transfer, wchar_t *newnick)
453{
454 if(transfer->peernick != NULL)
455 free(transfer->peernick);
456 transfer->peernick = swcsdup(newnick);
457 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
458}
459
460void transfersetsize(struct transfer *transfer, int newsize)
461{
462 transfer->size = newsize;
463 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
464}
465
466void transferseterror(struct transfer *transfer, int error)
467{
468 transfer->error = error;
469 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
470}
471
472void transfersetpath(struct transfer *transfer, wchar_t *path)
473{
474 if(transfer->path != NULL)
475 free(transfer->path);
476 transfer->path = swcsdup(path);
477 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
478}
479
2c086721 480void transfersethash(struct transfer *transfer, struct hash *hash)
481{
482 if(transfer->hash != NULL)
483 freehash(transfer->hash);
484 transfer->hash = hash;
485 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
486}
487
d3372da9 488int slotsleft(void)
489{
490 struct transfer *transfer;
491 int slots;
492
493 slots = confgetint("transfer", "slots");
494 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
495 {
496 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
497 slots--;
498 }
499 return(slots);
500}
501
502static void killfilter(struct transfer *transfer)
503{
504 if(transfer->filter != -1)
505 {
506 kill(-transfer->filter, SIGHUP);
507 transfer->filter = -1;
508 }
509 if(transfer->localend)
510 {
cab0b442 511 transfer->localend->readcb = NULL;
512 transfer->localend->writecb = NULL;
513 transfer->localend->errcb = NULL;
d3372da9 514 putsock(transfer->localend);
515 transfer->localend = NULL;
516 }
517 if(transfer->filterout)
518 {
cab0b442 519 transfer->filterout->readcb = NULL;
d3372da9 520 putsock(transfer->filterout);
521 transfer->filterout = NULL;
522 }
523 if(transfer->filterbuf)
524 {
525 free(transfer->filterbuf);
526 transfer->filterbuf = NULL;
527 }
528 transfer->filterbufsize = transfer->filterbufdata = 0;
529}
530
0309b83d 531static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
532{
533 if(!wcscmp(cmd, L"status")) {
534 if(arg == NULL)
535 arg = L"";
536 if(transfer->exitstatus != NULL)
537 free(transfer->exitstatus);
538 transfer->exitstatus = swcsdup(arg);
539 }
540}
541
cab0b442 542static void filterread(struct socket *sk, struct transfer *transfer)
d3372da9 543{
544 char *buf, *p, *p2;
545 size_t bufsize;
546 wchar_t *cmd, *arg;
547
548 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
cab0b442 549 return;
d3372da9 550 bufcat(transfer->filterbuf, buf, bufsize);
551 free(buf);
5b193db0 552 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
d3372da9 553 {
554 *(p++) = 0;
555 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
556 *(p2++) = 0;
557 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
558 {
559 arg = NULL;
560 if(p2 != NULL)
561 {
562 if((arg = icmbstowcs(p2, NULL)) == NULL)
13b303d4 563 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
d3372da9 564 }
0309b83d 565 handletranscmd(transfer, cmd, arg);
d3372da9 566 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
567 if(arg != NULL)
568 free(arg);
569 free(cmd);
570 } else {
571 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
572 }
573 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
574 }
575}
576
577static void filterexit(pid_t pid, int status, void *data)
578{
579 struct transfer *transfer;
734dc54e 580 struct fnet *fnet;
581 wchar_t *peerid;
d3372da9 582
583 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
584 {
585 if(transfer->filter == pid)
586 {
587 transfer->filter = -1;
588 killfilter(transfer);
734dc54e 589 fnet = transfer->fnet;
590 peerid = swcsdup(transfer->peerid);
d3372da9 591 if(WEXITSTATUS(status))
d3372da9 592 resettransfer(transfer);
734dc54e 593 else
d3372da9 594 freetransfer(transfer);
734dc54e 595 trytransferbypeer(fnet, peerid);
596 free(peerid);
d3372da9 597 break;
598 }
599 }
600}
601
602int forkfilter(struct transfer *transfer)
603{
604 char *filtername, *filename, *peerid, *buf;
605 wchar_t *wfilename;
606 struct passwd *pwent;
607 pid_t pid;
608 int inpipe, outpipe;
609 char **argv;
610 size_t argvsize, argvdata;
611 struct socket *insock, *outsock;
9ec790e8 612 struct wcspair *ta;
d3372da9 613 char *rec, *val;
614
615 wfilename = transfer->path;
616 if(transfer->fnet->filebasename != NULL)
617 wfilename = transfer->fnet->filebasename(wfilename);
618 if(transfer->auth == NULL)
619 {
620 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
621 errno = EACCES;
622 return(-1);
623 }
624 if((pwent = getpwuid(transfer->owner)) == NULL)
625 {
626 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
627 errno = EACCES;
628 return(-1);
629 }
81386042 630 filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0);
631 if(filtername == NULL)
632 filtername = findfile("dc-filter", pwent->pw_dir, 0);
633 if(filtername == NULL)
d3372da9 634 {
635 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
636 errno = ENOENT;
637 return(-1);
638 }
639 if((filename = icwcstombs(wfilename, NULL)) == NULL)
640 {
641 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
642 {
643 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
644 return(-1);
645 }
646 filename = sprintf2("utf8-%s", buf);
647 free(buf);
648 }
649 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
650 {
651 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
652 {
653 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
654 free(filename);
655 return(-1);
656 }
657 peerid = sprintf2("utf8-%s", buf);
658 free(buf);
659 }
660 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)
661 {
662 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
663 return(-1);
664 }
665 if(pid == 0)
666 {
667 argv = NULL;
668 argvsize = argvdata = 0;
669 buf = sprintf2("%i", transfer->size);
670 addtobuf(argv, filtername);
671 addtobuf(argv, filename);
672 addtobuf(argv, buf);
673 addtobuf(argv, peerid);
082b0419 674 if(transfer->hash)
675 {
676 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
677 {
678 /* XXX: I am very doubtful of this, but it can just as
679 * well be argued that all data should be presented as
680 * key-value pairs. */
681 addtobuf(argv, "hash");
682 addtobuf(argv, buf);
683 } else {
684 flog(LOG_WARNING, "could not convert hash to local charset");
685 }
686 }
d3372da9 687 for(ta = transfer->args; ta != NULL; ta = ta->next)
688 {
9ec790e8 689 if((rec = icwcstombs(ta->key, NULL)) == NULL)
d3372da9 690 continue;
691 if((val = icwcstombs(ta->val, NULL)) == NULL)
692 continue;
693 addtobuf(argv, rec);
694 addtobuf(argv, val);
695 }
696 addtobuf(argv, NULL);
697 execv(filtername, argv);
698 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
699 exit(127);
700 }
701 insock = wrapsock(inpipe);
702 outsock = wrapsock(outpipe);
703 /* Really, really strange thing here - sometimes the kernel would
704 * return POLLIN on insock, even though it's a write-side
705 * pipe. The corresponding read on the pipe naturally returns
706 * EBADF, causing doldacond to think there's something wrong with
707 * the fd, and thus it closes it. Until I can find out whyever the
708 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
709 * set ignread on insock for now. */
710 insock->ignread = 1;
711 transfer->filter = pid;
712 transfersetlocalend(transfer, insock);
713 getsock(transfer->filterout = outsock);
cab0b442 714 outsock->data = transfer;
715 outsock->readcb = (void (*)(struct socket *, void *))filterread;
d3372da9 716 putsock(insock);
717 putsock(outsock);
718 free(filtername);
719 free(filename);
720 free(peerid);
721 return(0);
722}
723
724static int run(void)
725{
726 struct transfer *transfer, *next;
727
728 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
729 {
730 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
731 {
732 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
733 transfer->iface->endofdata(transfer, transfer->ifacedata);
734 closesock(transfer->localend);
735 }
736 }
737 for(transfer = transfers; transfer != NULL; transfer = next)
738 {
739 next = transfer->next;
740 if(transfer->close)
741 {
742 transferdetach(transfer);
743 freetransfer(transfer);
744 continue;
745 }
746 }
747 return(0);
748}
749
750static struct configvar myvars[] =
751{
d9f89ef5 752 /** The maximum number of simultaneously permitted uploads. A
753 * common hub rule is that you will need at least as many slots as
754 * the number of hubs to which you are connected. */
d3372da9 755 {CONF_VAR_INT, "slots", {.num = 3}},
d9f89ef5 756 /** The TOS value to use for upload connections (see the TOS
757 * VALUES section). */
d3372da9 758 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
d9f89ef5 759 /** The TOS value to use for download connections (see the TOS
760 * VALUES section). */
d3372da9 761 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
d9f89ef5 762 /** The name of the filter script (see the FILES section for
763 * lookup information). */
d3372da9 764 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
d9f89ef5 765 /** If true, only one upload is allowed per remote peer. This
766 * option is still experimental, so it is recommended to leave it
767 * off. */
01663fd9 768 {CONF_VAR_BOOL, "ulquota", {.num = 0}},
d3372da9 769 {CONF_VAR_END}
770};
771
772static struct module me =
773{
774 .conf =
775 {
776 .vars = myvars
777 },
778 .name = "transfer",
779 .run = run
780};
781
782MODULE(me);