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)
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((ta = transfer->args) != NULL)
65 transfer->args = ta->next;
70 if(transfer->filter != -1)
72 if(transfer->etimer != NULL)
73 canceltimer(transfer->etimer);
74 if(transfer->auth != NULL)
75 authputhandle(transfer->auth);
76 if(transfer->peerid != NULL)
77 free(transfer->peerid);
78 if(transfer->peernick != NULL)
79 free(transfer->peernick);
80 if(transfer->path != NULL)
82 if(transfer->actdesc != NULL)
83 free(transfer->actdesc);
84 if(transfer->filterbuf != NULL)
85 free(transfer->filterbuf);
86 if(transfer->hash != NULL)
87 freehash(transfer->hash);
88 if(transfer->localend != NULL)
90 transfer->localend->readcb = NULL;
91 transfer->localend->writecb = NULL;
92 transfer->localend->errcb = NULL;
93 putsock(transfer->localend);
95 if(transfer->filterout != NULL)
97 transfer->filterout->readcb = NULL;
98 transfer->filterout->writecb = NULL;
99 transfer->filterout->errcb = NULL;
100 putsock(transfer->filterout);
102 if(transfer->fn != NULL)
103 putfnetnode(transfer->fn);
108 struct transfer *newtransfer(void)
110 struct transfer *new;
111 static int curid = 0;
113 new = smalloc(sizeof(*new));
114 memset(new, 0, sizeof(*new));
119 CBCHAININIT(new, trans_ac);
120 CBCHAININIT(new, trans_act);
121 CBCHAININIT(new, trans_p);
122 CBCHAININIT(new, trans_destroy);
123 CBCHAININIT(new, trans_filterout);
126 time(&new->activity);
131 void transferaddarg(struct transfer *transfer, wchar_t *rec, wchar_t *val)
135 ta = smalloc(sizeof(*ta));
136 ta->rec = swcsdup(rec);
137 ta->val = swcsdup(val);
138 ta->next = transfer->args;
142 void transferattach(struct transfer *transfer, struct transferiface *iface, void *data)
144 if(transfer->iface != NULL)
145 transferdetach(transfer);
146 transfer->iface = iface;
147 transfer->ifacedata = data;
150 void transferdetach(struct transfer *transfer)
152 if(transfer->iface != NULL)
154 transfer->iface->detach(transfer, transfer->ifacedata);
155 transfer->iface = NULL;
156 transfer->ifacedata = NULL;
160 struct transfer *finddownload(wchar_t *peerid)
162 struct transfer *transfer;
164 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
166 if((transfer->dir == TRNSD_DOWN) && (transfer->iface == NULL) && !wcscmp(peerid, transfer->peerid))
172 struct transfer *newupload(struct fnetnode *fn, struct fnet *fnet, wchar_t *nickid, struct transferiface *iface, void *data)
174 struct transfer *transfer;
176 transfer = newtransfer();
178 transfer->fnet = fnet;
180 transfer->fnet = fn->fnet;
181 transfer->peerid = swcsdup(nickid);
182 transfer->state = TRNS_HS;
183 transfer->dir = TRNSD_UP;
185 getfnetnode(transfer->fn = fn);
186 transferattach(transfer, iface, data);
187 linktransfer(transfer);
188 bumptransfer(transfer);
192 void linktransfer(struct transfer *transfer)
194 transfer->next = transfers;
195 transfer->prev = NULL;
196 if(transfers != NULL)
197 transfers->prev = transfer;
198 transfers = transfer;
199 GCBCHAINDOCB(newtransfercb, transfer);
202 void resettransfer(struct transfer *transfer)
204 if(transfer->dir == TRNSD_DOWN)
206 if(transfer->iface != NULL)
207 transferdetach(transfer);
208 killfilter(transfer);
209 transfersetstate(transfer, TRNS_WAITING);
210 transfersetactivity(transfer, L"reset");
215 struct transfer *findtransfer(int id)
217 struct transfer *transfer;
219 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
221 if(transfer->id == id)
227 static void transexpire(int cancelled, struct transfer *transfer)
229 transfer->etimer = NULL;
231 bumptransfer(transfer);
233 transfer->timeout = 0;
236 static void transferread(struct socket *sk, struct transfer *transfer)
238 if(sockgetdatalen(sk) >= 65536)
240 if((transfer->iface != NULL) && (transfer->iface->gotdata != NULL))
241 transfer->iface->gotdata(transfer, transfer->ifacedata);
244 static void transferwrite(struct socket *sk, struct transfer *transfer)
246 if((transfer->iface != NULL) && (transfer->iface->wantdata != NULL))
247 transfer->iface->wantdata(transfer, transfer->ifacedata);
250 static void transfererr(struct socket *sk, int errno, struct transfer *transfer)
252 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
253 transfer->iface->endofdata(transfer, transfer->ifacedata);
256 void transferputdata(struct transfer *transfer, void *buf, size_t size)
258 time(&transfer->activity);
259 sockqueue(transfer->localend, buf, size);
260 transfer->curpos += size;
261 CBCHAINDOCB(transfer, trans_p, transfer);
264 void transferendofdata(struct transfer *transfer)
266 if(transfer->curpos >= transfer->size)
268 transfersetstate(transfer, TRNS_DONE);
269 transfer->localend->readcb = NULL;
270 transfer->localend->writecb = NULL;
271 transfer->localend->errcb = NULL;
272 putsock(transfer->localend);
273 transfer->localend = NULL;
275 resettransfer(transfer);
279 size_t transferdatasize(struct transfer *transfer)
281 return(sockqueuesize(transfer->localend));
284 void *transfergetdata(struct transfer *transfer, size_t *size)
288 if(transfer->localend == NULL)
290 transfer->localend->ignread = 0;
291 time(&transfer->activity);
292 if((buf = sockgetinbuf(transfer->localend, size)) == NULL)
294 if((transfer->endpos >= 0) && (transfer->curpos + *size >= transfer->endpos))
296 *size = transfer->endpos - transfer->curpos;
297 buf = srealloc(buf, *size);
299 transfer->curpos += *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 void bumptransfer(struct transfer *transfer)
341 struct fnetpeer *peer;
344 if((now = time(NULL)) < transfer->timeout)
346 if(transfer->etimer == NULL)
347 transfer->etimer = timercallback(transfer->timeout, (void (*)(int, void *))transexpire, transfer);
350 if(transfer->etimer != NULL)
351 canceltimer(transfer->etimer);
352 switch(transfer->state)
355 if(transfer->fn != NULL)
358 if(fn->state != FNN_EST)
363 peer = fnetfindpeer(fn, transfer->peerid);
366 for(fn = fnetnodes; fn != NULL; fn = fn->next)
368 if((fn->state == FNN_EST) && (fn->fnet == transfer->fnet) && ((peer = fnetfindpeer(fn, transfer->peerid)) != NULL))
372 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 30), (void (*)(int, void *))transexpire, transfer);
373 if(now - transfer->lastreq > 30)
377 fn->fnet->reqconn(peer);
378 time(&transfer->lastreq);
383 if(transfer->dir == TRNSD_UP)
385 if(now - transfer->activity < 60)
386 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
389 } else if(transfer->dir == TRNSD_DOWN) {
390 if(now - transfer->activity < 60)
391 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 60), (void (*)(int, void *))transexpire, transfer);
393 resettransfer(transfer);
397 if(transfer->dir == TRNSD_UP)
399 if(now - transfer->activity < 300)
400 transfer->etimer = timercallback(transfer->timeout = (time(NULL) + 300), (void (*)(int, void *))transexpire, transfer);
408 void transfersetactivity(struct transfer *transfer, wchar_t *desc)
410 time(&transfer->activity);
413 if(transfer->actdesc != NULL)
414 free(transfer->actdesc);
415 transfer->actdesc = swcsdup(desc);
417 bumptransfer(transfer);
418 CBCHAINDOCB(transfer, trans_act, transfer);
421 void transfersetstate(struct transfer *transfer, int newstate)
423 transfer->state = newstate;
424 if(transfer->etimer != NULL)
425 canceltimer(transfer->etimer);
426 transfersetactivity(transfer, NULL);
427 CBCHAINDOCB(transfer, trans_ac, transfer, L"state");
430 void transfersetnick(struct transfer *transfer, wchar_t *newnick)
432 if(transfer->peernick != NULL)
433 free(transfer->peernick);
434 transfer->peernick = swcsdup(newnick);
435 CBCHAINDOCB(transfer, trans_ac, transfer, L"nick");
438 void transfersetsize(struct transfer *transfer, int newsize)
440 transfer->size = newsize;
441 CBCHAINDOCB(transfer, trans_ac, transfer, L"size");
444 void transferseterror(struct transfer *transfer, int error)
446 transfer->error = error;
447 CBCHAINDOCB(transfer, trans_ac, transfer, L"error");
450 void transfersetpath(struct transfer *transfer, wchar_t *path)
452 if(transfer->path != NULL)
453 free(transfer->path);
454 transfer->path = swcsdup(path);
455 CBCHAINDOCB(transfer, trans_ac, transfer, L"path");
458 void transfersethash(struct transfer *transfer, struct hash *hash)
460 if(transfer->hash != NULL)
461 freehash(transfer->hash);
462 transfer->hash = hash;
463 CBCHAINDOCB(transfer, trans_ac, transfer, L"hash");
468 struct transfer *transfer;
471 slots = confgetint("transfer", "slots");
472 for(transfer = transfers; (transfer != NULL) && (slots > 0); transfer = transfer->next)
474 if((transfer->dir == TRNSD_UP) && (transfer->state == TRNS_MAIN) && !transfer->flags.b.minislot)
480 static void killfilter(struct transfer *transfer)
482 if(transfer->filter != -1)
484 kill(-transfer->filter, SIGHUP);
485 transfer->filter = -1;
487 if(transfer->localend)
489 transfer->localend->readcb = NULL;
490 transfer->localend->writecb = NULL;
491 transfer->localend->errcb = NULL;
492 putsock(transfer->localend);
493 transfer->localend = NULL;
495 if(transfer->filterout)
497 transfer->filterout->readcb = NULL;
498 putsock(transfer->filterout);
499 transfer->filterout = NULL;
501 if(transfer->filterbuf)
503 free(transfer->filterbuf);
504 transfer->filterbuf = NULL;
506 transfer->filterbufsize = transfer->filterbufdata = 0;
509 static char *findfilter(struct passwd *pwd)
511 char *path, *filtername;
513 if((path = sprintf2("%s/.dcdl-filter", pwd->pw_dir)) != NULL)
515 if(!access(path, X_OK))
519 if((filtername = icwcstombs(confgetstr("transfer", "filter"), NULL)) == NULL)
521 flog(LOG_WARNING, "could not convert filter name into local charset: %s", strerror(errno));
523 if(strchr(filtername, '/') == NULL)
525 if((path = sprintf2("/etc/%s", filtername)) != NULL)
527 if(!access(path, X_OK))
534 if((path = sprintf2("/usr/etc/%s", filtername)) != NULL)
536 if(!access(path, X_OK))
543 if((path = sprintf2("/usr/local/etc/%s", filtername)) != NULL)
545 if(!access(path, X_OK))
553 if(!access(filtername, X_OK))
561 static void filterread(struct socket *sk, struct transfer *transfer)
567 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
569 bufcat(transfer->filterbuf, buf, bufsize);
571 if((p = memchr(transfer->filterbuf, '\n', transfer->filterbufdata)) != NULL)
574 if((p2 = strchr(transfer->filterbuf, ' ')) != NULL)
576 if((cmd = icmbstowcs(transfer->filterbuf, NULL)) != NULL)
581 if((arg = icmbstowcs(p2, NULL)) == NULL)
582 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
584 CBCHAINDOCB(transfer, trans_filterout, transfer, cmd, arg);
589 flog(LOG_WARNING, "filter sent a string which could not be converted into the local charset: %s: %s", transfer->filterbuf, strerror(errno));
591 memmove(transfer->filterbuf, p, transfer->filterbufdata -= (p - transfer->filterbuf));
595 static void filterexit(pid_t pid, int status, void *data)
597 struct transfer *transfer;
599 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
601 if(transfer->filter == pid)
603 transfer->filter = -1;
604 killfilter(transfer);
605 if(WEXITSTATUS(status))
607 resettransfer(transfer);
609 freetransfer(transfer);
616 int forkfilter(struct transfer *transfer)
618 char *filtername, *filename, *peerid, *buf;
620 struct passwd *pwent;
624 size_t argvsize, argvdata;
625 struct socket *insock, *outsock;
629 wfilename = transfer->path;
630 if(transfer->fnet->filebasename != NULL)
631 wfilename = transfer->fnet->filebasename(wfilename);
632 if(transfer->auth == NULL)
634 flog(LOG_WARNING, "tried to fork filter for transfer with NULL authhandle (tranfer %i)", transfer->id);
638 if((pwent = getpwuid(transfer->owner)) == NULL)
640 flog(LOG_WARNING, "no passwd entry for uid %i (found in transfer %i)", transfer->owner, transfer->id);
644 if((filtername = findfilter(pwent)) == NULL)
646 flog(LOG_WARNING, "could not find filter for user %s", pwent->pw_name);
650 if((filename = icwcstombs(wfilename, NULL)) == NULL)
652 if((buf = icwcstombs(wfilename, "UTF-8")) == NULL)
654 flog(LOG_WARNING, "could convert transfer filename to neither local charset nor UTF-8: %s", strerror(errno));
657 filename = sprintf2("utf8-%s", buf);
660 if((peerid = icwcstombs(transfer->peerid, NULL)) == NULL)
662 if((buf = icwcstombs(transfer->peerid, "UTF-8")) == NULL)
664 flog(LOG_WARNING, "could convert transfer peerid to neither local charset nor UTF-8: %s", strerror(errno));
668 peerid = sprintf2("utf8-%s", buf);
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("%i", 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->rec, 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. */
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;
739 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
741 if((transfer->endpos >= 0) && (transfer->state == TRNS_MAIN) && (transfer->localend != NULL) && (transfer->localend->state == SOCK_EST) && (transfer->curpos >= transfer->endpos))
743 if((transfer->iface != NULL) && (transfer->iface->endofdata != NULL))
744 transfer->iface->endofdata(transfer, transfer->ifacedata);
745 closesock(transfer->localend);
748 for(transfer = transfers; transfer != NULL; transfer = next)
750 next = transfer->next;
753 transferdetach(transfer);
754 freetransfer(transfer);
761 static struct configvar myvars[] =
763 {CONF_VAR_INT, "slots", {.num = 3}},
764 {CONF_VAR_INT, "ultos", {.num = SOCK_TOS_MAXTP}},
765 {CONF_VAR_INT, "dltos", {.num = SOCK_TOS_MAXTP}},
766 {CONF_VAR_STRING, "filter", {.str = L"dc-filter"}},
770 static struct module me =