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
36 #include "sysevents.h"
42 static void killfilter(struct transfer *transfer);
44 unsigned long long bytesupload = 0;
45 unsigned long long bytesdownload = 0;
46 struct transfer *transfers = NULL;
48 GCBCHAIN(newtransfercb, struct transfer *);
50 void freetransfer(struct transfer *transfer)
52 if(transfer == transfers)
53 transfers = transfer->next;
54 if(transfer->next != NULL)
55 transfer->next->prev = transfer->prev;
56 if(transfer->prev != NULL)
57 transfer->prev->next = transfer->next;
58 CBCHAINDOCB(transfer, trans_destroy, transfer);
59 CBCHAINFREE(transfer, trans_ac);
60 CBCHAINFREE(transfer, trans_act);
61 CBCHAINFREE(transfer, trans_p);
62 CBCHAINFREE(transfer, trans_destroy);
63 CBCHAINFREE(transfer, trans_filterout);
64 while(transfer->args != NULL)
65 freewcspair(transfer->args, &transfer->args);
66 if(transfer->filter != -1)
68 if(transfer->etimer != NULL)
69 canceltimer(transfer->etimer);
70 if(transfer->auth != NULL)
71 authputhandle(transfer->auth);
72 if(transfer->peerid != NULL)
73 free(transfer->peerid);
74 if(transfer->peernick != NULL)
75 free(transfer->peernick);
76 if(transfer->path != NULL)
78 if(transfer->actdesc != NULL)
79 free(transfer->actdesc);
80 if(transfer->filterbuf != NULL)
81 free(transfer->filterbuf);
82 if(transfer->hash != NULL)
83 freehash(transfer->hash);
84 if(transfer->exitstatus != NULL)
85 free(transfer->exitstatus);
86 if(transfer->localend != NULL)
88 transfer->localend->readcb = NULL;
89 transfer->localend->writecb = NULL;
90 transfer->localend->errcb = NULL;
91 putsock(transfer->localend);
93 if(transfer->filterout != NULL)
95 transfer->filterout->readcb = NULL;
96 transfer->filterout->writecb = NULL;
97 transfer->filterout->errcb = NULL;
98 putsock(transfer->filterout);
100 if(transfer->fn != NULL)
101 putfnetnode(transfer->fn);
106 struct transfer *newtransfer(void)
108 struct transfer *new;
109 static int curid = 0;
111 new = smalloc(sizeof(*new));
112 memset(new, 0, sizeof(*new));
117 CBCHAININIT(new, trans_ac);
118 CBCHAININIT(new, trans_act);
119 CBCHAININIT(new, trans_p);
120 CBCHAININIT(new, trans_destroy);
121 CBCHAININIT(new, trans_filterout);
124 time(&new->activity);
129 static void localread(struct socket *sk, struct transfer *transfer)
135 if((transfer->datapipe != NULL) && (sockqueueleft(transfer->datapipe) > 0)) {
136 buf = sockgetinbuf(sk, &blen);
137 if((transfer->endpos >= 0) && (transfer->localpos + blen > transfer->endpos))
138 blen = transfer->endpos - transfer->localpos;
139 sockqueue(transfer->datapipe, buf, blen);
141 time(&transfer->activity);
142 transfer->localpos += blen;
145 if((curpos = transfer->localpos - socktqueuesize(transfer->datapipe)) < 0)
147 if(curpos != transfer->curpos) {
148 transfer->curpos = curpos;
149 CBCHAINDOCB(transfer, trans_p, transfer);
153 static void dataread(struct socket *sk, struct transfer *transfer)
158 if((transfer->localend != NULL) && (sockqueueleft(transfer->localend) > 0)) {
159 buf = sockgetinbuf(sk, &blen);
160 if((transfer->endpos >= 0) && (transfer->curpos + blen > transfer->endpos))
161 blen = transfer->endpos - transfer->curpos;
162 sockqueue(transfer->localend, buf, blen);
164 transfer->curpos += blen;
165 bytesdownload += blen;
166 CBCHAINDOCB(transfer, trans_p, transfer);
170 static void localwrite(struct socket *sk, struct transfer *transfer)
172 if(transfer->datapipe != NULL)
173 dataread(transfer->datapipe, transfer);
176 static void datawrite(struct socket *sk, struct transfer *transfer)
178 if(transfer->localend != NULL)
179 localread(transfer->localend, transfer);
182 static void localerr(struct socket *sk, int errno, struct transfer *transfer)
184 if(transfer->datapipe != NULL)
185 closesock(transfer->datapipe);
188 static void dataerr(struct socket *sk, int errno, struct transfer *transfer)
190 if(transfer->dir == TRNSD_DOWN) {
191 if(transfer->curpos >= transfer->size) {
192 transfersetstate(transfer, TRNS_DONE);
193 if(transfer->localend != NULL) {
194 closesock(transfer->localend);
195 quitsock(transfer->localend);
196 transfer->localend = NULL;
199 resettransfer(transfer);
201 } else if(transfer->dir == TRNSD_UP) {
206 void transferattach(struct transfer *transfer, struct socket *dpipe)
208 transferdetach(transfer);
209 getsock(transfer->datapipe = dpipe);
210 dpipe->readcb = (void (*)(struct socket *, void *))dataread;
211 dpipe->writecb = (void (*)(struct socket *, void *))datawrite;
212 dpipe->errcb = (void (*)(struct socket *, int, void *))dataerr;
213 dpipe->data = transfer;
216 void transferdetach(struct transfer *transfer)
218 if(transfer->datapipe != NULL) {
219 closesock(transfer->datapipe);
220 quitsock(transfer->datapipe);
222 transfer->datapipe = NULL;
225 struct transfer *finddownload(wchar_t *peerid)
227 struct transfer *transfer;
229 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
231 if((transfer->dir == TRNSD_DOWN) && (transfer->datapipe == NULL) && !wcscmp(peerid, transfer->peerid))
237 struct transfer *hasupload(struct fnet *fnet, wchar_t *peerid)
239 struct transfer *transfer;
241 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
243 if((transfer->dir == TRNSD_UP) && (transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
249 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct socket *dpipe)
251 struct transfer *transfer;
253 transfer = newtransfer();
255 transfer->fnet = fnet;
257 transfer->fnet = fn->fnet;
258 transfer->peerid = swcsdup(nickid);
259 transfer->state = TRNS_HS;
260 transfer->dir = TRNSD_UP;
262 getfnetnode(transfer->fn = fn);
263 transferattach(transfer, dpipe);
264 linktransfer(transfer);
265 bumptransfer(transfer);
269 void linktransfer(struct transfer *transfer)
271 transfer->next = transfers;
272 transfer->prev = NULL;
273 if(transfers != NULL)
274 transfers->prev = transfer;
275 transfers = transfer;
276 GCBCHAINDOCB(newtransfercb, transfer);
279 void resettransfer(struct transfer *transfer)
281 if(transfer->dir == TRNSD_DOWN)
283 transferdetach(transfer);
284 killfilter(transfer);
285 transfersetstate(transfer, TRNS_WAITING);
286 transfersetactivity(transfer, L"reset");
291 struct transfer *findtransfer(int id)
293 struct transfer *transfer;
295 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
297 if(transfer->id == id)
303 static void transexpire(int cancelled, struct transfer *transfer)
305 transfer->etimer = NULL;
307 bumptransfer(transfer);
309 transfer->timeout = 0;
312 void transferprepul(struct transfer *transfer, off_t size, off_t start, off_t end, struct socket *lesk)
314 transfersetsize(transfer, size);
315 transfer->curpos = transfer->localpos = start;
316 transfer->endpos = end;
317 transfersetlocalend(transfer, lesk);
320 void transferstartdl(struct transfer *transfer, struct socket *sk)
322 transfersetstate(transfer, TRNS_MAIN);
323 socksettos(sk, confgetint("transfer", "dltos"));
326 void transferstartul(struct transfer *transfer, struct socket *sk)
328 transfersetstate(transfer, TRNS_MAIN);
329 socksettos(sk, confgetint("transfer", "ultos"));
330 if(transfer->localend != NULL)
331 localread(transfer->localend, transfer);
334 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
336 if(transfer->localend != NULL)
337 putsock(transfer->localend);
338 getsock(transfer->localend = sk);
340 sk->readcb = (void (*)(struct socket *, void *))localread;
341 sk->writecb = (void (*)(struct socket *, void *))localwrite;
342 sk->errcb = (void (*)(struct socket *, int, void *))localerr;
345 static int tryreq(struct transfer *transfer)
348 struct fnetpeer *peer;
350 if((fn = transfer->fn) != NULL)
352 if(fn->state != FNN_EST)
357 peer = fnetfindpeer(fn, transfer->peerid);
360 for(fn = fnetnodes; fn != NULL; fn = fn->next)
362 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
368 time(&transfer->lastreq);
369 return(fn->fnet->reqconn(peer));
374 void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
376 struct transfer *transfer;
378 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
380 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
382 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
384 if(!tryreq(transfer))
391 void bumptransfer(struct transfer *transfer)
395 if((now = time(NULL)) < transfer->timeout)
398 if(transfer->etimer == NULL)
399 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
402 if(transfer->etimer != NULL)
403 canceltimer(transfer->etimer);
404 switch(transfer->state)
407 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
408 if(now - transfer->lastreq > 30)
412 if(transfer->dir == TRNSD_UP)
414 if(now - transfer->activity < 60)
415 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
418 } else if(transfer->dir == TRNSD_DOWN) {
419 if(now - transfer->activity < 60)
420 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
422 resettransfer(transfer);
426 if(transfer->dir == TRNSD_UP)
428 if(now - transfer->activity < 300)
429 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
437 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
439 time(&transfer->activity);
442 if(transfer->actdesc != NULL)
443 free(transfer->actdesc);
444 transfer->actdesc = swcsdup(desc);
446 bumptransfer(transfer);
447 CBCHAINDOCB(transfer, trans_act, transfer);
450 void transfersetstate(struct transfer *transfer, int newstate)
452 transfer->state = newstate;
453 if(transfer->etimer != NULL)
454 canceltimer(transfer->etimer);
455 transfersetactivity(transfer, NULL);
456 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
459 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
461 if(transfer->peernick != NULL)
462 free(transfer->peernick);
463 transfer->peernick = swcsdup(newnick);
464 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
467 void transfersetsize(struct transfer *transfer, off_t newsize)
469 transfer->size = newsize;
470 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
473 void transferseterror(struct transfer *transfer, int error)
475 transfer->error = error;
476 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
479 void transfersetpath(struct transfer *transfer, wchar_t *path)
481 if(transfer->path != NULL)
482 free(transfer->path);
483 transfer->path = swcsdup(path);
484 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
487 void transfersethash(struct transfer *transfer, struct hash *hash)
489 if(transfer->hash != NULL)
490 freehash(transfer->hash);
491 transfer->hash = hash;
492 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
497 struct transfer *transfer;
500 slots = confgetint("transfer", "slots");
501 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
503 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
509 static void killfilter(struct transfer *transfer)
511 if(transfer->filter != -1)
513 kill(-transfer->filter, SIGHUP);
514 transfer->filter = -1;
516 if(transfer->localend)
518 transfer->localend->readcb = NULL;
519 transfer->localend->writecb = NULL;
520 transfer->localend->errcb = NULL;
521 putsock(transfer->localend);
522 transfer->localend = NULL;
524 if(transfer->filterout)
526 transfer->filterout->readcb = NULL;
527 putsock(transfer->filterout);
528 transfer->filterout = NULL;
530 if(transfer->filterbuf)
532 free(transfer->filterbuf);
533 transfer->filterbuf = NULL;
535 transfer->filterbufsize = transfer->filterbufdata = 0;
538 static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
540 if(!wcscmp(cmd, L"status")) {
543 if(transfer->exitstatus != NULL)
544 free(transfer->exitstatus);
545 transfer->exitstatus = swcsdup(arg);
549 static void filterread(struct socket *sk, struct transfer *transfer)
555 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
557 bufcat(transfer->filterbuf, buf, bufsize);
559 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
562 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
564 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
569 if((arg = icmbstowcs(p2, NULL)) == NULL)
570 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
572 handletranscmd(transfer, cmd, arg);
573 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
578 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
580 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
584 static void filterexit(pid_t pid, int status, void *data)
586 struct transfer *transfer;
590 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
592 if(transfer->filter == pid)
594 transfer->filter = -1;
595 killfilter(transfer);
596 fnet = transfer->fnet;
597 peerid = swcsdup(transfer->peerid);
598 if(WEXITSTATUS(status))
599 resettransfer(transfer);
601 freetransfer(transfer);
602 trytransferbypeer(fnet, peerid);
609 int forkfilter(struct transfer *transfer)
611 char *filtername, *filename, *peerid, *buf, *p;
613 struct passwd *pwent;
617 size_t argvsize, argvdata;
618 struct socket *insock, *outsock;
622 wfilename = fnfilebasename(transfer->path);
623 if(transfer->auth == NULL)
625 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
629 if((pwent = getpwuid(transfer->owner)) == NULL)
631 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
635 filtername = findfile("dc-filter", pwent->pw_dir, 0);
636 if(filtername == NULL)
637 filtername = findfile(icswcstombs(confgetstr("transfer", "filter"), NULL, NULL), NULL, 0);
638 if(filtername == NULL)
640 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
644 if((filename = icwcstombs(wfilename, NULL)) == NULL)
646 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
648 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
651 filename = sprintf2("utf8-%s", buf);
654 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
656 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
658 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
662 peerid = sprintf2("utf8-%s", buf);
665 for(p = filename; *p; p++) {
668 else if((p == filename) && (*p == '.'))
671 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)
673 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
679 argvsize = argvdata = 0;
680 buf = sprintf2("%ji", (intmax_t)transfer->size);
681 addtobuf(argv, filtername);
682 addtobuf(argv, filename);
684 addtobuf(argv, peerid);
687 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
689 /* XXX: I am very doubtful of this, but it can just as
690 * well be argued that all data should be presented as
691 * key-value pairs. */
692 addtobuf(argv, "hash");
695 flog(LOG_WARNING, "could not convert hash to local charset");
698 for(ta = transfer->args; ta != NULL; ta = ta->next)
700 if((rec = icwcstombs(ta->key, NULL)) == NULL)
702 if((val = icwcstombs(ta->val, NULL)) == NULL)
707 addtobuf(argv, NULL);
708 execv(filtername, argv);
709 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
712 insock = wrapsock(inpipe);
713 outsock = wrapsock(outpipe);
714 /* Really, really strange thing here - sometimes the kernel would
715 * return POLLIN on insock, even though it's a write-side
716 * pipe. The corresponding read on the pipe naturally returns
717 * EBADF, causing doldacond to think there's something wrong with
718 * the fd, and thus it closes it. Until I can find out whyever the
719 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
720 * set ignread on insock for now. */
721 /* sockblock(insock, 1); */
722 transfer->filter = pid;
723 transfersetlocalend(transfer, insock);
724 getsock(transfer->filterout = outsock);
725 outsock->data = transfer;
726 outsock->readcb = (void (*)(struct socket *, void *))filterread;
737 struct transfer *transfer, *next;
740 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
742 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
744 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
745 transfer->iface->endofdata(transfer, transfer->ifacedata);
746 closesock(transfer->localend);
750 for(transfer = transfers; transfer != NULL; transfer = next)
752 next = transfer->next;
755 transferdetach(transfer);
756 freetransfer(transfer);
763 static struct configvar myvars[] =
765 /** The maximum number of simultaneously permitted uploads. A
766 * common hub rule is that you will need at least as many slots as
767 * the number of hubs to which you are connected. */
768 {CONF_VAR_INT, "slots", {.num = 3}},
769 /** The TOS value to use for upload connections (see the TOS
770 * VALUES section). */
771 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
772 /** The TOS value to use for download connections (see the TOS
773 * VALUES section). */
774 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
775 /** The name of the filter script (see the FILES section for
776 * lookup information). */
777 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
778 /** If true, only one upload is allowed per remote peer. This
779 * option is still experimental, so it is recommended to leave it
781 {CONF_VAR_BOOL, "ulquota", {.num = 0}},
785 static struct module me =