solicit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * solicit.c - Qualcomm Atheros Network Solicitor;
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <limits.h>
  23. #include <errno.h>
  24. #include <sys/time.h>
  25. /*====================================================================*
  26. * custom header files;
  27. *--------------------------------------------------------------------*/
  28. #include "../ether/channel.h"
  29. #include "../tools/getoptv.h"
  30. #include "../tools/putoptv.h"
  31. #include "../tools/memory.h"
  32. #include "../tools/number.h"
  33. #include "../tools/symbol.h"
  34. #include "../tools/types.h"
  35. #include "../tools/files.h"
  36. #include "../tools/flags.h"
  37. #include "../tools/error.h"
  38. #include "../tools/timer.h"
  39. #include "../lldp/lldp.h"
  40. #include "../mme/mme.h"
  41. /*====================================================================*
  42. * custom source files;
  43. *--------------------------------------------------------------------*/
  44. #ifndef MAKEFILE
  45. #include "../tools/getoptv.c"
  46. #include "../tools/putoptv.c"
  47. #include "../tools/version.c"
  48. #include "../tools/uintspec.c"
  49. #include "../tools/hexdecode.c"
  50. #include "../tools/hexencode.c"
  51. #include "../tools/hexdump.c"
  52. #include "../tools/hexstring.c"
  53. #include "../tools/decdecode.c"
  54. #include "../tools/decstring.c"
  55. #include "../tools/todigit.c"
  56. #include "../tools/strfbits.c"
  57. #include "../tools/synonym.c"
  58. #include "../tools/memswap.c"
  59. #include "../tools/error.c"
  60. #endif
  61. #ifndef MAKEFILE
  62. #include "../ether/openchannel.c"
  63. #include "../ether/closechannel.c"
  64. #include "../ether/readpacket.c"
  65. #include "../ether/sendpacket.c"
  66. #include "../ether/channel.c"
  67. #endif
  68. #ifndef MAKEFILE
  69. #include "../mme/EthernetHeader.c"
  70. #endif
  71. #ifndef MAKEFILE
  72. #include "../lldp/TLVPack.c"
  73. #include "../lldp/TLVPackOS.c"
  74. #include "../lldp/TLVPick.c"
  75. #include "../lldp/TLVPeek.c"
  76. #include "../lldp/lldp.c"
  77. #endif
  78. /*====================================================================*
  79. * program constants;
  80. *--------------------------------------------------------------------*/
  81. #define SOLICIT_VERBOSE (1 << 0)
  82. #define SOLICIT_SILENCE (1 << 1)
  83. #define PLCDEVICE "PLC"
  84. #define PROFILE "solicit.ini"
  85. #define SECTION "default"
  86. /*====================================================================*
  87. *
  88. * void solicit (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section);
  89. *
  90. *--------------------------------------------------------------------*/
  91. static void solicit (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section)
  92. {
  93. memset (frame, 0, sizeof (* frame));
  94. EthernetHeader (frame, channel->peer, channel->host, ETH_P_LLDP);
  95. if (sendpacket (channel, frame, (ETHER_MIN_LEN - ETHER_CRC_LEN)) > 0)
  96. {
  97. while (readpacket (channel, frame, sizeof (* frame)) > 0)
  98. {
  99. if (! memcmp (frame->frame_dhost, channel->host, sizeof (frame->frame_dhost)))
  100. {
  101. TLVPeek (frame->frame_data, sizeof (frame->frame_data));
  102. }
  103. }
  104. }
  105. return;
  106. }
  107. /*====================================================================*
  108. *
  109. * void monitor (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section);
  110. *
  111. *--------------------------------------------------------------------*/
  112. static void monitor (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section)
  113. {
  114. struct timeval ts;
  115. struct timeval tc;
  116. ssize_t length;
  117. if (gettimeofday (& ts, NULL) == - 1)
  118. {
  119. error (1, errno, CANT_START_TIMER);
  120. }
  121. while ((length = readpacket (channel, frame, sizeof (* frame))) >= 0)
  122. {
  123. if (! memcmp (frame->frame_dhost, channel->peer, sizeof (frame->frame_dhost)))
  124. {
  125. TLVPeek (frame->frame_data, sizeof (frame->frame_data));
  126. EthernetHeader (frame, frame->frame_shost, channel->host, ETH_P_LLDP);
  127. memset (frame->frame_data, 0, sizeof (frame->frame_data));
  128. if (sendpacket (channel, frame, (ETHER_MIN_LEN - ETHER_CRC_LEN)) <= 0)
  129. {
  130. error (0, errno, CHANNEL_CANTSEND);
  131. }
  132. }
  133. if (gettimeofday (& tc, NULL) == - 1)
  134. {
  135. error (1, errno, CANT_RESET_TIMER);
  136. }
  137. if (channel->timeout < 0)
  138. {
  139. continue;
  140. }
  141. if (channel->timeout > MILLISECONDS (ts, tc))
  142. {
  143. continue;
  144. }
  145. break;
  146. }
  147. return;
  148. }
  149. /*====================================================================*
  150. *
  151. * int main (int argc, char const * argv[]);
  152. *
  153. *
  154. *--------------------------------------------------------------------*/
  155. int main (int argc, char const * argv [])
  156. {
  157. extern struct channel channel;
  158. extern struct _term_ const addresses [LLDP_ADDRESSES];
  159. extern byte const broadcast [ETHER_ADDR_LEN];
  160. static char const * optv [] =
  161. {
  162. "b:i:p:qs:t:vw:",
  163. "",
  164. "Qualcomm Atheros Network Solicitor",
  165. "b x\tbroadcast address is (x) [" LITERAL (BROADCAST) "]",
  166. #if defined (WINPCAP) || defined (LIBPCAP)
  167. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  168. #else
  169. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  170. #endif
  171. "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
  172. "q\tquiet mode",
  173. "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
  174. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  175. "v\tverbose mode",
  176. "w n\twakeup every (n) milliseconds [" LITERAL (LLDP_TIMER) "]",
  177. (char const *) (0)
  178. };
  179. struct ethernet_frame frame;
  180. char const * profile = PROFILE;
  181. char const * section = SECTION;
  182. signed c;
  183. memcpy (channel.peer, broadcast, sizeof (channel.peer));
  184. channel.type = ETH_P_LLDP;
  185. channel.timeout = LLDP_TIMER;
  186. if (getenv (PLCDEVICE))
  187. {
  188. #if defined (WINPCAP) || defined (LIBPCAP)
  189. channel.ifindex = atoi (getenv (PLCDEVICE));
  190. #else
  191. channel.ifname = strdup (getenv (PLCDEVICE));
  192. #endif
  193. }
  194. optind = 1;
  195. while (~ (c = getoptv (argc, argv, optv)))
  196. {
  197. switch (c)
  198. {
  199. case 'b':
  200. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (optarg, addresses, SIZEOF (addresses))))
  201. {
  202. error (1, errno, LLDP_BAD_MAC, optarg);
  203. }
  204. break;
  205. case 'i':
  206. #if defined (WINPCAP) || defined (LIBPCAP)
  207. channel.ifindex = atoi (optarg);
  208. #else
  209. channel.ifname = optarg;
  210. #endif
  211. break;
  212. case 'p':
  213. profile = optarg;
  214. break;
  215. case 'q':
  216. _setbits (channel.flags, CHANNEL_SILENCE);
  217. break;
  218. case 's':
  219. section = optarg;
  220. break;
  221. case 't':
  222. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  223. break;
  224. case 'v':
  225. _setbits (channel.flags, CHANNEL_VERBOSE);
  226. break;
  227. case 'w':
  228. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. argc -= optind;
  235. argv += optind;
  236. if (argc)
  237. {
  238. error (1, 0, ERROR_TOOMANY);
  239. }
  240. if (access (profile, R_OK))
  241. {
  242. error (1, errno, FILE_CANTSTAT, profile);
  243. }
  244. openchannel (& channel);
  245. while (true)
  246. {
  247. solicit (& channel, & frame, profile, section);
  248. monitor (& channel, & frame, profile, section);
  249. }
  250. closechannel (& channel);
  251. exit (0);
  252. }