Skip to content

Commit

Permalink
nut-scanner: split the C and H for nutscan-snmp (now header is in Git…
Browse files Browse the repository at this point in the history
…, and C with structure is now newly generated by script)
  • Loading branch information
jimklimov committed May 16, 2016
1 parent cd843dd commit 931ea7d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 42 deletions.
2 changes: 1 addition & 1 deletion tools/nut-scanner/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/nut-scanner
/nutscan-snmp.h
/nutscan-snmp.c
/nutscan-usb.h
17 changes: 9 additions & 8 deletions tools/nut-scanner/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
BUILT_SOURCES = nutscan-usb.h nutscan-snmp.h
BUILT_SOURCES = nutscan-usb.h nutscan-snmp.c

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

# Only build nut-scanner, and its library, if libltdl was found (required!)
if WITH_LIBLTDL
bin_PROGRAMS = nut-scanner
lib_LTLIBRARIES = libnutscan.la
endif
libnutscan_la_SOURCES = scan_nut.c scan_ipmi.c \
libnutscan_la_SOURCES = scan_nut.c scan_ipmi.c nutscan-snmp.c \
nutscan-device.c nutscan-ip.c nutscan-display.c \
nutscan-init.c scan_usb.c scan_snmp.c scan_xml_http.c \
nutscan-init.c scan_usb.c scan_snmp.c scan_xml_http.c \
scan_avahi.c scan_eaton_serial.c nutscan-serial.c \
../../drivers/serial.c \
../../drivers/bcmxcp_ser.c \
Expand Down Expand Up @@ -43,13 +43,14 @@ if WITH_IPMI
libnutscan_la_CFLAGS += $(LIBIPMI_CFLAGS)
endif

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

if WITH_DEV
include_HEADERS = nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h
include_HEADERS = nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h nutscan-serial.h nutscan-snmp.h
else
dist_noinst_HEADERS += nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h nutscan-serial.h
dist_noinst_HEADERS += nut-scan.h nutscan-device.h nutscan-ip.h nutscan-init.h nutscan-serial.h nutscan-snmp.h
endif

CLEANFILES = nutscan-usb.h nutscan-snmp.h
CLEANFILES = nutscan-usb.h nutscan-snmp.c

2 changes: 1 addition & 1 deletion tools/nut-scanner/README
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linkman:nut-scanner[8] is available to discover supported NUT devices
(USB, SNMP, Eaton XML/HTTP and IPMI) and NUT servers (using Avahi or the
classic connection method).

This tool actually use a library, called *libnutscan*, to perform actual
This tool actually uses a library, called *libnutscan*, to perform actual
processing.


Expand Down
45 changes: 45 additions & 0 deletions tools/nut-scanner/nutscan-snmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* nutscan-snmp
* Copyright (C) 2011 - Frederic Bohe <[email protected]>
* Copyright (C) 2016 - Arnaud Quette <[email protected]>
* Copyright (C) 2016 - Jim Klimov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef DEVSCAN_SNMP_H
#define DEVSCAN_SNMP_H

typedef struct {
char * oid;
char * mib;
char * sysoid;
} snmp_device_id_t;


#endif /* DEVSCAN_SNMP_H */

/* SNMP IDs device table, excerpt generated from our available MIBs */
/* Note: This is commented away with ifdefs, so the consumers who only need
* the structure definition are not burdened with an external reference to
* structure instances they would not need.
*/

#if WANT_DEVSCAN_SNMP_BUILTIN == 1
# ifndef DEVSCAN_SNMP_BUILTIN
# define DEVSCAN_SNMP_BUILTIN
/* Can use a copy of the structure that was pre-compiled into the binary */
extern snmp_device_id_t *snmp_device_table_builtin;
# endif /* DEVSCAN_SNMP_BUILTIN */
#endif /* WANT_DEVSCAN_SNMP_BUILTIN */
59 changes: 39 additions & 20 deletions tools/nut-scanner/scan_snmp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 - EATON
* Copyright (C) 2011-2016 by EATON
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,6 +19,7 @@
/*! \file scan_snmp.c
\brief detect NUT supported SNMP devices
\author Frederic Bohe <[email protected]>
\author Jim Klimov <[email protected]>
*/

#include "common.h"
Expand Down Expand Up @@ -58,6 +59,13 @@
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif

// Cause the header to also declare the external reference to pre-generated
// compilable structure with the subset of MIB mappings needed by nut-scanner
#ifndef WANT_DEVSCAN_SNMP_BUILTIN
#define WANT_DEVSCAN_SNMP_BUILTIN 1
#endif

#include "nutscan-snmp.h"

/* Address API change */
Expand All @@ -75,6 +83,8 @@ static pthread_mutex_t dev_mutex;
#endif
long g_usec_timeout ;

snmp_device_id_t *snmp_device_table = NULL;

