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 
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 _("Could not determine interface with internal/local network address");
113 
115  return _("No functioning gnunet-helper-nat-server installation found");
116 
118  return _("NAT test could not be initialized");
119 
121  return _("NAT test timeout reached");
122 
124  return _("could not register NAT");
125 
127  return _("No working gnunet-helper-nat-client installation found");
128 
129  default:
130  return "unknown status code";
131  }
132 }
133 
134 
142 static int
145 {
146  return GNUNET_OK;
147 }
148 
149 
156 static void
159 {
160  struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
161  size_t left;
163  enum GNUNET_NAT_Type type
164  = (enum GNUNET_NAT_Type)ntohl(res->type);
166  = (enum GNUNET_NAT_StatusCode)ntohl(res->status_code);
167 
168  left = ntohs(res->header.size) - sizeof(*res);
170  if (GNUNET_OK !=
172  (const char *)&res[1],
173  left,
174  NULL))
175  {
176  GNUNET_break(0);
177  ah->arc(ah->arc_cls,
178  NULL,
180  type);
181  }
182  else
183  {
184  ah->arc(ah->arc_cls,
185  cfg,
186  status,
187  type);
188  }
191 }
192 
193 
200 static void
202  enum GNUNET_MQ_Error error)
203 {
204  struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
205 
206  ah->arc(ah->arc_cls,
207  NULL,
211 }
212 
213 
226  void *cb_cls)
227 {
229  struct GNUNET_MQ_MessageHandler handlers[] = {
230  GNUNET_MQ_hd_var_size(auto_result,
233  ah),
235  };
236  struct GNUNET_MQ_Envelope *env;
238  char *buf;
239  size_t size;
240 
242  &size);
243  if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req))
244  {
245  GNUNET_break(0);
246  GNUNET_free(buf);
247  GNUNET_free(ah);
248  return NULL;
249  }
250  ah->arc = cb;
251  ah->arc_cls = cb_cls;
252  ah->mq = GNUNET_CLIENT_connect(cfg,
253  "nat",
254  handlers,
256  ah);
257  if (NULL == ah->mq)
258  {
259  GNUNET_break(0);
260  GNUNET_free(buf);
261  GNUNET_free(ah);
262  return NULL;
263  }
264  env = GNUNET_MQ_msg_extra(req,
265  size,
267  GNUNET_memcpy(&req[1],
268  buf,
269  size);
270  GNUNET_free(buf);
271  GNUNET_MQ_send(ah->mq,
272  env);
273  return ah;
274 }
275 
276 
282 void
284 {
285  GNUNET_MQ_destroy(ah->mq);
286  GNUNET_free(ah);
287 }
288 
289 /* 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:283
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:900
static void handle_auto_result(void *cls, const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
Handle result from autoconfiguration attempt.
Definition: nat_auto_api.c:157
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:83
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:92
`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:97
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:143
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:66
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:84
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
Messages for interaction with gnunet-nat-auto-service.
configuration data
Definition: configuration.c:83
Failed to run upnpc command.
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT.
Definition: nat-auto.h:87
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:201
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
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:351
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:224
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.