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;
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;
162 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn);
163 static void freedcpeer(struct dcpeer *peer);
164 static void transread(struct socket *sk, struct dcpeer *peer);
165 static void transerr(struct socket *sk, int err, struct dcpeer *peer);
166 static void transwrite(struct socket *sk, struct dcpeer *peer);
167 static void updatehmlist(void);
168 static void updatexmllist(void);
169 static void updatexmlbz2list(void);
170 static void requestfile(struct dcpeer *peer);
172 static int reservedchar(unsigned char c)
174 return((c == 0) || (c == 5) || (c == 124) || (c == 96) || (c == 126) || (c == 36));
177 /* Oh, how I despise having to do this... */
178 static char *dcmakekey(char *lock)
184 buf = smalloc(strlen(lock));
187 for(i = 0; lock[i]; i++)
189 buf[i] = lock[i] ^ save;
190 buf[i] = ((buf[i] & 0x0F) << 4) | ((buf[i] & 0xF0) >> 4);
192 if((i != 0) && reservedchar(buf[i]))
197 buf[0] ^= buf[i - 1];
198 if(reservedchar(buf[0]))
202 key = smalloc(len + 1);
204 for(i = 0; lock[i] != 0; i++)
206 if(reservedchar(buf[i]))
207 offset += sprintf(key + offset, "/%%DCN%03i%%/", buf[i]);
209 key[offset++] = buf[i];
216 static char *pathnmdc2adc(char *path)
219 size_t retsize, retdata;
221 if(!strcmp(path, "files.xml") || !strcmp(path, "files.xml.bz2") || !strcmp(path, "MyList.DcLst"))
222 return(sstrdup(path));
224 retsize = retdata = 0;
231 addtobuf(ret, *path);
237 static int isdchash(struct hash *hash)
239 if(wcscmp(hash->algo, L"TTH"))
246 static int supports(struct dcpeer *peer, char *cap)
250 if(peer->supports == NULL)
252 for(p = peer->supports; *p != NULL; p++)
254 if(!strcasecmp(*p, cap))
260 static void endcompress(struct dcpeer *peer)
262 if(peer->compress == CPRS_ZLIB)
264 deflateEnd(peer->cprsdata);
265 free(peer->cprsdata);
267 peer->compress = CPRS_NONE;
270 static void initcompress(struct dcpeer *peer, int algo)
275 peer->compress = algo;
276 if(algo == CPRS_ZLIB)
278 peer->cprsdata = smalloc(sizeof(z_stream));
279 memset(peer->cprsdata, 0, sizeof(z_stream));
280 if((ret = deflateInit(peer->cprsdata, 3)) != Z_OK)
282 flog(LOG_CRIT, "Aiya! zlib refuses to init (%i)!", ret);
288 static void unquote(wchar_t *in)
292 for(p = in; *p != L'\0'; p++)
296 for(p2 = p + 1; (*p2 != L'\0') && (*p2 != L';') && (*p2 != L'&'); p2++);
303 if(!wcscmp(p + 1, L"amp"))
306 } else if(p[1] == L'#') {
307 nc = ucptowc(wcstol(p + 2, NULL, 10));
316 memmove(p + 1, p2 + 1, (wcslen(p2 + 1) + 1) * sizeof(wchar_t));
321 static void freeexppeer(struct dcexppeer *ep)
324 ep->next->prev = ep->prev;
326 ep->prev->next = ep->next;
331 if(ep->expire != NULL)
332 canceltimer(ep->expire);
336 static void exppeerexpire(int cancelled, struct dcexppeer *ep)
343 static struct dcexppeer *expectpeer(char *nick, struct fnetnode *fn)
345 struct dcexppeer *ep;
347 ep = smalloc(sizeof(*ep));
348 ep->nick = sstrdup(nick);
349 getfnetnode(ep->fn = fn);
350 ep->expire = timercallback(ntime() + 300, (void (*)(int, void *))exppeerexpire, ep);
359 static struct qcommand *newqcmd(struct qcommand **queue, char *string)
361 struct qcommand *new;
363 while(*queue != NULL)
364 queue = &(*queue)->next;
365 new = smalloc(sizeof(*new));
366 new->string = sstrdup(string);
372 static struct qcommand *ulqcmd(struct qcommand **queue)
374 struct qcommand *qcmd;
376 if((qcmd = *queue) == NULL)
382 static void freeqcmd(struct qcommand *qcmd)
388 static void hubrecvchat(struct socket *sk, struct fnetnode *fn, char *from, char *string)
390 wchar_t *chat, *wfrom, *wpeer;
392 struct fnetpeer *peer;
396 end = string + strlen(string);
397 while((p = strchr(string, 13)) != NULL)
398 memmove(p, p + 1, (end-- - p));
401 if((strlen(string) > strlen(from) + 2) && (*string == '<') && !memcmp(string + 1, from, strlen(from)) && (*(string + strlen(from) + 1) == '>'))
402 string += strlen(from) + 2;
403 if((wfrom = icmbstowcs(from, hub->charset)) == NULL)
405 wpeer = swcsdup(wfrom);
411 for(p = string + 1; *p; p++)
413 if((*p == ' ') || (*p == '>'))
421 if((wpeer = icmbstowcs(string + 1, hub->charset)) == NULL)
427 wpeer = swcsdup(L"");
429 if((chat = icmbstowcs(string, hub->charset)) == NULL)
439 if((peer = fnetfindpeer(fn, wfrom)) == NULL) /* Assume public chat */
440 fnethandlechat(fn, 1, wfrom, wpeer, chat);
442 fnethandlechat(fn, 0, wfrom, wpeer, chat);
444 fnethandlechat(fn, 1, L"", wpeer, chat);
452 static void peertimeout(int cancelled, struct dcpeer *peer)
454 peer->timeout = NULL;
460 static void sendadc(struct socket *sk, char *arg)
463 size_t bufsize, bufdata;
466 bufsize = bufdata = 0;
472 bufcat(buf, "\\s", 2);
473 } else if(*arg == '\n') {
474 bufcat(buf, "\\n", 2);
475 } else if(*arg == '\\') {
476 bufcat(buf, "\\\\", 2);
481 sockqueue(sk, buf, bufdata);
485 static void sendadcf(struct socket *sk, char *arg, ...)
491 buf = vsprintf2(arg, args);
499 static char **parseadc(char *args)
502 size_t retbufsize, retbufdata;
504 size_t bufsize, bufdata;
509 retbufsize = retbufdata = bufsize = bufdata = 0;
518 else if(*args != ' ')
522 if((*args == ' ') || (*args == 0))
525 addtobuf(retbuf, buf);
527 bufsize = bufdata = 0;
530 } else if(*args == '\\') {
533 addtobuf(buf, *args);
542 addtobuf(retbuf, NULL);
545 } else if((*args == 's') || (*args == ' ')) {
547 } else if(*args == 'n') {
549 } else if(*args == '\\') {
559 addtobuf(retbuf, NULL);
563 /* Macros useful in command handlers */
564 #define skipspace(s) ({if(((s) = strchr((s), ' ')) == NULL) return; else (s)++;})
565 #define qstr(sk, str) sockqueue(sk, str, strlen(str))
566 #define qstrf(sk, strandargs...) \
569 if((__buf__ = sprintf2(strandargs)) != NULL) { \
570 sockqueue(sk, __buf__, strlen(__buf__)); \
575 static char *tr(char *str, char *trans)
579 for(; *trans; trans += 2)
581 for(p = strchr(str, trans[0]); p != NULL; p = strchr(p, trans[0]))
587 static char *getadcid(struct dcpeer *peer)
594 if(!wcscmp(peer->transfer->path, L"files.xml") || !wcscmp(peer->transfer->path, L"files.xml.bz2") || !wcscmp(peer->transfer->path, L"MyList.DcLst"))
596 if(!isfilelist && (peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
598 buf = base32encode(peer->transfer->hash->buf, 24);
599 ret = sprintf2("TTH/%.39s", buf);
602 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
604 ret = pathnmdc2adc(buf);
611 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
613 if(!wcscmp(cmd, L"resume"))
617 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
620 transfer->curpos = wcstol(arg, NULL, 10);
629 static void sendmynick(struct dcpeer *peer)
635 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), peer->charset, "DoldaConnectUser-IN"));
637 qstrf(peer->sk, "$MyNick %s|", icswcstombs(fn->mynick, peer->charset, "DoldaConnectUser-IN"));
640 static void sendpeerlock(struct dcpeer *peer)
643 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
645 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
648 static void sendsupports(struct dcpeer *peer)
651 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
653 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
656 static void requestfile(struct dcpeer *peer)
660 if(peer->transfer->size == -1)
662 /* Use DCCHARSET for $Get paths until further researched... */
663 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
665 transferseterror(peer->transfer, TRNSE_NOTFOUND);
669 /* The transfer will be restarted later from
670 * cmd_filelength when it detects that the sizes
672 qstrf(peer->sk, "$Get %s$1|", buf);
675 if((peer->transfer->hash == NULL) && !peer->notthl)
677 if(supports(peer, "adcget") && supports(peer, "tthl"))
679 qstr(peer->sk, "$ADCGET");
680 sendadc(peer->sk, "tthl");
681 if((buf = getadcid(peer)) == NULL)
683 transferseterror(peer->transfer, TRNSE_NOTFOUND);
687 sendadc(peer->sk, buf);
689 sendadc(peer->sk, "0");
690 sendadc(peer->sk, "-1");
692 peer->fetchingtthl = 1;
698 if(forkfilter(peer->transfer))
700 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
704 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
707 if(supports(peer, "adcget"))
709 qstr(peer->sk, "$ADCGET");
710 sendadc(peer->sk, "file");
711 if((buf = getadcid(peer)) == NULL)
713 transferseterror(peer->transfer, TRNSE_NOTFOUND);
717 sendadc(peer->sk, buf);
719 sendadcf(peer->sk, "%i", peer->transfer->curpos);
720 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
722 } else if(supports(peer, "xmlbzlist")) {
723 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
725 transferseterror(peer->transfer, TRNSE_NOTFOUND);
729 qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
731 /* Use DCCHARSET for $Get paths until further researched... */
732 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
734 transferseterror(peer->transfer, TRNSE_NOTFOUND);
738 qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1);
742 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
746 struct fnetnode *cfn;
750 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
751 buf = tr(icswcstombs(confgetstr("dc", "desc"), hub->charset, "Charset_conv_failure"), "$_|_");
752 qstrf(sk, "%s", buf);
754 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
756 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
758 if(cfn->regstatus == FNNS_OP)
760 else if(cfn->regstatus == FNNS_REG)
766 qstrf(sk, "<%s V:%s,M:%c,H:%i/%i/%i,S:%i>",
767 (hub->dcppemu)?"++":"Dolda",
768 (hub->dcppemu)?"0.674":VERSION,
769 (tcpsock == NULL)?'P':'A',
771 confgetint("transfer", "slots")
774 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), hub->charset, "Charset_conv_failure"), "$_|_");
775 qstrf(sk, "%s\x01$", buf);
776 buf = tr(icswcstombs(confgetstr("dc", "email"), hub->charset, "Charset_conv_failure"), "$_|_");
777 qstrf(sk, "%s$", buf);
778 qstrf(sk, "%llu$|", sharesize);
781 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
786 if(!strcmp(cmd, "$Lock"))
788 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
789 } else if(!strcmp(cmd, "$Hello")) {
790 if(fn->state == FNN_HS)
792 qstrf(sk, "$Version 1,0091|");
793 qstrf(sk, "$GetNickList|");
795 fnetsetstate(fn, FNN_EST);
797 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
802 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
809 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
811 if((p = strchr(args, ' ')) != NULL)
816 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
818 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
820 key = dcmakekey(args);
821 qstrf(sk, "$Key %s|", key);
823 hubhandleaction(sk, fn, cmd, args);
826 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
832 if(hub->nativename == NULL)
833 free(hub->nativename);
834 hub->nativename = sstrdup(args);
835 buf = icmbstowcs(args, hub->charset);
836 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
839 hubhandleaction(sk, fn, cmd, args);
842 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
848 if((nick = icmbstowcs(args, hub->charset)) == NULL)
850 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
851 fnetaddpeer(fn, nick, nick);
853 hubhandleaction(sk, fn, cmd, args);
856 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
859 struct fnetpeer *peer;
863 if((nick = icmbstowcs(args, hub->charset)) == NULL)
865 if((peer = fnetfindpeer(fn, nick)) != NULL)
868 hubhandleaction(sk, fn, cmd, args);
871 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
876 struct fnetpeer *peer, *npeer;
879 for(peer = fn->peers; peer != NULL; peer = peer->next)
880 peer->flags.b.delete = 1;
881 while((p = strstr(args, "$$")) != NULL)
884 if((buf = icmbstowcs(args, hub->charset)) != NULL)
886 if((peer = fnetfindpeer(fn, buf)) == NULL)
887 peer = fnetaddpeer(fn, buf, buf);
889 peer->flags.b.delete = 0;
891 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
895 for(peer = fn->peers; peer != NULL; peer = npeer)
898 if(peer->flags.b.delete)
901 hubhandleaction(sk, fn, cmd, args);
904 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
909 struct fnetpeer *peer;
912 for(peer = fn->peers; peer != NULL; peer = peer->next)
913 peer->flags.b.op = 0;
914 while((p = strstr(args, "$$")) != NULL)
917 if((buf = icmbstowcs(args, hub->charset)) != NULL)
919 if((peer = fnetfindpeer(fn, buf)) != NULL)
920 peer->flags.b.op = 1;
925 hubhandleaction(sk, fn, cmd, args);
928 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
931 wchar_t *buf, *wp, *wp2;
933 struct fnetpeer *peer;
938 if(strncmp(p, "$ALL ", 5))
941 if((p2 = strchr(p, ' ')) == NULL)
944 if((buf = icmbstowcs(p, hub->charset)) == NULL)
946 if((peer = fnetfindpeer(fn, buf)) == NULL)
947 peer = fnetaddpeer(fn, buf, buf);
950 if((p2 = strstr(p, "$ $")) == NULL)
953 if((buf = icmbstowcs(p, hub->charset)) == NULL)
955 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcsrchr(buf, L'<')) != NULL))
957 buf[wcslen(buf) - 1] = L'\0';
959 if((wp2 = wcschr(wp, L' ')) != NULL)
962 fnetpeersetstr(peer, L"dc-client", wp);
966 if((wp2 = wcschr(wp, L',')) != NULL)
970 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
971 fnetpeersetstr(peer, abuf, wp + 2);
973 } while(wp2 != NULL);
976 fnetpeersetstr(peer, L"descr", buf);
979 if((p2 = strchr(p, '$')) == NULL)
982 if((buf = icmbstowcs(p, hub->charset)) == NULL)
984 fnetpeersetstr(peer, L"dc-speed", buf);
987 if((p2 = strchr(p, '$')) == NULL)
990 if((buf = icmbstowcs(p, hub->charset)) == NULL)
992 fnetpeersetstr(peer, L"email", buf);
997 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
998 hubhandleaction(sk, fn, cmd, args);
1001 /* I do not implement the fully in that I do not disconnect from the
1002 * old hub. I do this since I believe that if the hub owner really
1003 * wants to get rid of us, then it is his responsibility to disconnect
1005 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1008 struct fnetnode *newfn;
1012 if(strchr(args, ':') == NULL)
1014 args = strcpy(smalloc(strlen(args) + 5), args);
1015 strcat(args, ":411");
1020 if((newfn = fnetinitconnect(L"dc", args, NULL)) != NULL)
1022 linkfnetnode(newfn);
1025 hubhandleaction(sk, fn, cmd, args);
1030 static char *getdcpath(struct sharecache *node, size_t *retlen, char *charset)
1035 if(node->parent == NULL)
1037 if(node->parent == shareroot)
1039 if((buf = icwcstombs(node->name, charset)) == NULL)
1042 *retlen = strlen(buf);
1045 if((buf2 = icwcstombs(node->name, charset)) == NULL)
1047 if((buf = getdcpath(node->parent, &len, charset)) == NULL)
1052 len2 = strlen(buf2);
1053 buf = srealloc(buf, len + 1 + len2 + 1);
1055 strcpy(buf + len, buf2);
1056 buf[len + len2] = 0;
1059 *retlen = len + len2;
1065 * This is the main share searching function for Direct Connect
1066 * peers. Feel free to optimize it if you feel the need for it. I
1067 * haven't ever seen it take any noticable CPU time anyway, so I
1068 * haven't felt that need.
1070 * It may feel dubious to just return the directory when all terms are
1071 * satisfied rather than all the files in it, but it really benefits
1072 * everyone: Less cycles used for us, less UDP squelching for active
1073 * searchers, and less bandwidth waste for hubs when serving passive
1076 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1081 char *prefix, *infix, *postfix, *buf, *buf2;
1083 struct sockaddr_in addr;
1084 struct sharecache *node;
1085 int minsize, maxsize;
1087 int termnum, satisfied, skipcheck;
1088 int level, tersat[32];
1093 if((p = strchr(args, ' ')) == NULL)
1097 memset(terms, 0, sizeof(terms));
1098 prefix = infix = postfix = NULL;
1102 if(!strncmp(args, "Hub:", 4))
1104 if(!strcmp(cmd, "$MultiSearch"))
1106 if(!strcmp(args + 4, hub->nativenick))
1108 prefix = sprintf2("$SR %s ", hub->nativenick);
1109 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1110 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1114 if((p2 = strchr(args, ':')) == NULL)
1117 addr.sin_family = AF_INET;
1118 if(!inet_aton(args, &addr.sin_addr))
1120 addr.sin_port = htons(atoi(p2));
1121 prefix = sprintf2("$SR %s ", hub->nativenick);
1122 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1123 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1124 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1127 minsize = maxsize = -1;
1136 if((*(p++) == 'T') && (minsize == 0))
1143 if((p2 = strchr(p, '?')) == NULL)
1158 if((*p2 == 0) || (*p2 == '$'))
1166 if(!dotth && !strncmp(p, "TTH:", 4))
1169 if((buf = base32decode(p + 4, &buflen)) == NULL)
1173 memcpy(hashtth, buf, 24);
1176 if((terms[termnum] = icmbstowcs(p, hub->charset)) != NULL)
1187 node = shareroot->child;
1189 for(i = 0; i < termnum; i++)
1195 if(node->f.b.type == FILE_REG)
1197 if((minsize >= 0) && (node->size < minsize))
1199 if((maxsize >= 0) && (node->size > maxsize))
1202 if(!skipcheck && dotth)
1204 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1209 for(i = 0; i < termnum; i++)
1213 if(wcsexists(node->name, terms[i]))
1217 } else if(node->child == NULL) {
1222 if(!skipcheck && (satisfied == termnum))
1224 /* Use DCCHARSET in $Get paths until further researched... */
1225 if((buf = getdcpath(node, NULL, DCCHARSET)) != NULL)
1227 if(node->f.b.hastth)
1229 buf2 = base32encode(node->hashtth, 24);
1230 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1233 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1238 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1240 while(node->next == NULL)
1242 if((node = node->parent) == shareroot)
1246 if(node == shareroot)
1248 for(i = 0; i < termnum; i++)
1250 if(tersat[i] >= level)
1263 hubhandleaction(sk, fn, cmd, args);
1274 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1278 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1282 struct socket *newsk;
1283 struct sockaddr_in addr;
1286 if((p = strchr(args, ' ')) == NULL)
1289 if(strcmp(args, hub->nativenick))
1291 addr.sin_family = AF_INET;
1293 if((p = strchr(args, ':')) == NULL)
1296 addr.sin_port = htons(atoi(p));
1297 if(!inet_aton(args, &addr.sin_addr))
1299 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1301 hubhandleaction(sk, fn, cmd, args);
1304 static void sendctm(struct socket *sk, char *nick)
1306 struct sockaddr *addr;
1311 if(sockgetremotename(tcpsock, &addr, &addrlen) < 0)
1313 if(addr->sa_family == AF_INET)
1314 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1316 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1320 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1326 if((p = strchr(args, ' ')) == NULL)
1329 if(strcmp(p, hub->nativenick))
1332 expectpeer(args, fn);
1333 hubhandleaction(sk, fn, cmd, args);
1336 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1339 struct fnetnode *node;
1344 for(node = fnetnodes; node != NULL; node = node->next)
1346 if(node->state == FNN_EST)
1349 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1350 hubhandleaction(sk, fn, cmd, args);
1353 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1360 if((p2 = strchr(p, ' ')) == NULL)
1364 if((p2 = strchr(p, ' ')) == NULL)
1367 if(strcmp(p, "From:"))
1370 if((p2 = strstr(p, " $")) == NULL)
1374 hubrecvchat(fn->sk, fn, p, p2);
1375 hubhandleaction(sk, fn, cmd, args);
1378 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1382 char *nick, *filename, *hubname;
1386 wchar_t *wnick, *wfile;
1390 if((p2 = strchr(p, ' ')) == NULL)
1395 if((p2 = strchr(p, 5)) == NULL)
1399 if((p2 = strchr(p, ' ')) == NULL)
1404 if((p2 = strchr(p, '/')) == NULL)
1409 if((p2 = strchr(p, 5)) == NULL)
1413 if((p2 = strstr(p, " (")) == NULL)
1416 if((wnick = icmbstowcs(nick, hub->charset)) == NULL)
1418 /* Use DCCHARSET in $Get paths until further researched... */
1419 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1424 sr = newsrchres(&dcnet, wfile, wnick);
1425 if(sr->peernick != NULL)
1427 sr->peernick = swcsdup(wnick);
1432 if(!strncmp(hubname, "TTH:", 4))
1434 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1437 sr->hash = newhash(L"TTH", 24, buf);
1441 getfnetnode(sr->fn = fn);
1444 hubhandleaction(sk, fn, cmd, args);
1447 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1449 /* Do nothing for now. */
1452 static void cmd_getpass(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1459 pw = wpfind(fn->args, L"password");
1460 if((pw == NULL) || ((mbspw = icwcstombs(pw, hub->charset)) == NULL))
1465 qstrf(sk, "$MyPass %s|", mbspw);
1467 fn->regstatus = FNNS_REG;
1468 hubhandleaction(sk, fn, cmd, args);
1471 static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1476 fn->regstatus = FNNS_OP;
1477 hubhandleaction(sk, fn, cmd, args);
1480 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1482 struct dcexppeer *expect;
1485 if(peer->nativename != NULL)
1486 free(peer->nativename);
1487 peer->nativename = sstrdup(args);
1488 if(peer->wcsname != NULL)
1489 free(peer->wcsname);
1490 if((peer->wcsname = icmbstowcs(peer->nativename, peer->charset)) == NULL)
1497 for(expect = expected; expect != NULL; expect = expect->next)
1499 if(!strcmp(expect->nick, args))
1506 hub = expect->fn->data;
1507 peer->fn = expect->fn;
1508 getfnetnode(peer->fn);
1509 peer->dcppemu = hub->dcppemu;
1510 freeexppeer(expect);
1515 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1519 struct transfer *transfer;
1521 if((p = strchr(args, ' ')) == NULL)
1524 if(!strcmp(args, "Upload"))
1526 if(!strcmp(args, "Download"))
1530 if((peer->transfer == NULL) || (mydir != peer->direction))
1535 if(peer->direction == TRNSD_DOWN)
1538 if(peer->wcsname == NULL)
1543 peer->direction = mydir;
1544 if(peer->direction == TRNSD_UP)
1546 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1548 if((transfer = finddownload(peer->wcsname)) == NULL)
1553 transferattach(transfer, &dctransfer, peer);
1554 transfersetstate(transfer, TRNS_HS);
1556 transfersetnick(transfer, peer->wcsname);
1557 peer->transfer = transfer;
1560 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1561 if(peer->key != NULL)
1562 qstrf(sk, "$Key %s|", peer->key);
1563 if(peer->direction == TRNSD_DOWN)
1568 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1571 struct transfer *transfer;
1573 if((p = strchr(args, ' ')) == NULL)
1576 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1578 key = dcmakekey(args);
1581 if(peer->wcsname == NULL)
1590 if((transfer = finddownload(peer->wcsname)) == NULL)
1592 peer->direction = TRNSD_UP;
1593 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1595 peer->direction = TRNSD_DOWN;
1596 transferattach(transfer, &dctransfer, peer);
1597 transfersetstate(transfer, TRNS_HS);
1599 transfersetnick(transfer, peer->wcsname);
1600 peer->transfer = transfer;
1601 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1602 qstrf(sk, "$Key %s|", key);
1605 if(peer->key != NULL)
1611 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1616 static void startdl(struct dcpeer *peer)
1618 if(peer->timeout != NULL)
1619 canceltimer(peer->timeout);
1620 peer->state = PEER_TRNS;
1621 transferstartdl(peer->transfer, peer->sk);
1622 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1623 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1626 static void startul(struct dcpeer *peer)
1628 if(peer->timeout != NULL)
1629 canceltimer(peer->timeout);
1630 peer->state = PEER_TRNS;
1631 transferstartul(peer->transfer, peer->sk);
1632 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1635 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1638 struct transfer *transfer;
1640 if(peer->transfer == NULL)
1646 if(peer->transfer->size != size)
1648 transfersetsize(peer->transfer, size);
1649 transfer = peer->transfer;
1651 trytransferbypeer(transfer->fnet, transfer->peerid);
1655 qstr(peer->sk, "$Send|");
1658 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1660 if(peer->fetchingtthl)
1662 peer->fetchingtthl = 0;
1667 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1669 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1670 resettransfer(peer->transfer);
1676 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1678 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1680 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1681 resettransfer(peer->transfer);
1694 {"MyList.DcLst", &hmlistname, updatehmlist},
1695 {"files.xml", &xmllistname, updatexmllist},
1696 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1700 static int openfilelist(char *name)
1705 for(i = 0; lists[i].name != NULL; i++)
1707 if(!strcmp(name, lists[i].name))
1711 if(lists[i].name == NULL)
1714 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1716 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1719 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1726 static struct sharecache *findbytth(char *tth32)
1730 struct sharecache *node;
1732 if((buf = base32decode(tth32, &buflen)) == NULL)
1739 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1741 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1748 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1750 struct sharecache *node;
1757 if((p2 = strchr(p, sep)) != NULL)
1759 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1766 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1771 struct sharecache *node;
1772 struct socket *lesk;
1776 if(peer->transfer == NULL)
1781 if((p = strchr(args, '$')) == NULL)
1787 if((offset = (atoi(p) - 1)) < 0)
1792 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1794 qstr(sk, "$Error Could not send file list|");
1797 } else if(fd >= 0) {
1798 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1799 transfersetpath(peer->transfer, buf2);
1800 peer->transfer->flags.b.minislot = 1;
1804 /* Use DCCHARSET in $Get paths until further researched... */
1805 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1807 qstrf(sk, "$Error File not in share|");
1811 if((fd = opensharecache(node)) < 0)
1813 qstrf(sk, "$Error %s|", strerror(errno));
1817 buf = getfspath(node);
1818 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1819 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1821 transfersetpath(peer->transfer, buf2);
1824 if(fstat(fd, &sb) < 0)
1827 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1828 qstrf(sk, "$Error|");
1832 if(sb.st_size < 65536)
1833 peer->transfer->flags.b.minislot = 1;
1834 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1836 qstr(sk, "$MaxedOut|");
1840 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1843 qstrf(sk, "$Error Offset out of range|");
1847 lesk = wrapsock(fd);
1848 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1850 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1853 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1855 if(peer->transfer == NULL)
1860 if(peer->transfer->localend == NULL)
1869 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1874 size_t arrsize, arrdata;
1876 if(peer->supports != NULL)
1878 for(i = 0; peer->supports[i] != NULL; i++)
1879 free(peer->supports[i]);
1880 free(peer->supports);
1883 arrsize = arrdata = 0;
1887 if((p2 = strchr(p, ' ')) != NULL)
1891 addtobuf(arr, sstrdup(p));
1892 } while((p = p2) != NULL);
1893 addtobuf(arr, NULL);
1894 peer->supports = arr;
1897 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1901 int start, numbytes;
1902 char *charset, *buf;
1904 struct sharecache *node;
1906 struct socket *lesk;
1908 if(peer->transfer == NULL)
1914 if((p2 = strchr(p, ' ')) == NULL)
1922 if((p2 = strchr(p, ' ')) == NULL)
1930 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1933 /* Use DCCHARSET in $Get paths until further researched... */
1934 charset = DCCHARSET;
1935 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1936 initcompress(peer, CPRS_ZLIB);
1937 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1939 qstr(sk, "$Error Could not send file list|");
1941 } else if(fd >= 0) {
1942 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1943 transfersetpath(peer->transfer, buf2);
1944 peer->transfer->flags.b.minislot = 1;
1948 if((node = resdcpath(p, charset, '\\')) == NULL)
1950 qstr(sk, "$Error File not in cache|");
1953 if((fd = opensharecache(node)) < 0)
1955 qstrf(sk, "$Error %s|", strerror(errno));
1958 buf = getfspath(node);
1959 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1960 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1962 transfersetpath(peer->transfer, buf2);
1965 if(fstat(fd, &sb) < 0)
1968 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1969 qstr(sk, "$Error|");
1972 if(sb.st_size < 65536)
1973 peer->transfer->flags.b.minislot = 1;
1974 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1976 qstr(sk, "$MaxedOut|");
1979 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1982 qstr(sk, "$Error Offset out of range|");
1985 if((numbytes < 0) || (start + numbytes > sb.st_size))
1986 numbytes = sb.st_size - start;
1987 lesk = wrapsock(fd);
1988 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1990 qstrf(sk, "$Sending %i|", numbytes);
1994 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1998 int start, numbytes;
1999 struct sharecache *node;
2001 struct socket *lesk;
2005 if(peer->transfer == NULL)
2010 if((argv = parseadc(args)) == NULL)
2015 if(parrlen(argv) < 4)
2020 start = atoi(argv[2]);
2021 numbytes = atoi(argv[3]);
2024 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
2026 qstr(sk, "$Error Could not send file list|");
2028 } else if(fd >= 0) {
2029 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
2030 transfersetpath(peer->transfer, wbuf);
2031 peer->transfer->flags.b.minislot = 1;
2035 if(!strncmp(argv[1], "TTH/", 4))
2037 if((node = findbytth(argv[1] + 4)) == NULL)
2039 qstr(sk, "$Error File not in cache|");
2043 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
2045 qstr(sk, "$Error File not in cache|");
2049 if((fd = opensharecache(node)) < 0)
2051 qstrf(sk, "$Error %s|", strerror(errno));
2054 buf = getfspath(node);
2055 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
2056 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2058 transfersetpath(peer->transfer, wbuf);
2061 if(!strcmp(argv[0], "file"))
2063 for(i = 4; argv[i] != NULL; i++)
2065 if(!strcmp(argv[i], "ZL1"))
2066 initcompress(peer, CPRS_ZLIB);
2068 if(fstat(fd, &sb) < 0)
2070 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2071 qstr(sk, "$Error|");
2074 if(sb.st_size < 65536)
2075 peer->transfer->flags.b.minislot = 1;
2076 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
2077 qstr(sk, "$MaxedOut|");
2080 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2082 qstr(sk, "$Error Offset out of range|");
2085 if((numbytes < 0) || (start + numbytes > sb.st_size))
2086 numbytes = sb.st_size - start;
2087 lesk = wrapsock(fd);
2088 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2091 qstr(sk, "$ADCSND");
2092 sendadc(sk, "file");
2093 sendadc(sk, argv[1]);
2094 sendadcf(sk, "%i", start);
2095 sendadcf(sk, "%i", numbytes);
2096 if(peer->compress == CPRS_ZLIB)
2100 } else if(!strcmp(argv[0], "tthl")) {
2102 * XXX: Implement full TTHL support.
2104 * In the meantime, this is better than nothing, since it at
2105 * least allows fetching of the TTH root if it isn't already
2110 qstr(sk, "$Error no TTHL data for virtual files|");
2113 qstr(sk, "$ADCSND");
2114 sendadc(sk, "tthl");
2115 sendadc(sk, argv[1]);
2119 sockqueue(sk, node->hashtth, 24);
2121 qstr(sk, "$Error Namespace not implemented|");
2131 static void handletthl(struct dcpeer *peer)
2135 while(peer->inbufdata >= 24)
2137 pushtigertree(&peer->tth, peer->inbuf);
2138 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2139 peer->curread += 24;
2141 if(peer->curread >= peer->totalsize)
2143 if(peer->timeout == NULL)
2144 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2145 peer->state = PEER_CMD;
2146 synctigertree(&peer->tth);
2147 restigertree(&peer->tth, buf);
2148 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2153 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2156 int start, numbytes;
2158 if(peer->transfer == NULL)
2163 if((argv = parseadc(args)) == NULL)
2168 if(parrlen(argv) < 4)
2173 start = atoi(argv[2]);
2174 numbytes = atoi(argv[3]);
2175 if(!strcmp(argv[0], "tthl"))
2177 if((start != 0) || (numbytes % 24 != 0))
2179 /* Weird. Bail out. */
2183 if(peer->timeout != NULL)
2184 canceltimer(peer->timeout);
2185 peer->state = PEER_TTHL;
2186 peer->totalsize = numbytes;
2188 peer->fetchingtthl = 0;
2189 inittigertree(&peer->tth);
2191 } else if(!strcmp(argv[0], "file")) {
2192 if(start != peer->transfer->curpos)
2197 if(start + numbytes != peer->transfer->size)
2199 transfersetsize(peer->transfer, start + numbytes);
2204 if(peer->inbufdata > 0)
2206 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2207 peer->inbufdata = 0;
2208 transread(sk, peer);
2211 /* We certainly didn't request this...*/
2220 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2224 if(peer->transfer == NULL)
2229 numbytes = atoi(args);
2230 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2232 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2237 if(peer->inbufdata > 0)
2239 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2240 peer->inbufdata = 0;
2241 transread(sk, peer);
2247 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2252 hubhandleaction(sk, fn, cmd, args);
2256 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2262 static int hubreqconn(struct fnetpeer *peer)
2267 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2272 if((hub = peer->fn->data) == NULL)
2277 if((mbsnick = icwcstombs(peer->id, hub->charset)) == NULL)
2278 return(1); /* Shouldn't happen, of course, but who knows... */
2281 sendctm(peer->fn->sk, mbsnick);
2282 expectpeer(mbsnick, peer->fn);
2284 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2290 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2293 char *mbsstring, *mbsto;
2296 if((mbsto = icwcstombs(to, hub->charset)) == NULL)
2301 if((mbsstring = icwcstombs(string, hub->charset)) == NULL)
2306 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2313 if(strchr(mbsstring, '|') != NULL)
2324 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2326 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2329 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2336 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2338 int minl, maxl, minr, maxr, retmin, retmax;
2343 findsizelimit(sexpr->l, &minl, &maxl);
2344 findsizelimit(sexpr->r, &minr, &maxr);
2345 retmin = (minl > minr)?minl:minr;
2346 if((maxl != -1) && (maxr != -1))
2347 retmax = (maxl < maxr)?maxl:maxr;
2356 findsizelimit(sexpr->l, &minl, &maxl);
2357 findsizelimit(sexpr->r, &minr, &maxr);
2358 retmin = (minl < minr)?minl:minr;
2359 if((maxl == -1) || (maxr == -1))
2362 retmax = (maxl > maxr)?maxl:maxr;
2365 findsizelimit(sexpr->l, &minl, &maxl);
2366 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2370 } else if((minl == 0) && (maxl != -1)) {
2373 } else if((minl != 0) && (maxl == -1)) {
2376 } else { /* This would yield two seperate intervals, which DC cannot handle */
2382 retmax = sexpr->d.n - 1;
2385 retmin = sexpr->d.n;
2386 retmax = sexpr->d.n;
2389 retmin = sexpr->d.n + 1;
2403 static struct hash *findsehash(struct sexpr *sexpr)
2405 struct hash *h1, *h2;
2410 if((h1 = findsehash(sexpr->l)) != NULL)
2412 if((h1 = findsehash(sexpr->r)) != NULL)
2416 h1 = findsehash(sexpr->l);
2417 h2 = findsehash(sexpr->r);
2422 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2423 return(sexpr->d.hash);
2430 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2433 struct wcslist *list, *cur;
2434 char *sstr, *buf, *p;
2435 size_t sstrsize, sstrdata;
2436 struct sockaddr *name;
2438 int minsize, maxsize;
2442 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2444 list = findsexprstrs(srch->sexpr);
2445 findsizelimit(srch->sexpr, &minsize, &maxsize);
2446 hash = findsehash(srch->sexpr);
2447 if((minsize != 0) && (maxsize != -1))
2449 /* Choose either minsize or maxsize by trying to determine
2450 * which choice will be most restrictive. The result can be
2451 * approximative at best anyway, so don't try too hard... */
2452 if((50000000 - maxsize) > (minsize - 50000000))
2458 sstrsize = sstrdata = 0;
2459 if((hash != NULL) && (hash->len == 24))
2461 /* Prioritize hash searches above all else */
2462 bufcat(sstr, "F?T?0?9?TTH:", 12);
2463 buf = base32encode(hash->buf, hash->len);
2465 bufcat(sstr, buf, 39);
2470 sizebuf2(sstr, sstrdata + 32, 1);
2471 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2472 } else if(maxsize != -1) {
2473 sizebuf2(sstr, sstrdata + 32, 1);
2474 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2476 bufcat(sstr, "F?F?0?1?", 8);
2480 for(cur = list; cur != NULL; cur = cur->next)
2482 /* Use DCCHARSET in $Get paths until further researched... */
2483 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2485 /* Can't find anything anyway if the search expression
2486 * requires characters outside DC's charset. There's
2487 * nothing technically wrong with the search itself,
2488 * however, so return success. This should be
2489 * considered as an optimization. */
2496 addtobuf(sstr, '$');
2498 * It probably doesn't hurt if buf contains any extra
2499 * dollar signs - it will just result in extra search
2500 * terms, and the extraneous results will be filtered by
2501 * the search layer anyway. It hurts if it contains any
2502 * pipes, though, so let's sell them for money.
2504 for(p = buf; *p; p++)
2509 bufcat(sstr, buf, strlen(buf));
2513 /* Will match all files... :-/ */
2514 addtobuf(sstr, '.');
2520 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2522 flog(LOG_WARNING, "cannot get address of UDP socket");
2524 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2528 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2532 fn->lastsrch = time(NULL);
2540 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2541 struct command hubcmds[] =
2543 {"$Lock", cc(cmd_lock)},
2544 {"$HubName", cc(cmd_hubname)},
2545 {"$Hello", cc(cmd_hello)},
2546 {"$Quit", cc(cmd_quit)},
2547 {"$NickList", cc(cmd_nicklist)},
2548 {"$OpList", cc(cmd_oplist)},
2549 {"$MyINFO", cc(cmd_myinfo)},
2550 {"$ForceMove", cc(cmd_forcemove)},
2551 {"$Search", cc(cmd_search)},
2552 {"$MultiSearch", cc(cmd_search)},
2553 {"$ConnectToMe", cc(cmd_connecttome)},
2554 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2555 {"$GetNetInfo", cc(cmd_getnetinfo)},
2556 {"$To:", cc(cmd_to)},
2557 {"$SR", cc(cmd_sr)},
2558 {"$UserCommand", cc(cmd_usercommand)},
2559 {"$GetPass", cc(cmd_getpass)},
2560 {"$LogedIn", cc(cmd_logedin)}, /* sic */
2564 struct command peercmds[] =
2566 {"$MyNick", cc(cmd_mynick)},
2567 {"$Lock", cc(cmd_peerlock)},
2568 {"$Direction", cc(cmd_direction)},
2569 {"$Key", cc(cmd_key)},
2570 {"$FileLength", cc(cmd_filelength)},
2571 {"$Error", cc(cmd_error)},
2572 {"$MaxedOut", cc(cmd_maxedout)},
2573 {"$Get", cc(cmd_get)},
2574 {"$Send", cc(cmd_send)},
2575 {"$Supports", cc(cmd_supports)},
2576 {"$GetBlock", cc(cmd_getblock)},
2577 {"$UGetBlock", cc(cmd_getblock)},
2578 {"$GetZBlock", cc(cmd_getblock)},
2579 {"$UGetZBlock", cc(cmd_getblock)},
2580 {"$ADCGET", cc(cmd_adcget)},
2581 {"$ADCSND", cc(cmd_adcsnd), 1},
2582 {"$Sending", cc(cmd_sending), 1},
2587 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2589 CBUNREG(transfer, trans_filterout, peer);
2592 peer->transfer = NULL;
2596 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2604 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2606 if(sockqueuesize(peer->sk) < 65536)
2608 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2610 if(peer->compress == CPRS_NONE)
2612 sockqueue(peer->sk, buf, bufsize);
2613 } else if(peer->compress == CPRS_ZLIB) {
2614 cstr = peer->cprsdata;
2615 cstr->next_in = buf;
2616 cstr->avail_in = bufsize;
2617 while(cstr->avail_in > 0)
2619 cstr->next_out = outbuf;
2620 cstr->avail_out = sizeof(outbuf);
2621 if((ret = deflate(cstr, 0)) != Z_OK)
2623 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2627 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2632 if(peer->state == PEER_SYNC)
2634 if(peer->compress == CPRS_ZLIB)
2636 cstr = peer->cprsdata;
2637 cstr->next_in = NULL;
2641 cstr->next_out = outbuf;
2642 cstr->avail_out = sizeof(outbuf);
2643 ret = deflate(cstr, Z_FINISH);
2644 if((ret != Z_OK) && (ret != Z_STREAM_END))
2646 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2650 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2651 } while(ret != Z_STREAM_END);
2657 if(peer->timeout == NULL)
2658 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2659 peer->state = PEER_CMD;
2661 transfersetstate(transfer, TRNS_HS);
2662 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2663 transfer->flags.b.minislot = 0;
2664 peer->sk->writecb = NULL;
2671 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2673 peer->state = PEER_SYNC;
2674 dctransgotdata(transfer, peer);
2677 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2679 if(transferdatasize(transfer) < 65536)
2680 peer->sk->ignread = 0;
2683 static void transread(struct socket *sk, struct dcpeer *peer)
2687 struct transfer *transfer;
2689 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2691 if(peer->transfer == NULL)
2697 transferputdata(peer->transfer, buf, bufsize);
2699 if(peer->transfer->curpos >= peer->transfer->size)
2701 transfer = peer->transfer;
2702 transferdetach(transfer);
2703 transferendofdata(transfer);
2706 if(transferdatasize(peer->transfer) > 65535)
2710 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2712 struct transfer *transfer;
2714 if((transfer = peer->transfer) == NULL)
2719 transferdetach(transfer);
2720 transferendofdata(transfer);
2723 static void transwrite(struct socket *sk, struct dcpeer *peer)
2725 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2727 if(peer->transfer == NULL)
2732 dctransgotdata(peer->transfer, peer);
2735 static void udpread(struct socket *sk, void *data)
2737 char *buf, *p, *p2, *hashbuf;
2738 size_t buflen, hashlen;
2739 char *nick, *filename, *hubname;
2740 struct sockaddr_in hubaddr;
2742 struct fnetnode *fn, *myfn;
2745 wchar_t *wnick, *wfile;
2748 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2750 buf = srealloc(buf, buflen + 1);
2752 if(!strncmp(buf, "$SR ", 4))
2756 if((p2 = strchr(p, ' ')) == NULL)
2764 if((p2 = strchr(p, 5)) == NULL)
2771 if((p2 = strchr(p, ' ')) == NULL)
2779 if((p2 = strchr(p, '/')) == NULL)
2787 if((p2 = strchr(p, 5)) == NULL)
2794 if((p2 = strstr(p, " (")) == NULL)
2801 if((p2 = strchr(p, ':')) == NULL)
2807 hubaddr.sin_family = AF_INET;
2808 if(!inet_aton(p, &hubaddr.sin_addr))
2814 if((p2 = strchr(p, ')')) == NULL)
2820 hubaddr.sin_port = htons(atoi(p));
2821 /* Use DCCHARSET in $Get paths until further researched... */
2822 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2829 if(!strncmp(hubname, "TTH:", 4))
2831 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2834 hash = newhash(L"TTH", 24, hashbuf);
2838 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2840 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2842 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2857 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2859 if((fn->fnet == &dcnet) && (fn->sk != NULL) && addreq(fn->sk->remote, (struct sockaddr *)&hubaddr))
2869 if((wnick = icmbstowcs(nick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
2874 sr = newsrchres(&dcnet, wfile, wnick);
2875 if(sr->peernick != NULL)
2877 sr->peernick = swcsdup(wnick);
2883 getfnetnode(sr->fn = myfn);
2892 static void hubread(struct socket *sk, struct fnetnode *fn)
2899 hub = (struct dchub *)fn->data;
2900 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2902 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2904 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2905 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2907 p = hub->inbuf + hub->inbufdata;
2908 hub->inbufdata += datalen;
2909 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2912 newqcmd(&hub->queue, hub->inbuf);
2913 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2914 datalen = hub->inbufdata;
2919 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2924 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2930 if((buf = icwcstombs(newnick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
2932 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2937 if(hub == NULL) /* Not yet connected */
2942 if(hub->nativenick != NULL)
2943 free(hub->nativenick);
2944 hub->nativenick = buf;
2948 static struct dchub *newdchub(struct fnetnode *fn)
2955 new = smalloc(sizeof(*new));
2956 memset(new, 0, sizeof(*new));
2958 if(confgetint("dc", "dcppemu"))
2960 if((emu = wpfind(fn->args, L"dcppemu")) != NULL)
2968 if((wcharset = wpfind(fn->args, L"charset")) != NULL)
2970 if((charset = icwcstombs(wcharset, "US-ASCII")) != NULL)
2972 if(!havecharset(charset))
2980 new->charset = charset;
2982 new->charset = sstrdup(DCCHARSET);
2983 if(hubsetnick(fn, fn->mynick))
2984 fnetsetnick(fn, L"DoldaConnectUser-IN");
2985 /* IN as in Invalid Nick */
2989 static struct dcpeer *newdcpeer(struct socket *sk)
2993 new = smalloc(sizeof(*new));
2994 memset(new, 0, sizeof(*new));
2995 new->transfer = NULL;
2998 if(confgetint("dc", "dcppemu"))
3009 static void freedcpeer(struct dcpeer *peer)
3012 struct qcommand *qcmd;
3017 if(peer->next != NULL)
3018 peer->next->prev = peer->prev;
3019 if(peer->prev != NULL)
3020 peer->prev->next = peer->next;
3021 if(peer->transfer != NULL)
3023 if(peer->transfer->dir == TRNSD_UP)
3024 peer->transfer->close = 1;
3025 if(peer->transfer->dir == TRNSD_DOWN)
3026 resettransfer(peer->transfer);
3027 transferdetach(peer->transfer);
3029 if(peer->timeout != NULL)
3030 canceltimer(peer->timeout);
3031 if(peer->sk->data == peer)
3032 peer->sk->data = NULL;
3033 peer->sk->readcb = NULL;
3034 peer->sk->writecb = NULL;
3035 peer->sk->errcb = NULL;
3038 if(peer->supports != NULL)
3040 for(i = 0; peer->supports[i] != NULL; i++)
3041 free(peer->supports[i]);
3042 free(peer->supports);
3044 if(peer->inbuf != NULL)
3046 if(peer->key != NULL)
3048 if(peer->wcsname != NULL)
3049 free(peer->wcsname);
3050 if(peer->nativename != NULL)
3051 free(peer->nativename);
3052 if(peer->charset != NULL)
3053 free(peer->charset);
3054 if(peer->fn != NULL)
3055 putfnetnode(peer->fn);
3056 while((qcmd = ulqcmd(&peer->queue)) != NULL)
3062 static void hubconnect(struct fnetnode *fn)
3064 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
3065 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
3067 fn->data = newdchub(fn);
3072 static void hubdestroy(struct fnetnode *fn)
3075 struct qcommand *qcmd;
3077 hub = (struct dchub *)fn->data;
3078 if((fn->sk != NULL) && (fn->sk->data == fn))
3080 fn->sk->data = NULL;
3085 while((qcmd = ulqcmd(&hub->queue)) != NULL)
3087 if(hub->nativename != NULL)
3088 free(hub->nativename);
3089 if(hub->nativenick != NULL)
3090 free(hub->nativenick);
3091 if(hub->charset != NULL)
3093 if(hub->inbuf != NULL)
3098 static wchar_t *dcbasename(wchar_t *filename)
3102 if((ret = wcsrchr(filename, L'\\')) != NULL)
3107 static struct transferiface dctransfer =
3109 .detach = (void (*)(struct transfer *, void *))dctransdetach,
3110 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
3111 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
3112 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
3115 static struct fnet dcnet =
3118 .connect = hubconnect,
3119 .destroy = hubdestroy,
3120 .setnick = hubsetnick,
3121 .reqconn = hubreqconn,
3122 .sendchat = hubsendchat,
3123 .search = hubsearch,
3124 .filebasename = dcbasename
3127 static void peerread(struct socket *sk, struct dcpeer *peer)
3131 struct command *cmd;
3133 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
3135 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
3136 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
3138 peer->inbufdata += datalen;
3139 if(peer->state == PEER_CMD)
3142 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
3145 newqcmd(&peer->queue, peer->inbuf);
3146 for(cmd = peercmds; cmd->handler != NULL; cmd++)
3148 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
3151 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
3154 peer->state = PEER_STOP;
3158 } else if(peer->state == PEER_TTHL) {
3163 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3168 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3170 struct dcpeer *peer;
3179 peer = newdcpeer(sk);
3182 peer->dcppemu = hub->dcppemu;
3183 sk->readcb = (void (*)(struct socket *, void *))peerread;
3184 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3186 socksettos(sk, confgetint("fnet", "fnptos"));
3188 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3193 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3195 struct dcpeer *peer;
3197 peer = newdcpeer(newsk);
3199 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3200 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3202 socksettos(newsk, confgetint("fnet", "fnptos"));
3203 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3206 static void updatehmlist(void)
3208 int i, lev, ic, ret;
3209 struct sharecache *node;
3210 char *buf, *buf2, numbuf[32];
3211 size_t bufsize, bufdata;
3215 buf = smalloc(bufsize = 65536);
3216 node = shareroot->child;
3221 /* Use DCCHARSET in $Get paths until further researched... */
3222 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3224 for(i = 0; i < lev; i++)
3226 bufcat(buf, buf2, strlen(buf2));
3228 if(node->f.b.type == FILE_REG)
3231 sprintf(numbuf, "%i", node->size);
3232 bufcat(buf, numbuf, strlen(numbuf));
3239 if((node->child != NULL) && !ic)
3243 } else if(node->next != NULL) {
3246 while(node->next == NULL)
3249 node = node->parent;
3250 if(node == shareroot)
3253 if(node == shareroot)
3258 if(hmlistname != NULL)
3263 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3264 if((fd = mkstemp(hmlistname)) < 0)
3266 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3271 * I do not want to implement a good Huffman encoder, and it's not
3272 * like Huffman encoding actually yields any impressive results
3273 * for DC file lists anyway, so I'll just output a bogus
3274 * tree. Implement a good encoder if you want to.
3276 write(fd, "HE3\r\0", 5);
3277 write(fd, &bufdata, 4);
3279 write(fd, &ibuf, 2);
3281 for(i = 0; i < 256; i++)
3284 write(fd, &ibuf, 1);
3286 for(i = 0; i < 256; i++)
3288 for(buf2 = buf; bufdata > 0;)
3290 if((ret = write(fd, buf2, bufdata)) < 0)
3292 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3303 static struct xmlent
3310 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3314 static wchar_t *escapexml(wchar_t *src)
3316 static wchar_t *buf = NULL;;
3319 size_t bufsize, bufdata;
3325 bufsize = bufdata = 0;
3326 for(; *src != L'\0'; src++)
3329 if((c > 0) && (c < 32))
3331 bufcat(buf, L"&#", 2);
3332 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3333 bufcat(buf, nbuf, ret);
3334 addtobuf(buf, L';');
3336 for(ent = entities; ent->ent != NULL; ent++)
3340 bufcat(buf, ent->ent, wcslen(ent->ent));
3344 if(ent->ent == NULL)
3345 addtobuf(buf, *src);
3348 addtobuf(buf, L'\0');
3352 static void updatexmllist(void)
3356 char cidbuf[14], *namebuf;
3358 struct sharecache *node;
3360 if(xmllistname != NULL)
3362 unlink(xmllistname);
3365 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3366 if((fd = mkstemp(xmllistname)) < 0)
3368 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3373 if((fs = fdopen(fd, "w")) == NULL)
3375 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3376 unlink(xmllistname);
3382 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3383 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3384 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3386 if(confgetint("dc", "dcppemu"))
3387 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"DC++ 0.674\">\r\n", cidbuf);
3389 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, "DoldaConnect" VERSION);
3391 node = shareroot->child;
3395 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3397 for(i = 0; i < lev; i++)
3399 if(node->child != NULL)
3401 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3406 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3407 if(node->f.b.hastth)
3409 hashbuf = base32encode(node->hashtth, 24);
3410 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3413 fprintf(fs, "/>\r\n");
3415 while(node->next == NULL)
3417 node = node->parent;
3418 if(node == shareroot)
3423 for(i = 0; i < lev; i++)
3425 fprintf(fs, "</Directory>\r\n");
3428 if(node == shareroot)
3434 if(confgetint("dc", "dcppemu"))
3435 fprintf(fs, "</FileListing>");
3437 fprintf(fs, "</FileListing>\r\n");
3441 static void updatexmlbz2list(void)
3450 if(xmllistname == NULL)
3452 if((in = fopen(xmllistname, "r")) == NULL)
3454 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3457 if(xmlbz2listname != NULL)
3459 unlink(xmlbz2listname);
3460 free(xmlbz2listname);
3462 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3463 if((fd = mkstemp(xmlbz2listname)) < 0)
3465 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3466 free(xmlbz2listname);
3467 xmlbz2listname = NULL;
3471 if((real = fdopen(fd, "w")) == NULL)
3473 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3475 unlink(xmlbz2listname);
3476 free(xmlbz2listname);
3477 xmlbz2listname = NULL;
3481 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3484 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3486 unlink(xmlbz2listname);
3487 free(xmlbz2listname);
3488 xmlbz2listname = NULL;
3494 bufdata = fread(buf, 1, sizeof(buf), in);
3495 BZ2_bzWrite(&err, out, buf, bufdata);
3498 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3502 static int shareupdate(unsigned long long uusharesize, void *data)
3510 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3513 struct command *cmd;
3515 if((p = strchr(qcmd->string, ' ')) != NULL)
3517 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3519 if(!strcmp(cmd->name, qcmd->string))
3522 if(cmd->handler != NULL)
3523 cmd->handler(sk, data, qcmd->string, p);
3526 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3530 static int run(void)
3532 struct fnetnode *fn, *nextfn;
3534 struct dcpeer *peer, *nextpeer;
3535 struct qcommand *qcmd;
3539 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3542 if(fn->fnet != &dcnet)
3544 if(fn->data == NULL)
3546 hub = (struct dchub *)fn->data;
3547 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3549 if(*qcmd->string == '$')
3551 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3552 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3553 } else if(*qcmd->string != 0) {
3554 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3561 for(peer = peers; peer != NULL; peer = nextpeer)
3563 nextpeer = peer->next;
3564 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3566 if(peer->timeout != NULL)
3567 canceltimer(peer->timeout);
3568 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3569 if(*qcmd->string == '$')
3570 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3579 static void preinit(int hup)
3586 static int updateudpport(struct configvar *var, void *uudata)
3588 struct sockaddr_in addr;
3589 struct socket *newsock;
3591 memset(&addr, 0, sizeof(addr));
3592 addr.sin_family = AF_INET;
3593 addr.sin_port = htons(var->val.num);
3594 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3596 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3599 newsock->readcb = udpread;
3606 static int updatetcpport(struct configvar *var, void *uudata)
3608 struct sockaddr_in addr;
3609 struct socket *newsock;
3611 memset(&addr, 0, sizeof(addr));
3612 addr.sin_family = AF_INET;
3613 addr.sin_port = htons(var->val.num);
3614 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3615 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3622 static int init(int hup)
3624 struct sockaddr_in addr;
3628 GCBREG(sharechangecb, shareupdate, NULL);
3633 addr.sin_family = AF_INET;
3634 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3635 addr.sin_port = htons(confgetint("dc", "udpport"));
3636 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3638 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3641 udpsock->readcb = udpread;
3642 addr.sin_port = htons(confgetint("dc", "tcpport"));
3643 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3644 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3645 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3646 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3647 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3652 static void terminate(void)
3654 if(hmlistname != NULL)
3659 if(xmllistname != NULL)
3661 unlink(xmllistname);
3664 if(xmlbz2listname != NULL)
3666 unlink(xmlbz2listname);
3667 free(xmlbz2listname);
3671 static struct configvar myvars[] =
3673 {CONF_VAR_STRING, "desc", {.str = L""}},
3674 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3675 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3676 {CONF_VAR_INT, "udpport", {.num = 0}},
3677 {CONF_VAR_INT, "tcpport", {.num = 0}},
3678 {CONF_VAR_BOOL, "dcppemu", {.num = 0}},
3682 static struct module me =
3691 .terminate = terminate,