starting re-implementation of CADET service
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_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 #include "platform.h"
22 #include "gnunet_util_lib.h"
23
24 #include "gnunet_statistics_service.h"
25 #include "gnunet_peerinfo_service.h"
26 #include "cadet_protocol.h"
27 #include "gnunet-service-cadet-new.h"
28 #include "gnunet-service-cadet-new_hello.h"
29 #include "gnunet-service-cadet-new_peer.h"
30
31 #define LOG(level, ...) GNUNET_log_from(level,"cadet-hll",__VA_ARGS__)
32
33 /**
34  * Hello message of local peer.
35  */
36 static struct GNUNET_HELLO_Message *mine;
37
38 /**
39  * Handle to peerinfo service.
40  */
41 static struct GNUNET_PEERINFO_Handle *peerinfo;
42
43 /**
44  * Iterator context.
45  */
46 static struct GNUNET_PEERINFO_NotifyContext* nc;
47
48
49 /**
50  * Process each hello message received from peerinfo.
51  *
52  * @param cls Closure (unused).
53  * @param peer Identity of the peer.
54  * @param hello Hello of the peer.
55  * @param err_msg Error message.
56  */
57 static void
58 got_hello (void *cls,
59            const struct GNUNET_PeerIdentity *id,
60            const struct GNUNET_HELLO_Message *hello,
61            const char *err_msg)
62 {
63   struct CadetPeer *peer;
64
65   if ( (NULL == id) ||
66        (NULL == hello) )
67     return;
68   if (0 == memcmp (id,
69                    &my_full_id,
70                    sizeof (struct GNUNET_PeerIdentity)))
71   {
72     GNUNET_free_non_null (mine);
73     mine = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (&hello->header);
74     return;
75   }
76
77   LOG (GNUNET_ERROR_TYPE_DEBUG,
78        "Hello for %s (%d bytes), expires on %s\n",
79        GNUNET_i2s (id),
80        GNUNET_HELLO_size (hello),
81        GNUNET_STRINGS_absolute_time_to_string (GNUNET_HELLO_get_last_expiration (hello)));
82   peer = GCP_get (id,
83                   GNUNET_YES);
84   GCP_set_hello (peer,
85                  hello);
86
87 }
88
89
90 /**
91  * Initialize the hello subsystem.
92  *
93  * @param c Configuration.
94  */
95 void
96 GCH_init (const struct GNUNET_CONFIGURATION_Handle *c)
97 {
98   GNUNET_assert (NULL == nc);
99   peerinfo = GNUNET_PEERINFO_connect (c);
100   nc = GNUNET_PEERINFO_notify (c,
101                                GNUNET_NO,
102                                &got_hello,
103                                NULL);
104 }
105
106
107 /**
108  * Shut down the hello subsystem.
109  */
110 void
111 GCH_shutdown ()
112 {
113   if (NULL != nc)
114   {
115     GNUNET_PEERINFO_notify_cancel (nc);
116     nc = NULL;
117   }
118   if (NULL != peerinfo)
119   {
120     GNUNET_PEERINFO_disconnect (peerinfo);
121     peerinfo = NULL;
122   }
123   if (NULL != mine)
124   {
125     GNUNET_free (mine);
126     mine = NULL;
127   }
128 }
129
130
131 /**
132  * Get own hello message.
133  *
134  * @return Own hello message.
135  */
136 const struct GNUNET_HELLO_Message *
137 GCH_get_mine (void)
138 {
139   return mine;
140 }
141
142 /* end of gnunet-service-cadet-new_hello.c */