gnunet-transport-wlan-sender.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2011 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/gnunet-transport-wlan-sender.c
  18. * @brief program to send via WLAN as much as possible (to test physical/theoretical throughput)
  19. * @author David Brodski
  20. */
  21. #include "platform.h"
  22. #include "plugin_transport_wlan.h"
  23. #include "gnunet_protocols.h"
  24. #define WLAN_MTU 1500
  25. /**
  26. * LLC fields for better compatibility
  27. */
  28. #define WLAN_LLC_DSAP_FIELD 0x1f
  29. #define WLAN_LLC_SSAP_FIELD 0x1f
  30. #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
  31. #define IEEE80211_FC0_VERSION_MASK 0x03
  32. #define IEEE80211_FC0_VERSION_SHIFT 0
  33. #define IEEE80211_FC0_VERSION_0 0x00
  34. #define IEEE80211_FC0_TYPE_MASK 0x0c
  35. #define IEEE80211_FC0_TYPE_SHIFT 2
  36. #define IEEE80211_FC0_TYPE_MGT 0x00
  37. #define IEEE80211_FC0_TYPE_CTL 0x04
  38. #define IEEE80211_FC0_TYPE_DATA 0x08
  39. /**
  40. * function to fill the radiotap header
  41. * @param header pointer to the radiotap header
  42. * @param size total message size
  43. * @return GNUNET_YES at success
  44. */
  45. static int
  46. getRadiotapHeader (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
  47. uint16_t size)
  48. {
  49. header->header.size = htons (size);
  50. header->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
  51. header->rate = 255;
  52. header->tx_power = 0;
  53. header->antenna = 0;
  54. return GNUNET_YES;
  55. }
  56. /**
  57. * function to generate the wlan hardware header for one packet
  58. * @param Header address to write the header to
  59. * @param to_mac_addr pointer to the address of the recipient
  60. * @param mac pointer to the mac address to send from (normally overwritten over by helper)
  61. * @param size size of the whole packet, needed to calculate the time to send the packet
  62. * @return GNUNET_YES if there was no error
  63. */
  64. static int
  65. getWlanHeader (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *Header,
  66. const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
  67. const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac, unsigned int size)
  68. {
  69. const int rate = 11000000;
  70. Header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
  71. Header->addr3 = mac_bssid_gnunet;
  72. Header->addr2 = *mac;
  73. Header->addr1 = *to_mac_addr;
  74. Header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
  75. Header->llc[0] = WLAN_LLC_DSAP_FIELD;
  76. Header->llc[1] = WLAN_LLC_SSAP_FIELD;
  77. Header->llc[2] = 0; // FIXME
  78. Header->llc[3] = 0; // FIXME
  79. return GNUNET_YES;
  80. }
  81. int
  82. main (int argc, char *argv[])
  83. {
  84. char msg_buf[WLAN_MTU];
  85. struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radiotap;
  86. unsigned int temp[6];
  87. struct GNUNET_TRANSPORT_WLAN_MacAddress inmac;
  88. struct GNUNET_TRANSPORT_WLAN_MacAddress outmac;
  89. struct GNUNET_TRANSPORT_WLAN_HelperControlMessage hcm;
  90. unsigned long long count;
  91. double bytes_per_s;
  92. time_t start;
  93. time_t akt;
  94. int i;
  95. ssize_t ret;
  96. pid_t pid;
  97. int commpipe[2]; /* This holds the fd for the input & output of the pipe */
  98. int macpipe[2]; /* This holds the fd for the input & output of the pipe */
  99. if (4 != argc)
  100. {
  101. fprintf (stderr,
  102. "This program must be started with the interface and the targets and source mac as argument.\n");
  103. fprintf (stderr,
  104. "Usage: interface-name mac-DST mac-SRC\n"
  105. "e.g. mon0 11-22-33-44-55-66 12-34-56-78-90-ab\n");
  106. return 1;
  107. }
  108. if (6 !=
  109. SSCANF (argv[2], "%x-%x-%x-%x-%x-%x", &temp[0], &temp[1], &temp[2],
  110. &temp[3], &temp[4], &temp[5]))
  111. {
  112. fprintf (stderr,
  113. "Usage: interface-name mac-DST mac-SRC\n"
  114. "e.g. mon0 11-22-33-44-55-66 12-34-56-78-90-ab\n");
  115. return 1;
  116. }
  117. for (i = 0; i < 6; i++)
  118. outmac.mac[i] = temp[i];
  119. if (6 !=
  120. SSCANF (argv[3], "%x-%x-%x-%x-%x-%x", &temp[0], &temp[1], &temp[2],
  121. &temp[3], &temp[4], &temp[5]))
  122. {
  123. fprintf (stderr,
  124. "Usage: interface-name mac-DST mac-SRC\n"
  125. "e.g. mon0 11-22-33-44-55-66 12-34-56-78-90-ab\n");
  126. return 1;
  127. }
  128. for (i = 0; i < 6; i++)
  129. inmac.mac[i] = temp[i];
  130. /* Setup communication pipeline first */
  131. if (pipe (commpipe))
  132. {
  133. fprintf (stderr,
  134. "Failed to create pipe: %s\n",
  135. STRERROR (errno));
  136. exit (1);
  137. }
  138. if (pipe (macpipe))
  139. {
  140. fprintf (stderr,
  141. "Failed to create pipe: %s\n",
  142. STRERROR (errno));
  143. exit (1);
  144. }
  145. /* Attempt to fork and check for errors */
  146. if ((pid = fork ()) == -1)
  147. {
  148. fprintf (stderr, "Failed to fork: %s\n",
  149. STRERROR (errno));
  150. exit (1);
  151. }
  152. memset (msg_buf, 0x42, sizeof (msg_buf));
  153. if (pid)
  154. {
  155. /* A positive (non-negative) PID indicates the parent process */
  156. if (0 != close (commpipe[0])) /* Close unused side of pipe (in side) */
  157. fprintf (stderr,
  158. "Failed to close fd: %s\n",
  159. strerror (errno));
  160. setvbuf (stdout, (char *) NULL, _IONBF, 0); /* Set non-buffered output on stdout */
  161. if (0 != close (macpipe[1]))
  162. fprintf (stderr,
  163. "Failed to close fd: %s\n",
  164. strerror (errno));
  165. if (sizeof (hcm) != read (macpipe[0], &hcm, sizeof (hcm)))
  166. fprintf (stderr,
  167. "Failed to read hcm...\n");
  168. fprintf (stderr,
  169. "Got MAC %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
  170. hcm.mac.mac[0], hcm.mac.mac[1],
  171. hcm.mac.mac[2], hcm.mac.mac[3], hcm.mac.mac[4], hcm.mac.mac[5]);
  172. radiotap = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) msg_buf;
  173. getRadiotapHeader (radiotap, WLAN_MTU);
  174. getWlanHeader (&radiotap->frame, &outmac, &inmac,
  175. WLAN_MTU);
  176. start = time (NULL);
  177. count = 0;
  178. while (1)
  179. {
  180. ret = write (commpipe[1], msg_buf, WLAN_MTU);
  181. if (0 > ret)
  182. {
  183. fprintf (stderr, "write failed: %s\n", strerror (errno));
  184. break;
  185. }
  186. count += ret;
  187. akt = time (NULL);
  188. if (akt - start > 30)
  189. {
  190. bytes_per_s = count / (akt - start);
  191. bytes_per_s /= 1024;
  192. printf ("send %f kbytes/s\n", bytes_per_s);
  193. start = akt;
  194. count = 0;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. /* A zero PID indicates that this is the child process */
  201. (void) close (0);
  202. (void) close (1);
  203. if (-1 == dup2 (commpipe[0], 0)) /* Replace stdin with the in side of the pipe */
  204. fprintf (stderr, "dup2 failed: %s\n", strerror (errno));
  205. if (-1 == dup2 (macpipe[1], 1)) /* Replace stdout with the out side of the pipe */
  206. fprintf (stderr, "dup2 failed: %s\n", strerror (errno));
  207. (void) close (commpipe[1]); /* Close unused side of pipe (out side) */
  208. (void) close (macpipe[0]); /* Close unused side of pipe (in side) */
  209. /* Replace the child fork with a new process */
  210. if (execlp
  211. ("gnunet-helper-transport-wlan", "gnunet-helper-transport-wlan",
  212. argv[1], NULL) == -1)
  213. {
  214. fprintf (stderr, "Could not start gnunet-helper-transport-wlan!");
  215. _exit (1);
  216. }
  217. }
  218. return 0;
  219. }