2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
35 #include "sysevents.h"
41 static void killfilter(struct transfer *transfer);
43 unsigned long long bytesupload = 0;
44 unsigned long long bytesdownload = 0;
45 struct transfer *transfers = NULL;
47 GCBCHAIN(newtransfercb, struct transfer *);
49 void freetransfer(struct transfer *transfer)
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)
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)
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)
85 transfer->localend->readcb = NULL;
86 transfer->localend->writecb = NULL;
87 transfer->localend->errcb = NULL;
88 putsock(transfer->localend);
90 if(transfer->filterout != NULL)
92 transfer->filterout->readcb = NULL;
93 transfer->filterout->writecb = NULL;
94 transfer->filterout->errcb = NULL;
95 putsock(transfer->filterout);
97 if(transfer->fn != NULL)
98 putfnetnode(transfer->fn);
103 struct transfer *newtransfer(void)
105 struct transfer *new;
106 static int curid = 0;
108 new = smalloc(sizeof(*new));
109 memset(new, 0, sizeof(*new));
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);
121 time(&new->activity);
126 void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
128 if(transfer->iface != NULL)
129 transferdetach(transfer);
130 transfer->iface = iface;
131 transfer->ifacedata = data;
134 void transferdetach(struct transfer *transfer)
136 if(transfer->iface != NULL)
138 transfer->iface->detach(transfer, transfer->ifacedata);
139 transfer->iface = NULL;
140 transfer->ifacedata = NULL;
144 struct transfer *finddownload(wchar_t *peerid)
146 struct transfer *transfer;
148 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
150 if((transfer->dir == TRNSD_DOWN) && (transfer->iface == NULL) && !wcscmp(peerid, transfer->peerid))
156 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
158 struct transfer *transfer;
160 transfer = newtransfer();
162 transfer->fnet = fnet;
164 transfer->fnet = fn->fnet;
165 transfer->peerid = swcsdup(nickid);
166 transfer->state = TRNS_HS;
167 transfer->dir = TRNSD_UP;
169 getfnetnode(transfer->fn = fn);
170 transferattach(transfer, iface, data);
171 linktransfer(transfer);
172 bumptransfer(transfer);
176 void linktransfer(struct transfer *transfer)
178 transfer->next = transfers;
179 transfer->prev = NULL;
180 if(transfers != NULL)
181 transfers->prev = transfer;
182 transfers = transfer;
183 GCBCHAINDOCB(newtransfercb, transfer);
186 void resettransfer(struct transfer *transfer)
188 if(transfer->dir == TRNSD_DOWN)
190 if(transfer->iface != NULL)
191 transferdetach(transfer);
192 killfilter(transfer);
193 transfersetstate(transfer, TRNS_WAITING);
194 transfersetactivity(transfer, L"reset");
199 struct transfer *findtransfer(int id)
201 struct transfer *transfer;
203 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
205 if(transfer->id == id)
211 static void transexpire(int cancelled, struct transfer *transfer)
213 transfer->etimer = NULL;
215 bumptransfer(transfer);
217 transfer->timeout = 0;
220 static void transferread(struct socket *sk, struct transfer *transfer)
222 if(sockgetdatalen(sk) >= 65536)
224 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
225 transfer->iface->gotdata(transfer, transfer->ifacedata);
228 static void transferwrite(struct socket *sk, struct transfer *transfer)
230 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
231 transfer->iface->wantdata(transfer, transfer->ifacedata);
234 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
236 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
237 transfer->iface->endofdata(transfer, transfer->ifacedata);
240 void transferputdata(struct transfer *transfer, void *buf, size_t size)
242 time(&transfer->activity);
243 sockqueue(transfer->localend, buf, size);
244 transfer->curpos += size;
245 bytesdownload += size;
246 CBCHAINDOCB(transfer, trans_p, transfer);
249 void transferendofdata(struct transfer *transfer)
251 if(transfer->curpos >= transfer->size)
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;
260 resettransfer(transfer);
264 size_t transferdatasize(struct transfer *transfer)
266 return(sockqueuesize(transfer->localend));
269 void *transfergetdata(struct transfer *transfer, size_t *size)
273 if(transfer->localend == NULL)
275 transfer->localend->ignread = 0;
276 time(&transfer->activity);
277 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
279 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
281 *size = transfer->endpos - transfer->curpos;
282 buf = srealloc(buf, *size);
284 transfer->curpos += *size;
285 bytesupload += *size;
286 CBCHAINDOCB(transfer, trans_p, transfer);
290 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
292 transfersetsize(transfer, size);
293 transfer->curpos = start;
294 transfer->endpos = end;
296 transfersetlocalend(transfer, lesk);
299 void transferstartdl(struct transfer *transfer, struct socket *sk)
301 transfersetstate(transfer, TRNS_MAIN);
302 socksettos(sk, confgetint("transfer", "dltos"));
305 void transferstartul(struct transfer *transfer, struct socket *sk)
307 transfersetstate(transfer, TRNS_MAIN);
308 socksettos(sk, confgetint("transfer", "ultos"));
309 if(transfer->localend != NULL)
310 transfer->localend->ignread = 0;
313 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
315 if(transfer->localend != NULL)
316 putsock(transfer->localend);
317 getsock(transfer->localend = sk);
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;
324 static int tryreq(struct transfer *transfer)
327 struct fnetpeer *peer;
329 if((fn = transfer->fn) != NULL)
331 if(fn->state != FNN_EST)
336 peer = fnetfindpeer(fn, transfer->peerid);
339 for(fn = fnetnodes; fn != NULL; fn = fn->next)
341 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
347 time(&transfer->lastreq);
348 return(fn->fnet->reqconn(peer));
353 void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
355 struct transfer *transfer;
357 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
359 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
361 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
363 if(!tryreq(transfer))
370 void bumptransfer(struct transfer *transfer)
374 if((now = time(NULL)) < transfer->timeout)
377 if(transfer->etimer == NULL)
378 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
381 if(transfer->etimer != NULL)
382 canceltimer(transfer->etimer);
383 switch(transfer->state)
386 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
387 if(now - transfer->lastreq > 30)
391 if(transfer->dir == TRNSD_UP)
393 if(now - transfer->activity < 60)
394 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
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);
401 resettransfer(transfer);
405 if(transfer->dir == TRNSD_UP)
407 if(now - transfer->activity < 300)
408 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
416 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
418 time(&transfer->activity);
421 if(transfer->actdesc != NULL)
422 free(transfer->actdesc);
423 transfer->actdesc = swcsdup(desc);
425 bumptransfer(transfer);
426 CBCHAINDOCB(transfer, trans_act, transfer);
429 void transfersetstate(struct transfer *transfer, int newstate)
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");
438 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
440 if(transfer->peernick != NULL)
441 free(transfer->peernick);
442 transfer->peernick = swcsdup(newnick);
443 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
446 void transfersetsize(struct transfer *transfer, int newsize)
448 transfer->size = newsize;
449 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
452 void transferseterror(struct transfer *transfer, int error)
454 transfer->error = error;
455 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
458 void transfersetpath(struct transfer *transfer, wchar_t *path)
460 if(transfer->path != NULL)
461 free(transfer->path);
462 transfer->path = swcsdup(path);
463 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
466 void transfersethash(struct transfer *transfer, struct hash *hash)
468 if(transfer->hash != NULL)
469 freehash(transfer->hash);
470 transfer->hash = hash;
471 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
476 struct transfer *transfer;
479 slots = confgetint("transfer", "slots");
480 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
482 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
488 static void killfilter(struct transfer *transfer)
490 if(transfer->filter != -1)
492 kill(-transfer->filter, SIGHUP);
493 transfer->filter = -1;
495 if(transfer->localend)
497 transfer->localend->readcb = NULL;
498 transfer->localend->writecb = NULL;
499 transfer->localend->errcb = NULL;
500 putsock(transfer->localend);
501 transfer->localend = NULL;
503 if(transfer->filterout)
505 transfer->filterout->readcb = NULL;
506 putsock(transfer->filterout);
507 transfer->filterout = NULL;
509 if(transfer->filterbuf)
511 free(transfer->filterbuf);
512 transfer->filterbuf = NULL;
514 transfer->filterbufsize = transfer->filterbufdata = 0;
517 static char *findfilter(struct passwd *pwd)
519 char *path, *filtername;
521 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
523 if(!access(path, X_OK))
527 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
529 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
531 if(strchr(filtername, '/') == NULL)
533 if((path = sprintf2("/etc/%s", filtername)) != NULL)
535 if(!access(path, X_OK))
542 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
544 if(!access(path, X_OK))
551 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
553 if(!access(path, X_OK))
561 if(!access(filtername, X_OK))
569 static void filterread(struct socket *sk, struct transfer *transfer)
575 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
577 bufcat(transfer->filterbuf, buf, bufsize);
579 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
582 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
584 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
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));
592 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
597 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
599 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
603 static void filterexit(pid_t pid, int status, void *data)
605 struct transfer *transfer;
607 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
609 if(transfer->filter == pid)
611 transfer->filter = -1;
612 killfilter(transfer);
613 if(WEXITSTATUS(status))
615 resettransfer(transfer);
617 freetransfer(transfer);
619 trytransferbypeer(transfer->fnet, transfer->peerid);
625 int forkfilter(struct transfer *transfer)
627 char *filtername, *filename, *peerid, *buf;
629 struct passwd *pwent;
633 size_t argvsize, argvdata;
634 struct socket *insock, *outsock;
638 wfilename = transfer->path;
639 if(transfer->fnet->filebasename != NULL)
640 wfilename = transfer->fnet->filebasename(wfilename);
641 if(transfer->auth == NULL)
643 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
647 if((pwent = getpwuid(transfer->owner)) == NULL)
649 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
653 if((filtername = findfilter(pwent)) == NULL)
655 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
659 if((filename = icwcstombs(wfilename, NULL)) == NULL)
661 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
663 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
666 filename = sprintf2("utf8-%s", buf);
669 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
671 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
673 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
677 peerid = sprintf2("utf8-%s", buf);
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)
682 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
688 argvsize = argvdata = 0;
689 buf = sprintf2("%i", transfer->size);
690 addtobuf(argv, filtername);
691 addtobuf(argv, filename);
693 addtobuf(argv, peerid);
696 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
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");
704 flog(LOG_WARNING, "could not convert hash to local charset");
707 for(ta = transfer->args; ta != NULL; ta = ta->next)
709 if((rec = icwcstombs(ta->key, NULL)) == NULL)
711 if((val = icwcstombs(ta->val, NULL)) == NULL)
716 addtobuf(argv, NULL);
717 execv(filtername, argv);
718 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
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. */
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;
746 struct transfer *transfer, *next;
748 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
750 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
752 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
753 transfer->iface->endofdata(transfer, transfer->ifacedata);
754 closesock(transfer->localend);
757 for(transfer = transfers; transfer != NULL; transfer = next)
759 next = transfer->next;
762 transferdetach(transfer);
763 freetransfer(transfer);
770 static struct configvar myvars[] =
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"}},
779 static struct module me =