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;
1410 wchar_t *wnick, *wfile;
1414 if((p2 = strchr(p, ' ')) == NULL)
1419 if((p2 = strchr(p, 5)) == NULL)
1423 if((p2 = strchr(p, ' ')) == NULL)
1428 if((p2 = strchr(p, '/')) == NULL)
1433 if((p2 = strchr(p, 5)) == NULL)
1437 if((p2 = strstr(p, " (")) == NULL)
1440 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
1442 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1447 sr = newsrchres(&dcnet, wfile, wnick);
1448 if(sr->peernick != NULL)
1450 sr->peernick = swcsdup(wnick);
1455 if(!strncmp(hubname, "TTH:", 4))
1457 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1460 sr->hash = newhash(L"TTH", 24, buf);
1464 getfnetnode(sr->fn = fn);
1467 hubhandleaction(sk, fn, cmd, args);
1470 static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1472 /* Do nothing for now. */
1475 static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1477 if(peer->nativename != NULL)
1478 free(peer->nativename);
1479 peer->nativename = sstrdup(args);
1480 if(peer->wcsname != NULL)
1481 free(peer->wcsname);
1482 if((peer->wcsname = icmbstowcs(peer->nativename, DCCHARSET)) == NULL)
1487 peerhandleaction(sk, peer, cmd, args);
1490 static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1494 if((p = strchr(args, ' ')) == NULL)
1497 if(!strcmp(args, "Upload"))
1498 peer->direction = TRNSD_DOWN;
1499 if(!strcmp(args, "Download"))
1500 peer->direction = TRNSD_UP;
1501 peerhandleaction(sk, peer, cmd, args);
1504 static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1508 if((p = strchr(args, ' ')) == NULL)
1511 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1513 if(peer->key != NULL)
1515 peer->key = dcmakekey(args);
1516 peerhandleaction(sk, peer, cmd, args);
1519 static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1521 peerhandleaction(sk, peer, cmd, args);
1524 static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1528 if(peer->transfer == NULL)
1534 if(peer->transfer->size == size)
1538 /* Will have to restart, then... I really hate this, but what
1539 * am I to do then, considering the DC protocol requires the
1540 * resume offset before it sends the filesize? */
1541 transfersetsize(peer->transfer, size);
1545 peerhandleaction(sk, peer, cmd, args);
1548 static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1550 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1552 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1553 resettransfer(peer->transfer);
1559 static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1561 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1563 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1564 resettransfer(peer->transfer);
1577 {"MyList.DcLst", &hmlistname, updatehmlist},
1578 {"files.xml", &xmllistname, updatexmllist},
1579 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1583 static int openfilelist(char *name)
1588 for(i = 0; lists[i].name != NULL; i++)
1590 if(!strcmp(name, lists[i].name))
1594 if(lists[i].name == NULL)
1597 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1599 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1602 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1609 static struct sharecache *findbytth(char *tth32)
1613 struct sharecache *node;
1615 if((buf = base32decode(tth32, &buflen)) == NULL)
1622 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1624 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1631 static struct sharecache *resdcpath(char *path, char *charset, char sep)
1633 struct sharecache *node;
1640 if((p2 = strchr(p, sep)) != NULL)
1642 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1649 static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1654 struct sharecache *node;
1655 struct socket *lesk;
1659 if(peer->transfer == NULL)
1664 if((p = strchr(args, '$')) == NULL)
1670 if((offset = (atoi(p) - 1)) < 0)
1675 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1677 qstr(sk, "$Error Could not send file list|");
1680 } else if(fd >= 0) {
1681 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1682 transfersetpath(peer->transfer, buf2);
1688 qstr(sk, "$MaxedOut|");
1692 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1694 qstrf(sk, "$Error File not in share|");
1698 if((fd = opensharecache(node)) < 0)
1700 qstrf(sk, "$Error %s|", strerror(errno));
1704 buf = getfspath(node);
1705 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1706 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1708 transfersetpath(peer->transfer, buf2);
1711 if(fstat(fd, &sb) < 0)
1714 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1715 qstrf(sk, "$Error|");
1719 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1722 qstrf(sk, "$Error Offset out of range|");
1726 lesk = wrapsock(fd);
1727 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1729 qstrf(sk, "$FileLength %i|", peer->transfer->size);
1730 peerhandleaction(sk, peer, cmd, args);
1733 static void starttrans(struct dcpeer *peer)
1735 peer->state = PEER_TRNS;
1736 transferstartul(peer->transfer, peer->sk);
1737 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
1740 static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1742 if(peer->transfer == NULL)
1747 if(peer->transfer->localend == NULL)
1754 peerhandleaction(sk, peer, cmd, args);
1757 static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1762 size_t arrsize, arrdata;
1764 if(peer->supports != NULL)
1766 for(i = 0; peer->supports[i] != NULL; i++)
1767 free(peer->supports[i]);
1768 free(peer->supports);
1771 arrsize = arrdata = 0;
1775 if((p2 = strchr(p, ' ')) != NULL)
1779 addtobuf(arr, sstrdup(p));
1780 } while((p = p2) != NULL);
1781 addtobuf(arr, NULL);
1782 peer->supports = arr;
1783 peerhandleaction(sk, peer, cmd, args);
1786 static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1790 int start, numbytes;
1791 char *charset, *buf;
1793 struct sharecache *node;
1795 struct socket *lesk;
1797 if(peer->transfer == NULL)
1803 if((p2 = strchr(p, ' ')) == NULL)
1811 if((p2 = strchr(p, ' ')) == NULL)
1819 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1822 charset = DCCHARSET;
1823 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1824 initcompress(peer, CPRS_ZLIB);
1825 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1827 qstr(sk, "$Error Could not send file list|");
1829 } else if(fd >= 0) {
1830 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1831 transfersetpath(peer->transfer, buf2);
1837 qstr(sk, "$MaxedOut|");
1840 if((node = resdcpath(p, charset, '\\')) == NULL)
1842 qstr(sk, "$Error File not in cache|");
1845 if((fd = opensharecache(node)) < 0)
1847 qstrf(sk, "$Error %s|", strerror(errno));
1850 buf = getfspath(node);
1851 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1852 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1854 transfersetpath(peer->transfer, buf2);
1857 if(fstat(fd, &sb) < 0)
1860 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1861 qstr(sk, "$Error|");
1864 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1867 qstr(sk, "$Error Offset out of range|");
1870 if((numbytes < 0) || (start + numbytes > sb.st_size))
1871 numbytes = sb.st_size - start;
1872 lesk = wrapsock(fd);
1873 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1875 qstrf(sk, "$Sending %i|", numbytes);
1877 peerhandleaction(sk, peer, cmd, args);
1880 static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1884 int start, numbytes;
1885 struct sharecache *node;
1887 struct socket *lesk;
1891 if(peer->transfer == NULL)
1896 if((argv = parseadc(args)) == NULL)
1901 if(parrlen(argv) < 4)
1906 start = atoi(argv[2]);
1907 numbytes = atoi(argv[3]);
1910 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
1912 qstr(sk, "$Error Could not send file list|");
1914 } else if(fd >= 0) {
1915 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
1916 transfersetpath(peer->transfer, wbuf);
1922 qstr(sk, "$MaxedOut|");
1925 if(!strncmp(argv[1], "TTH/", 4))
1927 if((node = findbytth(argv[1] + 4)) == NULL)
1929 qstr(sk, "$Error File not in cache|");
1933 if((node = resdcpath(argv[1], "UTF-8", '/')) == NULL)
1935 qstr(sk, "$Error File not in cache|");
1939 if((fd = opensharecache(node)) < 0)
1941 qstrf(sk, "$Error %s|", strerror(errno));
1944 buf = getfspath(node);
1945 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
1946 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1948 transfersetpath(peer->transfer, wbuf);
1951 if(!strcmp(argv[0], "file"))
1953 for(i = 4; argv[i] != NULL; i++)
1955 if(!strcmp(argv[i], "ZL1"))
1956 initcompress(peer, CPRS_ZLIB);
1958 if(fstat(fd, &sb) < 0)
1960 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1961 qstr(sk, "$Error|");
1964 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1966 qstr(sk, "$Error Offset out of range|");
1969 if((numbytes < 0) || (start + numbytes > sb.st_size))
1970 numbytes = sb.st_size - start;
1971 lesk = wrapsock(fd);
1972 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1975 qstr(sk, "$ADCSND");
1976 sendadc(sk, "file");
1977 sendadc(sk, argv[1]);
1978 sendadcf(sk, "%i", start);
1979 sendadcf(sk, "%i", numbytes);
1980 if(peer->compress == CPRS_ZLIB)
1984 } else if(!strcmp(argv[0], "tthl")) {
1986 * XXX: Implement full TTHL support.
1988 * In the meantime, this is better than nothing, since it at
1989 * least allows fetching of the TTH root if it isn't already
1994 qstr(sk, "$Error no TTHL data for virtual files|");
1997 qstr(sk, "$ADCSND");
1998 sendadc(sk, "tthl");
1999 sendadc(sk, argv[1]);
2003 sockqueue(sk, node->hashtth, 24);
2005 qstr(sk, "$Error Namespace not implemented|");
2008 peerhandleaction(sk, peer, cmd, args);
2018 static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2023 hubhandleaction(sk, fn, cmd, args);
2027 static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2029 peerhandleaction(sk, peer, cmd, args);
2034 static int hubreqconn(struct fnetpeer *peer)
2039 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2044 if((hub = peer->fn->data) == NULL)
2049 if((mbsnick = icwcstombs(peer->id, DCCHARSET)) == NULL)
2050 return(1); /* Shouldn't happen, of course, but who knows... */
2053 sendctm(peer->fn->sk, mbsnick);
2054 expectpeer(mbsnick, peer->fn);
2056 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2062 static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2065 char *mbsstring, *mbsto;
2068 if((mbsto = icwcstombs(to, DCCHARSET)) == NULL)
2073 if((mbsstring = icwcstombs(string, DCCHARSET)) == NULL)
2078 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2085 if(strchr(mbsstring, '|') != NULL)
2096 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2098 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2101 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2108 static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2110 int minl, maxl, minr, maxr, retmin, retmax;
2115 findsizelimit(sexpr->l, &minl, &maxl);
2116 findsizelimit(sexpr->r, &minr, &maxr);
2117 retmin = (minl > minr)?minl:minr;
2118 if((maxl != -1) && (maxr != -1))
2119 retmax = (maxl < maxr)?maxl:maxr;
2128 findsizelimit(sexpr->l, &minl, &maxl);
2129 findsizelimit(sexpr->r, &minr, &maxr);
2130 retmin = (minl < minr)?minl:minr;
2131 if((maxl == -1) || (maxr == -1))
2134 retmax = (maxl > maxr)?maxl:maxr;
2137 findsizelimit(sexpr->l, &minl, &maxl);
2138 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2142 } else if((minl == 0) && (maxl != -1)) {
2145 } else if((minl != 0) && (maxl == -1)) {
2148 } else { /* This would yield two seperate intervals, which DC cannot handle */
2154 retmax = sexpr->d.n - 1;
2157 retmin = sexpr->d.n;
2158 retmax = sexpr->d.n;
2161 retmin = sexpr->d.n + 1;
2175 static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2178 struct wcslist *list, *cur;
2179 char *sstr, *buf, *p;
2180 size_t sstrsize, sstrdata;
2181 struct sockaddr *name;
2183 int minsize, maxsize;
2187 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2189 list = findsexprstrs(srch->sexpr);
2190 findsizelimit(srch->sexpr, &minsize, &maxsize);
2191 if((minsize != 0) && (maxsize != -1))
2193 /* Choose either minsize or maxsize by trying to determine
2194 * which choice will be most restrictive. The result can be
2195 * approximative at best anyway, so don't try too hard... */
2196 if((50000000 - maxsize) > (minsize - 50000000))
2203 snprintf(sizedesc, sizeof(sizedesc), "T?F?%i", minsize);
2204 } else if(maxsize != -1) {
2205 snprintf(sizedesc, sizeof(sizedesc), "T?T?%i", maxsize);
2207 strcpy(sizedesc, "F?F?0");
2210 sstrsize = sstrdata = 0;
2213 for(cur = list; cur != NULL; cur = cur->next)
2215 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2217 /* Can't find anything anyway if the search expression
2218 * requires characters outside DC's charset. There's
2219 * nothing technically wrong with the search itself,
2220 * however, so return success. This should be
2221 * considered as an optimization. */
2228 addtobuf(sstr, '$');
2230 * It probably doesn't hurt if buf contains any extra
2231 * dollar signs - it will just result in extra search
2232 * terms, and the extraneous results will be filtered by
2233 * the search layer anyway. It hurts if it contains any
2234 * pipes, though, so let's sell them for money.
2236 for(p = buf; *p; p++)
2241 bufcat(sstr, buf, strlen(buf));
2245 /* Will match all files... :-/ */
2246 addtobuf(sstr, '.');
2251 if(sockgetremotename(udpsock, &name, &namelen) < 0)
2253 flog(LOG_WARNING, "cannot get address of UDP socket");
2255 qstrf(fn->sk, "$Search %s %s?1?%s|", formataddress(name, namelen), sizedesc, sstr);
2259 qstrf(fn->sk, "$Search Hub:%s %s?1?%s|", hub->nativenick, sizedesc, sstr);
2263 fn->lastsrch = time(NULL);
2271 #define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
2272 struct command hubcmds[] =
2274 {"$Lock", cc(cmd_lock)},
2275 {"$HubName", cc(cmd_hubname)},
2276 {"$Hello", cc(cmd_hello)},
2277 {"$Quit", cc(cmd_quit)},
2278 {"$NickList", cc(cmd_nicklist)},
2279 {"$OpList", cc(cmd_oplist)},
2280 {"$MyINFO", cc(cmd_myinfo)},
2281 {"$ForceMove", cc(cmd_forcemove)},
2282 {"$Search", cc(cmd_search)},
2283 {"$MultiSearch", cc(cmd_search)},
2284 {"$ConnectToMe", cc(cmd_connecttome)},
2285 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2286 {"$GetNetInfo", cc(cmd_getnetinfo)},
2287 {"$To:", cc(cmd_to)},
2288 {"$SR", cc(cmd_sr)},
2289 {"$UserCommand", cc(cmd_usercommand)},
2293 struct command peercmds[] =
2295 {"$MyNick", cc(cmd_mynick)},
2296 {"$Lock", cc(cmd_peerlock)},
2297 {"$Direction", cc(cmd_direction)},
2298 {"$Key", cc(cmd_key)},
2299 {"$FileLength", cc(cmd_filelength)},
2300 {"$Error", cc(cmd_error)},
2301 {"$MaxedOut", cc(cmd_maxedout)},
2302 {"$Get", cc(cmd_get)},
2303 {"$Send", cc(cmd_send)},
2304 {"$Supports", cc(cmd_supports)},
2305 {"$GetBlock", cc(cmd_getblock)},
2306 {"$UGetBlock", cc(cmd_getblock)},
2307 {"$GetZBlock", cc(cmd_getblock)},
2308 {"$UGetZBlock", cc(cmd_getblock)},
2309 {"$ADCGET", cc(cmd_adcget)},
2314 static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2316 CBUNREG(transfer, trans_filterout, peer);
2319 peer->transfer = NULL;
2323 static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2331 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2333 if(sockqueuesize(peer->sk) < 65536)
2335 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2337 if(peer->compress == CPRS_NONE)
2339 sockqueue(peer->sk, buf, bufsize);
2340 } else if(peer->compress == CPRS_ZLIB) {
2341 cstr = peer->cprsdata;
2342 cstr->next_in = buf;
2343 cstr->avail_in = bufsize;
2344 while(cstr->avail_in > 0)
2346 cstr->next_out = outbuf;
2347 cstr->avail_out = sizeof(outbuf);
2348 if((ret = deflate(cstr, 0)) != Z_OK)
2350 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2354 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2359 if(peer->state == PEER_SYNC)
2361 if(peer->compress == CPRS_ZLIB)
2363 cstr = peer->cprsdata;
2364 cstr->next_in = NULL;
2368 cstr->next_out = outbuf;
2369 cstr->avail_out = sizeof(outbuf);
2370 ret = deflate(cstr, Z_FINISH);
2371 if((ret != Z_OK) && (ret != Z_STREAM_END))
2373 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2377 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2378 } while(ret != Z_STREAM_END);
2384 peer->state = PEER_CMD;
2386 transfersetstate(transfer, TRNS_HS);
2387 socksettos(peer->sk, confgetint("fnet", "fnptos"));
2388 peer->sk->writecb = NULL;
2395 static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2397 peer->state = PEER_SYNC;
2398 dctransgotdata(transfer, peer);
2401 static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2403 if(transferdatasize(transfer) < 65536)
2404 peer->sk->ignread = 0;
2407 static void transread(struct socket *sk, struct dcpeer *peer)
2411 struct transfer *transfer;
2413 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2415 if(peer->transfer == NULL)
2421 transferputdata(peer->transfer, buf, bufsize);
2423 if(peer->transfer->curpos >= peer->transfer->size)
2425 transfer = peer->transfer;
2426 transferdetach(transfer);
2427 transferendofdata(transfer);
2430 if(transferdatasize(peer->transfer) > 65535)
2434 static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2436 struct transfer *transfer;
2438 if((transfer = peer->transfer) == NULL)
2443 transferdetach(transfer);
2444 transferendofdata(transfer);
2447 static void transwrite(struct socket *sk, struct dcpeer *peer)
2449 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2451 if(peer->transfer == NULL)
2456 dctransgotdata(peer->transfer, peer);
2459 static void udpread(struct socket *sk, void *data)
2461 char *buf, *p, *p2, *hashbuf;
2462 size_t buflen, hashlen;
2463 char *nick, *filename, *hubname;
2465 struct fnetnode *fn, *myfn;
2468 wchar_t *wnick, *wfile;
2471 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2473 buf = srealloc(buf, buflen + 1);
2475 if(!strncmp(buf, "$SR ", 4))
2479 if((p2 = strchr(p, ' ')) == NULL)
2487 if((p2 = strchr(p, 5)) == NULL)
2494 if((p2 = strchr(p, ' ')) == NULL)
2502 if((p2 = strchr(p, '/')) == NULL)
2510 if((p2 = strchr(p, 5)) == NULL)
2517 if((p2 = strstr(p, " (")) == NULL)
2523 if((wnick = icmbstowcs(nick, DCCHARSET)) == NULL)
2528 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2536 if(!strncmp(hubname, "TTH:", 4))
2538 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2541 hash = newhash(L"TTH", 24, hashbuf);
2545 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2547 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
2549 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
2562 sr = newsrchres(&dcnet, wfile, wnick);
2563 if(sr->peernick != NULL)
2565 sr->peernick = swcsdup(wnick);
2571 getfnetnode(sr->fn = myfn);
2580 static void hubread(struct socket *sk, struct fnetnode *fn)
2587 hub = (struct dchub *)fn->data;
2588 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2590 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2592 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2593 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2595 p = hub->inbuf + hub->inbufdata;
2596 hub->inbufdata += datalen;
2597 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2600 newqcmd(&hub->queue, hub->inbuf);
2601 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2602 datalen = hub->inbufdata;
2607 static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2612 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2618 if((buf = icwcstombs(newnick, DCCHARSET)) == NULL)
2620 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2625 if(hub == NULL) /* Not yet connected */
2630 if(hub->nativenick != NULL)
2631 free(hub->nativenick);
2632 hub->nativenick = buf;
2636 static struct dchub *newdchub(struct fnetnode *fn)
2640 new = smalloc(sizeof(*new));
2641 memset(new, 0, sizeof(*new));
2643 if(hubsetnick(fn, fn->mynick))
2644 fnetsetnick(fn, L"DoldaConnectUser-IN");
2645 /* IN as in Invalid Nick */
2649 static struct dcpeer *newdcpeer(struct socket *sk)
2653 new = smalloc(sizeof(*new));
2654 memset(new, 0, sizeof(*new));
2655 new->transfer = NULL;
2667 static void freedcpeer(struct dcpeer *peer)
2670 struct qcommand *qcmd;
2675 if(peer->next != NULL)
2676 peer->next->prev = peer->prev;
2677 if(peer->prev != NULL)
2678 peer->prev->next = peer->next;
2679 if(peer->transfer != NULL)
2681 if(peer->transfer->dir == TRNSD_UP)
2682 peer->transfer->close = 1;
2683 if(peer->transfer->dir == TRNSD_DOWN)
2684 resettransfer(peer->transfer);
2685 transferdetach(peer->transfer);
2687 if(peer->sk->data == peer)
2688 peer->sk->data = NULL;
2689 peer->sk->readcb = NULL;
2690 peer->sk->writecb = NULL;
2691 peer->sk->errcb = NULL;
2694 if(peer->supports != NULL)
2696 for(i = 0; peer->supports[i] != NULL; i++)
2697 free(peer->supports[i]);
2698 free(peer->supports);
2700 if(peer->mbspath != NULL)
2701 free(peer->mbspath);
2702 if(peer->inbuf != NULL)
2704 if(peer->key != NULL)
2706 if(peer->wcsname != NULL)
2707 free(peer->wcsname);
2708 if(peer->nativename != NULL)
2709 free(peer->nativename);
2710 if(peer->fn != NULL)
2711 putfnetnode(peer->fn);
2712 while((qcmd = ulqcmd(&peer->queue)) != NULL)
2718 static void hubconnect(struct fnetnode *fn)
2720 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
2721 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
2723 fn->data = newdchub(fn);
2728 static void hubdestroy(struct fnetnode *fn)
2731 struct qcommand *qcmd;
2733 hub = (struct dchub *)fn->data;
2736 if(fn->sk->data == fn)
2738 fn->sk->data = NULL;
2744 while((qcmd = ulqcmd(&hub->queue)) != NULL)
2746 if(hub->nativename != NULL)
2747 free(hub->nativename);
2748 if(hub->nativenick != NULL)
2749 free(hub->nativenick);
2750 if(hub->inbuf != NULL)
2755 static wchar_t *dcbasename(wchar_t *filename)
2759 if((ret = wcsrchr(filename, L'\\')) != NULL)
2764 static struct transferiface dctransfer =
2766 .detach = (void (*)(struct transfer *, void *))dctransdetach,
2767 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
2768 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
2769 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
2772 static struct fnet dcnet =
2775 .connect = hubconnect,
2776 .destroy = hubdestroy,
2777 .setnick = hubsetnick,
2778 .reqconn = hubreqconn,
2779 .sendchat = hubsendchat,
2780 .search = hubsearch,
2781 .filebasename = dcbasename
2784 static void peerread(struct socket *sk, struct dcpeer *peer)
2789 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2791 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
2792 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
2794 p = peer->inbuf + peer->inbufdata;
2795 peer->inbufdata += datalen;
2796 while((datalen > 0) && (p = memchr(p, '|', datalen)) != NULL)
2799 newqcmd(&peer->queue, peer->inbuf);
2800 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
2801 datalen = peer->inbufdata;
2806 static void peererror(struct socket *sk, int err, struct dcpeer *peer)
2811 static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
2813 struct dcpeer *peer;
2820 peer = newdcpeer(sk);
2823 sk->readcb = (void (*)(struct socket *, void *))peerread;
2824 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
2826 socksettos(sk, confgetint("fnet", "fnptos"));
2827 peerhandleaction(sk, peer, NULL, NULL);
2831 static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
2833 struct dcpeer *peer;
2835 peer = newdcpeer(newsk);
2837 newsk->readcb = (void (*)(struct socket *, void *))peerread;
2838 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
2840 socksettos(newsk, confgetint("fnet", "fnptos"));
2841 peerhandleaction(sk, peer, NULL, NULL);
2844 static void updatehmlist(void)
2846 int i, lev, ic, ret;
2847 struct sharecache *node;
2848 char *buf, *buf2, numbuf[32];
2849 size_t bufsize, bufdata;
2853 buf = smalloc(bufsize = 65536);
2854 node = shareroot->child;
2859 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
2861 for(i = 0; i < lev; i++)
2863 bufcat(buf, buf2, strlen(buf2));
2865 if(node->f.b.type == FILE_REG)
2868 sprintf(numbuf, "%i", node->size);
2869 bufcat(buf, numbuf, strlen(numbuf));
2876 if((node->child != NULL) && !ic)
2880 } else if(node->next != NULL) {
2883 while(node->next == NULL)
2886 node = node->parent;
2887 if(node == shareroot)
2890 if(node == shareroot)
2895 if(hmlistname != NULL)
2900 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
2901 if((fd = mkstemp(hmlistname)) < 0)
2903 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
2908 * I do not want to implement a good Huffman encoder, and it's not
2909 * like Huffman encoding actually yields any impressive results
2910 * for DC file lists anyway, so I'll just output a bogus
2911 * tree. Implement a good encoder if you want to.
2913 write(fd, "HE3\r\0", 5);
2914 write(fd, &bufdata, 4);
2916 write(fd, &ibuf, 2);
2918 for(i = 0; i < 256; i++)
2921 write(fd, &ibuf, 1);
2923 for(i = 0; i < 256; i++)
2925 for(buf2 = buf; bufdata > 0;)
2927 if((ret = write(fd, buf2, bufdata)) < 0)
2929 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
2940 static struct xmlent
2947 /* {L'\'', L"""}, Emulate DC++ escaping by omitting this. */
2951 static wchar_t *escapexml(wchar_t *src)
2953 static wchar_t *buf = NULL;;
2956 size_t bufsize, bufdata;
2962 bufsize = bufdata = 0;
2963 for(; *src != L'\0'; src++)
2966 if((c > 0) && (c < 32))
2968 bufcat(buf, L"&#", 2);
2969 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
2970 bufcat(buf, nbuf, ret);
2971 addtobuf(buf, L';');
2973 for(ent = entities; ent->ent != NULL; ent++)
2977 bufcat(buf, ent->ent, wcslen(ent->ent));
2981 if(ent->ent == NULL)
2982 addtobuf(buf, *src);
2985 addtobuf(buf, L'\0');
2989 static void updatexmllist(void)
2993 char cidbuf[14], *namebuf;
2995 struct sharecache *node;
2997 if(xmllistname != NULL)
2999 unlink(xmllistname);
3002 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3003 if((fd = mkstemp(xmllistname)) < 0)
3005 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3010 if((fs = fdopen(fd, "w")) == NULL)
3012 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3013 unlink(xmllistname);
3019 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3020 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3021 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3023 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, DCIDFULL);
3025 node = shareroot->child;
3029 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3031 for(i = 0; i < lev; i++)
3033 if(node->child != NULL)
3035 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3040 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3041 if(node->f.b.hastth)
3043 hashbuf = base32encode(node->hashtth, 24);
3044 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3047 fprintf(fs, "/>\r\n");
3049 while(node->next == NULL)
3051 node = node->parent;
3052 if(node == shareroot)
3057 for(i = 0; i < lev; i++)
3059 fprintf(fs, "</Directory>\r\n");
3062 if(node == shareroot)
3068 #ifdef DCPP_MASQUERADE
3069 fprintf(fs, "</FileListing>");
3071 fprintf(fs, "</FileListing>\r\n");
3076 static void updatexmlbz2list(void)
3085 if(xmllistname == NULL)
3087 if((in = fopen(xmllistname, "r")) == NULL)
3089 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3092 if(xmlbz2listname != NULL)
3094 unlink(xmlbz2listname);
3095 free(xmlbz2listname);
3097 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3098 if((fd = mkstemp(xmlbz2listname)) < 0)
3100 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3101 free(xmlbz2listname);
3102 xmlbz2listname = NULL;
3106 if((real = fdopen(fd, "w")) == NULL)
3108 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3110 unlink(xmlbz2listname);
3111 free(xmlbz2listname);
3112 xmlbz2listname = NULL;
3116 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3119 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3121 unlink(xmlbz2listname);
3122 free(xmlbz2listname);
3123 xmlbz2listname = NULL;
3129 bufdata = fread(buf, 1, sizeof(buf), in);
3130 BZ2_bzWrite(&err, out, buf, bufdata);
3133 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3137 static int shareupdate(unsigned long long uusharesize, void *data)
3145 static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3148 struct command *cmd;
3150 if((p = strchr(qcmd->string, ' ')) != NULL)
3152 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3154 if(!strcmp(cmd->name, qcmd->string))
3157 if(cmd->handler != NULL)
3158 cmd->handler(sk, data, qcmd->string, p);
3161 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3165 static int run(void)
3167 struct fnetnode *fn, *nextfn;
3169 struct dcpeer *peer, *nextpeer;
3170 struct qcommand *qcmd;
3174 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3177 if(fn->fnet != &dcnet)
3179 if(fn->data == NULL)
3181 hub = (struct dchub *)fn->data;
3182 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3184 if(*qcmd->string == '$')
3186 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3187 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3188 } else if(*qcmd->string != 0) {
3189 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3196 for(peer = peers; peer != NULL; peer = nextpeer)
3198 nextpeer = peer->next;
3199 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3201 if(*qcmd->string == '$')
3202 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3211 static void preinit(int hup)
3218 static int updateudpport(struct configvar *var, void *uudata)
3220 struct sockaddr_in addr;
3221 struct socket *newsock;
3223 memset(&addr, 0, sizeof(addr));
3224 addr.sin_family = AF_INET;
3225 addr.sin_port = htons(var->val.num);
3226 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3228 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3231 newsock->readcb = udpread;
3238 static int updatetcpport(struct configvar *var, void *uudata)
3240 struct sockaddr_in addr;
3241 struct socket *newsock;
3243 memset(&addr, 0, sizeof(addr));
3244 addr.sin_family = AF_INET;
3245 addr.sin_port = htons(var->val.num);
3246 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3247 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3254 static int init(int hup)
3256 struct sockaddr_in addr;
3260 GCBREG(sharechangecb, shareupdate, NULL);
3265 addr.sin_family = AF_INET;
3266 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3267 addr.sin_port = htons(confgetint("dc", "udpport"));
3268 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3270 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3273 udpsock->readcb = udpread;
3274 addr.sin_port = htons(confgetint("dc", "tcpport"));
3275 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3276 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3277 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3278 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3279 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3284 static void terminate(void)
3286 if(hmlistname != NULL)
3291 if(xmllistname != NULL)
3293 unlink(xmllistname);
3296 if(xmlbz2listname != NULL)
3298 unlink(xmlbz2listname);
3299 free(xmlbz2listname);
3303 static struct configvar myvars[] =
3305 {CONF_VAR_STRING, "desc", {.str = L""}},
3306 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3307 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3308 {CONF_VAR_INT, "udpport", {.num = 0}},
3309 {CONF_VAR_INT, "tcpport", {.num = 0}},
3313 static struct module me =
3322 .terminate = terminate,