Skip to content

Commit 931ea7d

Browse files
committed
nut-scanner: split the C and H for nutscan-snmp (now header is in Git, and C with structure is now newly generated by script)
1 parent cd843dd commit 931ea7d

6 files changed

Lines changed: 116 additions & 42 deletions

File tree

tools/nut-scanner/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/nut-scanner
2-
/nutscan-snmp.h
2+
/nutscan-snmp.c
33
/nutscan-usb.h

tools/nut-scanner/Makefile.am

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
BUILT_SOURCES = nutscan-usb.h nutscan-snmp.h
1+
BUILT_SOURCES = nutscan-usb.h nutscan-snmp.c
22

3-
nutscan-usb.h nutscan-snmp.h:
3+
nutscan-usb.h nutscan-snmp.c:
44
cd ..; $(MAKE) $(AM_MAKEFLAGS) nut-scanner-deps
55

66
# Only build nut-scanner, and its library, if libltdl was found (required!)
77
if WITH_LIBLTDL
88
bin_PROGRAMS = nut-scanner
99
lib_LTLIBRARIES = libnutscan.la
1010
endif
11-
libnutscan_la_SOURCES = scan_nut.c scan_ipmi.c \
11+
libnutscan_la_SOURCES = scan_nut.c scan_ipmi.c nutscan-snmp.c \
1212
nutscan-device.c nutscan-ip.c nutscan-display.c \
13-
nutscan-init.c scan_usb.c scan_snmp.c scan_xml_http.c \
13+
nutscan-init.c scan_usb.c scan_snmp.c scan_xml_http.c \
1414
scan_avahi.c scan_eaton_serial.c nutscan-serial.c \
1515
../../drivers/serial.c \
1616
../../drivers/bcmxcp_ser.c \
@@ -43,13 +43,14 @@ if WITH_IPMI
4343
libnutscan_la_CFLAGS += $(LIBIPMI_CFLAGS)
4444
endif
4545

46-
dist_noinst_HEADERS = nutscan-usb.h nutscan-snmp.h
46+
# C is not a header, but there is no dist_noinst_SOURCES
47+
dist_noinst_HEADERS = nutscan-usb.h nutscan-snmp.c
4748

4849
if WITH_DEV
49-
include_HEADERS = nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h
50+
include_HEADERS = nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h nutscan-serial.h nutscan-snmp.h
5051
else
51-
dist_noinst_HEADERS += nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h nutscan-serial.h
52+
dist_noinst_HEADERS += nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h nutscan-serial.h nutscan-snmp.h
5253
endif
5354

54-
CLEANFILES = nutscan-usb.h nutscan-snmp.h
55+
CLEANFILES = nutscan-usb.h nutscan-snmp.c
5556

tools/nut-scanner/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ linkman:nut-scanner[8] is available to discover supported NUT devices
88
(USB, SNMP, Eaton XML/HTTP and IPMI) and NUT servers (using Avahi or the
99
classic connection method).
1010

11-
This tool actually use a library, called *libnutscan*, to perform actual
11+
This tool actually uses a library, called *libnutscan*, to perform actual
1212
processing.
1313

1414

tools/nut-scanner/nutscan-snmp.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* nutscan-snmp
2+
* Copyright (C) 2011 - Frederic Bohe <FredericBohe@Eaton.com>
3+
* Copyright (C) 2016 - Arnaud Quette <ArnaudQuette@Eaton.com>
4+
* Copyright (C) 2016 - Jim Klimov <EvgenyKlimov@Eaton.com>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
*/
20+
21+
#ifndef DEVSCAN_SNMP_H
22+
#define DEVSCAN_SNMP_H
23+
24+
typedef struct {
25+
char * oid;
26+
char * mib;
27+
char * sysoid;
28+
} snmp_device_id_t;
29+
30+
31+
#endif /* DEVSCAN_SNMP_H */
32+
33+
/* SNMP IDs device table, excerpt generated from our available MIBs */
34+
/* Note: This is commented away with ifdefs, so the consumers who only need
35+
* the structure definition are not burdened with an external reference to
36+
* structure instances they would not need.
37+
*/
38+
39+
#if WANT_DEVSCAN_SNMP_BUILTIN == 1
40+
# ifndef DEVSCAN_SNMP_BUILTIN
41+
# define DEVSCAN_SNMP_BUILTIN
42+
/* Can use a copy of the structure that was pre-compiled into the binary */
43+
extern snmp_device_id_t *snmp_device_table_builtin;
44+
# endif /* DEVSCAN_SNMP_BUILTIN */
45+
#endif /* WANT_DEVSCAN_SNMP_BUILTIN */