/* dynamic link library stuff */
static lt_dlhandle dl_handle = NULL;
static const char *dl_error = NULL;
Expand Down Expand Up @@ -106,6 +116,16 @@ static oid * (*nut_usmDESPrivProtocol);
/* return 0 on error */
int nutscan_load_snmp_library(const char *libname_path)
{
#ifdef DEVSCAN_SNMP_BUILTIN
if (snmp_device_table == NULL)
snmp_device_table = snmp_device_table_builtin;
#endif

if (snmp_device_table == NULL) {
fprintf(stderr, "SNMP mapping table not found. SNMP search disabled.\n");
return 0;
}

if( dl_handle != NULL ) {
/* if previous init failed */
if( dl_handle == (void *)1 ) {
Expand Down Expand Up @@ -432,31 +452,31 @@ static int init_session(struct snmp_session * snmp_sess, nutscan_snmp_t * sec)
}

/* Process mandatory fields, based on the security level */
switch (snmp_sess->securityLevel) {
case SNMP_SEC_LEVEL_AUTHNOPRIV:
if (sec->authPassword == NULL) {
fprintf(stderr,
switch (snmp_sess->securityLevel) {
case SNMP_SEC_LEVEL_AUTHNOPRIV:
if (sec->authPassword == NULL) {
fprintf(stderr,
"authPassword is required for SNMPv3 in %s mode\n",
sec->secLevel);
return 0;
}
break;
case SNMP_SEC_LEVEL_AUTHPRIV:
if ((sec->authPassword == NULL) ||
case SNMP_SEC_LEVEL_AUTHPRIV:
if ((sec->authPassword == NULL) ||
(sec->privPassword == NULL)) {
fprintf(stderr,
fprintf(stderr,
"authPassword and privPassword are required for SNMPv3 in %s mode\n",
sec->secLevel);
return 0;
}
break;
default:
/* nothing else needed */
break;
}
default:
/* nothing else needed */
break;
}

/* Process authentication protocol and key */
snmp_sess->securityAuthKeyLen = USM_AUTH_KU_LEN;
/* Process authentication protocol and key */
snmp_sess->securityAuthKeyLen = USM_AUTH_KU_LEN;

/* default to MD5 */
snmp_sess->securityAuthProto = (*nut_usmHMACMD5AuthProtocol);
Expand Down Expand Up @@ -549,8 +569,8 @@ static void * try_SysOID(void * arg)
struct snmp_session snmp_sess;
void * handle;
struct snmp_pdu *pdu, *response = NULL, *resp = NULL;
oid name[MAX_OID_LEN];
size_t name_len = MAX_OID_LEN;
oid name[MAX_OID_LEN];
size_t name_len = MAX_OID_LEN;
nutscan_snmp_t * sec = (nutscan_snmp_t *)arg;
int index = 0;
int sysoid_found = 0;
Expand Down Expand Up @@ -667,10 +687,9 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip
pthread_mutex_init(&dev_mutex,NULL);
#endif

if( !nutscan_avail_snmp ) {
return NULL;
}

if( !nutscan_avail_snmp ) {
return NULL;
}

g_usec_timeout = usec_timeout;

Expand Down
33 changes: 21 additions & 12 deletions tools/nut-snmpinfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# Copyright (C) 2011 - Frederic Bohe <[email protected]>
# Copyright (C) 2016 - Arnaud Quette <[email protected]>
# Copyright (C) 2016 - Jim Klimov <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -22,7 +23,7 @@
import re
import sys

output_file_name="./nut-scanner/nutscan-snmp.h"
output_file_name="./nut-scanner/nutscan-snmp.c"
output_file = open(output_file_name,'w')

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


output_file.write( "/* nutscan-snmp\n" )
output_file.write( "/* nutscan-snmp.c - fully generated during build of NUT\n" )
output_file.write( " * Copyright (C) 2011 - Frederic Bohe <[email protected]>\n" )
output_file.write( " * Copyright (C) 2016 - Arnaud Quette <[email protected]>\n" )
output_file.write( " * Copyright (C) 2016 - Jim Klimov <[email protected]>\n" )
output_file.write( " *\n" )
output_file.write( " * This program is free software; you can redistribute it and/or modify\n" )
output_file.write( " * it under the terms of the GNU General Public License as published by\n" )
Expand All @@ -68,17 +70,24 @@ def expand_define(filename,constant):
output_file.write( " * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n" )
output_file.write( " */\n" )
output_file.write( "\n" )
output_file.write( "#ifndef DEVSCAN_SNMP_H\n" )
output_file.write( "#define DEVSCAN_SNMP_H\n" )
output_file.write( "#include \"nutscan-snmp.h\"\n" )
output_file.write( "\n" )
output_file.write( "typedef struct {\n" )
output_file.write( " char * oid;\n" )
output_file.write( " char * mib;\n" )
output_file.write( " char * sysoid;\n" )
output_file.write( "} snmp_device_id_t;\n" )
output_file.write( "#ifndef NULL\n" )
output_file.write( "#define NULL (void*)0ULL\n" )
output_file.write( "#endif\n" )
output_file.write( "\n" )
output_file.write( "/* SNMP IDs device table */\n" )
output_file.write( "static snmp_device_id_t snmp_device_table[] = {\n" )
output_file.write( "// marker to tell humans and GCC that the unused parameter is there for some\n" )
output_file.write( "// reason (i.e. API compatibility) and compiler should not warn if not used\n" )
output_file.write( "#ifndef UNUSED_PARAM\n" )
output_file.write( "# ifdef __GNUC__\n" )
output_file.write( "# define UNUSED_PARAM __attribute__ ((__unused__))\n" )
output_file.write( "# else\n" )
output_file.write( "# define UNUSED_PARAM\n" )
output_file.write( "# endif\n" )
output_file.write( "#endif\n" )
output_file.write( "\n" )
output_file.write( "/* SNMP IDs device table, not used in this file itself - silence the warning if we can */\n" )
output_file.write( "snmp_device_id_t snmp_device_table_builtin[] UNUSED_PARAM = {\n" )

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

0 comments on commit 931ea7d

Please sign in to comment.