aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_connection_addressing.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-08-29 19:37:54 +0000
committerChristian Grothoff <christian@grothoff.org>2009-08-29 19:37:54 +0000
commitdb5fce6cd527aa49fb5762d730564a918131a30f (patch)
tree78213929b0be999a8cee9b9bc4576c41dd0fe35c /src/util/test_connection_addressing.c
parentdf2e643d2ce35a310e58801a8841677fa50dcb15 (diff)
downloadgnunet-db5fce6cd527aa49fb5762d730564a918131a30f.tar.gz
gnunet-db5fce6cd527aa49fb5762d730564a918131a30f.zip
renames
Diffstat (limited to 'src/util/test_connection_addressing.c')
-rw-r--r--src/util/test_connection_addressing.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/src/util/test_connection_addressing.c b/src/util/test_connection_addressing.c
new file mode 100644
index 000000000..650b716af
--- /dev/null
+++ b/src/util/test_connection_addressing.c
@@ -0,0 +1,200 @@
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_connection_addressing.c
22 * @brief tests for connection.c
23 */
24#include "platform.h"
25#include "gnunet_common.h"
26#include "gnunet_connection_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
35static struct GNUNET_CONNECTION_Handle *csock;
36
37static struct GNUNET_CONNECTION_Handle *asock;
38
39static struct GNUNET_CONNECTION_Handle *lsock;
40
41static size_t sofar;
42
43static struct GNUNET_NETWORK_Descriptor *ls;
44
45
46
47/**
48 * Create and initialize a listen socket for the server.
49 *
50 * @return NULL on error, otherwise the listen socket
51 */
52static struct GNUNET_NETWORK_Descriptor *
53open_listen_socket ()
54{
55 const static int on = 1;
56 struct sockaddr_in sa;
57 struct GNUNET_NETWORK_Descriptor *desc;
58
59 memset (&sa, 0, sizeof (sa));
60 sa.sin_port = htons (PORT);
61 desc = GNUNET_NETWORK_socket_socket (AF_INET, SOCK_STREAM, 0);
62 GNUNET_assert (desc != 0);
63 if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0)
64 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
65 "setsockopt");
66 GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, &sa, sizeof (sa)) >= 0);
67 GNUNET_NETWORK_socket_listen (desc, 5);
68 return desc;
69}
70
71
72static void
73receive_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_CONNECTION_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_CONNECTION_destroy (asock);
95 }
96}
97
98
99static void
100run_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_CONNECTION_create_from_accept (tc->sched,
108 NULL, NULL, ls, 1024);
109 GNUNET_assert (asock != NULL);
110 GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
111 GNUNET_assert (GNUNET_OK ==
112 GNUNET_CONNECTION_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_CONNECTION_destroy (lsock);
122 GNUNET_CONNECTION_receive (asock,
123 1024,
124 GNUNET_TIME_relative_multiply
125 (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls);
126}
127
128static size_t
129make_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
136static void
137task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138{
139 struct sockaddr_in v4;
140 ls = open_listen_socket ();
141 lsock = GNUNET_CONNECTION_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_CONNECTION_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_CONNECTION_notify_transmit_ready (csock,
155 12,
156 GNUNET_TIME_UNIT_SECONDS,
157 &make_hello, NULL));
158 GNUNET_CONNECTION_destroy (csock);
159 GNUNET_SCHEDULER_add_read_net (tc->sched,
160 GNUNET_NO,
161 GNUNET_SCHEDULER_PRIORITY_HIGH,
162 GNUNET_SCHEDULER_NO_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 */
172static int
173check ()
174{
175 int ok;
176
177 ok = 1;
178 GNUNET_SCHEDULER_run (&task, &ok);
179 return ok;
180}
181
182
183
184int
185main (int argc, char *argv[])
186{
187 int ret = 0;
188
189 GNUNET_log_setup ("test_connection_addressing",
190#if VERBOSE
191 "DEBUG",
192#else
193 "WARNING",
194#endif
195 NULL);
196 ret += check ();
197 return ret;
198}
199
200/* end of test_connection_addressing.c */