home *** CD-ROM | disk | FTP | other *** search
/ ftp.umcs.maine.edu / 2015-02-07.ftp.umcs.maine.edu.tar / ftp.umcs.maine.edu / pub / thesis / zhongy / snmp / snmparse.c < prev    next >
C/C++ Source or Header  |  1994-05-11  |  2KB  |  105 lines

  1. /*
  2.  
  3.   This is snmp parse packet. It is used to parse the receiving character
  4.  
  5.   string to a C structure. So we can access the C structure later
  6.  
  7.   programmer: Zhong Yunxiang
  8.  
  9. */
  10.  
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21.  
  22. #include "asn1.h"
  23. #include "snmp.h"
  24.  
  25. /*
  26.   decode a packet
  27. */
  28. void de_packet(struct req_desc *rqdq, unsigned char *buff_in,int len)
  29. {
  30.   int len_interest;                    /*receive value start point*/
  31.  
  32.   /* &&We must do many check here
  33.   */
  34.  
  35.   /*  printf("\n in the de_packet function");
  36.   */
  37.   len_interest=len-2;                  /*minus size of last two bytes*/
  38.   /*find type*/
  39.   rqdq->bind.sb_val.sv_type=(int)buff_in[len_interest];
  40.   /*get the len of value*/
  41.   rqdq->bind.sb_alslen=buff_in[len_interest+1];
  42.   /*get internal express of this value*/
  43.   rqdq->bind.sb_alstr=buff_in+len_interest+2;
  44.  
  45. }
  46.  
  47. /*
  48.   convert an ASN.1 encodeed integer into a machine integer
  49. */
  50. int alreadint(unsigned char *pack,int len)
  51. {
  52.   int i,tlen;
  53.   unsigned int result;
  54.   char neg;
  55.  
  56.   /*  printf("\n in the alreadint function:%d",sizeof(int));
  57.   */
  58.   if ((tlen=len) > sizeof(int))
  59.      return(0);
  60.  
  61.   result=0;
  62.  
  63.  
  64.   for (i=0;i<len;i++) {
  65.     result=(result << 8); 
  66.     result=result | (int)pack[i];
  67.     }
  68.  
  69.   return(result);
  70. }
  71.  
  72.  
  73. /*
  74.   alreadval-convert object in ASN.1 edcoded form to internal form
  75. */
  76. void alreadval(struct req_desc *rqdq)
  77. {
  78.   int type;
  79.  
  80.   /*  printf("\n in the alreadval function");
  81.   */
  82.   type=rqdq->bind.sb_val.sv_type;
  83.  
  84.   switch (type) {
  85.     case ASN1_INT:
  86.     case ASN1_COUNTER:
  87.     case ASN1_GAUGE:
  88.     case ASN1_TIMETICKS:
  89.     rqdq->bind.sb_val.sv_val.sv_int=alreadint(rqdq->bind.sb_alstr,rqdq->bind.sb_alslen);
  90.          break;
  91.     case ASN1_OCTSTR:
  92.          break;
  93.     case ASN1_NULL:
  94.          break;
  95.     case ASN1_OBJID:
  96.          break;
  97.     case ASN1_IPADDR:
  98.          break;
  99.     default:
  100.          break;
  101.     }
  102. }
  103.  
  104.  
  105.