solicit.1.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <limits.h>
  22. #include <errno.h>
  23. /*====================================================================*
  24. * custom header files;
  25. *--------------------------------------------------------------------*/
  26. #include "../ether/channel.h"
  27. #include "../tools/getoptv.h"
  28. #include "../tools/putoptv.h"
  29. #include "../tools/memory.h"
  30. #include "../tools/number.h"
  31. #include "../tools/types.h"
  32. #include "../tools/flags.h"
  33. #include "../tools/error.h"
  34. #include "../nda/lldp.h"
  35. #include "../plc/plc.h"
  36. /*====================================================================*
  37. * custom source files;
  38. *--------------------------------------------------------------------*/
  39. #ifndef MAKEFILE
  40. #include "../tools/getoptv.c"
  41. #include "../tools/putoptv.c"
  42. #include "../tools/version.c"
  43. #include "../tools/uintspec.c"
  44. #include "../tools/hexdecode.c"
  45. #include "../tools/hexdump.c"
  46. #include "../tools/hexstring.c"
  47. #include "../tools/decdecode.c"
  48. #include "../tools/decstring.c"
  49. #include "../tools/todigit.c"
  50. #include "../tools/strfbits.c"
  51. #include "../tools/error.c"
  52. #endif
  53. #ifndef MAKEFILE
  54. #include "../ether/openchannel.c"
  55. #include "../ether/closechannel.c"
  56. #include "../ether/readpacket.c"
  57. #include "../ether/sendpacket.c"
  58. #include "../ether/channel.c"
  59. #endif
  60. #ifndef MAKEFILE
  61. #include "../plc/Devices.c"
  62. #endif
  63. #ifndef MAKEFILE
  64. #include "../mme/EthernetHeader.c"
  65. #include "../mme/QualcommHeader.c"
  66. #include "../mme/UnwantedMessage.c"
  67. #include "../mme/readmessage.c"
  68. #include "../mme/sendmessage.c"
  69. #endif
  70. #ifndef MAKEFILE
  71. #include "../lldp/TLVPack.c"
  72. #include "../lldp/TLVPackOS.c"
  73. #include "../lldp/TLVPick.c"
  74. #include "../lldp/TLVPeek.c"
  75. #endif
  76. /*====================================================================*
  77. * program constants;
  78. *--------------------------------------------------------------------*/
  79. #define SOLICIT_VERBOSE (1 << 0)
  80. #define SOLICIT_SILENCE (1 << 1)
  81. #define PLCDEVICE "PLC"
  82. #define PROFILE "solicit.ini"
  83. #define SECTION "default"
  84. /*====================================================================*
  85. *
  86. * void solicit (struct channel * channel, struct message * message, char const * profile, char const * section);
  87. *
  88. *--------------------------------------------------------------------*/
  89. static void solicit (struct channel * channel, struct message * message, char const * profile, char const * section)
  90. {
  91. #ifndef __GNUC__
  92. #pragma pack (push,1)
  93. #endif
  94. struct __packed ms_discover_request
  95. {
  96. struct ethernet_hdr ethernet;
  97. struct qualcomm_hdr qualcomm;
  98. uint8_t MACTION;
  99. }
  100. * request = (struct ms_discover_request *) (message);
  101. struct __packed ms_discover_confirm
  102. {
  103. struct ethernet_hdr ethernet;
  104. struct qualcomm_hdr qualcomm;
  105. uint8_t MDATA [1494];
  106. }
  107. * confirm = (struct ms_discover_confirm *) (message);
  108. #ifndef __GNUC__
  109. #pragma pack (pop)
  110. #endif
  111. memset (message, 0, sizeof (* message));
  112. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  113. QualcommHeader (& request->qualcomm, 0, (MS_DISCOVER | MMTYPE_REQ));
  114. if (sendmessage (channel, message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) > 0)
  115. {
  116. while (readmessage (channel, message, 0, (MS_DISCOVER | MMTYPE_CNF)) > 0)
  117. {
  118. TLVPeek (confirm->MDATA, sizeof (confirm->MDATA));
  119. }
  120. }
  121. return;
  122. }
  123. /*====================================================================*
  124. *
  125. * void monitor (struct channel * channel, struct message * message, char const * profile, char const * section, unsigned timer);
  126. *
  127. *--------------------------------------------------------------------*/
  128. static void monitor (struct channel * channel, struct message * message, char const * profile, char const * section, unsigned timer)
  129. {
  130. #ifndef __GNUC__
  131. #pragma pack (push,1)
  132. #endif
  133. struct __packed ms_discover_indicate
  134. {
  135. struct ethernet_hdr ethernet;
  136. struct qualcomm_hdr qualcomm;
  137. uint8_t MDATA [1494];
  138. }
  139. * indicate = (struct ms_discover_indicate *) (message);
  140. struct __packed ms_discover_response
  141. {
  142. struct ethernet_hdr ethernet;
  143. struct qualcomm_hdr qualcomm;
  144. uint8_t MDATA [1494];
  145. }
  146. * response = (struct ms_discover_response *) (message);
  147. #ifndef __GNUC__
  148. #pragma pack (pop)
  149. #endif
  150. unsigned timeout = channel->timeout;
  151. channel->timeout = timer;
  152. while (readmessage (channel, message, 0, (MS_DISCOVER | MMTYPE_IND)) > 0)
  153. {
  154. TLVPeek (indicate->MDATA, sizeof (indicate->MDATA));
  155. EthernetHeader (& response->ethernet, indicate->ethernet.OSA, channel->host, channel->type);
  156. QualcommHeader (& response->qualcomm, indicate->qualcomm.MMV, (MS_DISCOVER | MMTYPE_RSP));
  157. memset (response->MDATA, 0, sizeof (response->MDATA));
  158. if (sendmessage (channel, message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) <= 0)
  159. {
  160. error (0, errno, CHANNEL_CANTSEND);
  161. }
  162. }
  163. channel->timeout = timeout;
  164. return;
  165. }
  166. /*====================================================================*
  167. *
  168. * int main (int argc, char const * argv[]);
  169. *
  170. * parse command line, populate plc structure and perform selected
  171. * operations; show help summary when asked; see getoptv and putoptv
  172. * to understand command line parsing and help summary display; see
  173. * plc.h for the definition of struct plc;
  174. *
  175. *--------------------------------------------------------------------*/
  176. int main (int argc, char const * argv [])
  177. {
  178. extern struct channel channel;
  179. extern byte const broadcast [ETHER_ADDR_LEN];
  180. static char const * optv [] =
  181. {
  182. "i:p:qs:t:vw:",
  183. "",
  184. "Qualcomm Atheros Network Solicitor",
  185. #if defined (WINPCAP) || defined (LIBPCAP)
  186. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  187. #else
  188. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  189. #endif
  190. "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
  191. "q\tquiet mode",
  192. "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
  193. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  194. "v\tverbose mode",
  195. "w n\twakeup every (n) milliseconds [" LITERAL (PLC_LONGTIME) "]",
  196. (char const *) (0)
  197. };
  198. struct message message;
  199. char const * profile = PROFILE;
  200. char const * section = SECTION;
  201. unsigned timer = PLC_LONGTIME;
  202. signed c;
  203. if (getenv (PLCDEVICE))
  204. {
  205. #if defined (WINPCAP) || defined (LIBPCAP)
  206. channel.ifindex = atoi (getenv (PLCDEVICE));
  207. #else
  208. channel.ifname = strdup (getenv (PLCDEVICE));
  209. #endif
  210. }
  211. optind = 1;
  212. memcpy (channel.peer, broadcast, sizeof (channel.peer));
  213. while (~ (c = getoptv (argc, argv, optv)))
  214. {
  215. switch (c)
  216. {
  217. case 'i':
  218. #if defined (WINPCAP) || defined (LIBPCAP)
  219. channel.ifindex = atoi (optarg);
  220. #else
  221. channel.ifname = optarg;
  222. #endif
  223. break;
  224. case 'p':
  225. profile = optarg;
  226. break;
  227. case 'q':
  228. _setbits (channel.flags, CHANNEL_SILENCE);
  229. break;
  230. case 's':
  231. section = optarg;
  232. break;
  233. case 't':
  234. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  235. break;
  236. case 'v':
  237. _setbits (channel.flags, CHANNEL_VERBOSE);
  238. break;
  239. case 'w':
  240. timer = (unsigned) (uintspec (optarg, 1, UINT_MAX));
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. argc -= optind;
  247. argv += optind;
  248. if (argc)
  249. {
  250. error (1, 0, ERROR_TOOMANY);
  251. }
  252. if (access (profile, R_OK))
  253. {
  254. error (1, errno, "Can't access '%s'", profile);
  255. }
  256. openchannel (& channel);
  257. while (true)
  258. {
  259. solicit (& channel, & message, profile, section);
  260. monitor (& channel, & message, profile, section, timer);
  261. }
  262. closechannel (& channel);
  263. exit (0);
  264. }