aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-helper-transport-wlan-dummy.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-04-03 12:58:37 +0000
committerChristian Grothoff <christian@grothoff.org>2012-04-03 12:58:37 +0000
commitffd4fa2b692f7658a018be1fa49c1fad0cfab465 (patch)
tree8910b0357ac02ed9066c7ae7d13a01e7f81d6b6a /src/transport/gnunet-helper-transport-wlan-dummy.c
parent46960874cec563042540b13a60a9aa1d5e284e87 (diff)
downloadgnunet-ffd4fa2b692f7658a018be1fa49c1fad0cfab465.tar.gz
gnunet-ffd4fa2b692f7658a018be1fa49c1fad0cfab465.zip
-fix assertion, documentation, getting dummy to be closer to work on W32
Diffstat (limited to 'src/transport/gnunet-helper-transport-wlan-dummy.c')
-rw-r--r--src/transport/gnunet-helper-transport-wlan-dummy.c195
1 files changed, 123 insertions, 72 deletions
diff --git a/src/transport/gnunet-helper-transport-wlan-dummy.c b/src/transport/gnunet-helper-transport-wlan-dummy.c
index 476b403a5..dddd51cb7 100644
--- a/src/transport/gnunet-helper-transport-wlan-dummy.c
+++ b/src/transport/gnunet-helper-transport-wlan-dummy.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors) 3 (C) 2010, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -27,22 +27,60 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "plugin_transport_wlan.h" 28#include "plugin_transport_wlan.h"
29 29
30#define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in" 30/**
31#define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out" 31 * Name of the fifo to use for IPC with the other dummy process.
32 */
33#define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in"
34
35/**
36 * Name of the fifo to use for IPC with the other dummy process.
37 */
38#define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out"
32 39
40/**
41 * Maximum size of a message allowed in either direction
42 * (used for our receive and sent buffers).
43 */
33#define MAXLINE 4096 44#define MAXLINE 4096
34 45
35struct sendbuf 46
47/**
48 * IO buffer used for buffering data in transit.
49 */
50struct SendBuffer
36{ 51{
37 unsigned int pos; 52
38 unsigned int size; 53 /**
54 * How many bytes that were stored in 'buf' did we already write to the
55 * destination? Always smaller than 'size'.
56 */
57 size_t pos;
58
59 /**
60 * How many bytes of data are stored in 'buf' for transmission right now?
61 * Data always starts at offset 0 and extends to 'size'.
62 */
63 size_t size;
64
65 /**
66 * Buffered data; twice the maximum allowed message size as we add some
67 * headers.
68 */
39 char buf[MAXLINE * 2]; 69 char buf[MAXLINE * 2];
40}; 70};
41 71
42static int first;
43 72
73/**
74 * Flag set to 1 if we are to terminate, otherwise 0.
75 */
44static int closeprog; 76static int closeprog;
45 77
78
79/**
80 * We're being killed, clean up.
81 *
82 * @param sig killing signal
83 */
46static void 84static void
47sigfunc (int sig) 85sigfunc (int sig)
48{ 86{
@@ -53,7 +91,8 @@ sigfunc (int sig)
53 91
54 92
55/** 93/**
56 * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin 94 * Create control message for plugin
95 *
57 * @param buffer pointer to buffer for the message 96 * @param buffer pointer to buffer for the message
58 * @param mac pointer to the mac address 97 * @param mac pointer to the mac address
59 * @return number of bytes written 98 * @return number of bytes written
@@ -67,16 +106,23 @@ send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
67 memcpy (&macmsg.mac, (char *) mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)); 106 memcpy (&macmsg.mac, (char *) mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
68 macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage)); 107 macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
69 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); 108 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
70
71 memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage)); 109 memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
72 return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage); 110 return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
73} 111}
74 112
75 113
114/**
115 * We got a message from the FIFO, check it, convert the message
116 * type to the output forward and copy it to the buffer for stdout.
117 *
118 * @param cls the 'struct SendBuffer' to copy the converted message to
119 * @param client unused
120 * @param hdr inbound message from the FIFO
121 */
76static void 122static void
77stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) 123stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
78{ 124{
79 struct sendbuf *write_pout = cls; 125 struct SendBuffer *write_pout = cls;
80 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in; 126 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
81 size_t payload_size; 127 size_t payload_size;
82 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader; 128 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
@@ -87,15 +133,13 @@ stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
87 if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) || 133 if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ||
88 (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) > sendsize) ) 134 (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) > sendsize) )
89 { 135 {
90 fprintf (stderr, "Received malformed message\n", 136 fprintf (stderr, "Received malformed message\n");
91 (unsigned int) sendsize,
92 (unsigned int) ntohs (hdr->type));
93 exit (1); 137 exit (1);
94 } 138 }
95 payload_size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage); 139 payload_size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
96 if ((payload_size + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) + write_pout->size) > MAXLINE * 2) 140 if ((payload_size + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) + write_pout->size) > MAXLINE * 2)
97 { 141 {
98 fprintf (stderr, "Packet too big for buffer\n"); 142 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
99 exit (1); 143 exit (1);
100 } 144 }
101 memset (&newheader, 0, sizeof (newheader)); 145 memset (&newheader, 0, sizeof (newheader));
@@ -113,24 +157,37 @@ stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
113} 157}
114 158
115 159
160/**
161 * We read a full message from stdin. Copy it to our send buffer.
162 *
163 * @param cls the 'struct SendBuffer' to copy to
164 * @param client unused
165 * @param hdr the message we received to copy to the buffer
166 */
116static void 167static void
117file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) 168file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
118{ 169{
119 struct sendbuf *write_std = cls; 170 struct SendBuffer *write_std = cls;
120 uint16_t sendsize; 171 uint16_t sendsize;
121 172
122 sendsize = ntohs (hdr->size); 173 sendsize = ntohs (hdr->size);
123 if ((sendsize + write_std->size) > MAXLINE * 2) 174 if ((sendsize + write_std->size) > MAXLINE * 2)
124 { 175 {
125 fprintf (stderr, "Function file_in_send: Packet too big for buffer\n"); 176 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
126 exit (1); 177 exit (1);
127 } 178 }
128
129 memcpy (write_std->buf + write_std->size, hdr, sendsize); 179 memcpy (write_std->buf + write_std->size, hdr, sendsize);
130 write_std->size += sendsize; 180 write_std->size += sendsize;
131} 181}
132 182
133 183
184/**
185 * Main function of a program that pretends to be a WLAN card.
186 *
187 * @param argc should be 2
188 * @param argv either '1' or '2', depending on which of the two cards this dummy is to emulate
189 * @return 1 on error, 0 if terminated normally via signal
190 */
134int 191int
135main (int argc, char *argv[]) 192main (int argc, char *argv[])
136{ 193{
@@ -141,105 +198,98 @@ main (int argc, char *argv[])
141 int fdpin; 198 int fdpin;
142 int fdpout; 199 int fdpout;
143 char readbuf[MAXLINE]; 200 char readbuf[MAXLINE];
144 int readsize = 0; 201 int readsize;
145 struct sendbuf write_std; 202 struct SendBuffer write_std;
146 struct sendbuf write_pout; 203 struct SendBuffer write_pout;
147 int ret = 0; 204 int ret;
148 int maxfd = 0; 205 int maxfd;
149 fd_set rfds; 206 fd_set rfds;
150 fd_set wfds; 207 fd_set wfds;
151 struct timeval tv; 208 struct timeval tv;
152 int retval; 209 int retval;
153 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst; 210 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst = NULL;
154 struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst; 211 struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst = NULL;
155 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr; 212 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
213 int first;
156 214
157 if (2 != argc) 215 if ( (2 != argc) ||
216 ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
158 { 217 {
159 fprintf (stderr, 218 FPRINTF (stderr,
160 "This program must be started with the operating mode (1 or 2) as the only argument.\n"); 219 "%s",
220 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
161 return 1; 221 return 1;
162 } 222 }
163 if ((0 != strstr (argv[1], "1")) && (0 != strstr (argv[1], "2")))
164 return 1;
165 223
166 //make the fifos if needed 224 /* make the fifos if needed */
167 if (0 != stat (FIFO_FILE1, &st)) 225 if (0 != stat (FIFO_FILE1, &st))
168 { 226 {
169 if (0 == stat (FIFO_FILE2, &st)) 227 if (0 == stat (FIFO_FILE2, &st))
170 { 228 {
171 fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n"); 229 FPRINTF (stderr, "%s", "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
172 exit (1); 230 exit (1);
173 } 231 }
174 umask (0); 232 umask (0);
175 erg = mkfifo (FIFO_FILE1, 0666); 233 erg = mkfifo (FIFO_FILE1, 0666);
176 if (0 != erg) 234 if (0 != erg)
177 { 235 {
178 fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1, 236 FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
179 strerror (errno)); 237 strerror (errno));
180 //exit(1);
181 } 238 }
182 erg = mkfifo (FIFO_FILE2, 0666); 239 erg = mkfifo (FIFO_FILE2, 0666);
183 if (0 != erg) 240 if (0 != erg)
184 { 241 {
185 fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2, 242 FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
186 strerror (errno)); 243 strerror (errno));
187 //exit(1);
188 } 244 }
189
190 } 245 }
191 else 246 else
192 { 247 {
193 if (0 != stat (FIFO_FILE2, &st)) 248 if (0 != stat (FIFO_FILE2, &st))
194 { 249 {
195 fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n"); 250 FPRINTF (stderr, "%s", "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
196 exit (1); 251 exit (1);
197 } 252 }
198 } 253 }
199 254
200 if (strstr (argv[1], "1")) 255 if (0 != strcmp (argv[1], "1"))
201 { 256 {
202 //fprintf(stderr, "First\n");
203 first = 1; 257 first = 1;
204 fpin = fopen (FIFO_FILE1, "r"); 258 fpin = fopen (FIFO_FILE1, "r");
205 if (NULL == fpin) 259 if (NULL == fpin)
206 { 260 {
207 fprintf (stderr, "fopen of read FIFO_FILE1\n"); 261 FPRINTF (stderr, "fopen of read FIFO_FILE1 failed: %s\n", STRERROR (errno));
208 goto end; 262 goto end;
209 } 263 }
210 fpout = fopen (FIFO_FILE2, "w"); 264 fpout = fopen (FIFO_FILE2, "w");
211 if (NULL == fpout) 265 if (NULL == fpout)
212 { 266 {
213 fprintf (stderr, "fopen of write FIFO_FILE2\n"); 267 FPRINTF (stderr, "fopen of write FIFO_FILE2 failed: %s\n", STRERROR (errno));
214 goto end; 268 goto end;
215 } 269 }
216
217 } 270 }
218 else 271 else
219 { 272 {
220 first = 0; 273 first = 0;
221 //fprintf(stderr, "Second\n");
222 fpout = fopen (FIFO_FILE1, "w"); 274 fpout = fopen (FIFO_FILE1, "w");
223 if (NULL == fpout) 275 if (NULL == fpout)
224 { 276 {
225 fprintf (stderr, "fopen of write FIFO_FILE1\n"); 277 FPRINTF (stderr, "fopen of write FIFO_FILE1 failed: %s\n", STRERROR (errno));
226 goto end; 278 goto end;
227 } 279 }
228 fpin = fopen (FIFO_FILE2, "r"); 280 fpin = fopen (FIFO_FILE2, "r");
229 if (NULL == fpin) 281 if (NULL == fpin)
230 { 282 {
231 fprintf (stderr, "fopen of read FIFO_FILE2\n"); 283 FPRINTF (stderr, "fopen of read FIFO_FILE2 failed: %s\n", STRERROR (errno));
232 goto end; 284 goto end;
233 } 285 }
234
235 } 286 }
236 287
237 fdpin = fileno (fpin); 288 fdpin = fileno (fpin);
238 GNUNET_assert (fpin >= 0); 289 GNUNET_assert (fpin >= 0);
239
240 if (fdpin >= FD_SETSIZE) 290 if (fdpin >= FD_SETSIZE)
241 { 291 {
242 fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin, 292 FPRINTF (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
243 (unsigned int) FD_SETSIZE); 293 (unsigned int) FD_SETSIZE);
244 goto end; 294 goto end;
245 } 295 }
@@ -249,10 +299,9 @@ main (int argc, char *argv[])
249 299
250 if (fdpout >= FD_SETSIZE) 300 if (fdpout >= FD_SETSIZE)
251 { 301 {
252 fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout, 302 FPRINTF (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
253 (unsigned int) FD_SETSIZE); 303 (unsigned int) FD_SETSIZE);
254 goto end; 304 goto end;
255
256 } 305 }
257 306
258 signal (SIGINT, &sigfunc); 307 signal (SIGINT, &sigfunc);
@@ -265,7 +314,7 @@ main (int argc, char *argv[])
265 stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout); 314 stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
266 file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std); 315 file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
267 316
268 //Send random mac address 317 /* Send 'random' mac address */
269 macaddr.mac[0] = 0x13; 318 macaddr.mac[0] = 0x13;
270 macaddr.mac[1] = 0x22; 319 macaddr.mac[1] = 0x22;
271 macaddr.mac[2] = 0x33; 320 macaddr.mac[2] = 0x33;
@@ -277,12 +326,12 @@ main (int argc, char *argv[])
277 while (0 == closeprog) 326 while (0 == closeprog)
278 { 327 {
279 maxfd = -1; 328 maxfd = -1;
280 //set timeout
281 tv.tv_sec = 5; 329 tv.tv_sec = 5;
282 tv.tv_usec = 0; 330 tv.tv_usec = 0;
283 331
284 FD_ZERO (&rfds); 332 FD_ZERO (&rfds);
285 // if output queue is empty 333 FD_ZERO (&wfds);
334 /* if output queue is empty, read */
286 if (0 == write_pout.size) 335 if (0 == write_pout.size)
287 { 336 {
288 FD_SET (STDIN_FILENO, &rfds); 337 FD_SET (STDIN_FILENO, &rfds);
@@ -293,8 +342,8 @@ main (int argc, char *argv[])
293 FD_SET (fdpin, &rfds); 342 FD_SET (fdpin, &rfds);
294 maxfd = MAX (fdpin, maxfd); 343 maxfd = MAX (fdpin, maxfd);
295 } 344 }
296 FD_ZERO (&wfds); 345
297 // if there is something to write 346 /* if there is something to write, try to write */
298 if (0 < write_std.size) 347 if (0 < write_std.size)
299 { 348 {
300 FD_SET (STDOUT_FILENO, &wfds); 349 FD_SET (STDOUT_FILENO, &wfds);
@@ -311,7 +360,7 @@ main (int argc, char *argv[])
311 continue; 360 continue;
312 if (0 > retval) 361 if (0 > retval)
313 { 362 {
314 fprintf (stderr, "select failed: %s\n", strerror (errno)); 363 FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
315 closeprog = 1; 364 closeprog = 1;
316 break; 365 break;
317 } 366 }
@@ -324,14 +373,14 @@ main (int argc, char *argv[])
324 if (0 > ret) 373 if (0 > ret)
325 { 374 {
326 closeprog = 1; 375 closeprog = 1;
327 fprintf (stderr, "Write ERROR to STDOUT_FILENO: %s\n", 376 FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
328 strerror (errno)); 377 STRERROR (errno));
329 break; 378 break;
330 } 379 }
331 else 380 else
332 { 381 {
333 write_std.pos += ret; 382 write_std.pos += ret;
334 // check if finished 383 /* check if finished writing */
335 if (write_std.pos == write_std.size) 384 if (write_std.pos == write_std.size)
336 { 385 {
337 write_std.pos = 0; 386 write_std.pos = 0;
@@ -349,12 +398,12 @@ main (int argc, char *argv[])
349 if (0 > ret) 398 if (0 > ret)
350 { 399 {
351 closeprog = 1; 400 closeprog = 1;
352 fprintf (stderr, "Write ERROR to fdpout: %s\n", strerror (errno)); 401 FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
353 } 402 }
354 else 403 else
355 { 404 {
356 write_pout.pos += ret; 405 write_pout.pos += ret;
357 // check if finished 406 /* check if finished writing */
358 if (write_pout.pos == write_pout.size) 407 if (write_pout.pos == write_pout.size)
359 { 408 {
360 write_pout.pos = 0; 409 write_pout.pos = 0;
@@ -370,8 +419,8 @@ main (int argc, char *argv[])
370 if (0 > readsize) 419 if (0 > readsize)
371 { 420 {
372 closeprog = 1; 421 closeprog = 1;
373 fprintf (stderr, "Error reading from STDIN_FILENO: %s\n", 422 FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
374 strerror (errno)); 423 STRERROR (errno));
375 } 424 }
376 else if (0 < readsize) 425 else if (0 < readsize)
377 { 426 {
@@ -381,7 +430,7 @@ main (int argc, char *argv[])
381 } 430 }
382 else 431 else
383 { 432 {
384 //eof 433 /* eof */
385 closeprog = 1; 434 closeprog = 1;
386 } 435 }
387 } 436 }
@@ -392,7 +441,7 @@ main (int argc, char *argv[])
392 if (0 > readsize) 441 if (0 > readsize)
393 { 442 {
394 closeprog = 1; 443 closeprog = 1;
395 fprintf (stderr, "Error reading from fdpin: %s\n", strerror (errno)); 444 FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
396 break; 445 break;
397 } 446 }
398 else if (0 < readsize) 447 else if (0 < readsize)
@@ -402,20 +451,22 @@ main (int argc, char *argv[])
402 } 451 }
403 else 452 else
404 { 453 {
405 //eof 454 /* eof */
406 closeprog = 1; 455 closeprog = 1;
407 } 456 }
408 } 457 }
409 } 458 }
410 459
411 //clean up
412 GNUNET_SERVER_mst_destroy (stdin_mst);
413 GNUNET_SERVER_mst_destroy (file_in_mst);
414
415end: 460end:
416 if (fpout != NULL) 461 /* clean up */
462 if (NULL != stdin_mst)
463 GNUNET_SERVER_mst_destroy (stdin_mst);
464 if (NULL != file_in_mst)
465 GNUNET_SERVER_mst_destroy (file_in_mst);
466
467 if (NULL != fpout)
417 fclose (fpout); 468 fclose (fpout);
418 if (fpin != NULL) 469 if (NULL != fpin)
419 fclose (fpin); 470 fclose (fpin);
420 if (1 == first) 471 if (1 == first)
421 { 472 {