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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *  tcpConnTable MIB architecture support
  3.  *
  4.  * $Id: tcpConn_linux.c,v 1.2 2004/10/16 03:35:15 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.  * initialize arch specific storage
  13.  *
  14.  * @retval  0: success
  15.  * @retval <0: error
  16.  */
  17. int
  18. netsnmp_arch_tcpconn_entry_init(netsnmp_tcpconn_entry *entry)
  19. {
  20.     /*
  21.      * init
  22.      */
  23.     return 0;
  24. }
  25. /*
  26.  * cleanup arch specific storage
  27.  */
  28. void
  29. netsnmp_arch_tcpconn_entry_cleanup(netsnmp_tcpconn_entry *entry)
  30. {
  31.     /*
  32.      * cleanup
  33.      */
  34. }
  35. /*
  36.  * copy arch specific storage
  37.  */
  38. int
  39. netsnmp_arch_tcpconn_entry_copy(netsnmp_tcpconn_entry *lhs,
  40.                                   netsnmp_tcpconn_entry *rhs)
  41. {
  42.     return 0;
  43. }
  44. /*
  45.  * delete an entry
  46.  */
  47. int
  48. netsnmp_arch_tcpconn_delete(netsnmp_tcpconn_entry *entry)
  49. {
  50.     if (NULL == entry)
  51.         return -1;
  52. #warning "tcpConn delete"
  53.     return -1;
  54. }
  55. /**
  56.  *
  57.  * @retval  0 no errors
  58.  * @retval !0 errors
  59.  */
  60. int
  61. netsnmp_arch_tcpconn_container_load(netsnmp_container *container)
  62. {
  63.     int             rc = 0;
  64.     FILE           *in;
  65.     char            line[160];
  66.     u_char          *buf;
  67.     netsnmp_tcpconn_entry *entry;
  68.     
  69.     netsnmp_assert(NULL != container);
  70. #define PROCFILE "/proc/net/tcp"
  71.     if (!(in = fopen(PROCFILE, "r"))) {
  72.         snmp_log(LOG_ERR,"could not open " PROCFILE "n");
  73.         return -2;
  74.     }
  75.     
  76.     fgets(line, sizeof(line), in); /* skip header */
  77.     /*
  78.      *   sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
  79.      *   0: 00000000:8000 00000000:0000 0A 00000000:00000000 00:00000000 00000000    29        0 1028 1 df7b1b80 300 0 0 2 -1
  80.      */
  81.     while (fgets(line, sizeof(line), in)) {
  82.         netsnmp_tcpconn_entry *entry;
  83.         static int      linux_states[12] =
  84.             { 1, 5, 3, 4, 6, 7, 11, 1, 8, 9, 2, 10 };
  85.         int             state, rc, local_port, remote_port;
  86.         /*
  87.          */
  88.         entry = netsnmp_access_tcpconn_entry_create();
  89.         if(NULL == entry) {
  90.             rc = -3;
  91.             break;
  92.         }
  93.         if (5 != (rc = sscanf(line, "%*d: %x:%x %x:%x %x",
  94.                               &entry->indexes[NETSNMP_TCPCONN_IDX_LOCAL_ADDR],
  95.                               &local_port,
  96.                               &entry->indexes[NETSNMP_TCPCONN_IDX_REMOTE_ADDR],
  97.                               &remote_port, &state))) {
  98.             DEBUGMSGT(("access:tcpconn:container",
  99.                        "error parsing line (%d != 5)n", rc));
  100.             DEBUGMSGT(("access:tcpconn:container"," line '%s'n", line));
  101.             netsnmp_access_tcpconn_entry_free(entry);
  102.             continue;
  103.         }
  104.         DEBUGMSGT(("verbose:access:tcpconn:container"," line '%s'n", line));
  105.         entry->indexes[NETSNMP_TCPCONN_IDX_LOCAL_PORT] =
  106.             htons((unsigned short) local_port);
  107.         entry->indexes[NETSNMP_TCPCONN_IDX_REMOTE_PORT] =
  108.             htons((unsigned short) remote_port);
  109.         entry->tcpConnState = (state & 0xf) < 12 ? linux_states[state & 0xf] : 2;
  110.         /*
  111.          * add entry to container
  112.          */
  113.         CONTAINER_INSERT(container, entry);
  114.     }
  115.     fclose(in);
  116.     if(rc<0)
  117.         return rc;
  118.     return 0;
  119. }