"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.4.232/src/server/core/addrlist.cpp" (6 Jul 2020, 7815 Bytes) of package /linux/misc/netxms-3.4.232.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 "addrlist.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2020 Victor Kirhenshtein
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: addrlist.cpp
   20 **
   21 **/
   22 
   23 #include "nxcore.h"
   24 
   25 /**
   26  * Create address list element from NXCP message
   27  */
   28 InetAddressListElement::InetAddressListElement(NXCPMessage *msg, UINT32 baseId)
   29 {
   30    m_type = (AddressListElementType)msg->getFieldAsInt16(baseId);
   31    m_baseAddress = msg->getFieldAsInetAddress(baseId + 1);
   32    if (m_type == InetAddressListElement_SUBNET)
   33    {
   34       m_baseAddress.setMaskBits(msg->getFieldAsInt16(baseId + 2));
   35    }
   36    else
   37    {
   38       m_endAddress = msg->getFieldAsInetAddress(baseId + 2);
   39    }
   40    m_zoneUIN = msg->getFieldAsInt32(baseId + 3);
   41    m_proxyId = msg->getFieldAsInt32(baseId + 4);
   42    msg->getFieldAsString(baseId + 5, m_comments, MAX_ADDRESS_LIST_COMMENTS_LEN);
   43 }
   44 
   45 /**
   46  * Create new "range" type address list element
   47  */
   48 InetAddressListElement::InetAddressListElement(const InetAddress& baseAddr, const InetAddress& endAddr)
   49 {
   50    m_type = InetAddressListElement_RANGE;
   51    m_baseAddress = baseAddr;
   52    m_endAddress = endAddr;
   53    m_zoneUIN = 0;
   54    m_proxyId = 0;
   55    m_comments[0] = 0;
   56 }
   57 
   58 /**
   59  * Create new "subnet" type address list element
   60  */
   61 InetAddressListElement::InetAddressListElement(const InetAddress& baseAddr, int maskBits)
   62 {
   63    m_type = InetAddressListElement_SUBNET;
   64    m_baseAddress = baseAddr;
   65    m_baseAddress.setMaskBits(maskBits);
   66    m_zoneUIN = 0;
   67    m_proxyId = 0;
   68    m_comments[0] = 0;
   69 }
   70 
   71 /**
   72  * Create new "range" type address list element with zone and proxy id
   73  */
   74 InetAddressListElement::InetAddressListElement(const InetAddress& baseAddr, const InetAddress& endAddr, int32_t zoneUIN, UINT32 proxyId)
   75 {
   76    m_type = InetAddressListElement_RANGE;
   77    m_baseAddress = baseAddr;
   78    m_endAddress = endAddr;
   79    m_zoneUIN = zoneUIN;
   80    m_proxyId = proxyId;
   81    m_comments[0] = 0;
   82 }
   83 
   84 /**
   85  * Create address list element from DB record
   86  * Expected field order: addr_type,addr1,addr2,zone_uin,proxy_id,comment
   87  */
   88 InetAddressListElement::InetAddressListElement(DB_RESULT hResult, int row)
   89 {
   90    m_type = (AddressListElementType)DBGetFieldLong(hResult, row, 0);
   91    m_baseAddress = DBGetFieldInetAddr(hResult, row, 1);
   92    if (m_type == InetAddressListElement_SUBNET)
   93    {
   94       // mask field can be represented as bit count or as IPv4 mask (like 255.255.255.0)
   95       TCHAR mask[64];
   96       DBGetField(hResult, row, 2, mask, 64);
   97 
   98       TCHAR *eptr;
   99       int bits = _tcstol(mask, &eptr, 10);
  100       if ((bits != 0) && (*eptr == 0))
  101       {
  102          m_baseAddress.setMaskBits(bits);
  103       }
  104       else if (m_baseAddress.getFamily() == AF_INET)
  105       {
  106          InetAddress a = InetAddress::parse(mask);
  107          if (a.getFamily() == AF_INET)
  108          {
  109             m_baseAddress.setMaskBits(BitsInMask(a.getAddressV4()));
  110          }
  111       }
  112    }
  113    else
  114    {
  115       m_endAddress = DBGetFieldInetAddr(hResult, row, 2);
  116    }
  117    m_zoneUIN = DBGetFieldLong(hResult, row, 3);
  118    m_proxyId = DBGetFieldLong(hResult, row, 4);
  119    DBGetField(hResult, row, 5, m_comments, MAX_ADDRESS_LIST_COMMENTS_LEN);
  120 }
  121 
  122 /**
  123  * Fill NXCP message
  124  */
  125 void InetAddressListElement::fillMessage(NXCPMessage *msg, UINT32 baseId) const
  126 {
  127    msg->setField(baseId, static_cast<INT16>(m_type));
  128    msg->setField(baseId + 1, m_baseAddress);
  129    if (m_type == InetAddressListElement_SUBNET)
  130       msg->setField(baseId + 2, static_cast<INT16>(m_baseAddress.getMaskBits()));
  131    else
  132       msg->setField(baseId + 2, m_endAddress);
  133    msg->setField(baseId + 3, m_zoneUIN);
  134    msg->setField(baseId + 4, m_proxyId);
  135    msg->setField(baseId + 5, m_comments);
  136 }
  137 
  138 /**
  139  * Check if element contains given address
  140  */
  141 bool InetAddressListElement::contains(const InetAddress& addr) const
  142 {
  143    return (m_type == InetAddressListElement_SUBNET) ? m_baseAddress.contain(addr) : addr.inRange(m_baseAddress, m_endAddress);
  144 }
  145 
  146 /**
  147  * Convert to text representation
  148  */
  149 String InetAddressListElement::toString() const
  150 {
  151    StringBuffer s = m_baseAddress.toString();
  152    if (m_type == InetAddressListElement_SUBNET)
  153    {
  154       s.append(_T('/'));
  155       s.append(m_baseAddress.getMaskBits());
  156    }
  157    else
  158    {
  159       s.append(_T('-'));
  160       s.append(m_endAddress.toString());
  161    }
  162    if (m_zoneUIN != 0)
  163    {
  164       s.append(_T('@'));
  165       s.append(m_zoneUIN);
  166    }
  167    return s;
  168 }
  169 
  170 /**
  171  * Update address list from NXCP message
  172  */
  173 bool UpdateAddressListFromMessage(NXCPMessage *msg)
  174 {
  175    DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
  176 
  177    int listType = msg->getFieldAsInt32(VID_ADDR_LIST_TYPE);
  178 
  179    DBBegin(hdb);
  180 
  181    bool success = ExecuteQueryOnObject(hdb, listType, _T("DELETE FROM address_lists WHERE list_type=?"));
  182    if (success)
  183    {
  184       int count = msg->getFieldAsInt32(VID_NUM_RECORDS);
  185       if (count > 0)
  186       {
  187          DB_STATEMENT hStmt = DBPrepare(hdb, _T("INSERT INTO address_lists (list_type,addr_type,addr1,addr2,community_id,zone_uin,proxy_id,comments) VALUES (?,?,?,?,?,?,?,?)"), count > 1);
  188          if (hStmt != NULL)
  189          {
  190             DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, listType);
  191 
  192             UINT32 fieldId = VID_ADDR_LIST_BASE;
  193             for(int i = 0; (i < count) && success; i++, fieldId += 10)
  194             {
  195                InetAddressListElement e = InetAddressListElement(msg, fieldId);
  196                DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, e.getType());
  197                DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, e.getBaseAddress());
  198                if (e.getType() == InetAddressListElement_SUBNET)
  199                   DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, e.getBaseAddress().getMaskBits());
  200                else
  201                   DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, e.getEndAddress());
  202                DBBind(hStmt, 5, DB_SQLTYPE_INTEGER, (INT32)0);
  203                DBBind(hStmt, 6, DB_SQLTYPE_INTEGER, e.getZoneUIN());
  204                DBBind(hStmt, 7, DB_SQLTYPE_INTEGER, e.getProxyId());
  205                DBBind(hStmt, 8, DB_SQLTYPE_VARCHAR, e.getComments(), DB_BIND_STATIC);
  206                success = DBExecute(hStmt);
  207             }
  208 
  209             DBFreeStatement(hStmt);
  210          }
  211          else
  212          {
  213             success = false;
  214          }
  215       }
  216 
  217       if (success)
  218       {
  219          DBCommit(hdb);
  220       }
  221       else
  222       {
  223          DBRollback(hdb);
  224       }
  225    }
  226    else
  227    {
  228       DBRollback(hdb);
  229    }
  230    DBConnectionPoolReleaseConnection(hdb);
  231    return success;
  232 }
  233 
  234 /**
  235  * Load server address list of given type
  236  */
  237 ObjectArray<InetAddressListElement> *LoadServerAddressList(int listType)
  238 {
  239    DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
  240 
  241    TCHAR query[256];
  242    _sntprintf(query, 256, _T("SELECT addr_type,addr1,addr2,zone_uin,proxy_id,comments FROM address_lists WHERE list_type=%d"), listType);
  243    DB_RESULT hResult = DBSelect(hdb, query);
  244    if (hResult == NULL)
  245    {
  246       DBConnectionPoolReleaseConnection(hdb);
  247       return NULL;
  248    }
  249 
  250    int count = DBGetNumRows(hResult);
  251    ObjectArray<InetAddressListElement> *list = new ObjectArray<InetAddressListElement>(count, 16, Ownership::True);
  252 
  253    for(int i = 0; i < count; i++)
  254    {
  255       list->add(new InetAddressListElement(hResult, i));
  256    }
  257 
  258    DBFreeResult(hResult);
  259    DBConnectionPoolReleaseConnection(hdb);
  260    return list;
  261 }