#include #include #include /* change the word "define" to "undef" to try the (insecure) SNMPv1 version */ #undef DEMO_USE_SNMP_VERSION_3 #ifdef DEMO_USE_SNMP_VERSION_3 const char *our_v3_passphrase = "The Net-SNMP Demo Password"; #endif #define MAX_STRING 128 #define DEVICE "snmpdemoapp" typedef struct host_s { char *name; char *comunity; netsnmp_session session; } host_t; typedef struct req_s { netsnmp_pdu *req; netsnmp_pdu *resp; netsnmp_session *session; netsnmp_session *ss; } req_t; /******************************************************************* * Initialize the SNMP library */ void host_init ( host_t *host ) { init_snmp( DEVICE ); /* * Initialize a "session" that defines who we're going to talk to */ snmp_sess_init( &host->session ); /* set up defaults */ host->session.peername = strdup( host->name ); /* set the SNMP version number */ host->session.version = SNMP_VERSION_1; /* set the SNMPv1 community name used for authentication */ host->session.community = strdup( host->comunity ); host->session.community_len = strlen( host->session.community ); } void host_deinit ( host_t *host ) { free( host->session.peername ); host->session.peername = NULL; free( host->session.community ); host->session.community = NULL; snmp_close( &host->session ); } /*******************************************************************/ req_t *snmp_req_start ( host_t *host ) { netsnmp_session *ss; req_t *req; req = malloc( sizeof( req_t ) ); /* TODO check return */ req->session = &host->session; SOCK_STARTUP; req->ss = snmp_open( &host->session ); /* establish the session */ if (!req->ss) { snmp_sess_perror( "ack", &host->session ); SOCK_CLEANUP; exit(1); } return req; } /*******************************************************************/ void snmp_req_oid ( req_t *req, char *OID ) { oid anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; if (!snmp_parse_oid( OID, anOID, &anOID_len)) { snmp_perror( OID ); SOCK_CLEANUP; exit(1); } #if OTHER_METHODS read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len); get_node("sysDescr.0", anOID, &anOID_len); read_objid("system.sysDescr.0", anOID, &anOID_len); #endif snmp_add_null_var( req->req, anOID, anOID_len ); } /*******************************************************************/ int snmp_req_send ( req_t *req ) { int status; /* * Send the Request out. */ status = snmp_synch_response(req->ss, req->req, &req->resp); /* * Process the response. */ if (status == STAT_SUCCESS && req->resp->errstat == SNMP_ERR_NOERROR) { return status; } else { /* * FAILURE: print what went wrong! */ if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(req->resp->errstat)); else if (status == STAT_TIMEOUT) fprintf(stderr, "Timeout: No response from %s.\n", req->session->peername); else snmp_sess_perror("snmpdemoapp", req->ss); return 0; } } /*******************************************************************/ /* * Get the system description of a pdu */ netsnmp_pdu *snmp_req ( host_t *host, int cmd, char *OID ) { int status; req_t *req; netsnmp_pdu *resp; req = snmp_req_start( host ); /* * Create the PDU for the data for our request. * 1) We're going to GET the system.sysDescr.0 node. */ req->req = snmp_pdu_create( cmd); snmp_req_oid( req, OID ); status = snmp_req_send( req ); if( status == STAT_SUCCESS ) { resp = req->resp; } else { resp = NULL; snmp_free_pdu( req->resp ); req->resp = NULL; } snmp_close( req->ss ); req->ss = NULL; free( req ); return resp; } /*******************************************************************/ void snmp_pdu_dump_vars ( netsnmp_pdu *pdu ) { netsnmp_variable_list *var; int count=1; /* manipuate the information ourselves */ for( var = pdu->variables; var; var = var->next_variable ) { if ( var->type == ASN_OCTET_STR ) { char *sp = (char *)malloc( 1 + var->val_len ); memcpy(sp, var->val.string, var->val_len); sp[var->val_len] = '\0'; printf( "#%d: %s\n", count, sp ); free(sp); } else if( var->type == ASN_INTEGER ) { printf( "#%d: %i\n", count, *var->val.integer ); } else if( var->type == ASN_NULL ) { printf( "#%d: %p\n", count, NULL ); } else { printf("value #%d is of type (%i)\n", count, var->type ); } count++; } } /*******************************************************************/ netsnmp_pdu *snmp_sysDescr ( host_t *host ) { return snmp_req( host, SNMP_MSG_GET, ".1.3.6.1.2.1.1.1.0" ); } /*******************************************************************/ netsnmp_pdu *snmp_sysName ( host_t *host ) { return snmp_req( host, SNMP_MSG_GET, ".1.3.6.1.2.1.1.5.0" ); } /*******************************************************************/ netsnmp_pdu *snmp_OutletNames ( host_t *host ) { int q; netsnmp_pdu *resp; char oid[MAX_STRING]; netsnmp_variable_list *var; for( q=1 ; q <= 8 ; q++ ) { snprintf( oid, MAX_STRING, ".1.3.6.1.4.1.318.1.1.12.3.3.1.1.2.%i", q ); resp = snmp_req( host, SNMP_MSG_GET, oid ); if( resp != NULL ) { var = resp->variables; if( var->type == ASN_OCTET_STR ) { char *sp = (char *)malloc( 1 + var->val_len ); memcpy(sp, var->val.string, var->val_len); sp[ var->val_len ] = '\0'; printf( "Outlet %i: %*s\n", q, (var->val_len - 2 ), sp ); free( sp ); } else { printf( "Outlet %i: Not a string (%i)\n", q, var->type ); } snmp_free_pdu( resp ); } } return NULL; } /*******************************************************************/ int main(int argc, char ** argv) { host_t host = { "10.0.0.166", "apcread" }; netsnmp_pdu *response; host_init( &host ); response = snmp_OutletNames( &host ); if( response != NULL ) { /* * SUCCESS: Print the result variables */ snmp_pdu_dump_vars( response ); snmp_free_pdu( response ); } host_deinit( &host ); SOCK_CLEANUP; return (0); }