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)
593 if(!wcscmp(peer->transfer->path, L"files.xml") || !wcscmp(peer->transfer->path, L"files.xml.bz2") || !wcscmp(peer->transfer->path, L"MyList.DcLst"))
595 if(!isfilelist && (peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
597 buf = base32encode(peer->transfer->hash->buf, 24);
598 ret = sprintf2("TTH/%.39s", buf);
601 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
603 ret = pathnmdc2adc(buf);
610 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
612 if(!wcscmp(cmd, L"resume"))
616 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
619 transfer->curpos = wcstol(arg, NULL, 10);
628 static void sendmynick(struct dcpeer *peer)
634 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), peer->charset, "DoldaConnectUser-IN"));
636 qstrf(peer->sk, "$MyNick %s|", icswcstombs(fn->mynick, peer->charset, "DoldaConnectUser-IN"));
639 static void sendpeerlock(struct dcpeer *peer)
642 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
644 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
647 static void sendsupports(struct dcpeer *peer)
650 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
652 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
655 static void requestfile(struct dcpeer *peer)
659 if(peer->transfer->size == -1)
661 /* Use DCCHARSET for $Get paths until further researched... */
662 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
664 transferseterror(peer->transfer, TRNSE_NOTFOUND);
668 /* The transfer will be restarted later from
669 * cmd_filelength when it detects that the sizes
671 qstrf(peer->sk, "$Get %s$1|", buf);
674 if((peer->transfer->hash == NULL) && !peer->notthl)
676 if(supports(peer, "adcget") && supports(peer, "tthl"))
678 qstr(peer->sk, "$ADCGET");
679 sendadc(peer->sk, "tthl");
680 if((buf = getadcid(peer)) == NULL)
682 transferseterror(peer->transfer, TRNSE_NOTFOUND);
686 sendadc(peer->sk, buf);
688 sendadc(peer->sk, "0");
689 sendadc(peer->sk, "-1");
691 peer->fetchingtthl = 1;
697 if(forkfilter(peer->transfer))
699 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
703 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
706 if(supports(peer, "adcget"))
708 qstr(peer->sk, "$ADCGET");
709 sendadc(peer->sk, "file");
710 if((buf = getadcid(peer)) == NULL)
712 transferseterror(peer->transfer, TRNSE_NOTFOUND);
716 sendadc(peer->sk, buf);
718 sendadcf(peer->sk, "%i", peer->transfer->curpos);
719 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
721 } else if(supports(peer, "xmlbzlist")) {
722 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
724 transferseterror(peer->transfer, TRNSE_NOTFOUND);
728 qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
730 /* Use DCCHARSET for $Get paths until further researched... */
731 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
733 transferseterror(peer->transfer, TRNSE_NOTFOUND);
737 qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1);
741 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
745 struct fnetnode *cfn;
749 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
750 buf = tr(icswcstombs(confgetstr("dc", "desc"), hub->charset, "Charset_conv_failure"), "$_|_");
751 qstrf(sk, "%s", buf);
753 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
755 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
757 if(cfn->regstatus == FNNS_OP)
759 else if(cfn->regstatus == FNNS_REG)
765 qstrf(sk, "<%s V:%s,M:%c,H:%i/%i/%i,S:%i>",
766 (hub->dcppemu)?"++":"Dolda",
767 (hub->dcppemu)?"0.674":VERSION,
768 (tcpsock == NULL)?'P':'A',
770 confgetint("transfer", "slots")
773 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), hub->charset, "Charset_conv_failure"), "$_|_");
774 qstrf(sk, "%s\x01$", buf);
775 buf = tr(icswcstombs(confgetstr("dc", "email"), hub->charset, "Charset_conv_failure"), "$_|_");
776 qstrf(sk, "%s$", buf);
777 qstrf(sk, "%llu$|", sharesize);
780 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
785 if(!strcmp(cmd, "$Lock"))
787 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
788 } else if(!strcmp(cmd, "$Hello")) {
789 if(fn->state == FNN_HS)
791 qstrf(sk, "$Version 1,0091|");
792 qstrf(sk, "$GetNickList|");
794 fnetsetstate(fn, FNN_EST);
796 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
801 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
808 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
810 if((p = strchr(args, ' ')) != NULL)
815 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
817 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
819 key = dcmakekey(args);
820 qstrf(sk, "$Key %s|", key);
822 hubhandleaction(sk, fn, cmd, args);
825 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
831 if(hub->nativename == NULL)
832 free(hub->nativename);
833 hub->nativename = sstrdup(args);
834 buf = icmbstowcs(args, hub->charset);
835 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
838 hubhandleaction(sk, fn, cmd, args);
841 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
847 if((nick = icmbstowcs(args, hub->charset)) == NULL)
849 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
850 fnetaddpeer(fn, nick, nick);
852 hubhandleaction(sk, fn, cmd, args);
855 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
858 struct fnetpeer *peer;
862 if((nick = icmbstowcs(args, hub->charset)) == NULL)
864 if((peer = fnetfindpeer(fn, nick)) != NULL)
867 hubhandleaction(sk, fn, cmd, args);
870 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
875 struct fnetpeer *peer, *npeer;
878 for(peer = fn->peers; peer != NULL; peer = peer->next)
879 peer->flags.b.delete = 1;
880 while((p = strstr(args, "$$")) != NULL)
883 if((buf = icmbstowcs(args, hub->charset)) != NULL)
885 if((peer = fnetfindpeer(fn, buf)) == NULL)
886 peer = fnetaddpeer(fn, buf, buf);
888 peer->flags.b.delete = 0;
890 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
894 for(peer = fn->peers; peer != NULL; peer = npeer)
897 if(peer->flags.b.delete)
900 hubhandleaction(sk, fn, cmd, args);
903 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
908 struct fnetpeer *peer;
911 for(peer = fn->peers; peer != NULL; peer = peer->next)
912 peer->flags.b.op = 0;
913 while((p = strstr(args, "$$")) != NULL)
916 if((buf = icmbstowcs(args, hub->charset)) != NULL)
918 if((peer = fnetfindpeer(fn, buf)) != NULL)
919 peer->flags.b.op = 1;
924 hubhandleaction(sk, fn, cmd, args);
927 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
930 wchar_t *buf, *wp, *wp2;
932 struct fnetpeer *peer;
937 if(strncmp(p, "$ALL ", 5))
940 if((p2 = strchr(p, ' ')) == NULL)
943 if((buf = icmbstowcs(p, hub->charset)) == NULL)
945 if((peer = fnetfindpeer(fn, buf)) == NULL)
946 peer = fnetaddpeer(fn, buf, buf);
949 if((p2 = strstr(p, "$ $")) == NULL)
952 if((buf = icmbstowcs(p, hub->charset)) == NULL)
954 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcsrchr(buf, L'<')) != NULL))
956 buf[wcslen(buf) - 1] = L'\0';
958 if((wp2 = wcschr(wp, L' ')) != NULL)
961 fnetpeersetstr(peer, L"dc-client", wp);
965 if((wp2 = wcschr(wp, L',')) != NULL)
969 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
970 fnetpeersetstr(peer, abuf, wp + 2);
972 } while(wp2 != NULL);
975 fnetpeersetstr(peer, L"descr", buf);
978 if((p2 = strchr(p, '$')) == NULL)
981 if((buf = icmbstowcs(p, hub->charset)) == NULL)
983 fnetpeersetstr(peer, L"dc-speed", buf);
986 if((p2 = strchr(p, '$')) == NULL)
989 if((buf = icmbstowcs(p, hub->charset)) == NULL)
991 fnetpeersetstr(peer, L"email", buf);
996 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
997 hubhandleaction(sk, fn, cmd, args);
1000 /* I do not implement the fully in that I do not disconnect from the
1001 * old hub. I do this since I believe that if the hub owner really
1002 * wants to get rid of us, then it is his responsibility to disconnect
1004 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1007 struct fnetnode *newfn;
1011 if(strchr(args, ':') == NULL)
1013 args = strcpy(smalloc(strlen(args) + 5), args);
1014 strcat(args, ":411");
1019 if((newfn = fnetinitconnect(L"dc", args, NULL)) != NULL)
1021 linkfnetnode(newfn);
1024 hubhandleaction(sk, fn, cmd, args);
1029 static char *getdcpath(struct sharecache *node, size_t *retlen, char *charset)
1034 if(node->parent == NULL)
1036 if(node->parent == shareroot)
1038 if((buf = icwcstombs(node->name, charset)) == NULL)
1041 *retlen = strlen(buf);
1044 if((buf2 = icwcstombs(node->name, charset)) == NULL)
1046 if((buf = getdcpath(node->parent, &len, charset)) == NULL)
1051 len2 = strlen(buf2);
1052 buf = srealloc(buf, len + 1 + len2 + 1);
1054 strcpy(buf + len, buf2);
1055 buf[len + len2] = 0;
1058 *retlen = len + len2;
1064 * This is the main share searching function for Direct Connect
1065 * peers. Feel free to optimize it if you feel the need for it. I
1066 * haven't ever seen it take any noticable CPU time anyway, so I
1067 * haven't felt that need.
1069 * It may feel dubious to just return the directory when all terms are
1070 * satisfied rather than all the files in it, but it really benefits
1071 * everyone: Less cycles used for us, less UDP squelching for active
1072 * searchers, and less bandwidth waste for hubs when serving passive
1075 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1080 char *prefix, *infix, *postfix, *buf, *buf2;
1082 struct sockaddr_in addr;
1083 struct sharecache *node;
1084 int minsize, maxsize;
1086 int termnum, satisfied, skipcheck;
1087 int level, tersat[32];
1092 if((p = strchr(args, ' ')) == NULL)
1096 memset(terms, 0, sizeof(terms));
1097 prefix = infix = postfix = NULL;
1101 if(!strncmp(args, "Hub:", 4))
1103 if(!strcmp(cmd, "$MultiSearch"))
1105 if(!strcmp(args + 4, hub->nativenick))
1107 prefix = sprintf2("$SR %s ", hub->nativenick);
1108 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1109 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1113 if((p2 = strchr(args, ':')) == NULL)
1116 addr.sin_family = AF_INET;
1117 if(!inet_aton(args, &addr.sin_addr))
1119 addr.sin_port = htons(atoi(p2));
1120 prefix = sprintf2("$SR %s ", hub->nativenick);
1121 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1122 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1123 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1126 minsize = maxsize = -1;
1135 if((*(p++) == 'T') && (minsize == 0))
1142 if((p2 = strchr(p, '?')) == NULL)
1157 if((*p2 == 0) || (*p2 == '$'))
1165 if(!dotth && !strncmp(p, "TTH:", 4))
1168 if((buf = base32decode(p + 4, &buflen)) == NULL)
1172 memcpy(hashtth, buf, 24);
1175 if((terms[termnum] = icmbstowcs(p, hub->charset)) != NULL)
1186 node = shareroot->child;
1188 for(i = 0; i < termnum; i++)
1194 if(node->f.b.type == FILE_REG)
1196 if((minsize >= 0) && (node->size < minsize))
1198 if((maxsize >= 0) && (node->size > maxsize))
1201 if(!skipcheck && dotth)
1203 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1208 for(i = 0; i < termnum; i++)
1212 if(wcsexists(node->name, terms[i]))
1216 } else if(node->child == NULL) {
1221 if(!skipcheck && (satisfied == termnum))
1223 /* Use DCCHARSET in $Get paths until further researched... */
1224 if((buf = getdcpath(node, NULL, DCCHARSET)) != NULL)
1226 if(node->f.b.hastth)
1228 buf2 = base32encode(node->hashtth, 24);
1229 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1232 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1237 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1239 while(node->next == NULL)
1241 if((node = node->parent) == shareroot)
1245 if(node == shareroot)
1247 for(i = 0; i < termnum; i++)
1249 if(tersat[i] >= level)
1262 hubhandleaction(sk, fn, cmd, args);
1273 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1277 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1281 struct socket *newsk;
1282 struct sockaddr_in addr;
1285 if((p = strchr(args, ' ')) == NULL)
1288 if(strcmp(args, hub->nativenick))
1290 addr.sin_family = AF_INET;
1292 if((p = strchr(args, ':')) == NULL)
1295 addr.sin_port = htons(atoi(p));
1296 if(!inet_aton(args, &addr.sin_addr))
1298 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1300 hubhandleaction(sk, fn, cmd, args);
1303 static void sendctm(struct socket *sk, char *nick)
1305 struct sockaddr *addr;
1310 if(sockgetremotename(tcpsock, &addr, &addrlen) < 0)
1312 if(addr->sa_family == AF_INET)
1313 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1315 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1319 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1325 if((p = strchr(args, ' ')) == NULL)
1328 if(strcmp(p, hub->nativenick))
1331 expectpeer(args, fn);
1332 hubhandleaction(sk, fn, cmd, args);
1335 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1338 struct fnetnode *node;
1343 for(node = fnetnodes; node != NULL; node = node->next)
1345 if(node->state == FNN_EST)
1348 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1349 hubhandleaction(sk, fn, cmd, args);
1352 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1359 if((p2 = strchr(p, ' ')) == NULL)
1363 if((p2 = strchr(p, ' ')) == NULL)
1366 if(strcmp(p, "From:"))
1369 if((p2 = strstr(p, " $")) == NULL)
1373 hubrecvchat(fn->sk, fn, p, p2);
1374 hubhandleaction(sk, fn, cmd, args);
1377 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1381 char *nick, *filename, *hubname;
1385 wchar_t *wnick, *wfile;
1389 if((p2 = strchr(p, ' ')) == NULL)
1394 if((p2 = strchr(p, 5)) == NULL)
1398 if((p2 = strchr(p, ' ')) == NULL)
1403 if((p2 = strchr(p, '/')) == NULL)
1408 if((p2 = strchr(p, 5)) == NULL)
1412 if((p2 = strstr(p, " (")) == NULL)
1415 if((wnick = icmbstowcs(nick, hub->charset)) == NULL)
1417 /* Use DCCHARSET in $Get paths until further researched... */
1418 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1423 sr = newsrchres(&dcnet, wfile, wnick);
1424 if(sr->peernick != NULL)
1426 sr->peernick = swcsdup(wnick);
1431 if(!strncmp(hubname, "TTH:", 4))
1433 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1436 sr->hash = newhash(L"TTH", 24, buf);
1440 getfnetnode(sr->fn = fn);
1443 hubhandleaction(sk, fn, cmd, args);
1446 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1448 /* Do nothing for now. */
1451 static void cmd_getpass(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1458 pw = wpfind(fn->args, L"password");
1459 if((pw == NULL) || ((mbspw = icwcstombs(pw, hub->charset)) == NULL))
1464 qstrf(sk, "$MyPass %s|", mbspw);
1466 fn->regstatus = FNNS_REG;
1467 hubhandleaction(sk, fn, cmd, args);
1470 static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1475 fn->regstatus = FNNS_OP;
1476 hubhandleaction(sk, fn, cmd, args);
1479 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1481 struct dcexppeer *expect;
1484 if(peer->nativename != NULL)
1485 free(peer->nativename);
1486 peer->nativename = sstrdup(args);
1487 if(peer->wcsname != NULL)
1488 free(peer->wcsname);
1489 if((peer->wcsname = icmbstowcs(peer->nativename, peer->charset)) == NULL)
1496 for(expect = expected; expect != NULL; expect = expect->next)
1498 if(!strcmp(expect->nick, args))
1505 hub = expect->fn->data;
1506 peer->fn = expect->fn;
1507 getfnetnode(peer->fn);
1508 peer->dcppemu = hub->dcppemu;
1509 freeexppeer(expect);
1514 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1518 struct transfer *transfer;
1520 if((p = strchr(args, ' ')) == NULL)
1523 if(!strcmp(args, "Upload"))
1525 if(!strcmp(args, "Download"))
1529 if((peer->transfer == NULL) || (mydir != peer->direction))
1534 if(peer->direction == TRNSD_DOWN)
1537 if(peer->wcsname == NULL)
1542 peer->direction = mydir;
1543 if(peer->direction == TRNSD_UP)
1545 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1547 if((transfer = finddownload(peer->wcsname)) == NULL)
1552 transferattach(transfer, &dctransfer, peer);
1553 transfersetstate(transfer, TRNS_HS);
1555 transfersetnick(transfer, peer->wcsname);
1556 peer->transfer = transfer;
1559 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1560 if(peer->key != NULL)
1561 qstrf(sk, "$Key %s|", peer->key);
1562 if(peer->direction == TRNSD_DOWN)
1567 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1570 struct transfer *transfer;
1572 if((p = strchr(args, ' ')) == NULL)
1575 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1577 key = dcmakekey(args);
1580 if(peer->wcsname == NULL)
1589 if((transfer = finddownload(peer->wcsname)) == NULL)
1591 peer->direction = TRNSD_UP;
1592 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1594 peer->direction = TRNSD_DOWN;
1595 transferattach(transfer, &dctransfer, peer);
1596 transfersetstate(transfer, TRNS_HS);
1598 transfersetnick(transfer, peer->wcsname);
1599 peer->transfer = transfer;
1600 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1601 qstrf(sk, "$Key %s|", key);
1603 if(peer->key != NULL)
1609 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1614 static void startdl(struct dcpeer *peer)
1616 if(peer->timeout != NULL)
1617 canceltimer(peer->timeout);
1618 peer->state = PEER_TRNS;
1619 transferstartdl(peer->transfer, peer->sk);
1620 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1621 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1624 static void startul(struct dcpeer *peer)
1626 if(peer->timeout != NULL)
1627 canceltimer(peer->timeout);
1628 peer->state = PEER_TRNS;
1629 transferstartul(peer->transfer, peer->sk);
1630 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1633 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1636 struct transfer *transfer;
1638 if(peer->transfer == NULL)
1644 if(peer->transfer->size != size)
1646 transfersetsize(peer->transfer, size);
1647 transfer = peer->transfer;
1649 trytransferbypeer(transfer->fnet, transfer->peerid);
1653 qstr(peer->sk, "$Send|");
1656 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1658 if(peer->fetchingtthl)
1660 peer->fetchingtthl = 0;
1665 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1667 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1668 resettransfer(peer->transfer);
1674 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1676 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1678 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1679 resettransfer(peer->transfer);
1692 {"MyList.DcLst", &hmlistname, updatehmlist},
1693 {"files.xml", &xmllistname, updatexmllist},
1694 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1698 static int openfilelist(char *name)
1703 for(i = 0; lists[i].name != NULL; i++)
1705 if(!strcmp(name, lists[i].name))
1709 if(lists[i].name == NULL)
1712 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1714 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1717 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1724 static struct sharecache *findbytth(char *tth32)
1728 struct sharecache *node;
1730 if((buf = base32decode(tth32, &buflen)) == NULL)
1737 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1739 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1746 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1748 struct sharecache *node;
1755 if((p2 = strchr(p, sep)) != NULL)
1757 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1764 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1769 struct sharecache *node;
1770 struct socket *lesk;
1774 if(peer->transfer == NULL)
1779 if((p = strchr(args, '$')) == NULL)
1785 if((offset = (atoi(p) - 1)) < 0)
1790 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1792 qstr(sk, "$Error Could not send file list|");
1795 } else if(fd >= 0) {
1796 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1797 transfersetpath(peer->transfer, buf2);
1798 peer->transfer->flags.b.minislot = 1;
1802 /* Use DCCHARSET in $Get paths until further researched... */
1803 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1805 qstrf(sk, "$Error File not in share|");
1809 if((fd = opensharecache(node)) < 0)
1811 qstrf(sk, "$Error %s|", strerror(errno));
1815 buf = getfspath(node);
1816 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1817 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1819 transfersetpath(peer->transfer, buf2);
1822 if(fstat(fd, &sb) < 0)
1825 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1826 qstrf(sk, "$Error|");
1830 if(sb.st_size < 65536)
1831 peer->transfer->flags.b.minislot = 1;
1832 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1834 qstr(sk, "$MaxedOut|");
1838 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1841 qstrf(sk, "$Error Offset out of range|");
1845 lesk = wrapsock(fd);
1846 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1848 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1851 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1853 if(peer->transfer == NULL)
1858 if(peer->transfer->localend == NULL)
1867 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1872 size_t arrsize, arrdata;
1874 if(peer->supports != NULL)
1876 for(i = 0; peer->supports[i] != NULL; i++)
1877 free(peer->supports[i]);
1878 free(peer->supports);
1881 arrsize = arrdata = 0;
1885 if((p2 = strchr(p, ' ')) != NULL)
1889 addtobuf(arr, sstrdup(p));
1890 } while((p = p2) != NULL);
1891 addtobuf(arr, NULL);
1892 peer->supports = arr;
1895 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1899 int start, numbytes;
1900 char *charset, *buf;
1902 struct sharecache *node;
1904 struct socket *lesk;
1906 if(peer->transfer == NULL)
1912 if((p2 = strchr(p, ' ')) == NULL)
1920 if((p2 = strchr(p, ' ')) == NULL)
1928 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1931 /* Use DCCHARSET in $Get paths until further researched... */
1932 charset = DCCHARSET;
1933 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1934 initcompress(peer, CPRS_ZLIB);
1935 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1937 qstr(sk, "$Error Could not send file list|");
1939 } else if(fd >= 0) {
1940 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1941 transfersetpath(peer->transfer, buf2);
1942 peer->transfer->flags.b.minislot = 1;
1946 if((node = resdcpath(p, charset, '\\')) == NULL)
1948 qstr(sk, "$Error File not in cache|");
1951 if((fd = opensharecache(node)) < 0)
1953 qstrf(sk, "$Error %s|", strerror(errno));
1956 buf = getfspath(node);
1957 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1958 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1960 transfersetpath(peer->transfer, buf2);
1963 if(fstat(fd, &sb) < 0)
1966 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1967 qstr(sk, "$Error|");
1970 if(sb.st_size < 65536)
1971 peer->transfer->flags.b.minislot = 1;
1972 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
1974 qstr(sk, "$MaxedOut|");
1977 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1980 qstr(sk, "$Error Offset out of range|");
1983 if((numbytes < 0) || (start + numbytes > sb.st_size))
1984 numbytes = sb.st_size - start;
1985 lesk = wrapsock(fd);
1986 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1988 qstrf(sk, "$Sending %i|", numbytes);
1992 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1996 int start, numbytes;
1997 struct sharecache *node;
1999 struct socket *lesk;
2003 if(peer->transfer == NULL)
2008 if((argv = parseadc(args)) == NULL)
2013 if(parrlen(argv) < 4)
2018 start = atoi(argv[2]);
2019 numbytes = atoi(argv[3]);
2022 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
2024 qstr(sk, "$Error Could not send file list|");
2026 } else if(fd >= 0) {
2027 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
2028 transfersetpath(peer->transfer, wbuf);
2029 peer->transfer->flags.b.minislot = 1;
2033 if(!strncmp(argv[1], "TTH/", 4))
2035 if((node = findbytth(argv[1] + 4)) == NULL)
2037 qstr(sk, "$Error File not in cache|");
2041 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
2043 qstr(sk, "$Error File not in cache|");
2047 if((fd = opensharecache(node)) < 0)
2049 qstrf(sk, "$Error %s|", strerror(errno));
2052 buf = getfspath(node);
2053 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
2054 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2056 transfersetpath(peer->transfer, wbuf);
2059 if(!strcmp(argv[0], "file"))
2061 for(i = 4; argv[i] != NULL; i++)
2063 if(!strcmp(argv[i], "ZL1"))
2064 initcompress(peer, CPRS_ZLIB);
2066 if(fstat(fd, &sb) < 0)
2068 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2069 qstr(sk, "$Error|");
2072 if(sb.st_size < 65536)
2073 peer->transfer->flags.b.minislot = 1;
2074 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
2075 qstr(sk, "$MaxedOut|");
2078 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2080 qstr(sk, "$Error Offset out of range|");
2083 if((numbytes < 0) || (start + numbytes > sb.st_size))
2084 numbytes = sb.st_size - start;
2085 lesk = wrapsock(fd);
2086 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2089 qstr(sk, "$ADCSND");
2090 sendadc(sk, "file");
2091 sendadc(sk, argv[1]);
2092 sendadcf(sk, "%i", start);
2093 sendadcf(sk, "%i", numbytes);
2094 if(peer->compress == CPRS_ZLIB)
2098 } else if(!strcmp(argv[0], "tthl")) {
2100 * XXX: Implement full TTHL support.
2102 * In the meantime, this is better than nothing, since it at
2103 * least allows fetching of the TTH root if it isn't already
2108 qstr(sk, "$Error no TTHL data for virtual files|");
2111 qstr(sk, "$ADCSND");
2112 sendadc(sk, "tthl");
2113 sendadc(sk, argv[1]);
2117 sockqueue(sk, node->hashtth, 24);
2119 qstr(sk, "$Error Namespace not implemented|");
2129 static void handletthl(struct dcpeer *peer)
2133 while(peer->inbufdata >= 24)
2135 pushtigertree(&peer->tth, peer->inbuf);
2136 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2137 peer->curread += 24;
2139 if(peer->curread >= peer->totalsize)
2141 if(peer->timeout == NULL)
2142 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2143 peer->state = PEER_CMD;
2144 synctigertree(&peer->tth);
2145 restigertree(&peer->tth, buf);
2146 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2151 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2154 int start, numbytes;
2156 if(peer->transfer == NULL)
2161 if((argv = parseadc(args)) == NULL)
2166 if(parrlen(argv) < 4)
2171 start = atoi(argv[2]);
2172 numbytes = atoi(argv[3]);
2173 if(!strcmp(argv[0], "tthl"))
2175 if((start != 0) || (numbytes % 24 != 0))
2177 /* Weird. Bail out. */
2181 if(peer->timeout != NULL)
2182 canceltimer(peer->timeout);
2183 peer->state = PEER_TTHL;
2184 peer->totalsize = numbytes;
2186 peer->fetchingtthl = 0;
2187 inittigertree(&peer->tth);
2189 } else if(!strcmp(argv[0], "file")) {
2190 if(start != peer->transfer->curpos)
2195 if(start + numbytes != peer->transfer->size)
2197 transfersetsize(peer->transfer, start + numbytes);
2202 if(peer->inbufdata > 0)
2204 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2205 peer->inbufdata = 0;
2206 transread(sk, peer);
2209 /* We certainly didn't request this...*/
2218 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2222 if(peer->transfer == NULL)
2227 numbytes = atoi(args);
2228 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2230 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2235 if(peer->inbufdata > 0)
2237 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2238 peer->inbufdata = 0;
2239 transread(sk, peer);
2245 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2250 hubhandleaction(sk, fn, cmd, args);
2254 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2260 static int hubreqconn(struct fnetpeer *peer)
2265 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2270 if((hub = peer->fn->data) == NULL)
2275 if((mbsnick = icwcstombs(peer->id, hub->charset)) == NULL)
2276 return(1); /* Shouldn't happen, of course, but who knows... */
2279 sendctm(peer->fn->sk, mbsnick);
2280 expectpeer(mbsnick, peer->fn);
2282 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2288 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2291 char *mbsstring, *mbsto;
2294 if((mbsto = icwcstombs(to, hub->charset)) == NULL)
2299 if((mbsstring = icwcstombs(string, hub->charset)) == NULL)
2304 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2311 if(strchr(mbsstring, '|') != NULL)
2322 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2324 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2327 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2334 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2336 int minl, maxl, minr, maxr, retmin, retmax;
2341 findsizelimit(sexpr->l, &minl, &maxl);
2342 findsizelimit(sexpr->r, &minr, &maxr);
2343 retmin = (minl > minr)?minl:minr;
2344 if((maxl != -1) && (maxr != -1))
2345 retmax = (maxl < maxr)?maxl:maxr;
2354 findsizelimit(sexpr->l, &minl, &maxl);
2355 findsizelimit(sexpr->r, &minr, &maxr);
2356 retmin = (minl < minr)?minl:minr;
2357 if((maxl == -1) || (maxr == -1))
2360 retmax = (maxl > maxr)?maxl:maxr;
2363 findsizelimit(sexpr->l, &minl, &maxl);
2364 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2368 } else if((minl == 0) && (maxl != -1)) {
2371 } else if((minl != 0) && (maxl == -1)) {
2374 } else { /* This would yield two seperate intervals, which DC cannot handle */
2380 retmax = sexpr->d.n - 1;
2383 retmin = sexpr->d.n;
2384 retmax = sexpr->d.n;
2387 retmin = sexpr->d.n + 1;
2401 static struct hash *findsehash(struct sexpr *sexpr)
2403 struct hash *h1, *h2;
2408 if((h1 = findsehash(sexpr->l)) != NULL)
2410 if((h1 = findsehash(sexpr->r)) != NULL)
2414 h1 = findsehash(sexpr->l);
2415 h2 = findsehash(sexpr->r);
2420 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2421 return(sexpr->d.hash);
2428 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2431 struct wcslist *list, *cur;
2432 char *sstr, *buf, *p;
2433 size_t sstrsize, sstrdata;
2434 struct sockaddr *name;
2436 int minsize, maxsize;
2440 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2442 list = findsexprstrs(srch->sexpr);
2443 findsizelimit(srch->sexpr, &minsize, &maxsize);
2444 hash = findsehash(srch->sexpr);
2445 if((minsize != 0) && (maxsize != -1))
2447 /* Choose either minsize or maxsize by trying to determine
2448 * which choice will be most restrictive. The result can be
2449 * approximative at best anyway, so don't try too hard... */
2450 if((50000000 - maxsize) > (minsize - 50000000))
2456 sstrsize = sstrdata = 0;
2457 if((hash != NULL) && (hash->len == 24))
2459 /* Prioritize hash searches above all else */
2460 bufcat(sstr, "F?T?0?9?TTH:", 12);
2461 buf = base32encode(hash->buf, hash->len);
2463 bufcat(sstr, buf, 39);
2468 sizebuf2(sstr, sstrdata + 32, 1);
2469 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2470 } else if(maxsize != -1) {
2471 sizebuf2(sstr, sstrdata + 32, 1);
2472 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2474 bufcat(sstr, "F?F?0?1?", 8);
2478 for(cur = list; cur != NULL; cur = cur->next)
2480 /* Use DCCHARSET in $Get paths until further researched... */
2481 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2483 /* Can't find anything anyway if the search expression
2484 * requires characters outside DC's charset. There's
2485 * nothing technically wrong with the search itself,
2486 * however, so return success. This should be
2487 * considered as an optimization. */
2494 addtobuf(sstr, '$');
2496 * It probably doesn't hurt if buf contains any extra
2497 * dollar signs - it will just result in extra search
2498 * terms, and the extraneous results will be filtered by
2499 * the search layer anyway. It hurts if it contains any
2500 * pipes, though, so let's sell them for money.
2502 for(p = buf; *p; p++)
2507 bufcat(sstr, buf, strlen(buf));
2511 /* Will match all files... :-/ */
2512 addtobuf(sstr, '.');
2518 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2520 flog(LOG_WARNING, "cannot get address of UDP socket");
2522 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2526 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2530 fn->lastsrch = time(NULL);
2538 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2539 struct command hubcmds[] =
2541 {"$Lock", cc(cmd_lock)},
2542 {"$HubName", cc(cmd_hubname)},
2543 {"$Hello", cc(cmd_hello)},
2544 {"$Quit", cc(cmd_quit)},
2545 {"$NickList", cc(cmd_nicklist)},
2546 {"$OpList", cc(cmd_oplist)},
2547 {"$MyINFO", cc(cmd_myinfo)},
2548 {"$ForceMove", cc(cmd_forcemove)},
2549 {"$Search", cc(cmd_search)},
2550 {"$MultiSearch", cc(cmd_search)},
2551 {"$ConnectToMe", cc(cmd_connecttome)},
2552 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2553 {"$GetNetInfo", cc(cmd_getnetinfo)},
2554 {"$To:", cc(cmd_to)},
2555 {"$SR", cc(cmd_sr)},
2556 {"$UserCommand", cc(cmd_usercommand)},
2557 {"$GetPass", cc(cmd_getpass)},
2558 {"$LogedIn", cc(cmd_logedin)}, /* sic */
2562 struct command peercmds[] =
2564 {"$MyNick", cc(cmd_mynick)},
2565 {"$Lock", cc(cmd_peerlock)},
2566 {"$Direction", cc(cmd_direction)},
2567 {"$Key", cc(cmd_key)},
2568 {"$FileLength", cc(cmd_filelength)},
2569 {"$Error", cc(cmd_error)},
2570 {"$MaxedOut", cc(cmd_maxedout)},
2571 {"$Get", cc(cmd_get)},
2572 {"$Send", cc(cmd_send)},
2573 {"$Supports", cc(cmd_supports)},
2574 {"$GetBlock", cc(cmd_getblock)},
2575 {"$UGetBlock", cc(cmd_getblock)},
2576 {"$GetZBlock", cc(cmd_getblock)},
2577 {"$UGetZBlock", cc(cmd_getblock)},
2578 {"$ADCGET", cc(cmd_adcget)},
2579 {"$ADCSND", cc(cmd_adcsnd), 1},
2580 {"$Sending", cc(cmd_sending), 1},
2585 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2587 CBUNREG(transfer, trans_filterout, peer);
2590 peer->transfer = NULL;
2594 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2602 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2604 if(sockqueuesize(peer->sk) < 65536)
2606 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2608 if(peer->compress == CPRS_NONE)
2610 sockqueue(peer->sk, buf, bufsize);
2611 } else if(peer->compress == CPRS_ZLIB) {
2612 cstr = peer->cprsdata;
2613 cstr->next_in = buf;
2614 cstr->avail_in = bufsize;
2615 while(cstr->avail_in > 0)
2617 cstr->next_out = outbuf;
2618 cstr->avail_out = sizeof(outbuf);
2619 if((ret = deflate(cstr, 0)) != Z_OK)
2621 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2625 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2630 if(peer->state == PEER_SYNC)
2632 if(peer->compress == CPRS_ZLIB)
2634 cstr = peer->cprsdata;
2635 cstr->next_in = NULL;
2639 cstr->next_out = outbuf;
2640 cstr->avail_out = sizeof(outbuf);
2641 ret = deflate(cstr, Z_FINISH);
2642 if((ret != Z_OK) && (ret != Z_STREAM_END))
2644 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2648 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2649 } while(ret != Z_STREAM_END);
2655 if(peer->timeout == NULL)
2656 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
2657 peer->state = PEER_CMD;
2659 transfersetstate(transfer, TRNS_HS);
2660 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2661 transfer->flags.b.minislot = 0;
2662 peer->sk->writecb = NULL;
2669 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2671 peer->state = PEER_SYNC;
2672 dctransgotdata(transfer, peer);
2675 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2677 if(transferdatasize(transfer) < 65536)
2678 peer->sk->ignread = 0;
2681 static void transread(struct socket *sk, struct dcpeer *peer)
2685 struct transfer *transfer;
2687 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2689 if(peer->transfer == NULL)
2695 transferputdata(peer->transfer, buf, bufsize);
2697 if(peer->transfer->curpos >= peer->transfer->size)
2699 transfer = peer->transfer;
2700 transferdetach(transfer);
2701 transferendofdata(transfer);
2704 if(transferdatasize(peer->transfer) > 65535)
2708 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2710 struct transfer *transfer;
2712 if((transfer = peer->transfer) == NULL)
2717 transferdetach(transfer);
2718 transferendofdata(transfer);
2721 static void transwrite(struct socket *sk, struct dcpeer *peer)
2723 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2725 if(peer->transfer == NULL)
2730 dctransgotdata(peer->transfer, peer);
2733 static void udpread(struct socket *sk, void *data)
2735 char *buf, *p, *p2, *hashbuf;
2736 size_t buflen, hashlen;
2737 char *nick, *filename, *hubname;
2738 struct sockaddr_in hubaddr;
2740 struct fnetnode *fn, *myfn;
2743 wchar_t *wnick, *wfile;
2746 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2748 buf = srealloc(buf, buflen + 1);
2750 if(!strncmp(buf, "$SR ", 4))
2754 if((p2 = strchr(p, ' ')) == NULL)
2762 if((p2 = strchr(p, 5)) == NULL)
2769 if((p2 = strchr(p, ' ')) == NULL)
2777 if((p2 = strchr(p, '/')) == NULL)
2785 if((p2 = strchr(p, 5)) == NULL)
2792 if((p2 = strstr(p, " (")) == NULL)
2799 if((p2 = strchr(p, ':')) == NULL)
2805 hubaddr.sin_family = AF_INET;
2806 if(!inet_aton(p, &hubaddr.sin_addr))
2812 if((p2 = strchr(p, ')')) == NULL)
2818 hubaddr.sin_port = htons(atoi(p));
2819 /* Use DCCHARSET in $Get paths until further researched... */
2820 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2827 if(!strncmp(hubname, "TTH:", 4))
2829 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2832 hash = newhash(L"TTH", 24, hashbuf);
2836 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2838 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2840 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2855 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2857 if((fn->fnet == &dcnet) && (fn->sk != NULL) && addreq(fn->sk->remote, (struct sockaddr *)&hubaddr))
2867 if((wnick = icmbstowcs(nick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
2872 sr = newsrchres(&dcnet, wfile, wnick);
2873 if(sr->peernick != NULL)
2875 sr->peernick = swcsdup(wnick);
2881 getfnetnode(sr->fn = myfn);
2890 static void hubread(struct socket *sk, struct fnetnode *fn)
2897 hub = (struct dchub *)fn->data;
2898 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2900 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2902 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2903 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2905 p = hub->inbuf + hub->inbufdata;
2906 hub->inbufdata += datalen;
2907 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2910 newqcmd(&hub->queue, hub->inbuf);
2911 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2912 datalen = hub->inbufdata;
2917 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2922 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2928 if((buf = icwcstombs(newnick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
2930 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2935 if(hub == NULL) /* Not yet connected */
2940 if(hub->nativenick != NULL)
2941 free(hub->nativenick);
2942 hub->nativenick = buf;
2946 static struct dchub *newdchub(struct fnetnode *fn)
2953 new = smalloc(sizeof(*new));
2954 memset(new, 0, sizeof(*new));
2956 if(confgetint("dc", "dcppemu"))
2958 if((emu = wpfind(fn->args, L"dcppemu")) != NULL)
2966 if((wcharset = wpfind(fn->args, L"charset")) != NULL)
2968 if((charset = icwcstombs(wcharset, "US-ASCII")) != NULL)
2970 if(!havecharset(charset))
2978 new->charset = charset;
2980 new->charset = sstrdup(DCCHARSET);
2981 if(hubsetnick(fn, fn->mynick))
2982 fnetsetnick(fn, L"DoldaConnectUser-IN");
2983 /* IN as in Invalid Nick */
2987 static struct dcpeer *newdcpeer(struct socket *sk)
2991 new = smalloc(sizeof(*new));
2992 memset(new, 0, sizeof(*new));
2993 new->transfer = NULL;
2996 if(confgetint("dc", "dcppemu"))
3007 static void freedcpeer(struct dcpeer *peer)
3010 struct qcommand *qcmd;
3015 if(peer->next != NULL)
3016 peer->next->prev = peer->prev;
3017 if(peer->prev != NULL)
3018 peer->prev->next = peer->next;
3019 if(peer->transfer != NULL)
3021 if(peer->transfer->dir == TRNSD_UP)
3022 peer->transfer->close = 1;
3023 if(peer->transfer->dir == TRNSD_DOWN)
3024 resettransfer(peer->transfer);
3025 transferdetach(peer->transfer);
3027 if(peer->timeout != NULL)
3028 canceltimer(peer->timeout);
3029 if(peer->sk->data == peer)
3030 peer->sk->data = NULL;
3031 peer->sk->readcb = NULL;
3032 peer->sk->writecb = NULL;
3033 peer->sk->errcb = NULL;
3036 if(peer->supports != NULL)
3038 for(i = 0; peer->supports[i] != NULL; i++)
3039 free(peer->supports[i]);
3040 free(peer->supports);
3042 if(peer->inbuf != NULL)
3044 if(peer->key != NULL)
3046 if(peer->wcsname != NULL)
3047 free(peer->wcsname);
3048 if(peer->nativename != NULL)
3049 free(peer->nativename);
3050 if(peer->charset != NULL)
3051 free(peer->charset);
3052 if(peer->fn != NULL)
3053 putfnetnode(peer->fn);
3054 while((qcmd = ulqcmd(&peer->queue)) != NULL)
3060 static void hubconnect(struct fnetnode *fn)
3062 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
3063 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
3065 fn->data = newdchub(fn);
3070 static void hubdestroy(struct fnetnode *fn)
3073 struct qcommand *qcmd;
3075 hub = (struct dchub *)fn->data;
3078 if(fn->sk->data == fn)
3080 fn->sk->data = NULL;
3086 while((qcmd = ulqcmd(&hub->queue)) != NULL)
3088 if(hub->nativename != NULL)
3089 free(hub->nativename);
3090 if(hub->nativenick != NULL)
3091 free(hub->nativenick);
3092 if(hub->charset != NULL)
3094 if(hub->inbuf != NULL)
3099 static wchar_t *dcbasename(wchar_t *filename)
3103 if((ret = wcsrchr(filename, L'\\')) != NULL)
3108 static struct transferiface dctransfer =
3110 .detach = (void (*)(struct transfer *, void *))dctransdetach,
3111 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
3112 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
3113 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
3116 static struct fnet dcnet =
3119 .connect = hubconnect,
3120 .destroy = hubdestroy,
3121 .setnick = hubsetnick,
3122 .reqconn = hubreqconn,
3123 .sendchat = hubsendchat,
3124 .search = hubsearch,
3125 .filebasename = dcbasename
3128 static void peerread(struct socket *sk, struct dcpeer *peer)
3132 struct command *cmd;
3134 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
3136 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
3137 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
3139 peer->inbufdata += datalen;
3140 if(peer->state == PEER_CMD)
3143 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
3146 newqcmd(&peer->queue, peer->inbuf);
3147 for(cmd = peercmds; cmd->handler != NULL; cmd++)
3149 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
3152 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
3155 peer->state = PEER_STOP;
3159 } else if(peer->state == PEER_TTHL) {
3164 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3169 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3171 struct dcpeer *peer;
3180 peer = newdcpeer(sk);
3183 peer->dcppemu = hub->dcppemu;
3184 sk->readcb = (void (*)(struct socket *, void *))peerread;
3185 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3187 socksettos(sk, confgetint("fnet", "fnptos"));
3189 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3194 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3196 struct dcpeer *peer;
3198 peer = newdcpeer(newsk);
3200 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3201 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3203 socksettos(newsk, confgetint("fnet", "fnptos"));
3204 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3207 static void updatehmlist(void)
3209 int i, lev, ic, ret;
3210 struct sharecache *node;
3211 char *buf, *buf2, numbuf[32];
3212 size_t bufsize, bufdata;
3216 buf = smalloc(bufsize = 65536);
3217 node = shareroot->child;
3222 /* Use DCCHARSET in $Get paths until further researched... */
3223 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3225 for(i = 0; i < lev; i++)
3227 bufcat(buf, buf2, strlen(buf2));
3229 if(node->f.b.type == FILE_REG)
3232 sprintf(numbuf, "%i", node->size);
3233 bufcat(buf, numbuf, strlen(numbuf));
3240 if((node->child != NULL) && !ic)
3244 } else if(node->next != NULL) {
3247 while(node->next == NULL)
3250 node = node->parent;
3251 if(node == shareroot)
3254 if(node == shareroot)
3259 if(hmlistname != NULL)
3264 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3265 if((fd = mkstemp(hmlistname)) < 0)
3267 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3272 * I do not want to implement a good Huffman encoder, and it's not
3273 * like Huffman encoding actually yields any impressive results
3274 * for DC file lists anyway, so I'll just output a bogus
3275 * tree. Implement a good encoder if you want to.
3277 write(fd, "HE3\r\0", 5);
3278 write(fd, &bufdata, 4);
3280 write(fd, &ibuf, 2);
3282 for(i = 0; i < 256; i++)
3285 write(fd, &ibuf, 1);
3287 for(i = 0; i < 256; i++)
3289 for(buf2 = buf; bufdata > 0;)
3291 if((ret = write(fd, buf2, bufdata)) < 0)
3293 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3304 static struct xmlent
3311 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3315 static wchar_t *escapexml(wchar_t *src)
3317 static wchar_t *buf = NULL;;
3320 size_t bufsize, bufdata;
3326 bufsize = bufdata = 0;
3327 for(; *src != L'\0'; src++)
3330 if((c > 0) && (c < 32))
3332 bufcat(buf, L"&#", 2);
3333 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3334 bufcat(buf, nbuf, ret);
3335 addtobuf(buf, L';');
3337 for(ent = entities; ent->ent != NULL; ent++)
3341 bufcat(buf, ent->ent, wcslen(ent->ent));
3345 if(ent->ent == NULL)
3346 addtobuf(buf, *src);
3349 addtobuf(buf, L'\0');
3353 static void updatexmllist(void)
3357 char cidbuf[14], *namebuf;
3359 struct sharecache *node;
3361 if(xmllistname != NULL)
3363 unlink(xmllistname);
3366 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3367 if((fd = mkstemp(xmllistname)) < 0)
3369 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3374 if((fs = fdopen(fd, "w")) == NULL)
3376 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3377 unlink(xmllistname);
3383 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3384 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3385 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3387 if(confgetint("dc", "dcppemu"))
3388 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"DC++ 0.674\">\r\n", cidbuf);
3390 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, "DoldaConnect" VERSION);
3392 node = shareroot->child;
3396 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3398 for(i = 0; i < lev; i++)
3400 if(node->child != NULL)
3402 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3407 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3408 if(node->f.b.hastth)
3410 hashbuf = base32encode(node->hashtth, 24);
3411 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3414 fprintf(fs, "/>\r\n");
3416 while(node->next == NULL)
3418 node = node->parent;
3419 if(node == shareroot)
3424 for(i = 0; i < lev; i++)
3426 fprintf(fs, "</Directory>\r\n");
3429 if(node == shareroot)
3435 if(confgetint("dc", "dcppemu"))
3436 fprintf(fs, "</FileListing>");
3438 fprintf(fs, "</FileListing>\r\n");
3442 static void updatexmlbz2list(void)
3451 if(xmllistname == NULL)
3453 if((in = fopen(xmllistname, "r")) == NULL)
3455 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3458 if(xmlbz2listname != NULL)
3460 unlink(xmlbz2listname);
3461 free(xmlbz2listname);
3463 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3464 if((fd = mkstemp(xmlbz2listname)) < 0)
3466 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3467 free(xmlbz2listname);
3468 xmlbz2listname = NULL;
3472 if((real = fdopen(fd, "w")) == NULL)
3474 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3476 unlink(xmlbz2listname);
3477 free(xmlbz2listname);
3478 xmlbz2listname = NULL;
3482 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3485 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3487 unlink(xmlbz2listname);
3488 free(xmlbz2listname);
3489 xmlbz2listname = NULL;
3495 bufdata = fread(buf, 1, sizeof(buf), in);
3496 BZ2_bzWrite(&err, out, buf, bufdata);
3499 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3503 static int shareupdate(unsigned long long uusharesize, void *data)
3511 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3514 struct command *cmd;
3516 if((p = strchr(qcmd->string, ' ')) != NULL)
3518 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3520 if(!strcmp(cmd->name, qcmd->string))
3523 if(cmd->handler != NULL)
3524 cmd->handler(sk, data, qcmd->string, p);
3527 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3531 static int run(void)
3533 struct fnetnode *fn, *nextfn;
3535 struct dcpeer *peer, *nextpeer;
3536 struct qcommand *qcmd;
3540 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3543 if(fn->fnet != &dcnet)
3545 if(fn->data == NULL)
3547 hub = (struct dchub *)fn->data;
3548 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3550 if(*qcmd->string == '$')
3552 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3553 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3554 } else if(*qcmd->string != 0) {
3555 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3562 for(peer = peers; peer != NULL; peer = nextpeer)
3564 nextpeer = peer->next;
3565 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3567 if(peer->timeout != NULL)
3568 canceltimer(peer->timeout);
3569 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
3570 if(*qcmd->string == '$')
3571 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3580 static void preinit(int hup)
3587 static int updateudpport(struct configvar *var, void *uudata)
3589 struct sockaddr_in addr;
3590 struct socket *newsock;
3592 memset(&addr, 0, sizeof(addr));
3593 addr.sin_family = AF_INET;
3594 addr.sin_port = htons(var->val.num);
3595 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3597 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3600 newsock->readcb = udpread;
3607 static int updatetcpport(struct configvar *var, void *uudata)
3609 struct sockaddr_in addr;
3610 struct socket *newsock;
3612 memset(&addr, 0, sizeof(addr));
3613 addr.sin_family = AF_INET;
3614 addr.sin_port = htons(var->val.num);
3615 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3616 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3623 static int init(int hup)
3625 struct sockaddr_in addr;
3629 GCBREG(sharechangecb, shareupdate, NULL);
3634 addr.sin_family = AF_INET;
3635 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3636 addr.sin_port = htons(confgetint("dc", "udpport"));
3637 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3639 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3642 udpsock->readcb = udpread;
3643 addr.sin_port = htons(confgetint("dc", "tcpport"));
3644 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3645 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3646 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3647 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3648 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3653 static void terminate(void)
3655 if(hmlistname != NULL)
3660 if(xmllistname != NULL)
3662 unlink(xmllistname);
3665 if(xmlbz2listname != NULL)
3667 unlink(xmlbz2listname);
3668 free(xmlbz2listname);
3672 static struct configvar myvars[] =
3674 {CONF_VAR_STRING, "desc", {.str = L""}},
3675 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3676 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3677 {CONF_VAR_INT, "udpport", {.num = 0}},
3678 {CONF_VAR_INT, "tcpport", {.num = 0}},
3679 {CONF_VAR_BOOL, "dcppemu", {.num = 0}},
3683 static struct module me =
3692 .terminate = terminate,