[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r8441 - in GNUnet/src: applications applications/advertisin
From: |
gnunet |
Subject: |
[GNUnet-SVN] r8441 - in GNUnet/src: applications applications/advertising_gnunet include transports util/network |
Date: |
Fri, 1 May 2009 14:02:56 -0600 |
Author: durner
Date: 2009-05-01 14:02:56 -0600 (Fri, 01 May 2009)
New Revision: 8441
Added:
GNUnet/src/applications/advertising_gnunet/
GNUnet/src/applications/advertising_gnunet/advertising.c
GNUnet/src/applications/subnet/
Modified:
GNUnet/src/include/gnunet_util_network.h
GNUnet/src/transports/tcp.c
GNUnet/src/util/network/io.c
GNUnet/src/util/network/select.c
Log:
debug info
Copied: GNUnet/src/applications/advertising_gnunet/advertising.c (from rev
8108, GNUnet/src/applications/advertising/advertising.c)
===================================================================
--- GNUnet/src/applications/advertising_gnunet/advertising.c
(rev 0)
+++ GNUnet/src/applications/advertising_gnunet/advertising.c 2009-05-01
20:02:56 UTC (rev 8441)
@@ -0,0 +1,246 @@
+/*
+ This file is part of GNUnet.
+ (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff
(and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file advertising_gnunet/advertising.c
+ * @brief Advertising plugin that operates on GNUnet itself
+ *
+ * @author Christian Grothoff
+ * @author Nils Durner
+ */
+
+#include "platform.h"
+#include "gnunet_util.h"
+#include "gnunet_protocols.h"
+#include "gnunet_identity_service.h"
+#include "gnunet_transport_service.h"
+#include "gnunet_stats_service.h"
+#include "gnunet_topology_service.h"
+#include "bootstrap.h"
+
+/**
+ * Send our hello to a random connected host on a regular basis.
+ */
+#define HELLO_BROADCAST_FREQUENCY (2 * GNUNET_CRON_MINUTES)
+
+#define DEBUG_ADVERTISING GNUNET_NO
+
+static GNUNET_CoreAPIForPlugins *coreAPI;
+
+static GNUNET_Transport_ServiceAPI *transport;
+
+static GNUNET_Identity_ServiceAPI *identity;
+
+static GNUNET_Topology_ServiceAPI *topology;
+
+static GNUNET_Stats_ServiceAPI *stats;
+
+static struct GNUNET_GE_Context *ectx;
+
+static int stat_hello_out;
+
+
+static double
+getConnectPriority ()
+{
+ double preference;
+
+ /* we should'nt give lots of bandwidth for hellos if we're close to
+ the connection goal */
+ preference = topology->getSaturation ();
+ if (preference <= 0.0001)
+ preference = 0xFFFF;
+ else
+ preference = 1 / preference;
+ /* always give some decent, but compared to (migrated) content
+ competitive amount of bandwidth to peers sending (valid)
+ hellos */
+ if (preference < 0.2)
+ preference = 0.2;
+ return preference;
+}
+
+typedef struct
+{
+ /* the hello message */
+ GNUNET_MessageHello *m;
+ /* send the hello in 1 out of n cases */
+ int n;
+} SendData;
+
+static int
+broadcastHelper (const GNUNET_PeerIdentity * hi,
+ const unsigned short proto, int confirmed, void *cls)
+{
+ SendData *sd = cls;
+ GNUNET_MessageHello *hello;
+ GNUNET_TSession *tsession;
+ int prio;
+#if DEBUG_ADVERTISING
+ GNUNET_EncName other;
+#endif
+
+ if (confirmed == GNUNET_NO)
+ return GNUNET_OK;
+ if (proto == GNUNET_TRANSPORT_PROTOCOL_NUMBER_NAT)
+ {
+ sd->n--;
+ return GNUNET_OK; /* don't advertise NAT addresses via broadcast
*/
+ }
+ if ((sd->n != 0)
+ && (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, sd->n) != 0))
+ return GNUNET_OK;
+#if DEBUG_ADVERTISING
+ IF_GELOG (ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
+ GNUNET_hash_to_enc (&hi->hashPubKey, &other));
+ GNUNET_GE_LOG (ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
+ "Entering with target `%s'.\n", &other);
+#endif
+ if (0 == memcmp (hi, coreAPI->my_identity, sizeof (GNUNET_PeerIdentity)))
+ return GNUNET_OK; /* never advertise to myself... */
+ prio = (int) getConnectPriority ();
+ if (prio >= GNUNET_EXTREME_PRIORITY)
+ prio = GNUNET_EXTREME_PRIORITY / 4;
+ if (GNUNET_OK == coreAPI->p2p_connection_status_check (hi, NULL, NULL))
+ {
+ coreAPI->ciphertext_send (hi, &sd->m->header, prio,
+ HELLO_BROADCAST_FREQUENCY);
+ if (stats != NULL)
+ stats->change (stat_hello_out, 1);
+ return GNUNET_OK;
+ }
+ /* with even lower probability (with n peers
+ trying to contact with a probability of 1/n^2,
+ we get a probability of 1/n for this, which
+ is what we want: fewer attempts to contact fresh
+ peers as the network grows): */
+ if ((sd->n != 0)
+ && (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, sd->n) != 0))
+ return GNUNET_OK;
+
+ /* establish short-lived connection, send, tear down */
+ hello = identity->identity2Hello (hi, proto, GNUNET_NO);
+ if (NULL == hello)
+ return GNUNET_OK;
+ tsession = transport->connect (hello, __FILE__, GNUNET_YES);
+ GNUNET_free (hello);
+ if (tsession == NULL)
+ return GNUNET_OK; /* could not connect */
+ if (stats != NULL)
+ stats->change (stat_hello_out, 1);
+ coreAPI->plaintext_send (tsession,
+ (char *) &sd->m->header,
+ GNUNET_sizeof_hello (sd->m));
+ transport->disconnect (tsession, __FILE__);
+ return GNUNET_OK;
+}
+
+/**
+ * @brief Advertise this peer's identity
+ * @param msg the hello message
+ * @param prob send the hello in 1 out of n cases
+ */
+static void *advertise(GNUNET_MessageHello *msg,
+ int prob)
+{
+ SendData sd;
+
+ sd.n = prob;
+ sd.m = msg;
+ identity->forEachHost (now, &broadcastHelper, &sd);
+}
+
+GNUNET_Advertising_ServiceAPI *provide_module_advertising_gnunet
(GNUNET_CoreAPIForPlugins * capi)
+{
+ static GNUNET_Advertising_ServiceAPI api;
+
+ api.advertise = advertise;
+
+ coreAPI = capi;
+ ectx = capi->ectx;
+ identity = capi->service_request ("identity");
+ if (identity == NULL)
+ {
+ GNUNET_GE_BREAK (ectx, 0);
+ return GNUNET_SYSERR;
+ }
+ transport = capi->service_request ("transport");
+ if (transport == NULL)
+ {
+ GNUNET_GE_BREAK (ectx, 0);
+ capi->service_release (identity);
+ identity = NULL;
+ return GNUNET_SYSERR;
+ }
+ topology = capi->service_request ("topology");
+ if (topology == NULL)
+ {
+ GNUNET_GE_BREAK (ectx, 0);
+ capi->service_release (identity);
+ identity = NULL;
+ capi->service_release (transport);
+ transport = NULL;
+ return GNUNET_SYSERR;
+ }
+ stats = capi->service_request ("stats");
+ if (stats != NULL)
+ {
+ stat_hello_out =
+ stats->create (gettext_noop ("# Self advertisments transmitted"));
+ }
+
+ GNUNET_GE_ASSERT (capi->ectx,
+ 0 == GNUNET_GC_set_configuration_value_string (capi->cfg,
+ capi->ectx,
+ "ABOUT",
+
"advertising_gnunet",
+ _
+ ("ensures
that this peer is known by other"
+ " peers
and discovers other peers")));
+
+ return &api;
+}
+
+/**
+ * Stop advertising.
+ */
+void
+release_module_advertising_gnunet ()
+{
+ coreAPI->service_release (transport);
+ transport = NULL;
+ coreAPI->service_release (identity);
+ identity = NULL;
+ coreAPI->service_release (topology);
+ topology = NULL;
+ if (stats != NULL)
+ {
+ coreAPI->service_release (stats);
+ stats = NULL;
+ }
+ coreAPI = NULL;
+}
+
+
+
+
+/* end of advertising.c */
Modified: GNUnet/src/include/gnunet_util_network.h
===================================================================
--- GNUnet/src/include/gnunet_util_network.h 2009-05-01 19:39:23 UTC (rev
8440)
+++ GNUnet/src/include/gnunet_util_network.h 2009-05-01 20:02:56 UTC (rev
8441)
@@ -396,7 +396,15 @@
*/
int GNUNET_socket_test_valid (struct GNUNET_SocketHandle *s);
+/**
+ * Get the IP address of the remote peer
+ * @param s socket handle
+ * @param address IP address of the remote peer, freed by caller
+ * @return GNUNET_YES on success, GNUNET_SYSERR otherwise
+ */
+int GNUNET_socket_getpeername_string(struct GNUNET_SocketHandle *s, const char
**address);
+
/* ********************* select operations **************** */
Modified: GNUnet/src/transports/tcp.c
===================================================================
--- GNUnet/src/transports/tcp.c 2009-05-01 19:39:23 UTC (rev 8440)
+++ GNUnet/src/transports/tcp.c 2009-05-01 20:02:56 UTC (rev 8441)
@@ -33,6 +33,7 @@
#include "ip.h"
#define DEBUG_TCP GNUNET_NO
+#define DEBUG_CONNECT GNUNET_NO
/**
* after how much time of the core not being associated with a tcp
@@ -184,8 +185,26 @@
if ((tcpsession->users > 0) || (tcpsession->in_select == GNUNET_YES))
{
if (tcpsession->users == 0)
+ {
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ if (GNUNET_socket_getpeername_string(tcpsession->sock, &addr) !=
GNUNET_OK)
+ addr = GNUNET_strdup("unknown");
+
+ GNUNET_GE_LOG (coreAPI->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_ADMIN |
+ GNUNET_GE_USER | GNUNET_GE_BULK,
+ "TCP disconnect from peer `%s'\n",
+ addr);
+ GNUNET_free(addr);
+ }
+#endif
+
GNUNET_select_change_timeout (selector, tcpsession->sock,
TCP_FAST_TIMEOUT);
+ }
GNUNET_mutex_unlock (lock);
return GNUNET_OK;
}
@@ -263,6 +282,22 @@
/* at this point, we should be the only user! */
GNUNET_GE_ASSERT (NULL, tcpSession->users == 1);
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ if (GNUNET_socket_getpeername_string(tcpSession->sock, &addr) !=
GNUNET_OK)
+ addr = GNUNET_strdup("unknown");
+
+ GNUNET_GE_LOG (coreAPI->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_ADMIN |
+ GNUNET_GE_USER | GNUNET_GE_BULK,
+ "TCP got connetion from/to from peer `%s'\n",
+ addr);
+ GNUNET_free(addr);
+ }
+#endif
+
welcome = (const TCPWelcome *) msg;
if ((ntohs (welcome->header.type) != 0) || (len != sizeof (TCPWelcome)))
{
@@ -647,6 +682,22 @@
GNUNET_socket_destroy (s);
return GNUNET_SYSERR;
}
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ if (GNUNET_socket_getpeername_string(s, &addr) != GNUNET_OK)
+ addr = GNUNET_strdup("unknown");
+
+ GNUNET_GE_LOG (coreAPI->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_ADMIN |
+ GNUNET_GE_USER | GNUNET_GE_BULK,
+ "Connecting to peer `%s' using socket `%u'\n",
+ addr, sock);
+ GNUNET_free(addr);
+ }
+#endif
+
return tcp_connect_helper (hello, s, myAPI.protocol_number, tsessionPtr);
}
Modified: GNUnet/src/util/network/io.c
===================================================================
--- GNUnet/src/util/network/io.c 2009-05-01 19:39:23 UTC (rev 8440)
+++ GNUnet/src/util/network/io.c 2009-05-01 20:02:56 UTC (rev 8441)
@@ -676,5 +676,46 @@
#endif
}
+/**
+ * Get the IP address of the remote peer
+ * @param s socket handle
+ * @param address IP address of the remote peer, freed by caller
+ * @return GNUNET_YES on success, GNUNET_SYSERR otherwise
+ */
+int GNUNET_socket_getpeername_string(struct GNUNET_SocketHandle *s, const char
**address)
+{
+ int type;
+#ifdef AF_INET6
+ struct sockaddr_in6 addr;
+#else
+ struct sockaddr_in addr;
+#endif
+ int addrsize, strsize;
+ void *destaddr;
+ addrsize = sizeof(addr);
+ if (GETPEERNAME(s->handle, (struct sockaddr *) &addr, &addrsize) != 0)
+ return GNUNET_SYSERR;
+
+ type = ((struct sockaddr *) &addr)->sa_family;
+ if (type == AF_INET)
+ {
+ strsize = INET_ADDRSTRLEN;
+ destaddr = &((struct sockaddr_in *) &addr)->sin_addr;
+ }
+ else
+ {
+ strsize = INET6_ADDRSTRLEN;
+ destaddr = &((struct sockaddr_in6 *) &addr)->sin6_addr;
+ }
+ *address = (char *) GNUNET_malloc(strsize);
+ if (!inet_ntop(type, destaddr, (void *) *address, strsize))
+ {
+ GNUNET_free(*address);
+ return GNUNET_SYSERR;
+ }
+ else
+ return GNUNET_YES;
+}
+
/* end of io.c */
Modified: GNUnet/src/util/network/select.c
===================================================================
--- GNUnet/src/util/network/select.c 2009-05-01 19:39:23 UTC (rev 8440)
+++ GNUnet/src/util/network/select.c 2009-05-01 20:02:56 UTC (rev 8441)
@@ -29,6 +29,7 @@
#include "network.h"
#define DEBUG_SELECT GNUNET_NO
+#define DEBUG_CONNECT GNUNET_NO
/**
* Select Session handle.
@@ -292,6 +293,21 @@
#endif
if (ret != GNUNET_OK)
{
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ if (GNUNET_socket_getpeername_string(session->sock, &addr) !=
GNUNET_OK)
+ addr = GNUNET_strdup("unknown");
+ GNUNET_GE_LOG (sh->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_ADMIN |
+ GNUNET_GE_USER | GNUNET_GE_BULK,
+ "Peer `%s' closed connection %u\n",
+ addr, session->sock->handle);
+ GNUNET_free(addr);
+ }
+#endif
+
destroySession (sh, session);
return GNUNET_SYSERR; /* other side closed connection */
}
@@ -323,6 +339,7 @@
if (GNUNET_OK != sh->mh (sh->mh_cls,
sh, session->sock, session->sock_ctx, pack))
{
+ GNUNET_GE_BREAK(sh->ectx, 0);
GNUNET_mutex_lock (sh->lock);
if (session->locked == 1)
session->locked = 0;
@@ -332,6 +349,7 @@
GNUNET_mutex_lock (sh->lock);
if (session->locked == -1)
{
+ GNUNET_GE_BREAK(sh->ectx, 0);
session->locked = 0;
destroySession (sh, session);
return GNUNET_OK;
@@ -398,9 +416,22 @@
{
if (size == 0)
{
- /* send only returns 0 on error (happens if
- other side closed connection), so close
- the session */
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ /* send only returns 0 on error (happens if
+ other side closed connection), so close
+ the session */
+ if (GNUNET_socket_getpeername_string(session->sock, &addr) !=
GNUNET_OK)
+ addr = GNUNET_strdup("unknown");
+ GNUNET_GE_LOG (sh->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_DEVELOPER |
GNUNET_GE_BULK,
+ "Peer `%s' closed connection %u.\n",
+ addr, session->sock->handle);
+ GNUNET_free(addr);
+ }
+#endif
destroySession (sh, session);
return GNUNET_SYSERR;
}
@@ -508,7 +539,7 @@
if (!GNUNET_socket_test_valid (sock))
{
-#if DEBUG_SELECT
+#if 1
GNUNET_GE_LOG (sh->ectx,
GNUNET_GE_DEBUG | GNUNET_GE_DEVELOPER |
GNUNET_GE_BULK,
@@ -810,6 +841,26 @@
}
if (FD_ISSET (sock->handle, &errorSet))
{
+#if DEBUG_CONNECT
+ {
+ char *addr;
+ int error, len;
+
+ if (GNUNET_socket_getpeername_string(session->sock, &addr)
!= GNUNET_OK)
+ addr = GNUNET_strdup("");
+ error = 0;
+ len = sizeof(error);
+ if (GETSOCKOPT(sock->handle, SOL_SOCKET, SO_ERROR, &error,
&len) != 0)
+ GNUNET_GE_LOG_STRERROR(sh->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_DEVELOPER | GNUNET_GE_BULK,
+ "getsocktopt");
+ GNUNET_GE_LOG (sh->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_DEVELOPER |
GNUNET_GE_BULK,
+ "Closing faulty connection %u to `%s':
`%s'.\n",
+ session->sock->handle, addr, STRERROR(error));
+ GNUNET_free(addr);
+ }
+#endif
destroySession (sh, session);
i--;
continue;
@@ -817,6 +868,19 @@
if ((session->timeout != 0) &&
(now > session->lastUse + session->timeout))
{
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ if (GNUNET_socket_getpeername_string(session->sock, &addr)
!= GNUNET_OK)
+ addr = GNUNET_strdup("unknown");
+ GNUNET_GE_LOG (sh->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_DEVELOPER |
GNUNET_GE_BULK,
+ "Closing timed out/closed connection to
`%s'.\n",
+ addr);
+ GNUNET_free(addr);
+ }
+#endif
destroySession (sh, session);
i--;
continue;
@@ -914,6 +978,10 @@
(GNUNET_OK !=
GNUNET_pipe_make_nonblocking (sh->ectx, sh->signal_pipe[1])))
{
+ GNUNET_GE_LOG_STRERROR (ectx,
+ GNUNET_GE_WARNING | GNUNET_GE_IMMEDIATE |
+ GNUNET_GE_ADMIN, "GNUNET_pipe_make_nonblocking");
+
if ((0 != CLOSE (sh->signal_pipe[0])) ||
(0 != CLOSE (sh->signal_pipe[1])))
GNUNET_GE_LOG_STRERROR (ectx,
@@ -1215,6 +1283,21 @@
GNUNET_mutex_unlock (sh->lock);
return GNUNET_SYSERR;
}
+
+#if DEBUG_CONNECT
+ {
+ char *addr;
+
+ if (GNUNET_socket_getpeername_string(session->sock, &addr) != GNUNET_OK)
+ addr = GNUNET_strdup("");
+ GNUNET_GE_LOG (sh->ectx,
+ GNUNET_GE_DEBUG | GNUNET_GE_DEVELOPER | GNUNET_GE_BULK,
+ "Closing connection to `%s' because of select()
disconnect.\n",
+ addr);
+ GNUNET_free(addr);
+ }
+#endif
+
destroySession (sh, session);
GNUNET_mutex_unlock (sh->lock);
signalSelect (sh);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r8441 - in GNUnet/src: applications applications/advertising_gnunet include transports util/network,
gnunet <=