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)
580 if(!wcscmp(peer->transfer->path, L"files.xml") || !wcscmp(peer->transfer->path, L"files.xml.bz2") || !wcscmp(peer->transfer->path, L"MyList.DcLst"))
582 if(!isfilelist && (peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
584 buf = base32encode(peer->transfer->hash->buf, 24);
585 ret = sprintf2("TTH/%.39s", buf);
588 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
590 ret = pathnmdc2adc(buf);
597 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
599 if(!wcscmp(cmd, L"resume"))
603 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
606 transfer->curpos = wcstol(arg, NULL, 10);
615 static void sendmynick(struct dcpeer *peer)
619 hub = (peer->fn == NULL)?NULL:(peer->fn->data);
621 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), DCCHARSET, "DoldaConnectUser-IN"));
623 qstrf(peer->sk, "$MyNick %s|", hub->nativenick);
626 static void sendpeerlock(struct dcpeer *peer)
628 #ifdef DCPP_MASQUERADE
629 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
631 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
635 static void sendsupports(struct dcpeer *peer)
637 #ifdef DCPP_MASQUERADE
638 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
640 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
644 static void requestfile(struct dcpeer *peer)
648 if(peer->transfer->size == -1)
650 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
652 transferseterror(peer->transfer, TRNSE_NOTFOUND);
656 /* The transfer will be restarted later from
657 * cmd_filelength when it detects that the sizes
659 qstrf(peer->sk, "$Get %s$1|", buf);
662 if((peer->transfer->hash == NULL) && !peer->notthl)
664 if(supports(peer, "adcget") && supports(peer, "tthl"))
666 qstr(peer->sk, "$ADCGET");
667 sendadc(peer->sk, "tthl");
668 if((buf = getadcid(peer)) == NULL)
670 transferseterror(peer->transfer, TRNSE_NOTFOUND);
674 sendadc(peer->sk, buf);
676 sendadc(peer->sk, "0");
677 sendadc(peer->sk, "-1");
684 if(forkfilter(peer->transfer))
686 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
690 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
693 if(supports(peer, "adcget"))
695 qstr(peer->sk, "$ADCGET");
696 sendadc(peer->sk, "file");
697 if((buf = getadcid(peer)) == NULL)
699 transferseterror(peer->transfer, TRNSE_NOTFOUND);
703 sendadc(peer->sk, buf);
705 sendadcf(peer->sk, "%i", peer->transfer->curpos);
706 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
708 } else if(supports(peer, "xmlbzlist")) {
709 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
711 transferseterror(peer->transfer, TRNSE_NOTFOUND);
715 qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
717 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
719 transferseterror(peer->transfer, TRNSE_NOTFOUND);
723 qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1);
727 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
731 struct fnetnode *cfn;
735 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
736 buf = tr(icswcstombs(confgetstr("dc", "desc"), DCCHARSET, "Charset_conv_failure"), "$_|_");
737 qstrf(sk, "%s", buf);
739 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
741 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
744 qstrf(sk, "<%s V:%s,M:%c,H:%i/0/0,S:%i>",
747 (tcpsock == NULL)?'P':'A',
749 confgetint("transfer", "slots")
752 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), DCCHARSET, "Charset_conv_failure"), "$_|_");
753 qstrf(sk, "%s\x01$", buf);
754 buf = tr(icswcstombs(confgetstr("dc", "email"), DCCHARSET, "Charset_conv_failure"), "$_|_");
755 qstrf(sk, "%s$", buf);
756 qstrf(sk, "%llu$|", sharesize);
759 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
764 if(!strcmp(cmd, "$Lock"))
766 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
767 } else if(!strcmp(cmd, "$Hello")) {
768 if(fn->state == FNN_HS)
770 qstrf(sk, "$Version 1,0091|");
771 qstrf(sk, "$GetNickList|");
773 fnetsetstate(fn, FNN_EST);
775 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
780 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
787 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
789 if((p = strchr(args, ' ')) != NULL)
793 #ifdef DCPP_MASQUERADE
794 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
796 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
799 key = dcmakekey(args);
800 qstrf(sk, "$Key %s|", key);
802 hubhandleaction(sk, fn, cmd, args);
805 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
811 if(hub->nativename == NULL)
812 free(hub->nativename);
813 hub->nativename = sstrdup(args);
814 buf = icmbstowcs(args, DCCHARSET);
815 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
818 hubhandleaction(sk, fn, cmd, args);
821 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
827 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
829 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
830 fnetaddpeer(fn, nick, nick);
832 hubhandleaction(sk, fn, cmd, args);
835 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
838 struct fnetpeer *peer;
842 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
844 if((peer = fnetfindpeer(fn, nick)) != NULL)
847 hubhandleaction(sk, fn, cmd, args);
850 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
855 struct fnetpeer *peer, *npeer;
858 for(peer = fn->peers; peer != NULL; peer = peer->next)
859 peer->flags.b.delete = 1;
860 while((p = strstr(args, "$$")) != NULL)
863 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
865 if((peer = fnetfindpeer(fn, buf)) == NULL)
866 peer = fnetaddpeer(fn, buf, buf);
868 peer->flags.b.delete = 0;
870 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
874 for(peer = fn->peers; peer != NULL; peer = npeer)
877 if(peer->flags.b.delete)
880 hubhandleaction(sk, fn, cmd, args);
883 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
888 struct fnetpeer *peer;
891 for(peer = fn->peers; peer != NULL; peer = peer->next)
892 peer->flags.b.op = 0;
893 while((p = strstr(args, "$$")) != NULL)
896 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
898 if((peer = fnetfindpeer(fn, buf)) != NULL)
899 peer->flags.b.op = 1;
904 hubhandleaction(sk, fn, cmd, args);
907 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
910 wchar_t *buf, *wp, *wp2;
912 struct fnetpeer *peer;
917 if(strncmp(p, "$ALL ", 5))
920 if((p2 = strchr(p, ' ')) == NULL)
923 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
925 if((peer = fnetfindpeer(fn, buf)) == NULL)
926 peer = fnetaddpeer(fn, buf, buf);
929 if((p2 = strstr(p, "$ $")) == NULL)
932 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
934 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcschr(buf, L'<')) != NULL))
936 buf[wcslen(buf) - 1] = L'\0';
938 if((wp2 = wcschr(wp, L' ')) != NULL)
941 fnetpeersetstr(peer, L"dc-client", wp);
945 if((wp2 = wcschr(wp, L',')) != NULL)
949 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
950 fnetpeersetstr(peer, abuf, wp + 2);
952 } while(wp2 != NULL);
955 fnetpeersetstr(peer, L"descr", buf);
958 if((p2 = strchr(p, '$')) == NULL)
961 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
963 fnetpeersetstr(peer, L"dc-speed", buf);
966 if((p2 = strchr(p, '$')) == NULL)
969 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
971 fnetpeersetstr(peer, L"email", buf);
976 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
977 hubhandleaction(sk, fn, cmd, args);
980 /* I do not implement the fully in that I do not disconnect from the
981 * old hub. I do this since I believe that if the hub owner really
982 * wants to get rid of us, then it is his responsibility to disconnect
984 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
987 struct fnetnode *newfn;
991 if(strchr(args, ':') == NULL)
993 args = strcpy(smalloc(strlen(args) + 5), args);
994 strcat(args, ":411");
999 if((newfn = fnetinitconnect(L"dc", args, NULL)) != NULL)
1001 linkfnetnode(newfn);
1004 hubhandleaction(sk, fn, cmd, args);
1009 static char *getdcpath(struct sharecache *node, size_t *retlen)
1014 if(node->parent == NULL)
1016 if(node->parent == shareroot)
1018 if((buf = icwcstombs(node->name, DCCHARSET)) == NULL)
1021 *retlen = strlen(buf);
1024 if((buf2 = icwcstombs(node->name, DCCHARSET)) == NULL)
1026 if((buf = getdcpath(node->parent, &len)) == NULL)
1031 len2 = strlen(buf2);
1032 buf = srealloc(buf, len + 1 + len2 + 1);
1034 strcpy(buf + len, buf2);
1035 buf[len + len2] = 0;
1038 *retlen = len + len2;
1044 * This is the main share searching function for Direct Connect
1045 * peers. Feel free to optimize it if you feel the need for it. I
1046 * haven't ever seen it take any noticable CPU time anyway, so I
1047 * haven't felt that need.
1049 * It may feel dubious to just return the directory when all terms are
1050 * satisfied rather than all the files in it, but it really benefits
1051 * everyone: Less cycles used for us, less UDP squelching for active
1052 * searchers, and less bandwidth waste for hubs when serving passive
1055 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1060 char *prefix, *infix, *postfix, *buf, *buf2;
1062 struct sockaddr_in addr;
1063 struct sharecache *node;
1064 int minsize, maxsize;
1066 int termnum, satisfied, skipcheck;
1067 int level, tersat[32];
1072 if((p = strchr(args, ' ')) == NULL)
1076 memset(terms, 0, sizeof(terms));
1077 prefix = infix = postfix = NULL;
1081 if(!strncmp(args, "Hub:", 4))
1083 if(!strcmp(cmd, "$MultiSearch"))
1085 if(!strcmp(args + 4, hub->nativenick))
1087 prefix = sprintf2("$SR %s ", hub->nativenick);
1088 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1089 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1093 if((p2 = strchr(args, ':')) == NULL)
1096 addr.sin_family = AF_INET;
1097 if(!inet_aton(args, &addr.sin_addr))
1099 addr.sin_port = htons(atoi(p2));
1100 prefix = sprintf2("$SR %s ", hub->nativenick);
1101 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1102 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1103 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1106 minsize = maxsize = -1;
1115 if((*(p++) == 'T') && (minsize == 0))
1122 if((p2 = strchr(p, '?')) == NULL)
1137 if((*p2 == 0) || (*p2 == '$'))
1145 if(!dotth && !strncmp(p, "TTH:", 4))
1148 if((buf = base32decode(p + 4, &buflen)) == NULL)
1152 memcpy(hashtth, buf, 24);
1155 if((terms[termnum] = icmbstowcs(p, DCCHARSET)) != NULL)
1166 node = shareroot->child;
1168 for(i = 0; i < termnum; i++)
1174 if(node->f.b.type == FILE_REG)
1176 if((minsize >= 0) && (node->size < minsize))
1178 if((maxsize >= 0) && (node->size > maxsize))
1181 if(!skipcheck && dotth)
1183 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1188 for(i = 0; i < termnum; i++)
1192 if(wcsexists(node->name, terms[i]))
1196 } else if(node->child == NULL) {
1201 if(!skipcheck && (satisfied == termnum))
1203 if((buf = getdcpath(node, NULL)) != NULL)
1205 if(node->f.b.hastth)
1207 buf2 = base32encode(node->hashtth, 24);
1208 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1211 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1216 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1218 while(node->next == NULL)
1220 if((node = node->parent) == shareroot)
1224 if(node == shareroot)
1226 for(i = 0; i < termnum; i++)
1228 if(tersat[i] >= level)
1241 hubhandleaction(sk, fn, cmd, args);
1252 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1256 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1260 struct socket *newsk;
1261 struct sockaddr_in addr;
1264 if((p = strchr(args, ' ')) == NULL)
1267 if(strcmp(args, hub->nativenick))
1269 addr.sin_family = AF_INET;
1271 if((p = strchr(args, ':')) == NULL)
1274 addr.sin_port = htons(atoi(p));
1275 if(!inet_aton(args, &addr.sin_addr))
1277 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1279 hubhandleaction(sk, fn, cmd, args);
1282 static void sendctm(struct socket *sk, char *nick)
1284 struct sockaddr *addr;
1289 if(sockgetremotename(tcpsock, &addr, &addrlen) < 0)
1291 if(addr->sa_family == AF_INET)
1292 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1294 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1298 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1304 if((p = strchr(args, ' ')) == NULL)
1307 if(strcmp(p, hub->nativenick))
1310 expectpeer(args, fn);
1311 hubhandleaction(sk, fn, cmd, args);
1314 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1317 struct fnetnode *node;
1322 for(node = fnetnodes; node != NULL; node = node->next)
1324 if(node->state == FNN_EST)
1327 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1328 hubhandleaction(sk, fn, cmd, args);
1331 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1338 if((p2 = strchr(p, ' ')) == NULL)
1342 if((p2 = strchr(p, ' ')) == NULL)
1345 if(strcmp(p, "From:"))
1348 if((p2 = strstr(p, " $")) == NULL)
1352 hubrecvchat(fn->sk, fn, p, p2);
1353 hubhandleaction(sk, fn, cmd, args);
1356 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1360 char *nick, *filename, *hubname;
1364 wchar_t *wnick, *wfile;
1368 if((p2 = strchr(p, ' ')) == NULL)
1373 if((p2 = strchr(p, 5)) == NULL)
1377 if((p2 = strchr(p, ' ')) == NULL)
1382 if((p2 = strchr(p, '/')) == NULL)
1387 if((p2 = strchr(p, 5)) == NULL)
1391 if((p2 = strstr(p, " (")) == NULL)
1394 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
1396 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1401 sr = newsrchres(&dcnet, wfile, wnick);
1402 if(sr->peernick != NULL)
1404 sr->peernick = swcsdup(wnick);
1409 if(!strncmp(hubname, "TTH:", 4))
1411 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1414 sr->hash = newhash(L"TTH", 24, buf);
1418 getfnetnode(sr->fn = fn);
1421 hubhandleaction(sk, fn, cmd, args);
1424 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1426 /* Do nothing for now. */
1429 static void cmd_getpass(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1432 struct wcspair *arg;
1436 for(arg = fn->args; arg != NULL; arg = arg->next)
1438 if(!wcscmp(arg->key, L"password"))
1441 if((arg == NULL) || ((mbspw = icwcstombs(arg->val, DCCHARSET)) == NULL))
1446 qstrf(sk, "$MyPass %s|", mbspw);
1448 fn->regstatus = FNNS_REG;
1449 hubhandleaction(sk, fn, cmd, args);
1452 static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1457 fn->regstatus = FNNS_OP;
1458 hubhandleaction(sk, fn, cmd, args);
1461 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1463 struct dcexppeer *expect;
1465 if(peer->nativename != NULL)
1466 free(peer->nativename);
1467 peer->nativename = sstrdup(args);
1468 if(peer->wcsname != NULL)
1469 free(peer->wcsname);
1470 if((peer->wcsname = icmbstowcs(peer->nativename, DCCHARSET)) == NULL)
1477 for(expect = expected; expect != NULL; expect = expect->next)
1479 if(!strcmp(expect->nick, args))
1486 peer->fn = expect->fn;
1487 getfnetnode(peer->fn);
1488 freeexppeer(expect);
1493 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1497 struct transfer *transfer;
1499 if((p = strchr(args, ' ')) == NULL)
1502 if(!strcmp(args, "Upload"))
1504 if(!strcmp(args, "Download"))
1508 if((peer->transfer == NULL) || (mydir != peer->direction))
1513 if(peer->direction == TRNSD_DOWN)
1516 if(peer->wcsname == NULL)
1521 peer->direction = mydir;
1522 if(peer->direction == TRNSD_UP)
1524 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1526 if((transfer = finddownload(peer->wcsname)) == NULL)
1531 transferattach(transfer, &dctransfer, peer);
1532 transfersetstate(transfer, TRNS_HS);
1534 transfersetnick(transfer, peer->wcsname);
1535 peer->transfer = transfer;
1538 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1539 if(peer->key != NULL)
1540 qstrf(sk, "$Key %s|", peer->key);
1541 if(peer->direction == TRNSD_DOWN)
1546 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1549 struct transfer *transfer;
1551 if((p = strchr(args, ' ')) == NULL)
1554 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1556 key = dcmakekey(args);
1559 if(peer->wcsname == NULL)
1568 if((transfer = finddownload(peer->wcsname)) == NULL)
1570 peer->direction = TRNSD_UP;
1571 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1573 peer->direction = TRNSD_DOWN;
1574 transferattach(transfer, &dctransfer, peer);
1575 transfersetstate(transfer, TRNS_HS);
1577 transfersetnick(transfer, peer->wcsname);
1578 peer->transfer = transfer;
1579 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1580 qstrf(sk, "$Key %s|", key);
1582 if(peer->key != NULL)
1588 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1593 static void startdl(struct dcpeer *peer)
1595 peer->state = PEER_TRNS;
1596 transferstartdl(peer->transfer, peer->sk);
1597 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1598 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1601 static void startul(struct dcpeer *peer)
1603 peer->state = PEER_TRNS;
1604 transferstartul(peer->transfer, peer->sk);
1605 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1608 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1612 if(peer->transfer == NULL)
1618 if(peer->transfer->size != size)
1620 transfersetsize(peer->transfer, size);
1625 qstr(peer->sk, "$Send|");
1628 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1630 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1632 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1633 resettransfer(peer->transfer);
1639 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1641 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1643 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1644 resettransfer(peer->transfer);
1657 {"MyList.DcLst", &hmlistname, updatehmlist},
1658 {"files.xml", &xmllistname, updatexmllist},
1659 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1663 static int openfilelist(char *name)
1668 for(i = 0; lists[i].name != NULL; i++)
1670 if(!strcmp(name, lists[i].name))
1674 if(lists[i].name == NULL)
1677 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1679 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1682 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1689 static struct sharecache *findbytth(char *tth32)
1693 struct sharecache *node;
1695 if((buf = base32decode(tth32, &buflen)) == NULL)
1702 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1704 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1711 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1713 struct sharecache *node;
1720 if((p2 = strchr(p, sep)) != NULL)
1722 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1729 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1734 struct sharecache *node;
1735 struct socket *lesk;
1739 if(peer->transfer == NULL)
1744 if((p = strchr(args, '$')) == NULL)
1750 if((offset = (atoi(p) - 1)) < 0)
1755 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1757 qstr(sk, "$Error Could not send file list|");
1760 } else if(fd >= 0) {
1761 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1762 transfersetpath(peer->transfer, buf2);
1768 qstr(sk, "$MaxedOut|");
1772 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1774 qstrf(sk, "$Error File not in share|");
1778 if((fd = opensharecache(node)) < 0)
1780 qstrf(sk, "$Error %s|", strerror(errno));
1784 buf = getfspath(node);
1785 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1786 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1788 transfersetpath(peer->transfer, buf2);
1791 if(fstat(fd, &sb) < 0)
1794 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1795 qstrf(sk, "$Error|");
1799 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1802 qstrf(sk, "$Error Offset out of range|");
1806 lesk = wrapsock(fd);
1807 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1809 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1812 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1814 if(peer->transfer == NULL)
1819 if(peer->transfer->localend == NULL)
1828 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1833 size_t arrsize, arrdata;
1835 if(peer->supports != NULL)
1837 for(i = 0; peer->supports[i] != NULL; i++)
1838 free(peer->supports[i]);
1839 free(peer->supports);
1842 arrsize = arrdata = 0;
1846 if((p2 = strchr(p, ' ')) != NULL)
1850 addtobuf(arr, sstrdup(p));
1851 } while((p = p2) != NULL);
1852 addtobuf(arr, NULL);
1853 peer->supports = arr;
1856 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1860 int start, numbytes;
1861 char *charset, *buf;
1863 struct sharecache *node;
1865 struct socket *lesk;
1867 if(peer->transfer == NULL)
1873 if((p2 = strchr(p, ' ')) == NULL)
1881 if((p2 = strchr(p, ' ')) == NULL)
1889 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1892 charset = DCCHARSET;
1893 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1894 initcompress(peer, CPRS_ZLIB);
1895 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1897 qstr(sk, "$Error Could not send file list|");
1899 } else if(fd >= 0) {
1900 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1901 transfersetpath(peer->transfer, buf2);
1907 qstr(sk, "$MaxedOut|");
1910 if((node = resdcpath(p, charset, '\\')) == NULL)
1912 qstr(sk, "$Error File not in cache|");
1915 if((fd = opensharecache(node)) < 0)
1917 qstrf(sk, "$Error %s|", strerror(errno));
1920 buf = getfspath(node);
1921 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1922 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1924 transfersetpath(peer->transfer, buf2);
1927 if(fstat(fd, &sb) < 0)
1930 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1931 qstr(sk, "$Error|");
1934 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1937 qstr(sk, "$Error Offset out of range|");
1940 if((numbytes < 0) || (start + numbytes > sb.st_size))
1941 numbytes = sb.st_size - start;
1942 lesk = wrapsock(fd);
1943 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1945 qstrf(sk, "$Sending %i|", numbytes);
1949 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1953 int start, numbytes;
1954 struct sharecache *node;
1956 struct socket *lesk;
1960 if(peer->transfer == NULL)
1965 if((argv = parseadc(args)) == NULL)
1970 if(parrlen(argv) < 4)
1975 start = atoi(argv[2]);
1976 numbytes = atoi(argv[3]);
1979 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
1981 qstr(sk, "$Error Could not send file list|");
1983 } else if(fd >= 0) {
1984 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
1985 transfersetpath(peer->transfer, wbuf);
1991 qstr(sk, "$MaxedOut|");
1994 if(!strncmp(argv[1], "TTH/", 4))
1996 if((node = findbytth(argv[1] + 4)) == NULL)
1998 qstr(sk, "$Error File not in cache|");
2002 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
2004 qstr(sk, "$Error File not in cache|");
2008 if((fd = opensharecache(node)) < 0)
2010 qstrf(sk, "$Error %s|", strerror(errno));
2013 buf = getfspath(node);
2014 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
2015 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2017 transfersetpath(peer->transfer, wbuf);
2020 if(!strcmp(argv[0], "file"))
2022 for(i = 4; argv[i] != NULL; i++)
2024 if(!strcmp(argv[i], "ZL1"))
2025 initcompress(peer, CPRS_ZLIB);
2027 if(fstat(fd, &sb) < 0)
2029 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2030 qstr(sk, "$Error|");
2033 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2035 qstr(sk, "$Error Offset out of range|");
2038 if((numbytes < 0) || (start + numbytes > sb.st_size))
2039 numbytes = sb.st_size - start;
2040 lesk = wrapsock(fd);
2041 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2044 qstr(sk, "$ADCSND");
2045 sendadc(sk, "file");
2046 sendadc(sk, argv[1]);
2047 sendadcf(sk, "%i", start);
2048 sendadcf(sk, "%i", numbytes);
2049 if(peer->compress == CPRS_ZLIB)
2053 } else if(!strcmp(argv[0], "tthl")) {
2055 * XXX: Implement full TTHL support.
2057 * In the meantime, this is better than nothing, since it at
2058 * least allows fetching of the TTH root if it isn't already
2063 qstr(sk, "$Error no TTHL data for virtual files|");
2066 qstr(sk, "$ADCSND");
2067 sendadc(sk, "tthl");
2068 sendadc(sk, argv[1]);
2072 sockqueue(sk, node->hashtth, 24);
2074 qstr(sk, "$Error Namespace not implemented|");
2084 static void handletthl(struct dcpeer *peer)
2088 while(peer->inbufdata >= 24)
2090 pushtigertree(&peer->tth, peer->inbuf);
2091 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
2092 peer->curread += 24;
2094 if(peer->curread >= peer->totalsize)
2096 peer->state = PEER_CMD;
2097 synctigertree(&peer->tth);
2098 restigertree(&peer->tth, buf);
2099 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2104 static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2107 int start, numbytes;
2109 if(peer->transfer == NULL)
2114 if((argv = parseadc(args)) == NULL)
2119 if(parrlen(argv) < 4)
2124 start = atoi(argv[2]);
2125 numbytes = atoi(argv[3]);
2126 if(!strcmp(argv[0], "tthl"))
2128 if((start != 0) || (numbytes % 24 != 0))
2130 /* Weird. Bail out. */
2134 peer->state = PEER_TTHL;
2135 peer->totalsize = numbytes;
2137 inittigertree(&peer->tth);
2139 } else if(!strcmp(argv[0], "file")) {
2140 if(start != peer->transfer->curpos)
2145 if(start + numbytes != peer->transfer->size)
2147 transfersetsize(peer->transfer, start + numbytes);
2152 if(peer->inbufdata > 0)
2154 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2155 peer->inbufdata = 0;
2156 transread(sk, peer);
2159 /* We certainly didn't request this...*/
2168 static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2172 if(peer->transfer == NULL)
2177 numbytes = atoi(args);
2178 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2180 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2185 if(peer->inbufdata > 0)
2187 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2188 peer->inbufdata = 0;
2189 transread(sk, peer);
2195 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2200 hubhandleaction(sk, fn, cmd, args);
2204 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2210 static int hubreqconn(struct fnetpeer *peer)
2215 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2220 if((hub = peer->fn->data) == NULL)
2225 if((mbsnick = icwcstombs(peer->id, DCCHARSET)) == NULL)
2226 return(1); /* Shouldn't happen, of course, but who knows... */
2229 sendctm(peer->fn->sk, mbsnick);
2230 expectpeer(mbsnick, peer->fn);
2232 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2238 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2241 char *mbsstring, *mbsto;
2244 if((mbsto = icwcstombs(to, DCCHARSET)) == NULL)
2249 if((mbsstring = icwcstombs(string, DCCHARSET)) == NULL)
2254 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2261 if(strchr(mbsstring, '|') != NULL)
2272 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2274 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2277 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2284 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2286 int minl, maxl, minr, maxr, retmin, retmax;
2291 findsizelimit(sexpr->l, &minl, &maxl);
2292 findsizelimit(sexpr->r, &minr, &maxr);
2293 retmin = (minl > minr)?minl:minr;
2294 if((maxl != -1) && (maxr != -1))
2295 retmax = (maxl < maxr)?maxl:maxr;
2304 findsizelimit(sexpr->l, &minl, &maxl);
2305 findsizelimit(sexpr->r, &minr, &maxr);
2306 retmin = (minl < minr)?minl:minr;
2307 if((maxl == -1) || (maxr == -1))
2310 retmax = (maxl > maxr)?maxl:maxr;
2313 findsizelimit(sexpr->l, &minl, &maxl);
2314 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2318 } else if((minl == 0) && (maxl != -1)) {
2321 } else if((minl != 0) && (maxl == -1)) {
2324 } else { /* This would yield two seperate intervals, which DC cannot handle */
2330 retmax = sexpr->d.n - 1;
2333 retmin = sexpr->d.n;
2334 retmax = sexpr->d.n;
2337 retmin = sexpr->d.n + 1;
2351 static struct hash *findsehash(struct sexpr *sexpr)
2353 struct hash *h1, *h2;
2358 if((h1 = findsehash(sexpr->l)) != NULL)
2360 if((h1 = findsehash(sexpr->r)) != NULL)
2364 h1 = findsehash(sexpr->l);
2365 h2 = findsehash(sexpr->r);
2370 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2371 return(sexpr->d.hash);
2378 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2381 struct wcslist *list, *cur;
2382 char *sstr, *buf, *p;
2383 size_t sstrsize, sstrdata;
2384 struct sockaddr *name;
2386 int minsize, maxsize;
2390 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2392 list = findsexprstrs(srch->sexpr);
2393 findsizelimit(srch->sexpr, &minsize, &maxsize);
2394 hash = findsehash(srch->sexpr);
2395 if((minsize != 0) && (maxsize != -1))
2397 /* Choose either minsize or maxsize by trying to determine
2398 * which choice will be most restrictive. The result can be
2399 * approximative at best anyway, so don't try too hard... */
2400 if((50000000 - maxsize) > (minsize - 50000000))
2406 sstrsize = sstrdata = 0;
2407 if((hash != NULL) && (hash->len == 24))
2409 /* Prioritize hash searches above all else */
2410 bufcat(sstr, "F?T?0?9?TTH:", 12);
2411 buf = base32encode(hash->buf, hash->len);
2413 bufcat(sstr, buf, 39);
2418 sizebuf2(sstr, sstrdata + 32, 1);
2419 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
2420 } else if(maxsize != -1) {
2421 sizebuf2(sstr, sstrdata + 32, 1);
2422 snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
2424 bufcat(sstr, "F?F?0?1?", 8);
2428 for(cur = list; cur != NULL; cur = cur->next)
2430 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2432 /* Can't find anything anyway if the search expression
2433 * requires characters outside DC's charset. There's
2434 * nothing technically wrong with the search itself,
2435 * however, so return success. This should be
2436 * considered as an optimization. */
2443 addtobuf(sstr, '$');
2445 * It probably doesn't hurt if buf contains any extra
2446 * dollar signs - it will just result in extra search
2447 * terms, and the extraneous results will be filtered by
2448 * the search layer anyway. It hurts if it contains any
2449 * pipes, though, so let's sell them for money.
2451 for(p = buf; *p; p++)
2456 bufcat(sstr, buf, strlen(buf));
2460 /* Will match all files... :-/ */
2461 addtobuf(sstr, '.');
2467 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2469 flog(LOG_WARNING, "cannot get address of UDP socket");
2471 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
2475 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
2479 fn->lastsrch = time(NULL);
2487 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2488 struct command hubcmds[] =
2490 {"$Lock", cc(cmd_lock)},
2491 {"$HubName", cc(cmd_hubname)},
2492 {"$Hello", cc(cmd_hello)},
2493 {"$Quit", cc(cmd_quit)},
2494 {"$NickList", cc(cmd_nicklist)},
2495 {"$OpList", cc(cmd_oplist)},
2496 {"$MyINFO", cc(cmd_myinfo)},
2497 {"$ForceMove", cc(cmd_forcemove)},
2498 {"$Search", cc(cmd_search)},
2499 {"$MultiSearch", cc(cmd_search)},
2500 {"$ConnectToMe", cc(cmd_connecttome)},
2501 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2502 {"$GetNetInfo", cc(cmd_getnetinfo)},
2503 {"$To:", cc(cmd_to)},
2504 {"$SR", cc(cmd_sr)},
2505 {"$UserCommand", cc(cmd_usercommand)},
2506 {"$GetPass", cc(cmd_getpass)},
2507 {"$LogedIn", cc(cmd_logedin)}, /* sic */
2511 struct command peercmds[] =
2513 {"$MyNick", cc(cmd_mynick)},
2514 {"$Lock", cc(cmd_peerlock)},
2515 {"$Direction", cc(cmd_direction)},
2516 {"$Key", cc(cmd_key)},
2517 {"$FileLength", cc(cmd_filelength)},
2518 {"$Error", cc(cmd_error)},
2519 {"$MaxedOut", cc(cmd_maxedout)},
2520 {"$Get", cc(cmd_get)},
2521 {"$Send", cc(cmd_send)},
2522 {"$Supports", cc(cmd_supports)},
2523 {"$GetBlock", cc(cmd_getblock)},
2524 {"$UGetBlock", cc(cmd_getblock)},
2525 {"$GetZBlock", cc(cmd_getblock)},
2526 {"$UGetZBlock", cc(cmd_getblock)},
2527 {"$ADCGET", cc(cmd_adcget)},
2528 {"$ADCSND", cc(cmd_adcsnd), 1},
2529 {"$Sending", cc(cmd_sending), 1},
2534 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2536 CBUNREG(transfer, trans_filterout, peer);
2539 peer->transfer = NULL;
2543 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2551 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2553 if(sockqueuesize(peer->sk) < 65536)
2555 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2557 if(peer->compress == CPRS_NONE)
2559 sockqueue(peer->sk, buf, bufsize);
2560 } else if(peer->compress == CPRS_ZLIB) {
2561 cstr = peer->cprsdata;
2562 cstr->next_in = buf;
2563 cstr->avail_in = bufsize;
2564 while(cstr->avail_in > 0)
2566 cstr->next_out = outbuf;
2567 cstr->avail_out = sizeof(outbuf);
2568 if((ret = deflate(cstr, 0)) != Z_OK)
2570 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2574 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2579 if(peer->state == PEER_SYNC)
2581 if(peer->compress == CPRS_ZLIB)
2583 cstr = peer->cprsdata;
2584 cstr->next_in = NULL;
2588 cstr->next_out = outbuf;
2589 cstr->avail_out = sizeof(outbuf);
2590 ret = deflate(cstr, Z_FINISH);
2591 if((ret != Z_OK) && (ret != Z_STREAM_END))
2593 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2597 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2598 } while(ret != Z_STREAM_END);
2604 peer->state = PEER_CMD;
2606 transfersetstate(transfer, TRNS_HS);
2607 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2608 peer->sk->writecb = NULL;
2615 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2617 peer->state = PEER_SYNC;
2618 dctransgotdata(transfer, peer);
2621 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2623 if(transferdatasize(transfer) < 65536)
2624 peer->sk->ignread = 0;
2627 static void transread(struct socket *sk, struct dcpeer *peer)
2631 struct transfer *transfer;
2633 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2635 if(peer->transfer == NULL)
2641 transferputdata(peer->transfer, buf, bufsize);
2643 if(peer->transfer->curpos >= peer->transfer->size)
2645 transfer = peer->transfer;
2646 transferdetach(transfer);
2647 transferendofdata(transfer);
2650 if(transferdatasize(peer->transfer) > 65535)
2654 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2656 struct transfer *transfer;
2658 if((transfer = peer->transfer) == NULL)
2663 transferdetach(transfer);
2664 transferendofdata(transfer);
2667 static void transwrite(struct socket *sk, struct dcpeer *peer)
2669 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2671 if(peer->transfer == NULL)
2676 dctransgotdata(peer->transfer, peer);
2679 static void udpread(struct socket *sk, void *data)
2681 char *buf, *p, *p2, *hashbuf;
2682 size_t buflen, hashlen;
2683 char *nick, *filename, *hubname;
2685 struct fnetnode *fn, *myfn;
2688 wchar_t *wnick, *wfile;
2691 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2693 buf = srealloc(buf, buflen + 1);
2695 if(!strncmp(buf, "$SR ", 4))
2699 if((p2 = strchr(p, ' ')) == NULL)
2707 if((p2 = strchr(p, 5)) == NULL)
2714 if((p2 = strchr(p, ' ')) == NULL)
2722 if((p2 = strchr(p, '/')) == NULL)
2730 if((p2 = strchr(p, 5)) == NULL)
2737 if((p2 = strstr(p, " (")) == NULL)
2743 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
2748 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2756 if(!strncmp(hubname, "TTH:", 4))
2758 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2761 hash = newhash(L"TTH", 24, hashbuf);
2765 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2767 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2769 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2782 sr = newsrchres(&dcnet, wfile, wnick);
2783 if(sr->peernick != NULL)
2785 sr->peernick = swcsdup(wnick);
2791 getfnetnode(sr->fn = myfn);
2800 static void hubread(struct socket *sk, struct fnetnode *fn)
2807 hub = (struct dchub *)fn->data;
2808 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2810 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2812 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2813 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2815 p = hub->inbuf + hub->inbufdata;
2816 hub->inbufdata += datalen;
2817 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2820 newqcmd(&hub->queue, hub->inbuf);
2821 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2822 datalen = hub->inbufdata;
2827 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2832 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2838 if((buf = icwcstombs(newnick, DCCHARSET)) == NULL)
2840 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2845 if(hub == NULL) /* Not yet connected */
2850 if(hub->nativenick != NULL)
2851 free(hub->nativenick);
2852 hub->nativenick = buf;
2856 static struct dchub *newdchub(struct fnetnode *fn)
2860 new = smalloc(sizeof(*new));
2861 memset(new, 0, sizeof(*new));
2863 if(hubsetnick(fn, fn->mynick))
2864 fnetsetnick(fn, L"DoldaConnectUser-IN");
2865 /* IN as in Invalid Nick */
2869 static struct dcpeer *newdcpeer(struct socket *sk)
2873 new = smalloc(sizeof(*new));
2874 memset(new, 0, sizeof(*new));
2875 new->transfer = NULL;
2887 static void freedcpeer(struct dcpeer *peer)
2890 struct qcommand *qcmd;
2895 if(peer->next != NULL)
2896 peer->next->prev = peer->prev;
2897 if(peer->prev != NULL)
2898 peer->prev->next = peer->next;
2899 if(peer->transfer != NULL)
2901 if(peer->transfer->dir == TRNSD_UP)
2902 peer->transfer->close = 1;
2903 if(peer->transfer->dir == TRNSD_DOWN)
2904 resettransfer(peer->transfer);
2905 transferdetach(peer->transfer);
2907 if(peer->sk->data == peer)
2908 peer->sk->data = NULL;
2909 peer->sk->readcb = NULL;
2910 peer->sk->writecb = NULL;
2911 peer->sk->errcb = NULL;
2914 if(peer->supports != NULL)
2916 for(i = 0; peer->supports[i] != NULL; i++)
2917 free(peer->supports[i]);
2918 free(peer->supports);
2920 if(peer->inbuf != NULL)
2922 if(peer->key != NULL)
2924 if(peer->wcsname != NULL)
2925 free(peer->wcsname);
2926 if(peer->nativename != NULL)
2927 free(peer->nativename);
2928 if(peer->fn != NULL)
2929 putfnetnode(peer->fn);
2930 while((qcmd = ulqcmd(&peer->queue)) != NULL)
2936 static void hubconnect(struct fnetnode *fn)
2938 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
2939 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
2941 fn->data = newdchub(fn);
2946 static void hubdestroy(struct fnetnode *fn)
2949 struct qcommand *qcmd;
2951 hub = (struct dchub *)fn->data;
2954 if(fn->sk->data == fn)
2956 fn->sk->data = NULL;
2962 while((qcmd = ulqcmd(&hub->queue)) != NULL)
2964 if(hub->nativename != NULL)
2965 free(hub->nativename);
2966 if(hub->nativenick != NULL)
2967 free(hub->nativenick);
2968 if(hub->inbuf != NULL)
2973 static wchar_t *dcbasename(wchar_t *filename)
2977 if((ret = wcsrchr(filename, L'\\')) != NULL)
2982 static struct transferiface dctransfer =
2984 .detach = (void (*)(struct transfer *, void *))dctransdetach,
2985 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
2986 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
2987 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
2990 static struct fnet dcnet =
2993 .connect = hubconnect,
2994 .destroy = hubdestroy,
2995 .setnick = hubsetnick,
2996 .reqconn = hubreqconn,
2997 .sendchat = hubsendchat,
2998 .search = hubsearch,
2999 .filebasename = dcbasename
3002 static void peerread(struct socket *sk, struct dcpeer *peer)
3006 struct command *cmd;
3008 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
3010 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
3011 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
3013 peer->inbufdata += datalen;
3014 if(peer->state == PEER_CMD)
3017 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
3020 newqcmd(&peer->queue, peer->inbuf);
3021 for(cmd = peercmds; cmd->handler != NULL; cmd++)
3023 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
3026 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
3029 peer->state = PEER_STOP;
3033 } else if(peer->state == PEER_TTHL) {
3038 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3043 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3045 struct dcpeer *peer;
3052 peer = newdcpeer(sk);
3055 sk->readcb = (void (*)(struct socket *, void *))peerread;
3056 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3058 socksettos(sk, confgetint("fnet", "fnptos"));
3064 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3066 struct dcpeer *peer;
3068 peer = newdcpeer(newsk);
3070 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3071 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3073 socksettos(newsk, confgetint("fnet", "fnptos"));
3076 static void updatehmlist(void)
3078 int i, lev, ic, ret;
3079 struct sharecache *node;
3080 char *buf, *buf2, numbuf[32];
3081 size_t bufsize, bufdata;
3085 buf = smalloc(bufsize = 65536);
3086 node = shareroot->child;
3091 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3093 for(i = 0; i < lev; i++)
3095 bufcat(buf, buf2, strlen(buf2));
3097 if(node->f.b.type == FILE_REG)
3100 sprintf(numbuf, "%i", node->size);
3101 bufcat(buf, numbuf, strlen(numbuf));
3108 if((node->child != NULL) && !ic)
3112 } else if(node->next != NULL) {
3115 while(node->next == NULL)
3118 node = node->parent;
3119 if(node == shareroot)
3122 if(node == shareroot)
3127 if(hmlistname != NULL)
3132 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3133 if((fd = mkstemp(hmlistname)) < 0)
3135 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3140 * I do not want to implement a good Huffman encoder, and it's not
3141 * like Huffman encoding actually yields any impressive results
3142 * for DC file lists anyway, so I'll just output a bogus
3143 * tree. Implement a good encoder if you want to.
3145 write(fd, "HE3\r\0", 5);
3146 write(fd, &bufdata, 4);
3148 write(fd, &ibuf, 2);
3150 for(i = 0; i < 256; i++)
3153 write(fd, &ibuf, 1);
3155 for(i = 0; i < 256; i++)
3157 for(buf2 = buf; bufdata > 0;)
3159 if((ret = write(fd, buf2, bufdata)) < 0)
3161 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3172 static struct xmlent
3179 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
3183 static wchar_t *escapexml(wchar_t *src)
3185 static wchar_t *buf = NULL;;
3188 size_t bufsize, bufdata;
3194 bufsize = bufdata = 0;
3195 for(; *src != L'\0'; src++)
3198 if((c > 0) && (c < 32))
3200 bufcat(buf, L"&#", 2);
3201 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3202 bufcat(buf, nbuf, ret);
3203 addtobuf(buf, L';');
3205 for(ent = entities; ent->ent != NULL; ent++)
3209 bufcat(buf, ent->ent, wcslen(ent->ent));
3213 if(ent->ent == NULL)
3214 addtobuf(buf, *src);
3217 addtobuf(buf, L'\0');
3221 static void updatexmllist(void)
3225 char cidbuf[14], *namebuf;
3227 struct sharecache *node;
3229 if(xmllistname != NULL)
3231 unlink(xmllistname);
3234 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3235 if((fd = mkstemp(xmllistname)) < 0)
3237 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3242 if((fs = fdopen(fd, "w")) == NULL)
3244 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3245 unlink(xmllistname);
3251 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3252 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3253 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3255 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, DCIDFULL);
3257 node = shareroot->child;
3261 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3263 for(i = 0; i < lev; i++)
3265 if(node->child != NULL)
3267 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3272 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3273 if(node->f.b.hastth)
3275 hashbuf = base32encode(node->hashtth, 24);
3276 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3279 fprintf(fs, "/>\r\n");
3281 while(node->next == NULL)
3283 node = node->parent;
3284 if(node == shareroot)
3289 for(i = 0; i < lev; i++)
3291 fprintf(fs, "</Directory>\r\n");
3294 if(node == shareroot)
3300 #ifdef DCPP_MASQUERADE
3301 fprintf(fs, "</FileListing>");
3303 fprintf(fs, "</FileListing>\r\n");
3308 static void updatexmlbz2list(void)
3317 if(xmllistname == NULL)
3319 if((in = fopen(xmllistname, "r")) == NULL)
3321 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3324 if(xmlbz2listname != NULL)
3326 unlink(xmlbz2listname);
3327 free(xmlbz2listname);
3329 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3330 if((fd = mkstemp(xmlbz2listname)) < 0)
3332 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3333 free(xmlbz2listname);
3334 xmlbz2listname = NULL;
3338 if((real = fdopen(fd, "w")) == NULL)
3340 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3342 unlink(xmlbz2listname);
3343 free(xmlbz2listname);
3344 xmlbz2listname = NULL;
3348 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3351 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3353 unlink(xmlbz2listname);
3354 free(xmlbz2listname);
3355 xmlbz2listname = NULL;
3361 bufdata = fread(buf, 1, sizeof(buf), in);
3362 BZ2_bzWrite(&err, out, buf, bufdata);
3365 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3369 static int shareupdate(unsigned long long uusharesize, void *data)
3377 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3380 struct command *cmd;
3382 if((p = strchr(qcmd->string, ' ')) != NULL)
3384 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3386 if(!strcmp(cmd->name, qcmd->string))
3389 if(cmd->handler != NULL)
3390 cmd->handler(sk, data, qcmd->string, p);
3393 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3397 static int run(void)
3399 struct fnetnode *fn, *nextfn;
3401 struct dcpeer *peer, *nextpeer;
3402 struct qcommand *qcmd;
3406 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3409 if(fn->fnet != &dcnet)
3411 if(fn->data == NULL)
3413 hub = (struct dchub *)fn->data;
3414 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3416 if(*qcmd->string == '$')
3418 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3419 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3420 } else if(*qcmd->string != 0) {
3421 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3428 for(peer = peers; peer != NULL; peer = nextpeer)
3430 nextpeer = peer->next;
3431 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3433 if(*qcmd->string == '$')
3434 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3443 static void preinit(int hup)
3450 static int updateudpport(struct configvar *var, void *uudata)
3452 struct sockaddr_in addr;
3453 struct socket *newsock;
3455 memset(&addr, 0, sizeof(addr));
3456 addr.sin_family = AF_INET;
3457 addr.sin_port = htons(var->val.num);
3458 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3460 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3463 newsock->readcb = udpread;
3470 static int updatetcpport(struct configvar *var, void *uudata)
3472 struct sockaddr_in addr;
3473 struct socket *newsock;
3475 memset(&addr, 0, sizeof(addr));
3476 addr.sin_family = AF_INET;
3477 addr.sin_port = htons(var->val.num);
3478 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3479 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3486 static int init(int hup)
3488 struct sockaddr_in addr;
3492 GCBREG(sharechangecb, shareupdate, NULL);
3497 addr.sin_family = AF_INET;
3498 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3499 addr.sin_port = htons(confgetint("dc", "udpport"));
3500 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3502 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3505 udpsock->readcb = udpread;
3506 addr.sin_port = htons(confgetint("dc", "tcpport"));
3507 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3508 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3509 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3510 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3511 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3516 static void terminate(void)
3518 if(hmlistname != NULL)
3523 if(xmllistname != NULL)
3525 unlink(xmllistname);
3528 if(xmlbz2listname != NULL)
3530 unlink(xmlbz2listname);
3531 free(xmlbz2listname);
3535 static struct configvar myvars[] =
3537 {CONF_VAR_STRING, "desc", {.str = L""}},
3538 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3539 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3540 {CONF_VAR_INT, "udpport", {.num = 0}},
3541 {CONF_VAR_INT, "tcpport", {.num = 0}},
3545 static struct module me =
3554 .terminate = terminate,