leakfix
[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_free (addr);
121   GNUNET_NETWORK_socket_destroy (lsock);
122   GNUNET_NETWORK_receive (asock,
123                           1024,
124                           GNUNET_TIME_relative_multiply
125                           (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls);
126 }
127
128 static size_t
129 make_hello (void *cls, size_t size, void *buf)
130 {
131   GNUNET_assert (size >= 12);
132   strcpy ((char *) buf, "Hello World");
133   return 12;
134 }
135
136 static void
137 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {
139   struct sockaddr_in v4;
140   ls = open_listen_socket ();
141   lsock = GNUNET_NETWORK_socket_create_from_existing (tc->sched, ls, 0);
142   GNUNET_assert (lsock != NULL);
143
144   v4.sin_family = AF_INET;
145   v4.sin_port = htons (PORT);
146   v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
147   csock = GNUNET_NETWORK_socket_create_from_sockaddr (tc->sched,
148                                                       AF_INET,
149                                                       (const struct sockaddr
150                                                        *) &v4, sizeof (v4),
151                                                       1024);
152   GNUNET_assert (csock != NULL);
153   GNUNET_assert (NULL !=
154                  GNUNET_NETWORK_notify_transmit_ready (csock,
155                                                        12,
156                                                        GNUNET_TIME_UNIT_SECONDS,
157                                                        &make_hello, NULL));
158   GNUNET_NETWORK_socket_destroy (csock);
159   GNUNET_SCHEDULER_add_read (tc->sched,
160                              GNUNET_NO,
161                              GNUNET_SCHEDULER_PRIORITY_HIGH,
162                              GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
163                              GNUNET_TIME_UNIT_FOREVER_REL,
164                              ls, &run_accept, cls);
165 }
166
167
168 /**
169  * Main method, starts scheduler with task ,
170  * checks that "ok" is correct at the end.
171  */
172 static int
173 check ()
174 {
175   int ok;
176
177   ok = 1;
178   GNUNET_SCHEDULER_run (&task, &ok);
179   return ok;
180 }
181
182
183
184 int
185 main (int argc, char *argv[])
186 {
187   int ret = 0;
188
189   GNUNET_log_setup ("test_network_addressing", "WARNING", NULL);
190   ret += check ();
191   return ret;
192 }
193
194 /* end of test_network_addressing.c */