Nut/OS  4.10.3
API Reference
snmpd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2007 by egnite Software GmbH
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00021  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  */
00032 
00033 #include <dev/board.h>
00034 
00035 #include <sys/types.h>
00036 #include <ctype.h>
00037 #include <errno.h>
00038 
00039 #include <arpa/inet.h>
00040 #include <net/route.h>
00041 #include <pro/dhcp.h>
00042 
00043 #include <sys/version.h>
00044 #include <sys/timer.h>
00045 
00046 #include <pro/snmp_config.h>
00047 #include <pro/snmp_mib.h>
00048 #include <pro/snmp_api.h>
00049 #include <pro/snmp_agent.h>
00050 
00051 #include <stdio.h>
00052 #include <io.h>
00053 
00054 #include "mib2sys.h"
00055 #include "mib2if.h"
00056 
00062 static char *version = "0.2.0";
00063 
00066 #define MYMAC   0x00, 0x06, 0x98, 0x33, 0x44, 0x00
00067 
00070 #define MYIP    "192.168.192.100"
00071 
00074 #define MYMASK  "255.255.255.0"
00075 
00080 #define MYGATE  "192.168.192.1"
00081 
00083 #define MYUART  DEV_CONSOLE_NAME
00084 
00086 #define MYDEV   DEV_CONSOLE
00087 
00089 #define MYBAUD  115200
00090 
00091 /* Determine the compiler. */
00092 #if defined(__IMAGECRAFT__)
00093 #if defined(__AVR__)
00094 #define COMPILERNAME   "ICCAVR"
00095 #else
00096 #define COMPILERNAME   "ICC"
00097 #endif
00098 #elif defined(__GNUC__)
00099 #if defined(__AVR__)
00100 #define COMPILERNAME   "AVRGCC"
00101 #elif defined(__arm__)
00102 #define COMPILERNAME   "ARMGCC"
00103 #else
00104 #define COMPILERNAME   "GCC"
00105 #endif
00106 #else
00107 #define COMPILERNAME   "Compiler unknown"
00108 #endif
00109 
00110 /* Result codes. */
00111 #define UART_OK     0x0001
00112 #define STDOUT_OK   0x0002
00113 #define STDERR_OK   0x0004
00114 #define BAUDRATE_OK 0x0008
00115 #define LANDEV_OK   0x0010
00116 #define NETIF_OK    0x0020
00117 #define NETROUTE_OK 0x0040
00118 #define TIMED_OK    0x0080
00119 
00120 int main(void)
00121 {
00122     UDPSOCKET *sock;
00123     OID view_all[] = { SNMP_OID_INTERNET };
00124     int view_idx;
00125     uint32_t baud = MYBAUD;
00126     uint8_t mac[6] = { MYMAC };
00127     int rc = 0;
00128 
00129     /*
00130      * Register UART devices, assign stdout and stderr to this device
00131      * and set the baudrate.
00132      */
00133     if (NutRegisterDevice(&MYDEV, 0, 0) == 0) {
00134         rc |= UART_OK;
00135         if (freopen(MYUART, "w", stdout)) {
00136             rc |= STDOUT_OK;
00137             if (_ioctl(_fileno(stdout), UART_SETSPEED, &baud) == 0) {
00138                 rc |= BAUDRATE_OK;
00139             }
00140         }
00141         if (freopen(MYUART, "w", stderr)) {
00142             rc |= STDERR_OK;
00143         }
00144     }
00145 
00146     /*
00147      * Print banner.
00148      */
00149     if (rc & STDOUT_OK) {
00150         printf("\n\nSNMP Agent %s\nNut/OS %s\n", version, NutVersionString());
00151         puts("Compiled by " COMPILERNAME);
00152         puts("Configure network");
00153     }
00154 
00155     /*
00156      * Register LAN device and configure network interface.
00157      */
00158 #ifdef DEV_ETHER
00159     if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
00160         rc |= LANDEV_OK;
00161         if (NutDhcpIfConfig("eth0", 0, 60000) == 0) {
00162             rc |= NETIF_OK;
00163         } else if (NutDhcpIfConfig("eth0", mac, 60000) == 0) {
00164             rc |= NETIF_OK;
00165         } else if (NutNetIfConfig("eth0", mac, inet_addr(MYIP), inet_addr(MYMASK)) == 0) {
00166             rc |= NETIF_OK;
00167 #ifdef MYGATE
00168             if (NutIpRouteAdd(0, 0, inet_addr(MYGATE), &DEV_ETHER) == 0) {
00169                 rc |= NETROUTE_OK;
00170             }
00171 #endif
00172         }
00173     }
00174 
00175     sock = NutUdpCreateSocket(SNMP_PORT);
00176     /* Nut/Net doesn't provide UDP datagram buffering by default. */
00177     {
00178         uint16_t max_ms = SNMP_MAX_MSG_SIZE * 2;
00179         NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms));
00180     }
00181 
00182     /* Register system variables. */
00183     if (MibRegisterSysVars()) {
00184         printf("Failed to register MibSys\n");
00185         for (;;)
00186             NutSleep(1000);
00187     }
00188     /* Register interface variables. */
00189     if (MibRegisterIfVars()) {
00190         printf("Failed to register MibIf\n");
00191         for (;;)
00192             NutSleep(1000);
00193     }
00194 
00195     /* Create views. */
00196     if ((view_idx = SnmpViewCreate("all", view_all, sizeof(view_all), SNMP_VIEW_INCLUDED)) <= 0) {
00197         printf("Failed to create view\n");
00198         for (;;)
00199             NutSleep(1000);
00200     }
00201     /* Create communities. */
00202     if (SnmpCommunityCreate("public", view_idx, view_idx) || SnmpCommunityCreate("private", view_idx, view_idx)) {
00203         printf("Failed to create communities\n");
00204         for (;;)
00205             NutSleep(1000);
00206     }
00207 
00208     /* Run agent. */
00209     SnmpAgent(sock);
00210 
00211     /* Program stopped. */
00212     NutUdpDestroySocket(sock);
00213 #endif
00214 
00215     for (;;) {
00216         NutSleep(100);
00217         printf("Hello ");
00218     }
00219     return 0;
00220 }