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 struct transfer *transfers = NULL;
45 GCBCHAIN(newtransfercb, struct transfer *);
47 void freetransfer(struct transfer *transfer)
49 if(transfer == transfers)
50 transfers = transfer->next;
51 if(transfer->next != NULL)
52 transfer->next->prev = transfer->prev;
53 if(transfer->prev != NULL)
54 transfer->prev->next = transfer->next;
55 CBCHAINDOCB(transfer, trans_destroy, transfer);
56 CBCHAINFREE(transfer, trans_ac);
57 CBCHAINFREE(transfer, trans_act);
58 CBCHAINFREE(transfer, trans_p);
59 CBCHAINFREE(transfer, trans_destroy);
60 CBCHAINFREE(transfer, trans_filterout);
61 while(transfer->args != NULL)
62 freewcspair(transfer->args, &transfer->args);
63 if(transfer->filter != -1)
65 if(transfer->etimer != NULL)
66 canceltimer(transfer->etimer);
67 if(transfer->auth != NULL)
68 authputhandle(transfer->auth);
69 if(transfer->peerid != NULL)
70 free(transfer->peerid);
71 if(transfer->peernick != NULL)
72 free(transfer->peernick);
73 if(transfer->path != NULL)
75 if(transfer->actdesc != NULL)
76 free(transfer->actdesc);
77 if(transfer->filterbuf != NULL)
78 free(transfer->filterbuf);
79 if(transfer->hash != NULL)
80 freehash(transfer->hash);
81 if(transfer->localend != NULL)
83 transfer->localend->readcb = NULL;
84 transfer->localend->writecb = NULL;
85 transfer->localend->errcb = NULL;
86 putsock(transfer->localend);
88 if(transfer->filterout != NULL)
90 transfer->filterout->readcb = NULL;
91 transfer->filterout->writecb = NULL;
92 transfer->filterout->errcb = NULL;
93 putsock(transfer->filterout);
95 if(transfer->fn != NULL)
96 putfnetnode(transfer->fn);
101 struct transfer *newtransfer(void)
103 struct transfer *new;
104 static int curid = 0;
106 new = smalloc(sizeof(*new));
107 memset(new, 0, sizeof(*new));
112 CBCHAININIT(new, trans_ac);
113 CBCHAININIT(new, trans_act);
114 CBCHAININIT(new, trans_p);
115 CBCHAININIT(new, trans_destroy);
116 CBCHAININIT(new, trans_filterout);
119 time(&new->activity);
124 void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
126 if(transfer->iface != NULL)
127 transferdetach(transfer);
128 transfer->iface = iface;
129 transfer->ifacedata = data;
132 void transferdetach(struct transfer *transfer)
134 if(transfer->iface != NULL)
136 transfer->iface->detach(transfer, transfer->ifacedata);
137 transfer->iface = NULL;
138 transfer->ifacedata = NULL;
142 struct transfer *finddownload(wchar_t *peerid)
144 struct transfer *transfer;
146 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
148 if((transfer->dir == TRNSD_DOWN) && (transfer->iface == NULL) && !wcscmp(peerid, transfer->peerid))
154 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
156 struct transfer *transfer;
158 transfer = newtransfer();
160 transfer->fnet = fnet;
162 transfer->fnet = fn->fnet;
163 transfer->peerid = swcsdup(nickid);
164 transfer->state = TRNS_HS;
165 transfer->dir = TRNSD_UP;
167 getfnetnode(transfer->fn = fn);
168 transferattach(transfer, iface, data);
169 linktransfer(transfer);
170 bumptransfer(transfer);
174 void linktransfer(struct transfer *transfer)
176 transfer->next = transfers;
177 transfer->prev = NULL;
178 if(transfers != NULL)
179 transfers->prev = transfer;
180 transfers = transfer;
181 GCBCHAINDOCB(newtransfercb, transfer);
184 void resettransfer(struct transfer *transfer)
186 if(transfer->dir == TRNSD_DOWN)
188 if(transfer->iface != NULL)
189 transferdetach(transfer);
190 killfilter(transfer);
191 transfersetstate(transfer, TRNS_WAITING);
192 transfersetactivity(transfer, L"reset");
197 struct transfer *findtransfer(int id)
199 struct transfer *transfer;
201 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
203 if(transfer->id == id)
209 static void transexpire(int cancelled, struct transfer *transfer)
211 transfer->etimer = NULL;
213 bumptransfer(transfer);
215 transfer->timeout = 0;
218 static void transferread(struct socket *sk, struct transfer *transfer)
220 if(sockgetdatalen(sk) >= 65536)
222 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
223 transfer->iface->gotdata(transfer, transfer->ifacedata);
226 static void transferwrite(struct socket *sk, struct transfer *transfer)
228 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
229 transfer->iface->wantdata(transfer, transfer->ifacedata);
232 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
234 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
235 transfer->iface->endofdata(transfer, transfer->ifacedata);
238 void transferputdata(struct transfer *transfer, void *buf, size_t size)
240 time(&transfer->activity);
241 sockqueue(transfer->localend, buf, size);
242 transfer->curpos += size;
243 CBCHAINDOCB(transfer, trans_p, transfer);
246 void transferendofdata(struct transfer *transfer)
248 if(transfer->curpos >= transfer->size)
250 transfersetstate(transfer, TRNS_DONE);
251 transfer->localend->readcb = NULL;
252 transfer->localend->writecb = NULL;
253 transfer->localend->errcb = NULL;
254 putsock(transfer->localend);
255 transfer->localend = NULL;
257 resettransfer(transfer);
261 size_t transferdatasize(struct transfer *transfer)
263 return(sockqueuesize(transfer->localend));
266 void *transfergetdata(struct transfer *transfer, size_t *size)
270 if(transfer->localend == NULL)
272 transfer->localend->ignread = 0;
273 time(&transfer->activity);
274 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
276 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
278 *size = transfer->endpos - transfer->curpos;
279 buf = srealloc(buf, *size);
281 transfer->curpos += *size;
282 CBCHAINDOCB(transfer, trans_p, transfer);
286 void transferprepul(struct transfer *transfer, size_t size, size_t start, size_t end, struct socket *lesk)
288 transfersetsize(transfer, size);
289 transfer->curpos = start;
290 transfer->endpos = end;
292 transfersetlocalend(transfer, lesk);
295 void transferstartdl(struct transfer *transfer, struct socket *sk)
297 transfersetstate(transfer, TRNS_MAIN);
298 socksettos(sk, confgetint("transfer", "dltos"));
301 void transferstartul(struct transfer *transfer, struct socket *sk)
303 transfersetstate(transfer, TRNS_MAIN);
304 socksettos(sk, confgetint("transfer", "ultos"));
305 if(transfer->localend != NULL)
306 transfer->localend->ignread = 0;
309 void transfersetlocalend(struct transfer *transfer, struct socket *sk)
311 if(transfer->localend != NULL)
312 putsock(transfer->localend);
313 getsock(transfer->localend = sk);
315 sk->readcb = (void (*)(struct socket *, void *))transferread;
316 sk->writecb = (void (*)(struct socket *, void *))transferwrite;
317 sk->errcb = (void (*)(struct socket *, int, void *))transfererr;
320 static int tryreq(struct transfer *transfer)
323 struct fnetpeer *peer;
325 if((fn = transfer->fn) != NULL)
327 if(fn->state != FNN_EST)
332 peer = fnetfindpeer(fn, transfer->peerid);
335 for(fn = fnetnodes; fn != NULL; fn = fn->next)
337 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
342 return(fn->fnet->reqconn(peer));
346 void trytransferbypeer(struct fnet *fnet, wchar_t *peerid)
348 struct transfer *transfer;
350 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
352 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING))
354 if((transfer->fnet == fnet) && !wcscmp(transfer->peerid, peerid))
356 if(!tryreq(transfer))
363 void bumptransfer(struct transfer *transfer)
367 if((now = time(NULL)) < transfer->timeout)
370 if(transfer->etimer == NULL)
371 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
374 if(transfer->etimer != NULL)
375 canceltimer(transfer->etimer);
376 switch(transfer->state)
379 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
380 if(now - transfer->lastreq > 30)
384 if(transfer->dir == TRNSD_UP)
386 if(now - transfer->activity < 60)
387 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
390 } else if(transfer->dir == TRNSD_DOWN) {
391 if(now - transfer->activity < 60)
392 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
394 resettransfer(transfer);
398 if(transfer->dir == TRNSD_UP)
400 if(now - transfer->activity < 300)
401 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
409 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
411 time(&transfer->activity);
414 if(transfer->actdesc != NULL)
415 free(transfer->actdesc);
416 transfer->actdesc = swcsdup(desc);
418 bumptransfer(transfer);
419 CBCHAINDOCB(transfer, trans_act, transfer);
422 void transfersetstate(struct transfer *transfer, int newstate)
424 transfer->state = newstate;
425 if(transfer->etimer != NULL)
426 canceltimer(transfer->etimer);
427 transfersetactivity(transfer, NULL);
428 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
431 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
433 if(transfer->peernick != NULL)
434 free(transfer->peernick);
435 transfer->peernick = swcsdup(newnick);
436 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
439 void transfersetsize(struct transfer *transfer, int newsize)
441 transfer->size = newsize;
442 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
445 void transferseterror(struct transfer *transfer, int error)
447 transfer->error = error;
448 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
451 void transfersetpath(struct transfer *transfer, wchar_t *path)
453 if(transfer->path != NULL)
454 free(transfer->path);
455 transfer->path = swcsdup(path);
456 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
459 void transfersethash(struct transfer *transfer, struct hash *hash)
461 if(transfer->hash != NULL)
462 freehash(transfer->hash);
463 transfer->hash = hash;
464 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
469 struct transfer *transfer;
472 slots = confgetint("transfer", "slots");
473 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
475 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
481 static void killfilter(struct transfer *transfer)
483 if(transfer->filter != -1)
485 kill(-transfer->filter, SIGHUP);
486 transfer->filter = -1;
488 if(transfer->localend)
490 transfer->localend->readcb = NULL;
491 transfer->localend->writecb = NULL;
492 transfer->localend->errcb = NULL;
493 putsock(transfer->localend);
494 transfer->localend = NULL;
496 if(transfer->filterout)
498 transfer->filterout->readcb = NULL;
499 putsock(transfer->filterout);
500 transfer->filterout = NULL;
502 if(transfer->filterbuf)
504 free(transfer->filterbuf);
505 transfer->filterbuf = NULL;
507 transfer->filterbufsize = transfer->filterbufdata = 0;
510 static char *findfilter(struct passwd *pwd)
512 char *path, *filtername;
514 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
516 if(!access(path, X_OK))
520 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
522 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
524 if(strchr(filtername, '/') == NULL)
526 if((path = sprintf2("/etc/%s", filtername)) != NULL)
528 if(!access(path, X_OK))
535 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
537 if(!access(path, X_OK))
544 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
546 if(!access(path, X_OK))
554 if(!access(filtername, X_OK))
562 static void filterread(struct socket *sk, struct transfer *transfer)
568 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
570 bufcat(transfer->filterbuf, buf, bufsize);
572 if((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
575 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
577 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
582 if((arg = icmbstowcs(p2, NULL)) == NULL)
583 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
585 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
590 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
592 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
596 static void filterexit(pid_t pid, int status, void *data)
598 struct transfer *transfer;
600 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
602 if(transfer->filter == pid)
604 transfer->filter = -1;
605 killfilter(transfer);
606 if(WEXITSTATUS(status))
608 resettransfer(transfer);
610 freetransfer(transfer);
612 trytransferbypeer(transfer->fnet, transfer->peerid);
618 int forkfilter(struct transfer *transfer)
620 char *filtername, *filename, *peerid, *buf;
622 struct passwd *pwent;
626 size_t argvsize, argvdata;
627 struct socket *insock, *outsock;
631 wfilename = transfer->path;
632 if(transfer->fnet->filebasename != NULL)
633 wfilename = transfer->fnet->filebasename(wfilename);
634 if(transfer->auth == NULL)
636 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
640 if((pwent = getpwuid(transfer->owner)) == NULL)
642 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
646 if((filtername = findfilter(pwent)) == NULL)
648 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
652 if((filename = icwcstombs(wfilename, NULL)) == NULL)
654 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
656 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
659 filename = sprintf2("utf8-%s", buf);
662 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
664 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
666 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
670 peerid = sprintf2("utf8-%s", buf);
673 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)
675 flog(LOG_WARNING, "could not fork session for filter for transfer %i: %s", transfer->id, strerror(errno));
681 argvsize = argvdata = 0;
682 buf = sprintf2("%i", transfer->size);
683 addtobuf(argv, filtername);
684 addtobuf(argv, filename);
686 addtobuf(argv, peerid);
689 if((buf = icwcstombs(unparsehash(transfer->hash), NULL)) != NULL)
691 /* XXX: I am very doubtful of this, but it can just as
692 * well be argued that all data should be presented as
693 * key-value pairs. */
694 addtobuf(argv, "hash");
697 flog(LOG_WARNING, "could not convert hash to local charset");
700 for(ta = transfer->args; ta != NULL; ta = ta->next)
702 if((rec = icwcstombs(ta->key, NULL)) == NULL)
704 if((val = icwcstombs(ta->val, NULL)) == NULL)
709 addtobuf(argv, NULL);
710 execv(filtername, argv);
711 flog(LOG_WARNING, "could not exec filter %s: %s", filtername, strerror(errno));
714 insock = wrapsock(inpipe);
715 outsock = wrapsock(outpipe);
716 /* Really, really strange thing here - sometimes the kernel would
717 * return POLLIN on insock, even though it's a write-side
718 * pipe. The corresponding read on the pipe naturally returns
719 * EBADF, causing doldacond to think there's something wrong with
720 * the fd, and thus it closes it. Until I can find out whyever the
721 * kernel gives a POLLIN on the fd (if I can at all...), I'll just
722 * set ignread on insock for now. */
724 transfer->filter = pid;
725 transfersetlocalend(transfer, insock);
726 getsock(transfer->filterout = outsock);
727 outsock->data = transfer;
728 outsock->readcb = (void (*)(struct socket *, void *))filterread;
739 struct transfer *transfer, *next;
741 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
743 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
745 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
746 transfer->iface->endofdata(transfer, transfer->ifacedata);
747 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 {CONF_VAR_INT, "slots", {.num = 3}},
766 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
767 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
768 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
772 static struct module me =