Make {hub,peer}cmds static as they should be.
[doldaconnect.git] / daemon / fnet-dc.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
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.
9 *
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.
14 *
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
18*/
19#include <stdlib.h>
20#include <stdio.h>
21#include <wchar.h>
22#include <malloc.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <string.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
28#include <alloca.h>
29#include <wctype.h>
30#include <time.h>
31#include <errno.h>
32#include <bzlib.h>
33#include <zlib.h>
34#include <sys/stat.h>
35
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39#include "filenet.h"
40#include "log.h"
41#include "module.h"
42#include "utils.h"
43#include "client.h"
44#include "transfer.h"
45#include "sysevents.h"
46#include "net.h"
9a2d6cb8 47#include "tiger.h"
d3372da9 48
49/*
50 * The Direct Connect protocol is extremely ugly. Thus, this code must
51 * also be a bit ugly in certain places. Please forgive me. =/
52 *
53 * This also means that there might be some pieces of the code that
54 * look completely illogical to you, and you think that you may make
55 * them much neater/better. However, in many cases it might just be
56 * that it's required to cope with some oddity of the protocol, such
57 * as the amazingly ugly delayed key response in the peer protocol.
58 */
59
60/* I assume this is the correct character set to use for DC,
61 * considering it was developed without i18n support under Windows */
62#define DCCHARSET "windows-1252"
63
64#ifdef DCPP_MASQUERADE
65/*
66 * I honestly don't want to pretend being a client that I'm not, but
67 * there are so many hubs that simply do not accept any clients
68 * outside their whitelists, for no obvious reasons, so I feel that I
69 * am left with little choice. Anyhow, as long as I actually support
70 * all the features that my faked DC++ version does, there should be
71 * very little harm done.
72 */
73#define DCIDTAG "++"
74#define DCIDTAGV "0.674"
75#define DCIDFULL "DC++ 0.674"
76#else
77#define DCIDTAG "Dolda"
78#define DCIDTAGV VERSION
79#define DCIDFULL "DoldaConnect " VERSION
80#endif
81
82#define PEER_CMD 0
9a2d6cb8 83#define PEER_STOP 1
84#define PEER_TRNS 2
85#define PEER_SYNC 3
86#define PEER_TTHL 4
d3372da9 87
88#define CPRS_NONE 0
89#define CPRS_ZLIB 1
90
91struct command
92{
93 char *name;
94 void (*handler)(struct socket *sk, void *data, char *cmd, char *args);
9a2d6cb8 95 int stop;
d3372da9 96};
97
98struct qcommand
99{
100 struct qcommand *next;
101 char *string;
102};
103
104struct dchub
105{
106 char *inbuf;
107 size_t inbufdata, inbufsize;
108 struct qcommand *queue;
1c4b02cd 109 int extended, dcppemu;
1d85f24b 110 char *charset;
d3372da9 111 char *nativename;
112 char *nativenick;
113};
114
115struct dcexppeer
116{
117 struct dcexppeer *next, *prev;
118 char *nick;
119 struct fnetnode *fn;
120 struct timer *expire;
121};
122
123struct dcpeer
124{
125 struct dcpeer *next, *prev;
126 struct socket *sk;
127 struct fnetnode *fn;
128 char *inbuf;
129 size_t inbufdata, inbufsize;
9a2d6cb8 130 size_t curread, totalsize;
d3372da9 131 int freeing;
1e9d82b1 132 struct timer *timeout;
d3372da9 133 struct qcommand *queue;
134 struct transfer *transfer;
135 int state;
136 int ptclose; /* Close after transfer is complete */
137 int accepted; /* If false, we connected, otherwise, we accepted */
1c4b02cd 138 int extended, dcppemu;
d3372da9 139 int direction; /* Using the constants from transfer.h */
140 int compress;
df873cd0 141 int hascurpos, fetchingtthl, notthl;
9a2d6cb8 142 struct tigertreehash tth;
1d85f24b 143 char *charset;
d3372da9 144 void *cprsdata;
d3372da9 145 char *key;
146 char *nativename;
147 char **supports;
148 wchar_t *wcsname;
149};
150
151static struct fnet dcnet;
152static struct transferiface dctransfer;
153static struct socket *udpsock = NULL;
154static struct socket *tcpsock = NULL;
155static struct dcpeer *peers = NULL;
156int numdcpeers = 0;
157static struct dcexppeer *expected = NULL;
158static char *hmlistname = NULL;
159static char *xmllistname = NULL;
160static char *xmlbz2listname = NULL;
161
162static void peerconnect(struct socket *sk, int err, struct fnetnode *fn);
163static void freedcpeer(struct dcpeer *peer);
164static void transread(struct socket *sk, struct dcpeer *peer);
165static void transerr(struct socket *sk, int err, struct dcpeer *peer);
166static void transwrite(struct socket *sk, struct dcpeer *peer);
167static void updatehmlist(void);
168static void updatexmllist(void);
169static void updatexmlbz2list(void);
9a2d6cb8 170static void requestfile(struct dcpeer *peer);
d3372da9 171
172static int reservedchar(unsigned char c)
173{
174 return((c == 0) || (c == 5) || (c == 124) || (c == 96) || (c == 126) || (c == 36));
175}
176
177/* Oh, how I despise having to do this... */
178static char *dcmakekey(char *lock)
179{
180 int i, len, offset;
181 char *buf, *key;
182 char save;
183
184 buf = smalloc(strlen(lock));
185 save = 5;
186 len = 0;
187 for(i = 0; lock[i]; i++)
188 {
189 buf[i] = lock[i] ^ save;
190 buf[i] = ((buf[i] & 0x0F) << 4) | ((buf[i] & 0xF0) >> 4);
191 save = lock[i];
192 if((i != 0) && reservedchar(buf[i]))
193 len += 10;
194 else
195 len++;
196 }
197 buf[0] ^= buf[i - 1];
198 if(reservedchar(buf[0]))
199 len += 10;
200 else
201 len++;
202 key = smalloc(len + 1);
203 offset = 0;
204 for(i = 0; lock[i] != 0; i++)
205 {
206 if(reservedchar(buf[i]))
207 offset += sprintf(key + offset, "/%%DCN%03i%%/", buf[i]);
208 else
209 key[offset++] = buf[i];
210 }
211 key[offset] = 0;
212 free(buf);
213 return(key);
214}
215
9a2d6cb8 216static char *pathnmdc2adc(char *path)
217{
218 char *ret;
219 size_t retsize, retdata;
220
221 if(!strcmp(path, "files.xml") || !strcmp(path, "files.xml.bz2") || !strcmp(path, "MyList.DcLst"))
222 return(sstrdup(path));
223 ret = NULL;
224 retsize = retdata = 0;
225 addtobuf(ret, '/');
226 for(; *path; path++)
227 {
228 if(*path == '\\')
229 addtobuf(ret, '/');
230 else
231 addtobuf(ret, *path);
232 }
233 addtobuf(ret, 0);
234 return(ret);
235}
236
237static int isdchash(struct hash *hash)
238{
239 if(wcscmp(hash->algo, L"TTH"))
240 return(0);
241 if(hash->len != 24)
242 return(0);
243 return(1);
244}
245
246static int supports(struct dcpeer *peer, char *cap)
247{
248 char **p;
249
250 if(peer->supports == NULL)
251 return(0);
252 for(p = peer->supports; *p != NULL; p++)
253 {
254 if(!strcasecmp(*p, cap))
255 return(1);
256 }
257 return(0);
258}
259
d3372da9 260static void endcompress(struct dcpeer *peer)
261{
262 if(peer->compress == CPRS_ZLIB)
263 {
264 deflateEnd(peer->cprsdata);
265 free(peer->cprsdata);
266 }
267 peer->compress = CPRS_NONE;
268}
269
270static void initcompress(struct dcpeer *peer, int algo)
271{
272 int ret;
273
274 endcompress(peer);
275 peer->compress = algo;
276 if(algo == CPRS_ZLIB)
277 {
278 peer->cprsdata = smalloc(sizeof(z_stream));
279 memset(peer->cprsdata, 0, sizeof(z_stream));
280 if((ret = deflateInit(peer->cprsdata, 3)) != Z_OK)
281 {
282 flog(LOG_CRIT, "Aiya! zlib refuses to init (%i)!", ret);
283 abort();
284 }
285 }
286}
287
288static void unquote(wchar_t *in)
289{
290 wchar_t *p, *p2, nc;
291
292 for(p = in; *p != L'\0'; p++)
293 {
294 if(*p == L'&')
295 {
296 for(p2 = p + 1; (*p2 != L'\0') && (*p2 != L';') && (*p2 != L'&'); p2++);
297 if(*p2 == L'&')
298 continue;
299 if(*p2 == L'\0')
300 return;
301 *p2 = L'\0';
302 nc = L'\0';
303 if(!wcscmp(p + 1, L"amp"))
304 {
305 nc = L'&';
306 } else if(p[1] == L'#') {
307 nc = ucptowc(wcstol(p + 2, NULL, 10));
308 }
309 if(nc == L'\0')
310 {
311 *p2 = L';';
312 p = p2;
313 continue;
314 }
315 *p = nc;
316 memmove(p + 1, p2 + 1, (wcslen(p2 + 1) + 1) * sizeof(wchar_t));
317 }
318 }
319}
320
321static void freeexppeer(struct dcexppeer *ep)
322{
323 if(ep->next != NULL)
324 ep->next->prev = ep->prev;
325 if(ep->prev != NULL)
326 ep->prev->next = ep->next;
327 if(ep == expected)
328 expected = ep->next;
329 free(ep->nick);
330 putfnetnode(ep->fn);
331 if(ep->expire != NULL)
332 canceltimer(ep->expire);
333 free(ep);
334}
335
336static void exppeerexpire(int cancelled, struct dcexppeer *ep)
337{
338 ep->expire = NULL;
339 if(!cancelled)
340 freeexppeer(ep);
341}
342
343static struct dcexppeer *expectpeer(char *nick, struct fnetnode *fn)
344{
345 struct dcexppeer *ep;
346
347 ep = smalloc(sizeof(*ep));
348 ep->nick = sstrdup(nick);
349 getfnetnode(ep->fn = fn);
350 ep->expire = timercallback(ntime() + 300, (void (*)(int, void *))exppeerexpire, ep);
351 ep->next = expected;
352 ep->prev = NULL;
353 if(expected != NULL)
354 expected->prev = ep;
355 expected = ep;
356 return(ep);
357}
358
359static struct qcommand *newqcmd(struct qcommand **queue, char *string)
360{
361 struct qcommand *new;
362
363 while(*queue != NULL)
364 queue = &(*queue)->next;
365 new = smalloc(sizeof(*new));
366 new->string = sstrdup(string);
367 new->next = *queue;
368 *queue = new;
369 return(new);
370}
371
372static struct qcommand *ulqcmd(struct qcommand **queue)
373{
374 struct qcommand *qcmd;
375
376 if((qcmd = *queue) == NULL)
377 return(NULL);
378 *queue = qcmd->next;
379 return(qcmd);
380}
381
382static void freeqcmd(struct qcommand *qcmd)
383{
384 free(qcmd->string);
385 free(qcmd);
386}
387
388static void hubrecvchat(struct socket *sk, struct fnetnode *fn, char *from, char *string)
389{
390 wchar_t *chat, *wfrom, *wpeer;
391 char *p, *end;
392 struct fnetpeer *peer;
1d85f24b 393 struct dchub *hub;
d3372da9 394
1d85f24b 395 hub = fn->data;
d3372da9 396 end = string + strlen(string);
397 while((p = strchr(string, 13)) != NULL)
398 memmove(p, p + 1, (end-- - p));
399 if(from != NULL)
400 {
401 if((strlen(string) > strlen(from) + 2) && (*string == '<') && !memcmp(string + 1, from, strlen(from)) && (*(string + strlen(from) + 1) == '>'))
402 string += strlen(from) + 2;
1d85f24b 403 if((wfrom = icmbstowcs(from, hub->charset)) == NULL)
d3372da9 404 return;
405 wpeer = swcsdup(wfrom);
406 } else {
407 wfrom = NULL;
408 wpeer = NULL;
409 if(*string == '<')
410 {
411 for(p = string + 1; *p; p++)
412 {
413 if((*p == ' ') || (*p == '>'))
414 break;
415 }
416 if(*p == '>')
417 {
418 *(p++) = 0;
419 if(*p == ' ')
420 p++;
1d85f24b 421 if((wpeer = icmbstowcs(string + 1, hub->charset)) == NULL)
d3372da9 422 return;
423 string = p;
424 }
425 }
426 if(wpeer == NULL)
427 wpeer = swcsdup(L"");
428 }
1d85f24b 429 if((chat = icmbstowcs(string, hub->charset)) == NULL)
d3372da9 430 {
431 if(wfrom != NULL)
432 free(wfrom);
433 free(wpeer);
434 return;
435 }
436 unquote(chat);
437 if(wfrom != NULL)
438 {
439 if((peer = fnetfindpeer(fn, wfrom)) == NULL) /* Assume public chat */
440 fnethandlechat(fn, 1, wfrom, wpeer, chat);
441 else
442 fnethandlechat(fn, 0, wfrom, wpeer, chat);
443 } else {
444 fnethandlechat(fn, 1, L"", wpeer, chat);
445 }
446 if(wfrom != NULL)
447 free(wfrom);
448 free(wpeer);
449 free(chat);
450}
451
1e9d82b1 452static void peertimeout(int cancelled, struct dcpeer *peer)
453{
454 peer->timeout = NULL;
455 if(cancelled)
456 return;
457 freedcpeer(peer);
458}
459
d3372da9 460static void sendadc(struct socket *sk, char *arg)
461{
462 char *buf;
463 size_t bufsize, bufdata;
464
465 buf = NULL;
466 bufsize = bufdata = 0;
467 addtobuf(buf, ' ');
468 for(; *arg; arg++)
469 {
470 if(*arg == ' ')
471 {
472 bufcat(buf, "\\s", 2);
473 } else if(*arg == '\n') {
474 bufcat(buf, "\\n", 2);
475 } else if(*arg == '\\') {
476 bufcat(buf, "\\\\", 2);
477 } else {
478 addtobuf(buf, *arg);
479 }
480 }
481 sockqueue(sk, buf, bufdata);
482 free(buf);
483}
484
485static void sendadcf(struct socket *sk, char *arg, ...)
486{
487 char *buf;
488 va_list args;
489
490 va_start(args, arg);
491 buf = vsprintf2(arg, args);
492 va_end(args);
493 if(buf == NULL)
494 return;
495 sendadc(sk, buf);
496 free(buf);
497}
498
499static char **parseadc(char *args)
500{
501 char **retbuf;
502 size_t retbufsize, retbufdata;
503 char *buf;
504 size_t bufsize, bufdata;
505 int state;
506
507 retbuf = NULL;
508 buf = NULL;
509 retbufsize = retbufdata = bufsize = bufdata = 0;
510 state = 0;
511 while(state != 3)
512 {
513 switch(state)
514 {
515 case 0:
516 if(*args == 0)
517 state = 3;
518 else if(*args != ' ')
519 state = 1;
520 break;
521 case 1:
522 if((*args == ' ') || (*args == 0))
523 {
524 addtobuf(buf, 0);
525 addtobuf(retbuf, buf);
526 buf = NULL;
527 bufsize = bufdata = 0;
528 if(*args == 0)
529 state = 3;
530 } else if(*args == '\\') {
531 state = 2;
532 } else {
533 addtobuf(buf, *args);
534 }
535 args++;
536 break;
537 case 2:
538 if(*args == 0)
539 {
540 if(buf != NULL)
541 free(buf);
542 addtobuf(retbuf, NULL);
543 freeparr(retbuf);
544 return(NULL);
f24d5cfb 545 } else if((*args == 's') || (*args == ' ')) {
d3372da9 546 addtobuf(buf, ' ');
547 } else if(*args == 'n') {
548 addtobuf(buf, '\n');
549 } else if(*args == '\\') {
550 addtobuf(buf, '\\');
551 }
a478552c 552 args++;
d3372da9 553 state = 1;
a478552c 554 break;
d3372da9 555 }
556 }
557 if(buf != NULL)
558 free(buf);
559 addtobuf(retbuf, NULL);
560 return(retbuf);
561}
562
563/* Macros useful in command handlers */
564#define skipspace(s) ({if(((s) = strchr((s), ' ')) == NULL) return; else (s)++;})
565#define qstr(sk, str) sockqueue(sk, str, strlen(str))
566#define qstrf(sk, strandargs...) \
567do { \
568 char *__buf__; \
569 if((__buf__ = sprintf2(strandargs)) != NULL) { \
570 sockqueue(sk, __buf__, strlen(__buf__)); \
571 free(__buf__); \
572 } \
573} while(0)
574
575static char *tr(char *str, char *trans)
576{
577 char *p;
578
579 for(; *trans; trans += 2)
580 {
581 for(p = strchr(str, trans[0]); p != NULL; p = strchr(p, trans[0]))
582 *p = trans[1];
583 }
584 return(str);
585}
586
9a2d6cb8 587static char *getadcid(struct dcpeer *peer)
588{
589 char *buf;
590 char *ret;
d57f938a 591 int isfilelist;
9a2d6cb8 592
2a157d1a 593 isfilelist = 0;
d57f938a 594 if(!wcscmp(peer->transfer->path, L"files.xml") || !wcscmp(peer->transfer->path, L"files.xml.bz2") || !wcscmp(peer->transfer->path, L"MyList.DcLst"))
595 isfilelist = 1;
596 if(!isfilelist && (peer->transfer->hash != NULL) && isdchash(peer->transfer->hash) && supports(peer, "tthf"))
9a2d6cb8 597 {
598 buf = base32encode(peer->transfer->hash->buf, 24);
599 ret = sprintf2("TTH/%.39s", buf);
600 free(buf);
601 } else {
602 if((buf = icwcstombs(peer->transfer->path, "UTF-8")) == NULL)
603 return(NULL);
604 ret = pathnmdc2adc(buf);
605 free(buf);
606 }
607 return(ret);
608}
609
610
d3372da9 611static int trresumecb(struct transfer *transfer, wchar_t *cmd, wchar_t *arg, struct dcpeer *peer)
612{
613 if(!wcscmp(cmd, L"resume"))
614 {
615 if(arg == NULL)
616 {
617 flog(LOG_WARNING, "filter returned no position for \"resume\" on transfer %i", transfer->id);
618 freedcpeer(peer);
619 } else {
620 transfer->curpos = wcstol(arg, NULL, 10);
9a2d6cb8 621 peer->hascurpos = 1;
622 requestfile(peer);
d3372da9 623 }
d3372da9 624 return(1);
625 }
626 return(0);
627}
628
9a2d6cb8 629static void sendmynick(struct dcpeer *peer)
d3372da9 630{
1d85f24b 631 struct fnetnode *fn;
d3372da9 632
1d85f24b 633 fn = peer->fn;
634 if(fn == NULL)
635 qstrf(peer->sk, "$MyNick %s|", icswcstombs(confgetstr("cli", "defnick"), peer->charset, "DoldaConnectUser-IN"));
9a2d6cb8 636 else
1d85f24b 637 qstrf(peer->sk, "$MyNick %s|", icswcstombs(fn->mynick, peer->charset, "DoldaConnectUser-IN"));
9a2d6cb8 638}
639
640static void sendpeerlock(struct dcpeer *peer)
641{
1c4b02cd 642 if(peer->dcppemu)
643 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.674ABCABC|");
644 else
645 qstrf(peer->sk, "$Lock EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DOLDA%sABCABCABC|", VERSION);
9a2d6cb8 646}
647
648static void sendsupports(struct dcpeer *peer)
649{
1c4b02cd 650 if(peer->dcppemu)
651 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG |");
652 else
653 qstr(peer->sk, "$Supports MiniSlots XmlBZList ADCGet TTHL TTHF GetZBlock ZLIG|");
9a2d6cb8 654}
655
656static void requestfile(struct dcpeer *peer)
657{
658 char *buf;
659
660 if(peer->transfer->size == -1)
d3372da9 661 {
1d85f24b 662 /* Use DCCHARSET for $Get paths until further researched... */
9a2d6cb8 663 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
d3372da9 664 {
9a2d6cb8 665 transferseterror(peer->transfer, TRNSE_NOTFOUND);
666 freedcpeer(peer);
d3372da9 667 return;
668 }
9a2d6cb8 669 /* The transfer will be restarted later from
670 * cmd_filelength when it detects that the sizes
671 * don't match. */
672 qstrf(peer->sk, "$Get %s$1|", buf);
673 return;
d3372da9 674 }
9a2d6cb8 675 if((peer->transfer->hash == NULL) && !peer->notthl)
d3372da9 676 {
9a2d6cb8 677 if(supports(peer, "adcget") && supports(peer, "tthl"))
d3372da9 678 {
9a2d6cb8 679 qstr(peer->sk, "$ADCGET");
680 sendadc(peer->sk, "tthl");
681 if((buf = getadcid(peer)) == NULL)
d3372da9 682 {
9a2d6cb8 683 transferseterror(peer->transfer, TRNSE_NOTFOUND);
d3372da9 684 freedcpeer(peer);
685 return;
686 }
9a2d6cb8 687 sendadc(peer->sk, buf);
688 free(buf);
689 sendadc(peer->sk, "0");
690 sendadc(peer->sk, "-1");
691 qstr(peer->sk, "|");
df873cd0 692 peer->fetchingtthl = 1;
9a2d6cb8 693 return;
694 }
695 }
696 if(!peer->hascurpos)
697 {
698 if(forkfilter(peer->transfer))
699 {
700 flog(LOG_WARNING, "could not fork filter for transfer %i: %s", peer->transfer->id, strerror(errno));
701 freedcpeer(peer);
702 return;
d3372da9 703 }
9a2d6cb8 704 CBREG(peer->transfer, trans_filterout, (int (*)(struct transfer *, wchar_t *, wchar_t *, void *))trresumecb, NULL, peer);
705 return;
706 }
707 if(supports(peer, "adcget"))
708 {
709 qstr(peer->sk, "$ADCGET");
710 sendadc(peer->sk, "file");
711 if((buf = getadcid(peer)) == NULL)
712 {
713 transferseterror(peer->transfer, TRNSE_NOTFOUND);
714 freedcpeer(peer);
715 return;
716 }
717 sendadc(peer->sk, buf);
718 free(buf);
719 sendadcf(peer->sk, "%i", peer->transfer->curpos);
720 sendadcf(peer->sk, "%i", peer->transfer->size - peer->transfer->curpos);
721 qstr(peer->sk, "|");
722 } else if(supports(peer, "xmlbzlist")) {
723 if((buf = icswcstombs(peer->transfer->path, "UTF-8", NULL)) == NULL)
724 {
725 transferseterror(peer->transfer, TRNSE_NOTFOUND);
726 freedcpeer(peer);
727 return;
728 }
729 qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf);
d3372da9 730 } else {
1d85f24b 731 /* Use DCCHARSET for $Get paths until further researched... */
9a2d6cb8 732 if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL)
d3372da9 733 {
9a2d6cb8 734 transferseterror(peer->transfer, TRNSE_NOTFOUND);
735 freedcpeer(peer);
736 return;
d3372da9 737 }
9a2d6cb8 738 qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1);
d3372da9 739 }
740}
741
742static void sendmyinfo(struct socket *sk, struct fnetnode *fn)
743{
744 struct dchub *hub;
745 char *buf;
746 struct fnetnode *cfn;
69757b8c 747 int hn1, hn2, hn3;
d3372da9 748
749 hub = fn->data;
750 qstrf(sk, "$MyINFO $ALL %s ", hub->nativenick);
1d85f24b 751 buf = tr(icswcstombs(confgetstr("dc", "desc"), hub->charset, "Charset_conv_failure"), "$_|_");
d3372da9 752 qstrf(sk, "%s", buf);
69757b8c 753 hn1 = hn2 = hn3 = 0;
d3372da9 754 for(cfn = fnetnodes; cfn != NULL; cfn = cfn->next)
755 {
756 if((cfn->state == FNN_EST) || (cfn->state == FNN_HS))
69757b8c 757 {
758 if(cfn->regstatus == FNNS_OP)
759 hn3++;
760 else if(cfn->regstatus == FNNS_REG)
761 hn2++;
762 else
763 hn1++;
764 }
d3372da9 765 }
69757b8c 766 qstrf(sk, "<%s V:%s,M:%c,H:%i/%i/%i,S:%i>",
1c4b02cd 767 (hub->dcppemu)?"++":"Dolda",
768 (hub->dcppemu)?"0.674":VERSION,
d3372da9 769 (tcpsock == NULL)?'P':'A',
69757b8c 770 hn1, hn2, hn3,
d3372da9 771 confgetint("transfer", "slots")
772 );
773 qstrf(sk, "$ $");
1d85f24b 774 buf = tr(icswcstombs(confgetstr("dc", "speedstring"), hub->charset, "Charset_conv_failure"), "$_|_");
d3372da9 775 qstrf(sk, "%s\x01$", buf);
1d85f24b 776 buf = tr(icswcstombs(confgetstr("dc", "email"), hub->charset, "Charset_conv_failure"), "$_|_");
d3372da9 777 qstrf(sk, "%s$", buf);
778 qstrf(sk, "%llu$|", sharesize);
779}
780
781static void hubhandleaction(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
782{
783 struct dchub *hub;
784
785 hub = fn->data;
786 if(!strcmp(cmd, "$Lock"))
787 {
788 qstrf(sk, "$ValidateNick %s|", hub->nativenick);
789 } else if(!strcmp(cmd, "$Hello")) {
790 if(fn->state == FNN_HS)
791 {
792 qstrf(sk, "$Version 1,0091|");
793 qstrf(sk, "$GetNickList|");
794 sendmyinfo(sk, fn);
795 fnetsetstate(fn, FNN_EST);
796 } else {
797 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
798 }
799 }
800}
801
802static void cmd_lock(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
803{
804 struct dchub *hub;
805 char *key;
806 char *p;
807
808 hub = fn->data;
809 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
810 hub->extended = 1;
811 if((p = strchr(args, ' ')) != NULL)
812 *(p++) = 0;
813 if(hub->extended)
814 {
1c4b02cd 815 if(hub->dcppemu)
816 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock |");
817 else
818 qstrf(sk, "$Supports UserCommand NoGetINFO NoHello UserIP2 TTHSearch GetZBlock|");
d3372da9 819 }
820 key = dcmakekey(args);
821 qstrf(sk, "$Key %s|", key);
822 free(key);
823 hubhandleaction(sk, fn, cmd, args);
824}
825
826static void cmd_hubname(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
827{
828 wchar_t *buf;
829 struct dchub *hub;
830
831 hub = fn->data;
832 if(hub->nativename == NULL)
833 free(hub->nativename);
834 hub->nativename = sstrdup(args);
1d85f24b 835 buf = icmbstowcs(args, hub->charset);
d3372da9 836 fnetsetname(fn, (buf == NULL)?L"Hubname conv error":buf);
837 if(buf != NULL)
838 free(buf);
839 hubhandleaction(sk, fn, cmd, args);
840}
841
842static void cmd_hello(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
843{
844 wchar_t *nick;
845 struct dchub *hub;
846
847 hub = fn->data;
1d85f24b 848 if((nick = icmbstowcs(args, hub->charset)) == NULL)
d3372da9 849 return;
850 if(strcmp(args, hub->nativenick) && (fnetfindpeer(fn, nick) == NULL))
851 fnetaddpeer(fn, nick, nick);
852 free(nick);
853 hubhandleaction(sk, fn, cmd, args);
854}
855
856static void cmd_quit(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
857{
858 wchar_t *nick;
859 struct fnetpeer *peer;
860 struct dchub *hub;
861
862 hub = fn->data;
1d85f24b 863 if((nick = icmbstowcs(args, hub->charset)) == NULL)
d3372da9 864 return;
865 if((peer = fnetfindpeer(fn, nick)) != NULL)
866 fnetdelpeer(peer);
867 free(nick);
868 hubhandleaction(sk, fn, cmd, args);
869}
870
871static void cmd_nicklist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
872{
873 struct dchub *hub;
874 char *p;
875 wchar_t *buf;
876 struct fnetpeer *peer, *npeer;
877
878 hub = fn->data;
879 for(peer = fn->peers; peer != NULL; peer = peer->next)
880 peer->flags.b.delete = 1;
881 while((p = strstr(args, "$$")) != NULL)
882 {
883 *p = 0;
1d85f24b 884 if((buf = icmbstowcs(args, hub->charset)) != NULL)
d3372da9 885 {
886 if((peer = fnetfindpeer(fn, buf)) == NULL)
887 peer = fnetaddpeer(fn, buf, buf);
888 else
889 peer->flags.b.delete = 0;
890 free(buf);
891 qstrf(sk, "$GetINFO %s %s|", args, hub->nativenick);
892 }
893 args = p + 2;
894 }
895 for(peer = fn->peers; peer != NULL; peer = npeer)
896 {
897 npeer = peer->next;
898 if(peer->flags.b.delete)
899 fnetdelpeer(peer);
900 }
901 hubhandleaction(sk, fn, cmd, args);
902}
903
904static void cmd_oplist(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
905{
906 struct dchub *hub;
907 char *p;
908 wchar_t *buf;
909 struct fnetpeer *peer;
910
911 hub = fn->data;
912 for(peer = fn->peers; peer != NULL; peer = peer->next)
913 peer->flags.b.op = 0;
914 while((p = strstr(args, "$$")) != NULL)
915 {
916 *p = 0;
1d85f24b 917 if((buf = icmbstowcs(args, hub->charset)) != NULL)
d3372da9 918 {
919 if((peer = fnetfindpeer(fn, buf)) != NULL)
920 peer->flags.b.op = 1;
921 free(buf);
922 }
923 args = p + 2;
924 }
925 hubhandleaction(sk, fn, cmd, args);
926}
927
928static void cmd_myinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
929{
930 char *p, *p2;
931 wchar_t *buf, *wp, *wp2;
932 wchar_t abuf[10];
933 struct fnetpeer *peer;
934 struct dchub *hub;
935
936 hub = fn->data;
937 p = args;
938 if(strncmp(p, "$ALL ", 5))
939 return;
940 p += 5;
941 if((p2 = strchr(p, ' ')) == NULL)
942 return;
943 *p2 = 0;
1d85f24b 944 if((buf = icmbstowcs(p, hub->charset)) == NULL)
d3372da9 945 return;
946 if((peer = fnetfindpeer(fn, buf)) == NULL)
947 peer = fnetaddpeer(fn, buf, buf);
948 free(buf);
949 p = p2 + 1;
950 if((p2 = strstr(p, "$ $")) == NULL)
951 return;
952 *p2 = 0;
1d85f24b 953 if((buf = icmbstowcs(p, hub->charset)) == NULL)
d3372da9 954 return;
316e9a13 955 if((wcslen(buf) > 0) && (buf[wcslen(buf) - 1] == L'>') && ((wp = wcsrchr(buf, L'<')) != NULL))
d3372da9 956 {
957 buf[wcslen(buf) - 1] = L'\0';
958 *(wp++) = L'\0';
959 if((wp2 = wcschr(wp, L' ')) != NULL)
960 {
961 *(wp2++) = L'\0';
962 fnetpeersetstr(peer, L"dc-client", wp);
963 wp = wp2;
964 do
965 {
966 if((wp2 = wcschr(wp, L',')) != NULL)
967 *(wp2++) = L'\0';
968 if(wp[1] != L':')
969 continue;
970 swprintf(abuf, 10, L"dc-tag-%lc", wp[0]);
971 fnetpeersetstr(peer, abuf, wp + 2);
972 wp = wp2;
973 } while(wp2 != NULL);
974 }
975 }
976 fnetpeersetstr(peer, L"descr", buf);
977 free(buf);
978 p = p2 + 3;
979 if((p2 = strchr(p, '$')) == NULL)
980 return;
981 *(p2 - 1) = 0;
1d85f24b 982 if((buf = icmbstowcs(p, hub->charset)) == NULL)
d3372da9 983 return;
984 fnetpeersetstr(peer, L"dc-speed", buf);
985 free(buf);
986 p = p2 + 1;
987 if((p2 = strchr(p, '$')) == NULL)
988 return;
989 *p2 = 0;
1d85f24b 990 if((buf = icmbstowcs(p, hub->charset)) == NULL)
d3372da9 991 return;
992 fnetpeersetstr(peer, L"email", buf);
993 free(buf);
994 p = p2 + 1;
995 if(strlen(p) < 1)
996 return;
997 fnetpeersetlnum(peer, L"share", strtoll(p, NULL, 10));
998 hubhandleaction(sk, fn, cmd, args);
999}
1000
1001/* I do not implement the fully in that I do not disconnect from the
1002 * old hub. I do this since I believe that if the hub owner really
1003 * wants to get rid of us, then it is his responsibility to disconnect
1004 * us. */
1005static void cmd_forcemove(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1006{
1007 struct dchub *hub;
1008 struct fnetnode *newfn;
1009 int freeargs;
1010
1011 hub = fn->data;
1012 if(strchr(args, ':') == NULL)
1013 {
1014 args = strcpy(smalloc(strlen(args) + 5), args);
1015 strcat(args, ":411");
1016 freeargs = 1;
1017 } else {
1018 freeargs = 0;
1019 }
efc05613 1020 if((newfn = fnetinitconnect(L"dc", args, NULL)) != NULL)
d3372da9 1021 {
1022 linkfnetnode(newfn);
1023 putfnetnode(newfn);
1024 }
1025 hubhandleaction(sk, fn, cmd, args);
1026 if(freeargs)
1027 free(args);
1028}
1029
1d85f24b 1030static char *getdcpath(struct sharecache *node, size_t *retlen, char *charset)
d3372da9 1031{
1032 char *buf, *buf2;
1033 size_t len, len2;
1034
1035 if(node->parent == NULL)
1036 return(NULL);
1037 if(node->parent == shareroot)
1038 {
1d85f24b 1039 if((buf = icwcstombs(node->name, charset)) == NULL)
d3372da9 1040 return(NULL);
1041 if(retlen != NULL)
1042 *retlen = strlen(buf);
1043 return(buf);
1044 } else {
1d85f24b 1045 if((buf2 = icwcstombs(node->name, charset)) == NULL)
d3372da9 1046 return(NULL);
1d85f24b 1047 if((buf = getdcpath(node->parent, &len, charset)) == NULL)
d3372da9 1048 {
1049 free(buf2);
1050 return(NULL);
1051 }
1052 len2 = strlen(buf2);
1053 buf = srealloc(buf, len + 1 + len2 + 1);
1054 buf[len++] = '\\';
1055 strcpy(buf + len, buf2);
1056 buf[len + len2] = 0;
1057 free(buf2);
1058 if(retlen != NULL)
1059 *retlen = len + len2;
1060 return(buf);
1061 }
1062}
1063
1064/*
1065 * This is the main share searching function for Direct Connect
1066 * peers. Feel free to optimize it if you feel the need for it. I
1067 * haven't ever seen it take any noticable CPU time anyway, so I
1068 * haven't felt that need.
1069 *
1070 * It may feel dubious to just return the directory when all terms are
1071 * satisfied rather than all the files in it, but it really benefits
1072 * everyone: Less cycles used for us, less UDP squelching for active
1073 * searchers, and less bandwidth waste for hubs when serving passive
1074 * searchers.
1075 */
1076static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1077{
1078 int i, done;
1079 struct dchub *hub;
1080 char *p, *p2;
1081 char *prefix, *infix, *postfix, *buf, *buf2;
1082 struct socket *dsk;
1083 struct sockaddr_in addr;
1084 struct sharecache *node;
1085 int minsize, maxsize;
c7185019 1086 int dotth;
1087 size_t buflen;
d3372da9 1088 int termnum, satisfied, skipcheck;
1089 int level, tersat[32];
1090 wchar_t *terms[32];
1091 char hashtth[24];
1092
1093 hub = fn->data;
1094 if((p = strchr(args, ' ')) == NULL)
1095 return;
1096 *(p++) = 0;
1097
1098 memset(terms, 0, sizeof(terms));
1099 prefix = infix = postfix = NULL;
1100 dsk = NULL;
1101 dotth = 0;
1102
1103 if(!strncmp(args, "Hub:", 4))
1104 {
1105 if(!strcmp(cmd, "$MultiSearch"))
1106 goto out;
1107 if(!strcmp(args + 4, hub->nativenick))
1108 goto out;
1109 prefix = sprintf2("$SR %s ", hub->nativenick);
1110 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1111 postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4);
1112 dsk = sk;
1113 getsock(dsk);
1114 } else {
1115 if((p2 = strchr(args, ':')) == NULL)
1116 goto out;
1117 *(p2++) = 0;
1118 addr.sin_family = AF_INET;
1119 if(!inet_aton(args, &addr.sin_addr))
1120 goto out;
1121 addr.sin_port = htons(atoi(p2));
1122 prefix = sprintf2("$SR %s ", hub->nativenick);
1123 infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots"));
1124 postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen));
1125 netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr));
1126 }
1127
1128 minsize = maxsize = -1;
1129 if(*p == 0)
1130 goto out;
1131 if(*(p++) == 'T')
1132 minsize = 0;
1133 if(*(p++) != '?')
1134 goto out;
1135 if(*p == 0)
1136 goto out;
9d8cf743 1137 if((*(p++) == 'T') && (minsize == 0))
d3372da9 1138 {
1139 maxsize = 0;
1140 minsize = -1;
1141 }
1142 if(*(p++) != '?')
1143 goto out;
1144 if((p2 = strchr(p, '?')) == NULL)
1145 goto out;
1146 *(p2++) = 0;
1147 if(minsize == 0)
1148 minsize = atoi(p);
1149 if(maxsize == 0)
1150 maxsize = atoi(p);
1151 p = p2 + 1;
1152 if(*(p++) != '?')
1153 goto out;
1154 termnum = 0;
1155 p2 = p;
1156 done = 0;
1157 while(!done)
1158 {
1159 if((*p2 == 0) || (*p2 == '$'))
1160 {
1161 if(*p2 == 0)
1162 done = 1;
1163 else
1164 *p2 = 0;
1165 if(*p)
1166 {
1167 if(!dotth && !strncmp(p, "TTH:", 4))
1168 {
1169 dotth = 1;
2dd01e6e 1170 if(((buf = base32decode(p + 4, &buflen)) == NULL) || (buflen != 24))
1171 {
1172 free(buf);
d3372da9 1173 goto out;
2dd01e6e 1174 }
d3372da9 1175 memcpy(hashtth, buf, 24);
1176 free(buf);
1177 } else {
1d85f24b 1178 if((terms[termnum] = icmbstowcs(p, hub->charset)) != NULL)
d3372da9 1179 termnum++;
1180 }
1181 }
1182 p = p2 + 1;
1183 if(termnum == 32)
1184 break;
1185 }
1186 p2++;
1187 }
1188
1189 node = shareroot->child;
1190 level = 0;
1191 for(i = 0; i < termnum; i++)
1192 tersat[i] = -1;
1193 satisfied = 0;
1194 while(1)
1195 {
1196 skipcheck = 0;
1197 if(node->f.b.type == FILE_REG)
1198 {
1199 if((minsize >= 0) && (node->size < minsize))
1200 skipcheck = 1;
1201 if((maxsize >= 0) && (node->size > maxsize))
1202 skipcheck = 1;
1203 }
1204 if(!skipcheck && dotth)
1205 {
1206 if((node->f.b.type != FILE_REG) || (node->f.b.hastth && memcmp(hashtth, node->hashtth, 24)))
1207 skipcheck = 1;
1208 }
1209 if(!skipcheck)
1210 {
1211 for(i = 0; i < termnum; i++)
1212 {
1213 if(tersat[i] >= 0)
1214 continue;
1215 if(wcsexists(node->name, terms[i]))
1216 {
1217 tersat[i] = level;
1218 satisfied++;
1219 } else if(node->child == NULL) {
1220 break;
1221 }
1222 }
1223 }
1224 if(!skipcheck && (satisfied == termnum))
1225 {
1d85f24b 1226 /* Use DCCHARSET in $Get paths until further researched... */
1227 if((buf = getdcpath(node, NULL, DCCHARSET)) != NULL)
d3372da9 1228 {
1229 if(node->f.b.hastth)
1230 {
1231 buf2 = base32encode(node->hashtth, 24);
1232 qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix);
1233 free(buf2);
1234 } else {
1235 qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix);
1236 }
1237 free(buf);
1238 }
1239 }
1240 if((!skipcheck && (satisfied == termnum)) || (node->child == NULL))
1241 {
1242 while(node->next == NULL)
1243 {
1244 if((node = node->parent) == shareroot)
1245 break;
1246 level--;
1247 }
1248 if(node == shareroot)
1249 break;
1250 for(i = 0; i < termnum; i++)
1251 {
1252 if(tersat[i] >= level)
1253 {
1254 tersat[i] = -1;
1255 satisfied--;
1256 }
1257 }
1258 node = node->next;
1259 } else {
1260 node = node->child;
1261 level++;
1262 }
1263 }
1264
1265 hubhandleaction(sk, fn, cmd, args);
1266
1267 out:
1268 if(dsk != NULL)
1269 putsock(dsk);
1270 if(prefix != NULL)
1271 free(prefix);
1272 if(infix != NULL)
1273 free(infix);
1274 if(postfix != NULL)
1275 free(postfix);
1276 for(i = 0; (i < 32) && (terms[i] != NULL); i++)
1277 free(terms[i]);
1278}
1279
1280static void cmd_connecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1281{
1282 char *p;
1283 struct dchub *hub;
1284 struct socket *newsk;
1285 struct sockaddr_in addr;
1286
1287 hub = fn->data;
1288 if((p = strchr(args, ' ')) == NULL)
1289 return;
1290 *(p++) = 0;
1291 if(strcmp(args, hub->nativenick))
1292 return;
1293 addr.sin_family = AF_INET;
1294 args = p;
1295 if((p = strchr(args, ':')) == NULL)
1296 return;
1297 *(p++) = 0;
1298 addr.sin_port = htons(atoi(p));
1299 if(!inet_aton(args, &addr.sin_addr))
1300 return;
1301 newsk = netcsconn((struct sockaddr *)&addr, sizeof(addr), (void (*)(struct socket *, int, void *))peerconnect, fn);
1302 getfnetnode(fn);
1303 hubhandleaction(sk, fn, cmd, args);
1304}
1305
1306static void sendctm(struct socket *sk, char *nick)
1307{
1308 struct sockaddr *addr;
1309 socklen_t addrlen;
1310
1311 if(tcpsock == NULL)
1312 return;
c8a90770 1313 if(sockgetremotename(sk, &addr, &addrlen) < 0)
d3372da9 1314 return;
1315 if(addr->sa_family == AF_INET)
1316 qstrf(sk, "$ConnectToMe %s %s|", nick, formataddress(addr, addrlen));
1317 else
1318 flog(LOG_WARNING, "Direct Connect TCP socket is suddenly not AF_INET, but %i", addr->sa_family);
1319 free(addr);
1320}
1321
1322static void cmd_revconnecttome(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1323{
1324 struct dchub *hub;
1325 char *p;
1326
1327 hub = fn->data;
1328 if((p = strchr(args, ' ')) == NULL)
1329 return;
1330 *(p++) = 0;
1331 if(strcmp(p, hub->nativenick))
1332 return;
1333 sendctm(sk, args);
1334 expectpeer(args, fn);
1335 hubhandleaction(sk, fn, cmd, args);
1336}
1337
1338static void cmd_getnetinfo(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1339{
1340 struct dchub *hub;
1341 struct fnetnode *node;
1342 int numhubs;
1343
1344 hub = fn->data;
1345 numhubs = 0;
1346 for(node = fnetnodes; node != NULL; node = node->next)
1347 {
1348 if(node->state == FNN_EST)
1349 numhubs++;
1350 }
1351 qstrf(sk, "$NetInfo %i$%i$%c$0|", confgetint("transfer", "slots"), numhubs, (tcpsock == NULL)?'P':'A');
1352 hubhandleaction(sk, fn, cmd, args);
1353}
1354
1355static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1356{
1357 struct dchub *hub;
1358 char *p, *p2;
1359
1360 hub = fn->data;
1361 p = args;
1362 if((p2 = strchr(p, ' ')) == NULL)
1363 return;
1364 *(p2++) = 0;
1365 p = p2;
1366 if((p2 = strchr(p, ' ')) == NULL)
1367 return;
1368 *(p2++) = 0;
1369 if(strcmp(p, "From:"))
1370 return;
1371 p = p2;
1372 if((p2 = strstr(p, " $")) == NULL)
1373 return;
1374 *p2 = 0;
1375 p2 += 2;
1376 hubrecvchat(fn->sk, fn, p, p2);
1377 hubhandleaction(sk, fn, cmd, args);
1378}
1379
1380static void cmd_sr(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1381{
1382 struct dchub *hub;
d86b7a9f 1383 char *p, *p2, *buf;
d3372da9 1384 char *nick, *filename, *hubname;
1385 int size, slots;
d86b7a9f 1386 size_t buflen;
d3372da9 1387 struct srchres *sr;
1388 wchar_t *wnick, *wfile;
1389
1390 hub = fn->data;
1391 nick = p = args;
1392 if((p2 = strchr(p, ' ')) == NULL)
1393 return;
1394 *p2 = 0;
1395 p = p2 + 1;
1396 filename = p;
1397 if((p2 = strchr(p, 5)) == NULL)
1398 return;
1399 *p2 = 0;
1400 p = p2 + 1;
1401 if((p2 = strchr(p, ' ')) == NULL)
1402 return;
1403 *p2 = 0;
1404 size = atoi(p);
1405 p = p2 + 1;
1406 if((p2 = strchr(p, '/')) == NULL)
1407 return;
1408 *p2 = 0;
1409 slots = atoi(p);
1410 p = p2 + 1;
1411 if((p2 = strchr(p, 5)) == NULL)
1412 return;
1413 p = p2 + 1;
1414 hubname = p;
1415 if((p2 = strstr(p, " (")) == NULL)
1416 return;
1417 *p2 = 0;
1d85f24b 1418 if((wnick = icmbstowcs(nick, hub->charset)) == NULL)
d3372da9 1419 return;
1d85f24b 1420 /* Use DCCHARSET in $Get paths until further researched... */
d3372da9 1421 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
1422 {
1423 free(wnick);
1424 return;
1425 }
1426 sr = newsrchres(&dcnet, wfile, wnick);
1427 if(sr->peernick != NULL)
1428 free(sr->peernick);
1429 sr->peernick = swcsdup(wnick);
1430 sr->size = size;
1431 sr->slots = slots;
1432 free(wfile);
1433 free(wnick);
d86b7a9f 1434 if(!strncmp(hubname, "TTH:", 4))
1435 {
1436 if((buf = base32decode(hubname + 4, &buflen)) != NULL)
1437 {
1438 if(buflen == 24)
1439 sr->hash = newhash(L"TTH", 24, buf);
1440 free(buf);
1441 }
1442 }
d3372da9 1443 getfnetnode(sr->fn = fn);
1444 submitsrchres(sr);
1445 freesrchres(sr);
1446 hubhandleaction(sk, fn, cmd, args);
1447}
1448
1449static void cmd_usercommand(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1450{
1451 /* Do nothing for now. */
1452}
1453
48509089 1454static void cmd_getpass(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1455{
1456 struct dchub *hub;
1c4b02cd 1457 wchar_t *pw;
48509089 1458 char *mbspw;
1459
1460 hub = fn->data;
1c4b02cd 1461 pw = wpfind(fn->args, L"password");
1d85f24b 1462 if((pw == NULL) || ((mbspw = icwcstombs(pw, hub->charset)) == NULL))
48509089 1463 {
1464 killfnetnode(fn);
1465 return;
1466 }
1467 qstrf(sk, "$MyPass %s|", mbspw);
1468 free(mbspw);
fcd0627b 1469 fn->regstatus = FNNS_REG;
48509089 1470 hubhandleaction(sk, fn, cmd, args);
1471}
1472
c9b3ad74 1473static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
1474{
1475 struct dchub *hub;
1476
1477 hub = fn->data;
fcd0627b 1478 fn->regstatus = FNNS_OP;
c9b3ad74 1479 hubhandleaction(sk, fn, cmd, args);
1480}
1481
d3372da9 1482static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1483{
9a2d6cb8 1484 struct dcexppeer *expect;
1c4b02cd 1485 struct dchub *hub;
9a2d6cb8 1486
d3372da9 1487 if(peer->nativename != NULL)
1488 free(peer->nativename);
1489 peer->nativename = sstrdup(args);
1490 if(peer->wcsname != NULL)
1491 free(peer->wcsname);
1d85f24b 1492 if((peer->wcsname = icmbstowcs(peer->nativename, peer->charset)) == NULL)
d3372da9 1493 {
1494 freedcpeer(peer);
1495 return;
1496 }
9a2d6cb8 1497 if(peer->accepted)
1498 {
1499 for(expect = expected; expect != NULL; expect = expect->next)
1500 {
1501 if(!strcmp(expect->nick, args))
1502 break;
1503 }
1504 if(expect == NULL)
1505 {
1506 peer->fn = NULL;
1507 } else {
1c4b02cd 1508 hub = expect->fn->data;
9a2d6cb8 1509 peer->fn = expect->fn;
1510 getfnetnode(peer->fn);
1c4b02cd 1511 peer->dcppemu = hub->dcppemu;
9a2d6cb8 1512 freeexppeer(expect);
1513 }
1514 }
d3372da9 1515}
1516
1517static void cmd_direction(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1518{
1519 char *p;
9a2d6cb8 1520 int mydir;
1521 struct transfer *transfer;
d3372da9 1522
1523 if((p = strchr(args, ' ')) == NULL)
1524 return;
1525 *p = 0;
1526 if(!strcmp(args, "Upload"))
9a2d6cb8 1527 mydir = TRNSD_DOWN;
d3372da9 1528 if(!strcmp(args, "Download"))
9a2d6cb8 1529 mydir = TRNSD_UP;
1530 if(peer->accepted)
1531 {
1532 if((peer->transfer == NULL) || (mydir != peer->direction))
1533 {
1534 freedcpeer(peer);
1535 return;
1536 }
1b14d3d8 1537 if(peer->direction == TRNSD_DOWN)
1538 requestfile(peer);
9a2d6cb8 1539 } else {
1540 if(peer->wcsname == NULL)
1541 {
1542 freedcpeer(peer);
1543 return;
1544 }
1545 peer->direction = mydir;
1546 if(peer->direction == TRNSD_UP)
1547 {
1548 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1549 } else {
1550 if((transfer = finddownload(peer->wcsname)) == NULL)
1551 {
1552 freedcpeer(peer);
1553 return;
1554 }
1555 transferattach(transfer, &dctransfer, peer);
1556 transfersetstate(transfer, TRNS_HS);
1557 }
1558 transfersetnick(transfer, peer->wcsname);
1559 peer->transfer = transfer;
1560 if(peer->extended)
1561 sendsupports(peer);
1562 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1563 if(peer->key != NULL)
1564 qstrf(sk, "$Key %s|", peer->key);
1565 if(peer->direction == TRNSD_DOWN)
1566 requestfile(peer);
1567 }
d3372da9 1568}
1569
1570static void cmd_peerlock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1571{
9a2d6cb8 1572 char *p, *key;
1573 struct transfer *transfer;
d3372da9 1574
1575 if((p = strchr(args, ' ')) == NULL)
1576 return;
1577 *p = 0;
1578 if(!strncmp(args, "EXTENDEDPROTOCOL", 16))
1579 peer->extended = 1;
9a2d6cb8 1580 key = dcmakekey(args);
1581 if(peer->accepted)
1582 {
1583 if(peer->wcsname == NULL)
1584 {
1585 freedcpeer(peer);
1586 return;
1587 }
1588 sendmynick(peer);
1589 sendpeerlock(peer);
1590 if(peer->extended)
1591 sendsupports(peer);
1592 if((transfer = finddownload(peer->wcsname)) == NULL)
1593 {
1594 peer->direction = TRNSD_UP;
1595 transfer = newupload(peer->fn, &dcnet, peer->wcsname, &dctransfer, peer);
1596 } else {
1597 peer->direction = TRNSD_DOWN;
1598 transferattach(transfer, &dctransfer, peer);
1599 transfersetstate(transfer, TRNS_HS);
1600 }
1601 transfersetnick(transfer, peer->wcsname);
1602 peer->transfer = transfer;
1603 qstrf(sk, "$Direction %s %i|", (peer->direction == TRNSD_UP)?"Upload":"Download", rand() % 10000);
1604 qstrf(sk, "$Key %s|", key);
62da2712 1605 free(key);
9a2d6cb8 1606 } else {
1607 if(peer->key != NULL)
1608 free(peer->key);
1609 peer->key = key;
1610 }
d3372da9 1611}
1612
1613static void cmd_key(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1614{
9a2d6cb8 1615 /* NOP */
1616}
1617
1618static void startdl(struct dcpeer *peer)
1619{
1e9d82b1 1620 if(peer->timeout != NULL)
1621 canceltimer(peer->timeout);
9a2d6cb8 1622 peer->state = PEER_TRNS;
1623 transferstartdl(peer->transfer, peer->sk);
1624 peer->sk->readcb = (void (*)(struct socket *, void *))transread;
1625 peer->sk->errcb = (void (*)(struct socket *, int, void *))transerr;
1626}
1627
1628static void startul(struct dcpeer *peer)
1629{
1e9d82b1 1630 if(peer->timeout != NULL)
1631 canceltimer(peer->timeout);
9a2d6cb8 1632 peer->state = PEER_TRNS;
1633 transferstartul(peer->transfer, peer->sk);
1634 peer->sk->writecb = (void (*)(struct socket *, void *))transwrite;
d3372da9 1635}
1636
1637static void cmd_filelength(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1638{
1639 int size;
1fc81821 1640 struct transfer *transfer;
d3372da9 1641
1642 if(peer->transfer == NULL)
1643 {
1644 freedcpeer(peer);
1645 return;
1646 }
1647 size = atoi(args);
9a2d6cb8 1648 if(peer->transfer->size != size)
d3372da9 1649 {
d3372da9 1650 transfersetsize(peer->transfer, size);
1fc81821 1651 transfer = peer->transfer;
d3372da9 1652 freedcpeer(peer);
1fc81821 1653 trytransferbypeer(transfer->fnet, transfer->peerid);
d3372da9 1654 return;
1655 }
9a2d6cb8 1656 startdl(peer);
1657 qstr(peer->sk, "$Send|");
d3372da9 1658}
1659
1660static void cmd_error(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1661{
df873cd0 1662 if(peer->fetchingtthl)
1663 {
1664 peer->fetchingtthl = 0;
1665 peer->notthl = 1;
1666 requestfile(peer);
1667 return;
1668 }
d3372da9 1669 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1670 {
1671 transferseterror(peer->transfer, TRNSE_NOTFOUND);
1672 resettransfer(peer->transfer);
1673 return;
1674 }
1675 freedcpeer(peer);
1676}
1677
1678static void cmd_maxedout(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1679{
1680 if((peer->transfer != NULL) && (peer->transfer->dir == TRNSD_DOWN))
1681 {
1682 transferseterror(peer->transfer, TRNSE_NOSLOTS);
1683 resettransfer(peer->transfer);
1684 return;
1685 }
1686 freedcpeer(peer);
1687}
1688
1689static struct
1690{
1691 char *name;
1692 char **file;
1693 void (*func)(void);
1694} lists[] =
1695{
1696 {"MyList.DcLst", &hmlistname, updatehmlist},
1697 {"files.xml", &xmllistname, updatexmllist},
1698 {"files.xml.bz2", &xmlbz2listname, updatexmlbz2list},
1699 {NULL, NULL}
1700};
1701
1702static int openfilelist(char *name)
1703{
1704 int i, fd;
1705 int errnobak;
1706
1707 for(i = 0; lists[i].name != NULL; i++)
1708 {
1709 if(!strcmp(name, lists[i].name))
1710 break;
1711 }
1712 errno = 0;
1713 if(lists[i].name == NULL)
1714 return(-1);
1715 fd = -1;
1716 if((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0))
1717 lists[i].func();
1718 if((fd < 0) && ((*lists[i].file == NULL) || ((fd = open(*lists[i].file, O_RDONLY)) < 0)))
1719 {
1720 errnobak = errno;
1721 flog(LOG_ERR, "could not open filelist tempfile: %s", strerror(errno));
1722 errno = errnobak;
1723 return(-1);
1724 }
1725 return(fd);
1726}
1727
1728static struct sharecache *findbytth(char *tth32)
1729{
1730 char *buf;
1731 size_t buflen;
1732 struct sharecache *node;
1733
1734 if((buf = base32decode(tth32, &buflen)) == NULL)
1735 return(NULL);
1736 if(buflen != 24)
1737 {
1738 free(buf);
1739 return(NULL);
1740 }
1741 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1742 {
1743 if(node->f.b.hastth && !memcmp(node->hashtth, buf, 24))
1744 break;
1745 }
1746 free(buf);
1747 return(node);
1748}
1749
1750static struct sharecache *resdcpath(char *path, char *charset, char sep)
1751{
1752 struct sharecache *node;
1753 char *p, *p2;
1754
1755 node = shareroot;
1756 p = path;
1757 while(p != NULL)
1758 {
1759 if((p2 = strchr(p, sep)) != NULL)
1760 *(p2++) = 0;
1761 if((node = findcache(node, icsmbstowcs(p, charset, L""))) == NULL)
1762 return(NULL);
1763 p = p2;
1764 }
1765 return(node);
1766}
1767
1768static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1769{
1770 int offset;
1771 char *p, *buf;
1772 wchar_t *buf2;
1773 struct sharecache *node;
1774 struct socket *lesk;
1775 int fd;
1776 struct stat sb;
1777
1778 if(peer->transfer == NULL)
1779 {
1780 freedcpeer(peer);
1781 return;
1782 }
1783 if((p = strchr(args, '$')) == NULL)
1784 {
1785 freedcpeer(peer);
1786 return;
1787 }
1788 *(p++) = 0;
1789 if((offset = (atoi(p) - 1)) < 0)
1790 {
1791 freedcpeer(peer);
1792 return;
1793 }
1794 if(((fd = openfilelist(args)) < 0) && (errno != 0))
1795 {
1796 qstr(sk, "$Error Could not send file list|");
1797 freedcpeer(peer);
1798 return;
1799 } else if(fd >= 0) {
1800 if((buf2 = icsmbstowcs(args, DCCHARSET, NULL)) != NULL)
1801 transfersetpath(peer->transfer, buf2);
f00d7432 1802 peer->transfer->flags.b.minislot = 1;
d3372da9 1803 }
1804 if(fd < 0)
1805 {
1d85f24b 1806 /* Use DCCHARSET in $Get paths until further researched... */
d3372da9 1807 if((node = resdcpath(args, DCCHARSET, '\\')) == NULL)
1808 {
1809 qstrf(sk, "$Error File not in share|");
1810 freedcpeer(peer);
1811 return;
1812 }
1813 if((fd = opensharecache(node)) < 0)
1814 {
1815 qstrf(sk, "$Error %s|", strerror(errno));
1816 freedcpeer(peer);
1817 return;
1818 }
1819 buf = getfspath(node);
1820 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1821 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1822 else
1823 transfersetpath(peer->transfer, buf2);
1824 free(buf);
1825 }
1826 if(fstat(fd, &sb) < 0)
1827 {
1828 close(fd);
1829 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1830 qstrf(sk, "$Error|");
1831 freedcpeer(peer);
1832 return;
1833 }
f00d7432 1834 if(sb.st_size < 65536)
1835 peer->transfer->flags.b.minislot = 1;
1836 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
565006d1 1837 close(fd);
f00d7432 1838 qstr(sk, "$MaxedOut|");
1839 freedcpeer(peer);
1840 return;
1841 }
d3372da9 1842 if((offset != 0) && (lseek(fd, offset, SEEK_SET) < 0))
1843 {
1844 close(fd);
1845 qstrf(sk, "$Error Offset out of range|");
1846 freedcpeer(peer);
1847 return;
1848 }
1849 lesk = wrapsock(fd);
1850 transferprepul(peer->transfer, sb.st_size, offset, -1, lesk);
1851 putsock(lesk);
1852 qstrf(sk, "$FileLength %i|", peer->transfer->size);
d3372da9 1853}
1854
1855static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1856{
1857 if(peer->transfer == NULL)
1858 {
1859 freedcpeer(peer);
1860 return;
1861 }
1862 if(peer->transfer->localend == NULL)
1863 {
1864 freedcpeer(peer);
1865 return;
1866 }
1867 peer->ptclose = 1;
9a2d6cb8 1868 startul(peer);
d3372da9 1869}
1870
1871static void cmd_supports(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1872{
1873 int i;
1874 char *p, *p2;
1875 char **arr;
1876 size_t arrsize, arrdata;
1877
1878 if(peer->supports != NULL)
1879 {
1880 for(i = 0; peer->supports[i] != NULL; i++)
1881 free(peer->supports[i]);
1882 free(peer->supports);
1883 }
1884 arr = NULL;
1885 arrsize = arrdata = 0;
1886 p = args;
1887 do
1888 {
1889 if((p2 = strchr(p, ' ')) != NULL)
1890 *(p2++) = 0;
1891 if(*p == 0)
1892 continue;
1893 addtobuf(arr, sstrdup(p));
1894 } while((p = p2) != NULL);
1895 addtobuf(arr, NULL);
1896 peer->supports = arr;
d3372da9 1897}
1898
1899static void cmd_getblock(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1900{
1901 int fd;
1902 char *p, *p2;
1903 int start, numbytes;
1904 char *charset, *buf;
1905 wchar_t *buf2;
1906 struct sharecache *node;
1907 struct stat sb;
1908 struct socket *lesk;
1909
1910 if(peer->transfer == NULL)
1911 {
1912 freedcpeer(peer);
1913 return;
1914 }
1915 p = args;
1916 if((p2 = strchr(p, ' ')) == NULL)
1917 {
1918 freedcpeer(peer);
1919 return;
1920 }
1921 *(p2++) = 0;
1922 start = atoi(p);
1923 p = p2;
1924 if((p2 = strchr(p, ' ')) == NULL)
1925 {
1926 freedcpeer(peer);
1927 return;
1928 }
1929 *(p2++) = 0;
1930 numbytes = atoi(p);
1931 p = p2;
1932 if(!strcmp(cmd, "$UGetBlock") || !strcmp(cmd, "$UGetZBlock"))
1933 charset = "UTF-8";
1934 else
1d85f24b 1935 /* Use DCCHARSET in $Get paths until further researched... */
d3372da9 1936 charset = DCCHARSET;
1937 if(!strcmp(cmd, "$GetZBlock") || !strcmp(cmd, "$UGetZBlock"))
1938 initcompress(peer, CPRS_ZLIB);
1939 if(((fd = openfilelist(p)) < 0) && (errno != 0))
1940 {
1941 qstr(sk, "$Error Could not send file list|");
1942 return;
1943 } else if(fd >= 0) {
1944 if((buf2 = icsmbstowcs(args, charset, NULL)) != NULL)
1945 transfersetpath(peer->transfer, buf2);
f00d7432 1946 peer->transfer->flags.b.minislot = 1;
d3372da9 1947 }
1948 if(fd < 0)
1949 {
d3372da9 1950 if((node = resdcpath(p, charset, '\\')) == NULL)
1951 {
1952 qstr(sk, "$Error File not in cache|");
1953 return;
1954 }
1955 if((fd = opensharecache(node)) < 0)
1956 {
1957 qstrf(sk, "$Error %s|", strerror(errno));
1958 return;
1959 }
1960 buf = getfspath(node);
1961 if((buf2 = icsmbstowcs(buf, NULL, NULL)) == NULL)
1962 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
1963 else
1964 transfersetpath(peer->transfer, buf2);
1965 free(buf);
1966 }
1967 if(fstat(fd, &sb) < 0)
1968 {
1969 close(fd);
1970 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
1971 qstr(sk, "$Error|");
1972 return;
1973 }
f00d7432 1974 if(sb.st_size < 65536)
1975 peer->transfer->flags.b.minislot = 1;
1976 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
565006d1 1977 close(fd);
f00d7432 1978 qstr(sk, "$MaxedOut|");
1979 return;
1980 }
d3372da9 1981 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
1982 {
1983 close(fd);
1984 qstr(sk, "$Error Offset out of range|");
1985 return;
1986 }
1987 if((numbytes < 0) || (start + numbytes > sb.st_size))
1988 numbytes = sb.st_size - start;
1989 lesk = wrapsock(fd);
1990 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
1991 putsock(lesk);
1992 qstrf(sk, "$Sending %i|", numbytes);
9a2d6cb8 1993 startul(peer);
d3372da9 1994}
1995
1996static void cmd_adcget(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
1997{
1998 int i;
1999 char **argv, *buf;
2000 int start, numbytes;
2001 struct sharecache *node;
2002 struct stat sb;
2003 struct socket *lesk;
2004 wchar_t *wbuf;
2005 int fd;
2006
2007 if(peer->transfer == NULL)
2008 {
2009 freedcpeer(peer);
2010 return;
2011 }
2012 if((argv = parseadc(args)) == NULL)
2013 {
2014 freedcpeer(peer);
2015 return;
2016 }
2017 if(parrlen(argv) < 4)
2018 {
2019 freedcpeer(peer);
2020 goto out;
2021 }
2022 start = atoi(argv[2]);
2023 numbytes = atoi(argv[3]);
2024 node = NULL;
2025 fd = -1;
2026 if(((fd = openfilelist(argv[1])) < 0) && (errno != 0))
2027 {
2028 qstr(sk, "$Error Could not send file list|");
2029 goto out;
2030 } else if(fd >= 0) {
2031 if((wbuf = icsmbstowcs(argv[1], "UTF-8", NULL)) != NULL)
2032 transfersetpath(peer->transfer, wbuf);
f00d7432 2033 peer->transfer->flags.b.minislot = 1;
d3372da9 2034 }
2035 if(fd < 0)
2036 {
d3372da9 2037 if(!strncmp(argv[1], "TTH/", 4))
2038 {
2039 if((node = findbytth(argv[1] + 4)) == NULL)
2040 {
2041 qstr(sk, "$Error File not in cache|");
2042 goto out;
2043 }
2044 } else {
9a2d6cb8 2045 if((node = resdcpath((argv[1][0] == '/')?(argv[1] + 1):(argv[1]), "UTF-8", '/')) == NULL)
d3372da9 2046 {
2047 qstr(sk, "$Error File not in cache|");
2048 goto out;
2049 }
2050 }
2051 if((fd = opensharecache(node)) < 0)
2052 {
2053 qstrf(sk, "$Error %s|", strerror(errno));
2054 goto out;
2055 }
2056 buf = getfspath(node);
2057 if((wbuf = icsmbstowcs(buf, NULL, NULL)) == NULL)
2058 flog(LOG_WARNING, "could not convert native path into a wcs (%s): %s", buf, strerror(errno));
2059 else
2060 transfersetpath(peer->transfer, wbuf);
2061 free(buf);
2062 }
2063 if(!strcmp(argv[0], "file"))
2064 {
2065 for(i = 4; argv[i] != NULL; i++)
2066 {
2067 if(!strcmp(argv[i], "ZL1"))
2068 initcompress(peer, CPRS_ZLIB);
2069 }
2070 if(fstat(fd, &sb) < 0)
2071 {
2072 flog(LOG_WARNING, "could not stat file %ls: %s", node->name, strerror(errno));
2073 qstr(sk, "$Error|");
2074 goto out;
2075 }
f00d7432 2076 if(sb.st_size < 65536)
2077 peer->transfer->flags.b.minislot = 1;
2078 if(!peer->transfer->flags.b.minislot && (slotsleft() < 1)) {
2079 qstr(sk, "$MaxedOut|");
2080 goto out;
2081 }
d3372da9 2082 if((start != 0) && ((start >= sb.st_size) || (lseek(fd, start, SEEK_SET) < 0)))
2083 {
2084 qstr(sk, "$Error Offset out of range|");
2085 goto out;
2086 }
2087 if((numbytes < 0) || (start + numbytes > sb.st_size))
2088 numbytes = sb.st_size - start;
2089 lesk = wrapsock(fd);
2090 transferprepul(peer->transfer, sb.st_size, start, start + numbytes, lesk);
2091 putsock(lesk);
2092 fd = -1;
2093 qstr(sk, "$ADCSND");
2094 sendadc(sk, "file");
2095 sendadc(sk, argv[1]);
2096 sendadcf(sk, "%i", start);
2097 sendadcf(sk, "%i", numbytes);
2098 if(peer->compress == CPRS_ZLIB)
2099 sendadc(sk, "ZL1");
2100 qstr(sk, "|");
9a2d6cb8 2101 startul(peer);
d3372da9 2102 } else if(!strcmp(argv[0], "tthl")) {
2103 /*
2104 * XXX: Implement full TTHL support.
2105 *
2106 * In the meantime, this is better than nothing, since it at
2107 * least allows fetching of the TTH root if it isn't already
2108 * known.
2109 */
2110 if(node == NULL)
2111 {
2112 qstr(sk, "$Error no TTHL data for virtual files|");
2113 goto out;
2114 }
2115 qstr(sk, "$ADCSND");
2116 sendadc(sk, "tthl");
2117 sendadc(sk, argv[1]);
2118 sendadc(sk, "0");
2119 sendadc(sk, "24");
2120 qstr(sk, "|");
2121 sockqueue(sk, node->hashtth, 24);
2122 } else {
2123 qstr(sk, "$Error Namespace not implemented|");
2124 goto out;
2125 }
d3372da9 2126
2127 out:
2128 if(fd >= 0)
2129 close(fd);
2130 freeparr(argv);
2131}
2132
9a2d6cb8 2133static void handletthl(struct dcpeer *peer)
2134{
2135 char buf[24];
2136
2137 while(peer->inbufdata >= 24)
2138 {
2139 pushtigertree(&peer->tth, peer->inbuf);
2140 memmove(peer->inbuf, peer->inbuf + 24, peer->inbufdata -= 24);
e52006fe 2141 peer->curread += 24;
9a2d6cb8 2142 }
e52006fe 2143 if(peer->curread >= peer->totalsize)
9a2d6cb8 2144 {
1e9d82b1 2145 if(peer->timeout == NULL)
2146 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
9a2d6cb8 2147 peer->state = PEER_CMD;
2148 synctigertree(&peer->tth);
2149 restigertree(&peer->tth, buf);
2150 transfersethash(peer->transfer, newhash(L"TTH", 24, buf));
2151 requestfile(peer);
2152 }
2153}
2154
2155static void cmd_adcsnd(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2156{
2157 char **argv;
2158 int start, numbytes;
2159
2160 if(peer->transfer == NULL)
2161 {
2162 freedcpeer(peer);
2163 return;
2164 }
2165 if((argv = parseadc(args)) == NULL)
2166 {
2167 freedcpeer(peer);
2168 return;
2169 }
2170 if(parrlen(argv) < 4)
2171 {
2172 freedcpeer(peer);
2173 goto out;
2174 }
2175 start = atoi(argv[2]);
2176 numbytes = atoi(argv[3]);
2177 if(!strcmp(argv[0], "tthl"))
2178 {
2179 if((start != 0) || (numbytes % 24 != 0))
2180 {
2181 /* Weird. Bail out. */
2182 freedcpeer(peer);
2183 goto out;
2184 }
1e9d82b1 2185 if(peer->timeout != NULL)
2186 canceltimer(peer->timeout);
9a2d6cb8 2187 peer->state = PEER_TTHL;
2188 peer->totalsize = numbytes;
2189 peer->curread = 0;
df873cd0 2190 peer->fetchingtthl = 0;
9a2d6cb8 2191 inittigertree(&peer->tth);
2192 handletthl(peer);
2193 } else if(!strcmp(argv[0], "file")) {
2194 if(start != peer->transfer->curpos)
2195 {
2196 freedcpeer(peer);
2197 goto out;
2198 }
2199 if(start + numbytes != peer->transfer->size)
2200 {
2201 transfersetsize(peer->transfer, start + numbytes);
2202 freedcpeer(peer);
2203 goto out;
2204 }
2205 startdl(peer);
e52006fe 2206 if(peer->inbufdata > 0)
2207 {
2208 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2209 peer->inbufdata = 0;
2210 transread(sk, peer);
2211 }
9a2d6cb8 2212 } else {
2213 /* We certainly didn't request this...*/
2214 freedcpeer(peer);
2215 goto out;
2216 }
2217
2218 out:
2219 freeparr(argv);
2220}
2221
2222static void cmd_sending(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2223{
2224 int numbytes;
2225
2226 if(peer->transfer == NULL)
2227 {
2228 freedcpeer(peer);
2229 return;
2230 }
2231 numbytes = atoi(args);
2232 if(peer->transfer->size - peer->transfer->curpos != numbytes)
2233 {
2234 transfersetsize(peer->transfer, peer->transfer->curpos + numbytes);
2235 freedcpeer(peer);
2236 return;
2237 }
2238 startdl(peer);
0d8c93cb 2239 if(peer->inbufdata > 0)
2240 {
2241 sockpushdata(sk, peer->inbuf, peer->inbufdata);
2242 peer->inbufdata = 0;
2243 transread(sk, peer);
2244 }
9a2d6cb8 2245}
2246
d3372da9 2247/*
2248Hub skeleton:
2249static void cmd_(struct socket *sk, struct fnetnode *fn, char *cmd, char *args)
2250{
2251 struct dchub *hub;
2252
2253 hub = fn->data;
2254 hubhandleaction(sk, fn, cmd, args);
2255}
2256
2257Peer skeleton:
2258static void cmd_(struct socket *sk, struct dcpeer *peer, char *cmd, char *args)
2259{
d3372da9 2260}
2261
2262*/
2263
2264static int hubreqconn(struct fnetpeer *peer)
2265{
2266 struct dchub *hub;
2267 char *mbsnick;
2268
2269 if((peer->fn->state != FNN_EST) || (peer->fn->fnet != &dcnet))
2270 {
2271 errno = EINVAL;
2272 return(1);
2273 }
2274 if((hub = peer->fn->data) == NULL)
2275 {
2276 errno = EFAULT;
2277 return(1);
2278 }
1d85f24b 2279 if((mbsnick = icwcstombs(peer->id, hub->charset)) == NULL)
d3372da9 2280 return(1); /* Shouldn't happen, of course, but who knows... */
2281 if(tcpsock != NULL)
2282 {
2283 sendctm(peer->fn->sk, mbsnick);
2284 expectpeer(mbsnick, peer->fn);
2285 } else {
2286 qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick);
2287 }
2288 free(mbsnick);
2289 return(0);
2290}
2291
2292static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
2293{
2294 struct dchub *hub;
2295 char *mbsstring, *mbsto;
2296
2297 hub = fn->data;
1d85f24b 2298 if((mbsto = icwcstombs(to, hub->charset)) == NULL)
d3372da9 2299 {
2300 errno = EILSEQ;
2301 return(1);
2302 }
1d85f24b 2303 if((mbsstring = icwcstombs(string, hub->charset)) == NULL)
d3372da9 2304 {
2305 errno = EILSEQ;
2306 return(1);
2307 }
2308 if((strchr(mbsto, '|') != NULL) || (strchr(mbsto, ' ') != NULL))
2309 {
2310 free(mbsto);
2311 free(mbsstring);
2312 errno = ESRCH;
2313 return(1);
2314 }
2315 if(strchr(mbsstring, '|') != NULL)
2316 {
2317 free(mbsto);
2318 free(mbsstring);
2319 errno = EILSEQ;
2320 return(1);
2321 }
2322 if(public)
2323 {
2324 if(*to == L'\0')
2325 {
2326 qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring);
2327 } else {
2328 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2329 }
2330 } else {
2331 qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring);
2332 }
2333 free(mbsto);
2334 free(mbsstring);
2335 return(0);
2336}
2337
2338static void findsizelimit(struct sexpr *sexpr, int *min, int *max)
2339{
2340 int minl, maxl, minr, maxr, retmin, retmax;
2341
2342 switch(sexpr->op)
2343 {
2344 case SOP_AND:
2345 findsizelimit(sexpr->l, &minl, &maxl);
2346 findsizelimit(sexpr->r, &minr, &maxr);
2347 retmin = (minl > minr)?minl:minr;
2348 if((maxl != -1) && (maxr != -1))
2349 retmax = (maxl < maxr)?maxl:maxr;
2350 else if(maxl != -1)
2351 retmax = maxl;
2352 else if(maxr != -1)
2353 retmax = maxr;
2354 else
2355 retmax = -1;
2356 break;
2357 case SOP_OR:
2358 findsizelimit(sexpr->l, &minl, &maxl);
2359 findsizelimit(sexpr->r, &minr, &maxr);
2360 retmin = (minl < minr)?minl:minr;
2361 if((maxl == -1) || (maxr == -1))
2362 retmax = -1;
2363 else
2364 retmax = (maxl > maxr)?maxl:maxr;
2365 break;
2366 case SOP_NOT:
2367 findsizelimit(sexpr->l, &minl, &maxl);
2368 if((minl == 0) && (maxl == -1)) /* Interval is unspecified */
2369 {
2370 retmin = 0;
2371 retmax = -1;
2372 } else if((minl == 0) && (maxl != -1)) {
2373 retmin = maxl + 1;
2374 retmax = -1;
2375 } else if((minl != 0) && (maxl == -1)) {
2376 retmin = 0;
2377 retmax = minl - 1;
2378 } else { /* This would yield two seperate intervals, which DC cannot handle */
2379 retmin = 0;
2380 retmax = -1;
2381 }
2382 case SOP_SIZELT:
2383 retmin = 0;
2384 retmax = sexpr->d.n - 1;
2385 break;
2386 case SOP_SIZEEQ:
2387 retmin = sexpr->d.n;
2388 retmax = sexpr->d.n;
2389 break;
2390 case SOP_SIZEGT:
2391 retmin = sexpr->d.n + 1;
2392 retmax = -1;
2393 break;
2394 default:
2395 retmin = 0;
2396 retmax = -1;
2397 break;
2398 }
2399 if(min != NULL)
2400 *min = retmin;
2401 if(max != NULL)
2402 *max = retmax;
2403}
2404
9d8cf743 2405static struct hash *findsehash(struct sexpr *sexpr)
2406{
2407 struct hash *h1, *h2;
2408
2409 switch(sexpr->op)
2410 {
2411 case SOP_AND:
2412 if((h1 = findsehash(sexpr->l)) != NULL)
2413 return(h1);
2414 if((h1 = findsehash(sexpr->r)) != NULL)
2415 return(h1);
2416 break;
2417 case SOP_OR:
2418 h1 = findsehash(sexpr->l);
2419 h2 = findsehash(sexpr->r);
2420 if(hashcmp(h1, h2))
2421 return(h1);
2422 break;
2423 case SOP_HASHIS:
2424 if(!wcscmp(sexpr->d.hash->algo, L"TTH"))
2425 return(sexpr->d.hash);
2426 default:
2427 break;
2428 }
2429 return(NULL);
2430}
2431
d3372da9 2432static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
2433{
2434 struct dchub *hub;
2435 struct wcslist *list, *cur;
2436 char *sstr, *buf, *p;
2437 size_t sstrsize, sstrdata;
2438 struct sockaddr *name;
2439 socklen_t namelen;
2440 int minsize, maxsize;
9d8cf743 2441 struct hash *hash;
d3372da9 2442
2443 hub = fn->data;
2444 if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST))
2445 return(1);
2446 list = findsexprstrs(srch->sexpr);
2447 findsizelimit(srch->sexpr, &minsize, &maxsize);
9d8cf743 2448 hash = findsehash(srch->sexpr);
d3372da9 2449 if((minsize != 0) && (maxsize != -1))
2450 {
2451 /* Choose either minsize or maxsize by trying to determine
2452 * which choice will be most restrictive. The result can be
2453 * approximative at best anyway, so don't try too hard... */
2454 if((50000000 - maxsize) > (minsize - 50000000))
2455 minsize = 0;
2456 else
2457 maxsize = -1;
2458 }
d3372da9 2459 sstr = NULL;
2460 sstrsize = sstrdata = 0;
9d8cf743 2461 if((hash != NULL) && (hash->len == 24))
d3372da9 2462 {
9d8cf743 2463 /* Prioritize hash searches above all else */
2464 bufcat(sstr, "F?T?0?9?TTH:", 12);
2465 buf = base32encode(hash->buf, hash->len);
2466
2467 bufcat(sstr, buf, 39);
2468 free(buf);
2469 } else {
2470 if(minsize != 0)
d3372da9 2471 {
9d8cf743 2472 sizebuf2(sstr, sstrdata + 32, 1);
fe42d689 2473 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?F?%i?1?", minsize);
9d8cf743 2474 } else if(maxsize != -1) {
2475 sizebuf2(sstr, sstrdata + 32, 1);
fe42d689 2476 sstrdata += snprintf(sstr + sstrdata, sstrsize - sstrdata, "T?T?%i?1?", maxsize);
9d8cf743 2477 } else {
2478 bufcat(sstr, "F?F?0?1?", 8);
2479 }
2480 if(list != NULL)
2481 {
2482 for(cur = list; cur != NULL; cur = cur->next)
d3372da9 2483 {
1d85f24b 2484 /* Use DCCHARSET in $Get paths until further researched... */
9d8cf743 2485 if((buf = icwcstombs(cur->str, DCCHARSET)) == NULL)
2486 {
2487 /* Can't find anything anyway if the search expression
2488 * requires characters outside DC's charset. There's
2489 * nothing technically wrong with the search itself,
2490 * however, so return success. This should be
2491 * considered as an optimization. */
2492 freesl(&list);
2493 if(sstr != NULL)
2494 free(sstr);
2495 return(0);
2496 }
2497 if(cur != list)
2498 addtobuf(sstr, '$');
2499 /*
2500 * It probably doesn't hurt if buf contains any extra
2501 * dollar signs - it will just result in extra search
2502 * terms, and the extraneous results will be filtered by
2503 * the search layer anyway. It hurts if it contains any
2504 * pipes, though, so let's sell them for money.
2505 */
2506 for(p = buf; *p; p++)
2507 {
2508 if(*p == '|')
2509 *p = '$';
2510 }
2511 bufcat(sstr, buf, strlen(buf));
2512 free(buf);
d3372da9 2513 }
9d8cf743 2514 } else {
2515 /* Will match all files... :-/ */
2516 addtobuf(sstr, '.');
d3372da9 2517 }
d3372da9 2518 }
2519 addtobuf(sstr, 0);
2520 if(tcpsock != NULL)
2521 {
c8a90770 2522 if(sockgetremotename(fn->sk, &name, &namelen) < 0)
d3372da9 2523 {
2524 flog(LOG_WARNING, "cannot get address of UDP socket");
2525 } else {
9d8cf743 2526 qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr);
d3372da9 2527 free(name);
2528 }
2529 } else {
9d8cf743 2530 qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr);
d3372da9 2531 }
2532 free(sstr);
2533 freesl(&list);
2534 fn->lastsrch = time(NULL);
2535 return(0);
2536}
2537
2538#undef skipcmd
2539#undef qstr
2540#undef qstrf
2541
2542#define cc(c) ((void (*)(struct socket *, void *, char *, char *))(c))
8b930085 2543static struct command hubcmds[] =
d3372da9 2544{
2545 {"$Lock", cc(cmd_lock)},
2546 {"$HubName", cc(cmd_hubname)},
2547 {"$Hello", cc(cmd_hello)},
2548 {"$Quit", cc(cmd_quit)},
2549 {"$NickList", cc(cmd_nicklist)},
2550 {"$OpList", cc(cmd_oplist)},
2551 {"$MyINFO", cc(cmd_myinfo)},
2552 {"$ForceMove", cc(cmd_forcemove)},
2553 {"$Search", cc(cmd_search)},
2554 {"$MultiSearch", cc(cmd_search)},
2555 {"$ConnectToMe", cc(cmd_connecttome)},
2556 {"$RevConnectToMe", cc(cmd_revconnecttome)},
2557 {"$GetNetInfo", cc(cmd_getnetinfo)},
2558 {"$To:", cc(cmd_to)},
2559 {"$SR", cc(cmd_sr)},
2560 {"$UserCommand", cc(cmd_usercommand)},
48509089 2561 {"$GetPass", cc(cmd_getpass)},
c9b3ad74 2562 {"$LogedIn", cc(cmd_logedin)}, /* sic */
d3372da9 2563 {NULL, NULL}
2564};
2565
8b930085 2566static struct command peercmds[] =
d3372da9 2567{
2568 {"$MyNick", cc(cmd_mynick)},
2569 {"$Lock", cc(cmd_peerlock)},
2570 {"$Direction", cc(cmd_direction)},
2571 {"$Key", cc(cmd_key)},
2572 {"$FileLength", cc(cmd_filelength)},
2573 {"$Error", cc(cmd_error)},
2574 {"$MaxedOut", cc(cmd_maxedout)},
2575 {"$Get", cc(cmd_get)},
2576 {"$Send", cc(cmd_send)},
2577 {"$Supports", cc(cmd_supports)},
2578 {"$GetBlock", cc(cmd_getblock)},
2579 {"$UGetBlock", cc(cmd_getblock)},
2580 {"$GetZBlock", cc(cmd_getblock)},
2581 {"$UGetZBlock", cc(cmd_getblock)},
2582 {"$ADCGET", cc(cmd_adcget)},
9a2d6cb8 2583 {"$ADCSND", cc(cmd_adcsnd), 1},
2584 {"$Sending", cc(cmd_sending), 1},
d3372da9 2585 {NULL, NULL}
2586};
2587#undef cc
2588
2589static void dctransdetach(struct transfer *transfer, struct dcpeer *peer)
2590{
2591 CBUNREG(transfer, trans_filterout, peer);
2592 if(peer->freeing)
2593 return;
2594 peer->transfer = NULL;
2595 freedcpeer(peer);
2596}
2597
2598static void dctransgotdata(struct transfer *transfer, struct dcpeer *peer)
2599{
2600 int ret;
2601 void *buf;
c7185019 2602 unsigned char outbuf[1024];
d3372da9 2603 z_stream *cstr;
2604 size_t bufsize;
2605
2606 if((peer->state == PEER_TRNS) || (peer->state == PEER_SYNC))
2607 {
2608 if(sockqueuesize(peer->sk) < 65536)
2609 {
2610 if((buf = transfergetdata(transfer, &bufsize)) != NULL)
2611 {
2612 if(peer->compress == CPRS_NONE)
2613 {
2614 sockqueue(peer->sk, buf, bufsize);
2615 } else if(peer->compress == CPRS_ZLIB) {
2616 cstr = peer->cprsdata;
2617 cstr->next_in = buf;
2618 cstr->avail_in = bufsize;
2619 while(cstr->avail_in > 0)
2620 {
2621 cstr->next_out = outbuf;
2622 cstr->avail_out = sizeof(outbuf);
2623 if((ret = deflate(cstr, 0)) != Z_OK)
2624 {
2625 flog(LOG_WARNING, "bug? deflate() did not return Z_OK (but rather %i)", ret);
2626 freedcpeer(peer);
2627 return;
2628 }
2629 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2630 }
2631 }
2632 free(buf);
2633 }
2634 if(peer->state == PEER_SYNC)
2635 {
2636 if(peer->compress == CPRS_ZLIB)
2637 {
2638 cstr = peer->cprsdata;
2639 cstr->next_in = NULL;
2640 cstr->avail_in = 0;
2641 do
2642 {
2643 cstr->next_out = outbuf;
2644 cstr->avail_out = sizeof(outbuf);
2645 ret = deflate(cstr, Z_FINISH);
2646 if((ret != Z_OK) && (ret != Z_STREAM_END))
2647 {
2648 flog(LOG_WARNING, "bug? deflate(Z_FINISH) did not return Z_OK (but rather %i)", ret);
2649 freedcpeer(peer);
2650 return;
2651 }
2652 sockqueue(peer->sk, outbuf, sizeof(outbuf) - cstr->avail_out);
2653 } while(ret != Z_STREAM_END);
2654 }
2655 if(peer->ptclose)
2656 {
2657 freedcpeer(peer);
2658 } else {
1e9d82b1 2659 if(peer->timeout == NULL)
2660 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
d3372da9 2661 peer->state = PEER_CMD;
2662 endcompress(peer);
2663 transfersetstate(transfer, TRNS_HS);
2664 socksettos(peer->sk, confgetint("fnet", "fnptos"));
bfc261fe 2665 transfer->flags.b.minislot = 0;
d3372da9 2666 peer->sk->writecb = NULL;
2667 }
2668 }
2669 }
2670 }
2671}
2672
2673static void dctransendofdata(struct transfer *transfer, struct dcpeer *peer)
2674{
2675 peer->state = PEER_SYNC;
2676 dctransgotdata(transfer, peer);
2677}
2678
2679static void dcwantdata(struct transfer *transfer, struct dcpeer *peer)
2680{
2681 if(transferdatasize(transfer) < 65536)
2682 peer->sk->ignread = 0;
2683}
2684
2685static void transread(struct socket *sk, struct dcpeer *peer)
2686{
2687 void *buf;
2688 size_t bufsize;
2689 struct transfer *transfer;
2690
2691 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
2692 return;
2693 if(peer->transfer == NULL)
2694 {
2695 free(buf);
2696 freedcpeer(peer);
2697 return;
2698 }
2699 transferputdata(peer->transfer, buf, bufsize);
2700 free(buf);
2701 if(peer->transfer->curpos >= peer->transfer->size)
2702 {
2703 transfer = peer->transfer;
2704 transferdetach(transfer);
2705 transferendofdata(transfer);
2706 return;
2707 }
2708 if(transferdatasize(peer->transfer) > 65535)
2709 sk->ignread = 1;
2710}
2711
2712static void transerr(struct socket *sk, int err, struct dcpeer *peer)
2713{
2714 struct transfer *transfer;
2715
2716 if((transfer = peer->transfer) == NULL)
2717 {
2718 freedcpeer(peer);
2719 return;
2720 }
2721 transferdetach(transfer);
2722 transferendofdata(transfer);
2723}
2724
2725static void transwrite(struct socket *sk, struct dcpeer *peer)
2726{
2727 if((peer->state != PEER_TRNS) && (peer->state != PEER_SYNC))
2728 return;
2729 if(peer->transfer == NULL)
2730 {
2731 freedcpeer(peer);
2732 return;
2733 }
2734 dctransgotdata(peer->transfer, peer);
2735}
2736
2737static void udpread(struct socket *sk, void *data)
2738{
d86b7a9f 2739 char *buf, *p, *p2, *hashbuf;
2740 size_t buflen, hashlen;
d3372da9 2741 char *nick, *filename, *hubname;
99a28d47 2742 struct sockaddr_in hubaddr;
d3372da9 2743 int size, slots;
2744 struct fnetnode *fn, *myfn;
2745 struct dchub *hub;
2746 struct srchres *sr;
2747 wchar_t *wnick, *wfile;
d86b7a9f 2748 struct hash *hash;
d3372da9 2749
2750 if((buf = sockgetinbuf(sk, &buflen)) == NULL)
2751 return;
2752 buf = srealloc(buf, buflen + 1);
2753 buf[buflen] = 0;
2754 if(!strncmp(buf, "$SR ", 4))
2755 {
2756 p = buf + 4;
2757 nick = p;
2758 if((p2 = strchr(p, ' ')) == NULL)
2759 {
2760 free(buf);
2761 return;
2762 }
2763 *p2 = 0;
2764 p = p2 + 1;
2765 filename = p;
2766 if((p2 = strchr(p, 5)) == NULL)
2767 {
2768 free(buf);
2769 return;
2770 }
2771 *p2 = 0;
2772 p = p2 + 1;
2773 if((p2 = strchr(p, ' ')) == NULL)
2774 {
2775 free(buf);
2776 return;
2777 }
2778 *p2 = 0;
2779 size = atoi(p);
2780 p = p2 + 1;
2781 if((p2 = strchr(p, '/')) == NULL)
2782 {
2783 free(buf);
2784 return;
2785 }
2786 *p2 = 0;
2787 slots = atoi(p);
2788 p = p2 + 1;
2789 if((p2 = strchr(p, 5)) == NULL)
2790 {
2791 free(buf);
2792 return;
2793 }
2794 p = p2 + 1;
2795 hubname = p;
2796 if((p2 = strstr(p, " (")) == NULL)
2797 {
2798 free(buf);
2799 return;
2800 }
2801 *p2 = 0;
99a28d47 2802 p = p2 + 2;
2803 if((p2 = strchr(p, ':')) == NULL)
2804 {
2805 free(buf);
2806 return;
2807 }
870bf4ad 2808 *(p2++) = 0;
99a28d47 2809 hubaddr.sin_family = AF_INET;
2810 if(!inet_aton(p, &hubaddr.sin_addr))
2811 {
2812 free(buf);
2813 return;
2814 }
2815 p = p2;
2816 if((p2 = strchr(p, ')')) == NULL)
2817 {
2818 free(buf);
2819 return;
2820 }
2821 *p2 = 0;
2822 hubaddr.sin_port = htons(atoi(p));
1d85f24b 2823 /* Use DCCHARSET in $Get paths until further researched... */
d3372da9 2824 if((wfile = icmbstowcs(filename, DCCHARSET)) == NULL)
2825 {
d3372da9 2826 free(buf);
2827 return;
2828 }
2829 myfn = NULL;
d86b7a9f 2830 hash = NULL;
2831 if(!strncmp(hubname, "TTH:", 4))
d3372da9 2832 {
d86b7a9f 2833 if((hashbuf = base32decode(hubname + 4, &hashlen)) != NULL)
2834 {
2835 if(hashlen == 24)
2836 hash = newhash(L"TTH", 24, hashbuf);
2837 free(hashbuf);
2838 }
2839 } else {
2840 for(fn = fnetnodes; fn != NULL; fn = fn->next)
d3372da9 2841 {
d86b7a9f 2842 if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL))
d3372da9 2843 {
d86b7a9f 2844 if((hub->nativename != NULL) && !strcmp(hub->nativename, hubname))
d3372da9 2845 {
d86b7a9f 2846 if(myfn == NULL)
2847 {
2848 myfn = fn;
2849 } else {
2850 myfn = NULL;
2851 break;
2852 }
d3372da9 2853 }
2854 }
2855 }
2856 }
99a28d47 2857 if(myfn == NULL)
2858 {
2859 for(fn = fnetnodes; fn != NULL; fn = fn->next)
2860 {
39cc1b93 2861 if((fn->fnet == &dcnet) && (fn->sk != NULL) && addreq(fn->sk->remote, (struct sockaddr *)&hubaddr))
99a28d47 2862 {
2863 myfn = fn;
2864 break;
2865 }
2866 }
2867 }
1d85f24b 2868 hub = NULL;
2869 if(myfn != NULL)
2870 hub = myfn->data;
2871 if((wnick = icmbstowcs(nick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
2872 {
2873 free(buf);
2874 return;
2875 }
d3372da9 2876 sr = newsrchres(&dcnet, wfile, wnick);
2877 if(sr->peernick != NULL)
2878 free(sr->peernick);
2879 sr->peernick = swcsdup(wnick);
2880 sr->size = size;
2881 sr->slots = slots;
2882 free(wfile);
2883 free(wnick);
2884 if(myfn != NULL)
2885 getfnetnode(sr->fn = myfn);
d86b7a9f 2886 if(hash != NULL)
2887 sr->hash = hash;
d3372da9 2888 submitsrchres(sr);
2889 freesrchres(sr);
2890 }
2891 free(buf);
2892}
2893
2894static void hubread(struct socket *sk, struct fnetnode *fn)
2895{
2896 struct dchub *hub;
2897 char *newbuf;
2898 size_t datalen;
2899 char *p;
2900
2901 hub = (struct dchub *)fn->data;
2902 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
2903 return;
2904 if(hub->inbufdata > 500000) /* Discard possible malicious data */
2905 hub->inbufdata = 0;
2906 sizebuf2(hub->inbuf, hub->inbufdata + datalen, 1);
2907 memcpy(hub->inbuf + hub->inbufdata, newbuf, datalen);
2908 free(newbuf);
2909 p = hub->inbuf + hub->inbufdata;
2910 hub->inbufdata += datalen;
2911 while((datalen > 0) && ((p = memchr(p, '|', datalen)) != NULL))
2912 {
2913 *(p++) = 0;
2914 newqcmd(&hub->queue, hub->inbuf);
2915 memmove(hub->inbuf, p, hub->inbufdata -= p - hub->inbuf);
2916 datalen = hub->inbufdata;
2917 p = hub->inbuf;
2918 }
2919}
2920
2921static void huberr(struct socket *sk, int err, struct fnetnode *fn)
2922{
2923 killfnetnode(fn);
2924}
2925
2926static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
2927{
2928 struct dchub *hub;
2929 char *buf;
2930
2931 hub = fn->data;
1d85f24b 2932 if((buf = icwcstombs(newnick, (hub == NULL)?DCCHARSET:(hub->charset))) == NULL)
d3372da9 2933 return(1);
2934 if((strchr(buf, ' ') != NULL) || (strchr(buf, '|') != NULL) || (strchr(buf, '$') != NULL))
2935 {
2936 free(buf);
2937 return(1);
2938 }
2939 if(hub == NULL) /* Not yet connected */
2940 {
2941 free(buf);
2942 return(0);
2943 }
2944 if(hub->nativenick != NULL)
2945 free(hub->nativenick);
2946 hub->nativenick = buf;
2947 return(0);
2948}
2949
2950static struct dchub *newdchub(struct fnetnode *fn)
2951{
2952 struct dchub *new;
1c4b02cd 2953 wchar_t *emu;
1d85f24b 2954 wchar_t *wcharset;
2955 char *charset;
d3372da9 2956
2957 new = smalloc(sizeof(*new));
2958 memset(new, 0, sizeof(*new));
2959 fn->data = new;
1c4b02cd 2960 if(confgetint("dc", "dcppemu"))
2961 new->dcppemu = 1;
2962 if((emu = wpfind(fn->args, L"dcppemu")) != NULL)
2963 {
2964 if(*emu == L'y')
2965 new->dcppemu = 1;
2966 if(*emu == L'n')
2967 new->dcppemu = 0;
2968 }
1d85f24b 2969 charset = NULL;
2970 if((wcharset = wpfind(fn->args, L"charset")) != NULL)
2971 {
2972 if((charset = icwcstombs(wcharset, "US-ASCII")) != NULL)
2973 {
2974 if(!havecharset(charset))
2975 {
2976 free(charset);
2977 charset = NULL;
2978 }
2979 }
2980 }
2981 if(charset != NULL)
2982 new->charset = charset;
2983 else
2984 new->charset = sstrdup(DCCHARSET);
d3372da9 2985 if(hubsetnick(fn, fn->mynick))
2986 fnetsetnick(fn, L"DoldaConnectUser-IN");
2987 /* IN as in Invalid Nick */
2988 return(new);
2989}
2990
2991static struct dcpeer *newdcpeer(struct socket *sk)
2992{
2993 struct dcpeer *new;
2994
2995 new = smalloc(sizeof(*new));
2996 memset(new, 0, sizeof(*new));
2997 new->transfer = NULL;
2998 getsock(sk);
2999 new->sk = sk;
1c4b02cd 3000 if(confgetint("dc", "dcppemu"))
3001 new->dcppemu = 1;
d3372da9 3002 new->next = peers;
3003 new->prev = NULL;
3004 if(peers != NULL)
3005 peers->prev = new;
3006 peers = new;
3007 numdcpeers++;
3008 return(new);
3009}
3010
3011static void freedcpeer(struct dcpeer *peer)
3012{
3013 int i;
3014 struct qcommand *qcmd;
3015
3016 peer->freeing = 1;
3017 if(peers == peer)
3018 peers = peer->next;
3019 if(peer->next != NULL)
3020 peer->next->prev = peer->prev;
3021 if(peer->prev != NULL)
3022 peer->prev->next = peer->next;
3023 if(peer->transfer != NULL)
3024 {
3025 if(peer->transfer->dir == TRNSD_UP)
3026 peer->transfer->close = 1;
3027 if(peer->transfer->dir == TRNSD_DOWN)
3028 resettransfer(peer->transfer);
3029 transferdetach(peer->transfer);
3030 }
1e9d82b1 3031 if(peer->timeout != NULL)
3032 canceltimer(peer->timeout);
d3372da9 3033 if(peer->sk->data == peer)
3034 peer->sk->data = NULL;
3035 peer->sk->readcb = NULL;
3036 peer->sk->writecb = NULL;
3037 peer->sk->errcb = NULL;
3038 putsock(peer->sk);
3039 endcompress(peer);
3040 if(peer->supports != NULL)
3041 {
3042 for(i = 0; peer->supports[i] != NULL; i++)
3043 free(peer->supports[i]);
3044 free(peer->supports);
3045 }
d3372da9 3046 if(peer->inbuf != NULL)
3047 free(peer->inbuf);
3048 if(peer->key != NULL)
3049 free(peer->key);
3050 if(peer->wcsname != NULL)
3051 free(peer->wcsname);
3052 if(peer->nativename != NULL)
3053 free(peer->nativename);
1d85f24b 3054 if(peer->charset != NULL)
3055 free(peer->charset);
d3372da9 3056 if(peer->fn != NULL)
3057 putfnetnode(peer->fn);
3058 while((qcmd = ulqcmd(&peer->queue)) != NULL)
3059 freeqcmd(qcmd);
3060 free(peer);
3061 numdcpeers--;
3062}
3063
3064static void hubconnect(struct fnetnode *fn)
3065{
3066 fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
3067 fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
3068 getfnetnode(fn);
3069 fn->data = newdchub(fn);
3070 fn->sk->data = fn;
3071 return;
3072}
3073
3074static void hubdestroy(struct fnetnode *fn)
3075{
3076 struct dchub *hub;
3077 struct qcommand *qcmd;
3078
3079 hub = (struct dchub *)fn->data;
9d131372 3080 if((fn->sk != NULL) && (fn->sk->data == fn))
d3372da9 3081 {
9d131372 3082 fn->sk->data = NULL;
3083 putfnetnode(fn);
d3372da9 3084 }
3085 if(hub == NULL)
3086 return;
3087 while((qcmd = ulqcmd(&hub->queue)) != NULL)
3088 freeqcmd(qcmd);
3089 if(hub->nativename != NULL)
3090 free(hub->nativename);
3091 if(hub->nativenick != NULL)
3092 free(hub->nativenick);
1d85f24b 3093 if(hub->charset != NULL)
3094 free(hub->charset);
d3372da9 3095 if(hub->inbuf != NULL)
3096 free(hub->inbuf);
3097 free(hub);
3098}
3099
3100static wchar_t *dcbasename(wchar_t *filename)
3101{
3102 wchar_t *ret;
3103
3104 if((ret = wcsrchr(filename, L'\\')) != NULL)
3105 return(ret + 1);
3106 return(filename);
3107}
3108
3109static struct transferiface dctransfer =
3110{
3111 .detach = (void (*)(struct transfer *, void *))dctransdetach,
3112 .gotdata = (void (*)(struct transfer *, void *))dctransgotdata,
3113 .endofdata = (void (*)(struct transfer *, void *))dctransendofdata,
3114 .wantdata = (void (*)(struct transfer *, void *))dcwantdata
3115};
3116
3117static struct fnet dcnet =
3118{
3119 .name = L"dc",
3120 .connect = hubconnect,
3121 .destroy = hubdestroy,
3122 .setnick = hubsetnick,
3123 .reqconn = hubreqconn,
3124 .sendchat = hubsendchat,
3125 .search = hubsearch,
3126 .filebasename = dcbasename
3127};
3128
3129static void peerread(struct socket *sk, struct dcpeer *peer)
3130{
3131 char *newbuf, *p;
3132 size_t datalen;
9a2d6cb8 3133 struct command *cmd;
d3372da9 3134
3135 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
3136 return;
3137 sizebuf2(peer->inbuf, peer->inbufdata + datalen, 1);
3138 memcpy(peer->inbuf + peer->inbufdata, newbuf, datalen);
3139 free(newbuf);
d3372da9 3140 peer->inbufdata += datalen;
9a2d6cb8 3141 if(peer->state == PEER_CMD)
d3372da9 3142 {
d3372da9 3143 p = peer->inbuf;
9a2d6cb8 3144 while((peer->inbufdata > 0) && (p = memchr(peer->inbuf, '|', peer->inbufdata)) != NULL)
3145 {
3146 *(p++) = 0;
3147 newqcmd(&peer->queue, peer->inbuf);
3148 for(cmd = peercmds; cmd->handler != NULL; cmd++)
3149 {
5eee9d66 3150 if(!memcmp(peer->inbuf, cmd->name, strlen(cmd->name)) && ((peer->inbuf[strlen(cmd->name)] == ' ') || (peer->inbuf[strlen(cmd->name)] == '|')))
9a2d6cb8 3151 break;
3152 }
5eee9d66 3153 memmove(peer->inbuf, p, peer->inbufdata -= p - peer->inbuf);
9a2d6cb8 3154 if(cmd->stop)
3155 {
3156 peer->state = PEER_STOP;
3157 break;
3158 }
9a2d6cb8 3159 }
3160 } else if(peer->state == PEER_TTHL) {
3161 handletthl(peer);
d3372da9 3162 }
3163}
3164
3165static void peererror(struct socket *sk, int err, struct dcpeer *peer)
3166{
3167 freedcpeer(peer);
3168}
3169
3170static void peerconnect(struct socket *sk, int err, struct fnetnode *fn)
3171{
3172 struct dcpeer *peer;
1c4b02cd 3173 struct dchub *hub;
d3372da9 3174
3175 if(err != 0)
3176 {
3177 putfnetnode(fn);
ff625695 3178 putsock(sk);
d3372da9 3179 return;
3180 }
1c4b02cd 3181 hub = fn->data;
d3372da9 3182 peer = newdcpeer(sk);
3183 peer->fn = fn;
3184 peer->accepted = 0;
1c4b02cd 3185 peer->dcppemu = hub->dcppemu;
d3372da9 3186 sk->readcb = (void (*)(struct socket *, void *))peerread;
3187 sk->errcb = (void (*)(struct socket *, int, void *))peererror;
3188 sk->data = peer;
3189 socksettos(sk, confgetint("fnet", "fnptos"));
d3372da9 3190 putsock(sk);
1e9d82b1 3191 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
9a2d6cb8 3192 sendmynick(peer);
3193 sendpeerlock(peer);
d3372da9 3194}
3195
3196static void peeraccept(struct socket *sk, struct socket *newsk, void *data)
3197{
3198 struct dcpeer *peer;
3199
3200 peer = newdcpeer(newsk);
3201 peer->accepted = 1;
3202 newsk->readcb = (void (*)(struct socket *, void *))peerread;
3203 newsk->errcb = (void (*)(struct socket *, int, void *))peererror;
3204 newsk->data = peer;
3205 socksettos(newsk, confgetint("fnet", "fnptos"));
1e9d82b1 3206 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
d3372da9 3207}
3208
3209static void updatehmlist(void)
3210{
3211 int i, lev, ic, ret;
3212 struct sharecache *node;
3213 char *buf, *buf2, numbuf[32];
3214 size_t bufsize, bufdata;
3215 int fd, ibuf;
3216
3217 bufdata = 0;
3218 buf = smalloc(bufsize = 65536);
3219 node = shareroot->child;
3220 lev = 0;
3221 while(1)
3222 {
3223 ic = 0;
1d85f24b 3224 /* Use DCCHARSET in $Get paths until further researched... */
d3372da9 3225 if((buf2 = icwcstombs(node->name, DCCHARSET)) != NULL)
3226 {
3227 for(i = 0; i < lev; i++)
3228 addtobuf(buf, 9);
3229 bufcat(buf, buf2, strlen(buf2));
3230 free(buf2);
3231 if(node->f.b.type == FILE_REG)
3232 {
3233 addtobuf(buf, '|');
3234 sprintf(numbuf, "%i", node->size);
3235 bufcat(buf, numbuf, strlen(numbuf));
3236 }
3237 addtobuf(buf, 13);
3238 addtobuf(buf, 10);
3239 } else {
3240 ic = 1;
3241 }
3242 if((node->child != NULL) && !ic)
3243 {
3244 lev++;
3245 node = node->child;
3246 } else if(node->next != NULL) {
3247 node = node->next;
3248 } else {
3249 while(node->next == NULL)
3250 {
3251 lev--;
3252 node = node->parent;
3253 if(node == shareroot)
3254 break;
3255 }
3256 if(node == shareroot)
3257 break;
3258 node = node->next;
3259 }
3260 }
3261 if(hmlistname != NULL)
3262 {
3263 unlink(hmlistname);
3264 free(hmlistname);
3265 }
3266 hmlistname = sstrdup("/tmp/dc-filelist-hm-XXXXXX");
3267 if((fd = mkstemp(hmlistname)) < 0)
3268 {
3269 flog(LOG_WARNING, "could not create HM file list tempfile: %s", strerror(errno));
3270 free(hmlistname);
3271 hmlistname = NULL;
3272 } else {
3273 /*
3274 * I do not want to implement a good Huffman encoder, and it's not
3275 * like Huffman encoding actually yields any impressive results
3276 * for DC file lists anyway, so I'll just output a bogus
3277 * tree. Implement a good encoder if you want to.
3278 */
3279 write(fd, "HE3\r\0", 5);
3280 write(fd, &bufdata, 4);
3281 ibuf = 256;
3282 write(fd, &ibuf, 2);
3283 ibuf = 8;
3284 for(i = 0; i < 256; i++)
3285 {
3286 write(fd, &i, 1);
3287 write(fd, &ibuf, 1);
3288 }
3289 for(i = 0; i < 256; i++)
3290 write(fd, &i, 1);
3291 for(buf2 = buf; bufdata > 0;)
3292 {
3293 if((ret = write(fd, buf2, bufdata)) < 0)
3294 {
3295 flog(LOG_WARNING, "could not write file list: %s", strerror(errno));
3296 break;
3297 }
3298 bufdata -= ret;
3299 buf2 += ret;
3300 }
3301 close(fd);
3302 }
3303 free(buf);
3304}
3305
3306static struct xmlent
3307{
3308 wchar_t c;
3309 wchar_t *ent;
3310} entities[] = {
3311 {L'&', L"&amp;"},
3312 {L'\"', L"&#34;"},
3313/* {L'\'', L"&quot;"}, Emulate DC++ escaping by omitting this. */
3314 {L'\0', NULL}
3315};
3316
3317static wchar_t *escapexml(wchar_t *src)
3318{
3319 static wchar_t *buf = NULL;;
3320 int ret, c;
3321 wchar_t nbuf[32];
3322 size_t bufsize, bufdata;
3323 struct xmlent *ent;
3324
3325 if(buf != NULL)
3326 free(buf);
3327 buf = NULL;
3328 bufsize = bufdata = 0;
3329 for(; *src != L'\0'; src++)
3330 {
3331 c = wctob(*src);
3332 if((c > 0) && (c < 32))
3333 {
3334 bufcat(buf, L"&#", 2);
3335 ret = swprintf(nbuf, (sizeof(nbuf) / sizeof(*nbuf)), L"%i", c);
3336 bufcat(buf, nbuf, ret);
3337 addtobuf(buf, L';');
3338 } else {
3339 for(ent = entities; ent->ent != NULL; ent++)
3340 {
3341 if(ent->c == *src)
3342 {
3343 bufcat(buf, ent->ent, wcslen(ent->ent));
3344 break;
3345 }
3346 }
3347 if(ent->ent == NULL)
3348 addtobuf(buf, *src);
3349 }
3350 }
3351 addtobuf(buf, L'\0');
3352 return(buf);
3353}
3354
3355static void updatexmllist(void)
3356{
3357 int i, fd, lev;
3358 FILE *fs;
3359 char cidbuf[14], *namebuf;
3360 char *hashbuf;
3361 struct sharecache *node;
3362
3363 if(xmllistname != NULL)
3364 {
3365 unlink(xmllistname);
3366 free(xmllistname);
3367 }
3368 xmllistname = sstrdup("/tmp/dc-filelist-dcxml-XXXXXX");
3369 if((fd = mkstemp(xmllistname)) < 0)
3370 {
3371 flog(LOG_WARNING, "could not create XML file list tempfile: %s", strerror(errno));
3372 free(xmllistname);
3373 xmllistname = NULL;
3374 return;
3375 }
3376 if((fs = fdopen(fd, "w")) == NULL)
3377 {
3378 flog(LOG_WARNING, "could not fdopen XML list fd %i: %s", fd, strerror(errno));
3379 unlink(xmllistname);
3380 free(xmllistname);
3381 xmllistname = NULL;
3382 close(fd);
3383 return;
3384 }
3385 fprintf(fs, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n");
3386 for(i = 0; i < sizeof(cidbuf) - 1; i++)
3387 cidbuf[i] = (rand() % ('Z' - 'A' + 1)) + 'A';
3388 cidbuf[i] = 0;
1c4b02cd 3389 if(confgetint("dc", "dcppemu"))
3390 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"DC++ 0.674\">\r\n", cidbuf);
3391 else
3392 fprintf(fs, "<FileListing Version=\"1\" CID=\"%s\" Base=\"/\" Generator=\"%s\">\r\n", cidbuf, "DoldaConnect" VERSION);
d3372da9 3393
3394 node = shareroot->child;
3395 lev = 0;
3396 while(1)
3397 {
3398 if((namebuf = icswcstombs(escapexml(node->name), "UTF-8", NULL)) != NULL)
3399 {
3400 for(i = 0; i < lev; i++)
3401 fputc('\t', fs);
3402 if(node->child != NULL)
3403 {
3404 fprintf(fs, "<Directory Name=\"%s\">\r\n", namebuf);
3405 node = node->child;
3406 lev++;
3407 continue;
3408 } else {
3409 fprintf(fs, "<File Name=\"%s\" Size=\"%i\"", namebuf, node->size);
3410 if(node->f.b.hastth)
3411 {
3412 hashbuf = base32encode(node->hashtth, 24);
3413 fprintf(fs, " TTH=\"%.39s\"", hashbuf);
3414 free(hashbuf);
3415 }
3416 fprintf(fs, "/>\r\n");
3417 }
3418 while(node->next == NULL)
3419 {
3420 node = node->parent;
3421 if(node == shareroot)
3422 {
3423 break;
3424 } else {
3425 lev--;
3426 for(i = 0; i < lev; i++)
3427 fputc('\t', fs);
3428 fprintf(fs, "</Directory>\r\n");
3429 }
3430 }
3431 if(node == shareroot)
3432 break;
3433 node = node->next;
3434 }
3435 }
3436
1c4b02cd 3437 if(confgetint("dc", "dcppemu"))
3438 fprintf(fs, "</FileListing>");
3439 else
3440 fprintf(fs, "</FileListing>\r\n");
d3372da9 3441 fclose(fs);
3442}
3443
3444static void updatexmlbz2list(void)
3445{
3446 int err, fd;
3447 FILE *in;
3448 FILE *real;
3449 BZFILE *out;
3450 char buf[1024];
3451 size_t bufdata;
3452
3453 if(xmllistname == NULL)
3454 return;
3455 if((in = fopen(xmllistname, "r")) == NULL)
3456 {
3457 flog(LOG_WARNING, "could not open XML file list for bzipping: %s", strerror(errno));
3458 return;
3459 }
3460 if(xmlbz2listname != NULL)
3461 {
3462 unlink(xmlbz2listname);
3463 free(xmlbz2listname);
3464 }
3465 xmlbz2listname = sstrdup("/tmp/dc-filelist-dcxmlbz2-XXXXXX");
3466 if((fd = mkstemp(xmlbz2listname)) < 0)
3467 {
3468 flog(LOG_WARNING, "could not create bzipped XML file list tempfile: %s", strerror(errno));
3469 free(xmlbz2listname);
3470 xmlbz2listname = NULL;
3471 fclose(in);
3472 return;
3473 }
3474 if((real = fdopen(fd, "w")) == NULL)
3475 {
3476 flog(LOG_WARNING, "could not fdopen bzipped XML list fd %i: %s", fd, strerror(errno));
3477 close(fd);
3478 unlink(xmlbz2listname);
3479 free(xmlbz2listname);
3480 xmlbz2listname = NULL;
3481 fclose(in);
3482 return;
3483 }
3484 out = BZ2_bzWriteOpen(&err, real, 9, 0, 0);
3485 if(err != BZ_OK)
3486 {
3487 flog(LOG_WARNING, "could not open bzip2 stream from XML list");
3488 fclose(real);
3489 unlink(xmlbz2listname);
3490 free(xmlbz2listname);
3491 xmlbz2listname = NULL;
3492 fclose(in);
3493 return;
3494 }
3495 while(!feof(in))
3496 {
3497 bufdata = fread(buf, 1, sizeof(buf), in);
3498 BZ2_bzWrite(&err, out, buf, bufdata);
3499 }
3500 fclose(in);
3501 BZ2_bzWriteClose(&err, out, 0, NULL, NULL);
3502 fclose(real);
3503}
3504
3505static int shareupdate(unsigned long long uusharesize, void *data)
3506{
3507 updatehmlist();
3508 updatexmllist();
3509 updatexmlbz2list();
3510 return(0);
3511}
3512
3513static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data)
3514{
3515 char *p;
3516 struct command *cmd;
3517
3518 if((p = strchr(qcmd->string, ' ')) != NULL)
3519 *(p++) = 0;
3520 for(cmd = cmdlist; cmd->handler != NULL; cmd++)
3521 {
3522 if(!strcmp(cmd->name, qcmd->string))
3523 break;
3524 }
3525 if(cmd->handler != NULL)
3526 cmd->handler(sk, data, qcmd->string, p);
3527/*
3528 else
3529 flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs");
3530*/
3531}
3532
3533static int run(void)
3534{
3535 struct fnetnode *fn, *nextfn;
3536 struct dchub *hub;
3537 struct dcpeer *peer, *nextpeer;
3538 struct qcommand *qcmd;
3539 int ret;
3540
3541 ret = 0;
3542 for(fn = fnetnodes; fn != NULL; fn = nextfn)
3543 {
3544 nextfn = fn->next;
3545 if(fn->fnet != &dcnet)
3546 continue;
3547 if(fn->data == NULL)
3548 continue;
3549 hub = (struct dchub *)fn->data;
3550 if((qcmd = ulqcmd(&hub->queue)) != NULL)
3551 {
3552 if(*qcmd->string == '$')
3553 {
3554 if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
3555 dispatchcommand(qcmd, hubcmds, fn->sk, fn);
3556 } else if(*qcmd->string != 0) {
3557 hubrecvchat(fn->sk, fn, NULL, qcmd->string);
3558 }
3559 freeqcmd(qcmd);
3560 ret = 1;
3561 break;
3562 }
3563 }
3564 for(peer = peers; peer != NULL; peer = nextpeer)
3565 {
3566 nextpeer = peer->next;
3567 if((qcmd = ulqcmd(&peer->queue)) != NULL)
3568 {
1e9d82b1 3569 if(peer->timeout != NULL)
3570 canceltimer(peer->timeout);
3571 peer->timeout = timercallback(ntime() + 180, (void (*)(int, void *))peertimeout, peer);
d3372da9 3572 if(*qcmd->string == '$')
3573 dispatchcommand(qcmd, peercmds, peer->sk, peer);
3574 freeqcmd(qcmd);
3575 ret = 1;
3576 break;
3577 }
3578 }
3579 return(ret);
3580}
3581
3582static void preinit(int hup)
3583{
3584 if(hup)
3585 return;
3586 regfnet(&dcnet);
3587}
3588
3589static int updateudpport(struct configvar *var, void *uudata)
3590{
3591 struct sockaddr_in addr;
3592 struct socket *newsock;
3593
3594 memset(&addr, 0, sizeof(addr));
3595 addr.sin_family = AF_INET;
3596 addr.sin_port = htons(var->val.num);
3597 if((newsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3598 {
3599 flog(LOG_WARNING, "could not create new DC UDP socket, reverting to old: %s", strerror(errno));
3600 return(0);
3601 }
3602 newsock->readcb = udpread;
3603 if(udpsock != NULL)
3604 putsock(udpsock);
3605 udpsock = newsock;
3606 return(0);
3607}
3608
3609static int updatetcpport(struct configvar *var, void *uudata)
3610{
3611 struct sockaddr_in addr;
3612 struct socket *newsock;
3613
3614 memset(&addr, 0, sizeof(addr));
3615 addr.sin_family = AF_INET;
3616 addr.sin_port = htons(var->val.num);
3617 if((newsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3618 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3619 if(tcpsock != NULL)
3620 putsock(tcpsock);
3621 tcpsock = newsock;
3622 return(0);
3623}
3624
3625static int init(int hup)
3626{
3627 struct sockaddr_in addr;
3628
3629 if(!hup)
3630 {
3631 GCBREG(sharechangecb, shareupdate, NULL);
3632 if(udpsock != NULL)
3633 putsock(udpsock);
3634 if(tcpsock != NULL)
3635 putsock(tcpsock);
3636 addr.sin_family = AF_INET;
3637 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
3638 addr.sin_port = htons(confgetint("dc", "udpport"));
3639 if((udpsock = netcsdgram((struct sockaddr *)&addr, sizeof(addr))) == NULL)
3640 {
3641 flog(LOG_CRIT, "could not create DC UDP socket: %s", strerror(errno));
3642 return(1);
3643 }
3644 udpsock->readcb = udpread;
3645 addr.sin_port = htons(confgetint("dc", "tcpport"));
3646 if((tcpsock = netcslisten(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), peeraccept, NULL)) == NULL)
3647 flog(LOG_INFO, "could not listen to a remote address, going into passive mode");
3648 CBREG(confgetvar("dc", "udpport"), conf_update, updateudpport, NULL, NULL);
3649 CBREG(confgetvar("dc", "tcpport"), conf_update, updatetcpport, NULL, NULL);
3650 CBREG(confgetvar("net", "mode"), conf_update, updatetcpport, NULL, NULL);
3651 }
3652 return(0);
3653}
3654
3655static void terminate(void)
3656{
3657 if(hmlistname != NULL)
3658 {
3659 unlink(hmlistname);
3660 free(hmlistname);
3661 }
3662 if(xmllistname != NULL)
3663 {
3664 unlink(xmllistname);
3665 free(xmllistname);
3666 }
3667 if(xmlbz2listname != NULL)
3668 {
3669 unlink(xmlbz2listname);
3670 free(xmlbz2listname);
3671 }
3672}
3673
3674static struct configvar myvars[] =
3675{
3676 {CONF_VAR_STRING, "desc", {.str = L""}},
3677 {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}},
3678 {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}},
3679 {CONF_VAR_INT, "udpport", {.num = 0}},
3680 {CONF_VAR_INT, "tcpport", {.num = 0}},
1c4b02cd 3681 {CONF_VAR_BOOL, "dcppemu", {.num = 0}},
d3372da9 3682 {CONF_VAR_END}
3683};
3684
3685static struct module me =
3686{
3687 .conf =
3688 {
3689 .vars = myvars
3690 },
3691 .preinit = preinit,
3692 .init = init,
3693 .run = run,
3694 .terminate = terminate,
3695 .name = "dc"
3696};
3697
3698MODULE(me)