一个类型上的问题,请大牛指导!
用c语言实现snmp中的set方法,做过snmp开发的朋友都知道,这时候是要封装pdu的,封装的时候要将所设参数的类型也一起封装进去,我在这里set的是system.sysName,它的类型是STRING,在编译的时候,报了STRING未定义,凭感觉应该是头文件没包含进来,但是我不知道该包含什么样的头文件?请大牛们指导!另外,我查了下man手册,里面讲到STRING类型是ASN.1中定义的类型,难道是跟这个有关系?
下面是我的源程序:
#include <net-snmp/net-snmp-config.h>
#include <stdio.h>
#include <sys/types.h>
//#include <net-snmp/library/snmp.h>
#include <net-snmp/net-snmp-includes.h>
int
main(int argc, char *argv[])
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu, *response = NULL;
netsnmp_variable_list *vars;
int count;
int status;
int exitval = 0;
struct variable_list *vp;
char names[] = "system.sysName.0";
char value[] = "I set the name!";
vp = (struct variable_list *)malloc(sizeof(struct variable_list));
vp->next_variable = NULL;
vp->name = NULL;
vp->val.string = NULL;
init_mib();
vp->name_length = MAX_NAME_LEN;
vp->name = (oid *)malloc(sizeof(oid) * vp->name_length);
if(snmp_parse_oid(names,vp->name,&vp->name_length) == NULL)
exit (-1);
vp->type = STRING;
vp->val_len = strlen(value);
vp->val.string = (u_char *)malloc(vp->val_len);
memcpy((char *)vp->val.string,(char *)value,vp->val_len);
init_snmp("mysetapp");
snmp_sess_init( &session );
session.peername = strdup("192.168.10.194");
session.version = SNMP_VERSION_2c;
session.securityName = strdup("motorola");
session.securityNameLen = strlen(session.securityName);
session.community = "public";
session.community_len = strlen(session.community);
session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
session.securityAuthProto = usmHMACMD5AuthProtocol;
session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
session.securityAuthKeyLen = USM_AUTH_KU_LEN;
SOCK_STARTUP;
/*
* open an SNMP session
*/
ss = snmp_open(&session);
if (ss == NULL) {
/*
* diagnose snmp_open errors with the input netsnmp_session pointer
*/
snmp_sess_perror("snmpset", &session);
SOCK_CLEANUP;
exit(1);
}
/*
* create PDU for SET request and add object names and values to request
*/
pdu = snmp_pdu_create(SNMP_MSG_SET);
pdu->variables = vp;
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
if (!quiet) {
for (vars = response->variables; vars;
vars = vars->next_variable)
print_variable(vars->name, vars->name_length, vars);
}
} else {
fprintf(stderr, "Error in packet.\nReason: %s\n",
snmp_errstring(response->errstat));
if (response->errindex != 0) {
fprintf(stderr, "Failed object: ");
for (count = 1, vars = response->variables;
vars && (count != response->errindex);
vars = vars->next_variable, count++);
if (vars)
fprint_objid(stderr, vars->name, vars->name_length);
fprintf(stderr, "\n");
}
exitval = 2;
}
} else if (status == STAT_TIMEOUT) {
fprintf(stderr, "Timeout: No Response from %s\n",
session.peername);
exitval = 1;
} else { /* status == STAT_ERROR */
snmp_sess_perror("snmpset", ss);
exitval = 1;
}
if (response)
snmp_free_pdu(response);
snmp_close(ss);
SOCK_CLEANUP;
return exitval;
}