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
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"
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
7684long g_usec_timeout ;
7785
86+ snmp_device_id_t * snmp_device_table = NULL ;
87+
7888/* dynamic link library stuff */
7989static lt_dlhandle dl_handle = NULL ;
8090static const char * dl_error = NULL ;
@@ -106,6 +116,16 @@ static oid * (*nut_usmDESPrivProtocol);
106116/* return 0 on error */
107117int 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
0 commit comments