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 *size = transfer->endpos - transfer->curpos;
296 buf = srealloc(buf, *size);
298 transfer->curpos += *size;
299 bytesupload += *size;
300 CBCHAINDOCB(transfer, trans_p, transfer);
304 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
306 transfersetsize(transfer, size);
307 transfer->curpos = start;
308 transfer->endpos = end;
310 transfersetlocalend(transfer, lesk);
313 void transferstartdl(struct transfer *transfer, struct socket *sk)
315 transfersetstate(transfer, TRNS_MAIN);
316 socksettos(sk, confgetint("transfer", "dltos"));
319 void transferstartul(struct transfer *transfer, struct socket *sk)
321 transfersetstate(transfer, TRNS_MAIN);
322 socksettos(sk, confgetint("transfer", "ultos"));
323 if(transfer->localend != NULL)
324 transfer->localend->ignread = 0;
327 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
329 if(transfer->localend != NULL)
330 putsock(transfer->localend);
331 getsock(transfer->localend = sk);
333 sk->readcb = (void (*)(struct socket *, void *))transferread;
334 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
335 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
338 static int tryreq(struct transfer *transfer)
341 struct fnetpeer *peer;
343 if((fn = transfer->fn) != NULL)
345 if(fn->state != FNN_EST)
350 peer = fnetfindpeer(fn, transfer->peerid);
353 for(fn = fnetnodes; fn != NULL; fn = fn->next)
355 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
361 time(&transfer->lastreq);
362 return(fn->fnet->reqconn(peer));
367 void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
369 struct transfer *transfer;
371 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
373 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
375 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
377 if(!tryreq(transfer))
384 void bumptransfer(struct transfer *transfer)
388 if((now = time(NULL)) < transfer->timeout)
391 if(transfer->etimer == NULL)
392 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
395 if(transfer->etimer != NULL)
396 canceltimer(transfer->etimer);
397 switch(transfer->state)
400 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
401 if(now - transfer->lastreq > 30)
405 if(transfer->dir == TRNSD_UP)
407 if(now - transfer->activity < 60)
408 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
411 } else if(transfer->dir == TRNSD_DOWN) {
412 if(now - transfer->activity < 60)
413 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
415 resettransfer(transfer);
419 if(transfer->dir == TRNSD_UP)
421 if(now - transfer->activity < 300)
422 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
430 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
432 time(&transfer->activity);
435 if(transfer->actdesc != NULL)
436 free(transfer->actdesc);
437 transfer->actdesc = swcsdup(desc);
439 bumptransfer(transfer);
440 CBCHAINDOCB(transfer, trans_act, transfer);
443 void transfersetstate(struct transfer *transfer, int newstate)
445 transfer->state = newstate;
446 if(transfer->etimer != NULL)
447 canceltimer(transfer->etimer);
448 transfersetactivity(transfer, NULL);
449 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
452 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
454 if(transfer->peernick != NULL)
455 free(transfer->peernick);
456 transfer->peernick = swcsdup(newnick);
457 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
460 void transfersetsize(struct transfer *transfer, int newsize)
462 transfer->size = newsize;
463 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
466 void transferseterror(struct transfer *transfer, int error)
468 transfer->error = error;
469 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
472 void transfersetpath(struct transfer *transfer, wchar_t *path)
474 if(transfer->path != NULL)
475 free(transfer->path);
476 transfer->path = swcsdup(path);
477 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
480 void transfersethash(struct transfer *transfer, struct hash *hash)
482 if(transfer->hash != NULL)
483 freehash(transfer->hash);
484 transfer->hash = hash;
485 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
490 struct transfer *transfer;
493 slots = confgetint("transfer", "slots");
494 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
496 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
502 static void killfilter(struct transfer *transfer)
504 if(transfer->filter != -1)
506 kill(-transfer->filter, SIGHUP);
507 transfer->filter = -1;
509 if(transfer->localend)
511 transfer->localend->readcb = NULL;
512 transfer->localend->writecb = NULL;
513 transfer->localend->errcb = NULL;
514 putsock(transfer->localend);
515 transfer->localend = NULL;
517 if(transfer->filterout)
519 transfer->filterout->readcb = NULL;
520 putsock(transfer->filterout);
521 transfer->filterout = NULL;
523 if(transfer->filterbuf)
525 free(transfer->filterbuf);
526 transfer->filterbuf = NULL;
528 transfer->filterbufsize = transfer->filterbufdata = 0;
531 static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
533 if(!wcscmp(cmd, L"status")) {
536 if(transfer->exitstatus != NULL)
537 free(transfer->exitstatus);
538 transfer->exitstatus = swcsdup(arg);
542 static void filterread(struct socket *sk, struct transfer *transfer)
548 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
550 bufcat(transfer->filterbuf, buf, bufsize);
552 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
555 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
557 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
562 if((arg = icmbstowcs(p2, NULL)) == NULL)
563 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
565 handletranscmd(transfer, cmd, arg);
566 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
571 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
573 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
577 static void filterexit(pid_t pid, int status, void *data)
579 struct transfer *transfer;
583 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
585 if(transfer->filter == pid)
587 transfer->filter = -1;
588 killfilter(transfer);
589 fnet = transfer->fnet;
590 peerid = swcsdup(transfer->peerid);
591 if(WEXITSTATUS(status))
592 resettransfer(transfer);
594 freetransfer(transfer);
595 trytransferbypeer(fnet, peerid);
602 int forkfilter(struct transfer *transfer)
604 char *filtername, *filename, *peerid, *buf;
606 struct passwd *pwent;
610 size_t argvsize, argvdata;
611 struct socket *insock, *outsock;
615 wfilename = transfer->path;
616 if(transfer->fnet->filebasename != NULL)
617 wfilename = transfer->fnet->filebasename(wfilename);
618 if(transfer->auth == NULL)
620 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
624 if((pwent = getpwuid(transfer->owner)) == NULL)
626 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
630 filtername = findfile("dc-filter", pwent->pw_dir, 0);
631 if(filtername == NULL)
632 filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0);
633 if(filtername == NULL)
635 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
639 if((filename = icwcstombs(wfilename, NULL)) == NULL)
641 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
643 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
646 filename = sprintf2("utf8-%s", buf);
649 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
651 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
653 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
657 peerid = sprintf2("utf8-%s", buf);
660 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)
662 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
668 argvsize = argvdata = 0;
669 buf = sprintf2("%zi", transfer->size);
670 addtobuf(argv, filtername);
671 addtobuf(argv, filename);
673 addtobuf(argv, peerid);
676 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
678 /* XXX: I am very doubtful of this, but it can just as
679 * well be argued that all data should be presented as
680 * key-value pairs. */
681 addtobuf(argv, "hash");
684 flog(LOG_WARNING, "could not convert hash to local charset");
687 for(ta = transfer->args; ta != NULL; ta = ta->next)
689 if((rec = icwcstombs(ta->key, NULL)) == NULL)
691 if((val = icwcstombs(ta->val, NULL)) == NULL)
696 addtobuf(argv, NULL);
697 execv(filtername, argv);
698 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
701 insock = wrapsock(inpipe);
702 outsock = wrapsock(outpipe);
703 /* Really, really strange thing here - sometimes the kernel would
704 * return POLLIN on insock, even though it's a write-side
705 * pipe. The corresponding read on the pipe naturally returns
706 * EBADF, causing doldacond to think there's something wrong with
707 * the fd, and thus it closes it. Until I can find out whyever the
708 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
709 * set ignread on insock for now. */
711 transfer->filter = pid;
712 transfersetlocalend(transfer, insock);
713 getsock(transfer->filterout = outsock);
714 outsock->data = transfer;
715 outsock->readcb = (void (*)(struct socket *, void *))filterread;
726 struct transfer *transfer, *next;
728 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
730 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
732 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
733 transfer->iface->endofdata(transfer, transfer->ifacedata);
734 closesock(transfer->localend);
737 for(transfer = transfers; transfer != NULL; transfer = next)
739 next = transfer->next;
742 transferdetach(transfer);
743 freetransfer(transfer);
750 static struct configvar myvars[] =
752 /** The maximum number of simultaneously permitted uploads. A
753 * common hub rule is that you will need at least as many slots as
754 * the number of hubs to which you are connected. */
755 {CONF_VAR_INT, "slots", {.num = 3}},
756 /** The TOS value to use for upload connections (see the TOS
757 * VALUES section). */
758 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
759 /** The TOS value to use for download connections (see the TOS
760 * VALUES section). */
761 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
762 /** The name of the filter script (see the FILES section for
763 * lookup information). */
764 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
765 /** If true, only one upload is allowed per remote peer. This
766 * option is still experimental, so it is recommended to leave it
768 {CONF_VAR_BOOL, "ulquota", {.num = 0}},
772 static struct module me =