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