annotate src/gt_gnutella.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_gnutella.c,v 1.74 2006/08/06 16:53:36 hexwab Exp $
paulo@0 3 *
paulo@0 4 * Copyright (C) 2001-2004 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_conf.h"
paulo@0 20 #include "sha1.h"
paulo@0 21 #include "xml.h"
paulo@0 22
paulo@0 23 #include "gt_share.h"
paulo@0 24 #include "gt_share_state.h"
paulo@0 25
paulo@0 26 #include "gt_accept.h"
paulo@0 27 #include "gt_ban.h"
paulo@0 28 #include "gt_bind.h"
paulo@0 29
paulo@0 30 #include "gt_node.h"
paulo@0 31 #include "gt_node_list.h"
paulo@0 32 #include "gt_netorg.h"
paulo@0 33
paulo@0 34 #include "gt_xfer_obj.h"
paulo@0 35 #include "gt_xfer.h"
paulo@0 36
paulo@0 37 #include "gt_search.h"
paulo@0 38 #include "gt_search_exec.h"
paulo@0 39
paulo@0 40 #include "gt_web_cache.h"
paulo@0 41 #include "gt_stats.h"
paulo@0 42 #include "gt_query_route.h"
paulo@0 43 #include "transfer/source.h" /* gnutella_source_{add,remove,cmp} */
paulo@0 44
paulo@0 45 /*****************************************************************************/
paulo@0 46
paulo@0 47 /* giFT protocol pointer */
paulo@0 48 Protocol *GT;
paulo@0 49
paulo@0 50 /*****************************************************************************/
paulo@0 51
paulo@0 52 /* The ip address is local if the address is local and the source from which
paulo@0 53 * it was discovered was not local */
paulo@0 54 BOOL gt_is_local_ip (in_addr_t ip, in_addr_t src)
paulo@0 55 {
paulo@0 56 if (ip == 0)
paulo@0 57 return TRUE;
paulo@0 58
paulo@0 59 if (net_match_host (ip, "LOCAL") &&
paulo@0 60 (src == 0 || !net_match_host (src, "LOCAL")))
paulo@0 61 {
paulo@0 62 return TRUE;
paulo@0 63 }
paulo@0 64
paulo@0 65 return FALSE;
paulo@0 66 }
paulo@0 67
paulo@0 68 /*****************************************************************************/
paulo@0 69
paulo@0 70 /* shutdown */
paulo@0 71 static void gnutella_destroy (Protocol *p)
paulo@0 72 {
paulo@0 73 GT->DBGFN (GT, "entered");
paulo@0 74
paulo@0 75 /* cleanup any network maintenance data */
paulo@0 76 gt_netorg_cleanup ();
paulo@0 77
paulo@0 78 /* save the node list to disk */
paulo@0 79 gt_node_list_save ();
paulo@0 80
paulo@0 81 /* cleanup any information about banned nodes */
paulo@0 82 gt_ban_cleanup ();
paulo@0 83
paulo@0 84 /* destroy query_router tables */
paulo@0 85 gt_query_router_self_destroy ();
paulo@0 86
paulo@0 87 /* cleanup remote search data structures */
paulo@0 88 gt_search_cleanup ();
paulo@0 89
paulo@0 90 /* cleanup local share state */
paulo@0 91 gt_share_state_local_cleanup ();
paulo@0 92
paulo@0 93 /* cleanup local search data structures */
paulo@0 94 gt_search_exec_cleanup ();
paulo@0 95
paulo@0 96 /* cleanup XML structures */
paulo@0 97 gt_xml_cleanup ();
paulo@0 98
paulo@0 99 /* free and disconnect all nodes */
paulo@0 100 gt_node_remove_all ();
paulo@0 101
paulo@0 102 /* destroy web cache information */
paulo@0 103 gt_web_cache_cleanup ();
paulo@0 104
paulo@0 105 /* stop binding to the local port */
paulo@0 106 gt_bind_cleanup ();
paulo@0 107
paulo@0 108 /* free configuration information */
paulo@0 109 gt_config_cleanup ();
paulo@0 110
paulo@0 111 /* free this client's GUID */
paulo@0 112 gt_guid_self_cleanup ();
paulo@0 113 }
paulo@0 114
paulo@0 115 /*****************************************************************************/
paulo@0 116
paulo@0 117 static unsigned char *gnutella_sha1_hash (const char *path, size_t *len)
paulo@0 118 {
paulo@0 119 *len = SHA1_BINSIZE;
paulo@0 120 return sha1_digest (path, 0);
paulo@0 121 }
paulo@0 122
paulo@0 123 static char *gnutella_sha1_dsp (unsigned char *hash, size_t len)
paulo@0 124 {
paulo@0 125 return sha1_string (hash);
paulo@0 126 }
paulo@0 127
paulo@0 128 /*****************************************************************************/
paulo@0 129
paulo@0 130 static BOOL self_is_too_old (void)
paulo@0 131 {
paulo@0 132 return FALSE;
paulo@0 133 }
paulo@0 134
paulo@0 135 static void too_old_error_msg (void)
paulo@0 136 {
paulo@0 137 GIFT_ERROR (("\nYour version of the Gnutella plugin is more than 1 year\n"
paulo@0 138 "old. In order to protect the Gnutella network from \n"
paulo@0 139 "older programs, this plugin has deactivated itself.\n\n"
paulo@0 140 "Please update the plugin with a new version from \n"
paulo@0 141 "http://www.giftproject.org/, or stop running the \n"
paulo@0 142 "plugin by runnning gift-setup or removing \"Gnutella\"\n"
paulo@0 143 "from the /main/plugins line in $HOME/.giFT/giftd.conf\n"
paulo@0 144 "manually.\n\n"
paulo@0 145 "Thanks, and sorry for the inconvenience.\n"));
paulo@0 146 }
paulo@0 147
paulo@0 148 static BOOL gnutella_start (Protocol *p)
paulo@0 149 {
paulo@0 150 p->DBGFN (p, "Starting Gnutella plugin");
paulo@0 151
paulo@0 152 /*
paulo@0 153 * If this node is extremely old, deactivate the plugin, but print a
paulo@0 154 * message to the log file.
paulo@0 155 *
paulo@0 156 * This is a temporary hack to clean out old nodes in the future, just
paulo@0 157 * until ultrapeer support is implemented, but we don't want to bother
paulo@0 158 * people that are using other plugins and don't care...
paulo@0 159 */
paulo@0 160 if (self_is_too_old ())
paulo@0 161 {
paulo@0 162 too_old_error_msg ();
paulo@0 163 return TRUE;
paulo@0 164 }
paulo@0 165
paulo@0 166 if (!gt_config_init ())
paulo@0 167 {
paulo@0 168 GIFT_ERROR (("Unable to load config file. Please copy it to "
paulo@0 169 "~/.giFT/Gnutella/Gnutella.conf"));
paulo@0 170 return FALSE;
paulo@0 171 }
paulo@0 172
paulo@0 173 if (!gt_web_cache_init ())
paulo@0 174 {
paulo@0 175 GIFT_ERROR (("Unable to load gwebcaches file. Please copy it to "
paulo@0 176 "~/.giFT/Gnutella/gwebcaches"));
paulo@0 177 return FALSE;
paulo@0 178 }
paulo@0 179
paulo@0 180 /* load any banned ip addresses */
paulo@0 181 gt_ban_init ();
paulo@0 182
paulo@0 183 /* initialize the GUID for this node */
paulo@0 184 gt_guid_self_init ();
paulo@0 185
paulo@0 186 /* listen for connections */
paulo@0 187 gt_bind_init ();
paulo@0 188
paulo@0 189 /* load the list of all previously contacted nodes */
paulo@0 190 gt_node_list_load ();
paulo@0 191
paulo@0 192 /* initialize query router tables */
paulo@0 193 gt_query_router_self_init ();
paulo@0 194
paulo@0 195 /* initialize the local search data structures */
paulo@0 196 gt_search_exec_init ();
paulo@0 197
paulo@0 198 /* initialize the local sharing state */
paulo@0 199 gt_share_state_local_init ();
paulo@0 200
paulo@0 201 /* initialize the remote search data structures */
paulo@0 202 gt_search_init ();
paulo@0 203
paulo@0 204 /* initialize support for xml metadata */
paulo@0 205 gt_xml_init ();
paulo@0 206
paulo@0 207 /* startup network maintenance */
paulo@0 208 gt_netorg_init ();
paulo@0 209
paulo@0 210 return TRUE;
paulo@0 211 }
paulo@0 212
paulo@0 213 /*
paulo@0 214 * The entry-point for the giFT daemon
paulo@0 215 */
paulo@0 216 BOOL Gnutella_init (Protocol *p)
paulo@0 217 {
paulo@0 218 if (protocol_compat (p, LIBGIFTPROTO_MKVERSION (0, 11, 4)) != 0)
paulo@0 219 return FALSE;
paulo@0 220
paulo@0 221 p->version_str = STRDUP (GT_VERSION);
paulo@0 222 GT = p;
paulo@0 223
paulo@0 224 /* gt_gnutella.c: */
paulo@0 225 p->start = gnutella_start;
paulo@0 226
paulo@0 227 /* skip initializing if too old. Note that we still need to provide a
paulo@0 228 * start function since gift's default handler will return FALSE. The
paulo@0 229 * actual error message describing the 'too old' problem is also generated
paulo@0 230 * by our start function.
paulo@0 231 */
paulo@0 232 if (self_is_too_old ())
paulo@0 233 return TRUE;
paulo@0 234
paulo@0 235 /* describe the hash algo */
paulo@0 236 p->hash_handler (p, "SHA1", HASH_PRIMARY, gnutella_sha1_hash,
paulo@0 237 gnutella_sha1_dsp);
paulo@0 238
paulo@0 239 /* gt_gnutella.c: */
paulo@0 240 p->destroy = gnutella_destroy;
paulo@0 241
paulo@0 242 /* gt_search.c: */
paulo@0 243 p->search = gnutella_search;
paulo@0 244 #if 0
paulo@0 245 p->browse = gnutella_browse;
paulo@0 246 #endif
paulo@0 247 p->locate = gnutella_locate;
paulo@0 248 p->search_cancel = gnutella_search_cancel;
paulo@0 249
paulo@0 250 /* gt_xfer.c: */
paulo@0 251 p->download_start = gnutella_download_start;
paulo@0 252 p->download_stop = gnutella_download_stop;
paulo@0 253 p->upload_stop = gnutella_upload_stop;
paulo@0 254 p->chunk_suspend = gnutella_chunk_suspend;
paulo@0 255 p->chunk_resume = gnutella_chunk_resume;
paulo@0 256
paulo@0 257 /* transfer/source.c: */
paulo@0 258 p->source_cmp = gnutella_source_cmp;
paulo@0 259 p->source_add = gnutella_source_add;
paulo@0 260 p->source_remove = gnutella_source_remove;
paulo@0 261
paulo@0 262 #if 0
paulo@0 263 p->upload_avail = gnutella_upload_avail;
paulo@0 264 p->user_cmp = gnutella_user_cmp;
paulo@0 265 #endif
paulo@0 266
paulo@0 267 /* gt_share.c: */
paulo@0 268 p->share_new = gnutella_share_new;
paulo@0 269 p->share_free = gnutella_share_free;
paulo@0 270 p->share_add = gnutella_share_add;
paulo@0 271 p->share_remove = gnutella_share_remove;
paulo@0 272 p->share_sync = gnutella_share_sync;
paulo@0 273
paulo@0 274 /* gt_share_state.c: */
paulo@0 275 p->share_hide = gnutella_share_hide;
paulo@0 276 p->share_show = gnutella_share_show;
paulo@0 277
paulo@0 278 /* gt_stats.c: */
paulo@0 279 p->stats = gnutella_stats;
paulo@0 280
paulo@0 281 return TRUE;
paulo@0 282 }