replace printf() with GNUNET_log()
[oweals/gnunet.git] / src / transport / transport_api_offer_hello.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016 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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file transport/transport_api_offer_hello.c
23  * @brief library to offer HELLOs to transport service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_transport_service.h"
31
32
33 /**
34  * Entry in linked list for all offer-HELLO requests.
35  */
36 struct GNUNET_TRANSPORT_OfferHelloHandle
37 {
38   /**
39    * Transport service handle we use for transmission.
40    */
41   struct GNUNET_MQ_Handle *mq;
42
43   /**
44    * Function to call once we are done.
45    */
46   GNUNET_SCHEDULER_TaskCallback cont;
47
48   /**
49    * Closure for @e cont
50    */
51   void *cls;
52 };
53
54
55 /**
56  * Done sending HELLO message to the service, notify application.
57  *
58  * @param cls the handle for the operation
59  */
60 static void
61 finished_hello (void *cls)
62 {
63   struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
64
65   if (NULL != ohh->cont)
66     ohh->cont (ohh->cls);
67   GNUNET_TRANSPORT_offer_hello_cancel (ohh);
68 }
69
70
71 /**
72  * Offer the transport service the HELLO of another peer.  Note that
73  * the transport service may just ignore this message if the HELLO is
74  * malformed or useless due to our local configuration.
75  *
76  * @param cfg configuration
77  * @param hello the hello message
78  * @param cont continuation to call when HELLO has been sent,
79  *      tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
80  *      tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
81  * @param cont_cls closure for @a cont
82  * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
83  *      in case of failure @a cont will not be called
84  *
85  */
86 struct GNUNET_TRANSPORT_OfferHelloHandle *
87 GNUNET_TRANSPORT_offer_hello (const struct GNUNET_CONFIGURATION_Handle *cfg,
88                               const struct GNUNET_MessageHeader *hello,
89                               GNUNET_SCHEDULER_TaskCallback cont,
90                               void *cont_cls)
91 {
92   struct GNUNET_TRANSPORT_OfferHelloHandle *ohh
93     = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
94   struct GNUNET_MQ_Envelope *env;
95   struct GNUNET_PeerIdentity peer;
96
97   if (GNUNET_OK !=
98       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello,
99                            &peer))
100   {
101     GNUNET_break (0);
102     GNUNET_free (ohh);
103     return NULL;
104   }
105   ohh->mq = GNUNET_CLIENT_connect (cfg,
106                                    "transport",
107                                    NULL,
108                                    NULL,
109                                    ohh);
110   if (NULL == ohh->mq)
111   {
112     GNUNET_free (ohh);
113     return NULL;
114   }
115   ohh->cont = cont;
116   ohh->cls = cont_cls;
117   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
118   env = GNUNET_MQ_msg_copy (hello);
119   GNUNET_MQ_notify_sent (env,
120                          &finished_hello,
121                          ohh);
122   GNUNET_MQ_send (ohh->mq,
123                   env);
124   return ohh;
125 }
126
127
128 /**
129  * Cancel the request to transport to offer the HELLO message
130  *
131  * @param ohh the handle for the operation to cancel
132  */
133 void
134 GNUNET_TRANSPORT_offer_hello_cancel (struct
135                                      GNUNET_TRANSPORT_OfferHelloHandle *ohh)
136 {
137   GNUNET_MQ_destroy (ohh->mq);
138   GNUNET_free (ohh);
139 }
140
141
142 /* end of transport_api_offer_hello.c */