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