1、snmp_session結構
struct snmp_session {
long version; /** snmpversion */
int retries; /**Number of retries before timeout. */
long timeout; /** Number of uS until first timeout, thenexponential backoff */
u_long flags;
struct snmp_session *subsession;
struct snmp_session *next;
char *peername;
u_short remote_port;
char *localname; /** My Domain name or dotted IP address, 0for default */
u_short local_port;
u_char *(*authenticator)(u_char *, size_t *, u_char *, size_t);
/** * Authentication function or NULL if null authentication is used */
netsnmp_callback callback; /** Function to interpret incoming data */
void *callback_magic;/*Pointer to data that the callback function may consider important*/
int s_errno; /** copyof system errno */
int s_snmp_errno; /** copy of library errno */
long sessid; /**Session id - AgentX only */
/* * SNMPv1 & SNMPv2cfields */
u_char *community; /**community for outgoing requests. */
size_t community_len; /**Length of community name. */
size_t rcvMsgMaxSize;/** Largest message to try toreceive. */
size_t sndMsgMaxSize;/** Largest message to try to send. */
/* * SNMPv3 fields */
u_char isAuthoritative;/** are we the authoritative engine? */
u_char *contextEngineID;/** authoritative snmpEngineID */
size_t contextEngineIDLen; /** Length of contextEngineID */
u_int engineBoots; /**initial engineBoots for remote engine */
u_int engineTime; /**initial engineTime for remote engine */
char *contextName; /** authoritative contextName */
size_t contextNameLen;/** Length of contextName */
u_char *securityEngineID; /** authoritative snmpEngineID */
size_t securityEngineIDLen; /** Length of contextEngineID */
char *securityName; /**on behalf of this principal */
size_t securityNameLen;/** Length of securityName. */
oid *securityAuthProto; /** authprotocol oid */
size_t securityAuthProtoLen; /** Length ofauth protocol oid */
u_char securityAuthKey[USM_AUTH_KU_LEN]; /** Ku for auth protocol XXX */
size_t securityAuthKeyLen; /** Length of Ku for auth protocol */
u_char *securityAuthLocalKey; /** Kul for auth protocol */
size_t securityAuthLocalKeyLen; /** Length of Kul for auth protocol XXX */
oid *securityPrivProto; /** priv protocol oid */
size_t securityPrivProtoLen; /** Length of priv protocol oid */
u_char securityPrivKey[USM_PRIV_KU_LEN]; /** Ku for privacy protocol XXX */
size_t securityPrivKeyLen; /** Length of Ku for priv protocol */
u_char *securityPrivLocalKey; /** Kul for priv protocol */
size_t securityPrivLocalKeyLen; /** Length of Kul for priv protocol XXX */
int securityModel; /**snmp security model, v1, v2c, usm */
int securityLevel; /**noAuthNoPriv, authNoPriv, authPriv */
char *paramName; /**target param name */
void *securityInfo; /*security module specific */
struct netsnmp_container_s *transport_configuration; /* transport specific configuration */
void *myvoid; /*use as you want data*/
};
2、netsnmp_pdu結構
typedef struct snmp_pdu {
#define non_repeaters errstat
#define max_repetitions errindex
/** Protocol-version independent fields*/
long version; /** snmpversion */
int command; /** Typeof this PDU */
long reqid; /** Request id - note: not incremented onretries */
long msgid; /** Messageid for V3 messages note: incremented for each retry */
long transid; /**Unique ID for incoming transactions */
long sessid; /**Session id for AgentX messages */
long errstat; /** Error status (non_repeaters in GetBulk) */
long errindex; /**Error index (max_repetitions in GetBulk) */
u_long time; /** Uptime*/
u_long flags;
int securityModel;
int securityLevel; /** noAuthNoPriv,authNoPriv, authPriv */
int msgParseModel;
/** * Transport-specificopaque data. This replaces theIP-centric address * field. */
void *transport_data;
int transport_data_length;
/* The actual transport domain. This SHOULD NOT BE FREE()D. */
const oid *tDomain;
size_t tDomainLen;
netsnmp_variable_list*variables;
/* * SNMPv1 & SNMPv2c fields */
u_char *community; /**community for outgoing requests. */
size_t community_len; /** length of community name. */
/* Trap information*/
oid *enterprise; /**System OID */
size_t enterprise_length;
long trap_type; /**trap type */
long specific_type; /** specific type*/
unsigned char agent_addr[4]; /** This is ONLY used for v1TRAPs */
/* SNMPv3 fields */
u_char *contextEngineID;/** context snmpEngineID */
size_t contextEngineIDLen; /** Length ofcontextEngineID */
char *contextName; /**authoritative contextName */
size_t contextNameLen;/** Length of contextName */
u_char *securityEngineID;/** authoritative snmpEngineID for security */
size_t securityEngineIDLen; /** Length of securityEngineID */
char *securityName; /**on behalf of this principal */
size_t securityNameLen;/** Length of securityName. */
} netsnmp_pdu;
3.netsnmp_variable_list結構
typedef struct variable_list {
struct variable_list*next_variable; /** NULL for last variable */
oid *name; /** Object identifier ofvariable */
size_t name_length; /** number of subid's inname */
u_char type; /** ASN type of variable */
netsnmp_vardata val; /**value of variable */
size_t val_len; /** the length of the valueto be copied into buf */
oid name_loc[MAX_OID_LEN]; /** bufferto hold the OID */
u_char buf[40]; /** 90 percentile < 40.*/
void *data; /** (Opaque) hook foradditional data */
void (*dataFreeHook)(void *);/** callbackto free above */
int index;
} netsnmp_variable_list;
typedefunion {
long *integer;
u_char *string;
oid *objid;
u_char *bitstring;
struct counter64 *counter64;
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
float *floatVal;
double *doubleVal;
/** t_union *unionVal; */
#endif /*NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
}netsnmp_vardata;
5、完整源代碼
#include<net-snmp/net-snmp-config.h>
#include<net-snmp/net-snmp-includes.h>
#include <string.h>
/* change the word"define" to "undef" to try the (insecure) SNMPv1 version */
#define DEMO_USE_SNMP_VERSION_3
#ifdef DEMO_USE_SNMP_VERSION_3
const char *our_v3_passphrase ="The Net-SNMP Demo Password";
#endif
int main(int argc, char ** argv)
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
oid anOID[MAX_OID_LEN];
size_t anOID_len;
netsnmp_variable_list *vars;
int status;
int count=1;
/* Initialize the SNMP library */
init_snmp("snmpdemoapp");
/* Initialize a "session" that defines who we're going to talkto */
snmp_sess_init( &session ); /* set up defaults */
session.peername = strdup("211.71.149.59");
/* set up the authentication parameters for talking to the server */
#ifdef DEMO_USE_SNMP_VERSION_3
/* Use SNMPv3 to talk to the experimental server */
/* set the SNMP version number */
session.version=SNMP_VERSION_3;
/* set the SNMPv3 user name */
session.securityName = strdup("MD5User");
session.securityNameLen = strlen(session.securityName);
/* set the security level to authenticated, but not encrypted */
session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
/* set the authentication method to MD5 */
session.securityAuthProto = usmHMACMD5AuthProtocol;
session.securityAuthProtoLen =sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
session.securityAuthKeyLen = USM_AUTH_KU_LEN;
/* set the authentication key to a MD5 hashed version of our
passphrase "The Net-SNMP DemoPassword" (which must be at least 8
characters long) */
if (generate_Ku(session.securityAuthProto,
session.securityAuthProtoLen,
(u_char *)our_v3_passphrase, strlen(our_v3_passphrase),
session.securityAuthKey,
&session.securityAuthKeyLen)!= SNMPERR_SUCCESS) {
snmp_perror(argv[0]);
snmp_log(LOG_ERR,
"Error generating Ku fromauthentication pass phrase. \n");
exit(1);
}
#else /* we'll use the insecure(but simplier) SNMPv1 */
/* set the SNMP version number */
session.version = SNMP_VERSION_1;
/* set the SNMPv1 community name used for authentication */
session.community = "demopublic";
session.community_len = strlen(session.community);
#endif /* SNMPv1 */
/*
* Open the session
*/
SOCK_STARTUP;
ss = snmp_open(&session); /* establish the session*/
if (!ss) {
snmp_sess_perror("ack",&session);
SOCK_CLEANUP;
exit(1);
}
/*
* Create the PDU for the data for ourrequest.
* 1) We're going to GET the system.sysDescr.0 node.
*/
pdu = snmp_pdu_create(SNMP_MSG_GET);
anOID_len = MAX_OID_LEN;
if (!snmp_parse_oid(".1.3.6.1.2.1.1.1.0", anOID,&anOID_len)) {
snmp_perror(".1.3.6.1.2.1.1.1.0");
SOCK_CLEANUP;
exit(1);
}
#if OTHER_METHODS
* These are alternatives to the 'snmp_parse_oid' call above, /*
* e.g. specifying the OID by name rather than numerically.
*/
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(pdu, anOID, anOID_len);
/*
* Send the Request out.
*/
status = snmp_synch_response(ss, pdu, &response);
/*
* Process the response.
*/
if (status == STAT_SUCCESS && response->errstat ==SNMP_ERR_NOERROR) {
/*
* SUCCESS: Print the result variables
*/
for(vars = response->variables; vars;vars = vars->next_variable)
print_variable(vars->name,vars->name_length, vars);
/* manipuate the information ourselves */
for(vars = response->variables; vars;vars = vars->next_variable) {
if (vars->type == ASN_OCTET_STR) {
char *sp = (char *)malloc(1 +vars->val_len);
memcpy(sp, vars->val.string,vars->val_len);
sp[vars->val_len] = '\0';
printf("value #%d is a string:%s\n", count++, sp);
free(sp);
}
else
printf("value #%d is NOT astring! Ack!\n", count++);
}
} else {
/*
* FAILURE: print what went wrong!
*/
if (status == STAT_SUCCESS)
fprintf(stderr, "Error inpacket\nReason: %s\n",
snmp_errstring(response->errstat));
else if (status == STAT_TIMEOUT)
fprintf(stderr, "Timeout: Noresponse from %s.\n",
session.peername);
else
snmp_sess_perror("snmpdemoapp", ss);
}
/* * Clean up: * 1) free the response. * 2) close the session. */ if (response) snmp_free_pdu(response); snmp_close(ss);
SOCK_CLEANUP; return (0); } /* main() */
|