-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
Showing
6 changed files
with
116 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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" | ||
|
@@ -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 */ | ||
|
@@ -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; | ||
|
@@ -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 ) { | ||
|
@@ -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); | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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" ) | ||
|
@@ -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(';') | ||
|
@@ -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" ) |