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_backup.c < prev    next >
C/C++ Source or Header  |  1994-05-10  |  8KB  |  376 lines

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