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