一个类型上的问题,请大牛指导!

jianzhibeihang 2009-03-27 05:49:08
用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;
}
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
actionecho 2009-03-27
  • 打赏
  • 举报
回复
bd
jianzhibeihang 2009-03-27
  • 打赏
  • 举报
回复
多谢bd啊!
继续求教.......
bfhtian 2009-03-27
  • 打赏
  • 举报
回复
帮顶
qqwx_1986 2009-03-27
  • 打赏
  • 举报
回复
up snmp不懂
jianzhibeihang 2009-03-27
  • 打赏
  • 举报
回复
没人帮忙顶哈?自己dd

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