GNUnet  0.10.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 
34 
39 {
40 
45 
50 
55 
59  void *arc_cls;
60 
61 };
62 
63 
70 const char *
72 {
73  switch (err)
74  {
76  return _ ("Operation Successful");
78  return _ ("IPC failure");
80  return _ ("Failure in network subsystem, check permissions.");
82  return _ ("Encountered timeout while performing operation");
84  return _ ("detected that we are offline");
86  return _ ("`upnpc` command not found");
88  return _ ("Failed to run `upnpc` command");
90  return _ ("`upnpc' command took too long, process killed");
92  return _ ("`upnpc' command failed to establish port mapping");
94  return _ ("`external-ip' command not found");
96  return _ ("Failed to run `external-ip` command");
98  return _ ("`external-ip' command output invalid");
100  return _ ("no valid address was returned by `external-ip'");
102  return _ ("Could not determine interface with internal/local network address");
104  return _ ("No functioning gnunet-helper-nat-server installation found");
106  return _ ("NAT test could not be initialized");
108  return _ ("NAT test timeout reached");
110  return _ ("could not register NAT");
112  return _ ("No working gnunet-helper-nat-client installation found");
113  default:
114  return "unknown status code";
115  }
116 }
117 
118 
126 static int
127 check_auto_result (void *cls,
129 {
130  return GNUNET_OK;
131 }
132 
133 
140 static void
143 {
144  struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
145  size_t left;
147  enum GNUNET_NAT_Type type
148  = (enum GNUNET_NAT_Type) ntohl (res->type);
150  = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code);
151 
152  left = ntohs (res->header.size) - sizeof (*res);
154  if (GNUNET_OK !=
156  (const char *) &res[1],
157  left,
158  NULL))
159  {
160  GNUNET_break (0);
161  ah->arc (ah->arc_cls,
162  NULL,
164  type);
165  }
166  else
167  {
168  ah->arc (ah->arc_cls,
169  cfg,
170  status,
171  type);
172  }
175 }
176 
177 
184 static void
185 ah_error_handler (void *cls,
186  enum GNUNET_MQ_Error error)
187 {
188  struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
189 
190  ah->arc (ah->arc_cls,
191  NULL,
195 }
196 
197 
210  void *cb_cls)
211 {
213  struct GNUNET_MQ_MessageHandler handlers[] = {
214  GNUNET_MQ_hd_var_size (auto_result,
217  ah),
219  };
220  struct GNUNET_MQ_Envelope *env;
222  char *buf;
223  size_t size;
224 
226  &size);
227  if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof (*req))
228  {
229  GNUNET_break (0);
230  GNUNET_free (buf);
231  GNUNET_free (ah);
232  return NULL;
233  }
234  ah->arc = cb;
235  ah->arc_cls = cb_cls;
236  ah->mq = GNUNET_CLIENT_connect (cfg,
237  "nat",
238  handlers,
240  ah);
241  if (NULL == ah->mq)
242  {
243  GNUNET_break (0);
244  GNUNET_free (buf);
245  GNUNET_free (ah);
246  return NULL;
247  }
248  env = GNUNET_MQ_msg_extra (req,
249  size,
251  GNUNET_memcpy (&req[1],
252  buf,
253  size);
254  GNUNET_free (buf);
255  GNUNET_MQ_send (ah->mq,
256  env);
257  return ah;
258 }
259 
260 
266 void
268 {
269  GNUNET_MQ_destroy (ah->mq);
270  GNUNET_free (ah);
271 }
272 
273 /* 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:267
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:901
static void handle_auto_result(void *cls, const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
Handle result from autoconfiguration attempt.
Definition: nat_auto_api.c:141
GNUNET_NAT_AUTO_AutoResultCallback arc
Function called with the result from the autoconfiguration.
Definition: nat_auto_api.c:54
`upnpc&#39; command failed to establish port mapping
GNUNET_MQ_Error
Error codes for the queue.
Client requesting automatic configuration.
Definition: nat-auto.h:72
Service responding with proposed configuration.
Definition: nat-auto.h:87
const char * GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err)
Converts enum GNUNET_NAT_StatusCode to string.
Definition: nat_auto_api.c:71
`upnpc&#39; command took too long, process killed
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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:97
`external-ip&#39; command output invalid
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
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
#define GNUNET_memcpy(dst, src, n)
int32_t type
An enum GNUNET_NAT_Type in NBO.
Definition: nat-auto.h:102
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_auto_api.c:44
#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:127
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:38
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:92
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:185
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:208
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:49
void * arc_cls
Closure for arc.
Definition: nat_auto_api.c:59
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.