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