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

SNMP编程

开发平台:

Unix_Linux

  1. #include <net-snmp/net-snmp-config.h>
  2. #if HAVE_WINSOCK_H
  3. #include <winsock.h>
  4. #endif
  5. #if HAVE_STRING_H
  6. #include <string.h>
  7. #else
  8. #include <strings.h>
  9. #endif
  10. #include <net-snmp/net-snmp-includes.h>
  11. #include <net-snmp/agent/net-snmp-agent-includes.h>
  12. #include "snmpTargetAddrEntry.h"
  13. #include "snmpTargetParamsEntry.h"
  14. #include "target.h"
  15. #define MAX_TAGS 128
  16. netsnmp_session *
  17. get_target_sessions(char *taglist, TargetFilterFunction * filterfunct,
  18.                     void *filterArg)
  19. {
  20.     netsnmp_session *ret = NULL, thissess;
  21.     struct targetAddrTable_struct *targaddrs;
  22.     char            buf[SPRINT_MAX_LEN];
  23.     char            tags[MAX_TAGS][SPRINT_MAX_LEN], *cp;
  24.     int             numtags = 0, i;
  25.     static struct targetParamTable_struct *param;
  26.     DEBUGMSGTL(("target_sessions", "looking for: %sn", taglist));
  27.     for (cp = taglist; cp && numtags < MAX_TAGS;) {
  28.         cp = copy_nword(cp, tags[numtags], sizeof(tags[numtags]));
  29.         DEBUGMSGTL(("target_sessions", " for: %d=%sn", numtags,
  30.                     tags[numtags]));
  31.         numtags++;
  32.     }
  33.     for (targaddrs = get_addrTable(); targaddrs;
  34.          targaddrs = targaddrs->next) {
  35.         /*
  36.          * legal row? 
  37.          */
  38.         if (targaddrs->tDomain == NULL ||
  39.             targaddrs->tAddress == NULL ||
  40.             targaddrs->rowStatus != SNMP_ROW_ACTIVE) {
  41.             DEBUGMSGTL(("target_sessions", "  which is not ready yetn"));
  42.             continue;
  43.         }
  44.         if (netsnmp_tdomain_support
  45.             (targaddrs->tDomain, targaddrs->tDomainLen, NULL, NULL) == 0) {
  46.             snmp_log(LOG_ERR,
  47.                      "unsupported domain for target address table entry %sn",
  48.                      targaddrs->name);
  49.         }
  50.         /*
  51.          * check tag list to see if we match 
  52.          */
  53.         if (targaddrs->tagList) {
  54.             /*
  55.              * loop through tag list looking for requested tags 
  56.              */
  57.             for (cp = targaddrs->tagList; cp;) {
  58.                 cp = copy_nword(cp, buf, sizeof(buf));
  59.                 for (i = 0; i < numtags; i++) {
  60.                     if (strcmp(buf, tags[i]) == 0) {
  61.                         /*
  62.                          * found a valid target table entry 
  63.                          */
  64.                         DEBUGMSGTL(("target_sessions", "found one: %sn",
  65.                                     tags[i]));
  66.                         if (targaddrs->params) {
  67.                             param = get_paramEntry(targaddrs->params);
  68.                             if (!param
  69.                                 || param->rowStatus != SNMP_ROW_ACTIVE) {
  70.                                 /*
  71.                                  * parameter entry must exist and be active 
  72.                                  */
  73.                                 continue;
  74.                             }
  75.                         } else {
  76.                             /*
  77.                              * parameter entry must be specified 
  78.                              */
  79.                             continue;
  80.                         }
  81.                         /*
  82.                          * last chance for caller to opt-out.  Call
  83.                          * filtering function 
  84.                          */
  85.                         if (filterfunct &&
  86.                             (*(filterfunct)) (targaddrs, param,
  87.                                               filterArg)) {
  88.                             continue;
  89.                         }
  90.                         if (targaddrs->storageType != ST_READONLY &&
  91.                             targaddrs->sess &&
  92.                             param->updateTime >=
  93.                             targaddrs->sessionCreationTime) {
  94.                             /*
  95.                              * parameters have changed, nuke the old session 
  96.                              */
  97.                             snmp_close(targaddrs->sess);
  98.                             targaddrs->sess = NULL;
  99.                         }
  100.                         /*
  101.                          * target session already exists? 
  102.                          */
  103.                         if (targaddrs->sess == NULL) {
  104.                             /*
  105.                              * create an appropriate snmp session and add
  106.                              * it to our return list 
  107.                              */
  108.                             netsnmp_transport *t = NULL;
  109.                             t = netsnmp_tdomain_transport_oid(targaddrs->
  110.                                                               tDomain,
  111.                                                               targaddrs->
  112.                                                               tDomainLen,
  113.                                                               targaddrs->
  114.                                                               tAddress,
  115.                                                               targaddrs->
  116.                                                               tAddressLen,
  117.                                                               0);
  118.                             if (t == NULL) {
  119.                                 DEBUGMSGTL(("target_sessions",
  120.                                             "bad dest ""));
  121.                                 DEBUGMSGOID(("target_sessions",
  122.                                              targaddrs->tDomain,
  123.                                              targaddrs->tDomainLen));
  124.                                 DEBUGMSG(("target_sessions", "", ""));
  125.                                 DEBUGMSGHEX(("target_sessions",
  126.                                              targaddrs->tAddress,
  127.                                              targaddrs->tAddressLen));
  128.                                 DEBUGMSG(("target_sessions", "n"));
  129.                                 continue;
  130.                             } else {
  131.                                 char           *dst_str =
  132.                                     t->f_fmtaddr(t, NULL, 0);
  133.                                 if (dst_str != NULL) {
  134.                                     DEBUGMSGTL(("target_sessions",
  135.                                                 "  to: %sn", dst_str));
  136.                                     free(dst_str);
  137.                                 }
  138.                             }
  139.                             memset(&thissess, 0, sizeof(thissess));
  140.                             thissess.timeout = (targaddrs->timeout) * 1000;
  141.                             thissess.retries = targaddrs->retryCount;
  142.                             DEBUGMSGTL(("target_sessions",
  143.                                         "timeout: %d -> %dn",
  144.                                         targaddrs->timeout,
  145.                                         thissess.timeout));
  146.                             if (param->mpModel == SNMP_VERSION_3 &&
  147.                                 param->secModel != 3) {
  148.                                 snmp_log(LOG_ERR,
  149.                                          "unsupported model/secmodel combo for target %sn",
  150.                                          targaddrs->name);
  151.                                 /*
  152.                                  * XXX: memleak 
  153.                                  */
  154.                                 netsnmp_transport_free(t);
  155.                                 continue;
  156.                             }
  157.                             thissess.version = param->mpModel;
  158.                             if (param->mpModel == SNMP_VERSION_3) {
  159.                                 thissess.securityName = param->secName;
  160.                                 thissess.securityNameLen =
  161.                                     strlen(thissess.securityName);
  162.                                 thissess.securityLevel = param->secLevel;
  163. #if !defined(DISABLE_SNMPV1) || !defined(DISABLE_SNMPV2C)
  164.                             } else {
  165.                                 thissess.community =
  166.                                     (u_char *) strdup(param->secName);
  167.                                 thissess.community_len =
  168.                                     strlen((char *) thissess.community);
  169. #endif
  170.                             }
  171.                             targaddrs->sess = snmp_add(&thissess, t,
  172.                                                        NULL, NULL);
  173.                             targaddrs->sessionCreationTime = time(NULL);
  174.                         }
  175.                         if (targaddrs->sess) {
  176.                             if (ret) {
  177.                                 targaddrs->sess->next = ret;
  178.                             }
  179.                             ret = targaddrs->sess;
  180.                         } else {
  181.                             snmp_sess_perror("target session", &thissess);
  182.                         }
  183.                     }
  184.                 }
  185.             }
  186.         }
  187.     }
  188.     return ret;
  189. }