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