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 / snmpbasic.c < prev    next >
C/C++ Source or Header  |  1994-05-19  |  8KB  |  373 lines

  1. /*
  2.  
  3.   This package includes the basic routine to do snmp retrieve
  4.  
  5. */
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22.  
  23. #include <ctype.h>
  24.  
  25. #include <signal.h>
  26. #include <setjmp.h>
  27.  
  28. #include "asn1.h"
  29. #include "snmp.h"
  30. #include "snmparse.h"
  31. #include "snmpencode.h"
  32.  
  33. #define MaxTimeout_sec 5
  34.  
  35. jmp_buf timeoutbuf;
  36.  
  37. void timer()
  38. {
  39.   longjmp(timeoutbuf,1);
  40. }
  41.  
  42.  
  43. /*
  44.   change a string to a oid struct
  45. */
  46. struct oid textoid(char *str)
  47. {
  48.   int len,sub_len;
  49.   char sub_id[10];
  50.   unsigned short sub_num;
  51.   struct oid idd;
  52.   int i;
  53.  
  54.   len=0;
  55.   sub_len=0;
  56.   bzero(sub_id, sizeof(sub_id));
  57.   for (i=0; str[i] != '\0';i++) {
  58.       if (str[i] != '.') {
  59.          sub_id[sub_len]=str[i];
  60.          sub_len++;
  61.          }
  62.       else {
  63.          sub_id[sub_len]='\0';
  64.          sub_num=(unsigned short)atoi(sub_id);
  65.  
  66.          /*assign to idd struct*/
  67.          idd.id[len]=sub_num;
  68.          len++;
  69.  
  70.          /*initialize*/
  71.          bzero(sub_id, sizeof(sub_id));
  72.          sub_len=0;
  73.          }
  74.       }
  75.   idd.len=len;
  76.  
  77.   /*
  78.   printf("\n");
  79.   for (i=0;i<len;i++)
  80.     printf("BEGIN:%d ",idd.id[i]);
  81.   */
  82.  
  83.   return(idd);
  84. }
  85.  
  86.  
  87. /*
  88.   send snmp package
  89.   receive snmp package
  90.   Both in straight buff
  91. */
  92. void snmp_send_recv(char *hostname,int len,unsigned char *buff,unsigned char *buff_in)
  93. {
  94.   struct sockaddr_in sa;               /*local socket*/
  95.   struct sockaddr_in from;             /*remote socket */
  96.   struct hostent *hostptr;             /*host address struct*/
  97.   int s;                               /*socket number       */
  98.   int fromlen;                         /*remote socket length*/
  99.  
  100.   unsigned long server_ipaddr_host;  /*real ip addr in host*/
  101.   unsigned long server_ipaddr_net;   /*real ip addr in net*/
  102.  
  103.  
  104.   int cc;
  105.     /*
  106.       initialize socket
  107.     */
  108.     bzero((char *)&sa,sizeof(sa));
  109.     sa.sin_family=AF_INET;
  110.  
  111.     if (hostptr=gethostbyname(hostname)) {
  112.        bcopy(hostptr->h_addr,&sa.sin_addr,hostptr->h_length);
  113.        }
  114.     else {
  115.       if(server_ipaddr_host=inet_addr(hostname)) {
  116.         server_ipaddr_net=htonl(server_ipaddr_host);/*change to network format*/
  117.         bcopy(&server_ipaddr_net,&sa.sin_addr,sizeof(server_ipaddr_net));
  118.         }
  119.       else {
  120.        fprintf(stderr,"can not find such host name");
  121.        /* exit(0); may be C++ don't allow to use it*/
  122.        };
  123.       }
  124.  
  125.     if ( (s=socket(AF_INET,SOCK_DGRAM,0) )<0) {
  126.       fprintf(stderr,"can not create socket");
  127.       exit(0);
  128.       };
  129.  
  130.    /*
  131.      put service number
  132.    */
  133.     sa.sin_port=htons(snmp_port);
  134.  
  135.  
  136.  
  137.    signal(SIGALRM,timer);
  138.    (void)setjmp(timeoutbuf);
  139.    /*
  140.      send the first request packet
  141.    */
  142.  
  143.    if ((cc=sendto(s,buff,len,0,(struct sockaddr *)&sa,sizeof(sa))) <0) {
  144.       fprintf(stderr, "\nsend error");
  145.       exit(0);
  146.       }
  147.  
  148.    /*
  149.       receive packet
  150.    */
  151.    alarm(5);
  152.    if ((cc=recvfrom(s,buff_in,600,0,(struct sockaddr *)&from,&fromlen)) <0){
  153.       fprintf(stderr, "\nread error");
  154.       exit(0);
  155.       }
  156.    alarm(0);
  157.  
  158.    close(s);
  159. }
  160.  
  161.  
  162. /*
  163.   read mib.txt file
  164.   put all "name, obj, type " in an array of structure snbentry
  165.   return: the number of mib items in the MIB.TXT file
  166. */
  167.  
  168.  
  169. int init_mib()
  170. {
  171.  int i,j;
  172.  int finish;
  173.  char obj_str[MAXSNMPNAME];
  174.  char type_str[MAXSNMPNAME];
  175.  char name[MAXSNMPNAME];
  176.  struct oid id;
  177.  
  178.  FILE *mib_file;                         /*MIB.TXT file          */
  179.  
  180.  if ((mib_file=fopen("MIB.TXT","r")) == NULL) {
  181.     printf("\n MIB.TXT can not be opened");
  182.     exit(0);
  183.     };
  184.  
  185.  i=0;
  186.  while (1) {
  187.    bzero(name,sizeof(name));
  188.    fscanf(mib_file,"%s",name);
  189.    if (!strcmp(name,"END_OF_MIB.TXT")) {
  190.       break;
  191.       }
  192.    strcpy(mib[i].name,name);
  193.    fscanf(mib_file,"%s",obj_str);
  194.    fscanf(mib_file,"%s",type_str);
  195.    /* printf("\n%s,%s, %s",mib[i].name,obj_str,type_str);
  196.    */
  197.  
  198.   
  199.    id=textoid(obj_str);
  200.    mib[i].sb_oid=id;
  201.    /*
  202.    printf("\n");
  203.    for (j=0;j<mib[i].sb_oid.len;j++)
  204.     printf("BEGIN:%d ",mib[i].sb_oid.id[j]);
  205.    */
  206.    if (!strcmp("counter",type_str))
  207.            mib[i].sb_val.sv_type=(int)ASN1_COUNTER;
  208.    if (!strcmp("integer",type_str))
  209.            mib[i].sb_val.sv_type=ASN1_INT;
  210.    if (!strcmp("gauge",type_str))
  211.            mib[i].sb_val.sv_type=ASN1_GAUGE;
  212.    if (!strcmp("ticks",type_str))
  213.            mib[i].sb_val.sv_type=ASN1_TIMETICKS;
  214.    if (!strcmp("number",type_str))
  215.            mib[i].sb_val.sv_type=ASN1_INT;
  216. /* if (!strcmp("counter",type_str))
  217.            mib[i].sb_val.sv_type=ASN1_;
  218.    */
  219.    i++;
  220.    }
  221.   close(mib_file);
  222.   return(i);
  223. }
  224.  
  225. /*
  226.   give a snbentry struct of a name
  227. */
  228.  
  229. struct snbentry get_struct(char *name)
  230. {
  231.   struct snbentry sn;
  232.   struct oid idd;
  233.   char prefix[100];
  234.   int pre_index;
  235.   int i,j;
  236.  
  237.   unsigned short sub_num;
  238.   char sub_id[10];
  239.   int sub_len;
  240.  
  241. /*printf("\n in get_struct");*/
  242.  
  243. /*
  244.   sn.sb_oid.len=9; sn.sb_oid.id[0]=1; sn.sb_oid.id[1]=3;
  245.   sn.sb_oid.id[2]=6; sn.sb_oid.id[3]=1; sn.sb_oid.id[4]=2;
  246.   sn.sb_oid.id[5]=1; sn.sb_oid.id[6]=5; sn.sb_oid.id[7]=j;j++;
  247.   sn.sb_oid.id[8]=0;
  248. */
  249.  
  250.   if (isdigit(name[0])) {
  251.       idd=textoid(name);
  252.       }
  253.   else {
  254.       pre_index=0;
  255.       for (j=0;name[j] != '\0'; j++) {
  256.           if (name[j] != '.') {
  257.              prefix[pre_index]=name[j];
  258.              pre_index++;
  259.              }
  260.           else {
  261.              prefix[pre_index]='\0';
  262.              break;
  263.              }
  264.           }
  265.   /*    printf("\n prefix is:%s",prefix);
  266.     */
  267.       for (i=0;i<NUM_MIBITEM;i++) {
  268.           /*
  269.           printf("\n in the loop,name is:%s",mib[i].name);
  270.           */
  271.           if (!strcmp(mib[i].name,prefix))
  272.              idd=mib[i].sb_oid;
  273.           }
  274.  
  275.   /* printf("\n");
  276.   for (i=0;i<idd.len;i++)
  277.     printf("B:%d ",idd.id[i]);
  278.   */
  279.  
  280.       bzero(sub_id,sizeof(sub_id));
  281.       sub_len=0;
  282.       for (j++;name[j] != '\0'; j++)
  283.          if (name[j] != '.') {
  284.             sub_id[sub_len]=name[j];
  285.             sub_len++;
  286.             }
  287.          else {
  288.             sub_id[sub_len]='\0';
  289.             sub_num=(unsigned short)atoi(sub_id);
  290.  
  291.          /*assign to idd struct*/
  292.          idd.id[idd.len]=sub_num;
  293.          idd.len++;
  294.  
  295.          /*initialize*/
  296.          bzero(sub_id, sizeof(sub_id));
  297.          sub_len=0;
  298.          }
  299.       };
  300.   /*
  301.   printf("\n");
  302.   for (i=0;i<idd.len;i++)
  303.     printf("B:%d ",idd.id[i]);
  304.   */
  305.  
  306.   sn.sb_oid=idd;
  307.   return(sn);
  308. }
  309. /*
  310.   procedure: give the struct snbentry according to the "name"
  311.   "name": is an instance, it can be single, it can be a variable inside a
  312.           table
  313.   "
  314.  
  315.   So, we can know its value
  316. */
  317.  
  318. struct snbentry get_value(char *name,char *hostname)
  319. {
  320.   struct req_desc *rqdq,*rqdq_in ;
  321.   struct snbentry bind1,bind1_in;
  322.   unsigned char buff[SNMPMAXSZ],buff_in[SNMPMAXSZ],reqidsave[10],reqidsavelen;
  323.   int snmpdev,len,frlen;
  324.   int i;
  325.  
  326. /*  printf("\n in get_value function");
  327. */
  328.   bzero(buff,SNMPMAXSZ);
  329.   bzero(buff_in,SNMPMAXSZ);
  330.   /*make packet
  331.   */
  332.  
  333.   bind1=get_struct(name);
  334.   rqdq=new struct req_desc;
  335.   rqdq->bind=bind1;
  336.  
  337.   rqdq->reqidlen=0;
  338.   rqdq->err_stat=0;
  339.   rqdq->err_idx=0;
  340.  
  341.   len=mkpacket(rqdq,buff);
  342.  
  343.   /*send packet and receive packet
  344.   */
  345.  
  346.   /*
  347.   for (i=0; i<=len; i++)
  348.       printf("%x ",buff[i]);
  349.   */
  350.  
  351.  
  352.   snmp_send_recv(hostname,len,buff,buff_in);
  353.  /*
  354.   printf("\n");
  355.   for (i=0; i<=len+20; i++)
  356.       printf("%x ",buff_in[i]);
  357.  */
  358.  
  359.   /*
  360.    decode snmp packet
  361.   */
  362.  
  363.   rqdq_in=new struct req_desc;
  364.   de_packet(rqdq_in,buff_in,len);
  365.   alreadval(rqdq_in);                        /*change from ASN.1 to internal*/
  366.   bind1_in=rqdq_in->bind;
  367.   strcpy(bind1_in.name,name);
  368.  
  369.   return(bind1_in);
  370. }
  371.  
  372.  
  373.