snmpd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2007 by egnite Software GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. #include <dev/board.h>
  33. #include <sys/types.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. #include <arpa/inet.h>
  37. #include <net/route.h>
  38. #include <pro/dhcp.h>
  39. #include <sys/version.h>
  40. #include <sys/timer.h>
  41. #include <pro/snmp_config.h>
  42. #include <pro/snmp_mib.h>
  43. #include <pro/snmp_api.h>
  44. #include <pro/snmp_agent.h>
  45. #include <stdio.h>
  46. #include <io.h>
  47. #include "mib2sys.h"
  48. #include "mib2if.h"
  49. /*!
  50. * \example snmpd/snmpd.c
  51. *
  52. * Basic SNMP Agent.
  53. */
  54. static char *version = "0.2.0";
  55. /*! \brief Default MAC.
  56. * Used if EEPROM is empty. */
  57. #define MYMAC 0x00, 0x06, 0x98, 0x33, 0x44, 0x00
  58. /*! \brief Default local IP address.
  59. * Used if EEPROM configuration and DHCP is unavailable. */
  60. #define MYIP "192.168.192.100"
  61. /*! \brief Default IP mask.
  62. * Used if EEPROM configuration and DHCP is unavailable. */
  63. #define MYMASK "255.255.255.0"
  64. /*! \brief Default gateway address.
  65. * Used if EEPROM configuration and DHCP is unavailable.
  66. * Only required if syslog or time server are located in
  67. * a different network. */
  68. #define MYGATE "192.168.192.1"
  69. /*! \brief Device name used for UART output. */
  70. #define MYUART DEV_CONSOLE.dev_name
  71. /*! \brief Output device. */
  72. #define MYDEV DEV_CONSOLE
  73. /*! \brief UART baudrate. */
  74. #define MYBAUD 115200
  75. /* Determine the compiler. */
  76. #if defined(__IMAGECRAFT__)
  77. #if defined(__AVR__)
  78. #define COMPILERNAME "ICCAVR"
  79. #else
  80. #define COMPILERNAME "ICC"
  81. #endif
  82. #elif defined(__GNUC__)
  83. #if defined(__AVR__)
  84. #define COMPILERNAME "AVRGCC"
  85. #elif defined(__arm__)
  86. #define COMPILERNAME "ARMGCC"
  87. #else
  88. #define COMPILERNAME "GCC"
  89. #endif
  90. #else
  91. #define COMPILERNAME "Compiler unknown"
  92. #endif
  93. /* Result codes. */
  94. #define UART_OK 0x0001
  95. #define STDOUT_OK 0x0002
  96. #define STDERR_OK 0x0004
  97. #define BAUDRATE_OK 0x0008
  98. #define LANDEV_OK 0x0010
  99. #define NETIF_OK 0x0020
  100. #define NETROUTE_OK 0x0040
  101. #define TIMED_OK 0x0080
  102. int main(void)
  103. {
  104. UDPSOCKET *sock;
  105. OID view_all[] = { SNMP_OID_INTERNET };
  106. int view_idx;
  107. uint32_t baud = MYBAUD;
  108. uint8_t mac[6] = { MYMAC };
  109. int rc = 0;
  110. /*
  111. * Register UART devices, assign stdout and stderr to this device
  112. * and set the baudrate.
  113. */
  114. if (NutRegisterDevice(&MYDEV, 0, 0) == 0) {
  115. rc |= UART_OK;
  116. if (freopen(MYUART, "w", stdout)) {
  117. rc |= STDOUT_OK;
  118. if (_ioctl(_fileno(stdout), UART_SETSPEED, &baud) == 0) {
  119. rc |= BAUDRATE_OK;
  120. }
  121. }
  122. if (freopen(MYUART, "w", stderr)) {
  123. rc |= STDERR_OK;
  124. }
  125. }
  126. /*
  127. * Print banner.
  128. */
  129. if (rc & STDOUT_OK) {
  130. printf("\n\nSNMP Agent %s\nNut/OS %s\n", version, NutVersionString());
  131. puts("Compiled by " COMPILERNAME);
  132. puts("Configure network");
  133. }
  134. /*
  135. * Register LAN device and configure network interface.
  136. */
  137. #ifdef DEV_ETHER
  138. if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
  139. rc |= LANDEV_OK;
  140. if (NutDhcpIfConfig("eth0", 0, 60000) == 0) {
  141. rc |= NETIF_OK;
  142. } else if (NutDhcpIfConfig("eth0", mac, 60000) == 0) {
  143. rc |= NETIF_OK;
  144. } else if (NutNetIfConfig("eth0", mac, inet_addr(MYIP), inet_addr(MYMASK)) == 0) {
  145. rc |= NETIF_OK;
  146. #ifdef MYGATE
  147. if (NutIpRouteAdd(0, 0, inet_addr(MYGATE), &DEV_ETHER) == 0) {
  148. rc |= NETROUTE_OK;
  149. }
  150. #endif
  151. }
  152. }
  153. sock = NutUdpCreateSocket(SNMP_PORT);
  154. /* Nut/Net doesn't provide UDP datagram buffering by default. */
  155. {
  156. uint16_t max_ms = SNMP_MAX_MSG_SIZE * 2;
  157. NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms));
  158. }
  159. /* Register system variables. */
  160. if (MibRegisterSysVars()) {
  161. printf("Failed to register MibSys\n");
  162. for (;;)
  163. NutSleep(1000);
  164. }
  165. /* Register interface variables. */
  166. if (MibRegisterIfVars()) {
  167. printf("Failed to register MibIf\n");
  168. for (;;)
  169. NutSleep(1000);
  170. }
  171. /* Create views. */
  172. if ((view_idx = SnmpViewCreate("all", view_all, sizeof(view_all), SNMP_VIEW_INCLUDED)) <= 0) {
  173. printf("Failed to create view\n");
  174. for (;;)
  175. NutSleep(1000);
  176. }
  177. /* Create communities. */
  178. if (SnmpCommunityCreate("public", view_idx, view_idx) || SnmpCommunityCreate("private", view_idx, view_idx)) {
  179. printf("Failed to create communities\n");
  180. for (;;)
  181. NutSleep(1000);
  182. }
  183. /* Run agent. */
  184. SnmpAgent(sock);
  185. /* Program stopped. */
  186. NutUdpDestroySocket(sock);
  187. #endif
  188. for (;;) {
  189. NutSleep(100);
  190. printf("Hello ");
  191. }
  192. return 0;
  193. }