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