hpavd.c.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. hpavd.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='hpav.c.html' title=' hpav.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='hpavkey.c.html' title=' hpavkey.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * hpavd.c - Qualcomm Atheros Homeplug AV Packet Daemon;
  64. *
  65. * prints incoming Qualcomm Atheros HomePlug AV vendor-specific
  66. * management messages on stdout in hex dump format for analysis;
  67. *
  68. * the program can be run in foreground or background, as a daemon;
  69. *
  70. * this program receives raw ethernet frames and so requires root
  71. * privileges; if you install it using &quot;chmod 555&quot; and &quot;chown
  72. * root:root&quot; then you must login as root to run it; otherwise, you
  73. * can install it using &quot;chmod 4555&quot; and &quot;chown root:root&quot; so that
  74. * anyone can run it; the program will refuse to run until you get
  75. * thing right;
  76. *
  77. *
  78. * Contributor(s):
  79. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  80. *
  81. *--------------------------------------------------------------------*/
  82. /*====================================================================*
  83. * system header files;
  84. *--------------------------------------------------------------------*/
  85. #include &lt;fcntl.h&gt;
  86. #include &lt;stdio.h&gt;
  87. #include &lt;ctype.h&gt;
  88. #include &lt;unistd.h&gt;
  89. #include &lt;stdint.h&gt;
  90. #include &lt;stdlib.h&gt;
  91. #include &lt;string.h&gt;
  92. #include &lt;sys/signal.h&gt;
  93. #include &lt;sys/ioctl.h&gt;
  94. #include &lt;errno.h&gt;
  95. #ifdef __linux__
  96. #include &lt;net/if.h&gt;
  97. #include &lt;net/if_arp.h&gt;
  98. #include &lt;netpacket/packet.h&gt;
  99. #include &lt;signal.h&gt;
  100. #endif
  101. /*====================================================================*
  102. * custom header files;
  103. *--------------------------------------------------------------------*/
  104. #include &quot;../tools/getoptv.h&quot;
  105. #include &quot;../tools/putoptv.h&quot;
  106. #include &quot;../tools/memory.h&quot;
  107. #include &quot;../tools/number.h&quot;
  108. #include &quot;../tools/types.h&quot;
  109. #include &quot;../tools/flags.h&quot;
  110. #include &quot;../tools/error.h&quot;
  111. #include &quot;../plc/plc.h&quot;
  112. #include &quot;../mme/mme.h&quot;
  113. /*====================================================================*
  114. * custom source files;
  115. *--------------------------------------------------------------------*/
  116. #ifndef MAKEFILE
  117. #include &quot;../tools/getoptv.c&quot;
  118. #include &quot;../tools/putoptv.c&quot;
  119. #include &quot;../tools/version.c&quot;
  120. #include &quot;../tools/hexdump.c&quot;
  121. #include &quot;../tools/hexstring.c&quot;
  122. #include &quot;../tools/hexdecode.c&quot;
  123. #include &quot;../tools/error.c&quot;
  124. #include &quot;../tools/emalloc.c&quot;
  125. #endif
  126. #ifndef MAKEFILE
  127. #include &quot;../mme/MMEPeek.c&quot;
  128. #include &quot;../mme/MMEName.c&quot;
  129. #include &quot;../mme/MMEMode.c&quot;
  130. #endif
  131. /*====================================================================*
  132. * program constants;
  133. *--------------------------------------------------------------------*/
  134. #define HPAVD_DAEMON (1 &lt;&lt; 0)
  135. #define HPAVD_VERBOSE (1 &lt;&lt; 1)
  136. #define HPAVD_SILENCE (1 &lt;&lt; 2)
  137. #define HPAVD_ERROR (1 &lt;&lt; 3)
  138. #define PLCDEVICE &quot;PLC&quot;
  139. #define ETHDEVICE &quot;eth1&quot;
  140. #define ETHNUMBER 2
  141. /*====================================================================*
  142. * program variables;
  143. *--------------------------------------------------------------------*/
  144. static bool done = false;
  145. /*====================================================================*
  146. *
  147. * void terminate (signo_t signal);
  148. *
  149. * terminate the program; we want to ensure an orgaized program
  150. * exit so that the original Ethernet adapter state is restored;
  151. *
  152. *
  153. *--------------------------------------------------------------------*/
  154. static void terminate (signo_t signal)
  155. {
  156. done = true;
  157. return;
  158. }
  159. /*====================================================================*
  160. *
  161. * int main (int argc, char * argv[]);
  162. *
  163. *
  164. *--------------------------------------------------------------------*/
  165. int main (int argc, char const * argv [])
  166. {
  167. struct sigaction sa;
  168. struct ifreq ifreq;
  169. struct sockaddr_ll sockaddr =
  170. {
  171. PF_PACKET,
  172. htons (ETH_P_HPAV),
  173. 0x0000,
  174. ARPHRD_ETHER,
  175. PACKET_OTHERHOST,
  176. ETHER_ADDR_LEN,
  177. {
  178. 0x00,
  179. 0x00,
  180. 0x00,
  181. 0x00,
  182. 0x00,
  183. 0x00,
  184. 0x00,
  185. 0x00
  186. }
  187. };
  188. static char const * optv [] =
  189. {
  190. &quot;di:qv&quot;,
  191. PUTOPTV_S_DIVINE,
  192. &quot;Qualcomm Atheros HomePlug AV Packet Daemon&quot;,
  193. &quot;d\trun in background as daemon&quot;,
  194. &quot;i s\thost interface is (s) [&quot; ETHDEVICE &quot;]&quot;,
  195. &quot;q\tsuppress normal output&quot;,
  196. &quot;v\tverbose messages on stdout&quot;,
  197. (char const *) (0)
  198. };
  199. uint8_t packet [ETHER_MAX_LEN];
  200. flag_t state = (flag_t)(0);
  201. flag_t flags = (flag_t)(0);
  202. sock_t fd = -1;
  203. signed c;
  204. memset (&amp;ifreq, 0, sizeof (ifreq));
  205. memcpy (ifreq.ifr_name, ETHDEVICE, sizeof (ETHDEVICE));
  206. if (getenv (PLCDEVICE))
  207. {
  208. memcpy (ifreq.ifr_name, getenv (PLCDEVICE), sizeof (ifreq.ifr_name));
  209. }
  210. optind = 1;
  211. while ((c = getoptv (argc, argv, optv)) != -1)
  212. {
  213. switch ((char) (c))
  214. {
  215. case 'd':
  216. _setbits (flags, HPAVD_DAEMON);
  217. break;
  218. case 'i':
  219. memcpy (ifreq.ifr_name, optarg, sizeof (ifreq.ifr_name));
  220. break;
  221. case 'q':
  222. _setbits (flags, HPAVD_SILENCE);
  223. break;
  224. case 'v':
  225. _setbits (flags, HPAVD_VERBOSE);
  226. break;
  227. default:
  228. break;
  229. }
  230. }
  231. argc -= optind;
  232. argv += optind;
  233. if (geteuid ())
  234. {
  235. error (1, EPERM, ERROR_NOTROOT);
  236. }
  237. if (_anyset (flags, HPAVD_DAEMON))
  238. {
  239. pid_t pid = fork ();
  240. if (pid &lt; 0)
  241. {
  242. error (1, errno, &quot;razzlefrats!&quot;);
  243. }
  244. if (pid &gt; 0)
  245. {
  246. exit (0);
  247. }
  248. }
  249. memset (&amp;sa, 0, sizeof (struct sigaction));
  250. sa.sa_handler = terminate;
  251. sigaction (SIGTERM, &amp;sa, (struct sigaction *)(0));
  252. sigaction (SIGQUIT, &amp;sa, (struct sigaction *)(0));
  253. sigaction (SIGTSTP, &amp;sa, (struct sigaction *)(0));
  254. sigaction (SIGINT, &amp;sa, (struct sigaction *)(0));
  255. sigaction (SIGHUP, &amp;sa, (struct sigaction *)(0));
  256. if ((fd = socket (sockaddr.sll_family, SOCK_RAW, sockaddr.sll_protocol)) == -1)
  257. {
  258. error (1, errno, &quot;Can't create socket for %s&quot;, ifreq.ifr_name);
  259. }
  260. if (ioctl (fd, SIOCGIFFLAGS, &amp;ifreq) &lt; 0)
  261. {
  262. error (1, errno, &quot;Can't read %s device state&quot;, ifreq.ifr_name);
  263. }
  264. state = ifreq.ifr_flags;
  265. _setbits (ifreq.ifr_flags, (IFF_UP | IFF_BROADCAST));
  266. _clrbits (ifreq.ifr_flags, (IFF_MULTICAST | IFF_ALLMULTI | IFF_PROMISC));
  267. if (ioctl (fd, SIOCSIFFLAGS, &amp;ifreq) &lt; 0)
  268. {
  269. error (1, errno, &quot;Can't change %s device state&quot;, ifreq.ifr_name);
  270. }
  271. if (ioctl (fd, SIOCGIFINDEX, &amp;ifreq) == -1)
  272. {
  273. error (1, errno, &quot;Can't get %s interface index&quot;, ifreq.ifr_name);
  274. }
  275. sockaddr.sll_ifindex = ifreq.ifr_ifindex;
  276. if (ioctl (fd, SIOCGIFHWADDR, &amp;ifreq) == -1)
  277. {
  278. error (1, errno, &quot;Can't get %s hardware address&quot;, ifreq.ifr_name);
  279. }
  280. memcpy (sockaddr.sll_addr, ifreq.ifr_ifru.ifru_hwaddr.sa_data, sizeof (sockaddr.sll_addr));
  281. if (bind (fd, (struct sockaddr *) (&amp;sockaddr), sizeof (struct sockaddr_ll)) == -1)
  282. {
  283. error (1, errno, &quot;Can't bind socket to %s&quot;, ifreq.ifr_name);
  284. }
  285. while (!done)
  286. {
  287. signed length = recvfrom (fd, packet, sizeof (packet), 0, (struct sockaddr *) (0), (socklen_t *)(0));
  288. if (length &gt; 0)
  289. {
  290. if (_allclr (flags, HPAVD_SILENCE))
  291. {
  292. MMEPeek (&amp;packet, length, stdout);
  293. }
  294. if (_anyset (flags, HPAVD_VERBOSE))
  295. {
  296. hexdump (&amp;packet, 0, length, stdout);
  297. }
  298. }
  299. }
  300. ifreq.ifr_flags = state;
  301. if (ioctl (fd, SIOCSIFFLAGS, &amp;ifreq) &lt; 0)
  302. {
  303. error (1, errno, &quot;Can't restore %s device state&quot;, ifreq.ifr_name);
  304. }
  305. close (fd);
  306. return (0);
  307. }
  308. </pre>
  309. <div class='footerlink'>
  310. [<a href='hpav.c.html' title=' hpav.c '>PREV</a>]
  311. [<a href='toolkit.html' title=' Index '>HOME</a>]
  312. [<a href='hpavkey.c.html' title=' hpavkey.c '>NEXT</a>]
  313. </div>
  314. </body>
  315. </html>