fix #3869: outdated FSF address
[oweals/gnunet.git] / src / nat / test_stun.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2015 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * Testcase for STUN server resolution
23  *
24  * @file nat/test_stun.c
25  * @brief Testcase for STUN library
26  * @author Bruno Souza Cabral - Major rewrite.
27
28  *
29  */
30
31
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_nat_lib.h"
37
38
39
40 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
41
42 /**
43  * The port the test service is running on (default 7895)
44  */
45 static unsigned long port = 7895;
46 static int ret = 1;
47
48 static char *stun_server = "stun.ekiga.net";
49 static int stun_port = 3478;
50
51 /**
52  * The listen socket of the service for IPv4
53  */
54 static struct GNUNET_NETWORK_Handle *lsock4;
55
56
57 /**
58  * The listen task ID for IPv4
59  */
60 static struct GNUNET_SCHEDULER_Task * ltask4;
61
62
63
64
65 static void
66 print_answer(struct sockaddr_in* answer)
67 {
68         printf("External IP is: %s , with port %d\n", inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
69 }
70
71
72 /**
73  * Activity on our incoming socket.  Read data from the
74  * incoming connection.
75  *
76  * @param cls 
77  * @param tc scheduler context
78  */
79 static void
80 do_udp_read (void *cls,
81              const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83     //struct GNUNET_NAT_Test *tst = cls;
84         unsigned char reply_buf[1024];
85         ssize_t rlen;
86         struct sockaddr_in answer;
87
88
89     if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
90       (GNUNET_NETWORK_fdset_isset (tc->read_ready,
91                                    lsock4)))
92         {
93                 rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));
94                 printf("Recivied something of size %d", rlen);
95                 
96                 //Lets handle the packet
97                 memset(&answer, 0, sizeof(struct sockaddr_in));
98         GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer);
99
100                 //Print the anser
101                 //TODO: Delete the object
102                 ret = 0;
103                 print_answer(&answer);
104                 
105                 
106         }
107
108
109 }
110
111
112 /**
113  * Create an IPv4 listen socket bound to our port.
114  *
115  * @return NULL on error
116  */
117 static struct GNUNET_NETWORK_Handle *
118 bind_v4 ()
119 {
120     struct GNUNET_NETWORK_Handle *ls;
121     struct sockaddr_in sa4;
122     int eno;
123
124     memset (&sa4, 0, sizeof (sa4));
125     sa4.sin_family = AF_INET;
126     sa4.sin_port = htons (port);
127 #if HAVE_SOCKADDR_IN_SIN_LEN
128     sa4.sin_len = sizeof (sa4);
129 #endif 
130     ls = GNUNET_NETWORK_socket_create (AF_INET,
131                                        SOCK_DGRAM,
132                                        0);
133     if (NULL == ls)
134         return NULL;
135     if (GNUNET_OK !=
136             GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
137                                         sizeof (sa4)))
138     {
139         eno = errno;
140         GNUNET_NETWORK_socket_close (ls);
141         errno = eno;
142         return NULL;
143     }
144     return ls;
145 }
146
147
148
149 /**
150  * Main function run with scheduler.
151  */
152
153
154 static void
155 run (void *cls, char *const *args, const char *cfgfile,
156      const struct GNUNET_CONFIGURATION_Handle *cfg)
157 {
158
159
160     //Lets create the socket
161     lsock4 = bind_v4 ();
162     if (NULL == lsock4)
163     {
164         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
165     }
166     else
167     {
168                 printf("Binded, now will call add_read\n");
169         //Lets call our function now when it accepts
170         ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
171                                                 lsock4, &do_udp_read, NULL);
172         /* So you read once and what will happen if you get an irregular message? Repeat and add timeout */
173
174     }
175     if(NULL == lsock4 )
176     {
177         /* FIXME: duplicate check  */
178         GNUNET_SCHEDULER_shutdown ();
179         return;
180     }
181     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
182                 "Service listens on port %u\n",
183                 port);
184         printf("Start main event\n");
185     GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4);
186     //Main event
187     //main_task = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
188
189 }
190
191
192 int
193 main (int argc, char *const argv[])
194 {
195     struct GNUNET_GETOPT_CommandLineOption options[] = {
196         GNUNET_GETOPT_OPTION_END
197     };
198
199     char *const argv_prog[] = {
200         "test-stun",
201         NULL
202     };
203     GNUNET_log_setup ("test-stun",
204                       "WARNING",
205                       NULL);
206
207     GNUNET_PROGRAM_run (1, argv_prog, "test-stun", "nohelp", options, &run, NULL);
208     
209         return ret;
210 }
211
212 /* FIXME: wrong file name */
213 /* end of test_nat.c */