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