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 char *findfilter(struct passwd *pwd)
533 char *path, *filtername;
535 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
537 if(!access(path, X_OK))
541 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
543 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
545 if(strchr(filtername, '/') == NULL)
547 if((path = sprintf2("/etc/%s", filtername)) != NULL)
549 if(!access(path, X_OK))
556 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
558 if(!access(path, X_OK))
565 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
567 if(!access(path, X_OK))
575 if(!access(filtername, X_OK))
583 static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
585 if(!wcscmp(cmd, L"status")) {
588 if(transfer->exitstatus != NULL)
589 free(transfer->exitstatus);
590 transfer->exitstatus = swcsdup(arg);
594 static void filterread(struct socket *sk, struct transfer *transfer)
600 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
602 bufcat(transfer->filterbuf, buf, bufsize);
604 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
607 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
609 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
614 if((arg = icmbstowcs(p2, NULL)) == NULL)
615 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
617 handletranscmd(transfer, cmd, arg);
618 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
623 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
625 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
629 static void filterexit(pid_t pid, int status, void *data)
631 struct transfer *transfer;
635 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
637 if(transfer->filter == pid)
639 transfer->filter = -1;
640 killfilter(transfer);
641 fnet = transfer->fnet;
642 peerid = swcsdup(transfer->peerid);
643 if(WEXITSTATUS(status))
644 resettransfer(transfer);
646 freetransfer(transfer);
647 trytransferbypeer(fnet, peerid);
654 int forkfilter(struct transfer *transfer)
656 char *filtername, *filename, *peerid, *buf;
658 struct passwd *pwent;
662 size_t argvsize, argvdata;
663 struct socket *insock, *outsock;
667 wfilename = transfer->path;
668 if(transfer->fnet->filebasename != NULL)
669 wfilename = transfer->fnet->filebasename(wfilename);
670 if(transfer->auth == NULL)
672 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
676 if((pwent = getpwuid(transfer->owner)) == NULL)
678 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
682 if((filtername = findfilter(pwent)) == NULL)
684 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
688 if((filename = icwcstombs(wfilename, NULL)) == NULL)
690 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
692 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
695 filename = sprintf2("utf8-%s", buf);
698 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
700 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
702 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
706 peerid = sprintf2("utf8-%s", buf);
709 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)
711 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
717 argvsize = argvdata = 0;
718 buf = sprintf2("%i", transfer->size);
719 addtobuf(argv, filtername);
720 addtobuf(argv, filename);
722 addtobuf(argv, peerid);
725 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
727 /* XXX: I am very doubtful of this, but it can just as
728 * well be argued that all data should be presented as
729 * key-value pairs. */
730 addtobuf(argv, "hash");
733 flog(LOG_WARNING, "could not convert hash to local charset");
736 for(ta = transfer->args; ta != NULL; ta = ta->next)
738 if((rec = icwcstombs(ta->key, NULL)) == NULL)
740 if((val = icwcstombs(ta->val, NULL)) == NULL)
745 addtobuf(argv, NULL);
746 execv(filtername, argv);
747 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
750 insock = wrapsock(inpipe);
751 outsock = wrapsock(outpipe);
752 /* Really, really strange thing here - sometimes the kernel would
753 * return POLLIN on insock, even though it's a write-side
754 * pipe. The corresponding read on the pipe naturally returns
755 * EBADF, causing doldacond to think there's something wrong with
756 * the fd, and thus it closes it. Until I can find out whyever the
757 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
758 * set ignread on insock for now. */
760 transfer->filter = pid;
761 transfersetlocalend(transfer, insock);
762 getsock(transfer->filterout = outsock);
763 outsock->data = transfer;
764 outsock->readcb = (void (*)(struct socket *, void *))filterread;
775 struct transfer *transfer, *next;
777 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
779 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
781 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
782 transfer->iface->endofdata(transfer, transfer->ifacedata);
783 closesock(transfer->localend);
786 for(transfer = transfers; transfer != NULL; transfer = next)
788 next = transfer->next;
791 transferdetach(transfer);
792 freetransfer(transfer);
799 static struct configvar myvars[] =
801 {CONF_VAR_INT, "slots", {.num = 3}},
802 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
803 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
804 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
805 {CONF_VAR_BOOL, "ulquota", {.num = 0}},
809 static struct module me =