接收没问题,但是不知道如何发送trap,如果用socket 自己组包是不是不太合理
直接用snmp api 函数如何求得那??? 高分求解

解决方案 »

  1.   

    你说的好像有道理,但是事实上恰恰相反,用socket组包是最合理的,用snmp api反而很晕。当然,你首先得仔细看看trap协议格式,尤其是vblist的格式
      

  2.   

    我看msdn上系统发送trap 是用的SnmpExtensionTrap
    请问自己可以吗
      

  3.   

    楼上的人兄能否给个应用snmp trap建立连接,发送数据的例子。
    小弟很急呀。
      

  4.   

    to dinghui96(ding) :你好像连SNMP TRAP的基本常识都不知道,TRAP是UDP,要建立什么连接啊?to  GX_NET(什么都不懂) :源码如下,请指点:函数定义:BOOL SendSnmpUdp(CString m_szDstIP,CString m_szSrcIP,CStringArray& m_ValueList,int m_SpecialTrapID)调用举例:
    CStringArray AgentStart;
    AgentStart.Add("IStart=start");
    SendSnmpUdp(m_GServerIpAddr,m_GHostIP,AgentStart,50);函数源码:
    BOOL SendSnmpUdp(CString m_szDstIP,CString m_szSrcIP,CStringArray& m_ValueList,int m_SpecialTrapID)
    {
    int SectionNum = 4;
    int nStatus;
    int SectionLength = 0;
    int VbLength = 0;
    int offset = 0;
    unsigned char SNMPTrapHeader[6] = { 0x30,0x3a };
    unsigned char SNMPTrapVersion[3] = {0x02,0x01,0x00};
    unsigned char SNMPTrapCommunity[8] = {0x04,0x06,0x70,0x75,0x62,0x6c,0x69,0x63 };
    unsigned char SNMPType[6] = { 0xa4,0x2d } ;
    unsigned char SNMPEnterprises[10] = {0x06,0x08,0x2b,0x06,0x01,0x04,0x01,0x81,0x9c,0x21 };
    unsigned char SNMPAgentAddress[6] = {0x40,0x04,0x0a,0x02,0x01,0x67 };
    unsigned char SNMPGenericID[3] = {0x02,0x01,0x06 };
    unsigned char SNMPSpecificID[3] ={0x02,0x01,0x00 };
    unsigned char SNMPTimeTicks[3] = {0x43,0x01,0x00};
    unsigned char SNMPVBList[2] ={0x30,0x00 };
    unsigned char SNMPVBListName[13] = {0x30,0x00,0x06,0x09,0x2b,0x06,0x01,0x04,0x01,0x81,0x9c,0x21,0x01 };
    unsigned char SNMPVBListValue[255] ;
    unsigned char SNMPTrapPDU[1200] ;
    unsigned char SNMPTrapPDUTmp[1200] ;
    SNMPSpecificID[2]=m_SpecialTrapID;
    // BuildVbListBuf(m_ValueList,SNMPVBListName,12);
    // m_szDstIP = "192.168.0.104";
    if(CheckInWarnInfoList(m_ValueList,m_SpecialTrapID)!=1)
    return TRUE;
    int VbListLength = 0;
    BuildVbListBuf(m_ValueList,SNMPVBListName,13,VbListLength);
    memcpy(SNMPTrapPDUTmp,BuildVbListBuf(m_ValueList,SNMPVBListName,13,VbListLength),VbListLength);
    //初始化socket
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 0 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
    return FALSE;
    }
    //拼装TRAPPDU
    struct hostent *thisHost; 
    struct in_addr in; 
    char MyName[80]; 
    char *ptr; 
    if(gethostname (MyName,80)==SOCKET_ERROR) {
    WSACleanup();
    return FALSE; 
    }
    if(!(thisHost = gethostbyname(MyName))) {
    WSACleanup();
    return FALSE; 
    }
    memset((void *)&in,sizeof(in),0); 
    in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]); 
    if(!(ptr = inet_ntoa(in))) {
    WSACleanup();
    return FALSE; 
    }
    CString szTmp,szIP;
    if(m_szSrcIP.IsEmpty()){
    szIP.Format("%s",ptr);
    }else{
    szIP = m_szSrcIP;
    }
    szTmp=szIP.Left(szIP.Find("."));
    szIP= szIP.Mid(szIP.Find(".")+1);
    SNMPAgentAddress[2] = (unsigned char) atoi(szTmp);
    szTmp=szIP.Left(szIP.Find("."));
    szIP= szIP.Mid(szIP.Find(".")+1);
    SNMPAgentAddress[3] = (unsigned char) atoi(szTmp);
    szTmp=szIP.Left(szIP.Find("."));
    szIP= szIP.Mid(szIP.Find(".")+1);
    SNMPAgentAddress[4] = (unsigned char) atoi(szTmp);
    SNMPAgentAddress[5] = (unsigned char) atoi(szIP);
    VbLength = 0;
    VbLength = VbListLength;
    int TrapHeadLen,CommandTrapLen;
    if(VbLength+25<127){
    SNMPType[1] = VbLength+25;
    CommandTrapLen = 2;
    }else if(VbLength+25<256){
    SNMPType[1] = 0x81;
    SNMPType[2] = VbLength+25;
    SNMPType[3] = '\0';
    CommandTrapLen = 3;
    }else{// if(VbLength+24<512){
    SNMPType[1] = 0x82;
    SNMPType[2] = ((VbLength+25)&0xff00)>>8;
    SNMPType[3] = ((VbLength+25)&0x00ff);
    SNMPType[4] = '\0';
    CommandTrapLen = 4;
    }/*else{
    SNMPType[1] = 0x83;
    SNMPType[2] = (offset&0xff0000)>>16;
    SNMPType[3] = (offset&0x00ff00)>>8;
    SNMPType[4] = (offset&0x0000ff);
    SNMPType[5] = '\0';
    CommandTrapLen = 5;
    }*/
    int offset1 = 0;
    offset1 = 13 - 2 + CommandTrapLen;
    if(VbLength+ offset1+25<127){
    SNMPTrapHeader[1] = VbLength+ 13+25;
    SNMPTrapHeader[2] = '\0';
    TrapHeadLen = 2;
    }else if(VbLength+ offset1+25<256){
    SNMPTrapHeader[1] = 0x81;
    SNMPTrapHeader[2] = VbLength+offset1+25;
    SNMPTrapHeader[3] = '\0';
    TrapHeadLen = 3;
    }else{// if(VbLength+offset1+24<512){
    SNMPTrapHeader[1] = 0x82;
    SNMPTrapHeader[2] = ((VbLength+offset1+25)&0xff00)>>8;
    SNMPTrapHeader[3] = ((VbLength+offset1+25)&0x00ff);
    SNMPTrapHeader[4] = '\0';
    TrapHeadLen = 4;
    }/*else{
    SNMPTrapHeader[1] = 0x83;
    SNMPTrapHeader[2] = ((VbLength+offset1+24)&0xff0000)>>16;
    SNMPTrapHeader[3] = ((VbLength+offset1+24)&0x00ff00)>>8;
    SNMPTrapHeader[4] = ((VbLength+offset1+24)&0x0000ff);
    SNMPTrapHeader[5] = '\0';
    TrapHeadLen = 5;
    }*/
    offset = 0;
    memcpy(SNMPTrapPDU,SNMPTrapHeader,TrapHeadLen);
    offset += TrapHeadLen;
    memcpy(SNMPTrapPDU+offset,SNMPTrapVersion,3);
    offset += 3;
    memcpy(SNMPTrapPDU+offset,SNMPTrapCommunity,8);
    offset += 8;
    memcpy(SNMPTrapPDU+offset,SNMPType,CommandTrapLen);
    offset += CommandTrapLen;
    memcpy(SNMPTrapPDU+offset,SNMPEnterprises,10);
    offset += 10;
    memcpy(SNMPTrapPDU+offset,SNMPAgentAddress,6);
    offset += 6;
    memcpy(SNMPTrapPDU+offset,SNMPGenericID,3);
    offset += 3;
    memcpy(SNMPTrapPDU+offset,SNMPSpecificID,3);
    offset += 3;
    memcpy(SNMPTrapPDU+offset,SNMPTimeTicks,3);
    offset += 3;
    memcpy(SNMPTrapPDU+offset,SNMPTrapPDUTmp,VbLength);
    offset += VbLength;
    SNMPTrapPDU[offset+1]='\0';
    //创建socket
    SOCKADDR_IN addr_in; 
    SOCKADDR_IN addr_out; 
    SOCKET sock; 
    if ((sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET){ 
    WSACleanup();
    return FALSE; 

    u_short alport=IPPORT_RESERVED; 
    addr_in.sin_port =htons(alport);
    addr_in.sin_family = AF_INET;
    addr_in.sin_addr.s_addr = INADDR_ANY;
    memset(&(addr_in.sin_zero),0, 8);
    if (bind(sock,(LPSOCKADDR)&addr_in, sizeof(addr_in))==SOCKET_ERROR) { 
    WSACleanup();
    return FALSE;

    addr_out.sin_family = AF_INET;
    addr_out.sin_port =htons(162);
    if(m_szDstIP.IsEmpty()){
    szIP.Format("%s",ptr);
    }else{
    szIP = m_szDstIP;
    }
    addr_out.sin_addr.s_addr=  inet_addr(szIP);
    memset(&(addr_out.sin_zero),0, 8);
    sendto(sock,(char *) SNMPTrapPDU,offset,0,(struct sockaddr*)&addr_out,sizeof(addr_out));
    closesocket(sock);
    WSACleanup();
    }
      

  5.   

    unsigned char* BuildVbListBuf(CStringArray& m_ValueList,unsigned char* SNMPVBListName,int m_ListNameLength,int& m_VbLength)
    {
    int SectionLength,VbLength,offset;
    int LenByteLen = 0;
    unsigned char VbList[1000];
    unsigned char m_szTmp[255];
    unsigned char m_SectionTmp[255];
    memset(VbList,0,sizeof(VbList));
    SectionLength = 0;
    VbLength = 0;
    offset = 0;
    for(int i=0;i<m_ValueList.GetSize();i++){
    memset(m_szTmp,0,sizeof(m_szTmp));
    memcpy(m_szTmp,m_ValueList.GetAt(i),m_ValueList.GetAt(i).GetLength());
    SectionLength = m_ValueList.GetAt(i).GetLength();
    if(SectionLength<127){
    memmove(m_szTmp+2,m_szTmp,SectionLength);
    m_szTmp[0] = 0x04;
    m_szTmp[1] = SectionLength;
    SectionLength += 2;
    }else{
    memmove(m_szTmp+3,m_szTmp,SectionLength);
    m_szTmp[0] = 0x04;
    m_szTmp[1] = 0x81;
    m_szTmp[2] = SectionLength;
    SectionLength += 3;
    }
    memset(m_SectionTmp,0,sizeof(m_SectionTmp));
    if(SectionLength + m_ListNameLength <127){
    m_SectionTmp[0] = 0x30;
    m_SectionTmp[1] = SectionLength + m_ListNameLength -2;
    memcpy(m_SectionTmp+2,SNMPVBListName+2,m_ListNameLength-2);
    memcpy(m_SectionTmp+2+m_ListNameLength-2,m_szTmp,SectionLength);
    SectionLength += m_ListNameLength+2 -2;
    }else{
    memmove(m_szTmp+3,m_szTmp,SectionLength);
    m_szTmp[0] = 0x30;
    m_szTmp[1] = 0x81;
    m_szTmp[2] = SectionLength+ m_ListNameLength -2;
    memcpy(m_SectionTmp+3,SNMPVBListName+2,m_ListNameLength-2);
    memcpy(m_SectionTmp+3+m_ListNameLength-2,m_szTmp,SectionLength);
    SectionLength += m_ListNameLength + 3 -2;
    }
    memcpy(VbList+offset,m_SectionTmp,SectionLength);
    offset += SectionLength;
    }
    m_VbLength = offset;
    if(offset<127){
    memmove(VbList+2,VbList,offset);
    VbList[0] = 0x30;
    VbList[1] = offset;
    m_VbLength += 2;
    }else if(offset<256){
    memmove(VbList+3,VbList,offset);
    VbList[0] = 0x30;
    VbList[1] = 0x81;
    VbList[2] = offset;
    m_VbLength += 3;
    }else {
    memmove(VbList+4,VbList,offset);
    VbList[0] = 0x30;
    VbList[1] = 0x82;
    VbList[2] = (offset&0xff00)>>8;
    VbList[3] = (offset&0x00ff);
    m_VbLength += 4;
    }/*else{
    memcpy(VbList+5,VbList,offset);
    VbList[0] = 0x30;
    VbList[1] = 0x83;
    VbList[2] = (offset&0xff0000)>>16;
    VbList[3] = (offset&0x00ff00)>>8;
    VbList[4] = (offset&0x0000ff);
    m_VbLength += 5;
    }*/
    return VbList;
    }
      

  6.   

    njtlxm(njtlxm) 
    惭愧,惭愧,以前只是做过sock的通信,对snmp从来没研究过,请多指教。
      

  7.   

    所有的网络通信,到最后都是变成一个BUFFER,里面放着一堆的数据。
    只要你了解协议,完全可以自己编码完成任何的网络数据包的封装。
      

  8.   

    非常感谢njtlxm(njtlxm)
    我现在计不得密码了,我登陆都是系统自动登陆的,不知道怎么能得到自己的密码
    一旦找到密码,马上给分 100
      

  9.   

    在一家事业单位上班,呵呵。
    有事用CSDN的短消息与我联系
      

  10.   

    to njtlxm 怎么联系你呀
    这段代码有一些疑惑,有的接收器收不到
    说 asn length too long
      

  11.   

    能把trap 包的格式说明给我一份码
    我给你发了短信了,不知道看到没有