- doc
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_hello.c
1 /*
2      This file is part of GNUnet.
3      (C) 2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23
24 #include "gnunet_statistics_service.h"
25 #include "gnunet_peerinfo_service.h"
26
27 #include "mesh_protocol.h"
28 #include "mesh_path.h"
29
30 #include "gnunet-service-mesh_hello.h"
31 #include "gnunet-service-mesh_peer.h"
32
33 #define LOG(level, ...) GNUNET_log_from(level,"mesh-hll",__VA_ARGS__)
34
35
36 /******************************************************************************/
37 /********************************   STRUCTS  **********************************/
38 /******************************************************************************/
39
40
41
42 /******************************************************************************/
43 /*******************************   GLOBALS  ***********************************/
44 /******************************************************************************/
45
46 /**
47  * Global handle to the statistics service.
48  */
49 extern struct GNUNET_STATISTICS_Handle *stats;
50
51 /**
52  * Local peer own ID (memory efficient handle).
53  */
54 extern GNUNET_PEER_Id myid;
55
56 /**
57  * Local peer own ID (full value).
58  */
59 extern struct GNUNET_PeerIdentity my_full_id;
60
61
62 /**
63  * Don't try to recover tunnels if shutting down.
64  */
65 extern int shutting_down;
66
67
68 /**
69  * Hello message of local peer.
70  */
71 const struct GNUNET_HELLO_Message *mine;
72
73 /**
74  * Handle to peerinfo service.
75  */
76 static struct GNUNET_PEERINFO_Handle *peerinfo;
77
78 /**
79  * Iterator context.
80  */
81 struct GNUNET_PEERINFO_NotifyContext* nc;
82
83
84 /******************************************************************************/
85 /********************************   STATIC  ***********************************/
86 /******************************************************************************/
87
88 /**
89  * Process each hello message received from peerinfo.
90  *
91  * @param cls the 'struct GetUriContext'
92  * @param peer identity of the peer
93  * @param hello addresses of the peer
94  * @param err_msg error message
95  */
96 static void
97 got_hello (void *cls, const struct GNUNET_PeerIdentity *id,
98            const struct GNUNET_HELLO_Message *hello,
99            const char *err_msg)
100 {
101   struct MeshPeer *peer;
102
103   if (NULL == id)
104     LOG (GNUNET_ERROR_TYPE_ERROR, "not a valid id\n");
105
106   peer = GMP_get (id);
107   GMP_set_hello (peer, hello);
108 }
109
110
111 /******************************************************************************/
112 /********************************    API    ***********************************/
113 /******************************************************************************/
114
115 /**
116  * Initialize the hello subsystem.
117  *
118  * @param c Configuration.
119  */
120 void
121 GMH_init (const struct GNUNET_CONFIGURATION_Handle *c)
122 {
123   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
124   GNUNET_assert (NULL != nc);
125   peerinfo = GNUNET_PEERINFO_connect (c);
126   nc = GNUNET_PEERINFO_notify (c, GNUNET_NO, &got_hello, NULL);
127 }
128
129
130 /**
131  * Shut down the hello subsystem.
132  */
133 void
134 GMH_shutdown ()
135 {
136   if (NULL != nc)
137   {
138     GNUNET_PEERINFO_notify_cancel (nc);
139     nc = NULL;
140   }
141   if (NULL != peerinfo)
142   {
143     GNUNET_PEERINFO_disconnect (peerinfo);
144     peerinfo = NULL;
145   }
146 }
147
148
149 /**
150  * Get own hello message.
151  *
152  * @return Own hello message.
153  */
154 const struct GNUNET_HELLO_Message *
155 GMH_get_mine (void)
156 {
157   return mine;
158 }
159
160
161 /**
162  * Get another peer's hello message.
163  *
164  * @param id ID of the peer whose hello message is requested.
165  *
166  * @return Hello message, if any (NULL possible).
167  */
168 const struct GNUNET_HELLO_Message *
169 GMH_get (const struct GNUNET_PeerIdentity *id)
170 {
171   return GMP_get_hello (GMP_get (id));
172 }
173
174
175 /**
176  * Convert a hello message to a string.
177  *
178  * @param h Hello message.
179  */
180 void
181 GMH_2s (const struct GNUNET_HELLO_Message *h)
182 {
183   return "hello (TODO)";
184 }
185
186