int PingSend(void)

来源:互联网 发布:135端口 编辑:程序博客网 时间:2024/05/16 07:09
int PingSend(void)
{
    static uchar mac[6];
    volatile IP_t *ip;
    volatile ushort *s;
    uchar *pkt;

    /* XXX always send arp request */

    memcpy(mac, NetEtherNullAddr, 6);                        //mac={0,0,0,0,0,0}

#ifdef ET_DEBUG
    printf("sending ARP for %08lx\n", NetPingIP);
#endif

    NetArpWaitPacketIP = NetPingIP;                             //目标ip
    NetArpWaitPacketMAC = mac;                                //目标mac=0

    pkt = NetArpWaitTxPacket;                                     //指向NetArpWaitTxPacket
    pkt += NetSetEther(pkt, mac, PROT_IP);                 //设置帧头(目标mac,本机mac,协议)目标mac=0

    ip = (volatile IP_t *)pkt;

    /*
     *    Construct an IP and ICMP header.  (need to set no fragment bit - XXX)
     */
    ip->ip_hl_v  = 0x45;        /* IP_HDR_SIZE / 4 (not including UDP) */  //协议版本4,协议头=5个字
    ip->ip_tos   = 0;                                                                                 //数据报优先级=0
    ip->ip_len   = htons(IP_HDR_SIZE_NO_UDP + 8);                             //数据报长度
    ip->ip_id    = htons(NetIPID++);                                                          //数据报编号
    ip->ip_off   = htons(0x4000);    /* No fragmentation */                      //分片设置
    ip->ip_ttl   = 255;                                                                                //数据报寿命
    ip->ip_p     = 0x01;        /* ICMP */                                                      //上层协议是ICMP
    ip->ip_sum   = 0;                                                                                //首部校验和暂设为0
    NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */      //本机ip地址
    NetCopyIP((void*)&ip->ip_dst, &NetPingIP);       /* - "" - */                                        //目标ip地址
    ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);                       //首部校验和计算

    s = &ip->udp_src;        /* XXX ICMP starts here */
    s[0] = htons(0x0800);        /* echo-request, code */
    s[1] = 0;            /* checksum */
    s[2] = 0;             /* identifier */
    s[3] = htons(PingSeqNo++);    /* sequence number */
    s[1] = ~NetCksum((uchar *)s, 8/2);

    /* size of the waiting packet */
    NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;    //ARP等待的数据报的长度

    /* and do the ARP request */
    NetArpWaitTry = 1;                                    //重试次数
    NetArpWaitTimerStart = get_timer(0);        //获取当前时间
    ArpRequest();
    return 1;    /* waiting */
}
原创粉丝点击