aboutsummaryrefslogtreecommitdiff
path: root/src/nat
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-01-05 14:39:22 +0100
committerChristian Grothoff <christian@grothoff.org>2017-01-05 14:39:22 +0100
commit4eebf33c266d28d6260118c83817fc59bc645dc8 (patch)
tree8673f753b109e4ea7f2af48dc33b8bac2beeeed4 /src/nat
parentaad1449328ffa0fbc78f9f4279417a4392218ec3 (diff)
downloadgnunet-4eebf33c266d28d6260118c83817fc59bc645dc8.tar.gz
gnunet-4eebf33c266d28d6260118c83817fc59bc645dc8.zip
splitting off ant_api_auto.c from main nat_api.c
Diffstat (limited to 'src/nat')
-rw-r--r--src/nat/Makefile.am1
-rw-r--r--src/nat/nat_api.c239
-rw-r--r--src/nat/nat_api_auto.c274
3 files changed, 275 insertions, 239 deletions
diff --git a/src/nat/Makefile.am b/src/nat/Makefile.am
index 1dd8e44b9..767cf8d82 100644
--- a/src/nat/Makefile.am
+++ b/src/nat/Makefile.am
@@ -85,6 +85,7 @@ libgnunetnat_la_LDFLAGS = \
85 85
86libgnunetnatnew_la_SOURCES = \ 86libgnunetnatnew_la_SOURCES = \
87 nat_api.c \ 87 nat_api.c \
88 nat_api_auto.c \
88 nat_api_stun.c nat_stun.h \ 89 nat_api_stun.c nat_stun.h \
89 nat_api_test.c \ 90 nat_api_test.c \
90 nat.h 91 nat.h
diff --git a/src/nat/nat_api.c b/src/nat/nat_api.c
index e4dfc1629..008ad19e4 100644
--- a/src/nat/nat_api.c
+++ b/src/nat/nat_api.c
@@ -716,243 +716,4 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh)
716} 716}
717 717
718 718
719
720/**
721 * Handle to auto-configuration in progress.
722 */
723struct GNUNET_NAT_AutoHandle
724{
725
726 /**
727 * Configuration we use.
728 */
729 const struct GNUNET_CONFIGURATION_Handle *cfg;
730
731 /**
732 * Message queue for communicating with the NAT service.
733 */
734 struct GNUNET_MQ_Handle *mq;
735
736 /**
737 * Function called with the result from the autoconfiguration.
738 */
739 GNUNET_NAT_AutoResultCallback arc;
740
741 /**
742 * Closure for @e arc.
743 */
744 void *arc_cls;
745
746};
747
748
749/**
750 * Converts `enum GNUNET_NAT_StatusCode` to string
751 *
752 * @param err error code to resolve to a string
753 * @return point to a static string containing the error code
754 */
755const char *
756GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err)
757{
758 switch (err)
759 {
760 case GNUNET_NAT_ERROR_SUCCESS:
761 return _ ("Operation Successful");
762 case GNUNET_NAT_ERROR_IPC_FAILURE:
763 return _ ("IPC failure");
764 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
765 return _ ("Failure in network subsystem, check permissions.");
766 case GNUNET_NAT_ERROR_TIMEOUT:
767 return _ ("Encountered timeout while performing operation");
768 case GNUNET_NAT_ERROR_NOT_ONLINE:
769 return _ ("detected that we are offline");
770 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
771 return _ ("`upnpc` command not found");
772 case GNUNET_NAT_ERROR_UPNPC_FAILED:
773 return _ ("Failed to run `upnpc` command");
774 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
775 return _ ("`upnpc' command took too long, process killed");
776 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
777 return _ ("`upnpc' command failed to establish port mapping");
778 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
779 return _ ("`external-ip' command not found");
780 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
781 return _ ("Failed to run `external-ip` command");
782 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
783 return _ ("`external-ip' command output invalid");
784 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
785 return _ ("no valid address was returned by `external-ip'");
786 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
787 return _ ("Could not determine interface with internal/local network address");
788 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
789 return _ ("No functioning gnunet-helper-nat-server installation found");
790 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
791 return _ ("NAT test could not be initialized");
792 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
793 return _ ("NAT test timeout reached");
794 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
795 return _ ("could not register NAT");
796 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
797 return _ ("No working gnunet-helper-nat-client installation found");
798 default:
799 return "unknown status code";
800 }
801}
802
803
804/**
805 * Check result from autoconfiguration attempt.
806 *
807 * @param cls the `struct GNUNET_NAT_AutoHandle`
808 * @param res the result
809 * @return #GNUNET_OK if @a res is well-formed (always for now)
810 */
811static int
812check_auto_result (void *cls,
813 const struct GNUNET_NAT_AutoconfigResultMessage *res)
814{
815 return GNUNET_OK;
816}
817
818
819/**
820 * Handle result from autoconfiguration attempt.
821 *
822 * @param cls the `struct GNUNET_NAT_AutoHandle`
823 * @param res the result
824 */
825static void
826handle_auto_result (void *cls,
827 const struct GNUNET_NAT_AutoconfigResultMessage *res)
828{
829 struct GNUNET_NAT_AutoHandle *ah = cls;
830 size_t left;
831 struct GNUNET_CONFIGURATION_Handle *cfg;
832 enum GNUNET_NAT_Type type
833 = (enum GNUNET_NAT_Type) ntohl (res->type);
834 enum GNUNET_NAT_StatusCode status
835 = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code);
836
837 left = ntohs (res->header.size) - sizeof (*res);
838 cfg = GNUNET_CONFIGURATION_create ();
839 if (GNUNET_OK !=
840 GNUNET_CONFIGURATION_deserialize (cfg,
841 (const char *) &res[1],
842 left,
843 GNUNET_NO))
844 {
845 GNUNET_break (0);
846 ah->arc (ah->arc_cls,
847 NULL,
848 GNUNET_NAT_ERROR_IPC_FAILURE,
849 type);
850 }
851 else
852 {
853 ah->arc (ah->arc_cls,
854 cfg,
855 status,
856 type);
857 }
858 GNUNET_CONFIGURATION_destroy (cfg);
859 GNUNET_NAT_autoconfig_cancel (ah);
860}
861
862
863/**
864 * Handle queue errors by reporting autoconfiguration failure.
865 *
866 * @param cls the `struct GNUNET_NAT_AutoHandle *`
867 * @param error details about the error
868 */
869static void
870ah_error_handler (void *cls,
871 enum GNUNET_MQ_Error error)
872{
873 struct GNUNET_NAT_AutoHandle *ah = cls;
874
875 ah->arc (ah->arc_cls,
876 NULL,
877 GNUNET_NAT_ERROR_IPC_FAILURE,
878 GNUNET_NAT_TYPE_UNKNOWN);
879 GNUNET_NAT_autoconfig_cancel (ah);
880}
881
882
883/**
884 * Start auto-configuration routine. The transport adapters should
885 * be stopped while this function is called.
886 *
887 * @param cfg initial configuration
888 * @param cb function to call with autoconfiguration result
889 * @param cb_cls closure for @a cb
890 * @return handle to cancel operation
891 */
892struct GNUNET_NAT_AutoHandle *
893GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
894 GNUNET_NAT_AutoResultCallback cb,
895 void *cb_cls)
896{
897 struct GNUNET_NAT_AutoHandle *ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
898 struct GNUNET_MQ_MessageHandler handlers[] = {
899 GNUNET_MQ_hd_var_size (auto_result,
900 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT,
901 struct GNUNET_NAT_AutoconfigResultMessage,
902 ah),
903 GNUNET_MQ_handler_end ()
904 };
905 struct GNUNET_MQ_Envelope *env;
906 struct GNUNET_NAT_AutoconfigRequestMessage *req;
907 char *buf;
908 size_t size;
909
910 buf = GNUNET_CONFIGURATION_serialize (cfg,
911 &size);
912 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*req))
913 {
914 GNUNET_break (0);
915 GNUNET_free (buf);
916 GNUNET_free (ah);
917 return NULL;
918 }
919 ah->arc = cb;
920 ah->arc_cls = cb_cls;
921 ah->mq = GNUNET_CLIENT_connecT (cfg,
922 "nat",
923 handlers,
924 &ah_error_handler,
925 ah);
926 if (NULL == ah->mq)
927 {
928 GNUNET_break (0);
929 GNUNET_free (buf);
930 GNUNET_free (ah);
931 return NULL;
932 }
933 env = GNUNET_MQ_msg_extra (req,
934 size,
935 GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG);
936 GNUNET_memcpy (&req[1],
937 buf,
938 size);
939 GNUNET_free (buf);
940 GNUNET_MQ_send (ah->mq,
941 env);
942 return ah;
943}
944
945
946/**
947 * Abort autoconfiguration.
948 *
949 * @param ah handle for operation to abort
950 */
951void
952GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
953{
954 GNUNET_MQ_destroy (ah->mq);
955 GNUNET_free (ah);
956}
957
958/* end of nat_api.c */ 719/* end of nat_api.c */
diff --git a/src/nat/nat_api_auto.c b/src/nat/nat_api_auto.c
new file mode 100644
index 000000000..c15a7fc36
--- /dev/null
+++ b/src/nat/nat_api_auto.c
@@ -0,0 +1,274 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2007-2016 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @author Christian Grothoff
24 * @author Milan Bouchet-Valat
25 *
26 * @file nat/nat_api_auto.c.c
27 * Routines for NAT auto configuration.
28 */
29#include "platform.h"
30#include "gnunet_nat_service.h"
31#include "nat.h"
32#include "nat_stun.h"
33
34
35
36/**
37 * Handle to auto-configuration in progress.
38 */
39struct GNUNET_NAT_AutoHandle
40{
41
42 /**
43 * Configuration we use.
44 */
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 /**
48 * Message queue for communicating with the NAT service.
49 */
50 struct GNUNET_MQ_Handle *mq;
51
52 /**
53 * Function called with the result from the autoconfiguration.
54 */
55 GNUNET_NAT_AutoResultCallback arc;
56
57 /**
58 * Closure for @e arc.
59 */
60 void *arc_cls;
61
62};
63
64
65/**
66 * Converts `enum GNUNET_NAT_StatusCode` to string
67 *
68 * @param err error code to resolve to a string
69 * @return point to a static string containing the error code
70 */
71const char *
72GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err)
73{
74 switch (err)
75 {
76 case GNUNET_NAT_ERROR_SUCCESS:
77 return _ ("Operation Successful");
78 case GNUNET_NAT_ERROR_IPC_FAILURE:
79 return _ ("IPC failure");
80 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
81 return _ ("Failure in network subsystem, check permissions.");
82 case GNUNET_NAT_ERROR_TIMEOUT:
83 return _ ("Encountered timeout while performing operation");
84 case GNUNET_NAT_ERROR_NOT_ONLINE:
85 return _ ("detected that we are offline");
86 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
87 return _ ("`upnpc` command not found");
88 case GNUNET_NAT_ERROR_UPNPC_FAILED:
89 return _ ("Failed to run `upnpc` command");
90 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
91 return _ ("`upnpc' command took too long, process killed");
92 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
93 return _ ("`upnpc' command failed to establish port mapping");
94 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
95 return _ ("`external-ip' command not found");
96 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
97 return _ ("Failed to run `external-ip` command");
98 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
99 return _ ("`external-ip' command output invalid");
100 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
101 return _ ("no valid address was returned by `external-ip'");
102 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
103 return _ ("Could not determine interface with internal/local network address");
104 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
105 return _ ("No functioning gnunet-helper-nat-server installation found");
106 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
107 return _ ("NAT test could not be initialized");
108 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
109 return _ ("NAT test timeout reached");
110 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
111 return _ ("could not register NAT");
112 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
113 return _ ("No working gnunet-helper-nat-client installation found");
114 default:
115 return "unknown status code";
116 }
117}
118
119
120/**
121 * Check result from autoconfiguration attempt.
122 *
123 * @param cls the `struct GNUNET_NAT_AutoHandle`
124 * @param res the result
125 * @return #GNUNET_OK if @a res is well-formed (always for now)
126 */
127static int
128check_auto_result (void *cls,
129 const struct GNUNET_NAT_AutoconfigResultMessage *res)
130{
131 return GNUNET_OK;
132}
133
134
135/**
136 * Handle result from autoconfiguration attempt.
137 *
138 * @param cls the `struct GNUNET_NAT_AutoHandle`
139 * @param res the result
140 */
141static void
142handle_auto_result (void *cls,
143 const struct GNUNET_NAT_AutoconfigResultMessage *res)
144{
145 struct GNUNET_NAT_AutoHandle *ah = cls;
146 size_t left;
147 struct GNUNET_CONFIGURATION_Handle *cfg;
148 enum GNUNET_NAT_Type type
149 = (enum GNUNET_NAT_Type) ntohl (res->type);
150 enum GNUNET_NAT_StatusCode status
151 = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code);
152
153 left = ntohs (res->header.size) - sizeof (*res);
154 cfg = GNUNET_CONFIGURATION_create ();
155 if (GNUNET_OK !=
156 GNUNET_CONFIGURATION_deserialize (cfg,
157 (const char *) &res[1],
158 left,
159 GNUNET_NO))
160 {
161 GNUNET_break (0);
162 ah->arc (ah->arc_cls,
163 NULL,
164 GNUNET_NAT_ERROR_IPC_FAILURE,
165 type);
166 }
167 else
168 {
169 ah->arc (ah->arc_cls,
170 cfg,
171 status,
172 type);
173 }
174 GNUNET_CONFIGURATION_destroy (cfg);
175 GNUNET_NAT_autoconfig_cancel (ah);
176}
177
178
179/**
180 * Handle queue errors by reporting autoconfiguration failure.
181 *
182 * @param cls the `struct GNUNET_NAT_AutoHandle *`
183 * @param error details about the error
184 */
185static void
186ah_error_handler (void *cls,
187 enum GNUNET_MQ_Error error)
188{
189 struct GNUNET_NAT_AutoHandle *ah = cls;
190
191 ah->arc (ah->arc_cls,
192 NULL,
193 GNUNET_NAT_ERROR_IPC_FAILURE,
194 GNUNET_NAT_TYPE_UNKNOWN);
195 GNUNET_NAT_autoconfig_cancel (ah);
196}
197
198
199/**
200 * Start auto-configuration routine. The transport adapters should
201 * be stopped while this function is called.
202 *
203 * @param cfg initial configuration
204 * @param cb function to call with autoconfiguration result
205 * @param cb_cls closure for @a cb
206 * @return handle to cancel operation
207 */
208struct GNUNET_NAT_AutoHandle *
209GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
210 GNUNET_NAT_AutoResultCallback cb,
211 void *cb_cls)
212{
213 struct GNUNET_NAT_AutoHandle *ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
214 struct GNUNET_MQ_MessageHandler handlers[] = {
215 GNUNET_MQ_hd_var_size (auto_result,
216 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT,
217 struct GNUNET_NAT_AutoconfigResultMessage,
218 ah),
219 GNUNET_MQ_handler_end ()
220 };
221 struct GNUNET_MQ_Envelope *env;
222 struct GNUNET_NAT_AutoconfigRequestMessage *req;
223 char *buf;
224 size_t size;
225
226 buf = GNUNET_CONFIGURATION_serialize (cfg,
227 &size);
228 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*req))
229 {
230 GNUNET_break (0);
231 GNUNET_free (buf);
232 GNUNET_free (ah);
233 return NULL;
234 }
235 ah->arc = cb;
236 ah->arc_cls = cb_cls;
237 ah->mq = GNUNET_CLIENT_connecT (cfg,
238 "nat",
239 handlers,
240 &ah_error_handler,
241 ah);
242 if (NULL == ah->mq)
243 {
244 GNUNET_break (0);
245 GNUNET_free (buf);
246 GNUNET_free (ah);
247 return NULL;
248 }
249 env = GNUNET_MQ_msg_extra (req,
250 size,
251 GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG);
252 GNUNET_memcpy (&req[1],
253 buf,
254 size);
255 GNUNET_free (buf);
256 GNUNET_MQ_send (ah->mq,
257 env);
258 return ah;
259}
260
261
262/**
263 * Abort autoconfiguration.
264 *
265 * @param ah handle for operation to abort
266 */
267void
268GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
269{
270 GNUNET_MQ_destroy (ah->mq);
271 GNUNET_free (ah);
272}
273
274/* end of nat_api_auto.c */