home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmptest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  7.8 KB  |  316 lines

  1. /*
  2.  * snmptest.c - send snmp requests to a network entity.
  3.  *
  4.  */
  5. /***********************************************************
  6.     Copyright 1988, 1989 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <errno.h>
  31.  
  32. #include "snmp.h"
  33. #include "snmp_impl.h"
  34. #include "asn1.h"
  35. #include "snmp_api.h"
  36. #include "snmp_client.h"
  37.  
  38. extern int  errno;
  39. int command = GET_REQ_MSG;
  40. int    snmp_dump_packet = 0;
  41.  
  42. main(argc, argv)
  43.     int        argc;
  44.     char    *argv[];
  45. {
  46.     struct snmp_session session, *ss;
  47.     struct snmp_pdu *pdu, *response;
  48.     struct variable_list *vars, *vp;
  49.     int    arg, ret;
  50.     char *gateway = NULL;
  51.     char *community = NULL;
  52.     int        status, count;
  53.  
  54.     init_mib();
  55.     /*
  56.      * usage: snmptest gateway-name community-name
  57.      */
  58.     for(arg = 1; arg < argc; arg++){
  59.     if (argv[arg][0] == '-'){
  60.         switch(argv[arg][1]){
  61.         case 'd':
  62.             snmp_dump_packet++;
  63.             break;
  64.         default:
  65.             printf("invalid option: -%c\n", argv[arg][1]);
  66.             break;
  67.         }
  68.         continue;
  69.     }
  70.     if (gateway == NULL){
  71.         gateway = argv[arg];
  72.     } else if (community == NULL){
  73.         community = argv[arg]; 
  74.     } else {
  75.         printf("usage: snmptest gateway-name community-name\n");
  76.         exit(1);
  77.     }
  78.     }
  79.     if (community == NULL)
  80.     community = "public";    /* default to public */
  81.  
  82.     if (!(gateway && community)){
  83.     printf("usage: snmptest gateway-name community-name\n");
  84.     exit(1);
  85.     }
  86.  
  87.     bzero((char *)&session, sizeof(struct snmp_session));
  88.     session.peername = gateway;
  89.     session.community = (u_char *)community;
  90.     session.community_len = strlen((char *)community);
  91.     session.retries = SNMP_DEFAULT_RETRIES;
  92.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  93.     session.authenticator = NULL;
  94.     snmp_synch_setup(&session);
  95.     ss = snmp_open(&session);
  96.     if (ss == NULL){
  97.     printf("Couldn't open snmp\n");
  98.     exit(-1);
  99.     }
  100.  
  101.     while(1){
  102.     vars = NULL;
  103.     for(ret = 1; ret != 0;){
  104.         vp = (struct variable_list *)malloc(sizeof(struct variable_list));
  105.         vp->next_variable = NULL;
  106.         vp->name = NULL;
  107.         vp->val.string = NULL;
  108.  
  109.         while((ret = input_variable(vp)) == -1)
  110.         ;
  111.         if (ret == 1){
  112.         /* add it to the list */
  113.         if (vars == NULL){
  114.             /* if first variable */
  115.             pdu = snmp_pdu_create(command);
  116.             pdu->variables = vp;
  117.         } else {
  118.             vars->next_variable = vp;
  119.         }
  120.         vars = vp;
  121.         } else {
  122.         /* free the last (unused) variable */
  123.         if (vp->name)
  124.             free((char *)vp->name);
  125.         if (vp->val.string)
  126.             free((char *)vp->val.string);
  127.         free((char *)vp);
  128.         }
  129.     }
  130.     status = snmp_synch_response(ss, pdu, &response);
  131.     if (status == STAT_SUCCESS){
  132.         switch(response->command){
  133.         case GET_REQ_MSG:
  134.             printf("Received GET REQUEST ");
  135.             break;
  136.         case GETNEXT_REQ_MSG:
  137.             printf("Received GETNEXT REQUEST ");
  138.             break;
  139.         case GET_RSP_MSG:
  140.             printf("Received GET RESPONSE ");
  141.             break;
  142.         case SET_REQ_MSG:
  143.             printf("Received SET REQUEST ");
  144.             break;
  145.         case TRP_REQ_MSG:
  146.             printf("Received TRAP REQUEST ");
  147.             break;
  148.         }
  149.         printf("from %s\n", inet_ntoa(response->address.sin_addr));
  150.         printf("requestid 0x%x errstat 0x%x errindex 0x%x\n",
  151.         response->reqid, response->errstat, response->errindex);
  152.         if (response->errstat == SNMP_ERR_NOERROR){
  153.         for(vars = response->variables; vars; vars = vars->next_variable)
  154.             print_variable(vars->name, vars->name_length, vars);
  155.         } else {
  156.         printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
  157.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  158.             for(count = 1, vars = response->variables; vars && count != response->errindex;
  159.             vars = vars->next_variable, count++)
  160.                 ;
  161.             if (vars){
  162.             printf("This name doesn't exist: ");
  163.             print_objid(vars->name, vars->name_length);
  164.             }
  165.             printf("\n");
  166.         }
  167.         }
  168.  
  169.     } else if (status == STAT_TIMEOUT){
  170.         printf("No Response from %s\n", gateway);
  171.     } else {    /* status == STAT_ERROR */
  172.         printf("An error occurred, Quitting\n");
  173.     }
  174.  
  175.     if (response)
  176.         snmp_free_pdu(response);
  177.     }
  178. }
  179.  
  180. int
  181. ascii_to_binary(cp, bufp)
  182.     u_char  *cp;
  183.     u_char *bufp;
  184. {
  185.     int    subidentifier;
  186.     u_char *bp = bufp;
  187.  
  188.     for(; *cp != '\0'; cp++){
  189.     if (isspace(*cp))
  190.         continue;
  191.     if (!isdigit(*cp)){
  192.         printf("Input error\n");
  193.         return -1;
  194.     }
  195.     subidentifier = atoi(cp);
  196.     if (subidentifier > 255){
  197.         printf("subidentifier %d is too large ( > 255)\n", subidentifier);
  198.         return -1;
  199.     }
  200.     *bp++ = (u_char)subidentifier;
  201.     while(isdigit(*cp))
  202.         cp++;
  203.     cp--;
  204.     }
  205.     return bp - bufp;
  206. }
  207.  
  208.  
  209. input_variable(vp)
  210.     struct variable_list    *vp;
  211. {
  212.     u_char  buf[256], value[256];
  213.  
  214.     printf("Please enter the variable name: ");
  215.     fflush(stdout);
  216.     gets(buf);
  217.  
  218.     if (*buf == 0){
  219.     vp->name_length = 0;
  220.     return 0;
  221.     }
  222.     if (*buf == '$'){
  223.     switch(buf[1]){
  224.         case 'G':
  225.         command = GET_REQ_MSG;
  226.         printf("Request type is GET REQUEST\n");
  227.         break;
  228.         case 'N':
  229.         command = GETNEXT_REQ_MSG;
  230.         printf("Request type is GETNEXT REQUEST\n");
  231.         break;
  232.         case 'S':
  233.         command = SET_REQ_MSG;
  234.         printf("Request type is SET REQUEST\n");
  235.         break;
  236.         case 'D':
  237.         if (snmp_dump_packet){
  238.             snmp_dump_packet = 0;
  239.             printf("Turned packet dump off\n");
  240.         } else {
  241.             snmp_dump_packet = 1;
  242.             printf("Turned packet dump on\n");
  243.         }
  244.         break;
  245.         case 'Q':
  246.         printf("Quitting,  Goodbye\n");
  247.         exit(0);
  248.         break;
  249.         default:
  250.         printf("Bad command\n");
  251.     }
  252.     return -1;
  253.     }
  254.     vp->name_length = MAX_NAME_LEN;
  255.     if (!read_objid(buf, value, &vp->name_length))
  256.     return -1;
  257.     vp->name = (oid *)malloc(vp->name_length * sizeof(oid));
  258.     bcopy((char *)value, (char *)vp->name, vp->name_length * sizeof(oid));
  259.  
  260.     if (command == SET_REQ_MSG){
  261.     printf("Please enter variable type [i|s|n|o]: ");
  262.     fflush(stdout);
  263.     gets(buf);
  264.     switch(*buf){
  265.         case 'i':
  266.         vp->type = INTEGER;
  267.         break;
  268.         case 's':
  269.         vp->type = STRING;
  270.         break;
  271.         case 'n':
  272.         vp->type = NULLOBJ;
  273.         break;
  274.         case 'o':
  275.         vp->type = OBJID;
  276.         break;
  277.         default:
  278.         printf("bad type \"%c\", use \"i\", \"s\", \"n\", or \"o\".\n", *buf);
  279.         return -1;
  280.     }
  281.     printf("Please enter new value: "); fflush(stdout);
  282.     gets(buf);
  283.     switch(vp->type){
  284.         case INTEGER:
  285.         vp->val.integer = (long *)malloc(sizeof(long));
  286.         *(vp->val.integer) = atoi(buf);
  287.         vp->val_len = sizeof(long);
  288.         break;
  289.         case STRING:
  290.         vp->val_len = ascii_to_binary(buf, value);
  291.         vp->val.string = (u_char *)malloc(vp->val_len);
  292.         bcopy((char *)value, (char *)vp->val.string, vp->val_len);
  293.         break;
  294.         case NULLOBJ:
  295.         vp->val_len = 0;
  296.         vp->val.string = NULL;
  297.         break;
  298.         case OBJID:
  299.         vp->val_len = MAX_NAME_LEN;;
  300.         read_objid(buf, value, &vp->val_len);
  301.         vp->val_len *= sizeof(oid);
  302.         vp->val.objid = (oid *)malloc(vp->val_len);
  303.         bcopy((char *)value, (char *)vp->val.objid, vp->val_len);
  304.         break;
  305.         default:
  306.         printf("Internal error\n");
  307.         break;
  308.     }
  309.     } else {    /* some form of get message */
  310.     vp->type = NULLOBJ;
  311.     vp->val_len = 0;
  312.     }
  313.     return 1;
  314. }
  315.  
  316.