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 <stdio.h> 00036 #include <io.h> 00037 #include <sys/types.h> 00038 #include <ctype.h> 00039 #include <errno.h> 00040 00041 #include <arpa/inet.h> 00042 #include <net/route.h> 00043 #include <pro/dhcp.h> 00044 00045 #include <sys/version.h> 00046 #include <sys/timer.h> 00047 00048 #include <pro/snmp_config.h> 00049 #include <pro/snmp_mib.h> 00050 #include <pro/snmp_api.h> 00051 #include <pro/snmp_agent.h> 00052 #include "mib2sys.h" 00053 #include "mib2if.h" 00054 00060 static char *version = "0.2.0"; 00061 00064 #define MYMAC 0x00, 0x06, 0x98, 0x33, 0x44, 0x00 00065 00068 #define MYIP "192.168.192.100" 00069 00072 #define MYMASK "255.255.255.0" 00073 00078 #define MYGATE "192.168.192.1" 00079 00081 #define MYUART DEV_DEBUG_NAME 00082 00084 #define MYDEV DEV_DEBUG 00085 00087 #define MYBAUD 115200 00088 00089 /* Determine the compiler. */ 00090 #if defined(__IMAGECRAFT__) 00091 #if defined(__AVR__) 00092 #define COMPILERNAME "ICCAVR" 00093 #else 00094 #define COMPILERNAME "ICC" 00095 #endif 00096 #elif defined(__GNUC__) 00097 #if defined(__AVR__) 00098 #define COMPILERNAME "AVRGCC" 00099 #elif defined(__arm__) 00100 #define COMPILERNAME "ARMGCC" 00101 #else 00102 #define COMPILERNAME "GCC" 00103 #endif 00104 #else 00105 #define COMPILERNAME "Compiler unknown" 00106 #endif 00107 00108 /* Result codes. */ 00109 #define UART_OK 0x0001 00110 #define STDOUT_OK 0x0002 00111 #define STDERR_OK 0x0004 00112 #define BAUDRATE_OK 0x0008 00113 #define LANDEV_OK 0x0010 00114 #define NETIF_OK 0x0020 00115 #define NETROUTE_OK 0x0040 00116 #define TIMED_OK 0x0080 00117 00118 int main(void) 00119 { 00120 UDPSOCKET *sock; 00121 OID view_all[] = { SNMP_OID_INTERNET }; 00122 int view_idx; 00123 u_long baud = MYBAUD; 00124 u_char mac[6] = { MYMAC }; 00125 int rc = 0; 00126 00127 /* 00128 * Register UART devices, assign stdout and stderr to this device 00129 * and set the baudrate. 00130 */ 00131 if (NutRegisterDevice(&MYDEV, 0, 0) == 0) { 00132 rc |= UART_OK; 00133 if (freopen(MYUART, "w", stdout)) { 00134 rc |= STDOUT_OK; 00135 if (_ioctl(_fileno(stdout), UART_SETSPEED, &baud) == 0) { 00136 rc |= BAUDRATE_OK; 00137 } 00138 } 00139 if (freopen(MYUART, "w", stderr)) { 00140 rc |= STDERR_OK; 00141 } 00142 } 00143 00144 /* 00145 * Print banner. 00146 */ 00147 if (rc & STDOUT_OK) { 00148 printf("\n\nSNMP Agent %s\nNut/OS %s\n", version, NutVersionString()); 00149 puts("Compiled by " COMPILERNAME); 00150 puts("Configure network"); 00151 } 00152 00153 /* 00154 * Register LAN device and configure network interface. 00155 */ 00156 if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) { 00157 rc |= LANDEV_OK; 00158 if (NutDhcpIfConfig("eth0", 0, 60000) == 0) { 00159 rc |= NETIF_OK; 00160 } else if (NutDhcpIfConfig("eth0", mac, 60000) == 0) { 00161 rc |= NETIF_OK; 00162 } else if (NutNetIfConfig("eth0", mac, inet_addr(MYIP), inet_addr(MYMASK)) == 0) { 00163 rc |= NETIF_OK; 00164 #ifdef MYGATE 00165 if (NutIpRouteAdd(0, 0, inet_addr(MYGATE), &DEV_ETHER) == 0) { 00166 rc |= NETROUTE_OK; 00167 } 00168 #endif 00169 } 00170 } 00171 00172 sock = NutUdpCreateSocket(SNMP_PORT); 00173 /* Nut/Net doesn't provide UDP datagram buffering by default. */ 00174 { 00175 u_short max_ms = SNMP_MAX_MSG_SIZE * 2; 00176 NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms)); 00177 } 00178 00179 /* Register system variables. */ 00180 if (MibRegisterSysVars()) { 00181 printf("Failed to register MibSys\n"); 00182 for (;;) 00183 NutSleep(1000); 00184 } 00185 /* Register interface variables. */ 00186 if (MibRegisterIfVars()) { 00187 printf("Failed to register MibIf\n"); 00188 for (;;) 00189 NutSleep(1000); 00190 } 00191 00192 /* Create views. */ 00193 if ((view_idx = SnmpViewCreate("all", view_all, sizeof(view_all), SNMP_VIEW_INCLUDED)) <= 0) { 00194 printf("Failed to create view\n"); 00195 for (;;) 00196 NutSleep(1000); 00197 } 00198 /* Create communities. */ 00199 if (SnmpCommunityCreate("public", view_idx, view_idx) || SnmpCommunityCreate("private", view_idx, view_idx)) { 00200 printf("Failed to create communities\n"); 00201 for (;;) 00202 NutSleep(1000); 00203 } 00204 00205 /* Run agent. */ 00206 SnmpAgent(sock); 00207 00208 /* Program stopped. */ 00209 NutUdpDestroySocket(sock); 00210 for (;;) { 00211 NutSleep(100); 00212 printf("Hello "); 00213 } 00214 return 0; 00215 }