diff src/gt_share_state.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_share_state.c	Sat Feb 20 21:18:28 2010 -0800
     1.3 @@ -0,0 +1,190 @@
     1.4 +/*
     1.5 + * $Id: gt_share_state.c,v 1.2 2004/03/31 08:58:24 hipnod Exp $
     1.6 + *
     1.7 + * Copyright (C) 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 +#include "gt_node.h"
    1.22 +#include "gt_node_list.h"
    1.23 +#include "gt_packet.h"
    1.24 +#include "gt_share_state.h"
    1.25 +
    1.26 +/*****************************************************************************/
    1.27 +
    1.28 +/* whether giftd has disabled shares */
    1.29 +static BOOL giftd_hidden             = FALSE;
    1.30 +
    1.31 +/*****************************************************************************/
    1.32 +
    1.33 +struct gt_share_state *gt_share_state_new (void)
    1.34 +{
    1.35 +	struct gt_share_state *state;
    1.36 +
    1.37 +	if (!(state = malloc (sizeof(struct gt_share_state))))
    1.38 +		return NULL;
    1.39 +
    1.40 +	/*
    1.41 +	 * Sharing may be disabled now globally, but it isn't for this
    1.42 +	 * gt_share_state until gt_share_state_update() is called.
    1.43 +	 */
    1.44 +	state->hidden        = FALSE;
    1.45 +	state->plugin_hidden = FALSE;
    1.46 +
    1.47 +	return state;
    1.48 +}
    1.49 +
    1.50 +void gt_share_state_free (struct gt_share_state *state)
    1.51 +{
    1.52 +	free (state);
    1.53 +}
    1.54 +
    1.55 +static GtPacket *hops_flow_message (uint8_t ttl)
    1.56 +{
    1.57 +	GtPacket *pkt;
    1.58 +
    1.59 +	if (!(pkt = gt_packet_vendor (GT_VMSG_HOPS_FLOW)))
    1.60 +		return NULL;
    1.61 +
    1.62 +	gt_packet_put_uint8 (pkt, ttl);
    1.63 +
    1.64 +	if (gt_packet_error (pkt))
    1.65 +	{
    1.66 +		gt_packet_free (pkt);
    1.67 +		return NULL;
    1.68 +	}
    1.69 +
    1.70 +	return pkt;
    1.71 +}
    1.72 +
    1.73 +static void toggle_sharing (GtNode *node, struct gt_share_state *state,
    1.74 +                            BOOL hidden)
    1.75 +{
    1.76 +	GtPacket *hops_disable;
    1.77 +	uint8_t   max_hops;
    1.78 +
    1.79 +	/* regardless of whether the node receives our HospFlow, record whether
    1.80 +	 * sharing _should_ be disabled */
    1.81 +	state->hidden = hidden;
    1.82 +
    1.83 +	if (hidden)
    1.84 +		max_hops = 0;
    1.85 +	else
    1.86 +		max_hops = 8;
    1.87 +
    1.88 +	if (!(hops_disable = hops_flow_message (max_hops)))
    1.89 +		return;
    1.90 +
    1.91 +	if (!dataset_lookupstr (node->hdr, "vendor-message"))
    1.92 +	{
    1.93 +		gt_packet_free (hops_disable);
    1.94 +		return;
    1.95 +	}
    1.96 +
    1.97 +	GT->DBGSOCK (GT, GT_CONN(node), "sending HopsFlow(%d)", max_hops);
    1.98 +
    1.99 +	gt_node_send (node, hops_disable);
   1.100 +	gt_packet_free (hops_disable);
   1.101 +}
   1.102 +
   1.103 +void gt_share_state_update (GtNode *node)
   1.104 +{
   1.105 +	struct gt_share_state *state;
   1.106 +
   1.107 +	assert (node->state == GT_NODE_CONNECTED);
   1.108 +	state  = node->share_state;
   1.109 +
   1.110 +	if (state->hidden)
   1.111 +	{
   1.112 +		/* sharing disable, reenable it */
   1.113 +		if (!giftd_hidden && !state->plugin_hidden)
   1.114 +			toggle_sharing (node, state, FALSE);
   1.115 +	}
   1.116 +	else
   1.117 +	{
   1.118 +		/* sharing enabled, disable it */
   1.119 +		if (giftd_hidden || state->plugin_hidden)
   1.120 +			toggle_sharing (node, state, TRUE);
   1.121 +	}
   1.122 +}
   1.123 +
   1.124 +/*****************************************************************************/
   1.125 +
   1.126 +/*
   1.127 + * gt_share_state_update() must have been called before calling either of
   1.128 + * these.
   1.129 + */
   1.130 +
   1.131 +void gt_share_state_hide (GtNode *node)
   1.132 +{
   1.133 +	node->share_state->plugin_hidden = TRUE;
   1.134 +	gt_share_state_update (node);
   1.135 +}
   1.136 +
   1.137 +void gt_share_state_show (GtNode *node)
   1.138 +{
   1.139 +	node->share_state->plugin_hidden = FALSE;
   1.140 +	gt_share_state_update (node);
   1.141 +}
   1.142 +
   1.143 +/*****************************************************************************/
   1.144 +
   1.145 +static GtNode *foreach_state (TCPC *c, GtNode *node, void *udata)
   1.146 +{
   1.147 +	gt_share_state_update (node);
   1.148 +	return NULL;
   1.149 +}
   1.150 +
   1.151 +static void update_share_state (BOOL hidden)
   1.152 +{
   1.153 +	giftd_hidden = hidden;
   1.154 +
   1.155 +	/*
   1.156 +	 * Ignore the command from giftd for ultrapeers. XXX: this isn't actually
   1.157 +	 * right, if we change status inbetween two of these message, we're
   1.158 +	 * screwed.
   1.159 +	 */
   1.160 +	if (GT_SELF->klass & GT_NODE_ULTRA)
   1.161 +		return;
   1.162 +
   1.163 +	gt_conn_foreach (foreach_state, NULL,
   1.164 +	                 GT_NODE_ULTRA, GT_NODE_CONNECTED, 0);
   1.165 +}
   1.166 +
   1.167 +void gnutella_share_hide (Protocol *p)
   1.168 +{
   1.169 +	if (giftd_hidden)
   1.170 +		return;
   1.171 +
   1.172 +	update_share_state (TRUE);
   1.173 +}
   1.174 +
   1.175 +void gnutella_share_show (Protocol *p)
   1.176 +{
   1.177 +	if (!giftd_hidden)
   1.178 +		return;
   1.179 +
   1.180 +	update_share_state (FALSE);
   1.181 +}
   1.182 +
   1.183 +/*****************************************************************************/
   1.184 +
   1.185 +void gt_share_state_local_init (void)
   1.186 +{
   1.187 +	giftd_hidden = FALSE;
   1.188 +}
   1.189 +
   1.190 +void gt_share_state_local_cleanup (void)
   1.191 +{
   1.192 +	giftd_hidden = FALSE;
   1.193 +}