tcpConn_common.c
上传用户:wxp200602
上传日期:2007-10-30
资源大小:4028k
文件大小:6k
源码类别:

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *  TcpConn MIB architecture support
  3.  *
  4.  * $Id: tcpConn_common.c,v 1.1 2004/09/16 15:31:47 rstory Exp $
  5.  */
  6. #include <net-snmp/net-snmp-config.h>
  7. #include <net-snmp/net-snmp-includes.h>
  8. #include <net-snmp/agent/net-snmp-agent-includes.h>
  9. #include <net-snmp/data_access/tcpConn.h>
  10. #include "tcp-mib/tcpConnTable/tcpConnTable_constants.h"
  11. /**---------------------------------------------------------------------*/
  12. /*
  13.  * local static prototypes
  14.  */
  15. static int _access_tcpconn_entry_compare_addr(const void *lhs,
  16.                                                 const void *rhs);
  17. static void _access_tcpconn_entry_release(netsnmp_tcpconn_entry * entry,
  18.                                             void *unused);
  19. /**---------------------------------------------------------------------*/
  20. /*
  21.  * external per-architecture functions prototypes
  22.  *
  23.  * These shouldn't be called by the general public, so they aren't in
  24.  * the header file.
  25.  */
  26. extern int
  27. netsnmp_arch_tcpconn_container_load(netsnmp_container* container,
  28.                                       u_int load_flags);
  29. extern int
  30. netsnmp_arch_tcpconn_entry_init(netsnmp_tcpconn_entry *entry);
  31. extern int
  32. netsnmp_arch_tcpconn_entry_copy(netsnmp_tcpconn_entry *lhs,
  33.                                   netsnmp_tcpconn_entry *rhs);
  34. extern void
  35. netsnmp_arch_tcpconn_entry_cleanup(netsnmp_tcpconn_entry *entry);
  36. /**---------------------------------------------------------------------*/
  37. /*
  38.  * container functions
  39.  */
  40. /**
  41.  */
  42. netsnmp_container *
  43. netsnmp_access_tcpconn_container_init(u_int flags)
  44. {
  45.     netsnmp_container *container1;
  46.     DEBUGMSGTL(("access:tcpconn:container", "initn"));
  47.     /*
  48.      * create the container
  49.      */
  50.     container1 = netsnmp_container_find("access:tcpconn:table_container");
  51.     if (NULL == container1) {
  52.         snmp_log(LOG_ERR, "tcpconn primary container not foundn");
  53.         return NULL;
  54.     }
  55.     container1->container_name = strdup("tcpConnTable");
  56.     return container1;
  57. }
  58. /**
  59.  * @retval NULL  error
  60.  * @retval !NULL pointer to container
  61.  */
  62. netsnmp_container*
  63. netsnmp_access_tcpconn_container_load(netsnmp_container* container, u_int load_flags)
  64. {
  65.     int rc;
  66.     DEBUGMSGTL(("access:tcpconn:container", "loadn"));
  67.     if (NULL == container)
  68.         container = netsnmp_access_tcpconn_container_init(load_flags);
  69.     if (NULL == container) {
  70.         snmp_log(LOG_ERR, "no container specified/found for access_tcpconnn");
  71.         return NULL;
  72.     }
  73.     rc =  netsnmp_arch_tcpconn_container_load(container, load_flags);
  74.     if (0 != rc) {
  75.         netsnmp_access_tcpconn_container_free(container,
  76.                                                 NETSNMP_ACCESS_TCPCONN_FREE_NOFLAGS);
  77.         container = NULL;
  78.     }
  79.     return container;
  80. }
  81. void
  82. netsnmp_access_tcpconn_container_free(netsnmp_container *container, u_int free_flags)
  83. {
  84.     DEBUGMSGTL(("access:tcpconn:container", "freen"));
  85.     if (NULL == container) {
  86.         snmp_log(LOG_ERR, "invalid container for netsnmp_access_tcpconn_freen");
  87.         return;
  88.     }
  89.     if(! (free_flags & NETSNMP_ACCESS_TCPCONN_FREE_DONT_CLEAR)) {
  90.         /*
  91.          * free all items.
  92.          */
  93.         CONTAINER_CLEAR(container,
  94.                         (netsnmp_container_obj_func*)_access_tcpconn_entry_release,
  95.                         NULL);
  96.     }
  97.     CONTAINER_FREE(container);
  98. }
  99. /**---------------------------------------------------------------------*/
  100. /*
  101.  * tcpconn_entry functions
  102.  */
  103. /**
  104.  */
  105. /**
  106.  */
  107. netsnmp_tcpconn_entry *
  108. netsnmp_access_tcpconn_entry_create(void)
  109. {
  110.     netsnmp_tcpconn_entry *entry =
  111.         SNMP_MALLOC_TYPEDEF(netsnmp_tcpconn_entry);
  112.     int rc = 0;
  113.     DEBUGMSGTL(("verbose:access:tcpconn:entry", "createn"));
  114.     entry->oid_index.len = 4;
  115.     entry->oid_index.oids = entry->indexes;
  116.     /*
  117.      * init arch data
  118.      */
  119.     rc = netsnmp_arch_tcpconn_entry_init(entry);
  120.     if (SNMP_ERR_NOERROR != rc) {
  121.         DEBUGMSGT(("access:tcpconn:create","error %d in arch initn"));
  122.         netsnmp_access_tcpconn_entry_free(entry);
  123.     }
  124.     return entry;
  125. }
  126. /**
  127.  */
  128. void
  129. netsnmp_access_tcpconn_entry_free(netsnmp_tcpconn_entry * entry)
  130. {
  131.     if (NULL == entry)
  132.         return;
  133.     DEBUGMSGTL(("verbose:access:tcpconn:entry", "freen"));
  134.     if (NULL != entry->arch_data)
  135.         netsnmp_arch_tcpconn_entry_cleanup(entry);
  136.     free(entry);
  137. }
  138. /**
  139.  * update underlying data store (kernel) for entry
  140.  *
  141.  * @retval  0 : success
  142.  * @retval -1 : error
  143.  */
  144. int
  145. netsnmp_access_tcpconn_entry_set(netsnmp_tcpconn_entry * entry)
  146. {
  147.     int rc = SNMP_ERR_NOERROR;
  148.     if (NULL == entry) {
  149.         netsnmp_assert(NULL != entry);
  150.         return -1;
  151.     }
  152.     
  153.     DEBUGMSGTL(("access:tcpconn:entry", "setn"));
  154.    
  155.     /*
  156.      * only option is delete
  157.      */
  158.     if (! (entry->flags & NETSNMP_ACCESS_TCPCONN_DELETE))
  159.         return -1;
  160.     
  161.     rc = netsnmp_arch_tcpconn_delete(entry);
  162.     
  163.     return rc;
  164. }
  165. /**
  166.  * update an old tcpconn_entry from a new one
  167.  *
  168.  * @note: only mib related items are compared. Internal objects
  169.  * such as oid_index, ns_ia_index and flags are not compared.
  170.  *
  171.  * @retval -1  : error
  172.  * @retval >=0 : number of fileds updated
  173.  */
  174. int
  175. netsnmp_access_tcpconn_entry_update(netsnmp_tcpconn_entry *lhs,
  176.                                       netsnmp_tcpconn_entry *rhs)
  177. {
  178.     int rc, changed = 0;
  179.     DEBUGMSGTL(("access:tcpconn:entry", "updaten"));
  180.     if (lhs->tcpConnState != rhs->tcpConnState) {
  181.         ++changed;
  182.         lhs->tcpConnState = rhs->tcpConnState;
  183.     }
  184.     /*
  185.      * copy arch stuff. we don't care if it changed
  186.      */
  187.     rc = netsnmp_arch_tcpconn_entry_copy(lhs,rhs);
  188.     if (0 != rc) {
  189.         snmp_log(LOG_ERR,"arch tcpconn copy failedn");
  190.         return -1;
  191.     }
  192.     return changed;
  193. }
  194. /**---------------------------------------------------------------------*/
  195. /*
  196.  * Utility routines
  197.  */
  198. /**
  199.  */
  200. void
  201. _access_tcpconn_entry_release(netsnmp_tcpconn_entry * entry, void *context)
  202. {
  203.     netsnmp_access_tcpconn_entry_free(entry);
  204. }
  205. #ifdef NETSNMP_TCPCONN_TEST
  206. int
  207. main(int argc, char** argv)
  208. {
  209.     netsnmp_container *container;
  210.     netsnmp_config("debugTokens access:tcp,verbose:access:tcp,tcp,verbose:tcp");
  211.     netsnmp_container_init_list();
  212.     dodebug = 1;
  213.     container = netsnmp_access_tcpconn_container_load(NULL, 0);
  214.     
  215.     netsnmp_access_tcpconn_container_free(container, 0);
  216. }
  217. #endif