1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ./agent/current/tests/snmpping.c
|
4 |
|
|
//
|
5 |
|
|
//
|
6 |
|
|
//==========================================================================
|
7 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
8 |
|
|
// -------------------------------------------
|
9 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
10 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
11 |
|
|
//
|
12 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
13 |
|
|
// the terms of the GNU General Public License as published by the Free
|
14 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
15 |
|
|
// version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
18 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License
|
23 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use
|
27 |
|
|
// macros or inline functions from this file, or you compile this file
|
28 |
|
|
// and link it with other works to produce a work based on this file,
|
29 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
30 |
|
|
// the GNU General Public License. However the source code for this file
|
31 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
32 |
|
|
// General Public License v2.
|
33 |
|
|
//
|
34 |
|
|
// This exception does not invalidate any other reasons why a work based
|
35 |
|
|
// on this file might be covered by the GNU General Public License.
|
36 |
|
|
// -------------------------------------------
|
37 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
38 |
|
|
//####UCDSNMPCOPYRIGHTBEGIN####
|
39 |
|
|
//
|
40 |
|
|
// -------------------------------------------
|
41 |
|
|
//
|
42 |
|
|
// Portions of this software may have been derived from the UCD-SNMP
|
43 |
|
|
// project, <http://ucd-snmp.ucdavis.edu/> from the University of
|
44 |
|
|
// California at Davis, which was originally based on the Carnegie Mellon
|
45 |
|
|
// University SNMP implementation. Portions of this software are therefore
|
46 |
|
|
// covered by the appropriate copyright disclaimers included herein.
|
47 |
|
|
//
|
48 |
|
|
// The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"
|
49 |
|
|
// -------------------------------------------
|
50 |
|
|
//
|
51 |
|
|
//####UCDSNMPCOPYRIGHTEND####
|
52 |
|
|
//==========================================================================
|
53 |
|
|
//#####DESCRIPTIONBEGIN####
|
54 |
|
|
//
|
55 |
|
|
// Author(s): hmt
|
56 |
|
|
// Contributors: hmt
|
57 |
|
|
// Date: 2000-05-30
|
58 |
|
|
// Purpose: Port of UCD-SNMP distribution to eCos.
|
59 |
|
|
// Description:
|
60 |
|
|
//
|
61 |
|
|
//
|
62 |
|
|
//####DESCRIPTIONEND####
|
63 |
|
|
//
|
64 |
|
|
//==========================================================================
|
65 |
|
|
/********************************************************************
|
66 |
|
|
Copyright 1989, 1991, 1992 by Carnegie Mellon University
|
67 |
|
|
|
68 |
|
|
Derivative Work -
|
69 |
|
|
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
|
70 |
|
|
|
71 |
|
|
All Rights Reserved
|
72 |
|
|
|
73 |
|
|
Permission to use, copy, modify and distribute this software and its
|
74 |
|
|
documentation for any purpose and without fee is hereby granted,
|
75 |
|
|
provided that the above copyright notice appears in all copies and
|
76 |
|
|
that both that copyright notice and this permission notice appear in
|
77 |
|
|
supporting documentation, and that the name of CMU and The Regents of
|
78 |
|
|
the University of California not be used in advertising or publicity
|
79 |
|
|
pertaining to distribution of the software without specific written
|
80 |
|
|
permission.
|
81 |
|
|
|
82 |
|
|
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
|
83 |
|
|
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
|
84 |
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU OR
|
85 |
|
|
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
|
86 |
|
|
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
87 |
|
|
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
|
88 |
|
|
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
89 |
|
|
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
90 |
|
|
*********************************************************************/
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
// -------------------------------------------------------------------------
|
94 |
|
|
// Configuration of the test... now from CDL
|
95 |
|
|
|
96 |
|
|
// Do we test the interfaces in promiscuous mode?
|
97 |
|
|
//#define CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
|
98 |
|
|
|
99 |
|
|
// Do we make the test run forever?
|
100 |
|
|
//#define CYGNUM_SNMPAGENT_TESTS_ITERATIONS 999999
|
101 |
|
|
|
102 |
|
|
// Do we initialize SNMP v3 MIBs and authentication database?
|
103 |
|
|
//#define CYGSEM_SNMPAGENT_TESTS_SNMPv3
|
104 |
|
|
|
105 |
|
|
// ------------------------------------------------------------------------
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
// PING test code
|
109 |
|
|
|
110 |
|
|
#include <network.h>
|
111 |
|
|
#include <stdio.h>
|
112 |
|
|
|
113 |
|
|
#include <pkgconf/system.h>
|
114 |
|
|
#include <pkgconf/net.h>
|
115 |
|
|
#include <pkgconf/snmpagent.h>
|
116 |
|
|
|
117 |
|
|
#include <unistd.h>
|
118 |
|
|
|
119 |
|
|
#ifdef CYGSEM_SNMPAGENT_TESTS_SNMPv3
|
120 |
|
|
#include <ucd-snmp/config.h>
|
121 |
|
|
#include <ucd-snmp/asn1.h>
|
122 |
|
|
#include <ucd-snmp/snmp_api.h>
|
123 |
|
|
#include <ucd-snmp/snmp_vars.h>
|
124 |
|
|
|
125 |
|
|
#include <ucd-snmp/snmpv3.h>
|
126 |
|
|
#include <ucd-snmp/usmUser.h>
|
127 |
|
|
#include <ucd-snmp/usmStats.h>
|
128 |
|
|
#include <ucd-snmp/snmpEngine.h>
|
129 |
|
|
#endif // CYGSEM_SNMPAGENT_TESTS_SNMPv3
|
130 |
|
|
|
131 |
|
|
#include <cyg/infra/testcase.h>
|
132 |
|
|
|
133 |
|
|
#ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
|
134 |
|
|
#include CYGBLD_DEVS_ETH_DEVICE_H // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
|
135 |
|
|
#endif
|
136 |
|
|
|
137 |
|
|
#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
|
138 |
|
|
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
|
139 |
|
|
# include CYGTST_DEVS_ETH_TEST_NET_REALTIME
|
140 |
|
|
# endif
|
141 |
|
|
#endif
|
142 |
|
|
|
143 |
|
|
// Fill in the blanks if necessary
|
144 |
|
|
#ifndef TNR_OFF
|
145 |
|
|
# define TNR_OFF()
|
146 |
|
|
#endif
|
147 |
|
|
#ifndef TNR_ON
|
148 |
|
|
# define TNR_ON()
|
149 |
|
|
#endif
|
150 |
|
|
#ifndef TNR_INIT
|
151 |
|
|
# define TNR_INIT()
|
152 |
|
|
#endif
|
153 |
|
|
#ifndef TNR_PRINT_ACTIVITY
|
154 |
|
|
# define TNR_PRINT_ACTIVITY()
|
155 |
|
|
#endif
|
156 |
|
|
|
157 |
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
|
158 |
|
|
static char stack[STACK_SIZE];
|
159 |
|
|
static cyg_thread thread_data;
|
160 |
|
|
static cyg_handle_t thread_handle;
|
161 |
|
|
|
162 |
|
|
#define NUM_PINGS 16
|
163 |
|
|
#define MAX_PACKET 4096
|
164 |
|
|
#define MIN_PACKET 64
|
165 |
|
|
#define MAX_SEND 4000
|
166 |
|
|
|
167 |
|
|
#define PACKET_ADD ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
|
168 |
|
|
#define nPACKET_ADD 1
|
169 |
|
|
|
170 |
|
|
static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
|
171 |
|
|
|
172 |
|
|
#define UNIQUEID 0x1234
|
173 |
|
|
|
174 |
|
|
// Compute INET checksum
|
175 |
|
|
int
|
176 |
|
|
inet_cksum(u_short *addr, int len)
|
177 |
|
|
{
|
178 |
|
|
register int nleft = len;
|
179 |
|
|
register u_short *w = addr;
|
180 |
|
|
register u_short answer;
|
181 |
|
|
register u_int sum = 0;
|
182 |
|
|
u_short odd_byte = 0;
|
183 |
|
|
|
184 |
|
|
/*
|
185 |
|
|
* Our algorithm is simple, using a 32 bit accumulator (sum),
|
186 |
|
|
* we add sequential 16 bit words to it, and at the end, fold
|
187 |
|
|
* back all the carry bits from the top 16 bits into the lower
|
188 |
|
|
* 16 bits.
|
189 |
|
|
*/
|
190 |
|
|
while( nleft > 1 ) {
|
191 |
|
|
sum += *w++;
|
192 |
|
|
nleft -= 2;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/* mop up an odd byte, if necessary */
|
196 |
|
|
if( nleft == 1 ) {
|
197 |
|
|
*(u_char *)(&odd_byte) = *(u_char *)w;
|
198 |
|
|
sum += odd_byte;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/*
|
202 |
|
|
* add back carry outs from top 16 bits to low 16 bits
|
203 |
|
|
*/
|
204 |
|
|
sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
|
205 |
|
|
sum += (sum >> 16); /* add carry */
|
206 |
|
|
answer = ~sum; /* truncate to 16 bits */
|
207 |
|
|
return (answer);
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
static int
|
211 |
|
|
show_icmp(unsigned char *pkt, int len,
|
212 |
|
|
struct sockaddr_in *from, struct sockaddr_in *to)
|
213 |
|
|
{
|
214 |
|
|
cyg_tick_count_t *tp, tv;
|
215 |
|
|
struct ip *ip;
|
216 |
|
|
struct icmp *icmp;
|
217 |
|
|
tv = cyg_current_time();
|
218 |
|
|
ip = (struct ip *)pkt;
|
219 |
|
|
if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
|
220 |
|
|
diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
|
221 |
|
|
inet_ntoa(from->sin_addr), len, ip->ip_v);
|
222 |
|
|
return 0;
|
223 |
|
|
}
|
224 |
|
|
icmp = (struct icmp *)(pkt + sizeof(*ip));
|
225 |
|
|
len -= (sizeof(*ip) + 8);
|
226 |
|
|
if ( 0 >= len ) {
|
227 |
|
|
diag_printf("%s: Completely bogus short packet%s\n",
|
228 |
|
|
inet_ntoa(from->sin_addr), 0 == len ? "" : " [no ICMP header]");
|
229 |
|
|
return 0;
|
230 |
|
|
}
|
231 |
|
|
tp = (cyg_tick_count_t *)&icmp->icmp_data;
|
232 |
|
|
if (icmp->icmp_type != ICMP_ECHOREPLY) {
|
233 |
|
|
diag_printf("%s: Invalid ICMP - type: %d, len (databytes): %d\n",
|
234 |
|
|
inet_ntoa(from->sin_addr), icmp->icmp_type, len);
|
235 |
|
|
return 0;
|
236 |
|
|
}
|
237 |
|
|
if (icmp->icmp_id != UNIQUEID) {
|
238 |
|
|
diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
|
239 |
|
|
inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
|
240 |
|
|
}
|
241 |
|
|
diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
|
242 |
|
|
diag_printf("icmp_seq=%d", icmp->icmp_seq);
|
243 |
|
|
diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
|
244 |
|
|
return (from->sin_addr.s_addr == to->sin_addr.s_addr);
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
static void
|
248 |
|
|
ping_host(int s, struct sockaddr_in *host)
|
249 |
|
|
{
|
250 |
|
|
struct icmp *icmp = (struct icmp *)pkt1;
|
251 |
|
|
int icmp_len = MIN_PACKET;
|
252 |
|
|
int seq, ok_recv, bogus_recv;
|
253 |
|
|
cyg_tick_count_t *tp;
|
254 |
|
|
long *dp;
|
255 |
|
|
struct sockaddr_in from;
|
256 |
|
|
int i, len, fromlen;
|
257 |
|
|
|
258 |
|
|
ok_recv = 0;
|
259 |
|
|
bogus_recv = 0;
|
260 |
|
|
TNR_OFF();
|
261 |
|
|
diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
|
262 |
|
|
for (seq = 0; seq < NUM_PINGS; seq++, icmp_len += PACKET_ADD ) {
|
263 |
|
|
TNR_ON();
|
264 |
|
|
// Build ICMP packet
|
265 |
|
|
icmp->icmp_type = ICMP_ECHO;
|
266 |
|
|
icmp->icmp_code = 0;
|
267 |
|
|
icmp->icmp_cksum = 0;
|
268 |
|
|
icmp->icmp_seq = seq;
|
269 |
|
|
icmp->icmp_id = 0x1234;
|
270 |
|
|
// Set up ping data
|
271 |
|
|
tp = (cyg_tick_count_t *)&icmp->icmp_data;
|
272 |
|
|
*tp++ = cyg_current_time();
|
273 |
|
|
dp = (long *)tp;
|
274 |
|
|
for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
|
275 |
|
|
*dp++ = i;
|
276 |
|
|
}
|
277 |
|
|
// Add checksum
|
278 |
|
|
icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
|
279 |
|
|
// Send it off
|
280 |
|
|
if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
|
281 |
|
|
TNR_OFF();
|
282 |
|
|
perror("sendto");
|
283 |
|
|
continue;
|
284 |
|
|
}
|
285 |
|
|
// Wait for a response
|
286 |
|
|
fromlen = sizeof(from);
|
287 |
|
|
len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
|
288 |
|
|
TNR_OFF();
|
289 |
|
|
if (len < 0) {
|
290 |
|
|
perror("recvfrom");
|
291 |
|
|
icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
|
292 |
|
|
} else {
|
293 |
|
|
if (show_icmp(pkt2, len, &from, host)) {
|
294 |
|
|
ok_recv++;
|
295 |
|
|
} else {
|
296 |
|
|
bogus_recv++;
|
297 |
|
|
}
|
298 |
|
|
}
|
299 |
|
|
}
|
300 |
|
|
TNR_OFF();
|
301 |
|
|
diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
static void
|
305 |
|
|
ping_test(struct bootp *bp)
|
306 |
|
|
{
|
307 |
|
|
struct protoent *p;
|
308 |
|
|
struct timeval tv;
|
309 |
|
|
struct sockaddr_in host;
|
310 |
|
|
int s;
|
311 |
|
|
|
312 |
|
|
if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
|
313 |
|
|
perror("getprotobyname");
|
314 |
|
|
return;
|
315 |
|
|
}
|
316 |
|
|
s = socket(AF_INET, SOCK_RAW, p->p_proto);
|
317 |
|
|
if (s < 0) {
|
318 |
|
|
perror("socket");
|
319 |
|
|
return;
|
320 |
|
|
}
|
321 |
|
|
tv.tv_sec = 1;
|
322 |
|
|
tv.tv_usec = 0;
|
323 |
|
|
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
324 |
|
|
// Set up host address
|
325 |
|
|
host.sin_family = AF_INET;
|
326 |
|
|
host.sin_len = sizeof(host);
|
327 |
|
|
host.sin_addr = bp->bp_siaddr;
|
328 |
|
|
host.sin_port = 0;
|
329 |
|
|
ping_host(s, &host);
|
330 |
|
|
// Now try a bogus host
|
331 |
|
|
// (also, map 76 <-> 191 so that if a pair runs they ping each other)
|
332 |
|
|
host.sin_addr = bp->bp_yiaddr; // *my* address.
|
333 |
|
|
// host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) ^ 0xf3);
|
334 |
|
|
host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) ^ 2);
|
335 |
|
|
ping_host(s, &host);
|
336 |
|
|
close(s);
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
static void
|
340 |
|
|
ping_test_loopback( int lo )
|
341 |
|
|
{
|
342 |
|
|
struct protoent *p;
|
343 |
|
|
struct timeval tv;
|
344 |
|
|
struct sockaddr_in host;
|
345 |
|
|
int s;
|
346 |
|
|
|
347 |
|
|
if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
|
348 |
|
|
perror("getprotobyname");
|
349 |
|
|
return;
|
350 |
|
|
}
|
351 |
|
|
s = socket(AF_INET, SOCK_RAW, p->p_proto);
|
352 |
|
|
if (s < 0) {
|
353 |
|
|
perror("socket");
|
354 |
|
|
return;
|
355 |
|
|
}
|
356 |
|
|
tv.tv_sec = 1;
|
357 |
|
|
tv.tv_usec = 0;
|
358 |
|
|
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
359 |
|
|
// Set up host address
|
360 |
|
|
host.sin_family = AF_INET;
|
361 |
|
|
host.sin_len = sizeof(host);
|
362 |
|
|
host.sin_addr.s_addr = htonl(INADDR_LOOPBACK + (0x100 * lo));
|
363 |
|
|
host.sin_port = 0;
|
364 |
|
|
ping_host(s, &host);
|
365 |
|
|
// Now try a bogus host
|
366 |
|
|
host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
|
367 |
|
|
ping_host(s, &host);
|
368 |
|
|
close(s);
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
#ifdef CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
|
372 |
|
|
static void
|
373 |
|
|
interface_promisc(const char *intf)
|
374 |
|
|
{
|
375 |
|
|
struct ifreq ifr;
|
376 |
|
|
int s;
|
377 |
|
|
|
378 |
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
379 |
|
|
if (s < 0) {
|
380 |
|
|
perror("socket");
|
381 |
|
|
return;
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
strcpy(ifr.ifr_name, intf);
|
385 |
|
|
ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_PROMISC;
|
386 |
|
|
if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
|
387 |
|
|
perror("SIOCSIFFLAGS");
|
388 |
|
|
}
|
389 |
|
|
close(s);
|
390 |
|
|
}
|
391 |
|
|
#endif // CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
|
392 |
|
|
|
393 |
|
|
void snmp_do_reinit( void )
|
394 |
|
|
{
|
395 |
|
|
diag_printf( "SNMP re-init function\n" );
|
396 |
|
|
#ifdef CYGSEM_SNMPAGENT_TESTS_SNMPv3
|
397 |
|
|
// Initialisation for USM is now invoked from mib_module_inits.h
|
398 |
|
|
//init_usmUser(); /* MIBs to support SNMPv3 */
|
399 |
|
|
//init_usmStats();
|
400 |
|
|
//init_snmpEngine();
|
401 |
|
|
usm_parse_create_usmUser(NULL, "root MD5 md5passwd DES DESpasswd");
|
402 |
|
|
#endif // CYGSEM_SNMPAGENT_TESTS_SNMPv3
|
403 |
|
|
}
|
404 |
|
|
|
405 |
|
|
int create_snmpd_conf (void) {
|
406 |
|
|
#ifdef CYGSEM_SNMPAGENT_TESTS_SNMPv3
|
407 |
|
|
#ifdef CYGPKG_SNMPLIB_FILESYSTEM_SUPPORT
|
408 |
|
|
#ifdef CYGPKG_FS_RAM
|
409 |
|
|
int c;
|
410 |
|
|
FILE *fd;
|
411 |
|
|
|
412 |
|
|
diag_printf ("\nStarting creation of snmpd.conf\n");
|
413 |
|
|
|
414 |
|
|
/* Mount RAM-FS */
|
415 |
|
|
if (mount ("", "/", "ramfs") != 0) {
|
416 |
|
|
diag_printf ("File system mount failed; errno=%d \n", errno);
|
417 |
|
|
return -1;
|
418 |
|
|
}
|
419 |
|
|
|
420 |
|
|
if (mkdir ("/etc", 0) != 0) {
|
421 |
|
|
diag_printf ("mkdir (etc) failed; errno=%d\n", errno);
|
422 |
|
|
return -1;
|
423 |
|
|
}
|
424 |
|
|
|
425 |
|
|
if (chdir ("/etc") != 0) {
|
426 |
|
|
diag_printf ("... Change-dir (etc) failed; errno=%d\n", errno);
|
427 |
|
|
return -1;
|
428 |
|
|
} else {
|
429 |
|
|
diag_printf ("chdir-etc done\n");
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
if (mkdir ("snmp", 0) != 0) {
|
433 |
|
|
diag_printf ("mkdir failed (snmp); errno=%d\n", errno);
|
434 |
|
|
return -1;
|
435 |
|
|
} else {
|
436 |
|
|
diag_printf ("mkdir-snmp done\n");
|
437 |
|
|
}
|
438 |
|
|
|
439 |
|
|
if (chdir ("snmp") != 0) {
|
440 |
|
|
diag_printf ("... Change-dir (snmp) failed; errno=%d\n", errno);
|
441 |
|
|
return -1;
|
442 |
|
|
} else {
|
443 |
|
|
diag_printf ("... Change-dir (snmp) done \n");
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
/* Open File & Write to it */
|
447 |
|
|
if ((fd = fopen( "snmpd.conf", "w" )) == NULL) {
|
448 |
|
|
diag_printf ("fopen failed\n");
|
449 |
|
|
return -1;
|
450 |
|
|
}
|
451 |
|
|
|
452 |
|
|
fprintf (fd, "# sec.name source community\n");
|
453 |
|
|
fprintf (fd, "com2sec public default crux\n");
|
454 |
|
|
fprintf (fd, "com2sec root default crux\n");
|
455 |
|
|
fprintf (fd, "# sec.model sec.name\n");
|
456 |
|
|
fprintf (fd, "group public v1 public\n");
|
457 |
|
|
fprintf (fd, "group public v2c public\n");
|
458 |
|
|
fprintf (fd, "group public usm root\n");
|
459 |
|
|
fprintf (fd, "view all included .1\n");
|
460 |
|
|
fprintf (fd, "access public \"\" any noauth exact all none none\n");
|
461 |
|
|
fprintf (fd, "\n\n");
|
462 |
|
|
|
463 |
|
|
if (fclose (fd)) {
|
464 |
|
|
diag_printf ("fclose failed\n");
|
465 |
|
|
return -1;
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
/* Read Back */
|
469 |
|
|
fd = fopen( "/etc/snmp/snmpd.conf", "r" );
|
470 |
|
|
if (fd == NULL) {
|
471 |
|
|
diag_printf ("fopen failed\n");
|
472 |
|
|
return -1;
|
473 |
|
|
}
|
474 |
|
|
|
475 |
|
|
while ((c=fgetc (fd)) != EOF) {
|
476 |
|
|
diag_printf ("%c", c);
|
477 |
|
|
}
|
478 |
|
|
|
479 |
|
|
if (fclose (fd)) {
|
480 |
|
|
diag_printf ("fclose failed\n");
|
481 |
|
|
return -1;
|
482 |
|
|
}
|
483 |
|
|
#endif
|
484 |
|
|
#endif
|
485 |
|
|
#endif
|
486 |
|
|
return 0;
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
void
|
490 |
|
|
net_test(cyg_addrword_t p)
|
491 |
|
|
{
|
492 |
|
|
int i = CYGNUM_SNMPAGENT_TESTS_ITERATIONS;
|
493 |
|
|
int ieth0_up = 0, ieth1_up = 0;
|
494 |
|
|
int j;
|
495 |
|
|
|
496 |
|
|
diag_printf("Start PING test\n");
|
497 |
|
|
TNR_INIT();
|
498 |
|
|
init_all_network_interfaces();
|
499 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
500 |
|
|
ieth0_up = eth0_up;
|
501 |
|
|
#endif
|
502 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
503 |
|
|
ieth1_up = eth1_up;
|
504 |
|
|
#endif
|
505 |
|
|
#ifdef CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
|
506 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
507 |
|
|
if (eth0_up)
|
508 |
|
|
interface_promisc("eth0");
|
509 |
|
|
#endif
|
510 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
511 |
|
|
if (eth1_up)
|
512 |
|
|
interface_promisc("eth1");
|
513 |
|
|
#endif
|
514 |
|
|
#endif // CYGSEM_SNMPAGENT_TESTS_PROMISCUOUS
|
515 |
|
|
{
|
516 |
|
|
extern void cyg_net_snmp_init(void);
|
517 |
|
|
extern void (*snmpd_reinit_function)( void );
|
518 |
|
|
|
519 |
|
|
snmpd_reinit_function = snmp_do_reinit;
|
520 |
|
|
|
521 |
|
|
if (create_snmpd_conf ()) {
|
522 |
|
|
CYG_TEST_FAIL_EXIT("create_snmpd_conf() error\n");
|
523 |
|
|
}
|
524 |
|
|
cyg_net_snmp_init();
|
525 |
|
|
}
|
526 |
|
|
do {
|
527 |
|
|
TNR_ON();
|
528 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
529 |
|
|
if (eth0_up) {
|
530 |
|
|
ping_test(ð0_bootp_data);
|
531 |
|
|
cyg_thread_delay(500);
|
532 |
|
|
}
|
533 |
|
|
#endif
|
534 |
|
|
#if NLOOP > 0
|
535 |
|
|
for ( j = 0; j < NLOOP; j++ ) {
|
536 |
|
|
ping_test_loopback( j );
|
537 |
|
|
cyg_thread_delay(500);
|
538 |
|
|
}
|
539 |
|
|
#endif
|
540 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
541 |
|
|
if (eth1_up) {
|
542 |
|
|
ping_test(ð1_bootp_data);
|
543 |
|
|
cyg_thread_delay(500);
|
544 |
|
|
}
|
545 |
|
|
#endif
|
546 |
|
|
TNR_OFF();
|
547 |
|
|
TNR_PRINT_ACTIVITY();
|
548 |
|
|
|
549 |
|
|
// If an interface has gone down eg. due to DHCP timing out,
|
550 |
|
|
// re-initialize everything:
|
551 |
|
|
if (0
|
552 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
553 |
|
|
|| ieth0_up != eth0_up
|
554 |
|
|
#endif
|
555 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
556 |
|
|
|| ieth1_up != eth1_up
|
557 |
|
|
#endif
|
558 |
|
|
) {
|
559 |
|
|
diag_printf( "Re-initializing the world: eth0 %d/%d eth1 %d/%d!\n",
|
560 |
|
|
ieth0_up, 0
|
561 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
562 |
|
|
| eth0_up
|
563 |
|
|
#endif
|
564 |
|
|
, ieth1_up, 0
|
565 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
566 |
|
|
| eth1_up
|
567 |
|
|
#endif
|
568 |
|
|
);
|
569 |
|
|
init_all_network_interfaces();
|
570 |
|
|
for ( j = 0; j < CYGPKG_NET_NLOOP; j++ )
|
571 |
|
|
init_loopback_interface( j );
|
572 |
|
|
|
573 |
|
|
diag_printf( "Re-initialized the world: eth0 %d/%d eth1 %d/%d!\n",
|
574 |
|
|
ieth0_up, 0
|
575 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
576 |
|
|
| eth0_up
|
577 |
|
|
#endif
|
578 |
|
|
, ieth1_up, 0
|
579 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
580 |
|
|
| eth1_up
|
581 |
|
|
#endif
|
582 |
|
|
);
|
583 |
|
|
}
|
584 |
|
|
} while ( i-- > 0 );
|
585 |
|
|
CYG_TEST_PASS_FINISH( "Done pinging while SNMP looks on" );
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
void
|
589 |
|
|
cyg_start(void)
|
590 |
|
|
{
|
591 |
|
|
// Create a main thread, so we can run the scheduler and have time 'pass'
|
592 |
|
|
cyg_thread_create(10, // Priority - just a number
|
593 |
|
|
net_test, // entry
|
594 |
|
|
0, // entry parameter
|
595 |
|
|
"Network test", // Name
|
596 |
|
|
&stack[0], // Stack
|
597 |
|
|
STACK_SIZE, // Size
|
598 |
|
|
&thread_handle, // Handle
|
599 |
|
|
&thread_data // Thread data structure
|
600 |
|
|
);
|
601 |
|
|
cyg_thread_resume(thread_handle); // Start it
|
602 |
|
|
cyg_scheduler_start();
|
603 |
|
|
}
|
604 |
|
|
|
605 |
|
|
// EOF snmpping.c
|