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