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