gnunet-android

GNUnet for Android
Log | Files | Refs | README

gnunet_nat_lib.h (6080B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2024 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 #include "gnunet_common.h"
     22 #if !defined (__GNUNET_UTIL_LIB_H_INSIDE__)
     23 #error "Only <gnunet_util_lib.h> can be included directly."
     24 #endif
     25 
     26 /**
     27  * @addtogroup Backbone
     28  * @{
     29  *
     30  * @file NAT traversal
     31  * @author t3sserakt
     32  *
     33  * @defgroup nat  NAT traversal
     34  *
     35  * @{
     36  */
     37 #ifndef GNUNET_NAT_LIB_H
     38 #define GNUNET_NAT_LIB_H
     39 
     40 GNUNET_NETWORK_STRUCT_BEGIN
     41 /**
     42  * Wrapper struct with the average RTT of message to some peer
     43  * and if this peer und us is ready to sync.
     44  */
     45 struct GNUNET_BurstSync
     46 {
     47   /**
     48    * The average RTT for the peer to communicate with.
     49    */
     50   struct GNUNET_TIME_RelativeNBO rtt_average;
     51 
     52   /**
     53    * Is this peer already ready to sync.
     54    */
     55   enum GNUNET_GenericReturnValue sync_ready;
     56 };
     57 
     58 /**
     59  * Message send during burst mode.
     60  */
     61 struct GNUNET_BurstMessage
     62 {
     63   /**
     64    * The peer who send the msg.
     65    */
     66   struct GNUNET_PeerIdentity peer;
     67 
     68   /**
     69    * The local port the message was send from.
     70    */
     71   unsigned int local_port;
     72 };
     73 GNUNET_NETWORK_STRUCT_END
     74 
     75 /**
     76  * Struct wrapping information we use for starting the burst.
     77  */
     78 struct GNUNET_StartBurstCls
     79 {
     80   /**
     81    * A Context which can be inserted into this struct, which is specific for the caller.
     82    */
     83   void *context;
     84 
     85   /**
     86    * The average RTT between the peers.
     87    */
     88   struct GNUNET_TIME_Relative rtt;
     89 
     90   /**
     91    * The delay - calculate from the RTT and which peer was ready to sync first, after
     92    * we will start the burst.
     93    */
     94   struct GNUNET_TIME_Relative delay;
     95 
     96   /**
     97    * Peerstore request to start burst with natted addresses only.
     98    */
     99   struct GNUNET_PEERSTORE_Monitor *mo;
    100 
    101   /**
    102    * The VirtualLink of the peer to which we like to burst with.
    103    */
    104   struct VirtualLink *vl;
    105 
    106   /**
    107    * We are ready to start the burst.
    108    */
    109   unsigned int sync_ready;
    110 };
    111 
    112 struct GNUNET_UdpSocketInfo;
    113 
    114 typedef void (*GNUNET_NotifyUdpSocket) (struct GNUNET_UdpSocketInfo *sock_info);
    115 
    116 /**
    117  * Struct with the socket we like to use to send messages to another peer.
    118  */
    119 struct GNUNET_UdpSocketInfo
    120 {
    121   /**
    122    * This is a linked list.
    123    */
    124   struct GNUNET_UdpSocketInfo *prev;
    125 
    126   /**
    127    * This is a linked list.
    128    */
    129   struct GNUNET_UdpSocketInfo *next;
    130 
    131   // The peer the socket is for.
    132   const struct GNUNET_PeerIdentity *peer;
    133 
    134   // The socket to send message with to the peer.
    135   struct GNUNET_NETWORK_Handle *udp_sock;
    136 
    137   /**
    138    * The actual RTT between the peers.
    139    */
    140   struct GNUNET_TIME_Relative rtt;
    141 
    142   /**
    143    * The peer we like to connect to.
    144    */
    145   struct GNUNET_PeerIdentity *pid;
    146 
    147   /**
    148    * The notify function to call if burst mode was successful.
    149    */
    150   GNUNET_NotifyUdpSocket nus;
    151 
    152   /**
    153    * The read task for retrieving a burst message for this socket.
    154    */
    155   struct GNUNET_SCHEDULER_Task *read_task;
    156 
    157   /**
    158    * Timeout task for this socket.
    159    */
    160   struct GNUNET_SCHEDULER_Task *timeout_task;
    161 
    162   /**
    163    * The address of the other peer without port.
    164    */
    165   char *address;
    166 
    167   /**
    168    * Our address without port.
    169    */
    170   const char *bind_address;
    171 
    172   /**
    173    * The port we are bound to.
    174    */
    175   unsigned int port;
    176 
    177   /**
    178    * The address of the other peer we received a burst message from.
    179    */
    180   struct sockaddr *actual_address;
    181 
    182   /**
    183    * Default local port we are bound to.
    184    */
    185   unsigned int std_port;
    186 
    187   /**
    188    * Flag indicating, if the address is without port information.
    189    */
    190   enum GNUNET_GenericReturnValue has_port;
    191 };
    192 
    193 
    194 /**
    195  * Create @a GNUNET_BurstSync message.
    196  *
    197  * @param rtt_average The average RTT for the peer to communicate with.
    198  * @param sync_ready Is this peer already ready to sync.
    199  *
    200  * @return The GNUNET_BurstSync message to send to the other peer.
    201  */
    202 struct GNUNET_BurstSync *
    203 GNUNET_get_burst_sync_msg (struct GNUNET_TIME_Relative rtt_average,
    204                            enum GNUNET_GenericReturnValue sync_ready);
    205 
    206 
    207 /**
    208  * Checks if we are ready and starts burst when we and the other peer is ready.
    209  *
    210  * @param rtt_average The average RTT for the peer to communicate with.
    211  * @param sync_ready Is this peer already ready to sync.
    212  * @param burst_sync The GNUNET_BurstSync from the other peer.
    213  * @param task Task to be executed if both peers are ready.
    214  * @param task_cls Closure for the task.
    215  *
    216  * @return Are we burst ready. This is independent from the other peer being ready.
    217  */
    218 void
    219 GNUNET_is_burst_ready (struct GNUNET_TIME_Relative rtt_average,
    220                        struct GNUNET_BurstSync *burst_sync,
    221                        GNUNET_SCHEDULER_TaskCallback task,
    222                        struct GNUNET_StartBurstCls *task_cls);
    223 
    224 
    225 /**
    226  * Method to get a UDP socket for a peer that is natted.
    227  *
    228  * @param sock_info Struct with information correlated to a specific port at the other peer.
    229  * @param nus Callback to give the caller the struct GNUNET_UdpSocketInfo to use to connect the other peer.
    230  * @return The initial read task to read from the default socket.
    231  */
    232 struct GNUNET_SCHEDULER_Task * 
    233 GNUNET_get_udp_socket (struct GNUNET_UdpSocketInfo *sock_info,
    234                        GNUNET_NotifyUdpSocket nus);
    235 
    236 /**
    237  * Method to stop all sockets we established to the other peer.
    238  *
    239  * @param do_not_touch The network handle we will use to connect to the other peer. This socket must not be closed.
    240  */
    241 void
    242 GNUNET_stop_burst (struct GNUNET_NETWORK_Handle *do_not_touch);
    243 
    244 #endif
    245 
    246 /** @} */  /* end of group */
    247 
    248 /** @} */  /* end of group addition to backbone */