annotate 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
rev   line source
paulo@0 1 /*
paulo@0 2 * $Id: gt_share_state.c,v 1.2 2004/03/31 08:58:24 hipnod Exp $
paulo@0 3 *
paulo@0 4 * Copyright (C) 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 #include "gt_node.h"
paulo@0 19 #include "gt_node_list.h"
paulo@0 20 #include "gt_packet.h"
paulo@0 21 #include "gt_share_state.h"
paulo@0 22
paulo@0 23 /*****************************************************************************/
paulo@0 24
paulo@0 25 /* whether giftd has disabled shares */
paulo@0 26 static BOOL giftd_hidden = FALSE;
paulo@0 27
paulo@0 28 /*****************************************************************************/
paulo@0 29
paulo@0 30 struct gt_share_state *gt_share_state_new (void)
paulo@0 31 {
paulo@0 32 struct gt_share_state *state;
paulo@0 33
paulo@0 34 if (!(state = malloc (sizeof(struct gt_share_state))))
paulo@0 35 return NULL;
paulo@0 36
paulo@0 37 /*
paulo@0 38 * Sharing may be disabled now globally, but it isn't for this
paulo@0 39 * gt_share_state until gt_share_state_update() is called.
paulo@0 40 */
paulo@0 41 state->hidden = FALSE;
paulo@0 42 state->plugin_hidden = FALSE;
paulo@0 43
paulo@0 44 return state;
paulo@0 45 }
paulo@0 46
paulo@0 47 void gt_share_state_free (struct gt_share_state *state)
paulo@0 48 {
paulo@0 49 free (state);
paulo@0 50 }
paulo@0 51
paulo@0 52 static GtPacket *hops_flow_message (uint8_t ttl)
paulo@0 53 {
paulo@0 54 GtPacket *pkt;
paulo@0 55
paulo@0 56 if (!(pkt = gt_packet_vendor (GT_VMSG_HOPS_FLOW)))
paulo@0 57 return NULL;
paulo@0 58
paulo@0 59 gt_packet_put_uint8 (pkt, ttl);
paulo@0 60
paulo@0 61 if (gt_packet_error (pkt))
paulo@0 62 {
paulo@0 63 gt_packet_free (pkt);
paulo@0 64 return NULL;
paulo@0 65 }
paulo@0 66
paulo@0 67 return pkt;
paulo@0 68 }
paulo@0 69
paulo@0 70 static void toggle_sharing (GtNode *node, struct gt_share_state *state,
paulo@0 71 BOOL hidden)
paulo@0 72 {
paulo@0 73 GtPacket *hops_disable;
paulo@0 74 uint8_t max_hops;
paulo@0 75
paulo@0 76 /* regardless of whether the node receives our HospFlow, record whether
paulo@0 77 * sharing _should_ be disabled */
paulo@0 78 state->hidden = hidden;
paulo@0 79
paulo@0 80 if (hidden)
paulo@0 81 max_hops = 0;
paulo@0 82 else
paulo@0 83 max_hops = 8;
paulo@0 84
paulo@0 85 if (!(hops_disable = hops_flow_message (max_hops)))
paulo@0 86 return;
paulo@0 87
paulo@0 88 if (!dataset_lookupstr (node->hdr, "vendor-message"))
paulo@0 89 {
paulo@0 90 gt_packet_free (hops_disable);
paulo@0 91 return;
paulo@0 92 }
paulo@0 93
paulo@0 94 GT->DBGSOCK (GT, GT_CONN(node), "sending HopsFlow(%d)", max_hops);
paulo@0 95
paulo@0 96 gt_node_send (node, hops_disable);
paulo@0 97 gt_packet_free (hops_disable);
paulo@0 98 }
paulo@0 99
paulo@0 100 void gt_share_state_update (GtNode *node)
paulo@0 101 {
paulo@0 102 struct gt_share_state *state;
paulo@0 103
paulo@0 104 assert (node->state == GT_NODE_CONNECTED);
paulo@0 105 state = node->share_state;
paulo@0 106
paulo@0 107 if (state->hidden)
paulo@0 108 {
paulo@0 109 /* sharing disable, reenable it */
paulo@0 110 if (!giftd_hidden && !state->plugin_hidden)
paulo@0 111 toggle_sharing (node, state, FALSE);
paulo@0 112 }
paulo@0 113 else
paulo@0 114 {
paulo@0 115 /* sharing enabled, disable it */
paulo@0 116 if (giftd_hidden || state->plugin_hidden)
paulo@0 117 toggle_sharing (node, state, TRUE);
paulo@0 118 }
paulo@0 119 }
paulo@0 120
paulo@0 121 /*****************************************************************************/
paulo@0 122
paulo@0 123 /*
paulo@0 124 * gt_share_state_update() must have been called before calling either of
paulo@0 125 * these.
paulo@0 126 */
paulo@0 127
paulo@0 128 void gt_share_state_hide (GtNode *node)
paulo@0 129 {
paulo@0 130 node->share_state->plugin_hidden = TRUE;
paulo@0 131 gt_share_state_update (node);
paulo@0 132 }
paulo@0 133
paulo@0 134 void gt_share_state_show (GtNode *node)
paulo@0 135 {
paulo@0 136 node->share_state->plugin_hidden = FALSE;
paulo@0 137 gt_share_state_update (node);
paulo@0 138 }
paulo@0 139
paulo@0 140 /*****************************************************************************/
paulo@0 141
paulo@0 142 static GtNode *foreach_state (TCPC *c, GtNode *node, void *udata)
paulo@0 143 {
paulo@0 144 gt_share_state_update (node);
paulo@0 145 return NULL;
paulo@0 146 }
paulo@0 147
paulo@0 148 static void update_share_state (BOOL hidden)
paulo@0 149 {
paulo@0 150 giftd_hidden = hidden;
paulo@0 151
paulo@0 152 /*
paulo@0 153 * Ignore the command from giftd for ultrapeers. XXX: this isn't actually
paulo@0 154 * right, if we change status inbetween two of these message, we're
paulo@0 155 * screwed.
paulo@0 156 */
paulo@0 157 if (GT_SELF->klass & GT_NODE_ULTRA)
paulo@0 158 return;
paulo@0 159
paulo@0 160 gt_conn_foreach (foreach_state, NULL,
paulo@0 161 GT_NODE_ULTRA, GT_NODE_CONNECTED, 0);
paulo@0 162 }
paulo@0 163
paulo@0 164 void gnutella_share_hide (Protocol *p)
paulo@0 165 {
paulo@0 166 if (giftd_hidden)
paulo@0 167 return;
paulo@0 168
paulo@0 169 update_share_state (TRUE);
paulo@0 170 }
paulo@0 171
paulo@0 172 void gnutella_share_show (Protocol *p)
paulo@0 173 {
paulo@0 174 if (!giftd_hidden)
paulo@0 175 return;
paulo@0 176
paulo@0 177 update_share_state (FALSE);
paulo@0 178 }
paulo@0 179
paulo@0 180 /*****************************************************************************/
paulo@0 181
paulo@0 182 void gt_share_state_local_init (void)
paulo@0 183 {
paulo@0 184 giftd_hidden = FALSE;
paulo@0 185 }
paulo@0 186
paulo@0 187 void gt_share_state_local_cleanup (void)
paulo@0 188 {
paulo@0 189 giftd_hidden = FALSE;
paulo@0 190 }