"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/server/drivers/at/at.cpp" (23 Feb 2021, 8854 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "at.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.6.300_vs_3.7.95.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** Driver for Allied Telesis switches
    4 ** Copyright (C) 2003-2014 Victor Kirhenshtein
    5 **
    6 ** This program is free software; you can redistribute it and/or modify
    7 ** it under the terms of the GNU Lesser General Public License as published by
    8 ** the Free Software Foundation; either version 3 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 Lesser General Public License
   17 ** along with this program; if not, write to the Free Software
   18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   19 **
   20 ** File: at.cpp
   21 **/
   22 
   23 #include "at.h"
   24 #include <netxms-version.h>
   25 
   26 /**
   27  * Driver name
   28  */
   29 static TCHAR s_driverName[] = _T("AT");
   30 
   31 /**
   32  * Driver version
   33  */
   34 static TCHAR s_driverVersion[] = NETXMS_VERSION_STRING;
   35 
   36 /**
   37  * Get driver name
   38  */
   39 const TCHAR *AlliedTelesisDriver::getName()
   40 {
   41     return s_driverName;
   42 }
   43 
   44 /**
   45  * Get driver version
   46  */
   47 const TCHAR *AlliedTelesisDriver::getVersion()
   48 {
   49     return s_driverVersion;
   50 }
   51 
   52 /**
   53  * Check if given device can be potentially supported by driver
   54  *
   55  * @param oid Device OID
   56  */
   57 int AlliedTelesisDriver::isPotentialDevice(const TCHAR *oid)
   58 {
   59     return (_tcsncmp(oid, _T(".1.3.6.1.4.1.207.1"), 18) == 0) ? 127 : 0;
   60 }
   61 
   62 /**
   63  * Check if given device is supported by driver
   64  *
   65  * @param snmp SNMP transport
   66  * @param oid Device OID
   67  */
   68 bool AlliedTelesisDriver::isDeviceSupported(SNMP_Transport *snmp, const TCHAR *oid)
   69 {
   70    TCHAR buffer[256];
   71    return (SnmpGet(snmp->getSnmpVersion(), snmp, _T(".1.3.6.1.4.1.207.8.33.1.1.0"), NULL, 0, buffer, 256, 0) == SNMP_ERR_SUCCESS) ||
   72           (SnmpGet(snmp->getSnmpVersion(), snmp, _T(".1.3.6.1.4.1.207.1.4.167.81.1.3.0"), NULL, 0, buffer, 256, 0) == SNMP_ERR_SUCCESS);
   73 }
   74 
   75 /**
   76  * Do additional checks on the device required by driver.
   77  * Driver can set device's custom attributes and driver's data from within this method.
   78  * Driver is responsible for destroying previously created data object.
   79  *
   80  * @param snmp SNMP transport
   81  * @param node Node
   82  * @param driverData pointer to pointer to driver-specific data
   83  */
   84 void AlliedTelesisDriver::analyzeDevice(SNMP_Transport *snmp, const TCHAR *oid, NObject *node, DriverData **driverData)
   85 {
   86    TCHAR buffer[256];
   87    if (SnmpGet(snmp->getSnmpVersion(), snmp, _T(".1.3.6.1.4.1.207.1.4.167.81.1.3.0"), NULL, 0, buffer, 256, 0) == SNMP_ERR_SUCCESS)
   88    {
   89       node->setCustomAttribute(_T(".alliedTelesis.isGS950"), _T("true"), StateChange::IGNORE);
   90    }
   91 }
   92 
   93 /**
   94  * Get list of interfaces for given node
   95  *
   96  * @param snmp SNMP transport
   97  * @param node Node
   98  */
   99 InterfaceList *AlliedTelesisDriver::getInterfaces(SNMP_Transport *snmp, NObject *node, DriverData *driverData, int useAliases, bool useIfXTable)
  100 {
  101    InterfaceList *ifList = NetworkDeviceDriver::getInterfaces(snmp, node, driverData, useAliases, useIfXTable);
  102    if (ifList != NULL)
  103    {
  104       bool isGS950 = node->getCustomAttributeAsBoolean(_T(".alliedTelesis.isGS950"), false);
  105 
  106       // Find physical ports
  107       for(int i = 0; i < ifList->size(); i++)
  108       {
  109          InterfaceInfo *iface = ifList->get(i);
  110          if (isGS950)
  111          {
  112             // GS950 does not support atiIfExtnTable so we use ifConnectorPresent from ifXTable
  113             // to identify physical ports
  114             TCHAR oid[256];
  115             _sntprintf(oid, 256, _T(".1.3.6.1.2.1.31.1.1.1.17.%d"), iface->index); // ifConnectorPresent
  116             UINT32 cp;
  117             if (SnmpGet(snmp->getSnmpVersion(), snmp, oid, NULL, 0, &cp, sizeof(UINT32), 0) == SNMP_ERR_SUCCESS)
  118             {
  119                if (cp == 1)   // 1 == true
  120                {
  121                   iface->isPhysicalPort = true;
  122                   iface->location.port = iface->index;
  123                }
  124             }
  125          }
  126          else
  127          {
  128             SNMP_PDU *request = new SNMP_PDU(SNMP_GET_REQUEST, SnmpNewRequestId(), snmp->getSnmpVersion());
  129 
  130             TCHAR oid[256];
  131             _sntprintf(oid, 256, _T(".1.3.6.1.4.1.207.8.33.9.1.1.1.2.%d"), iface->index);  // module
  132             request->bindVariable(new SNMP_Variable(oid));
  133             _sntprintf(oid, 256, _T(".1.3.6.1.4.1.207.8.33.9.1.1.1.3.%d"), iface->index);  // port
  134             request->bindVariable(new SNMP_Variable(oid));
  135 
  136             SNMP_PDU *response;
  137             UINT32 rcc = snmp->doRequest(request, &response, SnmpGetDefaultTimeout(), 3);
  138              delete request;
  139             if (rcc == SNMP_ERR_SUCCESS)
  140             {
  141                if ((response->getNumVariables() == 2) && 
  142                    (response->getVariable(0)->getType() != ASN_NO_SUCH_OBJECT) &&
  143                    (response->getVariable(0)->getType() != ASN_NO_SUCH_INSTANCE) &&
  144                    (response->getVariable(1)->getType() != ASN_NO_SUCH_OBJECT) &&
  145                    (response->getVariable(1)->getType() != ASN_NO_SUCH_INSTANCE))
  146                {
  147                   iface->location.module = response->getVariable(0)->getValueAsInt();
  148                   iface->location.port = response->getVariable(1)->getValueAsInt();
  149                   iface->isPhysicalPort = true;
  150                }
  151                delete response;
  152             }
  153          }
  154       }
  155    }
  156     return ifList;
  157 }
  158 
  159 /**
  160  * Handler for VLAN enumeration
  161  */
  162 static UINT32 HandlerVlanList(SNMP_Variable *var, SNMP_Transport *snmp, void *arg)
  163 {
  164    VlanList *vlanList = (VlanList *)arg;
  165 
  166    VlanInfo *vlan = new VlanInfo(var->getName().getElement(var->getName().length() - 1), VLAN_PRM_PHYLOC);
  167 
  168    TCHAR buffer[256];
  169    vlan->setName(var->getValueAsString(buffer, 256));
  170    vlanList->add(vlan);
  171 
  172    return SNMP_ERR_SUCCESS;
  173 }
  174 
  175 /**
  176  * Parse port list
  177  * Format of the input string would be like '1,2,5,7,12..15,18-22,26'
  178  */
  179 static void ParsePortList(TCHAR *ports, VlanInfo *vlan, UINT32 module)
  180 {
  181    TCHAR *curr = ports, *next;
  182 
  183    do
  184    {
  185       next = _tcschr(curr, _T(','));
  186       if (next != NULL)
  187          *next = 0;
  188 
  189       TCHAR *ptr = _tcschr(curr, _T('-'));
  190       if (ptr == NULL)
  191          ptr = _tcschr(curr, _T('.'));
  192       if (ptr != NULL)
  193       {
  194          *ptr = 0;
  195          ptr++;
  196          if (*ptr == _T('.')) // handle n..m case
  197             ptr++;
  198          UINT32 start = _tcstoul(curr, NULL, 10);
  199          UINT32 end = _tcstoul(ptr, NULL, 10);
  200          for(UINT32 p = start; p <= end; p++)
  201             vlan->add(0, module, 0, p);
  202       }
  203       else
  204       {
  205          UINT32 port = _tcstoul(curr, NULL, 10);
  206          vlan->add(0, module, 0, port);
  207       }
  208 
  209       curr = next + 1;
  210    } while(next != NULL);
  211 }
  212 
  213 /**
  214  * Get VLANs 
  215  */
  216 VlanList *AlliedTelesisDriver::getVlans(SNMP_Transport *snmp, NObject *node, DriverData *driverData)
  217 {
  218    VlanList *list = NetworkDeviceDriver::getVlans(snmp, node, driverData);
  219    if ((list != NULL) && (list->size() > 0))
  220       return list;   // retrieved from standard MIBs
  221 
  222    if (list == NULL)
  223       list = new VlanList();
  224 
  225    if (SnmpWalk(snmp, _T(".1.3.6.1.4.1.207.8.33.8.1.1.2"), HandlerVlanList, list) != SNMP_ERR_SUCCESS)
  226         goto failure;
  227 
  228    for(int i = 0; i < list->size(); i++)
  229    {
  230       VlanInfo *vlan = list->get(i);
  231 
  232       SNMP_PDU *request = new SNMP_PDU(SNMP_GET_REQUEST, SnmpNewRequestId(), snmp->getSnmpVersion());
  233 
  234       TCHAR oid[256];
  235       _sntprintf(oid, 256, _T(".1.3.6.1.4.1.207.8.33.8.1.1.5.%d"), vlan->getVlanId()); // tagged ports
  236       request->bindVariable(new SNMP_Variable(oid));
  237       _sntprintf(oid, 256, _T(".1.3.6.1.4.1.207.8.33.8.1.1.4.%d"), vlan->getVlanId()); // untagged ports
  238       request->bindVariable(new SNMP_Variable(oid));
  239       
  240       SNMP_PDU *response;
  241       if (snmp->doRequest(request, &response, SnmpGetDefaultTimeout(), 3) == SNMP_ERR_SUCCESS)
  242       {
  243          if ((response->getNumVariables() == 2) && 
  244              (response->getVariable(0)->getType() != ASN_NO_SUCH_OBJECT) &&
  245              (response->getVariable(0)->getType() != ASN_NO_SUCH_INSTANCE) &&
  246              (response->getVariable(1)->getType() != ASN_NO_SUCH_OBJECT) &&
  247              (response->getVariable(1)->getType() != ASN_NO_SUCH_INSTANCE))
  248          {
  249             TCHAR buffer[1024];
  250             ParsePortList(response->getVariable(0)->getValueAsString(buffer, 1024), vlan, 1);
  251             ParsePortList(response->getVariable(1)->getValueAsString(buffer, 1024), vlan, 1);
  252          }
  253          delete response;
  254       }
  255       delete request;
  256    }
  257 
  258    return list;
  259 
  260 failure:
  261     delete list;
  262     return NULL;
  263 }
  264 
  265 /**
  266  * Driver entry point
  267  */
  268 DECLARE_NDD_ENTRY_POINT(AlliedTelesisDriver);
  269 
  270 /**
  271  * DLL entry point
  272  */
  273 #ifdef _WIN32
  274 
  275 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  276 {
  277     if (dwReason == DLL_PROCESS_ATTACH)
  278         DisableThreadLibraryCalls(hInstance);
  279     return TRUE;
  280 }
  281 
  282 #endif