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