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