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))
1480 if(peer->wcsname == NULL)
1485 peer->direction = mydir;
1486 if(peer->direction == TRNSD_UP)
1488 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1490 if((transfer = finddownload(peer->wcsname)) == NULL)
1495 transferattach(transfer, &dctransfer, peer);
1496 transfersetstate(transfer, TRNS_HS);
1498 transfersetnick(transfer, peer->wcsname);
1499 peer->transfer = transfer;
1502 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1503 if(peer->key != NULL)
1504 qstrf(sk, "$Key %s|", peer->key);
1505 if(peer->direction == TRNSD_DOWN)
1510 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1513 struct transfer *transfer;
1515 if((p = strchr(args, ' ')) == NULL)
1518 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1520 key = dcmakekey(args);
1523 if(peer->wcsname == NULL)
1532 if((transfer = finddownload(peer->wcsname)) == NULL)
1534 peer->direction = TRNSD_UP;
1535 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1537 peer->direction = TRNSD_DOWN;
1538 transferattach(transfer, &dctransfer, peer);
1539 transfersetstate(transfer, TRNS_HS);
1541 transfersetnick(transfer, peer->wcsname);
1542 peer->transfer = transfer;
1543 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1544 qstrf(sk, "$Key %s|", key);
1546 if(peer->key != NULL)
1552 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1557 static void startdl(struct dcpeer *peer)
1559 peer->state = PEER_TRNS;
1560 transferstartdl(peer->transfer, peer->sk);
1561 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1562 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1565 static void startul(struct dcpeer *peer)
1567 peer->state = PEER_TRNS;
1568 transferstartul(peer->transfer, peer->sk);
1569 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1572 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1576 if(peer->transfer == NULL)
1582 if(peer->transfer->size != size)
1584 transfersetsize(peer->transfer, size);
1589 qstr(peer->sk, "$Send|");
1592 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1594 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1596 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1597 resettransfer(peer->transfer);
1603 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1605 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1607 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1608 resettransfer(peer->transfer);
1621 {"MyList.DcLst", &hmlistname, updatehmlist},
1622 {"files.xml", &xmllistname, updatexmllist},
1623 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1627 static int openfilelist(char *name)
1632 for(i = 0; lists[i].name != NULL; i++)
1634 if(!strcmp(name, lists[i].name))
1638 if(lists[i].name == NULL)
1641 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1643 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1646 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1653 static struct sharecache *findbytth(char *tth32)
1657 struct sharecache *node;
1659 if((buf = base32decode(tth32, &buflen)) == NULL)
1666 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1668 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1675 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1677 struct sharecache *node;
1684 if((p2 = strchr(p, sep)) != NULL)
1686 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1693 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1698 struct sharecache *node;
1699 struct socket *lesk;
1703 if(peer->transfer == NULL)
1708 if((p = strchr(args, '$')) == NULL)
1714 if((offset = (atoi(p) - 1)) < 0)
1719 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1721 qstr(sk, "$Error Could not send file list|");
1724 } else if(fd >= 0) {
1725 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1726 transfersetpath(peer->transfer, buf2);
1732 qstr(sk, "$MaxedOut|");
1736 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1738 qstrf(sk, "$Error File not in share|");
1742 if((fd = opensharecache(node)) < 0)
1744 qstrf(sk, "$Error %s|", strerror(errno));
1748 buf = getfspath(node);
1749 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1750 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1752 transfersetpath(peer->transfer, buf2);
1755 if(fstat(fd, &sb) < 0)
1758 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1759 qstrf(sk, "$Error|");
1763 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1766 qstrf(sk, "$Error Offset out of range|");
1770 lesk = wrapsock(fd);
1771 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1773 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1776 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1778 if(peer->transfer == NULL)
1783 if(peer->transfer->localend == NULL)
1792 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1797 size_t arrsize, arrdata;
1799 if(peer->supports != NULL)
1801 for(i = 0; peer->supports[i] != NULL; i++)
1802 free(peer->supports[i]);
1803 free(peer->supports);
1806 arrsize = arrdata = 0;
1810 if((p2 = strchr(p, ' ')) != NULL)
1814 addtobuf(arr, sstrdup(p));
1815 } while((p = p2) != NULL);
1816 addtobuf(arr, NULL);
1817 peer->supports = arr;
1820 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1824 int start, numbytes;
1825 char *charset, *buf;
1827 struct sharecache *node;
1829 struct socket *lesk;
1831 if(peer->transfer == NULL)
1837 if((p2 = strchr(p, ' ')) == NULL)
1845 if((p2 = strchr(p, ' ')) == NULL)
1853 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1856 charset = DCCHARSET;
1857 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1858 initcompress(peer, CPRS_ZLIB);
1859 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1861 qstr(sk, "$Error Could not send file list|");
1863 } else if(fd >= 0) {
1864 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1865 transfersetpath(peer->transfer, buf2);
1871 qstr(sk, "$MaxedOut|");
1874 if((node = resdcpath(p, charset, '\\')) == NULL)
1876 qstr(sk, "$Error File not in cache|");
1879 if((fd = opensharecache(node)) < 0)
1881 qstrf(sk, "$Error %s|", strerror(errno));
1884 buf = getfspath(node);
1885 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1886 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1888 transfersetpath(peer->transfer, buf2);
1891 if(fstat(fd, &sb) < 0)
1894 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1895 qstr(sk, "$Error|");
1898 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1901 qstr(sk, "$Error Offset out of range|");
1904 if((numbytes < 0) || (start + numbytes > sb.st_size))
1905 numbytes = sb.st_size - start;
1906 lesk = wrapsock(fd);
1907 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1909 qstrf(sk, "$Sending %i|", numbytes);
1913 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1917 int start, numbytes;
1918 struct sharecache *node;
1920 struct socket *lesk;
1924 if(peer->transfer == NULL)
1929 if((argv = parseadc(args)) == NULL)
1934 if(parrlen(argv) < 4)
1939 start = atoi(argv[2]);
1940 numbytes = atoi(argv[3]);
1943 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
1945 qstr(sk, "$Error Could not send file list|");
1947 } else if(fd >= 0) {
1948 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
1949 transfersetpath(peer->transfer, wbuf);
1955 qstr(sk, "$MaxedOut|");
1958 if(!strncmp(argv[1], "TTH/", 4))
1960 if((node = findbytth(argv[1] + 4)) == NULL)
1962 qstr(sk, "$Error File not in cache|");
1966 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
1968 qstr(sk, "$Error File not in cache|");
1972 if((fd = opensharecache(node)) < 0)
1974 qstrf(sk, "$Error %s|", strerror(errno));
1977 buf = getfspath(node);
1978 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
1979 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1981 transfersetpath(peer->transfer, wbuf);
1984 if(!strcmp(argv[0], "file"))
1986 for(i = 4; argv[i] != NULL; i++)
1988 if(!strcmp(argv[i], "ZL1"))
1989 initcompress(peer, CPRS_ZLIB);
1991 if(fstat(fd, &sb) < 0)
1993 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1994 qstr(sk, "$Error|");
1997 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1999 qstr(sk, "$Error Offset out of range|");
2002 if((numbytes < 0) || (start + numbytes > sb.st_size))
2003 numbytes = sb.st_size - start;
2004 lesk = wrapsock(fd);
2005 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2008 qstr(sk, "$ADCSND");
2009 sendadc(sk, "file");
2010 sendadc(sk, argv[1]);
2011 sendadcf(sk, "%i", start);
2012 sendadcf(sk, "%i", numbytes);
2013 if(peer->compress == CPRS_ZLIB)
2017 } else if(!strcmp(argv[0], "tthl")) {
2019 * XXX: Implement full TTHL support.
2021 * In the meantime, this is better than nothing, since it at
2022 * least allows fetching of the TTH root if it isn't already
2027 qstr(sk, "$Error no TTHL data for virtual files|");
2030 qstr(sk, "$ADCSND");
2031 sendadc(sk, "tthl");
2032 sendadc(sk, argv[1]);
2036 sockqueue(sk, node->hashtth, 24);
2038 qstr(sk, "$Error Namespace not implemented|");
2048 static void handletthl(struct dcpeer *peer)
2052 while(peer->inbufdata >= 24)
2054 pushtigertree(&peer->tth, peer->inbuf);
2055 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2056 peer->curread += 24;
2058 if(peer->curread >= peer->totalsize)
2060 peer->state = PEER_CMD;
2061 synctigertree(&peer->tth);
2062 restigertree(&peer->tth, buf);
2063 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2068 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2071 int start, numbytes;
2073 if(peer->transfer == NULL)
2078 if((argv = parseadc(args)) == NULL)
2083 if(parrlen(argv) < 4)
2088 start = atoi(argv[2]);
2089 numbytes = atoi(argv[3]);
2090 if(!strcmp(argv[0], "tthl"))
2092 if((start != 0) || (numbytes % 24 != 0))
2094 /* Weird. Bail out. */
2098 peer->state = PEER_TTHL;
2099 peer->totalsize = numbytes;
2101 inittigertree(&peer->tth);
2103 } else if(!strcmp(argv[0], "file")) {
2104 if(start != peer->transfer->curpos)
2109 if(start + numbytes != peer->transfer->size)
2111 transfersetsize(peer->transfer, start + numbytes);
2116 if(peer->inbufdata > 0)
2118 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2119 peer->inbufdata = 0;
2120 transread(sk, peer);
2123 /* We certainly didn't request this...*/
2132 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2136 if(peer->transfer == NULL)
2141 numbytes = atoi(args);
2142 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2144 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2149 if(peer->inbufdata > 0)
2151 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2152 peer->inbufdata = 0;
2153 transread(sk, peer);
2159 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2164 hubhandleaction(sk, fn, cmd, args);
2168 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2174 static int hubreqconn(struct fnetpeer *peer)
2179 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2184 if((hub = peer->fn->data) == NULL)
2189 if((mbsnick = icwcstombs(peer->id, DCCHARSET)) == NULL)
2190 return(1); /* Shouldn't happen, of course, but who knows... */
2193 sendctm(peer->fn->sk, mbsnick);
2194 expectpeer(mbsnick, peer->fn);
2196 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2202 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2205 char *mbsstring, *mbsto;
2208 if((mbsto = icwcstombs(to, DCCHARSET)) == NULL)
2213 if((mbsstring = icwcstombs(string, DCCHARSET)) == NULL)
2218 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2225 if(strchr(mbsstring, '|') != NULL)
2236 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2238 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2241 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2248 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2250 int minl, maxl, minr, maxr, retmin, retmax;
2255 findsizelimit(sexpr->l, &minl, &maxl);
2256 findsizelimit(sexpr->r, &minr, &maxr);
2257 retmin = (minl > minr)?minl:minr;
2258 if((maxl != -1) && (maxr != -1))
2259 retmax = (maxl < maxr)?maxl:maxr;
2268 findsizelimit(sexpr->l, &minl, &maxl);
2269 findsizelimit(sexpr->r, &minr, &maxr);
2270 retmin = (minl < minr)?minl:minr;
2271 if((maxl == -1) || (maxr == -1))
2274 retmax = (maxl > maxr)?maxl:maxr;
2277 findsizelimit(sexpr->l, &minl, &maxl);
2278 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2282 } else if((minl == 0) && (maxl != -1)) {
2285 } else if((minl != 0) && (maxl == -1)) {
2288 } else { /* This would yield two seperate intervals, which DC cannot handle */
2294 retmax = sexpr->d.n - 1;
2297 retmin = sexpr->d.n;
2298 retmax = sexpr->d.n;
2301 retmin = sexpr->d.n + 1;
2315 static struct hash *findsehash(struct sexpr *sexpr)
2317 struct hash *h1, *h2;
2322 if((h1 = findsehash(sexpr->l)) != NULL)
2324 if((h1 = findsehash(sexpr->r)) != NULL)
2328 h1 = findsehash(sexpr->l);
2329 h2 = findsehash(sexpr->r);
2334 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2335 return(sexpr->d.hash);
2342 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2345 struct wcslist *list, *cur;
2346 char *sstr, *buf, *p;
2347 size_t sstrsize, sstrdata;
2348 struct sockaddr *name;
2350 int minsize, maxsize;
2354 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2356 list = findsexprstrs(srch->sexpr);
2357 findsizelimit(srch->sexpr, &minsize, &maxsize);
2358 hash = findsehash(srch->sexpr);
2359 if((minsize != 0) && (maxsize != -1))
2361 /* Choose either minsize or maxsize by trying to determine
2362 * which choice will be most restrictive. The result can be
2363 * approximative at best anyway, so don't try too hard... */
2364 if((50000000 - maxsize) > (minsize - 50000000))
2370 sstrsize = sstrdata = 0;
2371 if((hash != NULL) && (hash->len == 24))
2373 /* Prioritize hash searches above all else */
2374 bufcat(sstr, "F?T?0?9?TTH:", 12);
2375 buf = base32encode(hash->buf, hash->len);
2377 bufcat(sstr, buf, 39);
2382 sizebuf2(sstr, sstrdata + 32, 1);
2383 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2384 } else if(maxsize != -1) {
2385 sizebuf2(sstr, sstrdata + 32, 1);
2386 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2388 bufcat(sstr, "F?F?0?1?", 8);
2392 for(cur = list; cur != NULL; cur = cur->next)
2394 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2396 /* Can't find anything anyway if the search expression
2397 * requires characters outside DC's charset. There's
2398 * nothing technically wrong with the search itself,
2399 * however, so return success. This should be
2400 * considered as an optimization. */
2407 addtobuf(sstr, '$');
2409 * It probably doesn't hurt if buf contains any extra
2410 * dollar signs - it will just result in extra search
2411 * terms, and the extraneous results will be filtered by
2412 * the search layer anyway. It hurts if it contains any
2413 * pipes, though, so let's sell them for money.
2415 for(p = buf; *p; p++)
2420 bufcat(sstr, buf, strlen(buf));
2424 /* Will match all files... :-/ */
2425 addtobuf(sstr, '.');
2431 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2433 flog(LOG_WARNING, "cannot get address of UDP socket");
2435 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2439 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2443 fn->lastsrch = time(NULL);
2451 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2452 struct command hubcmds[] =
2454 {"$Lock", cc(cmd_lock)},
2455 {"$HubName", cc(cmd_hubname)},
2456 {"$Hello", cc(cmd_hello)},
2457 {"$Quit", cc(cmd_quit)},
2458 {"$NickList", cc(cmd_nicklist)},
2459 {"$OpList", cc(cmd_oplist)},
2460 {"$MyINFO", cc(cmd_myinfo)},
2461 {"$ForceMove", cc(cmd_forcemove)},
2462 {"$Search", cc(cmd_search)},
2463 {"$MultiSearch", cc(cmd_search)},
2464 {"$ConnectToMe", cc(cmd_connecttome)},
2465 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2466 {"$GetNetInfo", cc(cmd_getnetinfo)},
2467 {"$To:", cc(cmd_to)},
2468 {"$SR", cc(cmd_sr)},
2469 {"$UserCommand", cc(cmd_usercommand)},
2473 struct command peercmds[] =
2475 {"$MyNick", cc(cmd_mynick)},
2476 {"$Lock", cc(cmd_peerlock)},
2477 {"$Direction", cc(cmd_direction)},
2478 {"$Key", cc(cmd_key)},
2479 {"$FileLength", cc(cmd_filelength)},
2480 {"$Error", cc(cmd_error)},
2481 {"$MaxedOut", cc(cmd_maxedout)},
2482 {"$Get", cc(cmd_get)},
2483 {"$Send", cc(cmd_send)},
2484 {"$Supports", cc(cmd_supports)},
2485 {"$GetBlock", cc(cmd_getblock)},
2486 {"$UGetBlock", cc(cmd_getblock)},
2487 {"$GetZBlock", cc(cmd_getblock)},
2488 {"$UGetZBlock", cc(cmd_getblock)},
2489 {"$ADCGET", cc(cmd_adcget)},
2490 {"$ADCSND", cc(cmd_adcsnd), 1},
2491 {"$Sending", cc(cmd_sending), 1},
2496 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2498 CBUNREG(transfer, trans_filterout, peer);
2501 peer->transfer = NULL;
2505 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2513 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2515 if(sockqueuesize(peer->sk) < 65536)
2517 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2519 if(peer->compress == CPRS_NONE)
2521 sockqueue(peer->sk, buf, bufsize);
2522 } else if(peer->compress == CPRS_ZLIB) {
2523 cstr = peer->cprsdata;
2524 cstr->next_in = buf;
2525 cstr->avail_in = bufsize;
2526 while(cstr->avail_in > 0)
2528 cstr->next_out = outbuf;
2529 cstr->avail_out = sizeof(outbuf);
2530 if((ret = deflate(cstr, 0)) != Z_OK)
2532 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2536 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2541 if(peer->state == PEER_SYNC)
2543 if(peer->compress == CPRS_ZLIB)
2545 cstr = peer->cprsdata;
2546 cstr->next_in = NULL;
2550 cstr->next_out = outbuf;
2551 cstr->avail_out = sizeof(outbuf);
2552 ret = deflate(cstr, Z_FINISH);
2553 if((ret != Z_OK) && (ret != Z_STREAM_END))
2555 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2559 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2560 } while(ret != Z_STREAM_END);
2566 peer->state = PEER_CMD;
2568 transfersetstate(transfer, TRNS_HS);
2569 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2570 peer->sk->writecb = NULL;
2577 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2579 peer->state = PEER_SYNC;
2580 dctransgotdata(transfer, peer);
2583 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2585 if(transferdatasize(transfer) < 65536)
2586 peer->sk->ignread = 0;
2589 static void transread(struct socket *sk, struct dcpeer *peer)
2593 struct transfer *transfer;
2595 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2597 if(peer->transfer == NULL)
2603 transferputdata(peer->transfer, buf, bufsize);
2605 if(peer->transfer->curpos >= peer->transfer->size)
2607 transfer = peer->transfer;
2608 transferdetach(transfer);
2609 transferendofdata(transfer);
2612 if(transferdatasize(peer->transfer) > 65535)
2616 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2618 struct transfer *transfer;
2620 if((transfer = peer->transfer) == NULL)
2625 transferdetach(transfer);
2626 transferendofdata(transfer);
2629 static void transwrite(struct socket *sk, struct dcpeer *peer)
2631 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2633 if(peer->transfer == NULL)
2638 dctransgotdata(peer->transfer, peer);
2641 static void udpread(struct socket *sk, void *data)
2643 char *buf, *p, *p2, *hashbuf;
2644 size_t buflen, hashlen;
2645 char *nick, *filename, *hubname;
2647 struct fnetnode *fn, *myfn;
2650 wchar_t *wnick, *wfile;
2653 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2655 buf = srealloc(buf, buflen + 1);
2657 if(!strncmp(buf, "$SR ", 4))
2661 if((p2 = strchr(p, ' ')) == NULL)
2669 if((p2 = strchr(p, 5)) == NULL)
2676 if((p2 = strchr(p, ' ')) == NULL)
2684 if((p2 = strchr(p, '/')) == NULL)
2692 if((p2 = strchr(p, 5)) == NULL)
2699 if((p2 = strstr(p, " (")) == NULL)
2705 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
2710 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2718 if(!strncmp(hubname, "TTH:", 4))
2720 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2723 hash = newhash(L"TTH", 24, hashbuf);
2727 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2729 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2731 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2744 sr = newsrchres(&dcnet, wfile, wnick);
2745 if(sr->peernick != NULL)
2747 sr->peernick = swcsdup(wnick);
2753 getfnetnode(sr->fn = myfn);
2762 static void hubread(struct socket *sk, struct fnetnode *fn)
2769 hub = (struct dchub *)fn->data;
2770 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2772 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2774 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2775 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2777 p = hub->inbuf + hub->inbufdata;
2778 hub->inbufdata += datalen;
2779 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2782 newqcmd(&hub->queue, hub->inbuf);
2783 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2784 datalen = hub->inbufdata;
2789 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2794 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2800 if((buf = icwcstombs(newnick, DCCHARSET)) == NULL)
2802 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2807 if(hub == NULL) /* Not yet connected */
2812 if(hub->nativenick != NULL)
2813 free(hub->nativenick);
2814 hub->nativenick = buf;
2818 static struct dchub *newdchub(struct fnetnode *fn)
2822 new = smalloc(sizeof(*new));
2823 memset(new, 0, sizeof(*new));
2825 if(hubsetnick(fn, fn->mynick))
2826 fnetsetnick(fn, L"DoldaConnectUser-IN");
2827 /* IN as in Invalid Nick */
2831 static struct dcpeer *newdcpeer(struct socket *sk)
2835 new = smalloc(sizeof(*new));
2836 memset(new, 0, sizeof(*new));
2837 new->transfer = NULL;
2849 static void freedcpeer(struct dcpeer *peer)
2852 struct qcommand *qcmd;
2857 if(peer->next != NULL)
2858 peer->next->prev = peer->prev;
2859 if(peer->prev != NULL)
2860 peer->prev->next = peer->next;
2861 if(peer->transfer != NULL)
2863 if(peer->transfer->dir == TRNSD_UP)
2864 peer->transfer->close = 1;
2865 if(peer->transfer->dir == TRNSD_DOWN)
2866 resettransfer(peer->transfer);
2867 transferdetach(peer->transfer);
2869 if(peer->sk->data == peer)
2870 peer->sk->data = NULL;
2871 peer->sk->readcb = NULL;
2872 peer->sk->writecb = NULL;
2873 peer->sk->errcb = NULL;
2876 if(peer->supports != NULL)
2878 for(i = 0; peer->supports[i] != NULL; i++)
2879 free(peer->supports[i]);
2880 free(peer->supports);
2882 if(peer->inbuf != NULL)
2884 if(peer->key != NULL)
2886 if(peer->wcsname != NULL)
2887 free(peer->wcsname);
2888 if(peer->nativename != NULL)
2889 free(peer->nativename);
2890 if(peer->fn != NULL)
2891 putfnetnode(peer->fn);
2892 while((qcmd = ulqcmd(&peer->queue)) != NULL)
2898 static void hubconnect(struct fnetnode *fn)
2900 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
2901 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
2903 fn->data = newdchub(fn);
2908 static void hubdestroy(struct fnetnode *fn)
2911 struct qcommand *qcmd;
2913 hub = (struct dchub *)fn->data;
2916 if(fn->sk->data == fn)
2918 fn->sk->data = NULL;
2924 while((qcmd = ulqcmd(&hub->queue)) != NULL)
2926 if(hub->nativename != NULL)
2927 free(hub->nativename);
2928 if(hub->nativenick != NULL)
2929 free(hub->nativenick);
2930 if(hub->inbuf != NULL)
2935 static wchar_t *dcbasename(wchar_t *filename)
2939 if((ret = wcsrchr(filename, L'\\')) != NULL)
2944 static struct transferiface dctransfer =
2946 .detach = (void (*)(struct transfer *, void *))dctransdetach,
2947 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
2948 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
2949 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
2952 static struct fnet dcnet =
2955 .connect = hubconnect,
2956 .destroy = hubdestroy,
2957 .setnick = hubsetnick,
2958 .reqconn = hubreqconn,
2959 .sendchat = hubsendchat,
2960 .search = hubsearch,
2961 .filebasename = dcbasename
2964 static void peerread(struct socket *sk, struct dcpeer *peer)
2968 struct command *cmd;
2970 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2972 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
2973 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
2975 peer->inbufdata += datalen;
2976 if(peer->state == PEER_CMD)
2979 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
2982 newqcmd(&peer->queue, peer->inbuf);
2983 for(cmd = peercmds; cmd->handler != NULL; cmd++)
2985 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && (peer->inbuf[strlen(cmd->name)] == 0))
2990 peer->state = PEER_STOP;
2993 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
2995 } else if(peer->state == PEER_TTHL) {
3000 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3005 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3007 struct dcpeer *peer;
3014 peer = newdcpeer(sk);
3017 sk->readcb = (void (*)(struct socket *, void *))peerread;
3018 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3020 socksettos(sk, confgetint("fnet", "fnptos"));
3026 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3028 struct dcpeer *peer;
3030 peer = newdcpeer(newsk);
3032 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3033 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3035 socksettos(newsk, confgetint("fnet", "fnptos"));
3038 static void updatehmlist(void)
3040 int i, lev, ic, ret;
3041 struct sharecache *node;
3042 char *buf, *buf2, numbuf[32];
3043 size_t bufsize, bufdata;
3047 buf = smalloc(bufsize = 65536);
3048 node = shareroot->child;
3053 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3055 for(i = 0; i < lev; i++)
3057 bufcat(buf, buf2, strlen(buf2));
3059 if(node->f.b.type == FILE_REG)
3062 sprintf(numbuf, "%i", node->size);
3063 bufcat(buf, numbuf, strlen(numbuf));
3070 if((node->child != NULL) && !ic)
3074 } else if(node->next != NULL) {
3077 while(node->next == NULL)
3080 node = node->parent;
3081 if(node == shareroot)
3084 if(node == shareroot)
3089 if(hmlistname != NULL)
3094 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3095 if((fd = mkstemp(hmlistname)) < 0)
3097 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3102 * I do not want to implement a good Huffman encoder, and it's not
3103 * like Huffman encoding actually yields any impressive results
3104 * for DC file lists anyway, so I'll just output a bogus
3105 * tree. Implement a good encoder if you want to.
3107 write(fd, "HE3\r\0", 5);
3108 write(fd, &bufdata, 4);
3110 write(fd, &ibuf, 2);
3112 for(i = 0; i < 256; i++)
3115 write(fd, &ibuf, 1);
3117 for(i = 0; i < 256; i++)
3119 for(buf2 = buf; bufdata > 0;)
3121 if((ret = write(fd, buf2, bufdata)) < 0)
3123 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3134 static struct xmlent
3141 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3145 static wchar_t *escapexml(wchar_t *src)
3147 static wchar_t *buf = NULL;;
3150 size_t bufsize, bufdata;
3156 bufsize = bufdata = 0;
3157 for(; *src != L'\0'; src++)
3160 if((c > 0) && (c < 32))
3162 bufcat(buf, L"&#", 2);
3163 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3164 bufcat(buf, nbuf, ret);
3165 addtobuf(buf, L';');
3167 for(ent = entities; ent->ent != NULL; ent++)
3171 bufcat(buf, ent->ent, wcslen(ent->ent));
3175 if(ent->ent == NULL)
3176 addtobuf(buf, *src);
3179 addtobuf(buf, L'\0');
3183 static void updatexmllist(void)
3187 char cidbuf[14], *namebuf;
3189 struct sharecache *node;
3191 if(xmllistname != NULL)
3193 unlink(xmllistname);
3196 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3197 if((fd = mkstemp(xmllistname)) < 0)
3199 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3204 if((fs = fdopen(fd, "w")) == NULL)
3206 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3207 unlink(xmllistname);
3213 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3214 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3215 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3217 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, DCIDFULL);
3219 node = shareroot->child;
3223 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3225 for(i = 0; i < lev; i++)
3227 if(node->child != NULL)
3229 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3234 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3235 if(node->f.b.hastth)
3237 hashbuf = base32encode(node->hashtth, 24);
3238 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3241 fprintf(fs, "/>\r\n");
3243 while(node->next == NULL)
3245 node = node->parent;
3246 if(node == shareroot)
3251 for(i = 0; i < lev; i++)
3253 fprintf(fs, "</Directory>\r\n");
3256 if(node == shareroot)
3262 #ifdef DCPP_MASQUERADE
3263 fprintf(fs, "</FileListing>");
3265 fprintf(fs, "</FileListing>\r\n");
3270 static void updatexmlbz2list(void)
3279 if(xmllistname == NULL)
3281 if((in = fopen(xmllistname, "r")) == NULL)
3283 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3286 if(xmlbz2listname != NULL)
3288 unlink(xmlbz2listname);
3289 free(xmlbz2listname);
3291 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3292 if((fd = mkstemp(xmlbz2listname)) < 0)
3294 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3295 free(xmlbz2listname);
3296 xmlbz2listname = NULL;
3300 if((real = fdopen(fd, "w")) == NULL)
3302 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3304 unlink(xmlbz2listname);
3305 free(xmlbz2listname);
3306 xmlbz2listname = NULL;
3310 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3313 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3315 unlink(xmlbz2listname);
3316 free(xmlbz2listname);
3317 xmlbz2listname = NULL;
3323 bufdata = fread(buf, 1, sizeof(buf), in);
3324 BZ2_bzWrite(&err, out, buf, bufdata);
3327 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3331 static int shareupdate(unsigned long long uusharesize, void *data)
3339 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3342 struct command *cmd;
3344 if((p = strchr(qcmd->string, ' ')) != NULL)
3346 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3348 if(!strcmp(cmd->name, qcmd->string))
3351 if(cmd->handler != NULL)
3352 cmd->handler(sk, data, qcmd->string, p);
3355 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3359 static int run(void)
3361 struct fnetnode *fn, *nextfn;
3363 struct dcpeer *peer, *nextpeer;
3364 struct qcommand *qcmd;
3368 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3371 if(fn->fnet != &dcnet)
3373 if(fn->data == NULL)
3375 hub = (struct dchub *)fn->data;
3376 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3378 if(*qcmd->string == '$')
3380 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3381 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3382 } else if(*qcmd->string != 0) {
3383 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3390 for(peer = peers; peer != NULL; peer = nextpeer)
3392 nextpeer = peer->next;
3393 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3395 if(*qcmd->string == '$')
3396 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3405 static void preinit(int hup)
3412 static int updateudpport(struct configvar *var, void *uudata)
3414 struct sockaddr_in addr;
3415 struct socket *newsock;
3417 memset(&addr, 0, sizeof(addr));
3418 addr.sin_family = AF_INET;
3419 addr.sin_port = htons(var->val.num);
3420 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3422 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3425 newsock->readcb = udpread;
3432 static int updatetcpport(struct configvar *var, void *uudata)
3434 struct sockaddr_in addr;
3435 struct socket *newsock;
3437 memset(&addr, 0, sizeof(addr));
3438 addr.sin_family = AF_INET;
3439 addr.sin_port = htons(var->val.num);
3440 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3441 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3448 static int init(int hup)
3450 struct sockaddr_in addr;
3454 GCBREG(sharechangecb, shareupdate, NULL);
3459 addr.sin_family = AF_INET;
3460 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3461 addr.sin_port = htons(confgetint("dc", "udpport"));
3462 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3464 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3467 udpsock->readcb = udpread;
3468 addr.sin_port = htons(confgetint("dc", "tcpport"));
3469 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3470 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3471 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3472 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3473 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3478 static void terminate(void)
3480 if(hmlistname != NULL)
3485 if(xmllistname != NULL)
3487 unlink(xmllistname);
3490 if(xmlbz2listname != NULL)
3492 unlink(xmlbz2listname);
3493 free(xmlbz2listname);
3497 static struct configvar myvars[] =
3499 {CONF_VAR_STRING, "desc", {.str = L""}},
3500 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3501 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3502 {CONF_VAR_INT, "udpport", {.num = 0}},
3503 {CONF_VAR_INT, "tcpport", {.num = 0}},
3507 static struct module me =
3516 .terminate = terminate,