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