char asnmpu[200];
char asnmpg[200];
char asnmpu1[32];
char asnmpg1[32];
//通过WINDOWS内建的SNMP读取ARP表
STDMETHODIMP Carpinfo::getarpinfo(int *num)
{
int i;
int j;
int k;
int numoid;
int ip[256][4];
char buffer[256];
SnmpVarBindList a;//是在SNMP.h中定义的结构
SnmpVarBindList b;
SnmpVarBind arpb[2];//在SNMP.h中定义的结构
AsnInteger32 a1;
AsnInteger32 a2;
BOOL lok;
DWORD addr;
DWORD addr1;
ZeroMemory(asnmpu,200);
ZeroMemory(asnmpg,200);
ZeroMemory(asnmpu1,32);
ZeroMemory(asnmpg1,32);
addr = (DWORD)asnmpu;
*(u_char *)addr = 1;
addr = addr + 4;
*(u_char *)addr = 3;
addr = addr + 4;
*(u_char *)addr = 6;
addr = addr + 4;
*(u_char *)addr = 1;
addr = addr + 4;
*(u_char *)addr= 2;
addr = addr + 4;
*(u_char *)addr= 1;
addr = addr + 4;
*(u_char *)addr= 4;
addr = addr + 4;
*(u_char *)addr= 22;
addr = addr + 4;
*(u_char *)addr= 1;
addr = addr + 4;
*(u_char *)addr= 2;
//以上是构造ARP表在SNMP MIB中的MAC ARP地址接点的OID,在WINDOWS的SNMP
请求中每个OID点是4个字节长度
arpb[0].name.idLength = 10;
//OID的长度
arpb[0].name.ids = (unsigned int *)asnmpu;
a.list = &arpb[0];
a.len = 1;
//构造了一个SnmpVarBindList请求,如果有多个SnmpVarBindList请求也可以
增加len和对应的a.list中的内容
a1 = 0;
a2 = 0;
addr = (DWORD)asnmpg;
*(u_char *)addr = 1;
addr = addr + 4;
*(u_char *)addr = 3;
addr = addr + 4;
*(u_char *)addr = 6;
addr = addr + 4;
*(u_char *)addr = 1;
addr = addr + 4;
*(u_char *)addr= 2;
addr = addr + 4;
*(u_char *)addr= 1;
addr = addr + 4;
*(u_char *)addr= 4;
addr = addr + 4;
*(u_char *)addr= 22;
addr = addr + 4;
*(u_char *)addr= 1;
addr = addr + 4;
*(u_char *)addr= 4;
//以上是构造ARP表在SNMP MIB中的ARP IP地址接点的OID,在WINDOWS的SNMP
请求中每个OID点是4个字节长度
arpb[1].name.idLength = 10;
arpb[1].name.ids = (unsigned int *)asnmpg;
b.list = &arpb[1];
b.len = 1;
* num = 0;
for(i=0;i<256;i++)
{
ZeroMemory(buffer,256);
lok=SnmpExtensionQuery(SNMP_PDU_GETNEXT,&a,&a2,&a1);
a1 = 0;
a2 = 0;
lok=SnmpExtensionQuery(SNMP_PDU_GETNEXT,&b,&a2,&a1);
numoid = arpb[0].name.idLength - 14;
addr= (DWORD)a.list->name.ids;
addr1 = (DWORD)asnmpg+40+4*numoid;
ip[i][0]=*(int *)(addr+40+4*numoid);
ip[i][1]=*(int *)(addr+44+4*numoid);
ip[i][2]=*(int *)(addr+48+4*numoid);
ip[i][3]=*(int *)(addr+52+4*numoid);
if(ip[i][0]==0 && ip[i][1]==0 && ip[i][2]==0 && ip[i][3]==0)
break;
if(i>0)
{
if(ip[i][0]==ip[0][0] && ip[i][1]==ip[0][1] && ip[i][2]==ip[0][2]
&& ip[i][3]==ip[0][3])
{
* num = i;
m_arpnum = i;
break;
}
}
addr = (DWORD)a.list ->name.ids ;
j = sprintf(buffer,"%d.%d.%d.%d=",*(int *)(addr+44),*(int *)(addr+48)
,*(int *)(addr+52),*(int *)(addr+56));
addr = (DWORD)a.list ->value.asnValue.string.stream ;
k = sprintf(buffer+j,"%02x-%02x-%02x-%02x-%02x-%02x",*(u_char *)(addr+0)
,*(u_char *)(addr+1),*(u_char *)(addr+2),*(u_char *)(addr+3),*(u_char *)
(addr+4),*(u_char *)(addr+5));
addr = (DWORD)(b.list) ->value.asnValue.number;
if(addr==4)
sprintf(buffer+j+k,"=static");
else if(addr==3)
sprintf(buffer+j+k,"=dynamic");
else
sprintf(buffer+j+k,"=status:%d",addr);
m_arpinfo[i] = buffer;
}
return S_OK;
}
//改变ARP表内容,如果是一个不存在的IP则是增加一条ARP记录
STDMETHODIMP Carpinfo::changearp
(BSTR ip, BSTR mac, int arptype, long mactype)
{
char arpinfo[4][200];
int i;
u_char ipstr[4];
u_char macstr[6];
SnmpVarBindList a;
SnmpVarBind arpb[4];
AsnInteger32 a1;
AsnInteger32 a2;
BOOL lok;
char ipstr1[10];
char macstr1[14];
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,ip,-1,ipstr1,10,NULL,NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,mac,-1,macstr1,14,NULL,NULL);
for(i=0;i<4;i++)
{
ipstr[i]=hextoint(ipstr1[2*i+0])*16+hextoint(ipstr1[2*i+1]);
}
for(i=0;i<6;i++)
{
macstr[i]=hextoint(macstr1[2*i+0])*16+hextoint(macstr1[2*i+1]);
}
for(i=0;i<4;i++)
{
ZeroMemory(arpinfo[i],200);
arpinfo[i][0]=1;
arpinfo[i][4]=3;
arpinfo[i][8]=6;
arpinfo[i][12]=1;
arpinfo[i][16]=2;
arpinfo[i][20]=1;
arpinfo[i][24]=4;
arpinfo[i][28]=22;
arpinfo[i][32]=1;
arpinfo[i][36]=i+1;
*(unsigned long *)(arpinfo[i]+40)=mactype;
arpinfo[i][44]=ipstr[0];
arpinfo[i][48]=ipstr[1];
arpinfo[i][52]=ipstr[2];
arpinfo[i][56]=ipstr[3];
arpb[i].name.idLength = 15;
arpb[i].name.ids = (unsigned int *)arpinfo[i];
if(i==0)
{
arpb[i].value.asnType = 2;
arpb[i].value.asnValue.number = mactype;
}
if(i==1)
{
arpb[i].value.asnType = 4;
arpb[i].value.asnValue.string.stream = macstr;
arpb[i].value.asnValue.string.length = 6;
arpb[i].value.asnValue.string.dynamic = 0;
}
if(i==2)
{
arpb[i].value.asnType = 0x40;
arpb[i].value.asnValue.address.stream = ipstr;
arpb[i].value.asnValue.address.length = 4;
arpb[i].value.asnValue.address.dynamic = 0;
}
if(i==3)
{
arpb[i].value.asnType = 2;
arpb[i].value.asnValue.number = arptype;
}
}
//对ARP表的修改涉及到4个OID接点,分别进行处理,asnType代表了接点类型的格式
a.list = &arpb[0];
a.len = 4;
a1 = 0;
a2 = 0;
lok=SnmpExtensionQuery(SNMP_PDU_SET,&a,&a1,&a2);
return S_OK;
}
int Carpinfo::hextoint(char hexc)
{
if (ascchar<='9') return(ascchar-'0');
else if(ascchar<='F') return(ascchar-'A'+10);
else return(ascchar-'a'+10);
}
|