没有使用Raw Sockets检测UDP数据包

时间:2016-09-07 06:15:49

标签: c++ linux sockets networking

我正在努力解决我现在正在组建的Raw Sockets程序。我正在开发Ubuntu 16.04并使用sys / sockets.h文件。我的Ubuntu盒子是一台虚拟机,通过我的物理Windows 10 PC NAT路由(VMWare)到我的物理网络。

我编写了以下代码并以root身份运行我的程序(以防止Raw套接字权限错误)。我从socket(),setsockopt()或sendto()命令中没有收到任何错误代码。 Sendto()返回32,表示正在发送UDP数据包。但是,Wireshark似乎没有证据表明该数据包曾被发布到网络上。

int main(){
    cout << "UDP Packet Sender\n";
    PacketArgs* p_args = new PacketArgs;
    p_args->setDestIP("192.168.204.1");
    p_args->setSrcIP("192.168.204.128");
    p_args->setDestPort(500);
    p_args->setSrcPort(500);
    p_args->setProtocol(17);
    p_args->setPacketLength(sizeof(struct ip_header)+ sizeof(struct udp_header));

    UDPArgs* udp_args = new UDPArgs;
    udp_args->setSrcPort(500);
    udp_args->setDestPort(500);
    udp_args->setLength(htons(sizeof(udp_header)));

    UDPPacket* packet = new UDPPacket(p_args,udp_args);
    for(int i = 0; i < 10; i++){    
         packet->sendPacket();
    }
}

^^ main.cpp

Packet::Packet(PacketArgs* args){
    buffer = new char[PCKT_LENGTH];
    std::memset(buffer, 0, PCKT_LENGTH);
    m_packet = (ip_header*)buffer;
    m_packet->iph_ihl = 5;
    m_packet->iph_ver = 4;
    m_packet->iph_tos = 16;
    m_packet->iph_len = args->getPacketLength();
    m_packet->iph_ident = htons(54321);
    m_packet->iph_ttl = 64;
    m_packet->iph_protocol = args->getProtocol();
    m_packet->iph_sourceip = args->getSrcIP();
    m_packet->iph_destip = args->getDestIP();

}

unsigned short Packet::calculateChecksum(size_t* buf, int nwords){
    unsigned long sum;
    for(sum=0; nwords>0; nwords--)
        sum += *buf++;
    sum = (sum >> 16) + (sum &0xffff);
    sum += (sum >> 16);
    return (unsigned short) (~sum);
}

^^ Packet.cpp

UDPPacket::UDPPacket(PacketArgs* p_args, UDPArgs* udp_args): Packet(p_args){
    m_udp = (struct udp_header*)(buffer + sizeof(struct ip_header));
    m_udp->udph_srcport = udp_args->getDestPort();
    m_udp->udph_destport = udp_args->getSrcPort();
    m_udp->udph_len = htons(sizeof(struct udp_header));
    m_packet->iph_checksum = calculateChecksum((size_t*)&m_packet, sizeof(struct ip_header) + sizeof(struct udp_header));
}

int UDPPacket::sendPacket(){
    int sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);

    struct sockaddr_in src, dest;   
    src.sin_family = AF_INET;
    dest.sin_family = AF_INET;

    src.sin_port = htons(m_udp->udph_srcport);
    dest.sin_port = htons(m_udp->udph_destport);
    src.sin_addr.s_addr = m_packet->iph_sourceip;
    dest.sin_addr.s_addr = m_packet->iph_destip;

    int one = 1;
    const int* val = &one;

    int sso = setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one));

    std::cout << "sso is: " << sso << std::endl << "sd is: " << sd << std::endl;

    int st = sendto(sd,buffer,m_packet->iph_len,0,(struct sockaddr*)&src, sizeof(src));

    std::cout << "st is: " << st << std::endl;

}

^^ UDPPacket.cpp

让我感到困惑的部分原因是,即使在Ubuntu机器上使用Wireshark进行监听也不会产生任何结果,因此看起来数据包根本不会真正离开接口。有没有人对此有任何建议?

1 个答案:

答案 0 :(得分:1)

原来在NAT模式下运行VM网络适配器导致了这个问题。一旦我切换到桥接,一切都很好。