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;
116 struct dcexppeer *next, *prev;
119 struct timer *expire;
124 struct dcpeer *next, *prev;
128 size_t inbufdata, inbufsize;
129 size_t curread, totalsize;
131 struct qcommand *queue;
132 struct transfer *transfer;
134 int ptclose; /* Close after transfer is complete */
135 int accepted; /* If false, we connected, otherwise, we accepted */
137 int direction; /* Using the constants from transfer.h */
139 int hascurpos, notthl;
140 struct tigertreehash tth;
148 static struct fnet dcnet;
149 static struct transferiface dctransfer;
150 static struct socket *udpsock = NULL;
151 static struct socket *tcpsock = NULL;
152 static struct dcpeer *peers = NULL;
154 static struct dcexppeer *expected = NULL;
155 static char *hmlistname = NULL;
156 static char *xmllistname = NULL;
157 static char *xmlbz2listname = NULL;
159 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn);
160 static void freedcpeer(struct dcpeer *peer);
161 static void transread(struct socket *sk, struct dcpeer *peer);
162 static void transerr(struct socket *sk, int err, struct dcpeer *peer);
163 static void transwrite(struct socket *sk, struct dcpeer *peer);
164 static void updatehmlist(void);
165 static void updatexmllist(void);
166 static void updatexmlbz2list(void);
167 static void requestfile(struct dcpeer *peer);
169 static int reservedchar(unsigned char c)
171 return((c == 0) || (c == 5) || (c == 124) || (c == 96) || (c == 126) || (c == 36));
174 /* Oh, how I despise having to do this... */
175 static char *dcmakekey(char *lock)
181 buf = smalloc(strlen(lock));
184 for(i = 0; lock[i]; i++)
186 buf[i] = lock[i] ^ save;
187 buf[i] = ((buf[i] & 0x0F) << 4) | ((buf[i] & 0xF0) >> 4);
189 if((i != 0) && reservedchar(buf[i]))
194 buf[0] ^= buf[i - 1];
195 if(reservedchar(buf[0]))
199 key = smalloc(len + 1);
201 for(i = 0; lock[i] != 0; i++)
203 if(reservedchar(buf[i]))
204 offset += sprintf(key + offset, "/%%DCN%03i%%/", buf[i]);
206 key[offset++] = buf[i];
213 static char *pathnmdc2adc(char *path)
216 size_t retsize, retdata;
218 if(!strcmp(path, "files.xml") || !strcmp(path, "files.xml.bz2") || !strcmp(path, "MyList.DcLst"))
219 return(sstrdup(path));
221 retsize = retdata = 0;
228 addtobuf(ret, *path);
234 static int isdchash(struct hash *hash)
236 if(wcscmp(hash->algo, L"TTH"))
243 static int supports(struct dcpeer *peer, char *cap)
247 if(peer->supports == NULL)
249 for(p = peer->supports; *p != NULL; p++)
251 if(!strcasecmp(*p, cap))
257 static void endcompress(struct dcpeer *peer)
259 if(peer->compress == CPRS_ZLIB)
261 deflateEnd(peer->cprsdata);
262 free(peer->cprsdata);
264 peer->compress = CPRS_NONE;
267 static void initcompress(struct dcpeer *peer, int algo)
272 peer->compress = algo;
273 if(algo == CPRS_ZLIB)
275 peer->cprsdata = smalloc(sizeof(z_stream));
276 memset(peer->cprsdata, 0, sizeof(z_stream));
277 if((ret = deflateInit(peer->cprsdata, 3)) != Z_OK)
279 flog(LOG_CRIT, "Aiya! zlib refuses to init (%i)!", ret);
285 static void unquote(wchar_t *in)
289 for(p = in; *p != L'\0'; p++)
293 for(p2 = p + 1; (*p2 != L'\0') && (*p2 != L';') && (*p2 != L'&'); p2++);
300 if(!wcscmp(p + 1, L"amp"))
303 } else if(p[1] == L'#') {
304 nc = ucptowc(wcstol(p + 2, NULL, 10));
313 memmove(p + 1, p2 + 1, (wcslen(p2 + 1) + 1) * sizeof(wchar_t));
318 static void freeexppeer(struct dcexppeer *ep)
321 ep->next->prev = ep->prev;
323 ep->prev->next = ep->next;
328 if(ep->expire != NULL)
329 canceltimer(ep->expire);
333 static void exppeerexpire(int cancelled, struct dcexppeer *ep)
340 static struct dcexppeer *expectpeer(char *nick, struct fnetnode *fn)
342 struct dcexppeer *ep;
344 ep = smalloc(sizeof(*ep));
345 ep->nick = sstrdup(nick);
346 getfnetnode(ep->fn = fn);
347 ep->expire = timercallback(ntime() + 300, (void (*)(int, void *))exppeerexpire, ep);
356 static struct qcommand *newqcmd(struct qcommand **queue, char *string)
358 struct qcommand *new;
360 while(*queue != NULL)
361 queue = &(*queue)->next;
362 new = smalloc(sizeof(*new));
363 new->string = sstrdup(string);
369 static struct qcommand *ulqcmd(struct qcommand **queue)
371 struct qcommand *qcmd;
373 if((qcmd = *queue) == NULL)
379 static void freeqcmd(struct qcommand *qcmd)
385 static void hubrecvchat(struct socket *sk, struct fnetnode *fn, char *from, char *string)
387 wchar_t *chat, *wfrom, *wpeer;
389 struct fnetpeer *peer;
391 end = string + strlen(string);
392 while((p = strchr(string, 13)) != NULL)
393 memmove(p, p + 1, (end-- - p));
396 if((strlen(string) > strlen(from) + 2) && (*string == '<') && !memcmp(string + 1, from, strlen(from)) && (*(string + strlen(from) + 1) == '>'))
397 string += strlen(from) + 2;
398 if((wfrom = icmbstowcs(from, DCCHARSET)) == NULL)
400 wpeer = swcsdup(wfrom);
406 for(p = string + 1; *p; p++)
408 if((*p == ' ') || (*p == '>'))
416 if((wpeer = icmbstowcs(string + 1, DCCHARSET)) == NULL)
422 wpeer = swcsdup(L"");
424 if((chat = icmbstowcs(string, DCCHARSET)) == NULL)
434 if((peer = fnetfindpeer(fn, wfrom)) == NULL) /* Assume public chat */
435 fnethandlechat(fn, 1, wfrom, wpeer, chat);
437 fnethandlechat(fn, 0, wfrom, wpeer, chat);
439 fnethandlechat(fn, 1, L"", wpeer, chat);
447 static void sendadc(struct socket *sk, char *arg)
450 size_t bufsize, bufdata;
453 bufsize = bufdata = 0;
459 bufcat(buf, "\\s", 2);
460 } else if(*arg == '\n') {
461 bufcat(buf, "\\n", 2);
462 } else if(*arg == '\\') {
463 bufcat(buf, "\\\\", 2);
468 sockqueue(sk, buf, bufdata);
472 static void sendadcf(struct socket *sk, char *arg, ...)
478 buf = vsprintf2(arg, args);
486 static char **parseadc(char *args)
489 size_t retbufsize, retbufdata;
491 size_t bufsize, bufdata;
496 retbufsize = retbufdata = bufsize = bufdata = 0;
505 else if(*args != ' ')
509 if((*args == ' ') || (*args == 0))
512 addtobuf(retbuf, buf);
514 bufsize = bufdata = 0;
517 } else if(*args == '\\') {
520 addtobuf(buf, *args);
529 addtobuf(retbuf, NULL);
532 } else if((*args == 's') || (*args == ' ')) {
534 } else if(*args == 'n') {
536 } else if(*args == '\\') {
546 addtobuf(retbuf, NULL);
550 /* Macros useful in command handlers */
551 #define skipspace(s) ({if(((s) = strchr((s), ' ')) == NULL) return; else (s)++;})
552 #define qstr(sk, str) sockqueue(sk, str, strlen(str))
553 #define qstrf(sk, strandargs...) \
556 if((__buf__ = sprintf2(strandargs)) != NULL) { \
557 sockqueue(sk, __buf__, strlen(__buf__)); \
562 static char *tr(char *str, char *trans)
566 for(; *trans; trans += 2)
568 for(p = strchr(str, trans[0]); p != NULL; p = strchr(p, trans[0]))
574 static char *getadcid(struct dcpeer *peer)
579 if((peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
581 buf = base32encode(peer->transfer->hash->buf, 24);
582 ret = sprintf2("TTH/%.39s", buf);
585 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
587 ret = pathnmdc2adc(buf);
594 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
596 if(!wcscmp(cmd, L"resume"))
600 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
603 transfer->curpos = wcstol(arg, NULL, 10);
612 static void sendmynick(struct dcpeer *peer)
616 hub = (peer->fn == NULL)?NULL:(peer->fn->data);
618 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), DCCHARSET, "DoldaConnectUser-IN"));
620 qstrf(peer->sk, "$MyNick %s|", hub->nativenick);
623 static void sendpeerlock(struct dcpeer *peer)
625 #ifdef DCPP_MASQUERADE
626 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
628 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
632 static void sendsupports(struct dcpeer *peer)
634 #ifdef DCPP_MASQUERADE
635 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
637 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
641 static void requestfile(struct dcpeer *peer)
645 if(peer->transfer->size == -1)
647 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
649 transferseterror(peer->transfer, TRNSE_NOTFOUND);
653 /* The transfer will be restarted later from
654 * cmd_filelength when it detects that the sizes
656 qstrf(peer->sk, "$Get %s$1|", buf);
659 if((peer->transfer->hash == NULL) && !peer->notthl)
661 if(supports(peer, "adcget") && supports(peer, "tthl"))
663 qstr(peer->sk, "$ADCGET");
664 sendadc(peer->sk, "tthl");
665 if((buf = getadcid(peer)) == NULL)
667 transferseterror(peer->transfer, TRNSE_NOTFOUND);
671 sendadc(peer->sk, buf);
673 sendadc(peer->sk, "0");
674 sendadc(peer->sk, "-1");
681 if(forkfilter(peer->transfer))
683 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
687 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
690 if(supports(peer, "adcget"))
692 qstr(peer->sk, "$ADCGET");
693 sendadc(peer->sk, "file");
694 if((buf = getadcid(peer)) == NULL)
696 transferseterror(peer->transfer, TRNSE_NOTFOUND);
700 sendadc(peer->sk, buf);
702 sendadcf(peer->sk, "%i", peer->transfer->curpos);
703 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
705 } else if(supports(peer, "xmlbzlist")) {
706 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
708 transferseterror(peer->transfer, TRNSE_NOTFOUND);
712 qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
714 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
716 transferseterror(peer->transfer, TRNSE_NOTFOUND);
720 qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1);
724 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
728 struct fnetnode *cfn;
732 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
733 buf = tr(icswcstombs(confgetstr("dc", "desc"), DCCHARSET, "Charset_conv_failure"), "$_|_");
734 qstrf(sk, "%s", buf);
736 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
738 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
741 qstrf(sk, "<%s V:%s,M:%c,H:%i/0/0,S:%i>",
744 (tcpsock == NULL)?'P':'A',
746 confgetint("transfer", "slots")
749 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), DCCHARSET, "Charset_conv_failure"), "$_|_");
750 qstrf(sk, "%s\x01$", buf);
751 buf = tr(icswcstombs(confgetstr("dc", "email"), DCCHARSET, "Charset_conv_failure"), "$_|_");
752 qstrf(sk, "%s$", buf);
753 qstrf(sk, "%llu$|", sharesize);
756 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
761 if(!strcmp(cmd, "$Lock"))
763 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
764 } else if(!strcmp(cmd, "$Hello")) {
765 if(fn->state == FNN_HS)
767 qstrf(sk, "$Version 1,0091|");
768 qstrf(sk, "$GetNickList|");
770 fnetsetstate(fn, FNN_EST);
772 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
777 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
784 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
786 if((p = strchr(args, ' ')) != NULL)
790 #ifdef DCPP_MASQUERADE
791 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
793 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
796 key = dcmakekey(args);
797 qstrf(sk, "$Key %s|", key);
799 hubhandleaction(sk, fn, cmd, args);
802 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
808 if(hub->nativename == NULL)
809 free(hub->nativename);
810 hub->nativename = sstrdup(args);
811 buf = icmbstowcs(args, DCCHARSET);
812 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
815 hubhandleaction(sk, fn, cmd, args);
818 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
824 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
826 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
827 fnetaddpeer(fn, nick, nick);
829 hubhandleaction(sk, fn, cmd, args);
832 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
835 struct fnetpeer *peer;
839 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
841 if((peer = fnetfindpeer(fn, nick)) != NULL)
844 hubhandleaction(sk, fn, cmd, args);
847 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
852 struct fnetpeer *peer, *npeer;
855 for(peer = fn->peers; peer != NULL; peer = peer->next)
856 peer->flags.b.delete = 1;
857 while((p = strstr(args, "$$")) != NULL)
860 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
862 if((peer = fnetfindpeer(fn, buf)) == NULL)
863 peer = fnetaddpeer(fn, buf, buf);
865 peer->flags.b.delete = 0;
867 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
871 for(peer = fn->peers; peer != NULL; peer = npeer)
874 if(peer->flags.b.delete)
877 hubhandleaction(sk, fn, cmd, args);
880 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
885 struct fnetpeer *peer;
888 for(peer = fn->peers; peer != NULL; peer = peer->next)
889 peer->flags.b.op = 0;
890 while((p = strstr(args, "$$")) != NULL)
893 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
895 if((peer = fnetfindpeer(fn, buf)) != NULL)
896 peer->flags.b.op = 1;
901 hubhandleaction(sk, fn, cmd, args);
904 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
907 wchar_t *buf, *wp, *wp2;
909 struct fnetpeer *peer;
914 if(strncmp(p, "$ALL ", 5))
917 if((p2 = strchr(p, ' ')) == NULL)
920 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
922 if((peer = fnetfindpeer(fn, buf)) == NULL)
923 peer = fnetaddpeer(fn, buf, buf);
926 if((p2 = strstr(p, "$ $")) == NULL)
929 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
931 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcschr(buf, L'<')) != NULL))
933 buf[wcslen(buf) - 1] = L'\0';
935 if((wp2 = wcschr(wp, L' ')) != NULL)
938 fnetpeersetstr(peer, L"dc-client", wp);
942 if((wp2 = wcschr(wp, L',')) != NULL)
946 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
947 fnetpeersetstr(peer, abuf, wp + 2);
949 } while(wp2 != NULL);
952 fnetpeersetstr(peer, L"descr", buf);
955 if((p2 = strchr(p, '$')) == NULL)
958 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
960 fnetpeersetstr(peer, L"dc-speed", buf);
963 if((p2 = strchr(p, '$')) == NULL)
966 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
968 fnetpeersetstr(peer, L"email", buf);
973 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
974 hubhandleaction(sk, fn, cmd, args);
977 /* I do not implement the fully in that I do not disconnect from the
978 * old hub. I do this since I believe that if the hub owner really
979 * wants to get rid of us, then it is his responsibility to disconnect
981 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
984 struct fnetnode *newfn;
988 if(strchr(args, ':') == NULL)
990 args = strcpy(smalloc(strlen(args) + 5), args);
991 strcat(args, ":411");
996 if((newfn = fnetinitconnect(L"dc", args)) != NULL)
1001 hubhandleaction(sk, fn, cmd, args);
1006 static char *getdcpath(struct sharecache *node, size_t *retlen)
1011 if(node->parent == NULL)
1013 if(node->parent == shareroot)
1015 if((buf = icwcstombs(node->name, DCCHARSET)) == NULL)
1018 *retlen = strlen(buf);
1021 if((buf2 = icwcstombs(node->name, DCCHARSET)) == NULL)
1023 if((buf = getdcpath(node->parent, &len)) == NULL)
1028 len2 = strlen(buf2);
1029 buf = srealloc(buf, len + 1 + len2 + 1);
1031 strcpy(buf + len, buf2);
1032 buf[len + len2] = 0;
1035 *retlen = len + len2;
1041 * This is the main share searching function for Direct Connect
1042 * peers. Feel free to optimize it if you feel the need for it. I
1043 * haven't ever seen it take any noticable CPU time anyway, so I
1044 * haven't felt that need.
1046 * It may feel dubious to just return the directory when all terms are
1047 * satisfied rather than all the files in it, but it really benefits
1048 * everyone: Less cycles used for us, less UDP squelching for active
1049 * searchers, and less bandwidth waste for hubs when serving passive
1052 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1057 char *prefix, *infix, *postfix, *buf, *buf2;
1059 struct sockaddr_in addr;
1060 struct sharecache *node;
1061 int minsize, maxsize;
1063 int termnum, satisfied, skipcheck;
1064 int level, tersat[32];
1069 if((p = strchr(args, ' ')) == NULL)
1073 memset(terms, 0, sizeof(terms));
1074 prefix = infix = postfix = NULL;
1078 if(!strncmp(args, "Hub:", 4))
1080 if(!strcmp(cmd, "$MultiSearch"))
1082 if(!strcmp(args + 4, hub->nativenick))
1084 prefix = sprintf2("$SR %s ", hub->nativenick);
1085 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1086 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1090 if((p2 = strchr(args, ':')) == NULL)
1093 addr.sin_family = AF_INET;
1094 if(!inet_aton(args, &addr.sin_addr))
1096 addr.sin_port = htons(atoi(p2));
1097 prefix = sprintf2("$SR %s ", hub->nativenick);
1098 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1099 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1100 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1103 minsize = maxsize = -1;
1112 if((*(p++) == 'T') && (minsize == 0))
1119 if((p2 = strchr(p, '?')) == NULL)
1134 if((*p2 == 0) || (*p2 == '$'))
1142 if(!dotth && !strncmp(p, "TTH:", 4))
1145 if((buf = base32decode(p + 4, &buflen)) == NULL)
1149 memcpy(hashtth, buf, 24);
1152 if((terms[termnum] = icmbstowcs(p, DCCHARSET)) != NULL)
1163 node = shareroot->child;
1165 for(i = 0; i < termnum; i++)
1171 if(node->f.b.type == FILE_REG)
1173 if((minsize >= 0) && (node->size < minsize))
1175 if((maxsize >= 0) && (node->size > maxsize))
1178 if(!skipcheck && dotth)
1180 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1185 for(i = 0; i < termnum; i++)
1189 if(wcsexists(node->name, terms[i]))
1193 } else if(node->child == NULL) {
1198 if(!skipcheck && (satisfied == termnum))
1200 if((buf = getdcpath(node, NULL)) != NULL)
1202 if(node->f.b.hastth)
1204 buf2 = base32encode(node->hashtth, 24);
1205 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1208 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1213 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1215 while(node->next == NULL)
1217 if((node = node->parent) == shareroot)
1221 if(node == shareroot)
1223 for(i = 0; i < termnum; i++)
1225 if(tersat[i] >= level)
1238 hubhandleaction(sk, fn, cmd, args);
1249 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1253 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1257 struct socket *newsk;
1258 struct sockaddr_in addr;
1261 if((p = strchr(args, ' ')) == NULL)
1264 if(strcmp(args, hub->nativenick))
1266 addr.sin_family = AF_INET;
1268 if((p = strchr(args, ':')) == NULL)
1271 addr.sin_port = htons(atoi(p));
1272 if(!inet_aton(args, &addr.sin_addr))
1274 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1276 hubhandleaction(sk, fn, cmd, args);
1279 static void sendctm(struct socket *sk, char *nick)
1281 struct sockaddr *addr;
1286 if(sockgetremotename(tcpsock, &addr, &addrlen) < 0)
1288 if(addr->sa_family == AF_INET)
1289 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1291 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1295 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1301 if((p = strchr(args, ' ')) == NULL)
1304 if(strcmp(p, hub->nativenick))
1307 expectpeer(args, fn);
1308 hubhandleaction(sk, fn, cmd, args);
1311 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1314 struct fnetnode *node;
1319 for(node = fnetnodes; node != NULL; node = node->next)
1321 if(node->state == FNN_EST)
1324 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1325 hubhandleaction(sk, fn, cmd, args);
1328 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1335 if((p2 = strchr(p, ' ')) == NULL)
1339 if((p2 = strchr(p, ' ')) == NULL)
1342 if(strcmp(p, "From:"))
1345 if((p2 = strstr(p, " $")) == NULL)
1349 hubrecvchat(fn->sk, fn, p, p2);
1350 hubhandleaction(sk, fn, cmd, args);
1353 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1357 char *nick, *filename, *hubname;
1361 wchar_t *wnick, *wfile;
1365 if((p2 = strchr(p, ' ')) == NULL)
1370 if((p2 = strchr(p, 5)) == NULL)
1374 if((p2 = strchr(p, ' ')) == NULL)
1379 if((p2 = strchr(p, '/')) == NULL)
1384 if((p2 = strchr(p, 5)) == NULL)
1388 if((p2 = strstr(p, " (")) == NULL)
1391 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
1393 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1398 sr = newsrchres(&dcnet, wfile, wnick);
1399 if(sr->peernick != NULL)
1401 sr->peernick = swcsdup(wnick);
1406 if(!strncmp(hubname, "TTH:", 4))
1408 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1411 sr->hash = newhash(L"TTH", 24, buf);
1415 getfnetnode(sr->fn = fn);
1418 hubhandleaction(sk, fn, cmd, args);
1421 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1423 /* Do nothing for now. */
1426 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1428 struct dcexppeer *expect;
1430 if(peer->nativename != NULL)
1431 free(peer->nativename);
1432 peer->nativename = sstrdup(args);
1433 if(peer->wcsname != NULL)
1434 free(peer->wcsname);
1435 if((peer->wcsname = icmbstowcs(peer->nativename, DCCHARSET)) == NULL)
1442 for(expect = expected; expect != NULL; expect = expect->next)
1444 if(!strcmp(expect->nick, args))
1451 peer->fn = expect->fn;
1452 getfnetnode(peer->fn);
1453 freeexppeer(expect);
1458 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1462 struct transfer *transfer;
1464 if((p = strchr(args, ' ')) == NULL)
1467 if(!strcmp(args, "Upload"))
1469 if(!strcmp(args, "Download"))
1473 if((peer->transfer == NULL) || (mydir != peer->direction))
1478 if(peer->direction == TRNSD_DOWN)
1481 if(peer->wcsname == NULL)
1486 peer->direction = mydir;
1487 if(peer->direction == TRNSD_UP)
1489 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1491 if((transfer = finddownload(peer->wcsname)) == NULL)
1496 transferattach(transfer, &dctransfer, peer);
1497 transfersetstate(transfer, TRNS_HS);
1499 transfersetnick(transfer, peer->wcsname);
1500 peer->transfer = transfer;
1503 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1504 if(peer->key != NULL)
1505 qstrf(sk, "$Key %s|", peer->key);
1506 if(peer->direction == TRNSD_DOWN)
1511 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1514 struct transfer *transfer;
1516 if((p = strchr(args, ' ')) == NULL)
1519 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1521 key = dcmakekey(args);
1524 if(peer->wcsname == NULL)
1533 if((transfer = finddownload(peer->wcsname)) == NULL)
1535 peer->direction = TRNSD_UP;
1536 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1538 peer->direction = TRNSD_DOWN;
1539 transferattach(transfer, &dctransfer, peer);
1540 transfersetstate(transfer, TRNS_HS);
1542 transfersetnick(transfer, peer->wcsname);
1543 peer->transfer = transfer;
1544 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1545 qstrf(sk, "$Key %s|", key);
1547 if(peer->key != NULL)
1553 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1558 static void startdl(struct dcpeer *peer)
1560 peer->state = PEER_TRNS;
1561 transferstartdl(peer->transfer, peer->sk);
1562 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1563 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1566 static void startul(struct dcpeer *peer)
1568 peer->state = PEER_TRNS;
1569 transferstartul(peer->transfer, peer->sk);
1570 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1573 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1577 if(peer->transfer == NULL)
1583 if(peer->transfer->size != size)
1585 transfersetsize(peer->transfer, size);
1590 qstr(peer->sk, "$Send|");
1593 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1595 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1597 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1598 resettransfer(peer->transfer);
1604 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1606 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1608 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1609 resettransfer(peer->transfer);
1622 {"MyList.DcLst", &hmlistname, updatehmlist},
1623 {"files.xml", &xmllistname, updatexmllist},
1624 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1628 static int openfilelist(char *name)
1633 for(i = 0; lists[i].name != NULL; i++)
1635 if(!strcmp(name, lists[i].name))
1639 if(lists[i].name == NULL)
1642 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1644 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1647 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1654 static struct sharecache *findbytth(char *tth32)
1658 struct sharecache *node;
1660 if((buf = base32decode(tth32, &buflen)) == NULL)
1667 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1669 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1676 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1678 struct sharecache *node;
1685 if((p2 = strchr(p, sep)) != NULL)
1687 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1694 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1699 struct sharecache *node;
1700 struct socket *lesk;
1704 if(peer->transfer == NULL)
1709 if((p = strchr(args, '$')) == NULL)
1715 if((offset = (atoi(p) - 1)) < 0)
1720 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1722 qstr(sk, "$Error Could not send file list|");
1725 } else if(fd >= 0) {
1726 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1727 transfersetpath(peer->transfer, buf2);
1733 qstr(sk, "$MaxedOut|");
1737 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1739 qstrf(sk, "$Error File not in share|");
1743 if((fd = opensharecache(node)) < 0)
1745 qstrf(sk, "$Error %s|", strerror(errno));
1749 buf = getfspath(node);
1750 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1751 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1753 transfersetpath(peer->transfer, buf2);
1756 if(fstat(fd, &sb) < 0)
1759 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1760 qstrf(sk, "$Error|");
1764 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1767 qstrf(sk, "$Error Offset out of range|");
1771 lesk = wrapsock(fd);
1772 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1774 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1777 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1779 if(peer->transfer == NULL)
1784 if(peer->transfer->localend == NULL)
1793 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1798 size_t arrsize, arrdata;
1800 if(peer->supports != NULL)
1802 for(i = 0; peer->supports[i] != NULL; i++)
1803 free(peer->supports[i]);
1804 free(peer->supports);
1807 arrsize = arrdata = 0;
1811 if((p2 = strchr(p, ' ')) != NULL)
1815 addtobuf(arr, sstrdup(p));
1816 } while((p = p2) != NULL);
1817 addtobuf(arr, NULL);
1818 peer->supports = arr;
1821 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1825 int start, numbytes;
1826 char *charset, *buf;
1828 struct sharecache *node;
1830 struct socket *lesk;
1832 if(peer->transfer == NULL)
1838 if((p2 = strchr(p, ' ')) == NULL)
1846 if((p2 = strchr(p, ' ')) == NULL)
1854 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1857 charset = DCCHARSET;
1858 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1859 initcompress(peer, CPRS_ZLIB);
1860 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1862 qstr(sk, "$Error Could not send file list|");
1864 } else if(fd >= 0) {
1865 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1866 transfersetpath(peer->transfer, buf2);
1872 qstr(sk, "$MaxedOut|");
1875 if((node = resdcpath(p, charset, '\\')) == NULL)
1877 qstr(sk, "$Error File not in cache|");
1880 if((fd = opensharecache(node)) < 0)
1882 qstrf(sk, "$Error %s|", strerror(errno));
1885 buf = getfspath(node);
1886 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1887 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1889 transfersetpath(peer->transfer, buf2);
1892 if(fstat(fd, &sb) < 0)
1895 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1896 qstr(sk, "$Error|");
1899 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1902 qstr(sk, "$Error Offset out of range|");
1905 if((numbytes < 0) || (start + numbytes > sb.st_size))
1906 numbytes = sb.st_size - start;
1907 lesk = wrapsock(fd);
1908 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1910 qstrf(sk, "$Sending %i|", numbytes);
1914 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1918 int start, numbytes;
1919 struct sharecache *node;
1921 struct socket *lesk;
1925 if(peer->transfer == NULL)
1930 if((argv = parseadc(args)) == NULL)
1935 if(parrlen(argv) < 4)
1940 start = atoi(argv[2]);
1941 numbytes = atoi(argv[3]);
1944 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
1946 qstr(sk, "$Error Could not send file list|");
1948 } else if(fd >= 0) {
1949 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
1950 transfersetpath(peer->transfer, wbuf);
1956 qstr(sk, "$MaxedOut|");
1959 if(!strncmp(argv[1], "TTH/", 4))
1961 if((node = findbytth(argv[1] + 4)) == NULL)
1963 qstr(sk, "$Error File not in cache|");
1967 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
1969 qstr(sk, "$Error File not in cache|");
1973 if((fd = opensharecache(node)) < 0)
1975 qstrf(sk, "$Error %s|", strerror(errno));
1978 buf = getfspath(node);
1979 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
1980 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1982 transfersetpath(peer->transfer, wbuf);
1985 if(!strcmp(argv[0], "file"))
1987 for(i = 4; argv[i] != NULL; i++)
1989 if(!strcmp(argv[i], "ZL1"))
1990 initcompress(peer, CPRS_ZLIB);
1992 if(fstat(fd, &sb) < 0)
1994 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1995 qstr(sk, "$Error|");
1998 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2000 qstr(sk, "$Error Offset out of range|");
2003 if((numbytes < 0) || (start + numbytes > sb.st_size))
2004 numbytes = sb.st_size - start;
2005 lesk = wrapsock(fd);
2006 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2009 qstr(sk, "$ADCSND");
2010 sendadc(sk, "file");
2011 sendadc(sk, argv[1]);
2012 sendadcf(sk, "%i", start);
2013 sendadcf(sk, "%i", numbytes);
2014 if(peer->compress == CPRS_ZLIB)
2018 } else if(!strcmp(argv[0], "tthl")) {
2020 * XXX: Implement full TTHL support.
2022 * In the meantime, this is better than nothing, since it at
2023 * least allows fetching of the TTH root if it isn't already
2028 qstr(sk, "$Error no TTHL data for virtual files|");
2031 qstr(sk, "$ADCSND");
2032 sendadc(sk, "tthl");
2033 sendadc(sk, argv[1]);
2037 sockqueue(sk, node->hashtth, 24);
2039 qstr(sk, "$Error Namespace not implemented|");
2049 static void handletthl(struct dcpeer *peer)
2053 while(peer->inbufdata >= 24)
2055 pushtigertree(&peer->tth, peer->inbuf);
2056 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2057 peer->curread += 24;
2059 if(peer->curread >= peer->totalsize)
2061 peer->state = PEER_CMD;
2062 synctigertree(&peer->tth);
2063 restigertree(&peer->tth, buf);
2064 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2069 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2072 int start, numbytes;
2074 if(peer->transfer == NULL)
2079 if((argv = parseadc(args)) == NULL)
2084 if(parrlen(argv) < 4)
2089 start = atoi(argv[2]);
2090 numbytes = atoi(argv[3]);
2091 if(!strcmp(argv[0], "tthl"))
2093 if((start != 0) || (numbytes % 24 != 0))
2095 /* Weird. Bail out. */
2099 peer->state = PEER_TTHL;
2100 peer->totalsize = numbytes;
2102 inittigertree(&peer->tth);
2104 } else if(!strcmp(argv[0], "file")) {
2105 if(start != peer->transfer->curpos)
2110 if(start + numbytes != peer->transfer->size)
2112 transfersetsize(peer->transfer, start + numbytes);
2117 if(peer->inbufdata > 0)
2119 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2120 peer->inbufdata = 0;
2121 transread(sk, peer);
2124 /* We certainly didn't request this...*/
2133 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2137 if(peer->transfer == NULL)
2142 numbytes = atoi(args);
2143 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2145 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2150 if(peer->inbufdata > 0)
2152 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2153 peer->inbufdata = 0;
2154 transread(sk, peer);
2160 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2165 hubhandleaction(sk, fn, cmd, args);
2169 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2175 static int hubreqconn(struct fnetpeer *peer)
2180 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2185 if((hub = peer->fn->data) == NULL)
2190 if((mbsnick = icwcstombs(peer->id, DCCHARSET)) == NULL)
2191 return(1); /* Shouldn't happen, of course, but who knows... */
2194 sendctm(peer->fn->sk, mbsnick);
2195 expectpeer(mbsnick, peer->fn);
2197 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2203 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2206 char *mbsstring, *mbsto;
2209 if((mbsto = icwcstombs(to, DCCHARSET)) == NULL)
2214 if((mbsstring = icwcstombs(string, DCCHARSET)) == NULL)
2219 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2226 if(strchr(mbsstring, '|') != NULL)
2237 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2239 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2242 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2249 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2251 int minl, maxl, minr, maxr, retmin, retmax;
2256 findsizelimit(sexpr->l, &minl, &maxl);
2257 findsizelimit(sexpr->r, &minr, &maxr);
2258 retmin = (minl > minr)?minl:minr;
2259 if((maxl != -1) && (maxr != -1))
2260 retmax = (maxl < maxr)?maxl:maxr;
2269 findsizelimit(sexpr->l, &minl, &maxl);
2270 findsizelimit(sexpr->r, &minr, &maxr);
2271 retmin = (minl < minr)?minl:minr;
2272 if((maxl == -1) || (maxr == -1))
2275 retmax = (maxl > maxr)?maxl:maxr;
2278 findsizelimit(sexpr->l, &minl, &maxl);
2279 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2283 } else if((minl == 0) && (maxl != -1)) {
2286 } else if((minl != 0) && (maxl == -1)) {
2289 } else { /* This would yield two seperate intervals, which DC cannot handle */
2295 retmax = sexpr->d.n - 1;
2298 retmin = sexpr->d.n;
2299 retmax = sexpr->d.n;
2302 retmin = sexpr->d.n + 1;
2316 static struct hash *findsehash(struct sexpr *sexpr)
2318 struct hash *h1, *h2;
2323 if((h1 = findsehash(sexpr->l)) != NULL)
2325 if((h1 = findsehash(sexpr->r)) != NULL)
2329 h1 = findsehash(sexpr->l);
2330 h2 = findsehash(sexpr->r);
2335 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2336 return(sexpr->d.hash);
2343 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2346 struct wcslist *list, *cur;
2347 char *sstr, *buf, *p;
2348 size_t sstrsize, sstrdata;
2349 struct sockaddr *name;
2351 int minsize, maxsize;
2355 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2357 list = findsexprstrs(srch->sexpr);
2358 findsizelimit(srch->sexpr, &minsize, &maxsize);
2359 hash = findsehash(srch->sexpr);
2360 if((minsize != 0) && (maxsize != -1))
2362 /* Choose either minsize or maxsize by trying to determine
2363 * which choice will be most restrictive. The result can be
2364 * approximative at best anyway, so don't try too hard... */
2365 if((50000000 - maxsize) > (minsize - 50000000))
2371 sstrsize = sstrdata = 0;
2372 if((hash != NULL) && (hash->len == 24))
2374 /* Prioritize hash searches above all else */
2375 bufcat(sstr, "F?T?0?9?TTH:", 12);
2376 buf = base32encode(hash->buf, hash->len);
2378 bufcat(sstr, buf, 39);
2383 sizebuf2(sstr, sstrdata + 32, 1);
2384 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2385 } else if(maxsize != -1) {
2386 sizebuf2(sstr, sstrdata + 32, 1);
2387 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2389 bufcat(sstr, "F?F?0?1?", 8);
2393 for(cur = list; cur != NULL; cur = cur->next)
2395 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2397 /* Can't find anything anyway if the search expression
2398 * requires characters outside DC's charset. There's
2399 * nothing technically wrong with the search itself,
2400 * however, so return success. This should be
2401 * considered as an optimization. */
2408 addtobuf(sstr, '$');
2410 * It probably doesn't hurt if buf contains any extra
2411 * dollar signs - it will just result in extra search
2412 * terms, and the extraneous results will be filtered by
2413 * the search layer anyway. It hurts if it contains any
2414 * pipes, though, so let's sell them for money.
2416 for(p = buf; *p; p++)
2421 bufcat(sstr, buf, strlen(buf));
2425 /* Will match all files... :-/ */
2426 addtobuf(sstr, '.');
2432 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2434 flog(LOG_WARNING, "cannot get address of UDP socket");
2436 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2440 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2444 fn->lastsrch = time(NULL);
2452 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2453 struct command hubcmds[] =
2455 {"$Lock", cc(cmd_lock)},
2456 {"$HubName", cc(cmd_hubname)},
2457 {"$Hello", cc(cmd_hello)},
2458 {"$Quit", cc(cmd_quit)},
2459 {"$NickList", cc(cmd_nicklist)},
2460 {"$OpList", cc(cmd_oplist)},
2461 {"$MyINFO", cc(cmd_myinfo)},
2462 {"$ForceMove", cc(cmd_forcemove)},
2463 {"$Search", cc(cmd_search)},
2464 {"$MultiSearch", cc(cmd_search)},
2465 {"$ConnectToMe", cc(cmd_connecttome)},
2466 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2467 {"$GetNetInfo", cc(cmd_getnetinfo)},
2468 {"$To:", cc(cmd_to)},
2469 {"$SR", cc(cmd_sr)},
2470 {"$UserCommand", cc(cmd_usercommand)},
2474 struct command peercmds[] =
2476 {"$MyNick", cc(cmd_mynick)},
2477 {"$Lock", cc(cmd_peerlock)},
2478 {"$Direction", cc(cmd_direction)},
2479 {"$Key", cc(cmd_key)},
2480 {"$FileLength", cc(cmd_filelength)},
2481 {"$Error", cc(cmd_error)},
2482 {"$MaxedOut", cc(cmd_maxedout)},
2483 {"$Get", cc(cmd_get)},
2484 {"$Send", cc(cmd_send)},
2485 {"$Supports", cc(cmd_supports)},
2486 {"$GetBlock", cc(cmd_getblock)},
2487 {"$UGetBlock", cc(cmd_getblock)},
2488 {"$GetZBlock", cc(cmd_getblock)},
2489 {"$UGetZBlock", cc(cmd_getblock)},
2490 {"$ADCGET", cc(cmd_adcget)},
2491 {"$ADCSND", cc(cmd_adcsnd), 1},
2492 {"$Sending", cc(cmd_sending), 1},
2497 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2499 CBUNREG(transfer, trans_filterout, peer);
2502 peer->transfer = NULL;
2506 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2514 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2516 if(sockqueuesize(peer->sk) < 65536)
2518 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2520 if(peer->compress == CPRS_NONE)
2522 sockqueue(peer->sk, buf, bufsize);
2523 } else if(peer->compress == CPRS_ZLIB) {
2524 cstr = peer->cprsdata;
2525 cstr->next_in = buf;
2526 cstr->avail_in = bufsize;
2527 while(cstr->avail_in > 0)
2529 cstr->next_out = outbuf;
2530 cstr->avail_out = sizeof(outbuf);
2531 if((ret = deflate(cstr, 0)) != Z_OK)
2533 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2537 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2542 if(peer->state == PEER_SYNC)
2544 if(peer->compress == CPRS_ZLIB)
2546 cstr = peer->cprsdata;
2547 cstr->next_in = NULL;
2551 cstr->next_out = outbuf;
2552 cstr->avail_out = sizeof(outbuf);
2553 ret = deflate(cstr, Z_FINISH);
2554 if((ret != Z_OK) && (ret != Z_STREAM_END))
2556 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2560 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2561 } while(ret != Z_STREAM_END);
2567 peer->state = PEER_CMD;
2569 transfersetstate(transfer, TRNS_HS);
2570 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2571 peer->sk->writecb = NULL;
2578 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2580 peer->state = PEER_SYNC;
2581 dctransgotdata(transfer, peer);
2584 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2586 if(transferdatasize(transfer) < 65536)
2587 peer->sk->ignread = 0;
2590 static void transread(struct socket *sk, struct dcpeer *peer)
2594 struct transfer *transfer;
2596 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2598 if(peer->transfer == NULL)
2604 transferputdata(peer->transfer, buf, bufsize);
2606 if(peer->transfer->curpos >= peer->transfer->size)
2608 transfer = peer->transfer;
2609 transferdetach(transfer);
2610 transferendofdata(transfer);
2613 if(transferdatasize(peer->transfer) > 65535)
2617 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2619 struct transfer *transfer;
2621 if((transfer = peer->transfer) == NULL)
2626 transferdetach(transfer);
2627 transferendofdata(transfer);
2630 static void transwrite(struct socket *sk, struct dcpeer *peer)
2632 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2634 if(peer->transfer == NULL)
2639 dctransgotdata(peer->transfer, peer);
2642 static void udpread(struct socket *sk, void *data)
2644 char *buf, *p, *p2, *hashbuf;
2645 size_t buflen, hashlen;
2646 char *nick, *filename, *hubname;
2648 struct fnetnode *fn, *myfn;
2651 wchar_t *wnick, *wfile;
2654 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2656 buf = srealloc(buf, buflen + 1);
2658 if(!strncmp(buf, "$SR ", 4))
2662 if((p2 = strchr(p, ' ')) == NULL)
2670 if((p2 = strchr(p, 5)) == NULL)
2677 if((p2 = strchr(p, ' ')) == NULL)
2685 if((p2 = strchr(p, '/')) == NULL)
2693 if((p2 = strchr(p, 5)) == NULL)
2700 if((p2 = strstr(p, " (")) == NULL)
2706 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
2711 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2719 if(!strncmp(hubname, "TTH:", 4))
2721 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2724 hash = newhash(L"TTH", 24, hashbuf);
2728 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2730 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2732 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2745 sr = newsrchres(&dcnet, wfile, wnick);
2746 if(sr->peernick != NULL)
2748 sr->peernick = swcsdup(wnick);
2754 getfnetnode(sr->fn = myfn);
2763 static void hubread(struct socket *sk, struct fnetnode *fn)
2770 hub = (struct dchub *)fn->data;
2771 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2773 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2775 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2776 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2778 p = hub->inbuf + hub->inbufdata;
2779 hub->inbufdata += datalen;
2780 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2783 newqcmd(&hub->queue, hub->inbuf);
2784 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2785 datalen = hub->inbufdata;
2790 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2795 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2801 if((buf = icwcstombs(newnick, DCCHARSET)) == NULL)
2803 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2808 if(hub == NULL) /* Not yet connected */
2813 if(hub->nativenick != NULL)
2814 free(hub->nativenick);
2815 hub->nativenick = buf;
2819 static struct dchub *newdchub(struct fnetnode *fn)
2823 new = smalloc(sizeof(*new));
2824 memset(new, 0, sizeof(*new));
2826 if(hubsetnick(fn, fn->mynick))
2827 fnetsetnick(fn, L"DoldaConnectUser-IN");
2828 /* IN as in Invalid Nick */
2832 static struct dcpeer *newdcpeer(struct socket *sk)
2836 new = smalloc(sizeof(*new));
2837 memset(new, 0, sizeof(*new));
2838 new->transfer = NULL;
2850 static void freedcpeer(struct dcpeer *peer)
2853 struct qcommand *qcmd;
2858 if(peer->next != NULL)
2859 peer->next->prev = peer->prev;
2860 if(peer->prev != NULL)
2861 peer->prev->next = peer->next;
2862 if(peer->transfer != NULL)
2864 if(peer->transfer->dir == TRNSD_UP)
2865 peer->transfer->close = 1;
2866 if(peer->transfer->dir == TRNSD_DOWN)
2867 resettransfer(peer->transfer);
2868 transferdetach(peer->transfer);
2870 if(peer->sk->data == peer)
2871 peer->sk->data = NULL;
2872 peer->sk->readcb = NULL;
2873 peer->sk->writecb = NULL;
2874 peer->sk->errcb = NULL;
2877 if(peer->supports != NULL)
2879 for(i = 0; peer->supports[i] != NULL; i++)
2880 free(peer->supports[i]);
2881 free(peer->supports);
2883 if(peer->inbuf != NULL)
2885 if(peer->key != NULL)
2887 if(peer->wcsname != NULL)
2888 free(peer->wcsname);
2889 if(peer->nativename != NULL)
2890 free(peer->nativename);
2891 if(peer->fn != NULL)
2892 putfnetnode(peer->fn);
2893 while((qcmd = ulqcmd(&peer->queue)) != NULL)
2899 static void hubconnect(struct fnetnode *fn)
2901 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
2902 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
2904 fn->data = newdchub(fn);
2909 static void hubdestroy(struct fnetnode *fn)
2912 struct qcommand *qcmd;
2914 hub = (struct dchub *)fn->data;
2917 if(fn->sk->data == fn)
2919 fn->sk->data = NULL;
2925 while((qcmd = ulqcmd(&hub->queue)) != NULL)
2927 if(hub->nativename != NULL)
2928 free(hub->nativename);
2929 if(hub->nativenick != NULL)
2930 free(hub->nativenick);
2931 if(hub->inbuf != NULL)
2936 static wchar_t *dcbasename(wchar_t *filename)
2940 if((ret = wcsrchr(filename, L'\\')) != NULL)
2945 static struct transferiface dctransfer =
2947 .detach = (void (*)(struct transfer *, void *))dctransdetach,
2948 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
2949 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
2950 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
2953 static struct fnet dcnet =
2956 .connect = hubconnect,
2957 .destroy = hubdestroy,
2958 .setnick = hubsetnick,
2959 .reqconn = hubreqconn,
2960 .sendchat = hubsendchat,
2961 .search = hubsearch,
2962 .filebasename = dcbasename
2965 static void peerread(struct socket *sk, struct dcpeer *peer)
2969 struct command *cmd;
2971 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2973 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
2974 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
2976 peer->inbufdata += datalen;
2977 if(peer->state == PEER_CMD)
2980 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
2983 newqcmd(&peer->queue, peer->inbuf);
2984 for(cmd = peercmds; cmd->handler != NULL; cmd++)
2986 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
2989 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
2992 peer->state = PEER_STOP;
2996 } else if(peer->state == PEER_TTHL) {
3001 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3006 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3008 struct dcpeer *peer;
3015 peer = newdcpeer(sk);
3018 sk->readcb = (void (*)(struct socket *, void *))peerread;
3019 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3021 socksettos(sk, confgetint("fnet", "fnptos"));
3027 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3029 struct dcpeer *peer;
3031 peer = newdcpeer(newsk);
3033 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3034 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3036 socksettos(newsk, confgetint("fnet", "fnptos"));
3039 static void updatehmlist(void)
3041 int i, lev, ic, ret;
3042 struct sharecache *node;
3043 char *buf, *buf2, numbuf[32];
3044 size_t bufsize, bufdata;
3048 buf = smalloc(bufsize = 65536);
3049 node = shareroot->child;
3054 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3056 for(i = 0; i < lev; i++)
3058 bufcat(buf, buf2, strlen(buf2));
3060 if(node->f.b.type == FILE_REG)
3063 sprintf(numbuf, "%i", node->size);
3064 bufcat(buf, numbuf, strlen(numbuf));
3071 if((node->child != NULL) && !ic)
3075 } else if(node->next != NULL) {
3078 while(node->next == NULL)
3081 node = node->parent;
3082 if(node == shareroot)
3085 if(node == shareroot)
3090 if(hmlistname != NULL)
3095 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3096 if((fd = mkstemp(hmlistname)) < 0)
3098 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3103 * I do not want to implement a good Huffman encoder, and it's not
3104 * like Huffman encoding actually yields any impressive results
3105 * for DC file lists anyway, so I'll just output a bogus
3106 * tree. Implement a good encoder if you want to.
3108 write(fd, "HE3\r\0", 5);
3109 write(fd, &bufdata, 4);
3111 write(fd, &ibuf, 2);
3113 for(i = 0; i < 256; i++)
3116 write(fd, &ibuf, 1);
3118 for(i = 0; i < 256; i++)
3120 for(buf2 = buf; bufdata > 0;)
3122 if((ret = write(fd, buf2, bufdata)) < 0)
3124 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3135 static struct xmlent
3142 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3146 static wchar_t *escapexml(wchar_t *src)
3148 static wchar_t *buf = NULL;;
3151 size_t bufsize, bufdata;
3157 bufsize = bufdata = 0;
3158 for(; *src != L'\0'; src++)
3161 if((c > 0) && (c < 32))
3163 bufcat(buf, L"&#", 2);
3164 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3165 bufcat(buf, nbuf, ret);
3166 addtobuf(buf, L';');
3168 for(ent = entities; ent->ent != NULL; ent++)
3172 bufcat(buf, ent->ent, wcslen(ent->ent));
3176 if(ent->ent == NULL)
3177 addtobuf(buf, *src);
3180 addtobuf(buf, L'\0');
3184 static void updatexmllist(void)
3188 char cidbuf[14], *namebuf;
3190 struct sharecache *node;
3192 if(xmllistname != NULL)
3194 unlink(xmllistname);
3197 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3198 if((fd = mkstemp(xmllistname)) < 0)
3200 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3205 if((fs = fdopen(fd, "w")) == NULL)
3207 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3208 unlink(xmllistname);
3214 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3215 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3216 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3218 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, DCIDFULL);
3220 node = shareroot->child;
3224 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3226 for(i = 0; i < lev; i++)
3228 if(node->child != NULL)
3230 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3235 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3236 if(node->f.b.hastth)
3238 hashbuf = base32encode(node->hashtth, 24);
3239 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3242 fprintf(fs, "/>\r\n");
3244 while(node->next == NULL)
3246 node = node->parent;
3247 if(node == shareroot)
3252 for(i = 0; i < lev; i++)
3254 fprintf(fs, "</Directory>\r\n");
3257 if(node == shareroot)
3263 #ifdef DCPP_MASQUERADE
3264 fprintf(fs, "</FileListing>");
3266 fprintf(fs, "</FileListing>\r\n");
3271 static void updatexmlbz2list(void)
3280 if(xmllistname == NULL)
3282 if((in = fopen(xmllistname, "r")) == NULL)
3284 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3287 if(xmlbz2listname != NULL)
3289 unlink(xmlbz2listname);
3290 free(xmlbz2listname);
3292 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3293 if((fd = mkstemp(xmlbz2listname)) < 0)
3295 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3296 free(xmlbz2listname);
3297 xmlbz2listname = NULL;
3301 if((real = fdopen(fd, "w")) == NULL)
3303 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3305 unlink(xmlbz2listname);
3306 free(xmlbz2listname);
3307 xmlbz2listname = NULL;
3311 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3314 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3316 unlink(xmlbz2listname);
3317 free(xmlbz2listname);
3318 xmlbz2listname = NULL;
3324 bufdata = fread(buf, 1, sizeof(buf), in);
3325 BZ2_bzWrite(&err, out, buf, bufdata);
3328 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3332 static int shareupdate(unsigned long long uusharesize, void *data)
3340 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3343 struct command *cmd;
3345 if((p = strchr(qcmd->string, ' ')) != NULL)
3347 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3349 if(!strcmp(cmd->name, qcmd->string))
3352 if(cmd->handler != NULL)
3353 cmd->handler(sk, data, qcmd->string, p);
3356 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3360 static int run(void)
3362 struct fnetnode *fn, *nextfn;
3364 struct dcpeer *peer, *nextpeer;
3365 struct qcommand *qcmd;
3369 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3372 if(fn->fnet != &dcnet)
3374 if(fn->data == NULL)
3376 hub = (struct dchub *)fn->data;
3377 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3379 if(*qcmd->string == '$')
3381 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3382 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3383 } else if(*qcmd->string != 0) {
3384 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3391 for(peer = peers; peer != NULL; peer = nextpeer)
3393 nextpeer = peer->next;
3394 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3396 if(*qcmd->string == '$')
3397 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3406 static void preinit(int hup)
3413 static int updateudpport(struct configvar *var, void *uudata)
3415 struct sockaddr_in addr;
3416 struct socket *newsock;
3418 memset(&addr, 0, sizeof(addr));
3419 addr.sin_family = AF_INET;
3420 addr.sin_port = htons(var->val.num);
3421 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3423 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3426 newsock->readcb = udpread;
3433 static int updatetcpport(struct configvar *var, void *uudata)
3435 struct sockaddr_in addr;
3436 struct socket *newsock;
3438 memset(&addr, 0, sizeof(addr));
3439 addr.sin_family = AF_INET;
3440 addr.sin_port = htons(var->val.num);
3441 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3442 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3449 static int init(int hup)
3451 struct sockaddr_in addr;
3455 GCBREG(sharechangecb, shareupdate, NULL);
3460 addr.sin_family = AF_INET;
3461 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3462 addr.sin_port = htons(confgetint("dc", "udpport"));
3463 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3465 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3468 udpsock->readcb = udpread;
3469 addr.sin_port = htons(confgetint("dc", "tcpport"));
3470 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3471 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3472 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3473 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3474 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3479 static void terminate(void)
3481 if(hmlistname != NULL)
3486 if(xmllistname != NULL)
3488 unlink(xmllistname);
3491 if(xmlbz2listname != NULL)
3493 unlink(xmlbz2listname);
3494 free(xmlbz2listname);
3498 static struct configvar myvars[] =
3500 {CONF_VAR_STRING, "desc", {.str = L""}},
3501 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3502 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3503 {CONF_VAR_INT, "udpport", {.num = 0}},
3504 {CONF_VAR_INT, "tcpport", {.num = 0}},
3508 static struct module me =
3517 .terminate = terminate,