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

SNMP编程

开发平台:

C/C++

  1. /*
  2.  * Simple Network Management Protocol (RFC 1067).
  3.  *
  4.  */
  5. /**********************************************************************
  6. Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  7.                       All Rights Reserved
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the name of CMU not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22. ******************************************************************/
  23. #include <config.h>
  24. #include <ctype.h>
  25. #ifdef KINETICS
  26. #include "gw.h"
  27. #include "ab.h"
  28. #include "inet.h"
  29. #include "fp4/cmdmacro.h"
  30. #include "fp4/pbuf.h"
  31. #include "glob.h"
  32. #endif
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <sys/types.h>
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #else
  39. #include <strings.h>
  40. #endif
  41. #if HAVE_NETINET_IN_H
  42. #include <netinet/in.h>
  43. #endif
  44. #ifdef HAVE_SYS_SELECT_H
  45. #include <sys/select.h>
  46. #endif
  47. #if HAVE_WINSOCK_H
  48. #include <ip/socket.h>
  49. #endif
  50. #ifndef NULL
  51. #define NULL 0
  52. #endif
  53. #if HAVE_DMALLOC_H
  54. #include <dmalloc.h>
  55. #endif
  56. #ifdef vms
  57. #include <in.h>
  58. #endif
  59. #include "asn1.h"
  60. #include "snmp.h"
  61. #include "snmp_api.h"
  62. #include "snmp_impl.h"
  63. #include "snmp_logging.h"
  64. #include "mib.h"
  65. void
  66. xdump(const u_char *cp,
  67.       size_t length,
  68.       const char *prefix)
  69. {
  70.     int col, count;
  71.     char *buffer;
  72.     buffer=(char *)malloc(strlen(prefix)+80);
  73.     if (!buffer) {
  74.       snmp_log(LOG_NOTICE, "xdump: malloc failed. packet-dump skippedn");
  75.       return;
  76.     }
  77.     count = 0;
  78.     while(count < (int)length){
  79. strcpy(buffer, prefix);
  80.         sprintf (buffer+strlen(buffer), "%.4d: ", count);
  81. for(col = 0; ((count + col) < (int)length) && col < 16; col++){
  82.     sprintf(buffer+strlen(buffer), "%02X ", cp[count + col]);
  83.             if (col % 4 == 3) strcat(buffer, " "); 
  84. }
  85.         for(;col < 16;col++){   /* pad end of buffer with zeros */
  86.             strcat(buffer, "   ");
  87.             if (col % 4 == 3) strcat(buffer, " ");
  88. }
  89. strcat(buffer, "  ");
  90. for(col = 0; ((count + col) < (int)length) && col < 16; col++){
  91.             buffer[col+60]=isprint(cp[count+col])?cp[count+col]:'.';
  92. }
  93.         buffer[col+60]='n';
  94.         buffer[col+60+1]=0;
  95.         snmp_log(LOG_DEBUG, "%s", buffer);
  96. count += col;
  97.     }
  98.     snmp_log(LOG_DEBUG, "n");
  99.     free(buffer);
  100. }  /* end xdump() */
  101. /* 
  102.    u_char * snmp_parse_var_op(
  103.    u_char *data              IN - pointer to the start of object
  104.    oid *var_name      OUT - object id of variable 
  105.    int *var_name_len         IN/OUT - length of variable name 
  106.    u_char *var_val_type      OUT - type of variable (int or octet string) (one byte) 
  107.    int *var_val_len          OUT - length of variable 
  108.    u_char **var_val      OUT - pointer to ASN1 encoded value of variable 
  109.    int *listlength          IN/OUT - number of valid bytes left in var_op_list 
  110. */
  111. u_char *
  112. snmp_parse_var_op(u_char *data,
  113.   oid *var_name,
  114.   size_t *var_name_len,
  115.   u_char *var_val_type,
  116.   size_t *var_val_len,
  117.   u_char **var_val,
  118.   size_t *listlength)
  119. {
  120.     u_char     var_op_type;
  121.     size_t     var_op_len = *listlength;
  122.     u_char     *var_op_start = data;
  123.     data = asn_parse_sequence(data, &var_op_len, &var_op_type,
  124. (ASN_SEQUENCE | ASN_CONSTRUCTOR), "var_op");
  125.     if (data == NULL){
  126.      /* msg detail is set */
  127. return NULL;
  128.     }
  129.     data = asn_parse_objid(data, &var_op_len, &var_op_type, var_name, var_name_len);
  130.     if (data == NULL){
  131. ERROR_MSG("No OID for variable");
  132. return NULL;
  133.     }
  134.     if (var_op_type != (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID))
  135. return NULL;
  136.     *var_val = data; /* save pointer to this object */
  137.     /* find out what type of object this is */
  138.     data = asn_parse_header(data, &var_op_len, var_val_type);
  139.     if (data == NULL){
  140. ERROR_MSG("No header for value");
  141. return NULL;
  142.     }
  143.     /* XXX no check for type! */
  144.     *var_val_len = var_op_len;
  145.     data += var_op_len;
  146.     *listlength -= (int)(data - var_op_start);
  147.     return data;
  148. }
  149. /*
  150.         u_char * snmp_build_var_op(
  151. u_char *data      IN - pointer to the beginning of the output buffer
  152. oid *var_name        IN - object id of variable 
  153. int *var_name_len    IN - length of object id 
  154. u_char var_val_type  IN - type of variable 
  155. int    var_val_len   IN - length of variable 
  156. u_char *var_val      IN - value of variable 
  157. int *listlength      IN/OUT - number of valid bytes left in
  158.    output buffer 
  159. */
  160. u_char *
  161. snmp_build_var_op(u_char *data,
  162.   oid *var_name,
  163.   size_t *var_name_len,
  164.   u_char var_val_type,
  165.   size_t var_val_len,
  166.   u_char *var_val,
  167.   size_t *listlength)
  168. {
  169.     size_t     dummyLen, headerLen;
  170.     u_char    *dataPtr;
  171.     dummyLen = *listlength;
  172.     dataPtr = data;
  173. #if 0
  174.     data = asn_build_sequence(data, &dummyLen,
  175.       (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 0);
  176.     if (data == NULL){
  177. return NULL;
  178.     }
  179. #endif
  180.     if (dummyLen < 4)
  181. return NULL;
  182.     data += 4;
  183.     dummyLen -=4;
  184.     headerLen = data - dataPtr;
  185.     *listlength -= headerLen;
  186.     data = asn_build_objid(data, listlength,
  187.     (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
  188.     var_name, *var_name_len);
  189.     if (data == NULL){
  190. ERROR_MSG("Can't build OID for variable");
  191. return NULL;
  192.     }
  193.     switch(var_val_type){
  194. case ASN_INTEGER:
  195.     data = asn_build_int(data, listlength, var_val_type,
  196.     (long *)var_val, var_val_len);
  197.     break;
  198. case ASN_GAUGE:
  199. case ASN_COUNTER:
  200. case ASN_TIMETICKS:
  201. case ASN_UINTEGER:
  202.     data = asn_build_unsigned_int(data, listlength, var_val_type,
  203.   (u_long *)var_val, var_val_len);
  204.     break;
  205. #ifdef OPAQUE_SPECIAL_TYPES
  206. case ASN_OPAQUE_COUNTER64:
  207. case ASN_OPAQUE_U64:
  208. #endif
  209. case ASN_COUNTER64:
  210.     data = asn_build_unsigned_int64(data, listlength, var_val_type,
  211.    (struct counter64 *)var_val,
  212.     var_val_len);
  213.     break;
  214. case ASN_OCTET_STR:
  215. case ASN_IPADDRESS:
  216. case ASN_OPAQUE:
  217.         case ASN_NSAP:
  218.     data = asn_build_string(data, listlength, var_val_type,
  219.     var_val, var_val_len);
  220.     break;
  221. case ASN_OBJECT_ID:
  222.     data = asn_build_objid(data, listlength, var_val_type,
  223.     (oid *)var_val, var_val_len / sizeof(oid));
  224.     break;
  225. case ASN_NULL:
  226.     data = asn_build_null(data, listlength, var_val_type);
  227.     break;
  228. case ASN_BIT_STR:
  229.     data = asn_build_bitstring(data, listlength, var_val_type,
  230.     var_val, var_val_len);
  231.     break;
  232. case SNMP_NOSUCHOBJECT:
  233. case SNMP_NOSUCHINSTANCE:
  234. case SNMP_ENDOFMIBVIEW:
  235.     data = asn_build_null(data, listlength, var_val_type);
  236.     break;
  237. #ifdef OPAQUE_SPECIAL_TYPES
  238.       case ASN_OPAQUE_FLOAT:
  239.         data = asn_build_float(data, listlength, var_val_type,
  240.                                (float *) var_val, var_val_len);
  241.         break;
  242.       case ASN_OPAQUE_DOUBLE:
  243.         data = asn_build_double(data, listlength, var_val_type,
  244.                                (double *) var_val, var_val_len);
  245.         break;
  246.       case ASN_OPAQUE_I64:
  247.         data = asn_build_signed_int64(data, listlength, var_val_type,
  248.                                       (struct counter64 *) var_val,
  249.                                       var_val_len);
  250.         break;
  251. #endif /* OPAQUE_SPECIAL_TYPES */
  252. default:
  253.     ERROR_MSG("wrong type");
  254.     return NULL;
  255.     }
  256.     if (data == NULL){
  257. ERROR_MSG("Can't build value");
  258. return NULL;
  259.     }
  260.     dummyLen = (data - dataPtr) - headerLen;
  261.     asn_build_sequence(dataPtr, &dummyLen,
  262.        (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), dummyLen);
  263.     return data;
  264. }