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 *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
160 struct transfer *transfer;
162 transfer = newtransfer();
164 transfer->fnet = fnet;
166 transfer->fnet = fn->fnet;
167 transfer->peerid = swcsdup(nickid);
168 transfer->state = TRNS_HS;
169 transfer->dir = TRNSD_UP;
171 getfnetnode(transfer->fn = fn);
172 transferattach(transfer, iface, data);
173 linktransfer(transfer);
174 bumptransfer(transfer);
178 void linktransfer(struct transfer *transfer)
180 transfer->next = transfers;
181 transfer->prev = NULL;
182 if(transfers != NULL)
183 transfers->prev = transfer;
184 transfers = transfer;
185 GCBCHAINDOCB(newtransfercb, transfer);
188 void resettransfer(struct transfer *transfer)
190 if(transfer->dir == TRNSD_DOWN)
192 if(transfer->iface != NULL)
193 transferdetach(transfer);
194 killfilter(transfer);
195 transfersetstate(transfer, TRNS_WAITING);
196 transfersetactivity(transfer, L"reset");
201 struct transfer *findtransfer(int id)
203 struct transfer *transfer;
205 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
207 if(transfer->id == id)
213 static void transexpire(int cancelled, struct transfer *transfer)
215 transfer->etimer = NULL;
217 bumptransfer(transfer);
219 transfer->timeout = 0;
222 static void transferread(struct socket *sk, struct transfer *transfer)
224 if(sockgetdatalen(sk) >= 65536)
226 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
227 transfer->iface->gotdata(transfer, transfer->ifacedata);
230 static void transferwrite(struct socket *sk, struct transfer *transfer)
232 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
233 transfer->iface->wantdata(transfer, transfer->ifacedata);
236 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
238 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
239 transfer->iface->endofdata(transfer, transfer->ifacedata);
242 void transferputdata(struct transfer *transfer, void *buf, size_t size)
244 time(&transfer->activity);
245 sockqueue(transfer->localend, buf, size);
246 transfer->curpos += size;
247 bytesdownload += size;
248 CBCHAINDOCB(transfer, trans_p, transfer);
251 void transferendofdata(struct transfer *transfer)
253 if(transfer->curpos >= transfer->size)
255 transfersetstate(transfer, TRNS_DONE);
256 transfer->localend->readcb = NULL;
257 transfer->localend->writecb = NULL;
258 transfer->localend->errcb = NULL;
259 putsock(transfer->localend);
260 transfer->localend = NULL;
262 resettransfer(transfer);
266 size_t transferdatasize(struct transfer *transfer)
268 return(sockqueuesize(transfer->localend));
271 void *transfergetdata(struct transfer *transfer, size_t *size)
275 if(transfer->localend == NULL)
277 transfer->localend->ignread = 0;
278 time(&transfer->activity);
279 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
281 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
283 *size = transfer->endpos - transfer->curpos;
284 buf = srealloc(buf, *size);
286 transfer->curpos += *size;
287 bytesupload += *size;
288 CBCHAINDOCB(transfer, trans_p, transfer);
292 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
294 transfersetsize(transfer, size);
295 transfer->curpos = start;
296 transfer->endpos = end;
298 transfersetlocalend(transfer, lesk);
301 void transferstartdl(struct transfer *transfer, struct socket *sk)
303 transfersetstate(transfer, TRNS_MAIN);
304 socksettos(sk, confgetint("transfer", "dltos"));
307 void transferstartul(struct transfer *transfer, struct socket *sk)
309 transfersetstate(transfer, TRNS_MAIN);
310 socksettos(sk, confgetint("transfer", "ultos"));
311 if(transfer->localend != NULL)
312 transfer->localend->ignread = 0;
315 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
317 if(transfer->localend != NULL)
318 putsock(transfer->localend);
319 getsock(transfer->localend = sk);
321 sk->readcb = (void (*)(struct socket *, void *))transferread;
322 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
323 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
326 static int tryreq(struct transfer *transfer)
329 struct fnetpeer *peer;
331 if((fn = transfer->fn) != NULL)
333 if(fn->state != FNN_EST)
338 peer = fnetfindpeer(fn, transfer->peerid);
341 for(fn = fnetnodes; fn != NULL; fn = fn->next)
343 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
349 time(&transfer->lastreq);
350 return(fn->fnet->reqconn(peer));
355 void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
357 struct transfer *transfer;
359 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
361 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
363 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
365 if(!tryreq(transfer))
372 void bumptransfer(struct transfer *transfer)
376 if((now = time(NULL)) < transfer->timeout)
379 if(transfer->etimer == NULL)
380 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
383 if(transfer->etimer != NULL)
384 canceltimer(transfer->etimer);
385 switch(transfer->state)
388 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
389 if(now - transfer->lastreq > 30)
393 if(transfer->dir == TRNSD_UP)
395 if(now - transfer->activity < 60)
396 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
399 } else if(transfer->dir == TRNSD_DOWN) {
400 if(now - transfer->activity < 60)
401 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
403 resettransfer(transfer);
407 if(transfer->dir == TRNSD_UP)
409 if(now - transfer->activity < 300)
410 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
418 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
420 time(&transfer->activity);
423 if(transfer->actdesc != NULL)
424 free(transfer->actdesc);
425 transfer->actdesc = swcsdup(desc);
427 bumptransfer(transfer);
428 CBCHAINDOCB(transfer, trans_act, transfer);
431 void transfersetstate(struct transfer *transfer, int newstate)
433 transfer->state = newstate;
434 if(transfer->etimer != NULL)
435 canceltimer(transfer->etimer);
436 transfersetactivity(transfer, NULL);
437 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
440 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
442 if(transfer->peernick != NULL)
443 free(transfer->peernick);
444 transfer->peernick = swcsdup(newnick);
445 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
448 void transfersetsize(struct transfer *transfer, int newsize)
450 transfer->size = newsize;
451 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
454 void transferseterror(struct transfer *transfer, int error)
456 transfer->error = error;
457 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
460 void transfersetpath(struct transfer *transfer, wchar_t *path)
462 if(transfer->path != NULL)
463 free(transfer->path);
464 transfer->path = swcsdup(path);
465 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
468 void transfersethash(struct transfer *transfer, struct hash *hash)
470 if(transfer->hash != NULL)
471 freehash(transfer->hash);
472 transfer->hash = hash;
473 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
478 struct transfer *transfer;
481 slots = confgetint("transfer", "slots");
482 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
484 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
490 static void killfilter(struct transfer *transfer)
492 if(transfer->filter != -1)
494 kill(-transfer->filter, SIGHUP);
495 transfer->filter = -1;
497 if(transfer->localend)
499 transfer->localend->readcb = NULL;
500 transfer->localend->writecb = NULL;
501 transfer->localend->errcb = NULL;
502 putsock(transfer->localend);
503 transfer->localend = NULL;
505 if(transfer->filterout)
507 transfer->filterout->readcb = NULL;
508 putsock(transfer->filterout);
509 transfer->filterout = NULL;
511 if(transfer->filterbuf)
513 free(transfer->filterbuf);
514 transfer->filterbuf = NULL;
516 transfer->filterbufsize = transfer->filterbufdata = 0;
519 static char *findfilter(struct passwd *pwd)
521 char *path, *filtername;
523 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
525 if(!access(path, X_OK))
529 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
531 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
533 if(strchr(filtername, '/') == NULL)
535 if((path = sprintf2("/etc/%s", filtername)) != NULL)
537 if(!access(path, X_OK))
544 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
546 if(!access(path, X_OK))
553 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
555 if(!access(path, X_OK))
563 if(!access(filtername, X_OK))
571 static void handletranscmd(struct transfer *transfer, wchar_t *cmd, wchar_t *arg)
573 if(!wcscmp(cmd, L"status")) {
576 if(transfer->exitstatus != NULL)
577 free(transfer->exitstatus);
578 transfer->exitstatus = swcsdup(arg);
582 static void filterread(struct socket *sk, struct transfer *transfer)
588 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
590 bufcat(transfer->filterbuf, buf, bufsize);
592 while((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
595 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
597 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
602 if((arg = icmbstowcs(p2, NULL)) == NULL)
603 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", p2, strerror(errno));
605 handletranscmd(transfer, cmd, arg);
606 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
611 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
613 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
617 static void filterexit(pid_t pid, int status, void *data)
619 struct transfer *transfer;
621 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
623 if(transfer->filter == pid)
625 transfer->filter = -1;
626 killfilter(transfer);
627 if(WEXITSTATUS(status))
629 resettransfer(transfer);
631 freetransfer(transfer);
633 trytransferbypeer(transfer->fnet, transfer->peerid);
639 int forkfilter(struct transfer *transfer)
641 char *filtername, *filename, *peerid, *buf;
643 struct passwd *pwent;
647 size_t argvsize, argvdata;
648 struct socket *insock, *outsock;
652 wfilename = transfer->path;
653 if(transfer->fnet->filebasename != NULL)
654 wfilename = transfer->fnet->filebasename(wfilename);
655 if(transfer->auth == NULL)
657 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
661 if((pwent = getpwuid(transfer->owner)) == NULL)
663 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
667 if((filtername = findfilter(pwent)) == NULL)
669 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
673 if((filename = icwcstombs(wfilename, NULL)) == NULL)
675 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
677 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
680 filename = sprintf2("utf8-%s", buf);
683 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
685 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
687 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
691 peerid = sprintf2("utf8-%s", buf);
694 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)
696 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
702 argvsize = argvdata = 0;
703 buf = sprintf2("%i", transfer->size);
704 addtobuf(argv, filtername);
705 addtobuf(argv, filename);
707 addtobuf(argv, peerid);
710 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
712 /* XXX: I am very doubtful of this, but it can just as
713 * well be argued that all data should be presented as
714 * key-value pairs. */
715 addtobuf(argv, "hash");
718 flog(LOG_WARNING, "could not convert hash to local charset");
721 for(ta = transfer->args; ta != NULL; ta = ta->next)
723 if((rec = icwcstombs(ta->key, NULL)) == NULL)
725 if((val = icwcstombs(ta->val, NULL)) == NULL)
730 addtobuf(argv, NULL);
731 execv(filtername, argv);
732 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
735 insock = wrapsock(inpipe);
736 outsock = wrapsock(outpipe);
737 /* Really, really strange thing here - sometimes the kernel would
738 * return POLLIN on insock, even though it's a write-side
739 * pipe. The corresponding read on the pipe naturally returns
740 * EBADF, causing doldacond to think there's something wrong with
741 * the fd, and thus it closes it. Until I can find out whyever the
742 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
743 * set ignread on insock for now. */
745 transfer->filter = pid;
746 transfersetlocalend(transfer, insock);
747 getsock(transfer->filterout = outsock);
748 outsock->data = transfer;
749 outsock->readcb = (void (*)(struct socket *, void *))filterread;
760 struct transfer *transfer, *next;
762 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
764 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
766 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
767 transfer->iface->endofdata(transfer, transfer->ifacedata);
768 closesock(transfer->localend);
771 for(transfer = transfers; transfer != NULL; transfer = next)
773 next = transfer->next;
776 transferdetach(transfer);
777 freetransfer(transfer);
784 static struct configvar myvars[] =
786 {CONF_VAR_INT, "slots", {.num = 3}},
787 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
788 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
789 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
793 static struct module me =