分享一段SNMP的代码,顺带列出几个不解之处

ynwlgh ynwlgh 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
codeproject上来的代码,用于获取ARP列表



// Function: GetEntries: Read ARP table for specific NIC interface.
//第一,二个参数用于获取数据
int GetEntries(arpTable* pTable, int tableLen, int adapterIndex)
{
UINT OID[3][10];

//三个OID。
for(int i=0;i<3;i++)
{
OID[i][0] = 1;
OID[i][1] = 3;
OID[i][2] = 6;
OID[i][3] = 1;
OID[i][4] = 2;
OID[i][5] = 1;
OID[i][6] = 4;
OID[i][7] = 22;
OID[i][8] = 1;

switch(i)
{
case 0:
// Adapter interface
OID[i][9] = 1;
break;

case 1:
// MAC address
OID[i][9] = 2;
break;

case 2:
// Entry Type
OID[i][9] = 4;
break;
}
}

ZeroMemory(pTable,sizeof(arpTable)*tableLen);


SnmpVarBindList SVBList[3];
SnmpVarBind SVBVars[3];

AsnObjectIdentifier AsnOID0={sizeof(OID[0])/sizeof(UINT),OID[0]};
AsnObjectIdentifier AsnOID1={sizeof(OID[1])/sizeof(UINT),OID[1]};
AsnObjectIdentifier AsnOID2={sizeof(OID[2])/sizeof(UINT),OID[2]};

SVBList[0].len=1;
SVBList[0].list=&SVBVars[0];
SnmpUtilOidCpy(&SVBVars[0].name,&AsnOID0);

SVBList[1].len=1;
SVBList[1].list=&SVBVars[1];
SnmpUtilOidCpy(&SVBVars[1].name,&AsnOID1);

SVBList[2].len=1;
SVBList[2].list=&SVBVars[2];
SnmpUtilOidCpy(&SVBVars[2].name,&AsnOID2);

int iEntries=0;
AsnInteger32 aiErrorStatus[3], aiErrorIndex[3];

do
{
aiErrorStatus[0]=0;
aiErrorIndex[0]=0;
aiErrorStatus[1]=0;
aiErrorIndex[1]=0;
aiErrorStatus[2]=0;
aiErrorIndex[2]=0;

//SnmpExtensionQuery的函数指针
if(!pfunSnmpExtensionQuery(SNMP_PDU_GETNEXT,&SVBList[0],&aiErrorStatus[0],&aiErrorIndex[0]))
continue; //@@@@@@@ 1
if(!pfunSnmpExtensionQuery(SNMP_PDU_GETNEXT,&SVBList[1],&aiErrorStatus[1],&aiErrorIndex[1]))
continue;
if(!pfunSnmpExtensionQuery(SNMP_PDU_GETNEXT,&SVBList[2],&aiErrorStatus[2],&aiErrorIndex[2]))
continue;

if(aiErrorStatus[0]!=SNMP_ERRORSTATUS_NOERROR
||aiErrorStatus[1]!=SNMP_ERRORSTATUS_NOERROR
||aiErrorStatus[2]!=SNMP_ERRORSTATUS_NOERROR)
break;

if(SnmpUtilOidNCmp(&SVBVars[0].name,&AsnOID0,AsnOID0.idLength))
break;
if(SnmpUtilOidNCmp(&SVBVars[1].name,&AsnOID1,AsnOID1.idLength))
break;
if(SnmpUtilOidNCmp(&SVBVars[2].name,&AsnOID2,AsnOID2.idLength))
break;


if(SVBList[0].list->value.asnValue.number==adapterIndex)
{
unsigned long pIPAddress;
unsigned long pMACAddress;

//获得ARP IP
pIPAddress=(unsigned long)SVBList[1].list->name.ids;

pTable[iEntries].IPAddress[0]=*(unsigned char*)(pIPAddress+44); //@@@@@@@@@@@@@ 2
pTable[iEntries].IPAddress[1]=*(unsigned char*)(pIPAddress+48);
pTable[iEntries].IPAddress[2]=*(unsigned char*)(pIPAddress+52);
pTable[iEntries].IPAddress[3]=*(unsigned char*)(pIPAddress+56);

//获得MAC
pMACAddress= (unsigned long)SVBList[1].list->value.asnValue.string.stream;
if (pMACAddress)
{
pTable[iEntries].MACAddress[0] = *(unsigned char *)(pMACAddress + 0);
pTable[iEntries].MACAddress[1] = *(unsigned char *)(pMACAddress + 1);
pTable[iEntries].MACAddress[2] = *(unsigned char *)(pMACAddress + 2);
pTable[iEntries].MACAddress[3] = *(unsigned char *)(pMACAddress + 3);
pTable[iEntries].MACAddress[4] = *(unsigned char *)(pMACAddress + 4);
pTable[iEntries].MACAddress[5] = *(unsigned char *)(pMACAddress + 5);
}


// 获得ARP类型,动态/静态。。
pTable[iEntries].Type = (unsigned long)SVBList[2].list->value.asnValue.number;

// Type must be one of (1, 2, 3, 4),确认类型是否有效
if (pTable[iEntries].Type>=1 && pTable[iEntries].Type<=4)
iEntries++; // Move to next array position

}

} while (iEntries<tableLen);

//清理工作
// Frees the memory allocated for the specified object identifiers
SnmpUtilOidFree(&SVBVars[2].name);
SnmpUtilOidFree(&SVBVars[1].name);
SnmpUtilOidFree(&SVBVars[0].name);

return iEntries; // Return number of Entries
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
ynwlgh
网络编程 18,356 成员 64,161 内容