diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gt_gnutella.c	Sat Feb 20 21:18:28 2010 -0800
     1.3 @@ -0,0 +1,282 @@
     1.4 +/*
     1.5 + * $Id: gt_gnutella.c,v 1.74 2006/08/06 16:53:36 hexwab Exp $
     1.6 + *
     1.7 + * Copyright (C) 2001-2004 giFT project (gift.sourceforge.net)
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or modify it
    1.10 + * under the terms of the GNU General Public License as published by the
    1.11 + * Free Software Foundation; either version 2, or (at your option) any
    1.12 + * later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful, but
    1.15 + * WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 + * General Public License for more details.
    1.18 + */
    1.19 +
    1.20 +#include "gt_gnutella.h"
    1.21 +
    1.22 +#include "gt_conf.h"
    1.23 +#include "sha1.h"
    1.24 +#include "xml.h"
    1.25 +
    1.26 +#include "gt_share.h"
    1.27 +#include "gt_share_state.h"
    1.28 +
    1.29 +#include "gt_accept.h"
    1.30 +#include "gt_ban.h"
    1.31 +#include "gt_bind.h"
    1.32 +
    1.33 +#include "gt_node.h"
    1.34 +#include "gt_node_list.h"
    1.35 +#include "gt_netorg.h"
    1.36 +
    1.37 +#include "gt_xfer_obj.h"
    1.38 +#include "gt_xfer.h"
    1.39 +
    1.40 +#include "gt_search.h"
    1.41 +#include "gt_search_exec.h"
    1.42 +
    1.43 +#include "gt_web_cache.h"
    1.44 +#include "gt_stats.h"
    1.45 +#include "gt_query_route.h"
    1.46 +#include "transfer/source.h"           /* gnutella_source_{add,remove,cmp} */
    1.47 +
    1.48 +/*****************************************************************************/
    1.49 +
    1.50 +/* giFT protocol pointer */
    1.51 +Protocol *GT;
    1.52 +
    1.53 +/*****************************************************************************/
    1.54 +
    1.55 +/* The ip address is local if the address is local and the source from which
    1.56 + * it was discovered was not local */
    1.57 +BOOL gt_is_local_ip (in_addr_t ip, in_addr_t src)
    1.58 +{
    1.59 +	if (ip == 0)
    1.60 +		return TRUE;
    1.61 +
    1.62 +	if (net_match_host (ip, "LOCAL") &&
    1.63 +	    (src == 0 || !net_match_host (src, "LOCAL")))
    1.64 +	{
    1.65 +		return TRUE;
    1.66 +	}
    1.67 +
    1.68 +	return FALSE;
    1.69 +}
    1.70 +
    1.71 +/*****************************************************************************/
    1.72 +
    1.73 +/* shutdown */
    1.74 +static void gnutella_destroy (Protocol *p)
    1.75 +{
    1.76 +	GT->DBGFN (GT, "entered");
    1.77 +
    1.78 +	/* cleanup any network maintenance data */
    1.79 +	gt_netorg_cleanup ();
    1.80 +
    1.81 +	/* save the node list to disk */
    1.82 +	gt_node_list_save ();
    1.83 +
    1.84 +	/* cleanup any information about banned nodes */
    1.85 +	gt_ban_cleanup ();
    1.86 +
    1.87 +	/* destroy query_router tables */
    1.88 +	gt_query_router_self_destroy ();
    1.89 +
    1.90 +	/* cleanup remote search data structures */
    1.91 +	gt_search_cleanup ();
    1.92 +
    1.93 +	/* cleanup local share state */
    1.94 +	gt_share_state_local_cleanup ();
    1.95 +
    1.96 +	/* cleanup local search data structures */
    1.97 +	gt_search_exec_cleanup ();
    1.98 +
    1.99 +	/* cleanup XML structures */
   1.100 +	gt_xml_cleanup ();
   1.101 +
   1.102 +	/* free and disconnect all nodes */
   1.103 +	gt_node_remove_all ();
   1.104 +
   1.105 +	/* destroy web cache information */
   1.106 +	gt_web_cache_cleanup ();
   1.107 +
   1.108 +	/* stop binding to the local port */
   1.109 +	gt_bind_cleanup ();
   1.110 +
   1.111 +	/* free configuration information */
   1.112 +	gt_config_cleanup ();
   1.113 +
   1.114 +	/* free this client's GUID */
   1.115 +	gt_guid_self_cleanup ();
   1.116 +}
   1.117 +
   1.118 +/*****************************************************************************/
   1.119 +
   1.120 +static unsigned char *gnutella_sha1_hash (const char *path, size_t *len)
   1.121 +{
   1.122 +	*len = SHA1_BINSIZE;
   1.123 +	return sha1_digest (path, 0);
   1.124 +}
   1.125 +
   1.126 +static char *gnutella_sha1_dsp (unsigned char *hash, size_t len)
   1.127 +{
   1.128 +	return sha1_string (hash);
   1.129 +}
   1.130 +
   1.131 +/*****************************************************************************/
   1.132 +
   1.133 +static BOOL self_is_too_old (void)
   1.134 +{
   1.135 +	return FALSE;
   1.136 +}
   1.137 +
   1.138 +static void too_old_error_msg (void)
   1.139 +{
   1.140 +	GIFT_ERROR (("\nYour version of the Gnutella plugin is more than 1 year\n"
   1.141 +	             "old.  In order to protect the Gnutella network from \n"
   1.142 +	             "older programs, this plugin has deactivated itself.\n\n"
   1.143 +	             "Please update the plugin with a new version from \n"
   1.144 +	             "http://www.giftproject.org/, or stop running the \n"
   1.145 +	             "plugin by runnning gift-setup or removing \"Gnutella\"\n"
   1.146 +	             "from the /main/plugins line in $HOME/.giFT/giftd.conf\n"
   1.147 +	             "manually.\n\n"
   1.148 +	             "Thanks, and sorry for the inconvenience.\n"));
   1.149 +}
   1.150 +
   1.151 +static BOOL gnutella_start (Protocol *p)
   1.152 +{
   1.153 +	p->DBGFN (p, "Starting Gnutella plugin");
   1.154 +
   1.155 +	/*
   1.156 +	 * If this node is extremely old, deactivate the plugin, but print a
   1.157 +	 * message to the log file.
   1.158 +	 *
   1.159 +	 * This is a temporary hack to clean out old nodes in the future, just
   1.160 +	 * until ultrapeer support is implemented, but we don't want to bother
   1.161 +	 * people that are using other plugins and don't care...
   1.162 +	 */
   1.163 +	if (self_is_too_old ())
   1.164 +	{
   1.165 +		too_old_error_msg ();
   1.166 +		return TRUE;
   1.167 +	}
   1.168 +
   1.169 +	if (!gt_config_init ())
   1.170 +	{
   1.171 +		GIFT_ERROR (("Unable to load config file. Please copy it to "
   1.172 +		             "~/.giFT/Gnutella/Gnutella.conf"));
   1.173 +		return FALSE;
   1.174 +	}
   1.175 +
   1.176 +	if (!gt_web_cache_init ())
   1.177 +	{
   1.178 +		GIFT_ERROR (("Unable to load gwebcaches file. Please copy it to "
   1.179 +		             "~/.giFT/Gnutella/gwebcaches"));
   1.180 +		return FALSE;
   1.181 +	}
   1.182 +
   1.183 +	/* load any banned ip addresses */
   1.184 +	gt_ban_init ();
   1.185 +
   1.186 +	/* initialize the GUID for this node */
   1.187 +	gt_guid_self_init ();
   1.188 +
   1.189 +	/* listen for connections */
   1.190 +	gt_bind_init ();
   1.191 +
   1.192 +	/* load the list of all previously contacted nodes */
   1.193 +	gt_node_list_load ();
   1.194 +
   1.195 +	/* initialize query router tables */
   1.196 +	gt_query_router_self_init ();
   1.197 +
   1.198 +	/* initialize the local search data structures */
   1.199 +	gt_search_exec_init ();
   1.200 +
   1.201 +	/* initialize the local sharing state */
   1.202 +	gt_share_state_local_init ();
   1.203 +
   1.204 +	/* initialize the remote search data structures */
   1.205 +	gt_search_init ();
   1.206 +
   1.207 +	/* initialize support for xml metadata */
   1.208 +	gt_xml_init ();
   1.209 +
   1.210 +	/* startup network maintenance */
   1.211 +	gt_netorg_init ();
   1.212 +
   1.213 +	return TRUE;
   1.214 +}
   1.215 +
   1.216 +/*
   1.217 + * The entry-point for the giFT daemon
   1.218 + */
   1.219 +BOOL Gnutella_init (Protocol *p)
   1.220 +{
   1.221 +	if (protocol_compat (p, LIBGIFTPROTO_MKVERSION (0, 11, 4)) != 0)
   1.222 +		return FALSE;
   1.223 +
   1.224 +	p->version_str = STRDUP (GT_VERSION);
   1.225 +	GT = p;
   1.226 +
   1.227 +	/* gt_gnutella.c: */
   1.228 +	p->start = gnutella_start;
   1.229 +
   1.230 +	/* skip initializing if too old. Note that we still need to provide a
   1.231 +	 * start function since gift's default handler will return FALSE. The
   1.232 +	 * actual error message describing the 'too old' problem is also generated
   1.233 +	 * by our start function.
   1.234 +	 */
   1.235 +	if (self_is_too_old ())
   1.236 +		return TRUE;
   1.237 +
   1.238 +	/* describe the hash algo */
   1.239 +	p->hash_handler (p, "SHA1", HASH_PRIMARY, gnutella_sha1_hash,
   1.240 +	                 gnutella_sha1_dsp);
   1.241 +
   1.242 +	/* gt_gnutella.c: */
   1.243 +	p->destroy        = gnutella_destroy;
   1.244 +
   1.245 +	/* gt_search.c: */
   1.246 +	p->search         = gnutella_search;
   1.247 +#if 0
   1.248 +	p->browse         = gnutella_browse;
   1.249 +#endif
   1.250 +	p->locate         = gnutella_locate;
   1.251 +	p->search_cancel  = gnutella_search_cancel;
   1.252 +
   1.253 +	/* gt_xfer.c: */
   1.254 +	p->download_start = gnutella_download_start;
   1.255 +	p->download_stop  = gnutella_download_stop;
   1.256 +	p->upload_stop    = gnutella_upload_stop;
   1.257 +	p->chunk_suspend  = gnutella_chunk_suspend;
   1.258 +	p->chunk_resume   = gnutella_chunk_resume;
   1.259 +
   1.260 +	/* transfer/source.c: */
   1.261 +	p->source_cmp     = gnutella_source_cmp;
   1.262 +	p->source_add     = gnutella_source_add;
   1.263 +	p->source_remove  = gnutella_source_remove;
   1.264 +
   1.265 +#if 0
   1.266 +	p->upload_avail   = gnutella_upload_avail;
   1.267 +	p->user_cmp       = gnutella_user_cmp;
   1.268 +#endif
   1.269 +
   1.270 +	/* gt_share.c: */
   1.271 +	p->share_new      = gnutella_share_new;
   1.272 +	p->share_free     = gnutella_share_free;
   1.273 +	p->share_add      = gnutella_share_add;
   1.274 +	p->share_remove   = gnutella_share_remove;
   1.275 +	p->share_sync     = gnutella_share_sync;
   1.276 +
   1.277 +	/* gt_share_state.c: */
   1.278 +	p->share_hide     = gnutella_share_hide;
   1.279 +	p->share_show     = gnutella_share_show;
   1.280 +
   1.281 +	/* gt_stats.c: */
   1.282 +	p->stats          = gnutella_stats;
   1.283 +
   1.284 +	return TRUE;
   1.285 +}