ng
[oweals/gnunet.git] / src / util / test_network_addressing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/test_network_addressing.c
22  * @brief tests for network.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_network_lib.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_time_lib.h"
29
30 #define VERBOSE GNUNET_NO
31
32 #define PORT 12435
33
34
35 static struct GNUNET_NETWORK_SocketHandle *csock;
36
37 static struct GNUNET_NETWORK_SocketHandle *asock;
38
39 static struct GNUNET_NETWORK_SocketHandle *lsock;
40
41 static size_t sofar;
42
43 static int ls;
44
45
46
47 /**
48  * Create and initialize a listen socket for the server.
49  *
50  * @return -1 on error, otherwise the listen socket
51  */
52 static int
53 open_listen_socket ()
54 {
55   const static int on = 1;
56   struct sockaddr_in sa;
57   int fd;
58
59   memset (&sa, 0, sizeof (sa));
60   sa.sin_port = htons (PORT);
61   fd = SOCKET (AF_INET, SOCK_STREAM, 0);
62   GNUNET_assert (fd >= 0);
63   if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0)
64     GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
65                 "setsockopt");
66   GNUNET_assert (BIND (fd, &sa, sizeof (sa)) >= 0);
67   LISTEN (fd, 5);
68   return fd;
69 }
70
71
72 static void
73 receive_check (void *cls,
74                const void *buf,
75                size_t available,
76                const struct sockaddr *addr, socklen_t addrlen, int errCode)
77 {
78   int *ok = cls;
79
80   GNUNET_assert (buf != NULL);  /* no timeout */
81   if (0 == memcmp (&"Hello World"[sofar], buf, available))
82     sofar += available;
83   if (sofar < 12)
84     {
85       GNUNET_NETWORK_receive (asock,
86                               1024,
87                               GNUNET_TIME_relative_multiply
88                               (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
89                               cls);
90     }
91   else
92     {
93       *ok = 0;
94       GNUNET_NETWORK_socket_destroy (asock);
95     }
96 }
97
98
99 static void
100 run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   void *addr;
103   size_t alen;
104   struct sockaddr_in *v4;
105   struct sockaddr_in expect;
106
107   asock = GNUNET_NETWORK_socket_create_from_accept (tc->sched,
108                                                     NULL, NULL, ls, 1024);
109   GNUNET_assert (asock != NULL);
110   GNUNET_assert (GNUNET_YES == GNUNET_NETWORK_socket_check (asock));
111   GNUNET_assert (GNUNET_OK ==
112                  GNUNET_NETWORK_socket_get_address (asock, &addr, &alen));
113   GNUNET_assert (alen == sizeof (struct sockaddr_in));
114   v4 = addr;
115   memset (&expect, 0, sizeof (expect));
116   expect.sin_family = AF_INET;
117   expect.sin_port = v4->sin_port;
118   expect.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
119   GNUNET_assert (0 == memcmp (&expect, v4, alen));
120   GNUNET_NETWORK_socket_destroy (lsock);
121   GNUNET_NETWORK_receive (asock,
122                           1024,
123                           GNUNET_TIME_relative_multiply
124                           (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls);
125 }
126
127 static size_t
128 make_hello (void *cls, size_t size, void *buf)
129 {
130   GNUNET_assert (size >= 12);
131   strcpy ((char *) buf, "Hello World");
132   return 12;
133 }
134
135 static void
136 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   struct sockaddr_in v4;
139   ls = open_listen_socket ();
140   lsock = GNUNET_NETWORK_socket_create_from_existing (tc->sched, ls, 0);
141   GNUNET_assert (lsock != NULL);
142
143   v4.sin_family = AF_INET;
144   v4.sin_port = htons (PORT);
145   v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
146   csock = GNUNET_NETWORK_socket_create_from_sockaddr (tc->sched,
147                                                       AF_INET,
148                                                       (const struct sockaddr
149                                                        *) &v4, sizeof (v4),
150                                                       1024);
151   GNUNET_assert (csock != NULL);
152   GNUNET_assert (NULL !=
153                  GNUNET_NETWORK_notify_transmit_ready (csock,
154                                                        12,
155                                                        GNUNET_TIME_UNIT_SECONDS,
156                                                        &make_hello, NULL));
157   GNUNET_NETWORK_socket_destroy (csock);
158   GNUNET_SCHEDULER_add_read (tc->sched,
159                              GNUNET_NO,
160                              GNUNET_SCHEDULER_PRIORITY_HIGH,
161                              GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
162                              GNUNET_TIME_UNIT_FOREVER_REL,
163                              ls, &run_accept, cls);
164 }
165
166
167 /**
168  * Main method, starts scheduler with task ,
169  * checks that "ok" is correct at the end.
170  */
171 static int
172 check ()
173 {
174   int ok;
175
176   ok = 1;
177   GNUNET_SCHEDULER_run (&task, &ok);
178   return ok;
179 }
180
181
182
183 int
184 main (int argc, char *argv[])
185 {
186   int ret = 0;
187
188   GNUNET_log_setup ("test_network_addressing", "WARNING", NULL);
189   ret += check ();
190   return ret;
191 }
192
193 /* end of test_network_addressing.c */