2 This file is part of GNUnet.
3 Copyright (C) 2007-2017 GNUnet e.V.
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.
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.
17 * @author Christian Grothoff
18 * @author Milan Bouchet-Valat
20 * @file nat-auto/nat_auto_api.c
21 * Routines for NAT auto configuration.
24 #include "gnunet_nat_service.h"
25 #include "gnunet_nat_auto_service.h"
31 * Handle to auto-configuration in progress.
33 struct GNUNET_NAT_AUTO_AutoHandle
37 * Configuration we use.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
42 * Message queue for communicating with the NAT service.
44 struct GNUNET_MQ_Handle *mq;
47 * Function called with the result from the autoconfiguration.
49 GNUNET_NAT_AUTO_AutoResultCallback arc;
60 * Converts `enum GNUNET_NAT_StatusCode` to string
62 * @param err error code to resolve to a string
63 * @return point to a static string containing the error code
66 GNUNET_NAT_AUTO_status2string (enum GNUNET_NAT_StatusCode err)
70 case GNUNET_NAT_ERROR_SUCCESS:
71 return _ ("Operation Successful");
72 case GNUNET_NAT_ERROR_IPC_FAILURE:
73 return _ ("IPC failure");
74 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
75 return _ ("Failure in network subsystem, check permissions.");
76 case GNUNET_NAT_ERROR_TIMEOUT:
77 return _ ("Encountered timeout while performing operation");
78 case GNUNET_NAT_ERROR_NOT_ONLINE:
79 return _ ("detected that we are offline");
80 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
81 return _ ("`upnpc` command not found");
82 case GNUNET_NAT_ERROR_UPNPC_FAILED:
83 return _ ("Failed to run `upnpc` command");
84 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
85 return _ ("`upnpc' command took too long, process killed");
86 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
87 return _ ("`upnpc' command failed to establish port mapping");
88 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
89 return _ ("`external-ip' command not found");
90 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
91 return _ ("Failed to run `external-ip` command");
92 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
93 return _ ("`external-ip' command output invalid");
94 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
95 return _ ("no valid address was returned by `external-ip'");
96 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
97 return _ ("Could not determine interface with internal/local network address");
98 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
99 return _ ("No functioning gnunet-helper-nat-server installation found");
100 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
101 return _ ("NAT test could not be initialized");
102 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
103 return _ ("NAT test timeout reached");
104 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
105 return _ ("could not register NAT");
106 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
107 return _ ("No working gnunet-helper-nat-client installation found");
109 return "unknown status code";
115 * Check result from autoconfiguration attempt.
117 * @param cls the `struct GNUNET_NAT_AUTO_AutoHandle`
118 * @param res the result
119 * @return #GNUNET_OK if @a res is well-formed (always for now)
122 check_auto_result (void *cls,
123 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
130 * Handle result from autoconfiguration attempt.
132 * @param cls the `struct GNUNET_NAT_AUTO_AutoHandle`
133 * @param res the result
136 handle_auto_result (void *cls,
137 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
139 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
141 struct GNUNET_CONFIGURATION_Handle *cfg;
142 enum GNUNET_NAT_Type type
143 = (enum GNUNET_NAT_Type) ntohl (res->type);
144 enum GNUNET_NAT_StatusCode status
145 = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code);
147 left = ntohs (res->header.size) - sizeof (*res);
148 cfg = GNUNET_CONFIGURATION_create ();
150 GNUNET_CONFIGURATION_deserialize (cfg,
151 (const char *) &res[1],
156 ah->arc (ah->arc_cls,
158 GNUNET_NAT_ERROR_IPC_FAILURE,
163 ah->arc (ah->arc_cls,
168 GNUNET_CONFIGURATION_destroy (cfg);
169 GNUNET_NAT_AUTO_autoconfig_cancel (ah);
174 * Handle queue errors by reporting autoconfiguration failure.
176 * @param cls the `struct GNUNET_NAT_AUTO_AutoHandle *`
177 * @param error details about the error
180 ah_error_handler (void *cls,
181 enum GNUNET_MQ_Error error)
183 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
185 ah->arc (ah->arc_cls,
187 GNUNET_NAT_ERROR_IPC_FAILURE,
188 GNUNET_NAT_TYPE_UNKNOWN);
189 GNUNET_NAT_AUTO_autoconfig_cancel (ah);
194 * Start auto-configuration routine. The transport adapters should
195 * be stopped while this function is called.
197 * @param cfg initial configuration
198 * @param cb function to call with autoconfiguration result
199 * @param cb_cls closure for @a cb
200 * @return handle to cancel operation
202 struct GNUNET_NAT_AUTO_AutoHandle *
203 GNUNET_NAT_AUTO_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
204 GNUNET_NAT_AUTO_AutoResultCallback cb,
207 struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new (struct GNUNET_NAT_AUTO_AutoHandle);
208 struct GNUNET_MQ_MessageHandler handlers[] = {
209 GNUNET_MQ_hd_var_size (auto_result,
210 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT,
211 struct GNUNET_NAT_AUTO_AutoconfigResultMessage,
213 GNUNET_MQ_handler_end ()
215 struct GNUNET_MQ_Envelope *env;
216 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *req;
220 buf = GNUNET_CONFIGURATION_serialize (cfg,
222 if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof (*req))
230 ah->arc_cls = cb_cls;
231 ah->mq = GNUNET_CLIENT_connect (cfg,
243 env = GNUNET_MQ_msg_extra (req,
245 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG);
246 GNUNET_memcpy (&req[1],
250 GNUNET_MQ_send (ah->mq,
257 * Abort autoconfiguration.
259 * @param ah handle for operation to abort
262 GNUNET_NAT_AUTO_autoconfig_cancel (struct GNUNET_NAT_AUTO_AutoHandle *ah)
264 GNUNET_MQ_destroy (ah->mq);
268 /* end of nat_api_auto.c */