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