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