【问题标题】:Wireshark doesn't detect any packet sent. sendto return 0Wireshark 不会检测到任何发送的数据包。发送返回 0
【发布时间】:2012-12-10 21:01:18
【问题描述】:

我一直在尝试在以下代码中使用原始套接字发送数据包。我在互联网的某个地方找到了这段代码。我创建了自己的 ipheader 和 udp 标头。整个数据包使用原始套接字上的 sendto() 函数发送。 sendto() 返回 0。这意味着从其中发送了一个长度为 0 的数据包,因此即使是 Wireshark 也没有检测到任何数据包。我的错在哪里?

// Must be run by root lol! Just datagram, no payload/data
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

// The packet length
#define PCKT_LEN 35

// Can create separate header file (.h) for all headers' structure
// The IP header's structure
struct ipheader {
    unsigned char      iph_ihl:4, iph_ver:4;
    unsigned char      iph_tos;
    unsigned short int iph_len;
    unsigned short int iph_ident;
    unsigned char      iph_flag;
    unsigned short int iph_offset;
    unsigned char      iph_ttl;
    unsigned char      iph_protocol;
    unsigned short int iph_chksum;
    unsigned int       iph_sourceip;
    unsigned int       iph_destip;
};

// UDP header's structure
struct udpheader {
    unsigned short int udph_srcport;
    unsigned short int udph_destport;
    unsigned short int udph_len;
    unsigned short int udph_chksum;
};
// total udp header length: 8 bytes (=64 bits)

// Function for checksum calculation. From the RFC,
// the checksum algorithm is:
//  "The checksum field is the 16 bit one's complement of the one's
//  complement sum of all 16 bit words in the header.  For purposes of
//  computing the checksum, the value of the checksum field is zero."
unsigned short csum(unsigned short *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);
}

// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
    int sd;
    // No data/payload just datagram
    char buffer[PCKT_LEN];
    // Our own headers' structures
    struct ipheader *ip = (struct ipheader *) buffer;
    struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader));
    // Source and destination addresses: IP and port
    struct sockaddr_in sin, din;
    int one = 1;
    const int *val = &one;

    memset(buffer, 0, PCKT_LEN);

    if(argc != 5)
    {
        printf("- Invalid parameters!!!\n");
        printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
        exit(-1);
    }

    // Create a raw socket with UDP protocol
    sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
    if(sd < 0)
    {
        perror("socket() error");
        // If something wrong just exit
        exit(-1);
    }
    else
        printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");

    // The source is redundant, may be used later if needed
    // The address family
    sin.sin_family = AF_INET;
    din.sin_family = AF_INET;
    // Port numbers
    sin.sin_port = htons(atoi(argv[2]));
    din.sin_port = htons(atoi(argv[4]));
    // IP addresses
    sin.sin_addr.s_addr = inet_addr(argv[1]);
    din.sin_addr.s_addr = inet_addr(argv[3]);

    // Fabricate the IP header or we can use the
    // standard header structures but assign our own values.
    ip->iph_ihl = 5;
    ip->iph_ver = 4;
    ip->iph_tos = 16; // Low delay
    ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
    ip->iph_ident = htons(54321);
    ip->iph_ttl = 64; // hops
    ip->iph_protocol = 17; // UDP
    // Source IP address, can use spoofed address here!!!
    ip->iph_sourceip = inet_addr(argv[1]);
    // The destination IP address
    ip->iph_destip = inet_addr(argv[3]);

    // Fabricate the UDP header. Source port number, redundant
    udp->udph_srcport = htons(atoi(argv[2]));
    // Destination port number
    udp->udph_destport = htons(atoi(argv[4]));
    udp->udph_len = htons(sizeof(struct udpheader));
    // Calculate the checksum for integrity
    ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader));
    // Inform the kernel do not fill up the packet structure. we will build our own...
    if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
    {
        perror("setsockopt() error");
        exit(-1);
    }
    else
        printf("setsockopt() is OK.\n");

    // Send loop, send for every 2 second for 100 count
    printf("Trying...\n");
    printf("Using raw socket and UDP protocol\n");
    printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));

    int count;
    int i;
    for(count = 1; count <=20; count++)
    {
        if(i = sendto(sd, buffer, PCKT_LEN, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
        // Verify
        {
            perror("sendto() error");
            exit(-1);
        }
        else
        {
            printf("Count #%u - sendto() is OK. Data Length#%d\n", count,i);
            sleep(2);
        }
    }
    close(sd);
    return 0;
}

【问题讨论】:

  • 我对其进行了编辑以使其“更好”,但我认为一些原始空间丢失了。也许Shashi 可以编辑并使用{​​} 来标记代码而不是` `。
  • 然而,我认为 0 packet len 意味着“没有发送任何内容”,所以 wireshark 什么也看不到,因为你从来没有发送过数据包。至于为什么会这样,我不确定。
  • 不要重新发明轮子,使用libtins:D

标签: c++ c sockets wireshark raw-sockets


【解决方案1】:

啊哈!我至少得到了一部分。

i = sendto(sd, buffer, PCKT_LEN, 0, (struct sockaddr *)&amp;sin, sizeof(sin)) &lt; 0 是相同的 i = (sendto(sd, buffer, PCKT_LEN, 0, (struct sockaddr *)&amp;sin, sizeof(sin)) &lt; 0)

你可能想要: (i = sendto(sd, buffer, PCKT_LEN, 0, (struct sockaddr *)&amp;sin, sizeof(sin))) &lt; 0

您可能想要:

  1. 在您的编译器中打开警告 - 至少如果您使用 gcc,应该会在同一 if 语句中进行比较和赋值时发出警告。
  2. 使用固定代码重试。

我确定您的代码中也可能存在其他问题 - 我不是网络专家。

【讨论】:

  • 嘿..ya...对不起!!但是我仍然在wireshark上看不到任何东西..为什么会这样?
  • 我不知道。你确定你已经正确配置了wireshark吗?例如。使用正确的接口,使用正确的协议等?
【解决方案2】:

阅读这段代码并理解你在做什么以及为什么要这样做真的很难。所以我可以推荐你看看我的代码:dhcp client implementation

查看函数 getSock() 以了解套接字是如何创建的,并查看函数talker() 以了解如何形成和发送完成的数据包。

【讨论】:

  • 为什么是减号?我已经给出了很好的例子(准备使用代码)如何完全按照他的意愿去做。
  • 这有点苛刻,我同意 - 这是一个很好的答案。我给了你一票!
  • -1 来自我。是的,你做了所有这些,但它应该是一个 comment 因为它没有解决实际问题(“我的错误在哪里?”)。 @Mats Petersson:这绝对不是答案。
  • 如果代码有错误,顶部可能会给出明确的答案。另一方面,如果代码只是写了一些错误的方式或有很多错误,那么就如何解决任务给出一个很好的例子会更合适。不过没关系,你有自己的想法。
【解决方案3】:

本地 IP 标头结构错误...我的建议是包含随您的发行版提供的 IP 标头(您使用的是 linux 吗?不是吗?)。

我所做的只是包含 linux/ip.h,将 ipheader 结构引用重命名为 iphdr,并根据后一个文件中描述的结构重命名 ip 头字段。

我尝试用 tcpdump 嗅探数据包,它现在可以工作了(我没有尝试使用 wireshark,但它也必须工作)

试试这个固定代码:

// Must be run by root lol! Just datagram, no payload/data
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/udp.h>
#include <linux/ip.h>

// The packet length
#define PCKT_LEN 35

// UDP header's structure
struct udpheader {
    unsigned short int udph_srcport;
    unsigned short int udph_destport;
    unsigned short int udph_len;
    unsigned short int udph_chksum;
};
// total udp header length: 8 bytes (=64 bits)

// Function for checksum calculation. From the RFC,
// the checksum algorithm is:
//  "The checksum field is the 16 bit one's complement of the one's
//  complement sum of all 16 bit words in the header.  For purposes of
//  computing the checksum, the value of the checksum field is zero."
unsigned short csum(unsigned short *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);
}

// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
    int sd;
    // No data/payload just datagram
    char buffer[PCKT_LEN];
    // Our own headers' structures
    struct iphdr *ip = (struct iphdr *) buffer;
    struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct iphdr));
    // Source and destination addresses: IP and port
    struct sockaddr_in sin, din;
    int one = 1;
    const int *val = &one;

    memset(buffer, 0, PCKT_LEN);

    if(argc != 5)
    {
        printf("- Invalid parameters!!!\n");
        printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
        exit(-1);
    }

    // Create a raw socket with UDP protocol
    sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
    if(sd < 0)
    {
        perror("socket() error");
        // If something wrong just exit
        exit(-1);
    }
    else
        printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");

    // The source is redundant, may be used later if needed
    // The address family
    sin.sin_family = AF_INET;
    din.sin_family = AF_INET;
    // Port numbers
    sin.sin_port = htons(atoi(argv[2]));
    din.sin_port = htons(atoi(argv[4]));
    // IP addresses
    sin.sin_addr.s_addr = inet_addr(argv[1]);
    din.sin_addr.s_addr = inet_addr(argv[3]);

    // Fabricate the IP header or we can use the
    // standard header structures but assign our own values.
    ip->ihl = 5;
    ip->version = 4;
    ip->tos = 16; // Low delay
    ip->tot_len = sizeof(struct iphdr) + sizeof(struct udpheader);
    ip->id = htons(54321);
    ip->ttl = 64; // hops
    ip->protocol = 17; // UDP
    // Source IP address, can use spoofed address here!!!
    ip->saddr = inet_addr(argv[1]);
    // The destination IP address
    ip->daddr = inet_addr(argv[3]);

    // Fabricate the UDP header. Source port number, redundant
    udp->udph_srcport = htons(atoi(argv[2]));
    // Destination port number
    udp->udph_destport = htons(atoi(argv[4]));
    udp->udph_len = htons(sizeof(struct udpheader));

    // Calculate the checksum for integrity
    ip->check = csum((unsigned short *)buffer, sizeof(struct iphdr) + sizeof(struct udpheader));

    // Inform the kernel do not fill up the packet structure. we will build our own...
    if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
    {
        perror("setsockopt() error");
        exit(-1);
    }
    else
        printf("setsockopt() is OK.\n");

    // Send loop, send for every 2 second for 100 count
    printf("Trying...\n");
    printf("Using raw socket and UDP protocol\n");
    printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));

    int count;
    int i;
    for(count = 1; count <=20; count++)
    {
        if((i = sendto(sd, buffer, PCKT_LEN, 0, (struct sockaddr *)&sin, sizeof(sin))) < 0)
        // Verify
        {
            perror("sendto() error");
            exit(-1);
        }
        else
        {
            printf("Count #%u - sendto() is OK. Data Length# %d\n", count,i);
            sleep(2);
        }
    }
    close(sd);
    return 0;
}

【讨论】:

    【解决方案4】:

    我猜你是基于this example code,它有多个致命错误。它浪费了我生命中的许多小时。

    但要回答这个特定问题(并帮助其他不幸尝试使用该代码的人),阻止您在 wireshark 中看到数据包的错误就在这里:

    sin.sin_addr.s_addr = inet_addr(argv[1]);
    

    这会将sentdo()中用于发送数据包的地址设置为源地址。因此,数据包通过环回接口发送,并且无处可去。 (如果您捕获lo/loopback 接口,Wireshark 或其他捕获工具将能够看到数据包,fwiw。)

    所以这个特定程序的更正行是:

    sin.sin_addr.s_addr = inet_addr(argv[3]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2013-07-01
      相关资源
      最近更新 更多