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"
49 * The Direct Connect protocol is extremely ugly. Thus, this code must
50 * also be a bit ugly in certain places. Please forgive me. =/
52 * This also means that there might be some pieces of the code that
53 * look completely illogical to you, and you think that you may make
54 * them much neater/better. However, in many cases it might just be
55 * that it's required to cope with some oddity of the protocol, such
56 * as the amazingly ugly delayed key response in the peer protocol.
59 /* I assume this is the correct character set to use for DC,
60 * considering it was developed without i18n support under Windows */
61 #define DCCHARSET "windows-1252"
63 #ifdef DCPP_MASQUERADE
65 * I honestly don't want to pretend being a client that I'm not, but
66 * there are so many hubs that simply do not accept any clients
67 * outside their whitelists, for no obvious reasons, so I feel that I
68 * am left with little choice. Anyhow, as long as I actually support
69 * all the features that my faked DC++ version does, there should be
70 * very little harm done.
73 #define DCIDTAGV "0.674"
74 #define DCIDFULL "DC++ 0.674"
76 #define DCIDTAG "Dolda"
77 #define DCIDTAGV VERSION
78 #define DCIDFULL "DoldaConnect " VERSION
91 void (*handler)(struct socket *sk, void *data, char *cmd, char *args);
96 struct qcommand *next;
103 size_t inbufdata, inbufsize;
104 struct qcommand *queue;
112 struct dcexppeer *next, *prev;
115 struct timer *expire;
120 struct dcpeer *next, *prev;
124 size_t inbufdata, inbufsize;
126 struct qcommand *queue;
127 struct transfer *transfer;
129 int ptclose; /* Close after transfer is complete */
130 int accepted; /* If false, we connected, otherwise, we accepted */
132 int direction; /* Using the constants from transfer.h */
142 static struct fnet dcnet;
143 static struct transferiface dctransfer;
144 static struct socket *udpsock = NULL;
145 static struct socket *tcpsock = NULL;
146 static struct dcpeer *peers = NULL;
148 static struct dcexppeer *expected = NULL;
149 static char *hmlistname = NULL;
150 static char *xmllistname = NULL;
151 static char *xmlbz2listname = NULL;
153 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn);
154 static void freedcpeer(struct dcpeer *peer);
155 static void transread(struct socket *sk, struct dcpeer *peer);
156 static void transerr(struct socket *sk, int err, struct dcpeer *peer);
157 static void transwrite(struct socket *sk, struct dcpeer *peer);
158 static void updatehmlist(void);
159 static void updatexmllist(void);
160 static void updatexmlbz2list(void);
162 static int reservedchar(unsigned char c)
164 return((c == 0) || (c == 5) || (c == 124) || (c == 96) || (c == 126) || (c == 36));
167 /* Oh, how I despise having to do this... */
168 static char *dcmakekey(char *lock)
174 buf = smalloc(strlen(lock));
177 for(i = 0; lock[i]; i++)
179 buf[i] = lock[i] ^ save;
180 buf[i] = ((buf[i] & 0x0F) << 4) | ((buf[i] & 0xF0) >> 4);
182 if((i != 0) && reservedchar(buf[i]))
187 buf[0] ^= buf[i - 1];
188 if(reservedchar(buf[0]))
192 key = smalloc(len + 1);
194 for(i = 0; lock[i] != 0; i++)
196 if(reservedchar(buf[i]))
197 offset += sprintf(key + offset, "/%%DCN%03i%%/", buf[i]);
199 key[offset++] = buf[i];
206 static void endcompress(struct dcpeer *peer)
208 if(peer->compress == CPRS_ZLIB)
210 deflateEnd(peer->cprsdata);
211 free(peer->cprsdata);
213 peer->compress = CPRS_NONE;
216 static void initcompress(struct dcpeer *peer, int algo)
221 peer->compress = algo;
222 if(algo == CPRS_ZLIB)
224 peer->cprsdata = smalloc(sizeof(z_stream));
225 memset(peer->cprsdata, 0, sizeof(z_stream));
226 if((ret = deflateInit(peer->cprsdata, 3)) != Z_OK)
228 flog(LOG_CRIT, "Aiya! zlib refuses to init (%i)!", ret);
234 static void unquote(wchar_t *in)
238 for(p = in; *p != L'\0'; p++)
242 for(p2 = p + 1; (*p2 != L'\0') && (*p2 != L';') && (*p2 != L'&'); p2++);
249 if(!wcscmp(p + 1, L"amp"))
252 } else if(p[1] == L'#') {
253 nc = ucptowc(wcstol(p + 2, NULL, 10));
262 memmove(p + 1, p2 + 1, (wcslen(p2 + 1) + 1) * sizeof(wchar_t));
267 static void freeexppeer(struct dcexppeer *ep)
270 ep->next->prev = ep->prev;
272 ep->prev->next = ep->next;
277 if(ep->expire != NULL)
278 canceltimer(ep->expire);
282 static void exppeerexpire(int cancelled, struct dcexppeer *ep)
289 static struct dcexppeer *expectpeer(char *nick, struct fnetnode *fn)
291 struct dcexppeer *ep;
293 ep = smalloc(sizeof(*ep));
294 ep->nick = sstrdup(nick);
295 getfnetnode(ep->fn = fn);
296 ep->expire = timercallback(ntime() + 300, (void (*)(int, void *))exppeerexpire, ep);
305 static struct qcommand *newqcmd(struct qcommand **queue, char *string)
307 struct qcommand *new;
309 while(*queue != NULL)
310 queue = &(*queue)->next;
311 new = smalloc(sizeof(*new));
312 new->string = sstrdup(string);
318 static struct qcommand *ulqcmd(struct qcommand **queue)
320 struct qcommand *qcmd;
322 if((qcmd = *queue) == NULL)
328 static void freeqcmd(struct qcommand *qcmd)
334 static void hubrecvchat(struct socket *sk, struct fnetnode *fn, char *from, char *string)
336 wchar_t *chat, *wfrom, *wpeer;
338 struct fnetpeer *peer;
340 end = string + strlen(string);
341 while((p = strchr(string, 13)) != NULL)
342 memmove(p, p + 1, (end-- - p));
345 if((strlen(string) > strlen(from) + 2) && (*string == '<') && !memcmp(string + 1, from, strlen(from)) && (*(string + strlen(from) + 1) == '>'))
346 string += strlen(from) + 2;
347 if((wfrom = icmbstowcs(from, DCCHARSET)) == NULL)
349 wpeer = swcsdup(wfrom);
355 for(p = string + 1; *p; p++)
357 if((*p == ' ') || (*p == '>'))
365 if((wpeer = icmbstowcs(string + 1, DCCHARSET)) == NULL)
371 wpeer = swcsdup(L"");
373 if((chat = icmbstowcs(string, DCCHARSET)) == NULL)
383 if((peer = fnetfindpeer(fn, wfrom)) == NULL) /* Assume public chat */
384 fnethandlechat(fn, 1, wfrom, wpeer, chat);
386 fnethandlechat(fn, 0, wfrom, wpeer, chat);
388 fnethandlechat(fn, 1, L"", wpeer, chat);
396 static void sendadc(struct socket *sk, char *arg)
399 size_t bufsize, bufdata;
402 bufsize = bufdata = 0;
408 bufcat(buf, "\\s", 2);
409 } else if(*arg == '\n') {
410 bufcat(buf, "\\n", 2);
411 } else if(*arg == '\\') {
412 bufcat(buf, "\\\\", 2);
417 sockqueue(sk, buf, bufdata);
421 static void sendadcf(struct socket *sk, char *arg, ...)
427 buf = vsprintf2(arg, args);
435 static char **parseadc(char *args)
438 size_t retbufsize, retbufdata;
440 size_t bufsize, bufdata;
445 retbufsize = retbufdata = bufsize = bufdata = 0;
454 else if(*args != ' ')
458 if((*args == ' ') || (*args == 0))
461 addtobuf(retbuf, buf);
463 bufsize = bufdata = 0;
466 } else if(*args == '\\') {
469 addtobuf(buf, *args);
478 addtobuf(retbuf, NULL);
481 } else if(*args == 's') {
483 } else if(*args == 'n') {
485 } else if(*args == '\\') {
493 addtobuf(retbuf, NULL);
497 /* Macros useful in command handlers */
498 #define skipspace(s) ({if(((s) = strchr((s), ' ')) == NULL) return; else (s)++;})
499 #define qstr(sk, str) sockqueue(sk, str, strlen(str))
500 #define qstrf(sk, strandargs...) \
503 if((__buf__ = sprintf2(strandargs)) != NULL) { \
504 sockqueue(sk, __buf__, strlen(__buf__)); \
509 static char *tr(char *str, char *trans)
513 for(; *trans; trans += 2)
515 for(p = strchr(str, trans[0]); p != NULL; p = strchr(p, trans[0]))
521 static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
523 if(!wcscmp(cmd, L"resume"))
527 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
530 transfer->curpos = wcstol(arg, NULL, 10);
531 qstrf(peer->sk, "$Get %s$%i|", peer->mbspath, transfer->curpos + 1);
534 peer->mbspath = NULL;
540 static void peerhandleaction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
543 struct dcexppeer *expect;
544 struct transfer *transfer;
551 if(peer->fn->fnet != &dcnet)
556 hub = peer->fn->data;
558 if(peer->transfer != NULL)
560 swprintf(tbuf, 128, L"hs: dc-%s", cmd);
561 transfersetactivity(peer->transfer, tbuf);
565 if(cmd == NULL) /* Connect event */
567 } else if(!strcmp(cmd, "$MyNick")) {
568 for(expect = expected; expect != NULL; expect = expect->next)
570 if(!strcmp(expect->nick, args))
577 peer->fn = expect->fn;
578 getfnetnode(peer->fn);
581 } else if(!strcmp(cmd, "$Lock")) {
582 if(peer->wcsname == NULL)
588 qstrf(sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), DCCHARSET, "DoldaConnectUser-IN"));
590 qstrf(sk, "$MyNick %s|", hub->nativenick);
591 #ifdef DCPP_MASQUERADE
592 qstrf(sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
594 qstrf(sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
598 #ifdef DCPP_MASQUERADE
599 qstr(sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
601 qstr(sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
604 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
606 if((transfer->dir == TRNSD_DOWN) && (transfer->iface == NULL) && !wcscmp(peer->wcsname, transfer->peerid))
611 peer->direction = TRNSD_UP;
612 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
613 transfersetnick(transfer, peer->wcsname);
614 peer->transfer = transfer;
616 peer->direction = TRNSD_DOWN;
617 peer->transfer = transfer;
618 transferattach(transfer, &dctransfer, peer);
619 transfersetnick(transfer, peer->wcsname);
620 transfersetstate(transfer, TRNS_HS);
622 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
623 if(peer->key != NULL)
625 /* I hate the DC protocol so much... */
626 qstrf(sk, "$Key %s|", peer->key);
630 if(peer->direction == TRNSD_DOWN)
632 if((mbsbuf = icwcstombs(peer->transfer->path, DCCHARSET)) == NULL)
634 /* I believe that NOTFOUND should be used
635 * since giving a path that cannot be
636 * represented in the protocol's charset is
637 * literally the same as giving a path that
638 * the client doesn't have. */
639 transferseterror(peer->transfer, TRNSE_NOTFOUND);
643 if(peer->transfer->size == -1)
645 /* The transfer will be restarted later from
646 * cmd_filelength when it detects that the sizes
648 qstrf(sk, "$Get %s$1|", mbsbuf);
650 if(forkfilter(transfer))
652 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", transfer->id, strerror(errno));
657 peer->mbspath = sstrdup(mbsbuf);
658 CBREG(transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
662 } else if(!strcmp(cmd, "$FileLength")) {
663 if(peer->transfer == NULL)
668 transfersetstate(peer->transfer, TRNS_MAIN);
669 socksettos(peer->sk, confgetint("transfer", "dltos"));
670 peer->state = PEER_TRNS;
671 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
672 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
673 qstr(peer->sk, "$Send|");
676 if(cmd == NULL) /* Connect event */
679 qstrf(sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), DCCHARSET, "DoldaConnectUser-IN"));
681 qstrf(sk, "$MyNick %s|", hub->nativenick);
682 #ifdef DCPP_MASQUERADE
683 qstrf(sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
685 qstrf(sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
687 } else if(!strcmp(cmd, "$Direction")) {
688 if(peer->wcsname == NULL)
693 if(peer->direction == TRNSD_UP)
695 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
696 transfersetnick(transfer, peer->wcsname);
697 peer->transfer = transfer;
699 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
701 if((transfer->dir == TRNSD_DOWN) && (transfer->state == TRNS_WAITING) && !wcscmp(peer->wcsname, transfer->peerid))
709 peer->transfer = transfer;
710 transferattach(transfer, &dctransfer, peer);
711 transfersetnick(transfer, peer->wcsname);
712 transfersetstate(transfer, TRNS_HS);
716 #ifdef DCPP_MASQUERADE
717 qstr(sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
719 qstr(sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
722 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
723 if(peer->key != NULL)
724 qstrf(sk, "$Key %s|", peer->key);
725 if(peer->direction == TRNSD_DOWN)
727 if((mbsbuf = icwcstombs(peer->transfer->path, DCCHARSET)) == NULL)
729 /* I believe that NOTFOUND should be used
730 * since giving a path that cannot be
731 * represented in the protocol's charset is
732 * literally the same as giving a path that
733 * the client doesn't have. */
734 transferseterror(peer->transfer, TRNSE_NOTFOUND);
738 if(peer->transfer->size == -1)
740 /* The transfer will be restarted later from
741 * cmd_filelength when it detects that the sizes
743 qstrf(sk, "$Get %s$1|", mbsbuf);
745 if(forkfilter(transfer))
747 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", transfer->id, strerror(errno));
752 peer->mbspath = sstrdup(mbsbuf);
753 CBREG(transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
757 } else if(!strcmp(cmd, "$FileLength")) {
758 if(peer->transfer == NULL)
763 transfersetstate(peer->transfer, TRNS_MAIN);
764 socksettos(peer->sk, confgetint("transfer", "dltos"));
765 peer->state = PEER_TRNS;
766 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
767 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
768 qstr(peer->sk, "$Send|");
773 static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
777 struct fnetnode *cfn;
781 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
782 buf = tr(icswcstombs(confgetstr("dc", "desc"), DCCHARSET, "Charset_conv_failure"), "$_|_");
783 qstrf(sk, "%s", buf);
785 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
787 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
790 qstrf(sk, "<%s V:%s,M:%c,H:%i/0/0,S:%i>",
793 (tcpsock == NULL)?'P':'A',
795 confgetint("transfer", "slots")
798 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), DCCHARSET, "Charset_conv_failure"), "$_|_");
799 qstrf(sk, "%s\x01$", buf);
800 buf = tr(icswcstombs(confgetstr("dc", "email"), DCCHARSET, "Charset_conv_failure"), "$_|_");
801 qstrf(sk, "%s$", buf);
802 qstrf(sk, "%llu$|", sharesize);
805 static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
810 if(!strcmp(cmd, "$Lock"))
812 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
813 } else if(!strcmp(cmd, "$Hello")) {
814 if(fn->state == FNN_HS)
816 qstrf(sk, "$Version 1,0091|");
817 qstrf(sk, "$GetNickList|");
819 fnetsetstate(fn, FNN_EST);
821 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
826 static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
833 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
835 if((p = strchr(args, ' ')) != NULL)
839 #ifdef DCPP_MASQUERADE
840 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
842 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
845 key = dcmakekey(args);
846 qstrf(sk, "$Key %s|", key);
848 hubhandleaction(sk, fn, cmd, args);
851 static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
857 if(hub->nativename == NULL)
858 free(hub->nativename);
859 hub->nativename = sstrdup(args);
860 buf = icmbstowcs(args, DCCHARSET);
861 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
864 hubhandleaction(sk, fn, cmd, args);
867 static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
873 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
875 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
876 fnetaddpeer(fn, nick, nick);
878 hubhandleaction(sk, fn, cmd, args);
881 static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
884 struct fnetpeer *peer;
888 if((nick = icmbstowcs(args, DCCHARSET)) == NULL)
890 if((peer = fnetfindpeer(fn, nick)) != NULL)
893 hubhandleaction(sk, fn, cmd, args);
896 static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
901 struct fnetpeer *peer, *npeer;
904 for(peer = fn->peers; peer != NULL; peer = peer->next)
905 peer->flags.b.delete = 1;
906 while((p = strstr(args, "$$")) != NULL)
909 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
911 if((peer = fnetfindpeer(fn, buf)) == NULL)
912 peer = fnetaddpeer(fn, buf, buf);
914 peer->flags.b.delete = 0;
916 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
920 for(peer = fn->peers; peer != NULL; peer = npeer)
923 if(peer->flags.b.delete)
926 hubhandleaction(sk, fn, cmd, args);
929 static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
934 struct fnetpeer *peer;
937 for(peer = fn->peers; peer != NULL; peer = peer->next)
938 peer->flags.b.op = 0;
939 while((p = strstr(args, "$$")) != NULL)
942 if((buf = icmbstowcs(args, DCCHARSET)) != NULL)
944 if((peer = fnetfindpeer(fn, buf)) != NULL)
945 peer->flags.b.op = 1;
950 hubhandleaction(sk, fn, cmd, args);
953 static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
956 wchar_t *buf, *wp, *wp2;
958 struct fnetpeer *peer;
963 if(strncmp(p, "$ALL ", 5))
966 if((p2 = strchr(p, ' ')) == NULL)
969 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
971 if((peer = fnetfindpeer(fn, buf)) == NULL)
972 peer = fnetaddpeer(fn, buf, buf);
975 if((p2 = strstr(p, "$ $")) == NULL)
978 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
980 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcschr(buf, L'<')) != NULL))
982 buf[wcslen(buf) - 1] = L'\0';
984 if((wp2 = wcschr(wp, L' ')) != NULL)
987 fnetpeersetstr(peer, L"dc-client", wp);
991 if((wp2 = wcschr(wp, L',')) != NULL)
995 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
996 fnetpeersetstr(peer, abuf, wp + 2);
998 } while(wp2 != NULL);
1001 fnetpeersetstr(peer, L"descr", buf);
1004 if((p2 = strchr(p, '$')) == NULL)
1007 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
1009 fnetpeersetstr(peer, L"dc-speed", buf);
1012 if((p2 = strchr(p, '$')) == NULL)
1015 if((buf = icmbstowcs(p, DCCHARSET)) == NULL)
1017 fnetpeersetstr(peer, L"email", buf);
1022 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
1023 hubhandleaction(sk, fn, cmd, args);
1026 /* I do not implement the fully in that I do not disconnect from the
1027 * old hub. I do this since I believe that if the hub owner really
1028 * wants to get rid of us, then it is his responsibility to disconnect
1030 static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1033 struct fnetnode *newfn;
1037 if(strchr(args, ':') == NULL)
1039 args = strcpy(smalloc(strlen(args) + 5), args);
1040 strcat(args, ":411");
1045 if((newfn = fnetinitconnect(L"dc", args)) != NULL)
1047 linkfnetnode(newfn);
1050 hubhandleaction(sk, fn, cmd, args);
1055 static char *getdcpath(struct sharecache *node, size_t *retlen)
1060 if(node->parent == NULL)
1062 if(node->parent == shareroot)
1064 if((buf = icwcstombs(node->name, DCCHARSET)) == NULL)
1067 *retlen = strlen(buf);
1070 if((buf2 = icwcstombs(node->name, DCCHARSET)) == NULL)
1072 if((buf = getdcpath(node->parent, &len)) == NULL)
1077 len2 = strlen(buf2);
1078 buf = srealloc(buf, len + 1 + len2 + 1);
1080 strcpy(buf + len, buf2);
1081 buf[len + len2] = 0;
1084 *retlen = len + len2;
1090 * This is the main share searching function for Direct Connect
1091 * peers. Feel free to optimize it if you feel the need for it. I
1092 * haven't ever seen it take any noticable CPU time anyway, so I
1093 * haven't felt that need.
1095 * It may feel dubious to just return the directory when all terms are
1096 * satisfied rather than all the files in it, but it really benefits
1097 * everyone: Less cycles used for us, less UDP squelching for active
1098 * searchers, and less bandwidth waste for hubs when serving passive
1101 static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1106 char *prefix, *infix, *postfix, *buf, *buf2;
1108 struct sockaddr_in addr;
1109 struct sharecache *node;
1110 int minsize, maxsize;
1112 int termnum, satisfied, skipcheck;
1113 int level, tersat[32];
1118 if((p = strchr(args, ' ')) == NULL)
1122 memset(terms, 0, sizeof(terms));
1123 prefix = infix = postfix = NULL;
1127 if(!strncmp(args, "Hub:", 4))
1129 if(!strcmp(cmd, "$MultiSearch"))
1131 if(!strcmp(args + 4, hub->nativenick))
1133 prefix = sprintf2("$SR %s ", hub->nativenick);
1134 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1135 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1139 if((p2 = strchr(args, ':')) == NULL)
1142 addr.sin_family = AF_INET;
1143 if(!inet_aton(args, &addr.sin_addr))
1145 addr.sin_port = htons(atoi(p2));
1146 prefix = sprintf2("$SR %s ", hub->nativenick);
1147 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1148 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1149 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1152 minsize = maxsize = -1;
1168 if((p2 = strchr(p, '?')) == NULL)
1183 if((*p2 == 0) || (*p2 == '$'))
1191 if(!dotth && !strncmp(p, "TTH:", 4))
1194 if((buf = base32decode(p + 4, &buflen)) == NULL)
1198 memcpy(hashtth, buf, 24);
1201 if((terms[termnum] = icmbstowcs(p, DCCHARSET)) != NULL)
1212 node = shareroot->child;
1214 for(i = 0; i < termnum; i++)
1220 if(node->f.b.type == FILE_REG)
1222 if((minsize >= 0) && (node->size < minsize))
1224 if((maxsize >= 0) && (node->size > maxsize))
1227 if(!skipcheck && dotth)
1229 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1234 for(i = 0; i < termnum; i++)
1238 if(wcsexists(node->name, terms[i]))
1242 } else if(node->child == NULL) {
1247 if(!skipcheck && (satisfied == termnum))
1249 if((buf = getdcpath(node, NULL)) != NULL)
1251 if(node->f.b.hastth)
1253 buf2 = base32encode(node->hashtth, 24);
1254 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1257 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1262 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1264 while(node->next == NULL)
1266 if((node = node->parent) == shareroot)
1270 if(node == shareroot)
1272 for(i = 0; i < termnum; i++)
1274 if(tersat[i] >= level)
1287 hubhandleaction(sk, fn, cmd, args);
1298 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1302 static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1306 struct socket *newsk;
1307 struct sockaddr_in addr;
1310 if((p = strchr(args, ' ')) == NULL)
1313 if(strcmp(args, hub->nativenick))
1315 addr.sin_family = AF_INET;
1317 if((p = strchr(args, ':')) == NULL)
1320 addr.sin_port = htons(atoi(p));
1321 if(!inet_aton(args, &addr.sin_addr))
1323 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1325 hubhandleaction(sk, fn, cmd, args);
1328 static void sendctm(struct socket *sk, char *nick)
1330 struct sockaddr *addr;
1335 if(sockgetremotename(tcpsock, &addr, &addrlen) < 0)
1337 if(addr->sa_family == AF_INET)
1338 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1340 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1344 static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1350 if((p = strchr(args, ' ')) == NULL)
1353 if(strcmp(p, hub->nativenick))
1356 expectpeer(args, fn);
1357 hubhandleaction(sk, fn, cmd, args);
1360 static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1363 struct fnetnode *node;
1368 for(node = fnetnodes; node != NULL; node = node->next)
1370 if(node->state == FNN_EST)
1373 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1374 hubhandleaction(sk, fn, cmd, args);
1377 static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1384 if((p2 = strchr(p, ' ')) == NULL)
1388 if((p2 = strchr(p, ' ')) == NULL)
1391 if(strcmp(p, "From:"))
1394 if((p2 = strstr(p, " $")) == NULL)
1398 hubrecvchat(fn->sk, fn, p, p2);
1399 hubhandleaction(sk, fn, cmd, args);
1402 static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1406 char *nick, *filename, *hubname;
1409 wchar_t *wnick, *wfile;
1413 if((p2 = strchr(p, ' ')) == NULL)
1418 if((p2 = strchr(p, 5)) == NULL)
1422 if((p2 = strchr(p, ' ')) == NULL)
1427 if((p2 = strchr(p, '/')) == NULL)
1432 if((p2 = strchr(p, 5)) == NULL)
1436 if((p2 = strstr(p, " (")) == NULL)
1439 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
1441 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1446 sr = newsrchres(&dcnet, wfile, wnick);
1447 if(sr->peernick != NULL)
1449 sr->peernick = swcsdup(wnick);
1454 getfnetnode(sr->fn = fn);
1457 hubhandleaction(sk, fn, cmd, args);
1460 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1462 /* Do nothing for now. */
1465 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1467 if(peer->nativename != NULL)
1468 free(peer->nativename);
1469 peer->nativename = sstrdup(args);
1470 if(peer->wcsname != NULL)
1471 free(peer->wcsname);
1472 if((peer->wcsname = icmbstowcs(peer->nativename, DCCHARSET)) == NULL)
1477 peerhandleaction(sk, peer, cmd, args);
1480 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1484 if((p = strchr(args, ' ')) == NULL)
1487 if(!strcmp(args, "Upload"))
1488 peer->direction = TRNSD_DOWN;
1489 if(!strcmp(args, "Download"))
1490 peer->direction = TRNSD_UP;
1491 peerhandleaction(sk, peer, cmd, args);
1494 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1498 if((p = strchr(args, ' ')) == NULL)
1501 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1503 if(peer->key != NULL)
1505 peer->key = dcmakekey(args);
1506 peerhandleaction(sk, peer, cmd, args);
1509 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1511 peerhandleaction(sk, peer, cmd, args);
1514 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1518 if(peer->transfer == NULL)
1524 if(peer->transfer->size == size)
1528 /* Will have to restart, then... I really hate this, but what
1529 * am I to do then, considering the DC protocol requires the
1530 * resume offset before it sends the filesize? */
1531 transfersetsize(peer->transfer, size);
1535 peerhandleaction(sk, peer, cmd, args);
1538 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1540 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1542 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1543 resettransfer(peer->transfer);
1549 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1551 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1553 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1554 resettransfer(peer->transfer);
1567 {"MyList.DcLst", &hmlistname, updatehmlist},
1568 {"files.xml", &xmllistname, updatexmllist},
1569 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1573 static int openfilelist(char *name)
1578 for(i = 0; lists[i].name != NULL; i++)
1580 if(!strcmp(name, lists[i].name))
1584 if(lists[i].name == NULL)
1587 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1589 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1592 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1599 static struct sharecache *findbytth(char *tth32)
1603 struct sharecache *node;
1605 if((buf = base32decode(tth32, &buflen)) == NULL)
1612 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1614 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1621 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1623 struct sharecache *node;
1630 if((p2 = strchr(p, sep)) != NULL)
1632 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1639 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1644 struct sharecache *node;
1645 struct socket *lesk;
1649 if(peer->transfer == NULL)
1654 if((p = strchr(args, '$')) == NULL)
1660 if((offset = (atoi(p) - 1)) < 0)
1665 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1667 qstr(sk, "$Error Could not send file list|");
1670 } else if(fd >= 0) {
1671 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1672 transfersetpath(peer->transfer, buf2);
1678 qstr(sk, "$MaxedOut|");
1682 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1684 qstrf(sk, "$Error File not in share|");
1688 if((fd = opensharecache(node)) < 0)
1690 qstrf(sk, "$Error %s|", strerror(errno));
1694 buf = getfspath(node);
1695 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1696 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1698 transfersetpath(peer->transfer, buf2);
1701 if(fstat(fd, &sb) < 0)
1704 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1705 qstrf(sk, "$Error|");
1709 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1712 qstrf(sk, "$Error Offset out of range|");
1716 lesk = wrapsock(fd);
1717 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1719 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1720 peerhandleaction(sk, peer, cmd, args);
1723 static void starttrans(struct dcpeer *peer)
1725 peer->state = PEER_TRNS;
1726 transferstartul(peer->transfer, peer->sk);
1727 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1730 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1732 if(peer->transfer == NULL)
1737 if(peer->transfer->localend == NULL)
1744 peerhandleaction(sk, peer, cmd, args);
1747 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1752 size_t arrsize, arrdata;
1754 if(peer->supports != NULL)
1756 for(i = 0; peer->supports[i] != NULL; i++)
1757 free(peer->supports[i]);
1758 free(peer->supports);
1761 arrsize = arrdata = 0;
1765 if((p2 = strchr(p, ' ')) != NULL)
1769 addtobuf(arr, sstrdup(p));
1770 } while((p = p2) != NULL);
1771 addtobuf(arr, NULL);
1772 peer->supports = arr;
1773 peerhandleaction(sk, peer, cmd, args);
1776 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1780 int start, numbytes;
1781 char *charset, *buf;
1783 struct sharecache *node;
1785 struct socket *lesk;
1787 if(peer->transfer == NULL)
1793 if((p2 = strchr(p, ' ')) == NULL)
1801 if((p2 = strchr(p, ' ')) == NULL)
1809 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1812 charset = DCCHARSET;
1813 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1814 initcompress(peer, CPRS_ZLIB);
1815 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1817 qstr(sk, "$Error Could not send file list|");
1819 } else if(fd >= 0) {
1820 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1821 transfersetpath(peer->transfer, buf2);
1827 qstr(sk, "$MaxedOut|");
1830 if((node = resdcpath(p, charset, '\\')) == NULL)
1832 qstr(sk, "$Error File not in cache|");
1835 if((fd = opensharecache(node)) < 0)
1837 qstrf(sk, "$Error %s|", strerror(errno));
1840 buf = getfspath(node);
1841 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1842 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1844 transfersetpath(peer->transfer, buf2);
1847 if(fstat(fd, &sb) < 0)
1850 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1851 qstr(sk, "$Error|");
1854 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1857 qstr(sk, "$Error Offset out of range|");
1860 if((numbytes < 0) || (start + numbytes > sb.st_size))
1861 numbytes = sb.st_size - start;
1862 lesk = wrapsock(fd);
1863 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1865 qstrf(sk, "$Sending %i|", numbytes);
1867 peerhandleaction(sk, peer, cmd, args);
1870 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1874 int start, numbytes;
1875 struct sharecache *node;
1877 struct socket *lesk;
1881 if(peer->transfer == NULL)
1886 if((argv = parseadc(args)) == NULL)
1891 if(parrlen(argv) < 4)
1896 start = atoi(argv[2]);
1897 numbytes = atoi(argv[3]);
1900 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
1902 qstr(sk, "$Error Could not send file list|");
1904 } else if(fd >= 0) {
1905 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
1906 transfersetpath(peer->transfer, wbuf);
1912 qstr(sk, "$MaxedOut|");
1915 if(!strncmp(argv[1], "TTH/", 4))
1917 if((node = findbytth(argv[1] + 4)) == NULL)
1919 qstr(sk, "$Error File not in cache|");
1923 if((node = resdcpath(argv[1], "UTF-8", '/')) == NULL)
1925 qstr(sk, "$Error File not in cache|");
1929 if((fd = opensharecache(node)) < 0)
1931 qstrf(sk, "$Error %s|", strerror(errno));
1934 buf = getfspath(node);
1935 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
1936 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1938 transfersetpath(peer->transfer, wbuf);
1941 if(!strcmp(argv[0], "file"))
1943 for(i = 4; argv[i] != NULL; i++)
1945 if(!strcmp(argv[i], "ZL1"))
1946 initcompress(peer, CPRS_ZLIB);
1948 if(fstat(fd, &sb) < 0)
1950 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1951 qstr(sk, "$Error|");
1954 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1956 qstr(sk, "$Error Offset out of range|");
1959 if((numbytes < 0) || (start + numbytes > sb.st_size))
1960 numbytes = sb.st_size - start;
1961 lesk = wrapsock(fd);
1962 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1965 qstr(sk, "$ADCSND");
1966 sendadc(sk, "file");
1967 sendadc(sk, argv[1]);
1968 sendadcf(sk, "%i", start);
1969 sendadcf(sk, "%i", numbytes);
1970 if(peer->compress == CPRS_ZLIB)
1974 } else if(!strcmp(argv[0], "tthl")) {
1976 * XXX: Implement full TTHL support.
1978 * In the meantime, this is better than nothing, since it at
1979 * least allows fetching of the TTH root if it isn't already
1984 qstr(sk, "$Error no TTHL data for virtual files|");
1987 qstr(sk, "$ADCSND");
1988 sendadc(sk, "tthl");
1989 sendadc(sk, argv[1]);
1993 sockqueue(sk, node->hashtth, 24);
1995 qstr(sk, "$Error Namespace not implemented|");
1998 peerhandleaction(sk, peer, cmd, args);
2008 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2013 hubhandleaction(sk, fn, cmd, args);
2017 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2019 peerhandleaction(sk, peer, cmd, args);
2024 static int hubreqconn(struct fnetpeer *peer)
2029 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2034 if((hub = peer->fn->data) == NULL)
2039 if((mbsnick = icwcstombs(peer->id, DCCHARSET)) == NULL)
2040 return(1); /* Shouldn't happen, of course, but who knows... */
2043 sendctm(peer->fn->sk, mbsnick);
2044 expectpeer(mbsnick, peer->fn);
2046 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2052 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2055 char *mbsstring, *mbsto;
2058 if((mbsto = icwcstombs(to, DCCHARSET)) == NULL)
2063 if((mbsstring = icwcstombs(string, DCCHARSET)) == NULL)
2068 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2075 if(strchr(mbsstring, '|') != NULL)
2086 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2088 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2091 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2098 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2100 int minl, maxl, minr, maxr, retmin, retmax;
2105 findsizelimit(sexpr->l, &minl, &maxl);
2106 findsizelimit(sexpr->r, &minr, &maxr);
2107 retmin = (minl > minr)?minl:minr;
2108 if((maxl != -1) && (maxr != -1))
2109 retmax = (maxl < maxr)?maxl:maxr;
2118 findsizelimit(sexpr->l, &minl, &maxl);
2119 findsizelimit(sexpr->r, &minr, &maxr);
2120 retmin = (minl < minr)?minl:minr;
2121 if((maxl == -1) || (maxr == -1))
2124 retmax = (maxl > maxr)?maxl:maxr;
2127 findsizelimit(sexpr->l, &minl, &maxl);
2128 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2132 } else if((minl == 0) && (maxl != -1)) {
2135 } else if((minl != 0) && (maxl == -1)) {
2138 } else { /* This would yield two seperate intervals, which DC cannot handle */
2144 retmax = sexpr->d.n - 1;
2147 retmin = sexpr->d.n;
2148 retmax = sexpr->d.n;
2151 retmin = sexpr->d.n + 1;
2165 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2168 struct wcslist *list, *cur;
2169 char *sstr, *buf, *p;
2170 size_t sstrsize, sstrdata;
2171 struct sockaddr *name;
2173 int minsize, maxsize;
2177 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2179 list = findsexprstrs(srch->sexpr);
2180 findsizelimit(srch->sexpr, &minsize, &maxsize);
2181 if((minsize != 0) && (maxsize != -1))
2183 /* Choose either minsize or maxsize by trying to determine
2184 * which choice will be most restrictive. The result can be
2185 * approximative at best anyway, so don't try too hard... */
2186 if((50000000 - maxsize) > (minsize - 50000000))
2193 snprintf(sizedesc, sizeof(sizedesc), "T?F?%i", minsize);
2194 } else if(maxsize != -1) {
2195 snprintf(sizedesc, sizeof(sizedesc), "T?T?%i", maxsize);
2197 strcpy(sizedesc, "F?F?0");
2200 sstrsize = sstrdata = 0;
2203 for(cur = list; cur != NULL; cur = cur->next)
2205 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2207 /* Can't find anything anyway if the search expression
2208 * requires characters outside DC's charset. There's
2209 * nothing technically wrong with the search itself,
2210 * however, so return success. This should be
2211 * considered as an optimization. */
2218 addtobuf(sstr, '$');
2220 * It probably doesn't hurt if buf contains any extra
2221 * dollar signs - it will just result in extra search
2222 * terms, and the extraneous results will be filtered by
2223 * the search layer anyway. It hurts if it contains any
2224 * pipes, though, so let's sell them for money.
2226 for(p = buf; *p; p++)
2231 bufcat(sstr, buf, strlen(buf));
2235 /* Will match all files... :-/ */
2236 addtobuf(sstr, '.');
2241 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2243 flog(LOG_WARNING, "cannot get address of UDP socket");
2245 qstrf(fn->sk, "$Search %s %s?1?%s|", formataddress(name, namelen), sizedesc, sstr);
2249 qstrf(fn->sk, "$Search Hub:%s %s?1?%s|", hub->nativenick, sizedesc, sstr);
2253 fn->lastsrch = time(NULL);
2261 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2262 struct command hubcmds[] =
2264 {"$Lock", cc(cmd_lock)},
2265 {"$HubName", cc(cmd_hubname)},
2266 {"$Hello", cc(cmd_hello)},
2267 {"$Quit", cc(cmd_quit)},
2268 {"$NickList", cc(cmd_nicklist)},
2269 {"$OpList", cc(cmd_oplist)},
2270 {"$MyINFO", cc(cmd_myinfo)},
2271 {"$ForceMove", cc(cmd_forcemove)},
2272 {"$Search", cc(cmd_search)},
2273 {"$MultiSearch", cc(cmd_search)},
2274 {"$ConnectToMe", cc(cmd_connecttome)},
2275 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2276 {"$GetNetInfo", cc(cmd_getnetinfo)},
2277 {"$To:", cc(cmd_to)},
2278 {"$SR", cc(cmd_sr)},
2279 {"$UserCommand", cc(cmd_usercommand)},
2283 struct command peercmds[] =
2285 {"$MyNick", cc(cmd_mynick)},
2286 {"$Lock", cc(cmd_peerlock)},
2287 {"$Direction", cc(cmd_direction)},
2288 {"$Key", cc(cmd_key)},
2289 {"$FileLength", cc(cmd_filelength)},
2290 {"$Error", cc(cmd_error)},
2291 {"$MaxedOut", cc(cmd_maxedout)},
2292 {"$Get", cc(cmd_get)},
2293 {"$Send", cc(cmd_send)},
2294 {"$Supports", cc(cmd_supports)},
2295 {"$GetBlock", cc(cmd_getblock)},
2296 {"$UGetBlock", cc(cmd_getblock)},
2297 {"$GetZBlock", cc(cmd_getblock)},
2298 {"$UGetZBlock", cc(cmd_getblock)},
2299 {"$ADCGET", cc(cmd_adcget)},
2304 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2306 CBUNREG(transfer, trans_filterout, peer);
2309 peer->transfer = NULL;
2313 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2321 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2323 if(sockqueuesize(peer->sk) < 65536)
2325 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2327 if(peer->compress == CPRS_NONE)
2329 sockqueue(peer->sk, buf, bufsize);
2330 } else if(peer->compress == CPRS_ZLIB) {
2331 cstr = peer->cprsdata;
2332 cstr->next_in = buf;
2333 cstr->avail_in = bufsize;
2334 while(cstr->avail_in > 0)
2336 cstr->next_out = outbuf;
2337 cstr->avail_out = sizeof(outbuf);
2338 if((ret = deflate(cstr, 0)) != Z_OK)
2340 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2344 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2349 if(peer->state == PEER_SYNC)
2351 if(peer->compress == CPRS_ZLIB)
2353 cstr = peer->cprsdata;
2354 cstr->next_in = NULL;
2358 cstr->next_out = outbuf;
2359 cstr->avail_out = sizeof(outbuf);
2360 ret = deflate(cstr, Z_FINISH);
2361 if((ret != Z_OK) && (ret != Z_STREAM_END))
2363 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2367 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2368 } while(ret != Z_STREAM_END);
2374 peer->state = PEER_CMD;
2376 transfersetstate(transfer, TRNS_HS);
2377 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2378 peer->sk->writecb = NULL;
2385 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2387 peer->state = PEER_SYNC;
2388 dctransgotdata(transfer, peer);
2391 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2393 if(transferdatasize(transfer) < 65536)
2394 peer->sk->ignread = 0;
2397 static void transread(struct socket *sk, struct dcpeer *peer)
2401 struct transfer *transfer;
2403 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2405 if(peer->transfer == NULL)
2411 transferputdata(peer->transfer, buf, bufsize);
2413 if(peer->transfer->curpos >= peer->transfer->size)
2415 transfer = peer->transfer;
2416 transferdetach(transfer);
2417 transferendofdata(transfer);
2420 if(transferdatasize(peer->transfer) > 65535)
2424 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2426 struct transfer *transfer;
2428 if((transfer = peer->transfer) == NULL)
2433 transferdetach(transfer);
2434 transferendofdata(transfer);
2437 static void transwrite(struct socket *sk, struct dcpeer *peer)
2439 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2441 if(peer->transfer == NULL)
2446 dctransgotdata(peer->transfer, peer);
2449 static void udpread(struct socket *sk, void *data)
2453 char *nick, *filename, *hubname;
2455 struct fnetnode *fn, *myfn;
2458 wchar_t *wnick, *wfile;
2460 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2462 buf = srealloc(buf, buflen + 1);
2464 if(!strncmp(buf, "$SR ", 4))
2468 if((p2 = strchr(p, ' ')) == NULL)
2476 if((p2 = strchr(p, 5)) == NULL)
2483 if((p2 = strchr(p, ' ')) == NULL)
2491 if((p2 = strchr(p, '/')) == NULL)
2499 if((p2 = strchr(p, 5)) == NULL)
2506 if((p2 = strstr(p, " (")) == NULL)
2512 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
2517 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2524 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2526 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2528 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2540 sr = newsrchres(&dcnet, wfile, wnick);
2541 if(sr->peernick != NULL)
2543 sr->peernick = swcsdup(wnick);
2549 getfnetnode(sr->fn = myfn);
2556 static void hubread(struct socket *sk, struct fnetnode *fn)
2563 hub = (struct dchub *)fn->data;
2564 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2566 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2568 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2569 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2571 p = hub->inbuf + hub->inbufdata;
2572 hub->inbufdata += datalen;
2573 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2576 newqcmd(&hub->queue, hub->inbuf);
2577 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2578 datalen = hub->inbufdata;
2583 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2588 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2594 if((buf = icwcstombs(newnick, DCCHARSET)) == NULL)
2596 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2601 if(hub == NULL) /* Not yet connected */
2606 if(hub->nativenick != NULL)
2607 free(hub->nativenick);
2608 hub->nativenick = buf;
2612 static struct dchub *newdchub(struct fnetnode *fn)
2616 new = smalloc(sizeof(*new));
2617 memset(new, 0, sizeof(*new));
2619 if(hubsetnick(fn, fn->mynick))
2620 fnetsetnick(fn, L"DoldaConnectUser-IN");
2621 /* IN as in Invalid Nick */
2625 static struct dcpeer *newdcpeer(struct socket *sk)
2629 new = smalloc(sizeof(*new));
2630 memset(new, 0, sizeof(*new));
2631 new->transfer = NULL;
2643 static void freedcpeer(struct dcpeer *peer)
2646 struct qcommand *qcmd;
2651 if(peer->next != NULL)
2652 peer->next->prev = peer->prev;
2653 if(peer->prev != NULL)
2654 peer->prev->next = peer->next;
2655 if(peer->transfer != NULL)
2657 if(peer->transfer->dir == TRNSD_UP)
2658 peer->transfer->close = 1;
2659 if(peer->transfer->dir == TRNSD_DOWN)
2660 resettransfer(peer->transfer);
2661 transferdetach(peer->transfer);
2663 if(peer->sk->data == peer)
2664 peer->sk->data = NULL;
2665 peer->sk->readcb = NULL;
2666 peer->sk->writecb = NULL;
2667 peer->sk->errcb = NULL;
2670 if(peer->supports != NULL)
2672 for(i = 0; peer->supports[i] != NULL; i++)
2673 free(peer->supports[i]);
2674 free(peer->supports);
2676 if(peer->mbspath != NULL)
2677 free(peer->mbspath);
2678 if(peer->inbuf != NULL)
2680 if(peer->key != NULL)
2682 if(peer->wcsname != NULL)
2683 free(peer->wcsname);
2684 if(peer->nativename != NULL)
2685 free(peer->nativename);
2686 if(peer->fn != NULL)
2687 putfnetnode(peer->fn);
2688 while((qcmd = ulqcmd(&peer->queue)) != NULL)
2694 static void hubconnect(struct fnetnode *fn)
2696 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
2697 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
2699 fn->data = newdchub(fn);
2704 static void hubdestroy(struct fnetnode *fn)
2707 struct qcommand *qcmd;
2709 hub = (struct dchub *)fn->data;
2712 if(fn->sk->data == fn)
2714 fn->sk->data = NULL;
2720 while((qcmd = ulqcmd(&hub->queue)) != NULL)
2722 if(hub->nativename != NULL)
2723 free(hub->nativename);
2724 if(hub->nativenick != NULL)
2725 free(hub->nativenick);
2726 if(hub->inbuf != NULL)
2731 static wchar_t *dcbasename(wchar_t *filename)
2735 if((ret = wcsrchr(filename, L'\\')) != NULL)
2740 static struct transferiface dctransfer =
2742 .detach = (void (*)(struct transfer *, void *))dctransdetach,
2743 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
2744 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
2745 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
2748 static struct fnet dcnet =
2751 .connect = hubconnect,
2752 .destroy = hubdestroy,
2753 .setnick = hubsetnick,
2754 .reqconn = hubreqconn,
2755 .sendchat = hubsendchat,
2756 .search = hubsearch,
2757 .filebasename = dcbasename
2760 static void peerread(struct socket *sk, struct dcpeer *peer)
2765 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2767 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
2768 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
2770 p = peer->inbuf + peer->inbufdata;
2771 peer->inbufdata += datalen;
2772 while((datalen > 0) && (p = memchr(p, '|', datalen)) != NULL)
2775 newqcmd(&peer->queue, peer->inbuf);
2776 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
2777 datalen = peer->inbufdata;
2782 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
2787 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
2789 struct dcpeer *peer;
2796 peer = newdcpeer(sk);
2799 sk->readcb = (void (*)(struct socket *, void *))peerread;
2800 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
2802 socksettos(sk, confgetint("fnet", "fnptos"));
2803 peerhandleaction(sk, peer, NULL, NULL);
2807 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
2809 struct dcpeer *peer;
2811 peer = newdcpeer(newsk);
2813 newsk->readcb = (void (*)(struct socket *, void *))peerread;
2814 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
2816 socksettos(newsk, confgetint("fnet", "fnptos"));
2817 peerhandleaction(sk, peer, NULL, NULL);
2820 static void updatehmlist(void)
2822 int i, lev, ic, ret;
2823 struct sharecache *node;
2824 char *buf, *buf2, numbuf[32];
2825 size_t bufsize, bufdata;
2829 buf = smalloc(bufsize = 65536);
2830 node = shareroot->child;
2835 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
2837 for(i = 0; i < lev; i++)
2839 bufcat(buf, buf2, strlen(buf2));
2841 if(node->f.b.type == FILE_REG)
2844 sprintf(numbuf, "%i", node->size);
2845 bufcat(buf, numbuf, strlen(numbuf));
2852 if((node->child != NULL) && !ic)
2856 } else if(node->next != NULL) {
2859 while(node->next == NULL)
2862 node = node->parent;
2863 if(node == shareroot)
2866 if(node == shareroot)
2871 if(hmlistname != NULL)
2876 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
2877 if((fd = mkstemp(hmlistname)) < 0)
2879 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
2884 * I do not want to implement a good Huffman encoder, and it's not
2885 * like Huffman encoding actually yields any impressive results
2886 * for DC file lists anyway, so I'll just output a bogus
2887 * tree. Implement a good encoder if you want to.
2889 write(fd, "HE3\r\0", 5);
2890 write(fd, &bufdata, 4);
2892 write(fd, &ibuf, 2);
2894 for(i = 0; i < 256; i++)
2897 write(fd, &ibuf, 1);
2899 for(i = 0; i < 256; i++)
2901 for(buf2 = buf; bufdata > 0;)
2903 if((ret = write(fd, buf2, bufdata)) < 0)
2905 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
2916 static struct xmlent
2923 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
2927 static wchar_t *escapexml(wchar_t *src)
2929 static wchar_t *buf = NULL;;
2932 size_t bufsize, bufdata;
2938 bufsize = bufdata = 0;
2939 for(; *src != L'\0'; src++)
2942 if((c > 0) && (c < 32))
2944 bufcat(buf, L"&#", 2);
2945 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
2946 bufcat(buf, nbuf, ret);
2947 addtobuf(buf, L';');
2949 for(ent = entities; ent->ent != NULL; ent++)
2953 bufcat(buf, ent->ent, wcslen(ent->ent));
2957 if(ent->ent == NULL)
2958 addtobuf(buf, *src);
2961 addtobuf(buf, L'\0');
2965 static void updatexmllist(void)
2969 char cidbuf[14], *namebuf;
2971 struct sharecache *node;
2973 if(xmllistname != NULL)
2975 unlink(xmllistname);
2978 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
2979 if((fd = mkstemp(xmllistname)) < 0)
2981 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
2986 if((fs = fdopen(fd, "w")) == NULL)
2988 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
2989 unlink(xmllistname);
2995 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
2996 for(i = 0; i < sizeof(cidbuf) - 1; i++)
2997 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
2999 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, DCIDFULL);
3001 node = shareroot->child;
3005 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3007 for(i = 0; i < lev; i++)
3009 if(node->child != NULL)
3011 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3016 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3017 if(node->f.b.hastth)
3019 hashbuf = base32encode(node->hashtth, 24);
3020 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3023 fprintf(fs, "/>\r\n");
3025 while(node->next == NULL)
3027 node = node->parent;
3028 if(node == shareroot)
3033 for(i = 0; i < lev; i++)
3035 fprintf(fs, "</Directory>\r\n");
3038 if(node == shareroot)
3044 #ifdef DCPP_MASQUERADE
3045 fprintf(fs, "</FileListing>");
3047 fprintf(fs, "</FileListing>\r\n");
3052 static void updatexmlbz2list(void)
3061 if(xmllistname == NULL)
3063 if((in = fopen(xmllistname, "r")) == NULL)
3065 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3068 if(xmlbz2listname != NULL)
3070 unlink(xmlbz2listname);
3071 free(xmlbz2listname);
3073 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3074 if((fd = mkstemp(xmlbz2listname)) < 0)
3076 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3077 free(xmlbz2listname);
3078 xmlbz2listname = NULL;
3082 if((real = fdopen(fd, "w")) == NULL)
3084 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3086 unlink(xmlbz2listname);
3087 free(xmlbz2listname);
3088 xmlbz2listname = NULL;
3092 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3095 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3097 unlink(xmlbz2listname);
3098 free(xmlbz2listname);
3099 xmlbz2listname = NULL;
3105 bufdata = fread(buf, 1, sizeof(buf), in);
3106 BZ2_bzWrite(&err, out, buf, bufdata);
3109 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3113 static int shareupdate(unsigned long long uusharesize, void *data)
3121 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3124 struct command *cmd;
3126 if((p = strchr(qcmd->string, ' ')) != NULL)
3128 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3130 if(!strcmp(cmd->name, qcmd->string))
3133 if(cmd->handler != NULL)
3134 cmd->handler(sk, data, qcmd->string, p);
3137 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3141 static int run(void)
3143 struct fnetnode *fn, *nextfn;
3145 struct dcpeer *peer, *nextpeer;
3146 struct qcommand *qcmd;
3150 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3153 if(fn->fnet != &dcnet)
3155 if(fn->data == NULL)
3157 hub = (struct dchub *)fn->data;
3158 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3160 if(*qcmd->string == '$')
3162 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3163 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3164 } else if(*qcmd->string != 0) {
3165 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3172 for(peer = peers; peer != NULL; peer = nextpeer)
3174 nextpeer = peer->next;
3175 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3177 if(*qcmd->string == '$')
3178 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3187 static void preinit(int hup)
3194 static int updateudpport(struct configvar *var, void *uudata)
3196 struct sockaddr_in addr;
3197 struct socket *newsock;
3199 memset(&addr, 0, sizeof(addr));
3200 addr.sin_family = AF_INET;
3201 addr.sin_port = htons(var->val.num);
3202 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3204 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3207 newsock->readcb = udpread;
3214 static int updatetcpport(struct configvar *var, void *uudata)
3216 struct sockaddr_in addr;
3217 struct socket *newsock;
3219 memset(&addr, 0, sizeof(addr));
3220 addr.sin_family = AF_INET;
3221 addr.sin_port = htons(var->val.num);
3222 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3223 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3230 static int init(int hup)
3232 struct sockaddr_in addr;
3236 GCBREG(sharechangecb, shareupdate, NULL);
3241 addr.sin_family = AF_INET;
3242 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3243 addr.sin_port = htons(confgetint("dc", "udpport"));
3244 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3246 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3249 udpsock->readcb = udpread;
3250 addr.sin_port = htons(confgetint("dc", "tcpport"));
3251 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3252 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3253 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3254 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3255 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3260 static void terminate(void)
3262 if(hmlistname != NULL)
3267 if(xmllistname != NULL)
3269 unlink(xmllistname);
3272 if(xmlbz2listname != NULL)
3274 unlink(xmlbz2listname);
3275 free(xmlbz2listname);
3279 static struct configvar myvars[] =
3281 {CONF_VAR_STRING, "desc", {.str = L""}},
3282 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3283 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3284 {CONF_VAR_INT, "udpport", {.num = 0}},
3285 {CONF_VAR_INT, "tcpport", {.num = 0}},
3289 static struct module me =
3298 .terminate = terminate,