aasa2
驱动中牛
驱动中牛
  • 注册日期2004-04-01
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分525分
  • 威望339点
  • 贡献值0点
  • 好评度106点
  • 原创分0分
  • 专家分0分
阅读:1833回复:1

有谁帮我看一下代码?

楼主#
更多 发布于:2004-07-20 09:08
int snparse(rqdp,snmppack,len)
{
struct snbentry *bl, *lastbl = 0;
u_char *packp;
int totpacklen = 0, commlen = 0;
int pdulen, totbindlen = 0;
u_int lenlen;
int varbindlen = 0;
u_char *packendp;

packp = snmppack;
packendp = snmppack + len;

/* sequence operator and total packet length */
if (*packp++ != ASN1_SEQ ||(totpacklen = a1readlen(packp, &lenlen)) < 0)//  为什么存在这个判别?
{
return SYSERR;
}

packp += lenlen;

/* verify total length, version, community */
if (packendp != packp + totpacklen ||*packp++ != ASN1_INT ||*packp++ != SVERS_LEN ||*packp++ != SVERSION ||*packp++ != ASN1_OCTSTR ||(commlen = a1readlen(packp, &lenlen)) < 0)
{
return SYSERR;
}

packp += lenlen;

if (strncmp((char *)packp, SCOMM_STR, commlen) != 0)
{
return SYSERR;
}

packp += commlen;

/* PDU type and length */
if (*packp == PDU_TRAP)
return SYSERR;
rqdp->pdutype_pos = packp - snmppack;
rqdp->reqtype = *packp++;
if ((pdulen = a1readlen(packp, &lenlen)) < 0) {
return SYSERR;
}

packp += lenlen;

/* verify PDU length */
if (packendp != packp + pdulen)
{
return SYSERR;
}

/* request id */
if (*packp++ != ASN1_INT ||(rqdp->reqidlen = a1readlen(packp, &lenlen)) < 0)
{
return SYSERR;
}

packp += lenlen;
memcpy(rqdp->reqid, packp, rqdp->reqidlen);
packp += rqdp->reqidlen;

/* error status */
if (*packp++ != ASN1_INT || *packp++ != 1)
{
return SYSERR;
}

rqdp->err_stat = *packp;
rqdp->err_stat_pos = packp++ - snmppack;

/* error index */
if (*packp++ != ASN1_INT || *packp++ != 1) {
return SYSERR;
}

rqdp->err_idx = *packp;
rqdp->err_idx_pos = packp++ - snmppack;

/* sequence of variable bindings */
if (*packp++ != ASN1_SEQ ||(totbindlen = a1readlen(packp, &lenlen)) < 0)
{
return SYSERR;
}

packp += lenlen;

/* canberra bug fix - rqdp might already have old dynamic memory */
/* attached, free before overwriting pointers */
snfreebl(&rqdp->bindlf);

/* verify variable bindings length */
if (packendp != packp + totbindlen)
return SYSERR;
/* build doubly-linked bindings list; fill in only sb_a1str's */
rqdp->bindlf = rqdp->bindle = (struct snbentry *) NULL;

do {
bl = (struct snbentry *) getmem(sizeof(struct snbentry));
bl->sb_next = 0;
bl->sb_prev = 0;
if (rqdp->bindlf) {
lastbl->sb_next = bl;
bl->sb_prev = lastbl;
lastbl = bl;
} else
lastbl = rqdp->bindlf = bl;
bl->sb_a1str = packp;
bl->sb_a1dynstr = FALSE;  /* not dynamically allocated */
if (*packp++ != ASN1_SEQ ||
   (varbindlen = a1readlen(packp, &lenlen)) < 0) {
return SYSERR;
}
packp += lenlen;
bl->sb_a1slen = varbindlen;
packp += varbindlen;
} while (packp < packendp);

/* check that the entire packet has now been parsed */
if (packp != packendp) {
return SYSERR;
}
rqdp->bindle = lastbl;
return OK;
}  
技术交流:aasa2@21cn.com QQ群:10863699
cafecat
驱动牛犊
驱动牛犊
  • 注册日期2004-08-26
  • 最后登录2004-08-26
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-08-26 16:17
ft,代码不是说得很明白么
检验包的完整性的
游客

返回顶部