cancel existing last_control_qe handle before scheduling a new one, always
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_hello.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2014 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 #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 "cadet_protocol.h"
28 #include "cadet_path.h"
29
30 #include "gnunet-service-cadet_hello.h"
31 #include "gnunet-service-cadet_peer.h"
32
33 #define LOG(level, ...) GNUNET_log_from(level,"cadet-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 Closure (unused).
92  * @param peer Identity of the peer.
93  * @param hello Hello 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 CadetPeer *peer;
102
103   if (NULL == id || NULL == hello)
104   {
105     LOG (GNUNET_ERROR_TYPE_DEBUG, " hello with id %p and msg %p\n", id, hello);
106     return;
107   }
108   LOG (GNUNET_ERROR_TYPE_DEBUG, " hello for %s (%d bytes), expires on %s\n",
109        GNUNET_i2s (id), GNUNET_HELLO_size (hello),
110        GNUNET_STRINGS_absolute_time_to_string (GNUNET_HELLO_get_last_expiration(hello)));
111   peer = GCP_get (id, GNUNET_YES);
112   GCP_set_hello (peer, hello);
113
114   if (GCP_get_short_id (peer) == myid)
115     mine = GCP_get_hello (peer);
116 }
117
118
119 /******************************************************************************/
120 /********************************    API    ***********************************/
121 /******************************************************************************/
122
123 /**
124  * Initialize the hello subsystem.
125  *
126  * @param c Configuration.
127  */
128 void
129 GCH_init (const struct GNUNET_CONFIGURATION_Handle *c)
130 {
131   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
132   GNUNET_assert (NULL == nc);
133   peerinfo = GNUNET_PEERINFO_connect (c);
134   nc = GNUNET_PEERINFO_notify (c, GNUNET_NO, &got_hello, NULL);
135 }
136
137
138 /**
139  * Shut down the hello subsystem.
140  */
141 void
142 GCH_shutdown ()
143 {
144   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down channels\n");
145   if (NULL != nc)
146   {
147     GNUNET_PEERINFO_notify_cancel (nc);
148     nc = NULL;
149   }
150   if (NULL != peerinfo)
151   {
152     GNUNET_PEERINFO_disconnect (peerinfo);
153     peerinfo = NULL;
154   }
155 }
156
157
158 /**
159  * Get own hello message.
160  *
161  * @return Own hello message.
162  */
163 const struct GNUNET_HELLO_Message *
164 GCH_get_mine (void)
165 {
166   return mine;
167 }
168
169
170 /**
171  * Get another peer's hello message.
172  *
173  * @param id ID of the peer whose hello message is requested.
174  *
175  * @return Hello message, if any (NULL possible).
176  */
177 const struct GNUNET_HELLO_Message *
178 GCH_get (const struct GNUNET_PeerIdentity *id)
179 {
180   struct CadetPeer *p;
181
182   p = GCP_get (id, GNUNET_NO);
183   if (NULL == p)
184     return NULL;
185   return GCP_get_hello (p);
186 }
187
188
189 /**
190  * Convert a hello message to a string.
191  *
192  * @param h Hello message.
193  */
194 char *
195 GCH_2s (const struct GNUNET_HELLO_Message *h)
196 {
197   return "hello (TODO)";
198 }
199
200