Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_hello.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2014, 2017 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file cadet/gnunet-service-cadet_hello.c
22  * @brief spread knowledge about how to contact other peers from PEERINFO
23  * @author Bartlomiej Polot
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - is most of this necessary/helpful?
28  * - should we not simply restrict this to OUR hello?
29  */
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "cadet_protocol.h"
36 #include "gnunet-service-cadet.h"
37 #include "gnunet-service-cadet_dht.h"
38 #include "gnunet-service-cadet_hello.h"
39 #include "gnunet-service-cadet_peer.h"
40
41 #define LOG(level, ...) GNUNET_log_from(level,"cadet-hll",__VA_ARGS__)
42
43 /**
44  * Hello message of local peer.
45  */
46 static struct GNUNET_HELLO_Message *mine;
47
48 /**
49  * Handle to peerinfo service.
50  */
51 static struct GNUNET_PEERINFO_Handle *peerinfo;
52
53 /**
54  * Iterator context.
55  */
56 static struct GNUNET_PEERINFO_NotifyContext *nc;
57
58
59 /**
60  * Process each hello message received from peerinfo.
61  *
62  * @param cls Closure (unused).
63  * @param peer Identity of the peer.
64  * @param hello Hello of the peer.
65  * @param err_msg Error message.
66  */
67 static void
68 got_hello (void *cls,
69            const struct GNUNET_PeerIdentity *id,
70            const struct GNUNET_HELLO_Message *hello,
71            const char *err_msg)
72 {
73   struct CadetPeer *peer;
74
75   if ( (NULL == id) ||
76        (NULL == hello) )
77     return;
78   if (0 == memcmp (id,
79                    &my_full_id,
80                    sizeof (struct GNUNET_PeerIdentity)))
81   {
82     GNUNET_free_non_null (mine);
83     mine = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (&hello->header);
84     GCD_hello_update ();
85     return;
86   }
87
88   LOG (GNUNET_ERROR_TYPE_DEBUG,
89        "Hello for %s (%d bytes), expires on %s\n",
90        GNUNET_i2s (id),
91        GNUNET_HELLO_size (hello),
92        GNUNET_STRINGS_absolute_time_to_string (GNUNET_HELLO_get_last_expiration (hello)));
93   peer = GCP_get (id,
94                   GNUNET_YES);
95   GCP_set_hello (peer,
96                  hello);
97 }
98
99
100 /**
101  * Initialize the hello subsystem.
102  *
103  * @param c Configuration.
104  */
105 void
106 GCH_init (const struct GNUNET_CONFIGURATION_Handle *c)
107 {
108   GNUNET_assert (NULL == nc);
109   peerinfo = GNUNET_PEERINFO_connect (c);
110   nc = GNUNET_PEERINFO_notify (c,
111                                GNUNET_NO,
112                                &got_hello,
113                                NULL);
114 }
115
116
117 /**
118  * Shut down the hello subsystem.
119  */
120 void
121 GCH_shutdown ()
122 {
123   if (NULL != nc)
124   {
125     GNUNET_PEERINFO_notify_cancel (nc);
126     nc = NULL;
127   }
128   if (NULL != peerinfo)
129   {
130     GNUNET_PEERINFO_disconnect (peerinfo);
131     peerinfo = NULL;
132   }
133   if (NULL != mine)
134   {
135     GNUNET_free (mine);
136     mine = NULL;
137   }
138 }
139
140
141 /**
142  * Get own hello message.
143  *
144  * @return Own hello message.
145  */
146 const struct GNUNET_HELLO_Message *
147 GCH_get_mine (void)
148 {
149   return mine;
150 }
151
152 /* end of gnunet-service-cadet-new_hello.c */