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->exitstatus != NULL)
84 free(transfer->exitstatus);
85 if(transfer->localend != NULL)
87 transfer->localend->readcb = NULL;
88 transfer->localend->writecb = NULL;
89 transfer->localend->errcb = NULL;
90 putsock(transfer->localend);
92 if(transfer->filterout != NULL)
94 transfer->filterout->readcb = NULL;
95 transfer->filterout->writecb = NULL;
96 transfer->filterout->errcb = NULL;
97 putsock(transfer->filterout);
99 if(transfer->fn != NULL)
100 putfnetnode(transfer->fn);
105 struct transfer *newtransfer(void)
107 struct transfer *new;
108 static int curid = 0;
110 new = smalloc(sizeof(*new));
111 memset(new, 0, sizeof(*new));
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);
123 time(&new->activity);
128 void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
130 if(transfer->iface != NULL)
131 transferdetach(transfer);
132 transfer->iface = iface;
133 transfer->ifacedata = data;
136 void transferdetach(struct transfer *transfer)
138 if(transfer->iface != NULL)
140 transfer->iface->detach(transfer, transfer->ifacedata);
141 transfer->iface = NULL;
142 transfer->ifacedata = NULL;
146 struct transfer *finddownload(wchar_t *peerid)
148 struct transfer *transfer;
150 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
152 if((transfer->dir == TRNSD_DOWN) && (transfer->iface == NULL) && !wcscmp(peerid, transfer->peerid))
158 struct transfer *hasupload(struct fnet *fnet, wchar_t *peerid)
160 struct transfer *transfer;
162 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
164 if((transfer->dir == TRNSD_UP) && (transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
170 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
172 struct transfer *transfer;
174 transfer = newtransfer();
176 transfer->fnet = fnet;
178 transfer->fnet = fn->fnet;
179 transfer->peerid = swcsdup(nickid);
180 transfer->state = TRNS_HS;
181 transfer->dir = TRNSD_UP;
183 getfnetnode(transfer->fn = fn);
184 transferattach(transfer, iface, data);
185 linktransfer(transfer);
186 bumptransfer(transfer);
190 void linktransfer(struct transfer *transfer)
192 transfer->next = transfers;
193 transfer->prev = NULL;
194 if(transfers != NULL)
195 transfers->prev = transfer;
196 transfers = transfer;
197 GCBCHAINDOCB(newtransfercb, transfer);
200 void resettransfer(struct transfer *transfer)
202 if(transfer->dir == TRNSD_DOWN)
204 if(transfer->iface != NULL)
205 transferdetach(transfer);
206 killfilter(transfer);
207 transfersetstate(transfer, TRNS_WAITING);
208 transfersetactivity(transfer, L"reset");
213 struct transfer *findtransfer(int id)
215 struct transfer *transfer;
217 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
219 if(transfer->id == id)
225 static void transexpire(int cancelled, struct transfer *transfer)
227 transfer->etimer = NULL;
229 bumptransfer(transfer);
231 transfer->timeout = 0;
234 static void transferread(struct socket *sk, struct transfer *transfer)
236 if(sockgetdatalen(sk) >= 65536)
238 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
239 transfer->iface->gotdata(transfer, transfer->ifacedata);
242 static void transferwrite(struct socket *sk, struct transfer *transfer)
244 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
245 transfer->iface->wantdata(transfer, transfer->ifacedata);
248 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
250 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
251 transfer->iface->endofdata(transfer, transfer->ifacedata);
254 void transferputdata(struct transfer *transfer, void *buf, size_t size)
256 time(&transfer->activity);
257 sockqueue(transfer->localend, buf, size);
258 transfer->curpos += size;
259 bytesdownload += size;
260 CBCHAINDOCB(transfer, trans_p, transfer);
263 void transferendofdata(struct transfer *transfer)
265 if(transfer->curpos >= transfer->size)
267 transfersetstate(transfer, TRNS_DONE);
268 transfer->localend->readcb = NULL;
269 transfer->localend->writecb = NULL;
270 transfer->localend->errcb = NULL;
271 putsock(transfer->localend);
272 transfer->localend = NULL;
274 resettransfer(transfer);
278 size_t transferdatasize(struct transfer *transfer)
280 return(sockqueuesize(transfer->localend));
283 void *transfergetdata(struct transfer *transfer, size_t *size)
287 if(transfer->localend == NULL)
289 transfer->localend->ignread = 0;
290 time(&transfer->activity);
291 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
293 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
295 if((*size = transfer->endpos - transfer->curpos) == 0) {
299 buf = srealloc(buf, *size);
302 transfer->curpos += *size;
303 bytesupload += *size;
304 CBCHAINDOCB(transfer, trans_p, transfer);
308 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
310 transfersetsize(transfer, size);
311 transfer->curpos = start;
312 transfer->endpos = end;
314 transfersetlocalend(transfer, lesk);
317 void transferstartdl(struct transfer *transfer, struct socket *sk)
319 transfersetstate(transfer, TRNS_MAIN);
320 socksettos(sk, confgetint("transfer", "dltos"));
323 void transferstartul(struct transfer *transfer, struct socket *sk)
325 transfersetstate(transfer, TRNS_MAIN);
326 socksettos(sk, confgetint("transfer", "ultos"));
327 if(transfer->localend != NULL)
328 transfer->localend->ignread = 0;
331 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
333 if(transfer->localend != NULL)
334 putsock(transfer->localend);
335 getsock(transfer->localend = sk);
337 sk->readcb = (void (*)(struct socket *, void *))transferread;
338 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
339 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
342 static int tryreq(struct transfer *transfer)
345 struct fnetpeer *peer;
347 if((fn = transfer->fn) != NULL)
349 if(fn->state != FNN_EST)
354 peer = fnetfindpeer(fn, transfer->peerid);
357 for(fn = fnetnodes; fn != NULL; fn = fn->next)
359 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
365 time(&transfer->lastreq);
366 return(fn->fnet->reqconn(peer));
371 void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
373 struct transfer *transfer;
375 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
377 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
379 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
381 if(!tryreq(transfer))
388 void bumptransfer(struct transfer *transfer)
392 if((now = time(NULL)) < transfer->timeout)
395 if(transfer->etimer == NULL)
396 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
399 if(transfer->etimer != NULL)
400 canceltimer(transfer->etimer);
401 switch(transfer->state)
404 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
405 if(now - transfer->lastreq > 30)
409 if(transfer->dir == TRNSD_UP)
411 if(now - transfer->activity < 60)
412 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
415 } else if(transfer->dir == TRNSD_DOWN) {
416 if(now - transfer->activity < 60)
417 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
419 resettransfer(transfer);
423 if(transfer->dir == TRNSD_UP)
425 if(now - transfer->activity < 300)
426 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
434 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
436 time(&transfer->activity);
439 if(transfer->actdesc != NULL)
440 free(transfer->actdesc);
441 transfer->actdesc = swcsdup(desc);
443 bumptransfer(transfer);
444 CBCHAINDOCB(transfer, trans_act, transfer);
447 void transfersetstate(struct transfer *transfer, int newstate)
449 transfer->state = newstate;
450 if(transfer->etimer != NULL)
451 canceltimer(transfer->etimer);
452 transfersetactivity(transfer, NULL);
453 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
456 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
458 if(transfer->peernick != NULL)
459 free(transfer->peernick);
460 transfer->peernick = swcsdup(newnick);
461 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
464 void transfersetsize(struct transfer *transfer, int newsize)
466 transfer->size = newsize;
467 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
470 void transferseterror(struct transfer *transfer, int error)
472 transfer->error = error;
473 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
476 void transfersetpath(struct transfer *transfer, wchar_t *path)
478 if(transfer->path != NULL)
479 free(transfer->path);
480 transfer->path = swcsdup(path);
481 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
484 void transfersethash(struct transfer *transfer, struct hash *hash)
486 if(transfer->hash != NULL)
487 freehash(transfer->hash);
488 transfer->hash = hash;
489 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
494 struct transfer *transfer;
497 slots = confgetint("transfer", "slots");
498 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
500 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
506 static void killfilter(struct transfer *transfer)
508 if(transfer->filter != -1)
510 kill(-transfer->filter, SIGHUP);
511 transfer->filter = -1;
513 if(transfer->localend)
515 transfer->localend->readcb = NULL;
516 transfer->localend->writecb = NULL;
517 transfer->localend->errcb = NULL;
518 putsock(transfer->localend);
519 transfer->localend = NULL;
521 if(transfer->filterout)
523 transfer->filterout->readcb = NULL;
524 putsock(transfer->filterout);
525 transfer->filterout = NULL;
527 if(transfer->filterbuf)
529 free(transfer->filterbuf);
530 transfer->filterbuf = NULL;
532 transfer->filterbufsize = transfer->filterbufdata = 0;
535 static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
537 if(!wcscmp(cmd, L"status")) {
540 if(transfer->exitstatus != NULL)
541 free(transfer->exitstatus);
542 transfer->exitstatus = swcsdup(arg);
546 static void filterread(struct socket *sk, struct transfer *transfer)
552 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
554 bufcat(transfer->filterbuf, buf, bufsize);
556 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
559 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
561 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
566 if((arg = icmbstowcs(p2, NULL)) == NULL)
567 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
569 handletranscmd(transfer, cmd, arg);
570 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
575 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
577 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
581 static void filterexit(pid_t pid, int status, void *data)
583 struct transfer *transfer;
587 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
589 if(transfer->filter == pid)
591 transfer->filter = -1;
592 killfilter(transfer);
593 fnet = transfer->fnet;
594 peerid = swcsdup(transfer->peerid);
595 if(WEXITSTATUS(status))
596 resettransfer(transfer);
598 freetransfer(transfer);
599 trytransferbypeer(fnet, peerid);
606 int forkfilter(struct transfer *transfer)
608 char *filtername, *filename, *peerid, *buf;
610 struct passwd *pwent;
614 size_t argvsize, argvdata;
615 struct socket *insock, *outsock;
619 wfilename = transfer->path;
620 if(transfer->fnet->filebasename != NULL)
621 wfilename = transfer->fnet->filebasename(wfilename);
622 if(transfer->auth == NULL)
624 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
628 if((pwent = getpwuid(transfer->owner)) == NULL)
630 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
634 filtername = findfile("dc-filter", pwent->pw_dir, 0);
635 if(filtername == NULL)
636 filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0);
637 if(filtername == NULL)
639 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
643 if((filename = icwcstombs(wfilename, NULL)) == NULL)
645 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
647 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
650 filename = sprintf2("utf8-%s", buf);
653 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
655 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
657 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
661 peerid = sprintf2("utf8-%s", buf);
664 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)
666 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
672 argvsize = argvdata = 0;
673 buf = sprintf2("%zi", transfer->size);
674 addtobuf(argv, filtername);
675 addtobuf(argv, filename);
677 addtobuf(argv, peerid);
680 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
682 /* XXX: I am very doubtful of this, but it can just as
683 * well be argued that all data should be presented as
684 * key-value pairs. */
685 addtobuf(argv, "hash");
688 flog(LOG_WARNING, "could not convert hash to local charset");
691 for(ta = transfer->args; ta != NULL; ta = ta->next)
693 if((rec = icwcstombs(ta->key, NULL)) == NULL)
695 if((val = icwcstombs(ta->val, NULL)) == NULL)
700 addtobuf(argv, NULL);
701 execv(filtername, argv);
702 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
705 insock = wrapsock(inpipe);
706 outsock = wrapsock(outpipe);
707 /* Really, really strange thing here - sometimes the kernel would
708 * return POLLIN on insock, even though it's a write-side
709 * pipe. The corresponding read on the pipe naturally returns
710 * EBADF, causing doldacond to think there's something wrong with
711 * the fd, and thus it closes it. Until I can find out whyever the
712 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
713 * set ignread on insock for now. */
715 transfer->filter = pid;
716 transfersetlocalend(transfer, insock);
717 getsock(transfer->filterout = outsock);
718 outsock->data = transfer;
719 outsock->readcb = (void (*)(struct socket *, void *))filterread;
730 struct transfer *transfer, *next;
732 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
734 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
736 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
737 transfer->iface->endofdata(transfer, transfer->ifacedata);
738 closesock(transfer->localend);
741 for(transfer = transfers; transfer != NULL; transfer = next)
743 next = transfer->next;
746 transferdetach(transfer);
747 freetransfer(transfer);
754 static struct configvar myvars[] =
756 /** The maximum number of simultaneously permitted uploads. A
757 * common hub rule is that you will need at least as many slots as
758 * the number of hubs to which you are connected. */
759 {CONF_VAR_INT, "slots", {.num = 3}},
760 /** The TOS value to use for upload connections (see the TOS
761 * VALUES section). */
762 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
763 /** The TOS value to use for download connections (see the TOS
764 * VALUES section). */
765 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
766 /** The name of the filter script (see the FILES section for
767 * lookup information). */
768 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
769 /** If true, only one upload is allowed per remote peer. This
770 * option is still experimental, so it is recommended to leave it
772 {CONF_VAR_BOOL, "ulquota", {.num = 0}},
776 static struct module me =