GNUnet  0.11.x
nat_auto_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2007-2017 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 
28 #include "platform.h"
29 #include "gnunet_nat_service.h"
31 #include "nat-auto.h"
32 
33 
38 {
43 
48 
53 
57  void *arc_cls;
58 };
59 
60 
67 const char *
69 {
70  switch (err)
71  {
73  return _ ("Operation Successful");
74 
76  return _ ("IPC failure");
77 
79  return _ ("Failure in network subsystem, check permissions.");
80 
82  return _ ("Encountered timeout while performing operation");
83 
85  return _ ("detected that we are offline");
86 
88  return _ ("`upnpc` command not found");
89 
91  return _ ("Failed to run `upnpc` command");
92 
94  return _ ("`upnpc' command took too long, process killed");
95 
97  return _ ("`upnpc' command failed to establish port mapping");
98 
100  return _ ("`external-ip' command not found");
101 
103  return _ ("Failed to run `external-ip` command");
104 
106  return _ ("`external-ip' command output invalid");
107 
109  return _ ("no valid address was returned by `external-ip'");
110 
112  return _ (
113  "Could not determine interface with internal/local network address");
114 
116  return _ ("No functioning gnunet-helper-nat-server installation found");
117 
119  return _ ("NAT test could not be initialized");
120 
122  return _ ("NAT test timeout reached");
123 
125  return _ ("could not register NAT");
126 
128  return _ ("No working gnunet-helper-nat-client installation found");
129 
130  default:
131  return "unknown status code";
132  }
133 }
134 
135 
143 static int
144 check_auto_result (void *cls,
146 {
147  return GNUNET_OK;
148 }
149 
150 
157 static void
160 {
161  struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
162  size_t left;
164  enum GNUNET_NAT_Type type
165  = (enum GNUNET_NAT_Type) ntohl (res->type);
167  = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code);
168 
169  left = ntohs (res->header.size) - sizeof(*res);
171  if (GNUNET_OK !=
173  (const char *) &res[1],
174  left,
175  NULL))
176  {
177  GNUNET_break (0);
178  ah->arc (ah->arc_cls,
179  NULL,
181  type);
182  }
183  else
184  {
185  ah->arc (ah->arc_cls,
186  cfg,
187  status,
188  type);
189  }
192 }
193 
194 
201 static void
202 ah_error_handler (void *cls,
203  enum GNUNET_MQ_Error error)
204 {
205  struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
206 
207  ah->arc (ah->arc_cls,
208  NULL,
212 }
213 
214 
227  void *cb_cls)
228 {
229  struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new (struct
231  struct GNUNET_MQ_MessageHandler handlers[] = {
232  GNUNET_MQ_hd_var_size (auto_result,
235  ah),
237  };
238  struct GNUNET_MQ_Envelope *env;
240  char *buf;
241  size_t size;
242 
244  &size);
245  if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req))
246  {
247  GNUNET_break (0);
248  GNUNET_free (buf);
249  GNUNET_free (ah);
250  return NULL;
251  }
252  ah->arc = cb;
253  ah->arc_cls = cb_cls;
254  ah->mq = GNUNET_CLIENT_connect (cfg,
255  "nat",
256  handlers,
258  ah);
259  if (NULL == ah->mq)
260  {
261  GNUNET_break (0);
262  GNUNET_free (buf);
263  GNUNET_free (ah);
264  return NULL;
265  }
266  env = GNUNET_MQ_msg_extra (req,
267  size,
269  GNUNET_memcpy (&req[1],
270  buf,
271  size);
272  GNUNET_free (buf);
273  GNUNET_MQ_send (ah->mq,
274  env);
275  return ah;
276 }
277 
278 
284 void
286 {
287  GNUNET_MQ_destroy (ah->mq);
288  GNUNET_free (ah);
289 }
290 
291 
292 /* end of nat_api_auto.c */
NAT test failed to initiate.
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
Message from NAT service with the autoconfiguration result.
detected that we are offline
void GNUNET_NAT_AUTO_autoconfig_cancel(struct GNUNET_NAT_AUTO_AutoHandle *ah)
Abort autoconfiguration.
Definition: nat_auto_api.c:285
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1057
static void handle_auto_result(void *cls, const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
Handle result from autoconfiguration attempt.
Definition: nat_auto_api.c:158
GNUNET_NAT_AUTO_AutoResultCallback arc
Function called with the result from the autoconfiguration.
Definition: nat_auto_api.c:52
`upnpc&#39; command failed to establish port mapping
GNUNET_MQ_Error
Error codes for the queue.
Client requesting automatic configuration.
Definition: nat-auto.h:70
Service responding with proposed configuration.
Definition: nat-auto.h:84
const char * GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err)
Converts enum GNUNET_NAT_StatusCode to string.
Definition: nat_auto_api.c:68
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
`upnpc&#39; command took too long, process killed
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void(* GNUNET_NAT_AUTO_AutoResultCallback)(void *cls, const struct GNUNET_CONFIGURATION_Handle *diff, enum GNUNET_NAT_StatusCode result, enum GNUNET_NAT_Type type)
Function called with the result from the autoconfiguration.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int32_t status_code
An enum GNUNET_NAT_StatusCode in NBO.
Definition: nat-auto.h:94
`external-ip&#39; command output invalid
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
Could not determine interface with internal/local network address.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
int32_t type
An enum GNUNET_NAT_Type in NBO.
Definition: nat-auto.h:99
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_auto_api.c:42
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
GNUNET_NAT_StatusCode
Error Types for the NAT subsystem (which can then later be converted/resolved to a string) ...
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
uint16_t status
See PRISM_STATUS_*-constants.
static char buf[2048]
static int check_auto_result(void *cls, const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
Check result from autoconfiguration attempt.
Definition: nat_auto_api.c:144
No working gnunet-helper-nat-server found.
Message handler for a specific message type.
static int res
Failure in network subsystem, check permissions.
We know nothing about the NAT.
NAT test could not be initialized.
static unsigned int size
Size of the "table".
Definition: peer.c:67
Handle to auto-configuration in progress.
Definition: nat_auto_api.c:37
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
`external-ip&#39; command not found
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG
Message to ask NAT service to request autoconfiguration.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
char * GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
Serializes the given configuration.
Handle to a message queue.
Definition: mq.c:85
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
Messages for interaction with gnunet-nat-auto-service.
configuration data
Definition: configuration.c:85
Failed to run upnpc command.
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT.
Definition: nat-auto.h:89
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
"no valid address was returned by `external-ip&#39;"
static void ah_error_handler(void *cls, enum GNUNET_MQ_Error error)
Handle queue errors by reporting autoconfiguration failure.
Definition: nat_auto_api.c:202
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
struct GNUNET_NAT_AUTO_AutoHandle * GNUNET_NAT_AUTO_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_NAT_AUTO_AutoResultCallback cb, void *cb_cls)
Start auto-configuration routine.
Definition: nat_auto_api.c:225
GNUNET_NAT_Type
What the situation of the NAT connectivity.
int GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, const char *basedir)
De-serializes configuration.
Failed to run external-ip command.
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_auto_api.c:47
void * arc_cls
Closure for arc.
Definition: nat_auto_api.c:57
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.