util_funcs.c
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:7k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /*
  2.  * util_funcs.c
  3.  */
  4. #include <config.h>
  5. #if HAVE_IO_H
  6. #include <io.h>
  7. #endif
  8. #include <stdio.h>
  9. #if HAVE_STDLIB_H
  10. #include <stdlib.h>
  11. #endif
  12. #if HAVE_MALLOC_H
  13. #include <malloc.h>
  14. #endif
  15. #include <sys/types.h>
  16. #ifdef __alpha
  17. #ifndef _BSD
  18. #define _BSD
  19. #define _myBSD
  20. #endif
  21. #endif
  22. #if HAVE_SYS_WAIT_H
  23. # include <sys/wait.h>
  24. #endif
  25. #ifdef __alpha
  26. #ifdef _myBSD
  27. #undef _BSD
  28. #undef _myBSD
  29. #endif
  30. #endif
  31. #ifndef WEXITSTATUS
  32. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  33. #endif
  34. #ifndef WIFEXITED
  35. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  36. #endif
  37. #if TIME_WITH_SYS_TIME
  38. # ifdef WIN32
  39. #  include <sys/timeb.h>
  40. # else
  41. #  include <time.h>
  42. # endif
  43. # include <time.h>
  44. #else
  45. # if HAVE_SYS_TIME_H
  46. #  include <sys/time.h>
  47. # else
  48. #  include <time.h>
  49. # endif
  50. #endif
  51. #if HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #if HAVE_FCNTL_H
  55. #include <fcntl.h>
  56. #endif
  57. #include <errno.h>
  58. #include <signal.h>
  59. #if HAVE_STRING_H
  60. #include <string.h>
  61. #else
  62. #include <strings.h>
  63. #endif
  64. #include <ctype.h>
  65. #if HAVE_WINSOCK_H
  66. #include <ip/socket.h>
  67. #endif
  68. #if HAVE_BASETSD_H
  69. #include <basetsd.h>
  70. #define ssize_t SSIZE_T
  71. #endif
  72. #if HAVE_RAISE
  73. #define alarm raise
  74. #endif
  75. #include "mibincl.h"
  76. #include "struct.h"
  77. #include "util_funcs.h"
  78. #include <snmplib/system.h>
  79. #if HAVE_LIMITS_H
  80. #include "limits.h"
  81. #endif
  82. #ifdef USING_UCD_SNMP_ERRORMIB_MODULE
  83. #include "ucd-snmp/errormib.h"
  84. #else
  85. #define setPerrorstatus(x) snmp_log_perror(x)
  86. #endif
  87. #include "read_config.h"
  88. extern int numprocs, numextens;
  89. extern char *skip_white (char *);
  90. char *skip_not_white (char *);
  91. #define MAXARGS 30
  92. /************************************************************************
  93.  * generic_header
  94.  *
  95.  * Parameters:
  96.  *   *vp    (I)     Pointer to variable entry that points here.
  97.  *   *name    (I/O)   Input name requested, output name found.
  98.  *   *length  (I/O)   Length of input and output oid's.
  99.  *    exact   (I)     TRUE if an exact match was requested.
  100.  *   *var_len (O)     Length of variable or 0 if function returned.
  101.  * (**write_method)   Hook to name a write method (UNUSED).
  102.  * 
  103.  * Returns:
  104.  * MATCH_SUCCEEDED If vp->name matches name (accounting for exact bit).
  105.  * MATCH_FAILED Otherwise,
  106.  *
  107.  *  vp->namelen:oid length of leaf
  108.  *  vp->namelen+1:oid length of index
  109.  * Check whether variable (vp) matches name.
  110.  */
  111. int header_table_generic(struct variable *vp,
  112.        oid *name,
  113.        int *length,
  114.        int exact,
  115.        int *var_len,
  116.        WriteMethod **write_method)
  117. {
  118.     int result;
  119.   /*  int flag = 0;*/
  120.     int len = min(vp->namelen, *length);
  121.     result = snmp_oid_compare(name, len, vp->name , len );
  122.      if(exact && result || result > 0 )  
  123.         return MATCH_FAILED;
  124.     if( result < 0 || (!exact && *length <= vp->namelen))/*未下发索引,将索引初始化为零*/
  125.     {
  126.         name[vp->namelen] = 0;
  127.      *length = vp->namelen + 1;
  128.     }
  129. *write_method = NULL;
  130. *var_len = sizeof(uint32);
  131. memcpy( name, vp->name, vp->namelen * sizeof(oid));
  132.     return(MATCH_SUCCEEDED);
  133. }
  134. /*
  135.   header_generic(...
  136.   Arguments:
  137.   vp   IN      - pointer to variable entry that points here
  138.   name    IN/OUT  - IN/name requested, OUT/name found
  139.   length  IN/OUT  - length of IN/OUT oid's 
  140.   exact   IN      - TRUE if an exact match was requested
  141.   var_len OUT     - length of variable or 0 if function returned
  142.   write_method
  143.   
  144. */
  145. /*******************************************************************-o-******
  146.  * generic_header
  147.  *
  148.  * Parameters:
  149.  *   *vp    (I)     Pointer to variable entry that points here.
  150.  *   *name    (I/O)   Input name requested, output name found.
  151.  *   *length  (I/O)   Length of input and output oid's.
  152.  *    exact   (I)     TRUE if an exact match was requested.
  153.  *   *var_len (O)     Length of variable or 0 if function returned.
  154.  * (**write_method)   Hook to name a write method (UNUSED).
  155.  *      
  156.  * Returns:
  157.  * MATCH_SUCCEEDED If vp->name matches name (accounting for exact bit).
  158.  * MATCH_FAILED Otherwise,
  159.  *
  160.  *
  161.  * Check whether variable (vp) matches name.
  162.  */
  163. int
  164. header_generic(struct variable *vp,
  165.        oid *name,
  166.        int *length,
  167.        int exact,
  168.        int *var_len,
  169.        WriteMethod **write_method)
  170. {
  171.     oid newname[MAX_OID_LEN];
  172.     int result;
  173.     DEBUGMSGTL(("util_funcs", "header_generic: "));
  174.     DEBUGMSGOID(("util_funcs", name, *length));
  175.     DEBUGMSG(("util_funcs"," exact=%dn", exact));
  176.     memcpy((char *)newname, (char *)vp->name, (int)vp->namelen * sizeof(oid));
  177.     newname[vp->namelen] = 0;
  178.     result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
  179.     DEBUGMSGTL(("util_funcs", "  result: %dn", result));
  180.     if ((exact && (result != 0)) || (!exact && (result >= 0)))
  181.         return(MATCH_FAILED);
  182.     memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid));
  183.     *length = vp->namelen + 1;
  184.     *write_method = 0;
  185.     *var_len = sizeof(long); /* default to 'long' results */
  186.     return(MATCH_SUCCEEDED);
  187. }
  188. #if 0
  189. /* checkmib(): provided for backwards compatibility, do not use: */
  190. int checkmib(struct variable *vp, oid *name, int *length,
  191.              int exact, int *var_len,
  192.              WriteMethod **write_method, int max) {
  193.   /* checkmib used to be header_simple_table, with reveresed boolean
  194.      return output.  header_simple_table() was created to match
  195.      header_generic(). */
  196.   return (!header_simple_table(vp, name, length, exact, var_len,
  197.                               write_method, max));
  198. }
  199. #endif
  200. char *find_field(char *ptr,
  201.  int field)
  202. {
  203.   int i;
  204.   char *init=ptr;
  205.   
  206.   if (field == LASTFIELD) {
  207.     /* skip to end */
  208.     while (*ptr++);
  209.     ptr = ptr - 2;
  210.     /* rewind a field length */
  211.     while (*ptr != 0 && isspace(*ptr) && init <= ptr) ptr--;
  212.     while (*ptr != 0 && !isspace(*ptr) && init <= ptr) ptr--;
  213.     if (isspace(*ptr)) ptr++;  /* past space */
  214.     if (ptr < init) ptr = init;
  215.     if (!isspace(*ptr) && *ptr != 0) return(ptr);
  216.   } else {
  217.     if ((ptr = skip_white(ptr)) == NULL) return(NULL);
  218.     for (i=1; *ptr != 0 && i != field; i++) 
  219.       {
  220.         if ((ptr = skip_not_white(ptr)) == NULL) return (NULL);
  221.         if ((ptr = skip_white(ptr)) == NULL) return (NULL);
  222.       }
  223.     if (*ptr != 0 && i == field) return(ptr);
  224.     return (NULL);
  225.   }
  226.   return(NULL);
  227. }
  228. int parse_miboid(const char *buf,
  229.  oid *oidout)
  230. {
  231.   int i;
  232.   
  233.   if (!buf)
  234.     return 0;
  235.   if (*buf == '.') buf++;
  236.   for(i=0;isdigit(*buf);i++) {
  237.     oidout[i] = atoi(buf);
  238.     while(isdigit(*buf++));
  239.     if (*buf == '.') buf++;
  240.   }
  241.   /* oidout[i] = -1; hmmm */
  242.   return i;
  243. }
  244. void
  245. string_append_int (char *s, 
  246.    int val)
  247. {
  248.     char textVal[16];
  249.     if (val < 10) {
  250. *s++ = (unsigned char)('0' + val);
  251. *s = '';
  252. return;
  253.     }
  254.     sprintf (textVal, "%d", val);
  255.     strcpy(s, textVal);
  256.     return;
  257. }