annotate src/gt_search.c @ 0:d39e1d0d75b6

initial add
author paulo@hit-nxdomain.opendns.com
date Sat, 20 Feb 2010 21:18:28 -0800
parents
children
rev   line source
paulo@0 1 /*
paulo@0 2 * $Id: gt_search.c,v 1.60 2004/11/29 12:32:12 mkern Exp $
paulo@0 3 *
paulo@0 4 * Copyright (C) 2001-2003 giFT project (gift.sourceforge.net)
paulo@0 5 *
paulo@0 6 * This program is free software; you can redistribute it and/or modify it
paulo@0 7 * under the terms of the GNU General Public License as published by the
paulo@0 8 * Free Software Foundation; either version 2, or (at your option) any
paulo@0 9 * later version.
paulo@0 10 *
paulo@0 11 * This program is distributed in the hope that it will be useful, but
paulo@0 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
paulo@0 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
paulo@0 14 * General Public License for more details.
paulo@0 15 */
paulo@0 16
paulo@0 17 #include "gt_gnutella.h"
paulo@0 18
paulo@0 19 #include "gt_node.h"
paulo@0 20 #include "gt_node_list.h"
paulo@0 21 #include "gt_share.h"
paulo@0 22 #include "gt_share_file.h"
paulo@0 23 #include "gt_packet.h"
paulo@0 24 #include "gt_search.h"
paulo@0 25 #include "gt_xfer.h"
paulo@0 26
paulo@0 27 #include "sha1.h"
paulo@0 28
paulo@0 29 #include "encoding/url.h" /* gt_url_decode */
paulo@0 30
paulo@0 31 #include "transfer/download.h"
paulo@0 32 #include "transfer/source.h"
paulo@0 33
paulo@0 34 #include <libgift/mime.h>
paulo@0 35
paulo@0 36 /******************************************************************************/
paulo@0 37
paulo@0 38 /* how often we check if the search has timed out */
paulo@0 39 #define TIMEOUT_CHECK_INTERVAL (20 * SECONDS)
paulo@0 40
paulo@0 41 /* after this many results, no more search submissions will occur */
paulo@0 42 #define RESULTS_BACKOFF (200)
paulo@0 43
paulo@0 44 /*
paulo@0 45 * Gnutella searches don't notify when they are done. So, we close the
paulo@0 46 * search after the following critieria are met:
paulo@0 47 *
paulo@0 48 * - we have submitted the search to at least 3 Ultrapeers
paulo@0 49 * [MIN_NODES]
paulo@0 50 * - at least 3 minutes have passed since we last submitted to an ultrapeer
paulo@0 51 * [MIN_SUBMIT_WAIT]
paulo@0 52 * - no results have been seen in the last minute
paulo@0 53 * [MIN_RESULT_WAIT]
paulo@0 54 *
paulo@0 55 * This means the fastest we'll time out a search is 3 minutes if
paulo@0 56 * we submit to 3 ultrapeers immediately and get no results within
paulo@0 57 * 1 minute of the 3 minute time limit.
paulo@0 58 *
paulo@0 59 * For hash searches, we wait for 2 * MIN_SUBMIT_WAIT, because the other
paulo@0 60 * factors won't come into play.
paulo@0 61 *
paulo@0 62 * There is also a large timeout for searches that receive no results
paulo@0 63 * [ANCIENT_TIME]. Searches that exceed this age and haven't received
paulo@0 64 * any results in the same time will automatically be cancelled, regardless of
paulo@0 65 * other critieria.
paulo@0 66 */
paulo@0 67 #define MIN_NODES (3) /* ultrapeers */
paulo@0 68 #define MIN_SUBMIT_WAIT (3 * EMINUTES)
paulo@0 69 #define MIN_RESULT_WAIT (1 * EMINUTES)
paulo@0 70 #define ANCIENT_TIME (10 * EMINUTES)
paulo@0 71
paulo@0 72 /******************************************************************************/
paulo@0 73
paulo@0 74 /* active keyword and hash searches from this node */
paulo@0 75 static List *active_searches;
paulo@0 76
paulo@0 77 /* probability of the next hash search not being dropped */
paulo@0 78 static double locate_pass_prob;
paulo@0 79
paulo@0 80 /******************************************************************************/
paulo@0 81
paulo@0 82 static BOOL finish_search (GtSearch *search)
paulo@0 83 {
paulo@0 84 GT->DBGFN (GT, "search query for \"%s\" timed out", search->query);
paulo@0 85 gt_search_free (search);
paulo@0 86 return FALSE;
paulo@0 87 }
paulo@0 88
paulo@0 89 static BOOL search_is_ancient (GtSearch *search, time_t now)
paulo@0 90 {
paulo@0 91 if (difftime (now, search->start) < ANCIENT_TIME)
paulo@0 92 return FALSE;
paulo@0 93
paulo@0 94 /*
paulo@0 95 * If the search is greater than ANCIENT_TIME and hasn't received
paulo@0 96 * a result in the same time, consider it ancient.
paulo@0 97 */
paulo@0 98 if (search->last_result == 0)
paulo@0 99 return TRUE;
paulo@0 100
paulo@0 101 if (difftime (now, search->last_result) >= ANCIENT_TIME)
paulo@0 102 return TRUE;
paulo@0 103
paulo@0 104 return FALSE;
paulo@0 105 }
paulo@0 106
paulo@0 107 /*
paulo@0 108 * search_timeout: check if the search needs to be closed.
paulo@0 109 *
paulo@0 110 * Its impossible to guarantee this will not close the search too early.
paulo@0 111 * It is more likely to miss results if bandwidth is being dedicated to
paulo@0 112 * other purposes besides reading Gnutella messages, or if the TTL and
paulo@0 113 * consequently the latency of the search is high.
paulo@0 114 *
paulo@0 115 * TODO: this should take into account that we may have disconnected
paulo@0 116 * from the nodes we submitted the search to. Perhaps, have
paulo@0 117 * a list of the submitted nodes, and make sure the list len >=
paulo@0 118 * MIN_NODES (but this may run into trouble with not submitting
paulo@0 119 * searches with results >= RESULTS_BACKOFF...)
paulo@0 120 */
paulo@0 121 static BOOL search_timeout (GtSearch *search)
paulo@0 122 {
paulo@0 123 time_t now;
paulo@0 124 double submit_wait;
paulo@0 125 double result_wait;
paulo@0 126
paulo@0 127 time (&now);
paulo@0 128
paulo@0 129 /* check if this search is really old and should be expired */
paulo@0 130 if (search_is_ancient (search, now))
paulo@0 131 return finish_search (search);
paulo@0 132
paulo@0 133 if (search->submitted < MIN_NODES)
paulo@0 134 return TRUE;
paulo@0 135
paulo@0 136 submit_wait = MIN_SUBMIT_WAIT;
paulo@0 137 result_wait = MIN_RESULT_WAIT;
paulo@0 138
paulo@0 139 /* hash searches get very few results, so give them a longer base time */
paulo@0 140 if (search->type == GT_SEARCH_HASH)
paulo@0 141 submit_wait *= 2;
paulo@0 142
paulo@0 143 /*
paulo@0 144 * If the search has lots of results, don't wait as long.
paulo@0 145 *
paulo@0 146 * RESULTS_BACKOFF is a conservative value for not submitting to other
paulo@0 147 * nodes when we already have plenty of results, and we want to be a
paulo@0 148 * little less conservative here, so multiply RESULTS_BACKOFF by 2.
paulo@0 149 */
paulo@0 150 if (search->results >= 2 * RESULTS_BACKOFF)
paulo@0 151 {
paulo@0 152 submit_wait /= 2;
paulo@0 153 result_wait /= 2;
paulo@0 154 }
paulo@0 155
paulo@0 156 if (difftime (now, search->last_submit) < submit_wait)
paulo@0 157 return TRUE;
paulo@0 158
paulo@0 159 if (difftime (now, search->last_result) < result_wait)
paulo@0 160 return TRUE;
paulo@0 161
paulo@0 162 /* the search has timed out */
paulo@0 163 return finish_search (search);
paulo@0 164 }
paulo@0 165
paulo@0 166 /*****************************************************************************/
paulo@0 167
paulo@0 168 GtSearch *gt_search_new (IFEvent *event, char *query, gt_search_type_t type)
paulo@0 169 {
paulo@0 170 GtSearch *search;
paulo@0 171
paulo@0 172 if (!(search = malloc (sizeof (GtSearch))))
paulo@0 173 return NULL;
paulo@0 174
paulo@0 175 memset (search, 0, sizeof (GtSearch));
paulo@0 176
paulo@0 177 search->event = event;
paulo@0 178 search->type = type;
paulo@0 179 search->guid = gt_guid_new ();
paulo@0 180 search->query = STRDUP (query);
paulo@0 181 search->results = 0;
paulo@0 182 search->start = time (NULL);
paulo@0 183
paulo@0 184 search->timeout_timer = timer_add (TIMEOUT_CHECK_INTERVAL,
paulo@0 185 (TimerCallback)search_timeout,
paulo@0 186 search);
paulo@0 187
paulo@0 188 GT->DBGFN (GT, "new search \"%s\"", query);
paulo@0 189
paulo@0 190 active_searches = list_prepend (active_searches, search);
paulo@0 191
paulo@0 192 return search;
paulo@0 193 }
paulo@0 194
paulo@0 195 void gt_search_free (GtSearch *search)
paulo@0 196 {
paulo@0 197 if (!search)
paulo@0 198 return;
paulo@0 199
paulo@0 200 if (!list_find (active_searches, search))
paulo@0 201 {
paulo@0 202 GIFT_ERROR (("couldn't find search %p (query:'%s')",
paulo@0 203 search, search->query));
paulo@0 204 return;
paulo@0 205 }
paulo@0 206
paulo@0 207 if (search->timeout_timer)
paulo@0 208 timer_remove (search->timeout_timer);
paulo@0 209
paulo@0 210 if (search->event)
paulo@0 211 GT->search_complete (GT, search->event);
paulo@0 212
paulo@0 213 /* NOTE: search_complete may have removed the search by calling
paulo@0 214 * gt_search_disable */
paulo@0 215 active_searches = list_remove (active_searches, search);
paulo@0 216
paulo@0 217 free (search->hash);
paulo@0 218 free (search->realm);
paulo@0 219 free (search->guid);
paulo@0 220 free (search->query);
paulo@0 221 free (search);
paulo@0 222 }
paulo@0 223
paulo@0 224 static int find_by_event (GtSearch *search, IFEvent *event)
paulo@0 225 {
paulo@0 226 if (search->event == event)
paulo@0 227 return 0;
paulo@0 228
paulo@0 229 return -1;
paulo@0 230 }
paulo@0 231
paulo@0 232 void gt_search_disable (IFEvent *event)
paulo@0 233 {
paulo@0 234 List *ls;
paulo@0 235 GtSearch *search;
paulo@0 236
paulo@0 237 ls = list_find_custom (active_searches, event,
paulo@0 238 (CompareFunc) find_by_event);
paulo@0 239
paulo@0 240 if (!ls)
paulo@0 241 {
paulo@0 242 GT->DBGFN (GT, "didnt find search id %p", (long) event);
paulo@0 243 return;
paulo@0 244 }
paulo@0 245
paulo@0 246 search = ls->data;
paulo@0 247
paulo@0 248 GT->DBGFN (GT, "disabled search event %p (query '%s')", event, search->query);
paulo@0 249 search->event = NULL;
paulo@0 250 }
paulo@0 251
paulo@0 252 /******************************************************************************/
paulo@0 253
paulo@0 254 static int find_by_guid (GtSearch *a, GtSearch *b)
paulo@0 255 {
paulo@0 256 return gt_guid_cmp (a->guid, b->guid);
paulo@0 257 }
paulo@0 258
paulo@0 259 GtSearch *gt_search_find (gt_guid_t *guid)
paulo@0 260 {
paulo@0 261 GtSearch key;
paulo@0 262 List *l;
paulo@0 263
paulo@0 264 key.guid = guid;
paulo@0 265
paulo@0 266 l = list_find_custom (active_searches, &key, (CompareFunc) find_by_guid);
paulo@0 267
paulo@0 268 if (!l)
paulo@0 269 return NULL;
paulo@0 270
paulo@0 271 return l->data;
paulo@0 272 }
paulo@0 273
paulo@0 274 static BOOL search_matches_realm (GtSearch *search, GtShare *share)
paulo@0 275 {
paulo@0 276 char *mime;
paulo@0 277
paulo@0 278 if (!search->realm)
paulo@0 279 return TRUE;
paulo@0 280
paulo@0 281 if (!(mime = mime_type (share->filename)))
paulo@0 282 return FALSE;
paulo@0 283
paulo@0 284 if (strstr (mime, search->realm))
paulo@0 285 return TRUE;
paulo@0 286
paulo@0 287 if (!STRCMP (search->realm, "text"))
paulo@0 288 {
paulo@0 289 /* HACK: special case application/pdf */
paulo@0 290 if (strstr (mime, "pdf"))
paulo@0 291 return TRUE;
paulo@0 292
paulo@0 293 /* HACK: special case application/msword */
paulo@0 294 if (strstr (mime, "doc"))
paulo@0 295 return TRUE;
paulo@0 296 }
paulo@0 297
paulo@0 298 return FALSE;
paulo@0 299 }
paulo@0 300
paulo@0 301 static BOOL search_matches_hash (GtSearch *search, Share *file)
paulo@0 302 {
paulo@0 303 Hash *hash;
paulo@0 304 char *str;
paulo@0 305 int ret;
paulo@0 306
paulo@0 307 if (search->type != GT_SEARCH_HASH)
paulo@0 308 return TRUE;
paulo@0 309
paulo@0 310 if (!(hash = share_get_hash (file, "SHA1")))
paulo@0 311 {
paulo@0 312 GT->DBGFN (GT, "bad result for hash query");
paulo@0 313 return FALSE;
paulo@0 314 }
paulo@0 315
paulo@0 316 if (!(str = hash_dsp (hash)))
paulo@0 317 return FALSE;
paulo@0 318
paulo@0 319 ret = strcmp (search->hash, hashstr_data (str));
paulo@0 320
paulo@0 321 free (str);
paulo@0 322
paulo@0 323 return (ret == 0);
paulo@0 324 }
paulo@0 325
paulo@0 326 /*
paulo@0 327 * We have to filter out backslashes from the name to workaround a bug
paulo@0 328 * in lib/file.c.
paulo@0 329 */
paulo@0 330 static void set_display_name (Share *share, const char *path)
paulo@0 331 {
paulo@0 332 char *p;
paulo@0 333 char *disp_name;
paulo@0 334
paulo@0 335 if (!(p = disp_name = STRDUP (path)))
paulo@0 336 return;
paulo@0 337
paulo@0 338 while (*p)
paulo@0 339 {
paulo@0 340 if (*p == '\\')
paulo@0 341 *p = '_';
paulo@0 342 p++;
paulo@0 343 }
paulo@0 344
paulo@0 345 /* NOTE: this makes the GtShare->filename invalid because it shares memory
paulo@0 346 * with the Share */
paulo@0 347 share_set_path (share, disp_name);
paulo@0 348 free (disp_name);
paulo@0 349 }
paulo@0 350
paulo@0 351 void gt_search_reply (GtSearch *search, TCPC *c, in_addr_t host,
paulo@0 352 in_port_t gt_port, gt_guid_t *client_guid,
paulo@0 353 int availability, BOOL firewalled,
paulo@0 354 FileShare *file)
paulo@0 355 {
paulo@0 356 char server[128];
paulo@0 357 char *url;
paulo@0 358 char *host_str;
paulo@0 359 char *path;
paulo@0 360 GtShare *share;
paulo@0 361 GtNode *node;
paulo@0 362 BOOL is_local;
paulo@0 363
paulo@0 364 node = GT_NODE(c);
paulo@0 365
paulo@0 366 if (!search->event)
paulo@0 367 return;
paulo@0 368
paulo@0 369 if (gt_is_local_ip (host, node->ip))
paulo@0 370 is_local = TRUE;
paulo@0 371 else
paulo@0 372 is_local = FALSE;
paulo@0 373
paulo@0 374 /* derive firewalled status if the address is local */
paulo@0 375 if (is_local)
paulo@0 376 firewalled = TRUE;
paulo@0 377
paulo@0 378 /* if they are firewalled and so are we, don't bother.
paulo@0 379 * NOTE: if we have a download proxy, we shouldnt do this */
paulo@0 380 if (firewalled && GT_SELF->firewalled)
paulo@0 381 return;
paulo@0 382
paulo@0 383 if (!(share = share_get_udata (file, GT->name)))
paulo@0 384 return;
paulo@0 385
paulo@0 386 /* check if the mimetype for the result matches the query (i.e. this does
paulo@0 387 * client-side filtering) */
paulo@0 388 if (!search_matches_realm (search, share))
paulo@0 389 return;
paulo@0 390
paulo@0 391 /* match against the hash if this is a hash search */
paulo@0 392 if (!search_matches_hash (search, file))
paulo@0 393 return;
paulo@0 394
paulo@0 395 /* get the whole path (result may have '/' separators) */
paulo@0 396 path = file->path;
paulo@0 397 assert (path != NULL);
paulo@0 398
paulo@0 399 url = gt_source_url_new (path, share->index, host, gt_port,
paulo@0 400 node->ip, node->gt_port,
paulo@0 401 firewalled, client_guid);
paulo@0 402
paulo@0 403 if (!url)
paulo@0 404 return;
paulo@0 405
paulo@0 406 /* workaround bug in lib/file.c */
paulo@0 407 set_display_name (file, path);
paulo@0 408
paulo@0 409 /* print out the server data so we know which connection to
paulo@0 410 * talk to when sending a push request */
paulo@0 411 snprintf (server, sizeof (server) - 1, "%s:%hu",
paulo@0 412 net_ip_str (node->ip), node->gt_port);
paulo@0 413
paulo@0 414 if (is_local)
paulo@0 415 {
paulo@0 416 /* use the Client GUID for the user if the remote connection is
paulo@0 417 * on the Internet and the host is 0 or local */
paulo@0 418 host_str = stringf_dup ("%s@%s", net_ip_str (host),
paulo@0 419 gt_guid_str (client_guid));
paulo@0 420 }
paulo@0 421 else
paulo@0 422 {
paulo@0 423 /* Just use a plain host for cleanliness */
paulo@0 424 host_str = stringf_dup ("%s", net_ip_str (host));
paulo@0 425 }
paulo@0 426
paulo@0 427 GT->search_result (GT, search->event, host_str, server,
paulo@0 428 url, availability, file);
paulo@0 429
paulo@0 430 /* update statistics */
paulo@0 431 search->results++;
paulo@0 432 time (&search->last_result);
paulo@0 433
paulo@0 434 free (host_str);
paulo@0 435 free (url);
paulo@0 436 }
paulo@0 437
paulo@0 438 /******************************************************************************/
paulo@0 439
paulo@0 440 static uint8_t get_search_ttl (GtNode *node, gt_search_type_t type)
paulo@0 441 {
paulo@0 442 char *max_ttl;
paulo@0 443 uint8_t ttl = 0;
paulo@0 444
paulo@0 445 if ((max_ttl = dataset_lookupstr (node->hdr, "x-max-ttl")))
paulo@0 446 ttl = ATOI (max_ttl);
paulo@0 447
paulo@0 448 if (ttl > GT_SEARCH_TTL || ttl == 0)
paulo@0 449 ttl = GT_SEARCH_TTL;
paulo@0 450
paulo@0 451 /* ok because locates are rate-limited */
paulo@0 452 #if 0
paulo@0 453 if (type == GT_SEARCH_HASH)
paulo@0 454 ttl = 1;
paulo@0 455 #endif
paulo@0 456
paulo@0 457 return ttl;
paulo@0 458 }
paulo@0 459
paulo@0 460 static TCPC *broadcast_search (TCPC *c, GtNode *node, GtSearch *search)
paulo@0 461 {
paulo@0 462 gt_query_flags_t flags;
paulo@0 463 uint8_t ttl;
paulo@0 464 char *hash = NULL;
paulo@0 465 GtPacket *pkt;
paulo@0 466
paulo@0 467 /* set this query as having flags to be interpolated */
paulo@0 468 flags = QF_HAS_FLAGS;
paulo@0 469
paulo@0 470 /* request that only non-firewalled nodes respond if we are firewalled
paulo@0 471 * NOTE: if we ever support a download proxy, need to unset this */
paulo@0 472 if (GT_SELF->firewalled)
paulo@0 473 flags |= QF_ONLY_NON_FW;
paulo@0 474
paulo@0 475 #ifdef USE_LIBXML2
paulo@0 476 flags |= QF_WANTS_XML;
paulo@0 477 #endif /* USE_LIBXML2 */
paulo@0 478
paulo@0 479 ttl = get_search_ttl (node, search->type);
paulo@0 480
paulo@0 481 if (search->type == GT_SEARCH_HASH && !search->hash)
paulo@0 482 {
paulo@0 483 GT->DBGFN (GT, "trying to search for \"%s\" without a hash?!?",
paulo@0 484 search->query);
paulo@0 485 return NULL;
paulo@0 486 }
paulo@0 487
paulo@0 488 if (!(pkt = gt_packet_new (GT_MSG_QUERY, ttl, search->guid)))
paulo@0 489 return NULL;
paulo@0 490
paulo@0 491 gt_packet_put_uint16 (pkt, flags);
paulo@0 492 gt_packet_put_str (pkt, search->query);
paulo@0 493
paulo@0 494 if (search->hash)
paulo@0 495 hash = stringf_dup ("urn:sha1:%s", search->hash);
paulo@0 496
paulo@0 497 if (hash)
paulo@0 498 gt_packet_put_str (pkt, hash);
paulo@0 499
paulo@0 500 gt_packet_send (c, pkt);
paulo@0 501 gt_packet_free (pkt);
paulo@0 502
paulo@0 503 free (hash);
paulo@0 504
paulo@0 505 /* TODO: check error return from gt_packet_send_fmt! */
paulo@0 506 search->submitted++;
paulo@0 507 time (&search->last_submit);
paulo@0 508
paulo@0 509 return NULL;
paulo@0 510 }
paulo@0 511
paulo@0 512 static BOOL submit_search (GtSearch *search, TCPC *c)
paulo@0 513 {
paulo@0 514 if (search->results >= RESULTS_BACKOFF)
paulo@0 515 {
paulo@0 516 /* still count the search as submitted to this node */
paulo@0 517 search->submitted++;
paulo@0 518 return FALSE;
paulo@0 519 }
paulo@0 520
paulo@0 521 broadcast_search (c, GT_NODE(c), search);
paulo@0 522 return FALSE;
paulo@0 523 }
paulo@0 524
paulo@0 525 static BOOL submit_searches (TCPC *c)
paulo@0 526 {
paulo@0 527 list_foreach (active_searches, (ListForeachFunc)submit_search, c);
paulo@0 528 GT_NODE(c)->search_timer = 0;
paulo@0 529 return FALSE;
paulo@0 530 }
paulo@0 531
paulo@0 532 static BOOL reset_submit (GtSearch *search, time_t *now)
paulo@0 533 {
paulo@0 534 if (search->results >= RESULTS_BACKOFF)
paulo@0 535 return FALSE;
paulo@0 536
paulo@0 537 search->last_submit = *now;
paulo@0 538 return FALSE;
paulo@0 539 }
paulo@0 540
paulo@0 541 void gt_searches_submit (TCPC *c, time_t delay_ms)
paulo@0 542 {
paulo@0 543 time_t now;
paulo@0 544
paulo@0 545 /* reset each search timeout because we will submit each search soon */
paulo@0 546 time (&now);
paulo@0 547 list_foreach (active_searches, (ListForeachFunc)reset_submit, &now);
paulo@0 548
paulo@0 549 /* submit the searches once after a delay */
paulo@0 550 if (!GT_NODE(c)->search_timer)
paulo@0 551 {
paulo@0 552 GT_NODE(c)->search_timer = timer_add (delay_ms,
paulo@0 553 (TimerCallback)submit_searches, c);
paulo@0 554 }
paulo@0 555 }
paulo@0 556
paulo@0 557 BOOL gnutella_search (Protocol *p, IFEvent *event, char *query, char *exclude,
paulo@0 558 char *realm, Dataset *meta)
paulo@0 559 {
paulo@0 560 GtSearch *search;
paulo@0 561
paulo@0 562 search = gt_search_new (event, query, GT_SEARCH_KEYWORD);
paulo@0 563 search->realm = STRDUP (realm);
paulo@0 564
paulo@0 565 gt_conn_foreach (GT_CONN_FOREACH(broadcast_search), search,
paulo@0 566 GT_NODE_NONE, GT_NODE_CONNECTED, 0);
paulo@0 567
paulo@0 568 return TRUE;
paulo@0 569 }
paulo@0 570
paulo@0 571 /*****************************************************************************/
paulo@0 572
paulo@0 573 /*
paulo@0 574 * Using the hash, grab words to stuff in the query section by looking at the
paulo@0 575 * download list.
paulo@0 576 */
paulo@0 577 char *get_query_words (char *htype, char *hash)
paulo@0 578 {
paulo@0 579 Source *src;
paulo@0 580 GtSource *gt_src;
paulo@0 581 char *dup;
paulo@0 582
paulo@0 583 if (htype && strcmp (htype, "SHA1") != 0)
paulo@0 584 {
paulo@0 585 GT->DBGFN (GT, "htype != \"SHA1\"!?: %s", htype);
paulo@0 586 return NULL;
paulo@0 587 }
paulo@0 588
paulo@0 589 /* HACK: need gift's prefix */
paulo@0 590 if (!(dup = stringf_dup ("SHA1:%s", hash)))
paulo@0 591 return NULL;
paulo@0 592
paulo@0 593 src = gt_download_lookup (dup);
paulo@0 594 free (dup);
paulo@0 595
paulo@0 596 if (!src)
paulo@0 597 return NULL;
paulo@0 598
paulo@0 599 if (!(gt_src = src->udata))
paulo@0 600 {
paulo@0 601 GT->DBGFN (GT, "gt_src == NULL?!?!");
paulo@0 602 return NULL;
paulo@0 603 }
paulo@0 604
paulo@0 605 return gt_url_decode (gt_src->filename);
paulo@0 606 }
paulo@0 607
paulo@0 608 /*
paulo@0 609 * Returns TRUE if the current locate is ok to send and FALSE if it should be
paulo@0 610 * dropped to rate-limit locates. To determine that, we assign the locate a
paulo@0 611 * "probability of passage". Then we roll dice and if it's less than the
paulo@0 612 * probability, accept.
paulo@0 613 *
paulo@0 614 * For each locate attempt the probability of success for the next locate is
paulo@0 615 * halved, down to a minimum of 0.01%. For each minute that passes since the
paulo@0 616 * last locate, the probability of the locate succeeding increases by 1%.
paulo@0 617 */
paulo@0 618 static BOOL should_send_locate (void)
paulo@0 619 {
paulo@0 620 static time_t last_locate = 0;
paulo@0 621 time_t now;
paulo@0 622 double n;
paulo@0 623 BOOL passed;
paulo@0 624
paulo@0 625 time (&now);
paulo@0 626
paulo@0 627 if (last_locate == 0)
paulo@0 628 locate_pass_prob = 100.0;
paulo@0 629 else
paulo@0 630 locate_pass_prob += difftime (now, last_locate) / (1.0 * EMINUTES);
paulo@0 631
paulo@0 632 last_locate = now;
paulo@0 633
paulo@0 634 if (locate_pass_prob > 100.0)
paulo@0 635 locate_pass_prob = 100.0;
paulo@0 636
paulo@0 637 /* hmm, should this be removed? */
paulo@0 638 if (locate_pass_prob < 0.01)
paulo@0 639 locate_pass_prob = 0.01;
paulo@0 640
paulo@0 641 n = 100.0 * rand() / (RAND_MAX + 1.0);
paulo@0 642
paulo@0 643 GT->DBGFN (GT, "locate_pass_prob=%f n=%f", locate_pass_prob, n);
paulo@0 644 passed = BOOL_EXPR (n < locate_pass_prob);
paulo@0 645
paulo@0 646 /* drop next chance of succeeding */
paulo@0 647 locate_pass_prob /= 2;
paulo@0 648
paulo@0 649 return passed;
paulo@0 650 }
paulo@0 651
paulo@0 652 BOOL gnutella_locate (Protocol *p, IFEvent *event, char *htype, char *hash)
paulo@0 653 {
paulo@0 654 GtSearch *search;
paulo@0 655 unsigned char *bin;
paulo@0 656 char *fname;
paulo@0 657
paulo@0 658 /* Only locate hashes which are valid on Gnutella. */
paulo@0 659 if (STRCMP (htype, "SHA1"))
paulo@0 660 return FALSE;
paulo@0 661
paulo@0 662 GT->DBGFN (GT, "new hash search: %s", hash);
paulo@0 663
paulo@0 664 /* sha1_bin() needs a string of at least 32 characters */
paulo@0 665 if (STRLEN (hash) < 32)
paulo@0 666 return FALSE;
paulo@0 667
paulo@0 668 /* skip the hash if it's not parseable in base32 */
paulo@0 669 if (!(bin = sha1_bin (hash)))
paulo@0 670 return FALSE;
paulo@0 671
paulo@0 672 free (bin);
paulo@0 673
paulo@0 674 /* rate-limit locate searches to save bandwidth */
paulo@0 675 if (should_send_locate () == FALSE)
paulo@0 676 {
paulo@0 677 GT->DBGFN (GT, "dropping locate for %s "
paulo@0 678 "(too many searches in short period)", hash);
paulo@0 679 return FALSE;
paulo@0 680 }
paulo@0 681
paulo@0 682 /* make sure the hash is uppercase (canonical form on Gnet) */
paulo@0 683 string_upper (hash);
paulo@0 684
paulo@0 685 /*
paulo@0 686 * Look for a download with this hash, to put those words in the query to
paulo@0 687 * reduce the bandwidth consumed through QRP.
paulo@0 688 */
paulo@0 689 if (!(fname = get_query_words (htype, hash)))
paulo@0 690 fname = STRDUP ("");
paulo@0 691
paulo@0 692 if (!(search = gt_search_new (event, fname, GT_SEARCH_HASH)))
paulo@0 693 {
paulo@0 694 free (fname);
paulo@0 695 return FALSE;
paulo@0 696 }
paulo@0 697
paulo@0 698 free (fname);
paulo@0 699
paulo@0 700 search->hash = STRDUP (hash);
paulo@0 701
paulo@0 702 gt_conn_foreach (GT_CONN_FOREACH(broadcast_search), search, GT_NODE_NONE,
paulo@0 703 GT_NODE_CONNECTED, 0);
paulo@0 704
paulo@0 705 return TRUE;
paulo@0 706 }
paulo@0 707
paulo@0 708 void gnutella_search_cancel (Protocol *p, IFEvent *event)
paulo@0 709 {
paulo@0 710 gt_search_disable (event);
paulo@0 711 }
paulo@0 712
paulo@0 713 /*****************************************************************************/
paulo@0 714
paulo@0 715 void gt_search_init (void)
paulo@0 716 {
paulo@0 717 /* nothing */
paulo@0 718 }
paulo@0 719
paulo@0 720 BOOL rm_search (GtSearch *search, void *udata)
paulo@0 721 {
paulo@0 722 gt_search_free (search);
paulo@0 723
paulo@0 724 /* return FALSE here because gt_search_free() removes search from list */
paulo@0 725 return FALSE;
paulo@0 726 }
paulo@0 727
paulo@0 728 void gt_search_cleanup (void)
paulo@0 729 {
paulo@0 730 list_foreach_remove (active_searches, (ListForeachFunc)rm_search, NULL);
paulo@0 731 assert (active_searches == NULL);
paulo@0 732 }