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
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
43 #include "sysevents.h"
48 * The Direct Connect protocol is extremely ugly. Thus, this code must
49 * also be a bit ugly in certain places. Please forgive me. =/
51 * This also means that there might be some pieces of the code that
52 * look completely illogical to you, and you think that you may make
53 * them much neater/better. However, in many cases it might just be
54 * that it's required to cope with some oddity of the protocol, such
55 * as the amazingly ugly delayed key response in the peer protocol.
58 /* I assume this is the correct character set to use for DC,
59 * considering it was developed without i18n support under Windows */
60 #define DCCHARSET "windows-1252"
62 #ifdef DCPP_MASQUERADE
64 * I honestly don't want to pretend being a client that I'm not, but
65 * there are so many hubs that simply do not accept any clients
66 * outside their whitelists, for no obvious reasons, so I feel that I
67 * am left with little choice. Anyhow, as long as I actually support
68 * all the features that my faked DC++ version does, there should be
69 * very little harm done.
72 #define DCIDTAGV "0.674"
73 #define DCIDFULL "DC++ 0.674"
75 #define DCIDTAG "Dolda"
76 #define DCIDTAGV VERSION
77 #define DCIDFULL "DoldaConnect " VERSION
92 void (*handler)(struct socket *sk, void *data, char *cmd, char *args);
98 struct qcommand *next;
106 size_t inbufdata, inbufsize;
107 struct qcommand *queue;
108 int extended, dcppemu;
117 struct dcexppeer *next, *prev;
120 struct timer *expire;
125 struct dcpeer *next, *prev;
129 size_t inbufdata, inbufsize;
130 size_t curread, totalsize;
132 struct timer *timeout;
133 struct qcommand *queue;
134 struct transfer *transfer;
136 int ptclose; /* Close after transfer is complete */
137 int accepted; /* If false, we connected, otherwise, we accepted */
138 int extended, dcppemu;
139 int direction; /* Using the constants from transfer.h */
141 int hascurpos, fetchingtthl, notthl;
142 struct tigertreehash tth;
151 static struct fnet dcnet;
152 static struct transferiface dctransfer;
153 static struct socket *udpsock = NULL;
154 static struct socket *tcpsock = NULL;
155 static struct dcpeer *peers = NULL;
157 static struct dcexppeer *expected = NULL;
158 static char *hmlistname = NULL;
159 static char *xmllistname = NULL;
160 static char *xmlbz2listname = NULL;
161 static struct timer *listwritetimer = NULL;
163 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn);
164 static void freedcpeer(struct dcpeer *peer);
165 static void transread(struct socket *sk, struct dcpeer *peer);
166 static void transerr(struct socket *sk, int err, struct dcpeer *peer);
167 static void transwrite(struct socket *sk, struct dcpeer *peer);
168 static void updatehmlist(void);
169 static void updatexmllist(void);
170 static void updatexmlbz2list(void);
171 static void requestfile(struct dcpeer *peer);
172 static void updatelists(int now);
174 static int reservedchar(unsigned char c)
176 return((c == 0) || (c == 5) || (c == 124) || (c == 96) || (c == 126) || (c == 36));
179 /* Oh, how I despise having to do this... */
180 static char *dcmakekey(char *lock)
186 buf = smalloc(strlen(lock));
189 for(i = 0; lock[i]; i++)
191 buf[i] = lock[i] ^ save;
192 buf[i] = ((buf[i] & 0x0F) << 4) | ((buf[i] & 0xF0) >> 4);
194 if((i != 0) && reservedchar(buf[i]))
199 buf[0] ^= buf[i - 1];
200 if(reservedchar(buf[0]))
204 key = smalloc(len + 1);
206 for(i = 0; lock[i] != 0; i++)
208 if(reservedchar(buf[i]))
209 offset += sprintf(key + offset, "/%%DCN%03i%%/", buf[i]);
211 key[offset++] = buf[i];
218 static char *pathnmdc2adc(char *path)
221 size_t retsize, retdata;
223 if(!strcmp(path, "files.xml") || !strcmp(path, "files.xml.bz2") || !strcmp(path, "MyList.DcLst"))
224 return(sstrdup(path));
226 retsize = retdata = 0;
233 addtobuf(ret, *path);
239 static int isdchash(struct hash *hash)
241 if(wcscmp(hash->algo, L"TTH"))
249 * Uncomment when used!
251 static int hubsupports(struct dchub *hub, char *cap)
255 if(hub->supports == NULL)
257 for(p = hub->supports; *p != NULL; p++)
259 if(!strcasecmp(*p, cap))
266 static int supports(struct dcpeer *peer, char *cap)
270 if(peer->supports == NULL)
272 for(p = peer->supports; *p != NULL; p++)
274 if(!strcasecmp(*p, cap))
280 static void endcompress(struct dcpeer *peer)
282 if(peer->compress == CPRS_ZLIB)
284 deflateEnd(peer->cprsdata);
285 free(peer->cprsdata);
287 peer->compress = CPRS_NONE;
290 static void initcompress(struct dcpeer *peer, int algo)
295 peer->compress = algo;
296 if(algo == CPRS_ZLIB)
298 peer->cprsdata = smalloc(sizeof(z_stream));
299 memset(peer->cprsdata, 0, sizeof(z_stream));
300 if((ret = deflateInit(peer->cprsdata, 3)) != Z_OK)
302 flog(LOG_CRIT, "Aiya! zlib refuses to init (%i)!", ret);
308 static void unquote(wchar_t *in)
312 for(p = in; *p != L'\0'; p++)
316 for(p2 = p + 1; (*p2 != L'\0') && (*p2 != L';') && (*p2 != L'&'); p2++);
323 if(!wcscmp(p + 1, L"amp"))
326 } else if(p[1] == L'#') {
327 nc = ucptowc(wcstol(p + 2, NULL, 10));
336 memmove(p + 1, p2 + 1, (wcslen(p2 + 1) + 1) * sizeof(wchar_t));
341 static void freeexppeer(struct dcexppeer *ep)
344 ep->next->prev = ep->prev;
346 ep->prev->next = ep->next;
351 if(ep->expire != NULL)
352 canceltimer(ep->expire);
356 static void exppeerexpire(int cancelled, struct dcexppeer *ep)
363 static struct dcexppeer *expectpeer(char *nick, struct fnetnode *fn)
365 struct dcexppeer *ep;
367 ep = smalloc(sizeof(*ep));
368 ep->nick = sstrdup(nick);
369 getfnetnode(ep->fn = fn);
370 ep->expire = timercallback(ntime() + 300, (void (*)(int, void *))exppeerexpire, ep);
379 static struct qcommand *newqcmd(struct qcommand **queue, char *string)
381 struct qcommand *new;
383 while(*queue != NULL)
384 queue = &(*queue)->next;
385 new = smalloc(sizeof(*new));
386 new->string = sstrdup(string);
392 static struct qcommand *ulqcmd(struct qcommand **queue)
394 struct qcommand *qcmd;
396 if((qcmd = *queue) == NULL)
402 static void freeqcmd(struct qcommand *qcmd)
408 static void hubrecvchat(struct socket *sk, struct fnetnode *fn, char *from, char *string)
410 wchar_t *chat, *wfrom, *wpeer;
412 struct fnetpeer *peer;
416 end = string + strlen(string);
417 while((p = strchr(string, 13)) != NULL)
418 memmove(p, p + 1, (end-- - p));
421 if((strlen(string) > strlen(from) + 2) && (*string == '<') && !memcmp(string + 1, from, strlen(from)) && (*(string + strlen(from) + 1) == '>'))
422 string += strlen(from) + 2;
423 if((wfrom = icmbstowcs(from, hub->charset)) == NULL)
425 wpeer = swcsdup(wfrom);
431 for(p = string + 1; *p; p++)
433 if((*p == ' ') || (*p == '>'))
441 if((wpeer = icmbstowcs(string + 1, hub->charset)) == NULL)
447 wpeer = swcsdup(L"");
449 if((chat = icmbstowcs(string, hub->charset)) == NULL)
459 if((peer = fnetfindpeer(fn, wfrom)) == NULL) /* Assume public chat */
460 fnethandlechat(fn, 1, wfrom, wpeer, chat);
462 fnethandlechat(fn, 0, wfrom, wpeer, chat);
464 fnethandlechat(fn, 1, L"", wpeer, chat);
472 static void peertimeout(int cancelled, struct dcpeer *peer)
474 peer->timeout = NULL;
480 static void sendadc(struct socket *sk, char *arg)
483 size_t bufsize, bufdata;
486 bufsize = bufdata = 0;
492 bufcat(buf, "\\s", 2);
493 } else if(*arg == '\n') {
494 bufcat(buf, "\\n", 2);
495 } else if(*arg == '\\') {
496 bufcat(buf, "\\\\", 2);
501 sockqueue(sk, buf, bufdata);
505 static void sendadcf(struct socket *sk, char *arg, ...)
511 buf = vsprintf2(arg, args);
519 static char **parseadc(char *args)
522 size_t retbufsize, retbufdata;
524 size_t bufsize, bufdata;
529 retbufsize = retbufdata = bufsize = bufdata = 0;
538 else if(*args != ' ')
542 if((*args == ' ') || (*args == 0))
545 addtobuf(retbuf, buf);
547 bufsize = bufdata = 0;
550 } else if(*args == '\\') {
553 addtobuf(buf, *args);
562 addtobuf(retbuf, NULL);
565 } else if((*args == 's') || (*args == ' ')) {
567 } else if(*args == 'n') {
569 } else if(*args == '\\') {
579 addtobuf(retbuf, NULL);
583 /* Macros useful in command handlers */
584 #define skipspace(s) ({if(((s) = strchr((s), ' ')) == NULL) return; else (s)++;})
585 #define qstr(sk, str) sockqueue(sk, str, strlen(str))
586 #define qstrf(sk, strandargs...) \
589 if((__buf__ = sprintf2(strandargs)) != NULL) { \
590 sockqueue(sk, __buf__, strlen(__buf__)); \
595 static char *tr(char *str, char *trans)
599 for(; *trans; trans += 2)
601 for(p = strchr(str, trans[0]); p != NULL; p = strchr(p, trans[0]))
607 static char *getadcid(struct dcpeer *peer)
614 if(!wcscmp(peer->transfer->path, L"files.xml") || !wcscmp(peer->transfer->path, L"files.xml.bz2") || !wcscmp(peer->transfer->path, L"MyList.DcLst"))
616 if(!isfilelist && (peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
618 buf = base32encode(peer->transfer->hash->buf, 24);
619 ret = sprintf2("TTH/%.39s", buf);
622 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
624 ret = pathnmdc2adc(buf);
631 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
633 if(!wcscmp(cmd, L"resume"))
637 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
640 transfer->curpos = wcstol(arg, NULL, 10);
649 static void sendmynick(struct dcpeer *peer)
655 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), peer->charset, "DoldaConnectUser-IN"));
657 qstrf(peer->sk, "$MyNick %s|", icswcstombs(fn->mynick, peer->charset, "DoldaConnectUser-IN"));
660 static void sendpeerlock(struct dcpeer *peer)
663 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
665 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
668 static void sendsupports(struct dcpeer *peer)
671 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
673 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
676 static void requestfile(struct dcpeer *peer)
680 if(peer->transfer->size == -1)
682 /* Use DCCHARSET for $Get paths until further researched... */
683 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
685 transferseterror(peer->transfer, TRNSE_NOTFOUND);
689 /* The transfer will be restarted later from
690 * cmd_filelength when it detects that the sizes
692 qstrf(peer->sk, "$Get %s$1|", buf);
695 if((peer->transfer->hash == NULL) && !peer->notthl)
697 if(supports(peer, "adcget") && supports(peer, "tthl"))
699 qstr(peer->sk, "$ADCGET");
700 sendadc(peer->sk, "tthl");
701 if((buf = getadcid(peer)) == NULL)
703 transferseterror(peer->transfer, TRNSE_NOTFOUND);
707 sendadc(peer->sk, buf);
709 sendadc(peer->sk, "0");
710 sendadc(peer->sk, "-1");
712 peer->fetchingtthl = 1;
718 if(forkfilter(peer->transfer))
720 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
724 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
727 if(supports(peer, "adcget"))
729 qstr(peer->sk, "$ADCGET");
730 sendadc(peer->sk, "file");
731 if((buf = getadcid(peer)) == NULL)
733 transferseterror(peer->transfer, TRNSE_NOTFOUND);
737 sendadc(peer->sk, buf);
739 sendadcf(peer->sk, "%i", peer->transfer->curpos);
740 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
742 } else if(supports(peer, "xmlbzlist")) {
743 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
745 transferseterror(peer->transfer, TRNSE_NOTFOUND);
749 qstrf(peer->sk, "$UGetBlock %zi %zi %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
751 /* Use DCCHARSET for $Get paths until further researched... */
752 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
754 transferseterror(peer->transfer, TRNSE_NOTFOUND);
758 qstrf(peer->sk, "$Get %s$%zi|", buf, peer->transfer->curpos + 1);
762 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
766 struct fnetnode *cfn;
770 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
771 buf = tr(icswcstombs(confgetstr("dc", "desc"), hub->charset, "Charset_conv_failure"), "$_|_");
772 qstrf(sk, "%s", buf);
774 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
776 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
778 if(cfn->regstatus == FNNS_OP)
780 else if(cfn->regstatus == FNNS_REG)
786 qstrf(sk, "<%s V:%s,M:%c,H:%i/%i/%i,S:%i>",
787 (hub->dcppemu)?"++":"Dolda",
788 (hub->dcppemu)?"0.674":VERSION,
789 (tcpsock == NULL)?'P':'A',
791 confgetint("transfer", "slots")
794 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), hub->charset, "Charset_conv_failure"), "$_|_");
795 qstrf(sk, "%s\x01$", buf);
796 buf = tr(icswcstombs(confgetstr("dc", "email"), hub->charset, "Charset_conv_failure"), "$_|_");
797 qstrf(sk, "%s$", buf);
798 qstrf(sk, "%llu$|", sharesize);
801 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
806 if(!strcmp(cmd, "$Lock"))
808 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
809 } else if(!strcmp(cmd, "$Hello")) {
810 if(fn->state == FNN_HS)
812 qstrf(sk, "$Version 1,0091|");
813 qstrf(sk, "$GetNickList|");
815 fnetsetstate(fn, FNN_EST);
817 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
822 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
829 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
831 if((p = strchr(args, ' ')) != NULL)
836 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
838 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
840 key = dcmakekey(args);
841 qstrf(sk, "$Key %s|", key);
843 hubhandleaction(sk, fn, cmd, args);
846 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
852 if(hub->nativename == NULL)
853 free(hub->nativename);
854 hub->nativename = sstrdup(args);
855 buf = icmbstowcs(args, hub->charset);
856 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
859 hubhandleaction(sk, fn, cmd, args);
862 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
868 if((nick = icmbstowcs(args, hub->charset)) == NULL)
870 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
871 fnetaddpeer(fn, nick, nick);
873 hubhandleaction(sk, fn, cmd, args);
876 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
879 struct fnetpeer *peer;
883 if((nick = icmbstowcs(args, hub->charset)) == NULL)
885 if((peer = fnetfindpeer(fn, nick)) != NULL)
888 hubhandleaction(sk, fn, cmd, args);
891 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
896 struct fnetpeer *peer, *npeer;
899 for(peer = fn->peers; peer != NULL; peer = peer->next)
900 peer->flags.b.delete = 1;
901 while((p = strstr(args, "$$")) != NULL)
904 if((buf = icmbstowcs(args, hub->charset)) != NULL)
906 if((peer = fnetfindpeer(fn, buf)) == NULL)
907 peer = fnetaddpeer(fn, buf, buf);
909 peer->flags.b.delete = 0;
911 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
915 for(peer = fn->peers; peer != NULL; peer = npeer)
918 if(peer->flags.b.delete)
921 hubhandleaction(sk, fn, cmd, args);
924 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
929 struct fnetpeer *peer;
932 for(peer = fn->peers; peer != NULL; peer = peer->next)
933 peer->flags.b.op = 0;
934 while((p = strstr(args, "$$")) != NULL)
937 if((buf = icmbstowcs(args, hub->charset)) != NULL)
939 if((peer = fnetfindpeer(fn, buf)) != NULL)
940 peer->flags.b.op = 1;
945 hubhandleaction(sk, fn, cmd, args);
948 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
951 wchar_t *buf, *wp, *wp2;
953 struct fnetpeer *peer;
958 if(strncmp(p, "$ALL ", 5))
961 if((p2 = strchr(p, ' ')) == NULL)
964 if((buf = icmbstowcs(p, hub->charset)) == NULL)
966 if((peer = fnetfindpeer(fn, buf)) == NULL)
967 peer = fnetaddpeer(fn, buf, buf);
970 if((p2 = strstr(p, "$ $")) == NULL)
973 if((buf = icmbstowcs(p, hub->charset)) == NULL)
975 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcsrchr(buf, L'<')) != NULL))
977 buf[wcslen(buf) - 1] = L'\0';
979 if((wp2 = wcschr(wp, L' ')) != NULL)
982 fnetpeersetstr(peer, L"dc-client", wp);
986 if((wp2 = wcschr(wp, L',')) != NULL)
990 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
991 fnetpeersetstr(peer, abuf, wp + 2);
993 } while(wp2 != NULL);
996 fnetpeersetstr(peer, L"descr", buf);
999 if((p2 = strchr(p, '$')) == NULL)
1002 if((buf = icmbstowcs(p, hub->charset)) == NULL)
1004 fnetpeersetstr(peer, L"dc-speed", buf);
1007 if((p2 = strchr(p, '$')) == NULL)
1010 if((buf = icmbstowcs(p, hub->charset)) == NULL)
1012 fnetpeersetstr(peer, L"email", buf);
1017 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
1018 hubhandleaction(sk, fn, cmd, args);
1021 /* I do not implement the fully in that I do not disconnect from the
1022 * old hub. I do this since I believe that if the hub owner really
1023 * wants to get rid of us, then it is his responsibility to disconnect
1025 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1028 struct fnetnode *newfn;
1032 if(strchr(args, ':') == NULL)
1034 args = strcpy(smalloc(strlen(args) + 5), args);
1035 strcat(args, ":411");
1040 if((newfn = fnetinitconnect(L"dc", fn->owner, args, NULL)) != NULL)
1042 linkfnetnode(newfn);
1045 hubhandleaction(sk, fn, cmd, args);
1050 static char *getdcpath(struct sharecache *node, size_t *retlen, char *charset)
1055 if(node->parent == NULL)
1057 if(node->parent == shareroot)
1059 if((buf = icwcstombs(node->name, charset)) == NULL)
1062 *retlen = strlen(buf);
1065 if((buf2 = icwcstombs(node->name, charset)) == NULL)
1067 if((buf = getdcpath(node->parent, &len, charset)) == NULL)
1072 len2 = strlen(buf2);
1073 buf = srealloc(buf, len + 1 + len2 + 1);
1075 strcpy(buf + len, buf2);
1076 buf[len + len2] = 0;
1079 *retlen = len + len2;
1085 * This is the main share searching function for Direct Connect
1086 * peers. Feel free to optimize it if you feel the need for it. I
1087 * haven't ever seen it take any noticable CPU time anyway, so I
1088 * haven't felt that need.
1090 * It may feel dubious to just return the directory when all terms are
1091 * satisfied rather than all the files in it, but it really benefits
1092 * everyone: Less cycles used for us, less UDP squelching for active
1093 * searchers, and less bandwidth waste for hubs when serving passive
1096 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1101 char *prefix, *infix, *postfix, *buf, *buf2;
1103 struct sockaddr_in addr;
1104 struct sharecache *node;
1105 int minsize, maxsize;
1108 int termnum, satisfied, skipcheck;
1109 int level, tersat[32];
1114 if((p = strchr(args, ' ')) == NULL)
1118 memset(terms, 0, sizeof(terms));
1119 prefix = infix = postfix = NULL;
1123 if(!strncmp(args, "Hub:", 4))
1125 if(!strcmp(cmd, "$MultiSearch"))
1127 if(!strcmp(args + 4, hub->nativenick))
1129 prefix = sprintf2("$SR %s ", hub->nativenick);
1130 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1131 postfix = sprintf2(" (%s)\005%s|", formataddress(hub->sk->remote, hub->sk->remotelen), args + 4);
1135 if((p2 = strchr(args, ':')) == NULL)
1138 addr.sin_family = AF_INET;
1139 if(!inet_aton(args, &addr.sin_addr))
1141 addr.sin_port = htons(atoi(p2));
1142 prefix = sprintf2("$SR %s ", hub->nativenick);
1143 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1144 postfix = sprintf2(" (%s)|", formataddress(hub->sk->remote, hub->sk->remotelen));
1145 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1148 minsize = maxsize = -1;
1157 if((*(p++) == 'T') && (minsize == 0))
1164 if((p2 = strchr(p, '?')) == NULL)
1179 if((*p2 == 0) || (*p2 == '$'))
1187 if(!dotth && !strncmp(p, "TTH:", 4))
1190 if(((buf = base32decode(p + 4, &buflen)) == NULL) || (buflen != 24))
1195 memcpy(hashtth, buf, 24);
1198 if((terms[termnum] = icmbstowcs(p, hub->charset)) != NULL)
1209 node = shareroot->child;
1211 for(i = 0; i < termnum; i++)
1217 if(node->f.b.type == FILE_REG)
1219 if((minsize >= 0) && (node->size < minsize))
1221 if((maxsize >= 0) && (node->size > maxsize))
1224 if(!skipcheck && dotth)
1226 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1231 for(i = 0; i < termnum; i++)
1235 if(wcsexists(node->name, terms[i]))
1239 } else if(node->child == NULL) {
1244 if(!skipcheck && (satisfied == termnum))
1246 /* Use DCCHARSET in $Get paths until further researched... */
1247 if((buf = getdcpath(node, NULL, DCCHARSET)) != NULL)
1249 if(node->f.b.hastth)
1251 buf2 = base32encode(node->hashtth, 24);
1252 qstrf(dsk, "%s%s\005%zi%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1255 qstrf(dsk, "%s%s\005%zi%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1260 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1262 while(node->next == NULL)
1264 if((node = node->parent) == shareroot)
1268 if(node == shareroot)
1270 for(i = 0; i < termnum; i++)
1272 if(tersat[i] >= level)
1285 hubhandleaction(sk, fn, cmd, args);
1296 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1300 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1304 struct socket *newsk;
1305 struct sockaddr_in addr;
1308 if((p = strchr(args, ' ')) == NULL)
1311 if(strcmp(args, hub->nativenick))
1313 addr.sin_family = AF_INET;
1315 if((p = strchr(args, ':')) == NULL)
1318 addr.sin_port = htons(atoi(p));
1319 if(!inet_aton(args, &addr.sin_addr))
1321 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1323 hubhandleaction(sk, fn, cmd, args);
1326 static void sendctm(struct socket *sk, char *nick)
1328 struct sockaddr *addr;
1333 if(sockgetremotename2(tcpsock, sk, &addr, &addrlen) < 0)
1335 if(addr->sa_family == AF_INET)
1336 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1338 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1342 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1348 if((p = strchr(args, ' ')) == NULL)
1351 if(strcmp(p, hub->nativenick))
1354 expectpeer(args, fn);
1355 hubhandleaction(sk, fn, cmd, args);
1358 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1361 struct fnetnode *node;
1366 for(node = fnetnodes; node != NULL; node = node->next)
1368 if(node->state == FNN_EST)
1371 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1372 hubhandleaction(sk, fn, cmd, args);
1375 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1382 if((p2 = strchr(p, ' ')) == NULL)
1386 if((p2 = strchr(p, ' ')) == NULL)
1389 if(strcmp(p, "From:"))
1392 if((p2 = strstr(p, " $")) == NULL)
1396 hubrecvchat(hub->sk, fn, p, p2);
1397 hubhandleaction(sk, fn, cmd, args);
1400 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1404 char *nick, *filename, *hubname;
1408 wchar_t *wnick, *wfile;
1412 if((p2 = strchr(p, ' ')) == NULL)
1417 if((p2 = strchr(p, 5)) == NULL)
1421 if((p2 = strchr(p, ' ')) == NULL)
1426 if((p2 = strchr(p, '/')) == NULL)
1431 if((p2 = strchr(p, 5)) == NULL)
1435 if((p2 = strstr(p, " (")) == NULL)
1438 if((wnick = icmbstowcs(nick, hub->charset)) == NULL)
1440 /* Use DCCHARSET in $Get paths until further researched... */
1441 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1446 sr = newsrchres(&dcnet, wfile, wnick);
1447 if(sr->peernick != NULL)
1449 sr->peernick = swcsdup(wnick);
1454 if(!strncmp(hubname, "TTH:", 4))
1456 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1459 sr->hash = newhash(L"TTH", 24, buf);
1463 getfnetnode(sr->fn = fn);
1466 hubhandleaction(sk, fn, cmd, args);
1469 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1471 /* Do nothing for now. */
1474 static void cmd_getpass(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1481 pw = wpfind(fn->args, L"password");
1482 if((pw == NULL) || ((mbspw = icwcstombs(pw, hub->charset)) == NULL))
1487 qstrf(sk, "$MyPass %s|", mbspw);
1489 fn->regstatus = FNNS_REG;
1490 hubhandleaction(sk, fn, cmd, args);
1493 static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1498 fn->regstatus = FNNS_OP;
1499 hubhandleaction(sk, fn, cmd, args);
1502 static void cmd_hubsupports(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1508 size_t arrsize, arrdata;
1511 if(hub->supports != NULL)
1513 for(i = 0; hub->supports[i] != NULL; i++)
1514 free(hub->supports[i]);
1515 free(hub->supports);
1518 arrsize = arrdata = 0;
1522 if((p2 = strchr(p, ' ')) != NULL)
1526 addtobuf(arr, sstrdup(p));
1527 } while((p = p2) != NULL);
1528 addtobuf(arr, NULL);
1529 hub->supports = arr;
1532 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1534 struct dcexppeer *expect;
1537 if(peer->nativename != NULL)
1538 free(peer->nativename);
1539 peer->nativename = sstrdup(args);
1540 if(peer->wcsname != NULL)
1541 free(peer->wcsname);
1542 if((peer->wcsname = icmbstowcs(peer->nativename, peer->charset)) == NULL)
1549 for(expect = expected; expect != NULL; expect = expect->next)
1551 if(!strcmp(expect->nick, args))
1558 hub = expect->fn->data;
1559 peer->fn = expect->fn;
1560 getfnetnode(peer->fn);
1561 peer->dcppemu = hub->dcppemu;
1562 freeexppeer(expect);
1567 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1571 struct transfer *transfer;
1573 if((p = strchr(args, ' ')) == NULL)
1576 if(!strcmp(args, "Upload"))
1578 if(!strcmp(args, "Download"))
1582 if((peer->transfer == NULL) || (mydir != peer->direction))
1587 if(peer->direction == TRNSD_DOWN)
1590 if(peer->wcsname == NULL)
1595 peer->direction = mydir;
1596 if(peer->direction == TRNSD_UP)
1598 if(confgetint("transfer", "ulquota") && hasupload(&dcnet, peer->wcsname))
1603 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1605 if((transfer = finddownload(peer->wcsname)) == NULL)
1610 transferattach(transfer, &dctransfer, peer);
1611 transfersetstate(transfer, TRNS_HS);
1613 transfersetnick(transfer, peer->wcsname);
1614 peer->transfer = transfer;
1617 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1618 if(peer->key != NULL)
1619 qstrf(sk, "$Key %s|", peer->key);
1620 if(peer->direction == TRNSD_DOWN)
1625 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1628 struct transfer *transfer;
1630 if((p = strchr(args, ' ')) == NULL)
1633 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1635 key = dcmakekey(args);
1638 if(peer->wcsname == NULL)
1647 if((transfer = finddownload(peer->wcsname)) == NULL)
1649 if(confgetint("transfer", "ulquota") && hasupload(&dcnet, peer->wcsname))
1654 peer->direction = TRNSD_UP;
1655 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1657 peer->direction = TRNSD_DOWN;
1658 transferattach(transfer, &dctransfer, peer);
1659 transfersetstate(transfer, TRNS_HS);
1661 transfersetnick(transfer, peer->wcsname);
1662 peer->transfer = transfer;
1663 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1664 qstrf(sk, "$Key %s|", key);
1667 if(peer->key != NULL)
1673 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1678 static void startdl(struct dcpeer *peer)
1680 if(peer->timeout != NULL)
1681 canceltimer(peer->timeout);
1682 peer->state = PEER_TRNS;
1683 transferstartdl(peer->transfer, peer->sk);
1684 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1685 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1688 static void startul(struct dcpeer *peer)
1690 if(peer->timeout != NULL)
1691 canceltimer(peer->timeout);
1692 peer->state = PEER_TRNS;
1693 transferstartul(peer->transfer, peer->sk);
1694 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1697 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1700 struct transfer *transfer;
1702 if(peer->transfer == NULL)
1708 if(peer->transfer->size != size)
1710 transfersetsize(peer->transfer, size);
1711 transfer = peer->transfer;
1713 trytransferbypeer(transfer->fnet, transfer->peerid);
1717 qstr(peer->sk, "$Send|");
1720 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1722 if(peer->fetchingtthl)
1724 peer->fetchingtthl = 0;
1729 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1731 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1732 resettransfer(peer->transfer);
1738 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1740 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1742 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1743 resettransfer(peer->transfer);
1756 {"MyList.DcLst", &hmlistname, updatehmlist},
1757 {"files.xml", &xmllistname, updatexmllist},
1758 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1762 static int openfilelist(char *name)
1767 for(i = 0; lists[i].name != NULL; i++)
1769 if(!strcmp(name, lists[i].name))
1773 if(lists[i].name == NULL)
1776 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1778 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1781 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1788 static struct sharecache *findbytth(char *tth32)
1792 struct sharecache *node;
1794 if((buf = base32decode(tth32, &buflen)) == NULL)
1801 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1803 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1810 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1812 struct sharecache *node;
1819 if((p2 = strchr(p, sep)) != NULL)
1821 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1828 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1833 struct sharecache *node;
1834 struct socket *lesk;
1838 if(peer->transfer == NULL)
1843 if((p = strchr(args, '$')) == NULL)
1849 if((offset = (atoi(p) - 1)) < 0)
1854 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1856 qstr(sk, "$Error Could not send file list|");
1859 } else if(fd >= 0) {
1860 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1861 transfersetpath(peer->transfer, buf2);
1862 peer->transfer->flags.b.minislot = 1;
1866 /* Use DCCHARSET in $Get paths until further researched... */
1867 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1869 qstrf(sk, "$Error File not in share|");
1873 if((fd = opensharecache(node)) < 0)
1875 qstrf(sk, "$Error %s|", strerror(errno));
1879 buf = getfspath(node);
1880 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1881 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1883 transfersetpath(peer->transfer, buf2);
1886 if(fstat(fd, &sb) < 0)
1889 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1890 qstrf(sk, "$Error|");
1894 if(sb.st_size < 65536)
1895 peer->transfer->flags.b.minislot = 1;
1896 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1898 qstr(sk, "$MaxedOut|");
1902 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1905 qstrf(sk, "$Error Offset out of range|");
1909 lesk = wrapsock(fd);
1910 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1912 qstrf(sk, "$FileLength %zi|", peer->transfer->size);
1915 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1917 if(peer->transfer == NULL)
1922 if(peer->transfer->localend == NULL)
1931 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1936 size_t arrsize, arrdata;
1938 if(peer->supports != NULL)
1940 for(i = 0; peer->supports[i] != NULL; i++)
1941 free(peer->supports[i]);
1942 free(peer->supports);
1945 arrsize = arrdata = 0;
1949 if((p2 = strchr(p, ' ')) != NULL)
1953 addtobuf(arr, sstrdup(p));
1954 } while((p = p2) != NULL);
1955 addtobuf(arr, NULL);
1956 peer->supports = arr;
1959 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1963 int start, numbytes;
1964 char *charset, *buf;
1966 struct sharecache *node;
1968 struct socket *lesk;
1970 if(peer->transfer == NULL)
1976 if((p2 = strchr(p, ' ')) == NULL)
1984 if((p2 = strchr(p, ' ')) == NULL)
1992 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1995 /* Use DCCHARSET in $Get paths until further researched... */
1996 charset = DCCHARSET;
1997 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1998 initcompress(peer, CPRS_ZLIB);
1999 if(((fd = openfilelist(p)) < 0) && (errno != 0))
2001 qstr(sk, "$Error Could not send file list|");
2003 } else if(fd >= 0) {
2004 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
2005 transfersetpath(peer->transfer, buf2);
2006 peer->transfer->flags.b.minislot = 1;
2010 if((node = resdcpath(p, charset, '\\')) == NULL)
2012 qstr(sk, "$Error File not in cache|");
2015 if((fd = opensharecache(node)) < 0)
2017 qstrf(sk, "$Error %s|", strerror(errno));
2020 buf = getfspath(node);
2021 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
2022 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2024 transfersetpath(peer->transfer, buf2);
2027 if(fstat(fd, &sb) < 0)
2030 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2031 qstr(sk, "$Error|");
2034 if(sb.st_size < 65536)
2035 peer->transfer->flags.b.minislot = 1;
2036 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
2038 qstr(sk, "$MaxedOut|");
2041 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2044 qstr(sk, "$Error Offset out of range|");
2047 if((numbytes < 0) || (start + numbytes > sb.st_size))
2048 numbytes = sb.st_size - start;
2049 lesk = wrapsock(fd);
2050 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2052 qstrf(sk, "$Sending %i|", numbytes);
2056 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2060 int start, numbytes;
2061 struct sharecache *node;
2063 struct socket *lesk;
2067 if(peer->transfer == NULL)
2072 if((argv = parseadc(args)) == NULL)
2077 if(parrlen(argv) < 4)
2082 start = atoi(argv[2]);
2083 numbytes = atoi(argv[3]);
2086 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
2088 qstr(sk, "$Error Could not send file list|");
2090 } else if(fd >= 0) {
2091 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
2092 transfersetpath(peer->transfer, wbuf);
2093 peer->transfer->flags.b.minislot = 1;
2097 if(!strncmp(argv[1], "TTH/", 4))
2099 if((node = findbytth(argv[1] + 4)) == NULL)
2101 qstr(sk, "$Error File not in cache|");
2105 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
2107 qstr(sk, "$Error File not in cache|");
2111 if((fd = opensharecache(node)) < 0)
2113 qstrf(sk, "$Error %s|", strerror(errno));
2116 buf = getfspath(node);
2117 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
2118 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2120 transfersetpath(peer->transfer, wbuf);
2123 if(!strcmp(argv[0], "file"))
2125 for(i = 4; argv[i] != NULL; i++)
2127 if(!strcmp(argv[i], "ZL1"))
2128 initcompress(peer, CPRS_ZLIB);
2130 if(fstat(fd, &sb) < 0)
2132 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2133 qstr(sk, "$Error|");
2136 if(sb.st_size < 65536)
2137 peer->transfer->flags.b.minislot = 1;
2138 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
2139 qstr(sk, "$MaxedOut|");
2142 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2144 qstr(sk, "$Error Offset out of range|");
2147 if((numbytes < 0) || (start + numbytes > sb.st_size))
2148 numbytes = sb.st_size - start;
2149 lesk = wrapsock(fd);
2150 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2153 qstr(sk, "$ADCSND");
2154 sendadc(sk, "file");
2155 sendadc(sk, argv[1]);
2156 sendadcf(sk, "%i", start);
2157 sendadcf(sk, "%i", numbytes);
2158 if(peer->compress == CPRS_ZLIB)
2162 } else if(!strcmp(argv[0], "tthl")) {
2164 * XXX: Implement full TTHL support.
2166 * In the meantime, this is better than nothing, since it at
2167 * least allows fetching of the TTH root if it isn't already
2172 qstr(sk, "$Error no TTHL data for virtual files|");
2175 qstr(sk, "$ADCSND");
2176 sendadc(sk, "tthl");
2177 sendadc(sk, argv[1]);
2181 sockqueue(sk, node->hashtth, 24);
2183 qstr(sk, "$Error Namespace not implemented|");
2193 static void handletthl(struct dcpeer *peer)
2197 while(peer->inbufdata >= 24)
2199 pushtigertree(&peer->tth, peer->inbuf);
2200 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2201 peer->curread += 24;
2203 if(peer->curread >= peer->totalsize)
2205 if(peer->timeout == NULL)
2206 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2207 peer->state = PEER_CMD;
2208 synctigertree(&peer->tth);
2209 restigertree(&peer->tth, buf);
2210 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2215 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2218 int start, numbytes;
2220 if(peer->transfer == NULL)
2225 if((argv = parseadc(args)) == NULL)
2230 if(parrlen(argv) < 4)
2235 start = atoi(argv[2]);
2236 numbytes = atoi(argv[3]);
2237 if(!strcmp(argv[0], "tthl"))
2239 if((start != 0) || (numbytes % 24 != 0))
2241 /* Weird. Bail out. */
2245 if(peer->timeout != NULL)
2246 canceltimer(peer->timeout);
2247 peer->state = PEER_TTHL;
2248 peer->totalsize = numbytes;
2250 peer->fetchingtthl = 0;
2251 inittigertree(&peer->tth);
2253 } else if(!strcmp(argv[0], "file")) {
2254 if(start != peer->transfer->curpos)
2259 if(start + numbytes != peer->transfer->size)
2261 transfersetsize(peer->transfer, start + numbytes);
2266 if(peer->inbufdata > 0)
2268 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2269 peer->inbufdata = 0;
2270 transread(sk, peer);
2273 /* We certainly didn't request this...*/
2282 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2286 if(peer->transfer == NULL)
2291 numbytes = atoi(args);
2292 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2294 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2299 if(peer->inbufdata > 0)
2301 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2302 peer->inbufdata = 0;
2303 transread(sk, peer);
2309 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2314 hubhandleaction(sk, fn, cmd, args);
2318 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2324 static int hubreqconn(struct fnetpeer *peer)
2329 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2334 if((hub = peer->fn->data) == NULL)
2339 if((mbsnick = icwcstombs(peer->id, hub->charset)) == NULL)
2340 return(1); /* Shouldn't happen, of course, but who knows... */
2343 sendctm(hub->sk, mbsnick);
2344 expectpeer(mbsnick, peer->fn);
2346 qstrf(hub->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2352 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2355 char *mbsstring, *mbsto;
2358 if((mbsto = icwcstombs(to, hub->charset)) == NULL)
2363 if((mbsstring = icwcstombs(string, hub->charset)) == NULL)
2368 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2375 if(strchr(mbsstring, '|') != NULL)
2386 qstrf(hub->sk, "<%s> %s|", hub->nativenick, mbsstring);
2388 qstrf(hub->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2391 qstrf(hub->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2398 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2400 int minl, maxl, minr, maxr, retmin, retmax;
2405 findsizelimit(sexpr->l, &minl, &maxl);
2406 findsizelimit(sexpr->r, &minr, &maxr);
2407 retmin = (minl > minr)?minl:minr;
2408 if((maxl != -1) && (maxr != -1))
2409 retmax = (maxl < maxr)?maxl:maxr;
2418 findsizelimit(sexpr->l, &minl, &maxl);
2419 findsizelimit(sexpr->r, &minr, &maxr);
2420 retmin = (minl < minr)?minl:minr;
2421 if((maxl == -1) || (maxr == -1))
2424 retmax = (maxl > maxr)?maxl:maxr;
2427 findsizelimit(sexpr->l, &minl, &maxl);
2428 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2432 } else if((minl == 0) && (maxl != -1)) {
2435 } else if((minl != 0) && (maxl == -1)) {
2438 } else { /* This would yield two seperate intervals, which DC cannot handle */
2444 retmax = sexpr->d.n - 1;
2447 retmin = sexpr->d.n;
2448 retmax = sexpr->d.n;
2451 retmin = sexpr->d.n + 1;
2465 static struct hash *findsehash(struct sexpr *sexpr)
2467 struct hash *h1, *h2;
2472 if((h1 = findsehash(sexpr->l)) != NULL)
2474 if((h1 = findsehash(sexpr->r)) != NULL)
2478 if((h1 = findsehash(sexpr->l)) == NULL)
2480 if((h2 = findsehash(sexpr->r)) == NULL)
2486 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2487 return(sexpr->d.hash);
2494 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2497 struct wcslist *list, *cur;
2498 char *sstr, *buf, *p;
2499 size_t sstrsize, sstrdata;
2500 struct sockaddr *name;
2502 int minsize, maxsize;
2506 if((fn->state != FNN_EST) || (hub->sk == NULL) || (hub->sk->state != SOCK_EST))
2508 list = findsexprstrs(srch->sexpr);
2509 findsizelimit(srch->sexpr, &minsize, &maxsize);
2510 hash = findsehash(srch->sexpr);
2511 if((minsize != 0) && (maxsize != -1))
2513 /* Choose either minsize or maxsize by trying to determine
2514 * which choice will be most restrictive. The result can be
2515 * approximative at best anyway, so don't try too hard... */
2516 if((50000000 - maxsize) > (minsize - 50000000))
2522 sstrsize = sstrdata = 0;
2523 if((hash != NULL) && (hash->len == 24))
2525 /* Prioritize hash searches above all else */
2526 bufcat(sstr, "F?T?0?9?TTH:", 12);
2527 buf = base32encode(hash->buf, hash->len);
2529 bufcat(sstr, buf, 39);
2534 sizebuf2(sstr, sstrdata + 32, 1);
2535 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2536 } else if(maxsize != -1) {
2537 sizebuf2(sstr, sstrdata + 32, 1);
2538 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2540 bufcat(sstr, "F?F?0?1?", 8);
2544 for(cur = list; cur != NULL; cur = cur->next)
2546 /* Use DCCHARSET in $Get paths until further researched... */
2547 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2549 /* Can't find anything anyway if the search expression
2550 * requires characters outside DC's charset. There's
2551 * nothing technically wrong with the search itself,
2552 * however, so return success. This should be
2553 * considered as an optimization. */
2560 addtobuf(sstr, '$');
2562 * It probably doesn't hurt if buf contains any extra
2563 * dollar signs - it will just result in extra search
2564 * terms, and the extraneous results will be filtered by
2565 * the search layer anyway. It hurts if it contains any
2566 * pipes, though, so let's sell them for money.
2568 for(p = buf; *p; p++)
2573 bufcat(sstr, buf, strlen(buf));
2577 /* Will match all files... :-/ */
2578 addtobuf(sstr, '.');
2584 if(sockgetremotename2(udpsock, hub->sk, &name, &namelen) < 0)
2586 flog(LOG_WARNING, "cannot get address of UDP socket");
2588 qstrf(hub->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2592 qstrf(hub->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2596 fn->lastsrch = time(NULL);
2604 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2605 static struct command hubcmds[] =
2607 {"$Lock", cc(cmd_lock)},
2608 {"$HubName", cc(cmd_hubname)},
2609 {"$Hello", cc(cmd_hello)},
2610 {"$Quit", cc(cmd_quit)},
2611 {"$NickList", cc(cmd_nicklist)},
2612 {"$OpList", cc(cmd_oplist)},
2613 {"$MyINFO", cc(cmd_myinfo)},
2614 {"$ForceMove", cc(cmd_forcemove)},
2615 {"$Search", cc(cmd_search)},
2616 {"$MultiSearch", cc(cmd_search)},
2617 {"$ConnectToMe", cc(cmd_connecttome)},
2618 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2619 {"$GetNetInfo", cc(cmd_getnetinfo)},
2620 {"$To:", cc(cmd_to)},
2621 {"$SR", cc(cmd_sr)},
2622 {"$UserCommand", cc(cmd_usercommand)},
2623 {"$GetPass", cc(cmd_getpass)},
2624 {"$LogedIn", cc(cmd_logedin)}, /* sic */
2625 {"$Supports", cc(cmd_hubsupports)},
2629 static struct command peercmds[] =
2631 {"$MyNick", cc(cmd_mynick)},
2632 {"$Lock", cc(cmd_peerlock)},
2633 {"$Direction", cc(cmd_direction)},
2634 {"$Key", cc(cmd_key)},
2635 {"$FileLength", cc(cmd_filelength)},
2636 {"$Error", cc(cmd_error)},
2637 {"$MaxedOut", cc(cmd_maxedout)},
2638 {"$Get", cc(cmd_get)},
2639 {"$Send", cc(cmd_send)},
2640 {"$Supports", cc(cmd_supports)},
2641 {"$GetBlock", cc(cmd_getblock)},
2642 {"$UGetBlock", cc(cmd_getblock)},
2643 {"$GetZBlock", cc(cmd_getblock)},
2644 {"$UGetZBlock", cc(cmd_getblock)},
2645 {"$ADCGET", cc(cmd_adcget)},
2646 {"$ADCSND", cc(cmd_adcsnd), 1},
2647 {"$Sending", cc(cmd_sending), 1},
2652 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2654 CBUNREG(transfer, trans_filterout, peer);
2657 peer->transfer = NULL;
2661 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2665 unsigned char outbuf[1024];
2669 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2671 if(sockqueuesize(peer->sk) < 65536)
2673 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2675 if(peer->compress == CPRS_NONE)
2677 sockqueue(peer->sk, buf, bufsize);
2678 } else if(peer->compress == CPRS_ZLIB) {
2679 cstr = peer->cprsdata;
2680 cstr->next_in = buf;
2681 cstr->avail_in = bufsize;
2682 while(cstr->avail_in > 0)
2684 cstr->next_out = outbuf;
2685 cstr->avail_out = sizeof(outbuf);
2686 if((ret = deflate(cstr, 0)) != Z_OK)
2688 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2692 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2697 if(peer->state == PEER_SYNC)
2699 if(peer->compress == CPRS_ZLIB)
2701 cstr = peer->cprsdata;
2702 cstr->next_in = NULL;
2706 cstr->next_out = outbuf;
2707 cstr->avail_out = sizeof(outbuf);
2708 ret = deflate(cstr, Z_FINISH);
2709 if((ret != Z_OK) && (ret != Z_STREAM_END))
2711 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2715 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2716 } while(ret != Z_STREAM_END);
2722 if(peer->timeout == NULL)
2723 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2724 peer->state = PEER_CMD;
2726 transfersetstate(transfer, TRNS_HS);
2727 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2728 transfer->flags.b.minislot = 0;
2729 peer->sk->writecb = NULL;
2736 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2738 peer->state = PEER_SYNC;
2739 dctransgotdata(transfer, peer);
2742 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2744 if(transferdatasize(transfer) < 65536)
2745 peer->sk->ignread = 0;
2748 static void transread(struct socket *sk, struct dcpeer *peer)
2752 struct transfer *transfer;
2754 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2756 if(peer->transfer == NULL)
2762 transferputdata(peer->transfer, buf, bufsize);
2764 if(peer->transfer->curpos >= peer->transfer->size)
2766 transfer = peer->transfer;
2767 transferdetach(transfer);
2768 transferendofdata(transfer);
2771 if(transferdatasize(peer->transfer) > 65535)
2775 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2777 struct transfer *transfer;
2779 if((transfer = peer->transfer) == NULL)
2784 transferdetach(transfer);
2785 transferendofdata(transfer);
2788 static void transwrite(struct socket *sk, struct dcpeer *peer)
2790 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2792 if(peer->transfer == NULL)
2797 dctransgotdata(peer->transfer, peer);
2800 static void udpread(struct socket *sk, void *data)
2802 char *buf, *p, *p2, *hashbuf;
2803 size_t buflen, hashlen;
2804 char *nick, *filename, *hubname;
2805 struct sockaddr_in hubaddr;
2807 struct fnetnode *fn, *myfn;
2810 wchar_t *wnick, *wfile;
2813 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2815 buf = srealloc(buf, buflen + 1);
2817 if(!strncmp(buf, "$SR ", 4))
2821 if((p2 = strchr(p, ' ')) == NULL)
2829 if((p2 = strchr(p, 5)) == NULL)
2836 if((p2 = strchr(p, ' ')) == NULL)
2844 if((p2 = strchr(p, '/')) == NULL)
2852 if((p2 = strchr(p, 5)) == NULL)
2859 if((p2 = strstr(p, " (")) == NULL)
2866 if((p2 = strchr(p, ':')) == NULL)
2872 hubaddr.sin_family = AF_INET;
2873 if(!inet_aton(p, &hubaddr.sin_addr))
2879 if((p2 = strchr(p, ')')) == NULL)
2885 hubaddr.sin_port = htons(atoi(p));
2886 /* Use DCCHARSET in $Get paths until further researched... */
2887 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2894 if(!strncmp(hubname, "TTH:", 4))
2896 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2899 hash = newhash(L"TTH", 24, hashbuf);
2903 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2905 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2907 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2922 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2924 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2926 if((hub->sk != NULL) && addreq(hub->sk->remote, (struct sockaddr *)&hubaddr))
2937 if((wnick = icmbstowcs(nick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
2942 sr = newsrchres(&dcnet, wfile, wnick);
2943 if(sr->peernick != NULL)
2945 sr->peernick = swcsdup(wnick);
2951 getfnetnode(sr->fn = myfn);
2960 static void hubread(struct socket *sk, struct fnetnode *fn)
2967 hub = (struct dchub *)fn->data;
2968 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2970 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2972 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2973 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2975 p = hub->inbuf + hub->inbufdata;
2976 hub->inbufdata += datalen;
2977 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2980 newqcmd(&hub->queue, hub->inbuf);
2981 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2982 datalen = hub->inbufdata;
2987 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2992 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2998 if((buf = icwcstombs(newnick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
3000 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
3005 if(hub == NULL) /* Not yet connected */
3010 if(hub->nativenick != NULL)
3011 free(hub->nativenick);
3012 hub->nativenick = buf;
3016 static struct dchub *newdchub(struct fnetnode *fn)
3023 new = smalloc(sizeof(*new));
3024 memset(new, 0, sizeof(*new));
3026 if(confgetint("dc", "dcppemu"))
3028 if((emu = wpfind(fn->args, L"dcppemu")) != NULL)
3036 if((wcharset = wpfind(fn->args, L"charset")) != NULL)
3038 if((charset = icwcstombs(wcharset, "US-ASCII")) != NULL)
3040 if(!havecharset(charset))
3048 new->charset = charset;
3050 new->charset = sstrdup(DCCHARSET);
3051 if(hubsetnick(fn, fn->mynick))
3052 fnetsetnick(fn, L"DoldaConnectUser-IN");
3053 /* IN as in Invalid Nick */
3057 static struct dcpeer *newdcpeer(struct socket *sk)
3061 new = smalloc(sizeof(*new));
3062 memset(new, 0, sizeof(*new));
3063 new->transfer = NULL;
3066 if(confgetint("dc", "dcppemu"))
3077 static void freedcpeer(struct dcpeer *peer)
3080 struct qcommand *qcmd;
3085 if(peer->next != NULL)
3086 peer->next->prev = peer->prev;
3087 if(peer->prev != NULL)
3088 peer->prev->next = peer->next;
3089 if(peer->transfer != NULL)
3091 if(peer->transfer->dir == TRNSD_UP)
3092 peer->transfer->close = 1;
3093 if(peer->transfer->dir == TRNSD_DOWN)
3094 resettransfer(peer->transfer);
3095 transferdetach(peer->transfer);
3097 if(peer->timeout != NULL)
3098 canceltimer(peer->timeout);
3099 if(peer->sk->data == peer)
3100 peer->sk->data = NULL;
3101 peer->sk->readcb = NULL;
3102 peer->sk->writecb = NULL;
3103 peer->sk->errcb = NULL;
3106 if(peer->supports != NULL)
3108 for(i = 0; peer->supports[i] != NULL; i++)
3109 free(peer->supports[i]);
3110 free(peer->supports);
3112 if(peer->inbuf != NULL)
3114 if(peer->key != NULL)
3116 if(peer->wcsname != NULL)
3117 free(peer->wcsname);
3118 if(peer->nativename != NULL)
3119 free(peer->nativename);
3120 if(peer->charset != NULL)
3121 free(peer->charset);
3122 if(peer->fn != NULL)
3123 putfnetnode(peer->fn);
3124 while((qcmd = ulqcmd(&peer->queue)) != NULL)
3130 static void hubconnect(struct fnetnode *fn, struct socket *sk)
3134 sk->readcb = (void (*)(struct socket *, void *))hubread;
3135 sk->errcb = (void (*)(struct socket *, int, void *))huberr;
3136 fn->data = hub = newdchub(fn);
3138 getsock(hub->sk = sk);
3142 static void hubdestroy(struct fnetnode *fn)
3146 struct qcommand *qcmd;
3148 hub = (struct dchub *)fn->data;
3150 while((qcmd = ulqcmd(&hub->queue)) != NULL)
3152 if(hub->supports != NULL)
3154 for(i = 0; hub->supports[i] != NULL; i++)
3155 free(hub->supports[i]);
3156 free(hub->supports);
3158 if(hub->nativename != NULL)
3159 free(hub->nativename);
3160 if(hub->nativenick != NULL)
3161 free(hub->nativenick);
3162 if(hub->charset != NULL)
3164 if(hub->inbuf != NULL)
3169 static void hubkill(struct fnetnode *fn)
3173 hub = (struct dchub *)fn->data;
3177 static wchar_t *dcbasename(wchar_t *filename)
3181 if((ret = wcsrchr(filename, L'\\')) != NULL)
3186 static struct transferiface dctransfer =
3188 .detach = (void (*)(struct transfer *, void *))dctransdetach,
3189 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
3190 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
3191 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
3194 static struct fnet dcnet =
3197 .connect = hubconnect,
3198 .destroy = hubdestroy,
3200 .setnick = hubsetnick,
3201 .reqconn = hubreqconn,
3202 .sendchat = hubsendchat,
3203 .search = hubsearch,
3204 .filebasename = dcbasename
3207 static void peerread(struct socket *sk, struct dcpeer *peer)
3211 struct command *cmd;
3213 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
3215 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
3216 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
3218 peer->inbufdata += datalen;
3219 if(peer->state == PEER_CMD)
3222 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
3225 newqcmd(&peer->queue, peer->inbuf);
3226 for(cmd = peercmds; cmd->handler != NULL; cmd++)
3228 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
3231 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
3234 peer->state = PEER_STOP;
3238 } else if(peer->state == PEER_TTHL) {
3243 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3248 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3250 struct dcpeer *peer;
3260 peer = newdcpeer(sk);
3263 peer->dcppemu = hub->dcppemu;
3264 sk->readcb = (void (*)(struct socket *, void *))peerread;
3265 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3267 socksettos(sk, confgetint("fnet", "fnptos"));
3269 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3274 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3276 struct dcpeer *peer;
3278 peer = newdcpeer(newsk);
3280 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3281 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3283 socksettos(newsk, confgetint("fnet", "fnptos"));
3284 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3287 static void updatehmlist(void)
3289 int i, lev, ic, ret;
3290 struct sharecache *node;
3291 char *buf, *buf2, numbuf[32];
3292 size_t bufsize, bufdata;
3296 buf = smalloc(bufsize = 65536);
3297 node = shareroot->child;
3302 /* Use DCCHARSET in $Get paths until further researched... */
3303 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3305 for(i = 0; i < lev; i++)
3307 bufcat(buf, buf2, strlen(buf2));
3309 if(node->f.b.type == FILE_REG)
3312 sprintf(numbuf, "%zi", node->size);
3313 bufcat(buf, numbuf, strlen(numbuf));
3320 if((node->child != NULL) && !ic)
3324 } else if(node->next != NULL) {
3327 while(node->next == NULL)
3330 node = node->parent;
3331 if(node == shareroot)
3334 if(node == shareroot)
3339 if(hmlistname != NULL)
3344 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3345 if((fd = mkstemp(hmlistname)) < 0)
3347 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3352 * I do not want to implement a good Huffman encoder, and it's not
3353 * like Huffman encoding actually yields any impressive results
3354 * for DC file lists anyway, so I'll just output a bogus
3355 * tree. Implement a good encoder if you want to.
3357 write(fd, "HE3\r\0", 5);
3358 write(fd, &bufdata, 4);
3360 write(fd, &ibuf, 2);
3362 for(i = 0; i < 256; i++)
3365 write(fd, &ibuf, 1);
3367 for(i = 0; i < 256; i++)
3369 for(buf2 = buf; bufdata > 0;)
3371 if((ret = write(fd, buf2, bufdata)) < 0)
3373 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3384 static struct xmlent
3391 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3395 static wchar_t *escapexml(wchar_t *src)
3397 static wchar_t *buf = NULL;;
3400 size_t bufsize, bufdata;
3406 bufsize = bufdata = 0;
3407 for(; *src != L'\0'; src++)
3410 if((c > 0) && (c < 32))
3412 bufcat(buf, L"&#", 2);
3413 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3414 bufcat(buf, nbuf, ret);
3415 addtobuf(buf, L';');
3417 for(ent = entities; ent->ent != NULL; ent++)
3421 bufcat(buf, ent->ent, wcslen(ent->ent));
3425 if(ent->ent == NULL)
3426 addtobuf(buf, *src);
3429 addtobuf(buf, L'\0');
3433 static void updatexmllist(void)
3437 char cidbuf[14], *namebuf;
3439 struct sharecache *node;
3441 if(xmllistname != NULL)
3443 unlink(xmllistname);
3446 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3447 if((fd = mkstemp(xmllistname)) < 0)
3449 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3454 if((fs = fdopen(fd, "w")) == NULL)
3456 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3457 unlink(xmllistname);
3463 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3464 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3465 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3467 if(confgetint("dc", "dcppemu"))
3468 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"DC++ 0.674\">\r\n", cidbuf);
3470 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, "DoldaConnect" VERSION);
3472 node = shareroot->child;
3476 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3478 for(i = 0; i < lev; i++)
3480 if(node->child != NULL)
3482 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3487 fprintf(fs, "<File Name=\"%s\" Size=\"%zi\"", namebuf, node->size);
3488 if(node->f.b.hastth)
3490 hashbuf = base32encode(node->hashtth, 24);
3491 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3494 fprintf(fs, "/>\r\n");
3496 while(node->next == NULL)
3498 node = node->parent;
3499 if(node == shareroot)
3504 for(i = 0; i < lev; i++)
3506 fprintf(fs, "</Directory>\r\n");
3509 if(node == shareroot)
3515 if(confgetint("dc", "dcppemu"))
3516 fprintf(fs, "</FileListing>");
3518 fprintf(fs, "</FileListing>\r\n");
3522 static void updatexmlbz2list(void)
3531 if(xmllistname == NULL)
3533 if((in = fopen(xmllistname, "r")) == NULL)
3535 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3538 if(xmlbz2listname != NULL)
3540 unlink(xmlbz2listname);
3541 free(xmlbz2listname);
3543 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3544 if((fd = mkstemp(xmlbz2listname)) < 0)
3546 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3547 free(xmlbz2listname);
3548 xmlbz2listname = NULL;
3552 if((real = fdopen(fd, "w")) == NULL)
3554 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3556 unlink(xmlbz2listname);
3557 free(xmlbz2listname);
3558 xmlbz2listname = NULL;
3562 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3565 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3567 unlink(xmlbz2listname);
3568 free(xmlbz2listname);
3569 xmlbz2listname = NULL;
3575 bufdata = fread(buf, 1, sizeof(buf), in);
3576 BZ2_bzWrite(&err, out, buf, bufdata);
3579 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3583 static void listtimercb(int cancelled, void *uudata)
3585 listwritetimer = NULL;
3590 static void updatelists(int now)
3592 if((hmlistname == NULL) || (xmllistname == NULL) || (xmlbz2listname == NULL))
3596 if(listwritetimer == NULL)
3597 listwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), listtimercb, NULL);
3600 if(listwritetimer != NULL)
3601 canceltimer(listwritetimer);
3607 static int shareupdate(unsigned long long uusharesize, void *data)
3613 static char *quotestr(char *str)
3617 size_t bufsize, bufdata;
3619 static char *enc = NULL;
3620 size_t encsize, encdata;
3623 bufsize = bufdata = 0;
3624 for(p = (unsigned char *)str; *p; p++)
3627 bufcat(buf, "\\b", 2);
3629 bufcat(buf, "\\t", 2);
3631 bufcat(buf, "\\n", 2);
3633 bufcat(buf, "\\r", 2);
3635 bufcat(buf, "\\\\", 2);
3639 bprintf(buf, "\\x%02x", *p);
3645 if((wbuf = icmbstowcs((char *)buf, DCCHARSET)) != NULL)
3647 enc = icwcstombs(wbuf, NULL);
3652 encsize = encdata = 0;
3653 for(p = buf; *p; p++) {
3657 bprintf(buf, "\\x%x", *p);
3664 static void logunimpl(char *list, char *cmd, char *args)
3668 if((log = fopen("/tmp/dc-unimpl", "a")) == NULL)
3670 flog(LOG_WARNING, "could not open unimpl log: %s", strerror(errno));
3675 fputs(quotestr(cmd), log);
3679 fputs(quotestr(args), log);
3685 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3688 struct command *cmd;
3690 if((p = strchr(qcmd->string, ' ')) != NULL)
3692 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3694 if(!strcmp(cmd->name, qcmd->string))
3697 if(cmd->handler != NULL)
3699 cmd->handler(sk, data, qcmd->string, p);
3700 } else if(confgetint("dc", "logunimpl")) {
3701 if(cmdlist == hubcmds)
3702 logunimpl("hub", qcmd->string, p);
3703 else if(cmdlist == peercmds)
3704 logunimpl("peer", qcmd->string, p);
3706 logunimpl("other?!", qcmd->string, p);
3710 static int run(void)
3712 struct fnetnode *fn, *nextfn;
3714 struct dcpeer *peer, *nextpeer;
3715 struct qcommand *qcmd;
3719 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3722 if(fn->fnet != &dcnet)
3724 if(fn->data == NULL)
3726 hub = (struct dchub *)fn->data;
3727 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3729 if(*qcmd->string == '$')
3731 if((hub->sk != NULL) && (hub->sk->state == SOCK_EST))
3732 dispatchcommand(qcmd, hubcmds, hub->sk, fn);
3733 } else if(*qcmd->string != 0) {
3734 hubrecvchat(hub->sk, fn, NULL, qcmd->string);
3741 for(peer = peers; peer != NULL; peer = nextpeer)
3743 nextpeer = peer->next;
3744 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3746 if(peer->timeout != NULL)
3747 canceltimer(peer->timeout);
3748 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3749 if(*qcmd->string == '$')
3750 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3759 static void preinit(int hup)
3766 static int updateudpport(struct configvar *var, void *uudata)
3768 struct sockaddr_in addr;
3769 struct socket *newsock;
3771 memset(&addr, 0, sizeof(addr));
3772 addr.sin_family = AF_INET;
3773 addr.sin_port = htons(var->val.num);
3774 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3776 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3779 newsock->readcb = udpread;
3786 static int updatetcpport(struct configvar *var, void *uudata)
3788 struct sockaddr_in addr;
3789 struct socket *newsock;
3791 memset(&addr, 0, sizeof(addr));
3792 addr.sin_family = AF_INET;
3793 addr.sin_port = htons(var->val.num);
3794 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3795 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3802 static int init(int hup)
3804 struct sockaddr_in addr;
3808 GCBREG(sharechangecb, shareupdate, NULL);
3813 addr.sin_family = AF_INET;
3814 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3815 addr.sin_port = htons(confgetint("dc", "udpport"));
3816 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3818 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3821 udpsock->readcb = udpread;
3822 addr.sin_port = htons(confgetint("dc", "tcpport"));
3823 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3824 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3825 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3826 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3827 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3832 static void terminate(void)
3834 if(hmlistname != NULL)
3839 if(xmllistname != NULL)
3841 unlink(xmllistname);
3844 if(xmlbz2listname != NULL)
3846 unlink(xmlbz2listname);
3847 free(xmlbz2listname);
3851 static struct configvar myvars[] =
3853 /** Specifies the share description reported to other DC users. */
3854 {CONF_VAR_STRING, "desc", {.str = L""}},
3855 /** Specifies the speed reported to other DC users. Normal values
3856 * are 28.8Kbps, 33.6Kbps, 56Kbps, Satellite, ISDN, DSL, Cable,
3857 * LAN(T1) or LAN(T3)*/
3858 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3859 /** The e-mail address to report to other DC users. */
3860 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3861 /** Specifies a specific UDP port to use for DC search results. If
3862 * left unspecified, a port is allocated dynamically. Useful for
3863 * NAT routers (see also the net.visibleipv4 address for those
3865 {CONF_VAR_INT, "udpport", {.num = 0}},
3866 /** Specifies a specific TCP port to use for DC peer
3867 * connections. If left unspecified, a port is allocated
3868 * dynamically. Useful for NAT routers (see also the
3869 * net.visibleipv4 address for those cases). */
3870 {CONF_VAR_INT, "tcpport", {.num = 0}},
3871 /** If set to true, doldacond will do its best to emulate DC++
3872 * (currently v0.674). This should be left off if at all possible,
3873 * since turning it on will violate the rules of most hubs and
3874 * thus give hub owners an actual reason to kick you if it is
3875 * detected. It might be needed for some of the more bone-headed
3876 * hub owners, though. Note that DC++ emulation can also be turned
3877 * on or off for individual hubs, overriding this setting. */
3878 {CONF_VAR_BOOL, "dcppemu", {.num = 0}},
3879 /** Use for debugging. If set to true, doldacond will log all
3880 * unknown commands it receives, and their arguments, to
3881 * /tmp/dc-unimpl. */
3882 {CONF_VAR_BOOL, "logunimpl", {.num = 0}},
3886 static struct module me =
3895 .terminate = terminate,