summaryrefslogtreecommitdiff
path: root/src/nat/test_stun.c
blob: e75d55bd0276719ce43fd0b32c825b822900a36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
     This file is part of GNUnet.
     Copyright (C) 2009, 2015 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * Testcase for STUN server resolution
 *
 * @file nat/test_stun.c
 * @brief Testcase for STUN library
 * @author Bruno Souza Cabral

 *
 */


#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_program_lib.h"
#include "gnunet_scheduler_lib.h"
#include "gnunet_nat_lib.h"



#define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)

/**
 * Time to wait before stopping NAT, in seconds
 */
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)


/**
 * The port the test service is running on (default 7895)
 */
static unsigned long port = 7895;
static int ret = 1;

static char *stun_server = "stun2.l.google.com";
static int stun_port = 19302;

/**
 * The listen socket of the service for IPv4
 */
static struct GNUNET_NETWORK_Handle *lsock4;


/**
 * The listen task ID for IPv4
 */
static struct GNUNET_SCHEDULER_Task * ltask4;




static void
print_answer(struct sockaddr_in* answer)
{
	printf("External IP is: %s , with port %d\n", inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
}


/**
 * Activity on our incoming socket.  Read data from the
 * incoming connection.
 *
 * @param cls 
 * @param tc scheduler context
 */
static void
do_udp_read (void *cls,
             const struct GNUNET_SCHEDULER_TaskContext *tc)
{
    //struct GNUNET_NAT_Test *tst = cls;
	unsigned char reply_buf[1024];
	ssize_t rlen;
	struct sockaddr_in answer;

    printf("UDP READ\n");


    if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
      (GNUNET_NETWORK_fdset_isset (tc->read_ready,
                                   lsock4)))
	{
		rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));

		
		//Lets handle the packet
		memset(&answer, 0, sizeof(struct sockaddr_in));
        GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer);

		//Print the answer
		ret = 0;
		print_answer(&answer);

        //Destroy the connection
        GNUNET_NETWORK_socket_close(lsock4);
		
	}


}


/**
 * Create an IPv4 listen socket bound to our port.
 *
 * @return NULL on error
 */
static struct GNUNET_NETWORK_Handle *
bind_v4 ()
{
    struct GNUNET_NETWORK_Handle *ls;
    struct sockaddr_in sa4;
    int eno;

    memset (&sa4, 0, sizeof (sa4));
    sa4.sin_family = AF_INET;
    sa4.sin_port = htons (port);
#if HAVE_SOCKADDR_IN_SIN_LEN
    sa4.sin_len = sizeof (sa4);
#endif 
    ls = GNUNET_NETWORK_socket_create (AF_INET,
                                       SOCK_DGRAM,
                                       0);
    if (NULL == ls)
        return NULL;
    if (GNUNET_OK !=
            GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
                                        sizeof (sa4)))
    {
        eno = errno;
        GNUNET_NETWORK_socket_close (ls);
        errno = eno;
        return NULL;
    }
    return ls;
}

/**
 * Function that terminates the test.
 */
static void
stop ()
{
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");

    printf("Stopped !!\n");
    //Clean task
    if(NULL != ltask4)
        GNUNET_SCHEDULER_cancel (ltask4);

    //Clean socket
    if(NULL != ltask4)
        GNUNET_NETWORK_socket_close (lsock4);

}


static void request_callback(void *cls,
enum GNUNET_NAT_StatusCode result)
{
    ret = result;
    stop();
    printf("Called back\n");
};


/**
 * Main function run with scheduler.
 */
static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{


    //Lets create the socket
    lsock4 = bind_v4 ();
    if (NULL == lsock4)
    {
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
        GNUNET_SCHEDULER_shutdown ();
        return;
    }
    else
    {
        //Lets call our function now when it accepts
        ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                                lsock4, &do_udp_read, NULL);
        /* So you read once and what will happen if you get an irregular message? Repeat and add timeout */

    }

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Service listens on port %u\n",
                port);
	printf("Start main event\n");
    GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4, &request_callback, NULL);
    printf("Made the requeest\n");

    //GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL);

}


int
main (int argc, char *const argv[])
{
    struct GNUNET_GETOPT_CommandLineOption options[] = {
        GNUNET_GETOPT_OPTION_END
    };

    char *const argv_prog[] = {
        "test-stun",
        "-c",
        "test_stun.conf",
        NULL
    };
    GNUNET_log_setup ("test-stun",
                      "WARNING",
                      NULL);

    /* Lets start resolver */
    char *fn;
    struct GNUNET_OS_Process *proc;

    fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
    proc = GNUNET_OS_start_process (GNUNET_YES,
                                    GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
                                    NULL, NULL, NULL,
                                    fn,
                                    "gnunet-service-resolver",
                                    "-c", "test_stun.conf", NULL);
    GNUNET_assert (NULL != proc);

    GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, NULL);

    /* Now kill the resolver */
    if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
    {
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
    }
    GNUNET_OS_process_wait (proc);
    GNUNET_OS_process_destroy (proc);
    proc = NULL;

    
	return ret;
}

/* end of test_stun.c */