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