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
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
45 #include "sysevents.h"
50 * The Direct Connect protocol is extremely ugly. Thus, this code must
51 * also be a bit ugly in certain places. Please forgive me. =/
53 * This also means that there might be some pieces of the code that
54 * look completely illogical to you, and you think that you may make
55 * them much neater/better. However, in many cases it might just be
56 * that it's required to cope with some oddity of the protocol, such
57 * as the amazingly ugly delayed key response in the peer protocol.
60 /* I assume this is the correct character set to use for DC,
61 * considering it was developed without i18n support under Windows */
62 #define DCCHARSET "windows-1252"
64 #ifdef DCPP_MASQUERADE
66 * I honestly don't want to pretend being a client that I'm not, but
67 * there are so many hubs that simply do not accept any clients
68 * outside their whitelists, for no obvious reasons, so I feel that I
69 * am left with little choice. Anyhow, as long as I actually support
70 * all the features that my faked DC++ version does, there should be
71 * very little harm done.
74 #define DCIDTAGV "0.674"
75 #define DCIDFULL "DC++ 0.674"
77 #define DCIDTAG "Dolda"
78 #define DCIDTAGV VERSION
79 #define DCIDFULL "DoldaConnect " VERSION
94 void (*handler)(struct socket *sk, void *data, char *cmd, char *args);
100 struct qcommand *next;
107 size_t inbufdata, inbufsize;
108 struct qcommand *queue;
109 int extended, dcppemu;
116 struct dcexppeer *next, *prev;
119 struct timer *expire;
124 struct dcpeer *next, *prev;
128 size_t inbufdata, inbufsize;
129 size_t curread, totalsize;
131 struct timer *timeout;
132 struct qcommand *queue;
133 struct transfer *transfer;
135 int ptclose; /* Close after transfer is complete */
136 int accepted; /* If false, we connected, otherwise, we accepted */
137 int extended, dcppemu;
138 int direction; /* Using the constants from transfer.h */
140 int hascurpos, fetchingtthl, notthl;
141 struct tigertreehash tth;
149 static struct fnet dcnet;
150 static struct transferiface dctransfer;
151 static struct socket *udpsock = NULL;
152 static struct socket *tcpsock = NULL;
153 static struct dcpeer *peers = NULL;
155 static struct dcexppeer *expected = NULL;
156 static char *hmlistname = NULL;
157 static char *xmllistname = NULL;
158 static char *xmlbz2listname = NULL;
160 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn);
161 static void freedcpeer(struct dcpeer *peer);
162 static void transread(struct socket *sk, struct dcpeer *peer);
163 static void transerr(struct socket *sk, int err, struct dcpeer *peer);
164 static void transwrite(struct socket *sk, struct dcpeer *peer);
165 static void updatehmlist(void);
166 static void updatexmllist(void);
167 static void updatexmlbz2list(void);
168 static void requestfile(struct dcpeer *peer);
170 static int reservedchar(unsigned char c)
172 return((c == 0) || (c == 5) || (c == 124) || (c == 96) || (c == 126) || (c == 36));
175 /* Oh, how I despise having to do this... */
176 static char *dcmakekey(char *lock)
182 buf = smalloc(strlen(lock));
185 for(i = 0; lock[i]; i++)
187 buf[i] = lock[i] ^ save;
188 buf[i] = ((buf[i] & 0x0F) << 4) | ((buf[i] & 0xF0) >> 4);
190 if((i != 0) && reservedchar(buf[i]))
195 buf[0] ^= buf[i - 1];
196 if(reservedchar(buf[0]))
200 key = smalloc(len + 1);
202 for(i = 0; lock[i] != 0; i++)
204 if(reservedchar(buf[i]))
205 offset += sprintf(key + offset, "/%%DCN%03i%%/", buf[i]);
207 key[offset++] = buf[i];
214 static char *pathnmdc2adc(char *path)
217 size_t retsize, retdata;
219 if(!strcmp(path, "files.xml") || !strcmp(path, "files.xml.bz2") || !strcmp(path, "MyList.DcLst"))
220 return(sstrdup(path));
222 retsize = retdata = 0;
229 addtobuf(ret, *path);
235 static int isdchash(struct hash *hash)
237 if(wcscmp(hash->algo, L"TTH"))
244 static int supports(struct dcpeer *peer, char *cap)
248 if(peer->supports == NULL)
250 for(p = peer->supports; *p != NULL; p++)
252 if(!strcasecmp(*p, cap))
258 static void endcompress(struct dcpeer *peer)
260 if(peer->compress == CPRS_ZLIB)
262 deflateEnd(peer->cprsdata);
263 free(peer->cprsdata);
265 peer->compress = CPRS_NONE;
268 static void initcompress(struct dcpeer *peer, int algo)
273 peer->compress = algo;
274 if(algo == CPRS_ZLIB)
276 peer->cprsdata = smalloc(sizeof(z_stream));
277 memset(peer->cprsdata, 0, sizeof(z_stream));
278 if((ret = deflateInit(peer->cprsdata, 3)) != Z_OK)
280 flog(LOG_CRIT, "Aiya! zlib refuses to init (%i)!", ret);
286 static void unquote(wchar_t *in)
290 for(p = in; *p != L'\0'; p++)
294 for(p2 = p + 1; (*p2 != L'\0') && (*p2 != L';') && (*p2 != L'&'); p2++);
301 if(!wcscmp(p + 1, L"amp"))
304 } else if(p[1] == L'#') {
305 nc = ucptowc(wcstol(p + 2, NULL, 10));
314 memmove(p + 1, p2 + 1, (wcslen(p2 + 1) + 1) * sizeof(wchar_t));
319 static void freeexppeer(struct dcexppeer *ep)
322 ep->next->prev = ep->prev;
324 ep->prev->next = ep->next;
329 if(ep->expire != NULL)
330 canceltimer(ep->expire);
334 static void exppeerexpire(int cancelled, struct dcexppeer *ep)
341 static struct dcexppeer *expectpeer(char *nick, struct fnetnode *fn)
343 struct dcexppeer *ep;
345 ep = smalloc(sizeof(*ep));
346 ep->nick = sstrdup(nick);
347 getfnetnode(ep->fn = fn);
348 ep->expire = timercallback(ntime() + 300, (void (*)(int, void *))exppeerexpire, ep);
357 static struct qcommand *newqcmd(struct qcommand **queue, char *string)
359 struct qcommand *new;
361 while(*queue != NULL)
362 queue = &(*queue)->next;
363 new = smalloc(sizeof(*new));
364 new->string = sstrdup(string);
370 static struct qcommand *ulqcmd(struct qcommand **queue)
372 struct qcommand *qcmd;
374 if((qcmd = *queue) == NULL)
380 static void freeqcmd(struct qcommand *qcmd)
386 static void hubrecvchat(struct socket *sk, struct fnetnode *fn, char *from, char *string)
388 wchar_t *chat, *wfrom, *wpeer;
390 struct fnetpeer *peer;
392 end = string + strlen(string);
393 while((p = strchr(string, 13)) != NULL)
394 memmove(p, p + 1, (end-- - p));
397 if((strlen(string) > strlen(from) + 2) && (*string == '<') && !memcmp(string + 1, from, strlen(from)) && (*(string + strlen(from) + 1) == '>'))
398 string += strlen(from) + 2;
399 if((wfrom = icmbstowcs(from, DCCHARSET)) == NULL)
401 wpeer = swcsdup(wfrom);
407 for(p = string + 1; *p; p++)
409 if((*p == ' ') || (*p == '>'))
417 if((wpeer = icmbstowcs(string + 1, DCCHARSET)) == NULL)
423 wpeer = swcsdup(L"");
425 if((chat = icmbstowcs(string, DCCHARSET)) == NULL)
435 if((peer = fnetfindpeer(fn, wfrom)) == NULL) /* Assume public chat */
436 fnethandlechat(fn, 1, wfrom, wpeer, chat);
438 fnethandlechat(fn, 0, wfrom, wpeer, chat);
440 fnethandlechat(fn, 1, L"", wpeer, chat);
448 static void peertimeout(int cancelled, struct dcpeer *peer)
450 peer->timeout = NULL;
456 static void sendadc(struct socket *sk, char *arg)
459 size_t bufsize, bufdata;
462 bufsize = bufdata = 0;
468 bufcat(buf, "\\s", 2);
469 } else if(*arg == '\n') {
470 bufcat(buf, "\\n", 2);
471 } else if(*arg == '\\') {
472 bufcat(buf, "\\\\", 2);
477 sockqueue(sk, buf, bufdata);
481 static void sendadcf(struct socket *sk, char *arg, ...)
487 buf = vsprintf2(arg, args);
495 static char **parseadc(char *args)
498 size_t retbufsize, retbufdata;
500 size_t bufsize, bufdata;
505 retbufsize = retbufdata = bufsize = bufdata = 0;
514 else if(*args != ' ')
518 if((*args == ' ') || (*args == 0))
521 addtobuf(retbuf, buf);
523 bufsize = bufdata = 0;
526 } else if(*args == '\\') {
529 addtobuf(buf, *args);
538 addtobuf(retbuf, NULL);
541 } else if((*args == 's') || (*args == ' ')) {
543 } else if(*args == 'n') {
545 } else if(*args == '\\') {
555 addtobuf(retbuf, NULL);
559 /* Macros useful in command handlers */
560 #define skipspace(s) ({if(((s) = strchr((s), ' ')) == NULL) return; else (s)++;})
561 #define qstr(sk, str) sockqueue(sk, str, strlen(str))
562 #define qstrf(sk, strandargs...) \
565 if((__buf__ = sprintf2(strandargs)) != NULL) { \
566 sockqueue(sk, __buf__, strlen(__buf__)); \
571 static char *tr(char *str, char *trans)
575 for(; *trans; trans += 2)
577 for(p = strchr(str, trans[0]); p != NULL; p = strchr(p, trans[0]))
583 static char *getadcid(struct dcpeer *peer)
589 if(!wcscmp(peer->transfer->path, L"files.xml") || !wcscmp(peer->transfer->path, L"files.xml.bz2") || !wcscmp(peer->transfer->path, L"MyList.DcLst"))
591 if(!isfilelist && (peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
593 buf = base32encode(peer->transfer->hash->buf, 24);
594 ret = sprintf2("TTH/%.39s", buf);
597 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
599 ret = pathnmdc2adc(buf);
606 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
608 if(!wcscmp(cmd, L"resume"))
612 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
615 transfer->curpos = wcstol(arg, NULL, 10);
624 static void sendmynick(struct dcpeer *peer)
628 hub = (peer->fn == NULL)?NULL:(peer->fn->data);
630 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), DCCHARSET, "DoldaConnectUser-IN"));
632 qstrf(peer->sk, "$MyNick %s|", hub->nativenick);
635 static void sendpeerlock(struct dcpeer *peer)
638 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
640 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
643 static void sendsupports(struct dcpeer *peer)
646 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
648 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
651 static void requestfile(struct dcpeer *peer)
655 if(peer->transfer->size == -1)
657 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
659 transferseterror(peer->transfer, TRNSE_NOTFOUND);
663 /* The transfer will be restarted later from
664 * cmd_filelength when it detects that the sizes
666 qstrf(peer->sk, "$Get %s$1|", buf);
669 if((peer->transfer->hash == NULL) && !peer->notthl)
671 if(supports(peer, "adcget") && supports(peer, "tthl"))
673 qstr(peer->sk, "$ADCGET");
674 sendadc(peer->sk, "tthl");
675 if((buf = getadcid(peer)) == NULL)
677 transferseterror(peer->transfer, TRNSE_NOTFOUND);
681 sendadc(peer->sk, buf);
683 sendadc(peer->sk, "0");
684 sendadc(peer->sk, "-1");
686 peer->fetchingtthl = 1;
692 if(forkfilter(peer->transfer))
694 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
698 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
701 if(supports(peer, "adcget"))
703 qstr(peer->sk, "$ADCGET");
704 sendadc(peer->sk, "file");
705 if((buf = getadcid(peer)) == NULL)
707 transferseterror(peer->transfer, TRNSE_NOTFOUND);
711 sendadc(peer->sk, buf);
713 sendadcf(peer->sk, "%i", peer->transfer->curpos);
714 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
716 } else if(supports(peer, "xmlbzlist")) {
717 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
719 transferseterror(peer->transfer, TRNSE_NOTFOUND);
723 qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
725 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
727 transferseterror(peer->transfer, TRNSE_NOTFOUND);
731 qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1);
735 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
739 struct fnetnode *cfn;
743 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
744 buf = tr(icswcstombs(confgetstr("dc", "desc"), DCCHARSET, "Charset_conv_failure"), "$_|_");
745 qstrf(sk, "%s", buf);
747 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
749 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
751 if(cfn->regstatus == FNNS_OP)
753 else if(cfn->regstatus == FNNS_REG)
759 qstrf(sk, "<%s V:%s,M:%c,H:%i/%i/%i,S:%i>",
760 (hub->dcppemu)?"++":"Dolda",
761 (hub->dcppemu)?"0.674":VERSION,
762 (tcpsock == NULL)?'P':'A',
764 confgetint("transfer", "slots")
767 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), DCCHARSET, "Charset_conv_failure"), "$_|_");
768 qstrf(sk, "%s\x01$", buf);
769 buf = tr(icswcstombs(confgetstr("dc", "email"), DCCHARSET, "Charset_conv_failure"), "$_|_");
770 qstrf(sk, "%s$", buf);
771 qstrf(sk, "%llu$|", sharesize);
774 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
779 if(!strcmp(cmd, "$Lock"))
781 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
782 } else if(!strcmp(cmd, "$Hello")) {
783 if(fn->state == FNN_HS)
785 qstrf(sk, "$Version 1,0091|");
786 qstrf(sk, "$GetNickList|");
788 fnetsetstate(fn, FNN_EST);
790 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
795 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
802 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
804 if((p = strchr(args, ' ')) != NULL)
809 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
811 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
813 key = dcmakekey(args);
814 qstrf(sk, "$Key %s|", key);
816 hubhandleaction(sk, fn, cmd, args);
819 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
825 if(hub->nativename == NULL)
826 free(hub->nativename);
827 hub->nativename = sstrdup(args);
828 buf = icmbstowcs(args, DCCHARSET);
829 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
832 hubhandleaction(sk, fn, cmd, args);
835 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
841 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
843 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
844 fnetaddpeer(fn, nick, nick);
846 hubhandleaction(sk, fn, cmd, args);
849 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
852 struct fnetpeer *peer;
856 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
858 if((peer = fnetfindpeer(fn, nick)) != NULL)
861 hubhandleaction(sk, fn, cmd, args);
864 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
869 struct fnetpeer *peer, *npeer;
872 for(peer = fn->peers; peer != NULL; peer = peer->next)
873 peer->flags.b.delete = 1;
874 while((p = strstr(args, "$$")) != NULL)
877 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
879 if((peer = fnetfindpeer(fn, buf)) == NULL)
880 peer = fnetaddpeer(fn, buf, buf);
882 peer->flags.b.delete = 0;
884 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
888 for(peer = fn->peers; peer != NULL; peer = npeer)
891 if(peer->flags.b.delete)
894 hubhandleaction(sk, fn, cmd, args);
897 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
902 struct fnetpeer *peer;
905 for(peer = fn->peers; peer != NULL; peer = peer->next)
906 peer->flags.b.op = 0;
907 while((p = strstr(args, "$$")) != NULL)
910 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
912 if((peer = fnetfindpeer(fn, buf)) != NULL)
913 peer->flags.b.op = 1;
918 hubhandleaction(sk, fn, cmd, args);
921 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
924 wchar_t *buf, *wp, *wp2;
926 struct fnetpeer *peer;
931 if(strncmp(p, "$ALL ", 5))
934 if((p2 = strchr(p, ' ')) == NULL)
937 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
939 if((peer = fnetfindpeer(fn, buf)) == NULL)
940 peer = fnetaddpeer(fn, buf, buf);
943 if((p2 = strstr(p, "$ $")) == NULL)
946 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
948 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcschr(buf, L'<')) != NULL))
950 buf[wcslen(buf) - 1] = L'\0';
952 if((wp2 = wcschr(wp, L' ')) != NULL)
955 fnetpeersetstr(peer, L"dc-client", wp);
959 if((wp2 = wcschr(wp, L',')) != NULL)
963 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
964 fnetpeersetstr(peer, abuf, wp + 2);
966 } while(wp2 != NULL);
969 fnetpeersetstr(peer, L"descr", buf);
972 if((p2 = strchr(p, '$')) == NULL)
975 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
977 fnetpeersetstr(peer, L"dc-speed", buf);
980 if((p2 = strchr(p, '$')) == NULL)
983 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
985 fnetpeersetstr(peer, L"email", buf);
990 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
991 hubhandleaction(sk, fn, cmd, args);
994 /* I do not implement the fully in that I do not disconnect from the
995 * old hub. I do this since I believe that if the hub owner really
996 * wants to get rid of us, then it is his responsibility to disconnect
998 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1001 struct fnetnode *newfn;
1005 if(strchr(args, ':') == NULL)
1007 args = strcpy(smalloc(strlen(args) + 5), args);
1008 strcat(args, ":411");
1013 if((newfn = fnetinitconnect(L"dc", args, NULL)) != NULL)
1015 linkfnetnode(newfn);
1018 hubhandleaction(sk, fn, cmd, args);
1023 static char *getdcpath(struct sharecache *node, size_t *retlen)
1028 if(node->parent == NULL)
1030 if(node->parent == shareroot)
1032 if((buf = icwcstombs(node->name, DCCHARSET)) == NULL)
1035 *retlen = strlen(buf);
1038 if((buf2 = icwcstombs(node->name, DCCHARSET)) == NULL)
1040 if((buf = getdcpath(node->parent, &len)) == NULL)
1045 len2 = strlen(buf2);
1046 buf = srealloc(buf, len + 1 + len2 + 1);
1048 strcpy(buf + len, buf2);
1049 buf[len + len2] = 0;
1052 *retlen = len + len2;
1058 * This is the main share searching function for Direct Connect
1059 * peers. Feel free to optimize it if you feel the need for it. I
1060 * haven't ever seen it take any noticable CPU time anyway, so I
1061 * haven't felt that need.
1063 * It may feel dubious to just return the directory when all terms are
1064 * satisfied rather than all the files in it, but it really benefits
1065 * everyone: Less cycles used for us, less UDP squelching for active
1066 * searchers, and less bandwidth waste for hubs when serving passive
1069 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1074 char *prefix, *infix, *postfix, *buf, *buf2;
1076 struct sockaddr_in addr;
1077 struct sharecache *node;
1078 int minsize, maxsize;
1080 int termnum, satisfied, skipcheck;
1081 int level, tersat[32];
1086 if((p = strchr(args, ' ')) == NULL)
1090 memset(terms, 0, sizeof(terms));
1091 prefix = infix = postfix = NULL;
1095 if(!strncmp(args, "Hub:", 4))
1097 if(!strcmp(cmd, "$MultiSearch"))
1099 if(!strcmp(args + 4, hub->nativenick))
1101 prefix = sprintf2("$SR %s ", hub->nativenick);
1102 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1103 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1107 if((p2 = strchr(args, ':')) == NULL)
1110 addr.sin_family = AF_INET;
1111 if(!inet_aton(args, &addr.sin_addr))
1113 addr.sin_port = htons(atoi(p2));
1114 prefix = sprintf2("$SR %s ", hub->nativenick);
1115 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1116 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1117 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1120 minsize = maxsize = -1;
1129 if((*(p++) == 'T') && (minsize == 0))
1136 if((p2 = strchr(p, '?')) == NULL)
1151 if((*p2 == 0) || (*p2 == '$'))
1159 if(!dotth && !strncmp(p, "TTH:", 4))
1162 if((buf = base32decode(p + 4, &buflen)) == NULL)
1166 memcpy(hashtth, buf, 24);
1169 if((terms[termnum] = icmbstowcs(p, DCCHARSET)) != NULL)
1180 node = shareroot->child;
1182 for(i = 0; i < termnum; i++)
1188 if(node->f.b.type == FILE_REG)
1190 if((minsize >= 0) && (node->size < minsize))
1192 if((maxsize >= 0) && (node->size > maxsize))
1195 if(!skipcheck && dotth)
1197 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1202 for(i = 0; i < termnum; i++)
1206 if(wcsexists(node->name, terms[i]))
1210 } else if(node->child == NULL) {
1215 if(!skipcheck && (satisfied == termnum))
1217 if((buf = getdcpath(node, NULL)) != NULL)
1219 if(node->f.b.hastth)
1221 buf2 = base32encode(node->hashtth, 24);
1222 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1225 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1230 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1232 while(node->next == NULL)
1234 if((node = node->parent) == shareroot)
1238 if(node == shareroot)
1240 for(i = 0; i < termnum; i++)
1242 if(tersat[i] >= level)
1255 hubhandleaction(sk, fn, cmd, args);
1266 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1270 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1274 struct socket *newsk;
1275 struct sockaddr_in addr;
1278 if((p = strchr(args, ' ')) == NULL)
1281 if(strcmp(args, hub->nativenick))
1283 addr.sin_family = AF_INET;
1285 if((p = strchr(args, ':')) == NULL)
1288 addr.sin_port = htons(atoi(p));
1289 if(!inet_aton(args, &addr.sin_addr))
1291 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1293 hubhandleaction(sk, fn, cmd, args);
1296 static void sendctm(struct socket *sk, char *nick)
1298 struct sockaddr *addr;
1303 if(sockgetremotename(tcpsock, &addr, &addrlen) < 0)
1305 if(addr->sa_family == AF_INET)
1306 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1308 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1312 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1318 if((p = strchr(args, ' ')) == NULL)
1321 if(strcmp(p, hub->nativenick))
1324 expectpeer(args, fn);
1325 hubhandleaction(sk, fn, cmd, args);
1328 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1331 struct fnetnode *node;
1336 for(node = fnetnodes; node != NULL; node = node->next)
1338 if(node->state == FNN_EST)
1341 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1342 hubhandleaction(sk, fn, cmd, args);
1345 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1352 if((p2 = strchr(p, ' ')) == NULL)
1356 if((p2 = strchr(p, ' ')) == NULL)
1359 if(strcmp(p, "From:"))
1362 if((p2 = strstr(p, " $")) == NULL)
1366 hubrecvchat(fn->sk, fn, p, p2);
1367 hubhandleaction(sk, fn, cmd, args);
1370 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1374 char *nick, *filename, *hubname;
1378 wchar_t *wnick, *wfile;
1382 if((p2 = strchr(p, ' ')) == NULL)
1387 if((p2 = strchr(p, 5)) == NULL)
1391 if((p2 = strchr(p, ' ')) == NULL)
1396 if((p2 = strchr(p, '/')) == NULL)
1401 if((p2 = strchr(p, 5)) == NULL)
1405 if((p2 = strstr(p, " (")) == NULL)
1408 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
1410 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1415 sr = newsrchres(&dcnet, wfile, wnick);
1416 if(sr->peernick != NULL)
1418 sr->peernick = swcsdup(wnick);
1423 if(!strncmp(hubname, "TTH:", 4))
1425 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1428 sr->hash = newhash(L"TTH", 24, buf);
1432 getfnetnode(sr->fn = fn);
1435 hubhandleaction(sk, fn, cmd, args);
1438 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1440 /* Do nothing for now. */
1443 static void cmd_getpass(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1450 pw = wpfind(fn->args, L"password");
1451 if((pw == NULL) || ((mbspw = icwcstombs(pw, DCCHARSET)) == NULL))
1456 qstrf(sk, "$MyPass %s|", mbspw);
1458 fn->regstatus = FNNS_REG;
1459 hubhandleaction(sk, fn, cmd, args);
1462 static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1467 fn->regstatus = FNNS_OP;
1468 hubhandleaction(sk, fn, cmd, args);
1471 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1473 struct dcexppeer *expect;
1476 if(peer->nativename != NULL)
1477 free(peer->nativename);
1478 peer->nativename = sstrdup(args);
1479 if(peer->wcsname != NULL)
1480 free(peer->wcsname);
1481 if((peer->wcsname = icmbstowcs(peer->nativename, DCCHARSET)) == NULL)
1488 for(expect = expected; expect != NULL; expect = expect->next)
1490 if(!strcmp(expect->nick, args))
1497 hub = expect->fn->data;
1498 peer->fn = expect->fn;
1499 getfnetnode(peer->fn);
1500 peer->dcppemu = hub->dcppemu;
1501 freeexppeer(expect);
1506 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1510 struct transfer *transfer;
1512 if((p = strchr(args, ' ')) == NULL)
1515 if(!strcmp(args, "Upload"))
1517 if(!strcmp(args, "Download"))
1521 if((peer->transfer == NULL) || (mydir != peer->direction))
1526 if(peer->direction == TRNSD_DOWN)
1529 if(peer->wcsname == NULL)
1534 peer->direction = mydir;
1535 if(peer->direction == TRNSD_UP)
1537 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1539 if((transfer = finddownload(peer->wcsname)) == NULL)
1544 transferattach(transfer, &dctransfer, peer);
1545 transfersetstate(transfer, TRNS_HS);
1547 transfersetnick(transfer, peer->wcsname);
1548 peer->transfer = transfer;
1551 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1552 if(peer->key != NULL)
1553 qstrf(sk, "$Key %s|", peer->key);
1554 if(peer->direction == TRNSD_DOWN)
1559 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1562 struct transfer *transfer;
1564 if((p = strchr(args, ' ')) == NULL)
1567 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1569 key = dcmakekey(args);
1572 if(peer->wcsname == NULL)
1581 if((transfer = finddownload(peer->wcsname)) == NULL)
1583 peer->direction = TRNSD_UP;
1584 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1586 peer->direction = TRNSD_DOWN;
1587 transferattach(transfer, &dctransfer, peer);
1588 transfersetstate(transfer, TRNS_HS);
1590 transfersetnick(transfer, peer->wcsname);
1591 peer->transfer = transfer;
1592 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1593 qstrf(sk, "$Key %s|", key);
1595 if(peer->key != NULL)
1601 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1606 static void startdl(struct dcpeer *peer)
1608 if(peer->timeout != NULL)
1609 canceltimer(peer->timeout);
1610 peer->state = PEER_TRNS;
1611 transferstartdl(peer->transfer, peer->sk);
1612 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1613 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1616 static void startul(struct dcpeer *peer)
1618 if(peer->timeout != NULL)
1619 canceltimer(peer->timeout);
1620 peer->state = PEER_TRNS;
1621 transferstartul(peer->transfer, peer->sk);
1622 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1625 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1628 struct transfer *transfer;
1630 if(peer->transfer == NULL)
1636 if(peer->transfer->size != size)
1638 transfersetsize(peer->transfer, size);
1639 transfer = peer->transfer;
1641 trytransferbypeer(transfer->fnet, transfer->peerid);
1645 qstr(peer->sk, "$Send|");
1648 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1650 if(peer->fetchingtthl)
1652 peer->fetchingtthl = 0;
1657 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1659 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1660 resettransfer(peer->transfer);
1666 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1668 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1670 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1671 resettransfer(peer->transfer);
1684 {"MyList.DcLst", &hmlistname, updatehmlist},
1685 {"files.xml", &xmllistname, updatexmllist},
1686 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1690 static int openfilelist(char *name)
1695 for(i = 0; lists[i].name != NULL; i++)
1697 if(!strcmp(name, lists[i].name))
1701 if(lists[i].name == NULL)
1704 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1706 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1709 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1716 static struct sharecache *findbytth(char *tth32)
1720 struct sharecache *node;
1722 if((buf = base32decode(tth32, &buflen)) == NULL)
1729 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1731 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1738 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1740 struct sharecache *node;
1747 if((p2 = strchr(p, sep)) != NULL)
1749 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1756 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1761 struct sharecache *node;
1762 struct socket *lesk;
1766 if(peer->transfer == NULL)
1771 if((p = strchr(args, '$')) == NULL)
1777 if((offset = (atoi(p) - 1)) < 0)
1782 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1784 qstr(sk, "$Error Could not send file list|");
1787 } else if(fd >= 0) {
1788 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1789 transfersetpath(peer->transfer, buf2);
1790 peer->transfer->flags.b.minislot = 1;
1794 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1796 qstrf(sk, "$Error File not in share|");
1800 if((fd = opensharecache(node)) < 0)
1802 qstrf(sk, "$Error %s|", strerror(errno));
1806 buf = getfspath(node);
1807 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1808 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1810 transfersetpath(peer->transfer, buf2);
1813 if(fstat(fd, &sb) < 0)
1816 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1817 qstrf(sk, "$Error|");
1821 if(sb.st_size < 65536)
1822 peer->transfer->flags.b.minislot = 1;
1823 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1825 qstr(sk, "$MaxedOut|");
1829 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1832 qstrf(sk, "$Error Offset out of range|");
1836 lesk = wrapsock(fd);
1837 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1839 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1842 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1844 if(peer->transfer == NULL)
1849 if(peer->transfer->localend == NULL)
1858 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1863 size_t arrsize, arrdata;
1865 if(peer->supports != NULL)
1867 for(i = 0; peer->supports[i] != NULL; i++)
1868 free(peer->supports[i]);
1869 free(peer->supports);
1872 arrsize = arrdata = 0;
1876 if((p2 = strchr(p, ' ')) != NULL)
1880 addtobuf(arr, sstrdup(p));
1881 } while((p = p2) != NULL);
1882 addtobuf(arr, NULL);
1883 peer->supports = arr;
1886 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1890 int start, numbytes;
1891 char *charset, *buf;
1893 struct sharecache *node;
1895 struct socket *lesk;
1897 if(peer->transfer == NULL)
1903 if((p2 = strchr(p, ' ')) == NULL)
1911 if((p2 = strchr(p, ' ')) == NULL)
1919 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1922 charset = DCCHARSET;
1923 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1924 initcompress(peer, CPRS_ZLIB);
1925 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1927 qstr(sk, "$Error Could not send file list|");
1929 } else if(fd >= 0) {
1930 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1931 transfersetpath(peer->transfer, buf2);
1932 peer->transfer->flags.b.minislot = 1;
1936 if((node = resdcpath(p, charset, '\\')) == NULL)
1938 qstr(sk, "$Error File not in cache|");
1941 if((fd = opensharecache(node)) < 0)
1943 qstrf(sk, "$Error %s|", strerror(errno));
1946 buf = getfspath(node);
1947 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1948 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1950 transfersetpath(peer->transfer, buf2);
1953 if(fstat(fd, &sb) < 0)
1956 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1957 qstr(sk, "$Error|");
1960 if(sb.st_size < 65536)
1961 peer->transfer->flags.b.minislot = 1;
1962 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1964 qstr(sk, "$MaxedOut|");
1967 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1970 qstr(sk, "$Error Offset out of range|");
1973 if((numbytes < 0) || (start + numbytes > sb.st_size))
1974 numbytes = sb.st_size - start;
1975 lesk = wrapsock(fd);
1976 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1978 qstrf(sk, "$Sending %i|", numbytes);
1982 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1986 int start, numbytes;
1987 struct sharecache *node;
1989 struct socket *lesk;
1993 if(peer->transfer == NULL)
1998 if((argv = parseadc(args)) == NULL)
2003 if(parrlen(argv) < 4)
2008 start = atoi(argv[2]);
2009 numbytes = atoi(argv[3]);
2012 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
2014 qstr(sk, "$Error Could not send file list|");
2016 } else if(fd >= 0) {
2017 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
2018 transfersetpath(peer->transfer, wbuf);
2019 peer->transfer->flags.b.minislot = 1;
2023 if(!strncmp(argv[1], "TTH/", 4))
2025 if((node = findbytth(argv[1] + 4)) == NULL)
2027 qstr(sk, "$Error File not in cache|");
2031 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
2033 qstr(sk, "$Error File not in cache|");
2037 if((fd = opensharecache(node)) < 0)
2039 qstrf(sk, "$Error %s|", strerror(errno));
2042 buf = getfspath(node);
2043 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
2044 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2046 transfersetpath(peer->transfer, wbuf);
2049 if(!strcmp(argv[0], "file"))
2051 for(i = 4; argv[i] != NULL; i++)
2053 if(!strcmp(argv[i], "ZL1"))
2054 initcompress(peer, CPRS_ZLIB);
2056 if(fstat(fd, &sb) < 0)
2058 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2059 qstr(sk, "$Error|");
2062 if(sb.st_size < 65536)
2063 peer->transfer->flags.b.minislot = 1;
2064 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
2065 qstr(sk, "$MaxedOut|");
2068 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2070 qstr(sk, "$Error Offset out of range|");
2073 if((numbytes < 0) || (start + numbytes > sb.st_size))
2074 numbytes = sb.st_size - start;
2075 lesk = wrapsock(fd);
2076 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2079 qstr(sk, "$ADCSND");
2080 sendadc(sk, "file");
2081 sendadc(sk, argv[1]);
2082 sendadcf(sk, "%i", start);
2083 sendadcf(sk, "%i", numbytes);
2084 if(peer->compress == CPRS_ZLIB)
2088 } else if(!strcmp(argv[0], "tthl")) {
2090 * XXX: Implement full TTHL support.
2092 * In the meantime, this is better than nothing, since it at
2093 * least allows fetching of the TTH root if it isn't already
2098 qstr(sk, "$Error no TTHL data for virtual files|");
2101 qstr(sk, "$ADCSND");
2102 sendadc(sk, "tthl");
2103 sendadc(sk, argv[1]);
2107 sockqueue(sk, node->hashtth, 24);
2109 qstr(sk, "$Error Namespace not implemented|");
2119 static void handletthl(struct dcpeer *peer)
2123 while(peer->inbufdata >= 24)
2125 pushtigertree(&peer->tth, peer->inbuf);
2126 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2127 peer->curread += 24;
2129 if(peer->curread >= peer->totalsize)
2131 if(peer->timeout == NULL)
2132 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2133 peer->state = PEER_CMD;
2134 synctigertree(&peer->tth);
2135 restigertree(&peer->tth, buf);
2136 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2141 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2144 int start, numbytes;
2146 if(peer->transfer == NULL)
2151 if((argv = parseadc(args)) == NULL)
2156 if(parrlen(argv) < 4)
2161 start = atoi(argv[2]);
2162 numbytes = atoi(argv[3]);
2163 if(!strcmp(argv[0], "tthl"))
2165 if((start != 0) || (numbytes % 24 != 0))
2167 /* Weird. Bail out. */
2171 if(peer->timeout != NULL)
2172 canceltimer(peer->timeout);
2173 peer->state = PEER_TTHL;
2174 peer->totalsize = numbytes;
2176 peer->fetchingtthl = 0;
2177 inittigertree(&peer->tth);
2179 } else if(!strcmp(argv[0], "file")) {
2180 if(start != peer->transfer->curpos)
2185 if(start + numbytes != peer->transfer->size)
2187 transfersetsize(peer->transfer, start + numbytes);
2192 if(peer->inbufdata > 0)
2194 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2195 peer->inbufdata = 0;
2196 transread(sk, peer);
2199 /* We certainly didn't request this...*/
2208 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2212 if(peer->transfer == NULL)
2217 numbytes = atoi(args);
2218 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2220 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2225 if(peer->inbufdata > 0)
2227 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2228 peer->inbufdata = 0;
2229 transread(sk, peer);
2235 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2240 hubhandleaction(sk, fn, cmd, args);
2244 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2250 static int hubreqconn(struct fnetpeer *peer)
2255 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2260 if((hub = peer->fn->data) == NULL)
2265 if((mbsnick = icwcstombs(peer->id, DCCHARSET)) == NULL)
2266 return(1); /* Shouldn't happen, of course, but who knows... */
2269 sendctm(peer->fn->sk, mbsnick);
2270 expectpeer(mbsnick, peer->fn);
2272 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2278 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2281 char *mbsstring, *mbsto;
2284 if((mbsto = icwcstombs(to, DCCHARSET)) == NULL)
2289 if((mbsstring = icwcstombs(string, DCCHARSET)) == NULL)
2294 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2301 if(strchr(mbsstring, '|') != NULL)
2312 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2314 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2317 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2324 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2326 int minl, maxl, minr, maxr, retmin, retmax;
2331 findsizelimit(sexpr->l, &minl, &maxl);
2332 findsizelimit(sexpr->r, &minr, &maxr);
2333 retmin = (minl > minr)?minl:minr;
2334 if((maxl != -1) && (maxr != -1))
2335 retmax = (maxl < maxr)?maxl:maxr;
2344 findsizelimit(sexpr->l, &minl, &maxl);
2345 findsizelimit(sexpr->r, &minr, &maxr);
2346 retmin = (minl < minr)?minl:minr;
2347 if((maxl == -1) || (maxr == -1))
2350 retmax = (maxl > maxr)?maxl:maxr;
2353 findsizelimit(sexpr->l, &minl, &maxl);
2354 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2358 } else if((minl == 0) && (maxl != -1)) {
2361 } else if((minl != 0) && (maxl == -1)) {
2364 } else { /* This would yield two seperate intervals, which DC cannot handle */
2370 retmax = sexpr->d.n - 1;
2373 retmin = sexpr->d.n;
2374 retmax = sexpr->d.n;
2377 retmin = sexpr->d.n + 1;
2391 static struct hash *findsehash(struct sexpr *sexpr)
2393 struct hash *h1, *h2;
2398 if((h1 = findsehash(sexpr->l)) != NULL)
2400 if((h1 = findsehash(sexpr->r)) != NULL)
2404 h1 = findsehash(sexpr->l);
2405 h2 = findsehash(sexpr->r);
2410 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2411 return(sexpr->d.hash);
2418 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2421 struct wcslist *list, *cur;
2422 char *sstr, *buf, *p;
2423 size_t sstrsize, sstrdata;
2424 struct sockaddr *name;
2426 int minsize, maxsize;
2430 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2432 list = findsexprstrs(srch->sexpr);
2433 findsizelimit(srch->sexpr, &minsize, &maxsize);
2434 hash = findsehash(srch->sexpr);
2435 if((minsize != 0) && (maxsize != -1))
2437 /* Choose either minsize or maxsize by trying to determine
2438 * which choice will be most restrictive. The result can be
2439 * approximative at best anyway, so don't try too hard... */
2440 if((50000000 - maxsize) > (minsize - 50000000))
2446 sstrsize = sstrdata = 0;
2447 if((hash != NULL) && (hash->len == 24))
2449 /* Prioritize hash searches above all else */
2450 bufcat(sstr, "F?T?0?9?TTH:", 12);
2451 buf = base32encode(hash->buf, hash->len);
2453 bufcat(sstr, buf, 39);
2458 sizebuf2(sstr, sstrdata + 32, 1);
2459 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2460 } else if(maxsize != -1) {
2461 sizebuf2(sstr, sstrdata + 32, 1);
2462 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2464 bufcat(sstr, "F?F?0?1?", 8);
2468 for(cur = list; cur != NULL; cur = cur->next)
2470 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2472 /* Can't find anything anyway if the search expression
2473 * requires characters outside DC's charset. There's
2474 * nothing technically wrong with the search itself,
2475 * however, so return success. This should be
2476 * considered as an optimization. */
2483 addtobuf(sstr, '$');
2485 * It probably doesn't hurt if buf contains any extra
2486 * dollar signs - it will just result in extra search
2487 * terms, and the extraneous results will be filtered by
2488 * the search layer anyway. It hurts if it contains any
2489 * pipes, though, so let's sell them for money.
2491 for(p = buf; *p; p++)
2496 bufcat(sstr, buf, strlen(buf));
2500 /* Will match all files... :-/ */
2501 addtobuf(sstr, '.');
2507 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2509 flog(LOG_WARNING, "cannot get address of UDP socket");
2511 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2515 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2519 fn->lastsrch = time(NULL);
2527 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2528 struct command hubcmds[] =
2530 {"$Lock", cc(cmd_lock)},
2531 {"$HubName", cc(cmd_hubname)},
2532 {"$Hello", cc(cmd_hello)},
2533 {"$Quit", cc(cmd_quit)},
2534 {"$NickList", cc(cmd_nicklist)},
2535 {"$OpList", cc(cmd_oplist)},
2536 {"$MyINFO", cc(cmd_myinfo)},
2537 {"$ForceMove", cc(cmd_forcemove)},
2538 {"$Search", cc(cmd_search)},
2539 {"$MultiSearch", cc(cmd_search)},
2540 {"$ConnectToMe", cc(cmd_connecttome)},
2541 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2542 {"$GetNetInfo", cc(cmd_getnetinfo)},
2543 {"$To:", cc(cmd_to)},
2544 {"$SR", cc(cmd_sr)},
2545 {"$UserCommand", cc(cmd_usercommand)},
2546 {"$GetPass", cc(cmd_getpass)},
2547 {"$LogedIn", cc(cmd_logedin)}, /* sic */
2551 struct command peercmds[] =
2553 {"$MyNick", cc(cmd_mynick)},
2554 {"$Lock", cc(cmd_peerlock)},
2555 {"$Direction", cc(cmd_direction)},
2556 {"$Key", cc(cmd_key)},
2557 {"$FileLength", cc(cmd_filelength)},
2558 {"$Error", cc(cmd_error)},
2559 {"$MaxedOut", cc(cmd_maxedout)},
2560 {"$Get", cc(cmd_get)},
2561 {"$Send", cc(cmd_send)},
2562 {"$Supports", cc(cmd_supports)},
2563 {"$GetBlock", cc(cmd_getblock)},
2564 {"$UGetBlock", cc(cmd_getblock)},
2565 {"$GetZBlock", cc(cmd_getblock)},
2566 {"$UGetZBlock", cc(cmd_getblock)},
2567 {"$ADCGET", cc(cmd_adcget)},
2568 {"$ADCSND", cc(cmd_adcsnd), 1},
2569 {"$Sending", cc(cmd_sending), 1},
2574 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2576 CBUNREG(transfer, trans_filterout, peer);
2579 peer->transfer = NULL;
2583 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2591 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2593 if(sockqueuesize(peer->sk) < 65536)
2595 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2597 if(peer->compress == CPRS_NONE)
2599 sockqueue(peer->sk, buf, bufsize);
2600 } else if(peer->compress == CPRS_ZLIB) {
2601 cstr = peer->cprsdata;
2602 cstr->next_in = buf;
2603 cstr->avail_in = bufsize;
2604 while(cstr->avail_in > 0)
2606 cstr->next_out = outbuf;
2607 cstr->avail_out = sizeof(outbuf);
2608 if((ret = deflate(cstr, 0)) != Z_OK)
2610 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2614 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2619 if(peer->state == PEER_SYNC)
2621 if(peer->compress == CPRS_ZLIB)
2623 cstr = peer->cprsdata;
2624 cstr->next_in = NULL;
2628 cstr->next_out = outbuf;
2629 cstr->avail_out = sizeof(outbuf);
2630 ret = deflate(cstr, Z_FINISH);
2631 if((ret != Z_OK) && (ret != Z_STREAM_END))
2633 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2637 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2638 } while(ret != Z_STREAM_END);
2644 if(peer->timeout == NULL)
2645 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2646 peer->state = PEER_CMD;
2648 transfersetstate(transfer, TRNS_HS);
2649 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2650 transfer->flags.b.minislot = 0;
2651 peer->sk->writecb = NULL;
2658 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2660 peer->state = PEER_SYNC;
2661 dctransgotdata(transfer, peer);
2664 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2666 if(transferdatasize(transfer) < 65536)
2667 peer->sk->ignread = 0;
2670 static void transread(struct socket *sk, struct dcpeer *peer)
2674 struct transfer *transfer;
2676 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2678 if(peer->transfer == NULL)
2684 transferputdata(peer->transfer, buf, bufsize);
2686 if(peer->transfer->curpos >= peer->transfer->size)
2688 transfer = peer->transfer;
2689 transferdetach(transfer);
2690 transferendofdata(transfer);
2693 if(transferdatasize(peer->transfer) > 65535)
2697 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2699 struct transfer *transfer;
2701 if((transfer = peer->transfer) == NULL)
2706 transferdetach(transfer);
2707 transferendofdata(transfer);
2710 static void transwrite(struct socket *sk, struct dcpeer *peer)
2712 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2714 if(peer->transfer == NULL)
2719 dctransgotdata(peer->transfer, peer);
2722 static void udpread(struct socket *sk, void *data)
2724 char *buf, *p, *p2, *hashbuf;
2725 size_t buflen, hashlen;
2726 char *nick, *filename, *hubname;
2728 struct fnetnode *fn, *myfn;
2731 wchar_t *wnick, *wfile;
2734 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2736 buf = srealloc(buf, buflen + 1);
2738 if(!strncmp(buf, "$SR ", 4))
2742 if((p2 = strchr(p, ' ')) == NULL)
2750 if((p2 = strchr(p, 5)) == NULL)
2757 if((p2 = strchr(p, ' ')) == NULL)
2765 if((p2 = strchr(p, '/')) == NULL)
2773 if((p2 = strchr(p, 5)) == NULL)
2780 if((p2 = strstr(p, " (")) == NULL)
2786 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
2791 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2799 if(!strncmp(hubname, "TTH:", 4))
2801 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2804 hash = newhash(L"TTH", 24, hashbuf);
2808 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2810 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2812 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2825 sr = newsrchres(&dcnet, wfile, wnick);
2826 if(sr->peernick != NULL)
2828 sr->peernick = swcsdup(wnick);
2834 getfnetnode(sr->fn = myfn);
2843 static void hubread(struct socket *sk, struct fnetnode *fn)
2850 hub = (struct dchub *)fn->data;
2851 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2853 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2855 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2856 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2858 p = hub->inbuf + hub->inbufdata;
2859 hub->inbufdata += datalen;
2860 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2863 newqcmd(&hub->queue, hub->inbuf);
2864 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2865 datalen = hub->inbufdata;
2870 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2875 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2881 if((buf = icwcstombs(newnick, DCCHARSET)) == NULL)
2883 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2888 if(hub == NULL) /* Not yet connected */
2893 if(hub->nativenick != NULL)
2894 free(hub->nativenick);
2895 hub->nativenick = buf;
2899 static struct dchub *newdchub(struct fnetnode *fn)
2904 new = smalloc(sizeof(*new));
2905 memset(new, 0, sizeof(*new));
2907 if(confgetint("dc", "dcppemu"))
2909 if((emu = wpfind(fn->args, L"dcppemu")) != NULL)
2916 if(hubsetnick(fn, fn->mynick))
2917 fnetsetnick(fn, L"DoldaConnectUser-IN");
2918 /* IN as in Invalid Nick */
2922 static struct dcpeer *newdcpeer(struct socket *sk)
2926 new = smalloc(sizeof(*new));
2927 memset(new, 0, sizeof(*new));
2928 new->transfer = NULL;
2931 if(confgetint("dc", "dcppemu"))
2942 static void freedcpeer(struct dcpeer *peer)
2945 struct qcommand *qcmd;
2950 if(peer->next != NULL)
2951 peer->next->prev = peer->prev;
2952 if(peer->prev != NULL)
2953 peer->prev->next = peer->next;
2954 if(peer->transfer != NULL)
2956 if(peer->transfer->dir == TRNSD_UP)
2957 peer->transfer->close = 1;
2958 if(peer->transfer->dir == TRNSD_DOWN)
2959 resettransfer(peer->transfer);
2960 transferdetach(peer->transfer);
2962 if(peer->timeout != NULL)
2963 canceltimer(peer->timeout);
2964 if(peer->sk->data == peer)
2965 peer->sk->data = NULL;
2966 peer->sk->readcb = NULL;
2967 peer->sk->writecb = NULL;
2968 peer->sk->errcb = NULL;
2971 if(peer->supports != NULL)
2973 for(i = 0; peer->supports[i] != NULL; i++)
2974 free(peer->supports[i]);
2975 free(peer->supports);
2977 if(peer->inbuf != NULL)
2979 if(peer->key != NULL)
2981 if(peer->wcsname != NULL)
2982 free(peer->wcsname);
2983 if(peer->nativename != NULL)
2984 free(peer->nativename);
2985 if(peer->fn != NULL)
2986 putfnetnode(peer->fn);
2987 while((qcmd = ulqcmd(&peer->queue)) != NULL)
2993 static void hubconnect(struct fnetnode *fn)
2995 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
2996 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
2998 fn->data = newdchub(fn);
3003 static void hubdestroy(struct fnetnode *fn)
3006 struct qcommand *qcmd;
3008 hub = (struct dchub *)fn->data;
3011 if(fn->sk->data == fn)
3013 fn->sk->data = NULL;
3019 while((qcmd = ulqcmd(&hub->queue)) != NULL)
3021 if(hub->nativename != NULL)
3022 free(hub->nativename);
3023 if(hub->nativenick != NULL)
3024 free(hub->nativenick);
3025 if(hub->inbuf != NULL)
3030 static wchar_t *dcbasename(wchar_t *filename)
3034 if((ret = wcsrchr(filename, L'\\')) != NULL)
3039 static struct transferiface dctransfer =
3041 .detach = (void (*)(struct transfer *, void *))dctransdetach,
3042 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
3043 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
3044 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
3047 static struct fnet dcnet =
3050 .connect = hubconnect,
3051 .destroy = hubdestroy,
3052 .setnick = hubsetnick,
3053 .reqconn = hubreqconn,
3054 .sendchat = hubsendchat,
3055 .search = hubsearch,
3056 .filebasename = dcbasename
3059 static void peerread(struct socket *sk, struct dcpeer *peer)
3063 struct command *cmd;
3065 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
3067 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
3068 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
3070 peer->inbufdata += datalen;
3071 if(peer->state == PEER_CMD)
3074 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
3077 newqcmd(&peer->queue, peer->inbuf);
3078 for(cmd = peercmds; cmd->handler != NULL; cmd++)
3080 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
3083 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
3086 peer->state = PEER_STOP;
3090 } else if(peer->state == PEER_TTHL) {
3095 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3100 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3102 struct dcpeer *peer;
3111 peer = newdcpeer(sk);
3114 peer->dcppemu = hub->dcppemu;
3115 sk->readcb = (void (*)(struct socket *, void *))peerread;
3116 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3118 socksettos(sk, confgetint("fnet", "fnptos"));
3120 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3125 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3127 struct dcpeer *peer;
3129 peer = newdcpeer(newsk);
3131 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3132 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3134 socksettos(newsk, confgetint("fnet", "fnptos"));
3135 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3138 static void updatehmlist(void)
3140 int i, lev, ic, ret;
3141 struct sharecache *node;
3142 char *buf, *buf2, numbuf[32];
3143 size_t bufsize, bufdata;
3147 buf = smalloc(bufsize = 65536);
3148 node = shareroot->child;
3153 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3155 for(i = 0; i < lev; i++)
3157 bufcat(buf, buf2, strlen(buf2));
3159 if(node->f.b.type == FILE_REG)
3162 sprintf(numbuf, "%i", node->size);
3163 bufcat(buf, numbuf, strlen(numbuf));
3170 if((node->child != NULL) && !ic)
3174 } else if(node->next != NULL) {
3177 while(node->next == NULL)
3180 node = node->parent;
3181 if(node == shareroot)
3184 if(node == shareroot)
3189 if(hmlistname != NULL)
3194 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3195 if((fd = mkstemp(hmlistname)) < 0)
3197 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3202 * I do not want to implement a good Huffman encoder, and it's not
3203 * like Huffman encoding actually yields any impressive results
3204 * for DC file lists anyway, so I'll just output a bogus
3205 * tree. Implement a good encoder if you want to.
3207 write(fd, "HE3\r\0", 5);
3208 write(fd, &bufdata, 4);
3210 write(fd, &ibuf, 2);
3212 for(i = 0; i < 256; i++)
3215 write(fd, &ibuf, 1);
3217 for(i = 0; i < 256; i++)
3219 for(buf2 = buf; bufdata > 0;)
3221 if((ret = write(fd, buf2, bufdata)) < 0)
3223 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3234 static struct xmlent
3241 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3245 static wchar_t *escapexml(wchar_t *src)
3247 static wchar_t *buf = NULL;;
3250 size_t bufsize, bufdata;
3256 bufsize = bufdata = 0;
3257 for(; *src != L'\0'; src++)
3260 if((c > 0) && (c < 32))
3262 bufcat(buf, L"&#", 2);
3263 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3264 bufcat(buf, nbuf, ret);
3265 addtobuf(buf, L';');
3267 for(ent = entities; ent->ent != NULL; ent++)
3271 bufcat(buf, ent->ent, wcslen(ent->ent));
3275 if(ent->ent == NULL)
3276 addtobuf(buf, *src);
3279 addtobuf(buf, L'\0');
3283 static void updatexmllist(void)
3287 char cidbuf[14], *namebuf;
3289 struct sharecache *node;
3291 if(xmllistname != NULL)
3293 unlink(xmllistname);
3296 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3297 if((fd = mkstemp(xmllistname)) < 0)
3299 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3304 if((fs = fdopen(fd, "w")) == NULL)
3306 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3307 unlink(xmllistname);
3313 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3314 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3315 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3317 if(confgetint("dc", "dcppemu"))
3318 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"DC++ 0.674\">\r\n", cidbuf);
3320 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, "DoldaConnect" VERSION);
3322 node = shareroot->child;
3326 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3328 for(i = 0; i < lev; i++)
3330 if(node->child != NULL)
3332 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3337 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3338 if(node->f.b.hastth)
3340 hashbuf = base32encode(node->hashtth, 24);
3341 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3344 fprintf(fs, "/>\r\n");
3346 while(node->next == NULL)
3348 node = node->parent;
3349 if(node == shareroot)
3354 for(i = 0; i < lev; i++)
3356 fprintf(fs, "</Directory>\r\n");
3359 if(node == shareroot)
3365 if(confgetint("dc", "dcppemu"))
3366 fprintf(fs, "</FileListing>");
3368 fprintf(fs, "</FileListing>\r\n");
3372 static void updatexmlbz2list(void)
3381 if(xmllistname == NULL)
3383 if((in = fopen(xmllistname, "r")) == NULL)
3385 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3388 if(xmlbz2listname != NULL)
3390 unlink(xmlbz2listname);
3391 free(xmlbz2listname);
3393 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3394 if((fd = mkstemp(xmlbz2listname)) < 0)
3396 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3397 free(xmlbz2listname);
3398 xmlbz2listname = NULL;
3402 if((real = fdopen(fd, "w")) == NULL)
3404 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3406 unlink(xmlbz2listname);
3407 free(xmlbz2listname);
3408 xmlbz2listname = NULL;
3412 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3415 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3417 unlink(xmlbz2listname);
3418 free(xmlbz2listname);
3419 xmlbz2listname = NULL;
3425 bufdata = fread(buf, 1, sizeof(buf), in);
3426 BZ2_bzWrite(&err, out, buf, bufdata);
3429 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3433 static int shareupdate(unsigned long long uusharesize, void *data)
3441 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3444 struct command *cmd;
3446 if((p = strchr(qcmd->string, ' ')) != NULL)
3448 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3450 if(!strcmp(cmd->name, qcmd->string))
3453 if(cmd->handler != NULL)
3454 cmd->handler(sk, data, qcmd->string, p);
3457 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3461 static int run(void)
3463 struct fnetnode *fn, *nextfn;
3465 struct dcpeer *peer, *nextpeer;
3466 struct qcommand *qcmd;
3470 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3473 if(fn->fnet != &dcnet)
3475 if(fn->data == NULL)
3477 hub = (struct dchub *)fn->data;
3478 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3480 if(*qcmd->string == '$')
3482 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3483 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3484 } else if(*qcmd->string != 0) {
3485 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3492 for(peer = peers; peer != NULL; peer = nextpeer)
3494 nextpeer = peer->next;
3495 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3497 if(peer->timeout != NULL)
3498 canceltimer(peer->timeout);
3499 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3500 if(*qcmd->string == '$')
3501 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3510 static void preinit(int hup)
3517 static int updateudpport(struct configvar *var, void *uudata)
3519 struct sockaddr_in addr;
3520 struct socket *newsock;
3522 memset(&addr, 0, sizeof(addr));
3523 addr.sin_family = AF_INET;
3524 addr.sin_port = htons(var->val.num);
3525 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3527 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3530 newsock->readcb = udpread;
3537 static int updatetcpport(struct configvar *var, void *uudata)
3539 struct sockaddr_in addr;
3540 struct socket *newsock;
3542 memset(&addr, 0, sizeof(addr));
3543 addr.sin_family = AF_INET;
3544 addr.sin_port = htons(var->val.num);
3545 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3546 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3553 static int init(int hup)
3555 struct sockaddr_in addr;
3559 GCBREG(sharechangecb, shareupdate, NULL);
3564 addr.sin_family = AF_INET;
3565 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3566 addr.sin_port = htons(confgetint("dc", "udpport"));
3567 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3569 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3572 udpsock->readcb = udpread;
3573 addr.sin_port = htons(confgetint("dc", "tcpport"));
3574 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3575 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3576 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3577 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3578 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3583 static void terminate(void)
3585 if(hmlistname != NULL)
3590 if(xmllistname != NULL)
3592 unlink(xmllistname);
3595 if(xmlbz2listname != NULL)
3597 unlink(xmlbz2listname);
3598 free(xmlbz2listname);
3602 static struct configvar myvars[] =
3604 {CONF_VAR_STRING, "desc", {.str = L""}},
3605 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3606 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3607 {CONF_VAR_INT, "udpport", {.num = 0}},
3608 {CONF_VAR_INT, "tcpport", {.num = 0}},
3609 {CONF_VAR_BOOL, "dcppemu", {.num = 0}},
3613 static struct module me =
3622 .terminate = terminate,