tools/nut-scanner/scan_snmp.c

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2011 - EATON
2+
* Copyright (C) 2011-2016 by EATON
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
1919
/*! \file scan_snmp.c
2020
\brief detect NUT supported SNMP devices
2121
\author Frederic Bohe <fredericbohe@eaton.com>
22+
\author Jim Klimov <EvgenyKlimov@Eaton.com>
2223
*/
2324

2425
#include "common.h"
@@ -58,6 +59,13 @@
5859
#ifdef HAVE_PTHREAD
5960
#include <pthread.h>
6061
#endif
62+
63+
// Cause the header to also declare the external reference to pre-generated
64+
// compilable structure with the subset of MIB mappings needed by nut-scanner
65+
#ifndef WANT_DEVSCAN_SNMP_BUILTIN
66+
#define WANT_DEVSCAN_SNMP_BUILTIN 1
67+
#endif
68+
6169
#include "nutscan-snmp.h"
6270

6371
/* Address API change */
@@ -75,6 +83,8 @@ static pthread_mutex_t dev_mutex;
7583
#endif
7684
long g_usec_timeout ;
7785

86+
snmp_device_id_t *snmp_device_table = NULL;
87+
7888
/* dynamic link library stuff */
7989
static lt_dlhandle dl_handle = NULL;
8090
static const char *dl_error = NULL;
@@ -106,6 +116,16 @@ static oid * (*nut_usmDESPrivProtocol);
106116
/* return 0 on error */
107117
int nutscan_load_snmp_library(const char *libname_path)
108118
{
119+
#ifdef DEVSCAN_SNMP_BUILTIN
120+
if (snmp_device_table == NULL)
121+
snmp_device_table = snmp_device_table_builtin;
122+
#endif
123+
124+
if (snmp_device_table == NULL) {
125+
fprintf(stderr, "SNMP mapping table not found. SNMP search disabled.\n");
126+
return 0;
127+
}
128+
109129
if( dl_handle != NULL ) {
110130
/* if previous init failed */
111131
if( dl_handle == (void *)1 ) {
@@ -432,31 +452,31 @@ static int init_session(struct snmp_session * snmp_sess, nutscan_snmp_t * sec)
432452
}
433453

434454
/* Process mandatory fields, based on the security level */
435-
switch (snmp_sess->securityLevel) {
436-
case SNMP_SEC_LEVEL_AUTHNOPRIV:
437-
if (sec->authPassword == NULL) {
438-
fprintf(stderr,
455+
switch (snmp_sess->securityLevel) {
456+
case SNMP_SEC_LEVEL_AUTHNOPRIV:
457+
if (sec->authPassword == NULL) {
458+
fprintf(stderr,
439459
"authPassword is required for SNMPv3 in %s mode\n",
440460
sec->secLevel);
441461
return 0;
442462
}
443463
break;
444-
case SNMP_SEC_LEVEL_AUTHPRIV:
445-
if ((sec->authPassword == NULL) ||
464+
case SNMP_SEC_LEVEL_AUTHPRIV:
465+
if ((sec->authPassword == NULL) ||
446466
(sec->privPassword == NULL)) {
447-
fprintf(stderr,
467+
fprintf(stderr,
448468
"authPassword and privPassword are required for SNMPv3 in %s mode\n",
449469
sec->secLevel);
450470
return 0;
451471
}
452472
break;
453-
default:
454-
/* nothing else needed */
455-
break;
456-
}
473+
default:
474+
/* nothing else needed */
475+
break;
476+
}
457477

458-
/* Process authentication protocol and key */
459-
snmp_sess->securityAuthKeyLen = USM_AUTH_KU_LEN;
478+
/* Process authentication protocol and key */
479+
snmp_sess->securityAuthKeyLen = USM_AUTH_KU_LEN;
460480

461481
/* default to MD5 */
462482
snmp_sess->securityAuthProto = (*nut_usmHMACMD5AuthProtocol);
@@ -549,8 +569,8 @@ static void * try_SysOID(void * arg)
549569
struct snmp_session snmp_sess;
550570
void * handle;
551571
struct snmp_pdu *pdu, *response = NULL, *resp = NULL;
552-
oid name[MAX_OID_LEN];
553-
size_t name_len = MAX_OID_LEN;
572+
oid name[MAX_OID_LEN];
573+
size_t name_len = MAX_OID_LEN;
554574
nutscan_snmp_t * sec = (nutscan_snmp_t *)arg;
555575
int index = 0;
556576
int sysoid_found = 0;
@@ -667,10 +687,9 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip
667687
pthread_mutex_init(&dev_mutex,NULL);
668688
#endif
669689

670-
if( !nutscan_avail_snmp ) {
671-
return NULL;
672-
}
673-
690+
if( !nutscan_avail_snmp ) {
691+
return NULL;
692+
}
674693

675694
g_usec_timeout = usec_timeout;
676695

tools/nut-snmpinfo.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# Copyright (C) 2011 - Frederic Bohe <FredericBohe@Eaton.com>
33
# Copyright (C) 2016 - Arnaud Quette <ArnaudQuette@Eaton.com>
4+
# Copyright (C) 2016 - Jim Klimov <EvgenyKlimov@Eaton.com>
45
#
56
# This program is free software; you can redistribute it and/or modify
67
# it under the terms of the GNU General Public License as published by
@@ -22,7 +23,7 @@
2223
import re
2324
import sys
2425

25-
output_file_name="./nut-scanner/nutscan-snmp.h"
26+
output_file_name="./nut-scanner/nutscan-snmp.c"
2627
output_file = open(output_file_name,'w')
2728

2829
#expand #define constant
@@ -49,9 +50,10 @@ def expand_define(filename,constant):
4950
return ret_line
5051

5152

52-
output_file.write( "/* nutscan-snmp\n" )
53+
output_file.write( "/* nutscan-snmp.c - fully generated during build of NUT\n" )
5354
output_file.write( " * Copyright (C) 2011 - Frederic Bohe <FredericBohe@Eaton.com>\n" )
5455
output_file.write( " * Copyright (C) 2016 - Arnaud Quette <ArnaudQuette@Eaton.com>\n" )
56+
output_file.write( " * Copyright (C) 2016 - Jim Klimov <EvgenyKlimov@Eaton.com>\n" )
5557
output_file.write( " *\n" )
5658
output_file.write( " * This program is free software; you can redistribute it and/or modify\n" )
5759
output_file.write( " * it under the terms of the GNU General Public License as published by\n" )
@@ -68,17 +70,24 @@ def expand_define(filename,constant):
6870
output_file.write( " * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n" )
6971
output_file.write( " */\n" )
7072
output_file.write( "\n" )
71-
output_file.write( "#ifndef DEVSCAN_SNMP_H\n" )
72-
output_file.write( "#define DEVSCAN_SNMP_H\n" )
73+
output_file.write( "#include \"nutscan-snmp.h\"\n" )
7374
output_file.write( "\n" )
74-
output_file.write( "typedef struct {\n" )
75-
output_file.write( " char * oid;\n" )
76-
output_file.write( " char * mib;\n" )
77-
output_file.write( " char * sysoid;\n" )
78-
output_file.write( "} snmp_device_id_t;\n" )
75+
output_file.write( "#ifndef NULL\n" )
76+
output_file.write( "#define NULL (void*)0ULL\n" )
77+
output_file.write( "#endif\n" )
7978
output_file.write( "\n" )
80-
output_file.write( "/* SNMP IDs device table */\n" )
81-
output_file.write( "static snmp_device_id_t snmp_device_table[] = {\n" )
79+
output_file.write( "// marker to tell humans and GCC that the unused parameter is there for some\n" )
80+
output_file.write( "// reason (i.e. API compatibility) and compiler should not warn if not used\n" )
81+
output_file.write( "#ifndef UNUSED_PARAM\n" )
82+
output_file.write( "# ifdef __GNUC__\n" )
83+
output_file.write( "# define UNUSED_PARAM __attribute__ ((__unused__))\n" )
84+
output_file.write( "# else\n" )
85+
output_file.write( "# define UNUSED_PARAM\n" )
86+
output_file.write( "# endif\n" )
87+
output_file.write( "#endif\n" )
88+
output_file.write( "\n" )
89+
output_file.write( "/* SNMP IDs device table, not used in this file itself - silence the warning if we can */\n" )
90+
output_file.write( "snmp_device_id_t snmp_device_table_builtin[] UNUSED_PARAM = {\n" )
8291

8392
for filename in glob.glob('../drivers/*-mib.c'):
8493
list_of_line = open(filename,'r').read().split(';')
@@ -143,4 +152,4 @@ def expand_define(filename,constant):
143152
output_file.write( " /* Terminating entry */\n" )
144153
output_file.write( " { NULL, NULL, NULL}\n" )
145154
output_file.write( "};\n" )
146-
output_file.write( "#endif /* DEVSCAN_SNMP_H */\n" )
155+
output_file.write( "\n" )

0 commit comments

Comments
 (0)