Difference between revisions of "Command Line Calling Order"

From Net-SNMP Wiki
Jump to: navigation, search
(expand snmp_open)
Line 50: Line 50:
 
    
 
    
 
     pdu = snmp_pdu_create(SNMP_MSG_(GET|SET|GETNEXT|...));
 
     pdu = snmp_pdu_create(SNMP_MSG_(GET|SET|GETNEXT|...));
 +
        <font color="green">// sets up default values in the PDU</font>
 
     snmp_add_null_var(pdu, ...);                // adding in variables you want to get/set
 
     snmp_add_null_var(pdu, ...);                // adding in variables you want to get/set
 +
    snmp_pdu_add_variable(pdu, ...);            // OR: use this one to add complete variables
 
   
 
   
 
     status = snmp_sync_response(ss, pdu, &response);  // Note: pdu is "consumed" by this call; don't use it again!
 
     status = snmp_sync_response(ss, pdu, &response);  // Note: pdu is "consumed" by this call; don't use it again!
+
        <font color="green">snmp_send(ss, pdu);
 +
        snmp_select_info()                      // figures out which sockets to select on and needed timeout values
 +
        select();
 +
        snmp_read(sockets);                      // parses any received packets</font>
 +
       
 
     // results are in the response list, assuming no errors.  See the [[snmpget]] code for example error handling
 
     // results are in the response list, assuming no errors.  See the [[snmpget]] code for example error handling
 
   
 
   

Revision as of 23:59, 17 February 2010

Most of the Net-SNMP command line applications (like snmpget, snmpwalk, snmpset, ...) have similar internal code. This page describes the calling structure of these applications. If you're writing your own applications this should help you understand the process flow of the applications.

Comments are written in // style for simplicity, but obviously this isn't callable code anyway...

Code in black (indented 4 spaces generally) is application code, and code in green is functions called by the library code (indented 8+ spaces generally).

main()
   netsnmp_session session, *ss;
   netsnmp_pdu    *pdu;
   netsnmp_pdu    *response;
   netsnmp_variable_list *vars;

   // the only thing you can do before snmp_parse_args is call the netsnmp_ds_set_(bool|int|string) functions

   snmp_parse_args(argv, argc, &session, ...);   // accepts argv, argc, a session to initialize and optional additional arguments
       snmp_sess_init(session);
       getopt();                                 // and process
       init_snmp("snmpapp");                     // change to your app name if something special
           // ensure we're only called once
           // set the application name to "snmpapp" (or whatever was passed in).
           snmp_debug_init();                    // starts debugging output if requested
           netsnmp_container_init_list();
           init_callbacks();
           init_snmp_logging();                  // figures out if we're logging to stdout, stderr, file, syslog, ...
           snmp_init_statistics();
           register_mib_handlers();              // registers default mibs, mibdirs, etc .conf handlers
           register_default_handlers();          // registers all the other common (zillion) snmp.conf tokens
           init_snmpv3(type);                    // SNMPv3 and security module specific init
           init_snmp_alarm();
           init_snmp_enum(type);
           init_vacm();                          // access control

           read_premib_configs();                // reads in early bootstapping .conf tokens (setting mibs to load, dirs, etc)
           netsnmp_init_mib();                   // reads in MIBs requested to load either default or special

           read_configs();                       // reads in the rest of the normal configuration tokens              

       // setup special session params
       // like v3/USM keys

       session->peername = NEXT_ARGV_AFTER_OPTS

   
   SOCK_STARTUP;                                 // a win32 specific required macro

   ss = snmp_open(&session);
       // initialize appropriate transport using peername
       // adds the session to the internal "list"
 
   pdu = snmp_pdu_create(SNMP_MSG_(GET|SET|GETNEXT|...));
       // sets up default values in the PDU
   snmp_add_null_var(pdu, ...);                 // adding in variables you want to get/set
   snmp_pdu_add_variable(pdu, ...);             // OR: use this one to add complete variables

   status = snmp_sync_response(ss, pdu, &response);  // Note: pdu is "consumed" by this call; don't use it again!
       snmp_send(ss, pdu);
       snmp_select_info()                       // figures out which sockets to select on and needed timeout values
       select();
       snmp_read(sockets);                      // parses any received packets
       
   // results are in the response list, assuming no errors.  See the snmpget code for example error handling

   if (response)
     snmp_free_pdu(response);
   snmp_close(ss);
   SOCK_CLEANUP;