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