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