c - Raw Socket Linux 发送/接收数据包

标签 c sockets networking send recv

收包有问题。 我可以接收和读取传入的数据包,但我认为我没有与任何主机握手。 我只想在收到答案时将数据包发送到具有开放端口的远程计算机,以查看 TTL(生存时间)和窗口大小。 有谁知道错误在哪里? (我对C编程没有很深的了解)

代码:

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

struct pseudohdr {
    u_int32_t src_addr;
    u_int32_t dst_addr;
    u_int8_t padding;
    u_int8_t proto;
    u_int16_t length;
};

struct data_4_checksum {
    struct pseudohdr pshd;
    struct tcphdr tcphdr;
    char payload[1024];
};
unsigned short comp_chksum(unsigned short *addr, int len) {
    long sum = 0;

    while (len > 1) {
        sum += *(addr++);
        len -= 2;
    }

    if (len > 0)
        sum += *addr;

    while (sum >> 16)
        sum = ((sum & 0xffff) + (sum >> 16));

    sum = ~sum;

    return ((u_short) sum);

}

int main(int argc, char *argv[]) {

    int sock, bytes, on = 1;
    char buffer[1024];
    struct iphdr *ip;
    struct tcphdr *tcp;
    struct sockaddr_in to;
    struct pseudohdr pseudoheader;
    struct data_4_checksum tcp_chk_construct;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s ", argv[0]);
        fprintf(stderr, "<dest-addr>\n");
        return 1;
    }

    sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
    if (sock == -1) {
        perror("socket() failed");
        return 1;
    }else{
        printf("socket() ok\n");
    }

    if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) == -1) {
        perror("setsockopt() failed");
        return 2;
    }else{
        printf("setsockopt() ok\n");
    }

    ip = (struct iphdr*) buffer;
    tcp = (struct tcphdr*) (buffer + sizeof(struct tcphdr));

    int iphdrlen = sizeof(struct iphdr);
    int tcphdrlen = sizeof(struct tcphdr);
    int datalen = 0;
    printf("Typecasting ok\n");

    ip->frag_off = 0;
    ip->version = 4;
    ip->ihl = 5;
    ip->tot_len = htons(iphdrlen + tcphdrlen);
    ip->id = 0;
    ip->ttl = 40;
    ip->protocol = IPPROTO_TCP;
    ip->saddr = inet_addr("192.168.165.135");
    ip->daddr = inet_addr(argv[1]);
    ip->check = 0;

    tcp->source     = htons(12345);
    tcp->dest       = htons(80);
    tcp->seq        = random();
    tcp->doff       = 5;
    tcp->ack        = 0;
    tcp->psh        = 0;
    tcp->rst        = 0;
    tcp->urg        = 0;
    tcp->syn        = 1;
    tcp->fin        = 0;
    tcp->window     = htons(65535);

    pseudoheader.src_addr = ip->saddr;
    pseudoheader.dst_addr = ip->daddr;
    pseudoheader.padding = 0;
    pseudoheader.proto = ip->protocol;
    pseudoheader.length = htons(tcphdrlen + datalen);

    tcp_chk_construct.pshd = pseudoheader;
    tcp_chk_construct.tcphdr = *tcp;

    int checksum = comp_chksum((unsigned short*) &tcp_chk_construct,
            sizeof(struct pseudohdr) + tcphdrlen + datalen);

    tcp->check = checksum;

    printf("TCP Checksum: %i\n", checksum);
    printf("Destination : %i\n", ntohs(tcp->dest));
    printf("Source: %i\n", ntohs(tcp->source));

    to.sin_addr.s_addr = ip->daddr;
    to.sin_family = AF_INET;
    to.sin_port = tcp->dest;

    bytes = sendto(sock, buffer, ntohs(ip->tot_len), 0, (struct sockaddr*) &to,
            sizeof(to));

    if (bytes == -1) {
        perror("sendto() failed");
        return 1;
    }

    recv(sock, buffer, sizeof(buffer), 0);
    printf("TTL= %d\n", ip->ttl);
    printf("Window= %d\n", tcp->window);
    printf("ACK= %d\n", tcp->ack);
    printf("%s:%d\t --> \t%s:%d \tSeq: %d \tAck: %d\n",
                    inet_ntoa(*(struct in_addr*) &ip->saddr), ntohs(tcp->source),
                    inet_ntoa(*(struct in_addr *) &ip->daddr), ntohs(tcp->dest),
                    ntohl(tcp->seq), ntohl(tcp->ack_seq));

    return 0;
}

最佳答案

  1. 您正在接收数据包并将其存储在 buffer 中,但是您正在打印来自 iptcp 的数据而不解析该缓冲区。您应该从 buffer 收到打印之前解析数据包。
  2. 您的代码假定所有数据包都是 TCP,但事实并非如此。 RAW 套接字仅支持第 3 层协议(protocol)(IP、ICMP 等)。换句话说,在创建 RAW 套接字时使用 IPPROTO_TCP 会产生误导。坚持使用 IPPROTO_IP,并为您关心的每个协议(protocol)(TCP、UDP 等)在您的代码中添加必要的条件。这恰好有效,因为 Linux 内核验证协议(protocol)号,并回退到 IPPROTO_IP。但是,这可能不适用于其他系统。
  3. 检查您的网络通信是否使用了正确的字节顺序。网络字节顺序是 Big-Endian,而主机字节顺序取决于您的架构,因此您可能需要来回转换多字节字段。
  4. 您的 tcp->seq 可能有一个无效值,因为 TCP 只接受最大 65535 的值,而 random() 返回从 0 到 RAND_MAX 的值 (0x7fffffff)。尝试 tcp->seq = htonl(random() % 65535);
  5. 您对 TCP header 的偏移量计算不正确。它应该是 sizeof(struct iphdr) 而不是 sizeof(struct tcphdr)

关于c - Raw Socket Linux 发送/接收数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088274/

相关文章:

c - "Pointer to Function"语法在 C 中如何工作?

c - 查找所有带有 miniupnp 的 UPnP 网关

networking - 我怎样才能从子网掩码计算IP地址范围

java - 安卓服务器: why it times out incoming connections randomly?

找不到C文件

php - nslookup 查询的 shell_exec 空响应

c++ - 数组作为指针

c++ - MySQL QUOTE() vs mysql_real_escape_string()?

c - 通过 SSL 套接字反向 shell

java - 套接字编程