简体   繁体   English

TCP数据包发送器错误

[英]TCP Packet Sender errors

I have been trying to create a tcp packet sender in c with the following code 我一直在尝试使用以下代码在c中创建一个tcp数据包发件人

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

int main(void){

    char buffer[1024]; 
    int packetsize[100]; 
    char* source_ip = "192.168.2.1";


struct sockaddr_in serv; 
struct iphdr *iph; 
struct tcphdr *tcph; 


int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
serv.sin_family = AF_INET;
serv.sin_port = htons(80); 
serv.sin_addr.s_addr = inet_addr ("8.8.8.8"); 

struct tcp_pseudo
{
    uint32_t src_addr;
    uint32_t dst_addr;
    uint8_t zero;
    uint8_t proto;
    uint16_t length;
} pseudohead;


struct help_checksum
{
    struct tcp_pseudo pshd;
    struct tcphdr tcphd; 
    char tcpdata[1024];
} tcp_chk_construct;

// Create Packet Header
iph.ihl = 5;
iph.version = 4;
iph.tos = 0;
iph.tot_len = sizeof(struct iphdr) + packetsize;
iph.id = rand_cmwc();
iph.frag_off = 0;
iph.ttl = MAXTTL;
iph.protocol = IPPROTO_TCP;
iph.check = 0;
iph.saddr = source_ip;
iph.daddr = serv.sin_addr.s_addr;

// Create TCP Header
tcph.source = htons (1234); 
tcph.dest = htons (80); 
tcph.seq = 0;
tcph.ack_seq = 0;
tcph.doff = 5;      
tcph.fin=0;
tcph.syn=1;
tcph.rst=0;
tcph.psh=0;
tcph.ack=0;
tcph.urg=0;
tcph.window = htons (5840); 
tcph.check = 0; 
tcph.urg_ptr = 0;


pseudohead.src_addr = iph->saddr;
pseudohead.dst_addr = iph->daddr;
pseudohead.dummy = 0;
pseudohead.proto = iph->protocol;
pseudohead.length = htons(sizeof(struct tcphdr) + packetsize);

tcp_chk_construct.pshd = pseudohead;
tcp_chk_construct.tcphd = tcph; 
memcpy(tcp_chk_construct.tcpdata, buffer, packetsize);

tcph->check = in_cksum((unsigned short *) &tcp_chk_construct, 
                          sizeof(struct tcp_pseudo) + sizeof(struct tcphdr) + packetsize);



memcpy(packetsize, (char *) &iph 
       , sizeof(iphdr)); 
memcpy(packetsize + sizeof(iphdr), (char *) &tcph 
       , sizeof(tcphdr));
memcpy(packetsize + sizeof(iphdr) + sizeof(tcphdr, buffer, packetsize);



if (sendto (sockfd,      /* our socket */
            buffer,   /* the buffer containing headers and data */
            iph->tot_len,    /* total length of our datagram */
            0,      /* routing flags, normally always 0 */
            (struct sockaddr *) &serv,   /* socket addr, just like in */
            sizeof (serv)) < 0)       /* a normal send() */
    {
        printf ("error\n");
    }
else
    {
        printf ("Packet Send \n");
    }
       }

I use the netinet/ip.h and netinet/tcp.h to make the actual tcp packet but I get the following errors when compiling my code: 我用的是netinet/ip.hnetinet/tcp.h进行实际的TCP数据包,但我的编译代码时,我得到以下错误:

error: variable has incomplete type 'struct iphdr'

struct iphdr iph;

forward declaration of 'struct iphdr'

struct iphdr iph;

error: invalid application of 'sizeof' to an incomplete type 'struct iphdr'

iph.tot_len = sizeof(struct iphdr) + packetsize;

error: no member named 'source' in 'struct tcphdr'

tcph.source = htons (1234);

error: no member named 'dest' in 'struct tcphdr'

tcph.dest = htons (80);

and so on for the tcp header. 依次类推tcp标头

One at a time. 一次一个。

warning: incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [12]' [-Wint-conversion] char source_ip = "192.168.2.1"; 警告:使用类型为'char [12]'的表达式初始化'char'的整数转换指针不兼容[-Wint-conversion] char source_ip =“ 192.168.2.1”;

This is due to the fact that you are trying to save a string literal in a char variable. 这是因为您试图将字符串文字保存在char变量中。 Change the line to 将行更改为

char* source_ip = "192.168.2.1";

warning: implicit declaration of function 'inet_addr' is invalid in C99 [-Wimplicit-function-declaration] serv.sin_addr.s_addr = inet_addr ("8.8.8.8"); 警告:函数'inet_addr'的隐式声明在C99中无效[-Wimplicit-function-declaration] serv.sin_addr.s_addr = inet_addr(“ 8.8.8.8”);

For this, include the header #include <arpa/inet.h> 为此,请包含标题#include <arpa/inet.h>


Only compilation error resolved version of your original code. 仅编译错误解决了您原始代码的版本。 Not guaranteed to run. 不保证可以运行。
[Compiler used - gcc 4.9.2 on Ubuntu 15.04] [使用的编译器-Ubuntu 15.04上的gcc 4.9.2]
Some code is commented since I could not understand its usage or found it ambiguous (eg use of 'packetsize' array variable - it is 'int' array but used in expression to calculate length using base address of array). 由于我无法理解其用法或发现其模棱两可,因此对某些代码进行了注释(例如,使用“ packetsize”数组变量-它是“ int”数组,但在表达式中用于使用数组的基地址来计算长度)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

int main(void)
{
    char buffer[1024]; 
    int packetsize[100]; 
    char *source_ip = "192.168.2.1";

    struct sockaddr_in serv; 
    struct iphdr iph; 
    struct tcphdr tcph; 

    int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
    serv.sin_family = AF_INET;
    serv.sin_port = htons(80); 
    serv.sin_addr.s_addr = inet_addr ("8.8.8.8"); 

    struct tcp_pseudo
    {
        uint32_t src_addr;
        uint32_t dst_addr;
        uint8_t zero;
        uint8_t proto;
        uint16_t length;
    } pseudohead;

    struct help_checksum
    {
        struct tcp_pseudo pshd;
        struct tcphdr tcphd; 
        char tcpdata[1024];
    } tcp_chk_construct;

    // Create Packet Header
    iph.ihl = 5;
    iph.version = 4;
    iph.tos = 0;
    //iph.tot_len = sizeof(struct iphdr) + packetsize;
    iph.tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
    iph.id = rand_cmwc();
    iph.frag_off = 0;
    iph.ttl = MAXTTL;
    iph.protocol = IPPROTO_TCP;
    iph.check = 0;
    iph.saddr = inet_addr(source_ip);
    iph.daddr = serv.sin_addr.s_addr;

    // Create TCP Header
    tcph.source = htons (1234); 
    tcph.dest = htons (80); 
    tcph.seq = 0;
    tcph.ack_seq = 0;
    tcph.doff = 5;      
    tcph.fin=0;
    tcph.syn=1;
    tcph.rst=0;
    tcph.psh=0;
    tcph.ack=0;
    tcph.urg=0;
    tcph.window = htons (5840); 
    tcph.check = 0; 
    tcph.urg_ptr = 0;

    //pseudohead.src_addr = iph.saddr;
    //pseudohead.dst_addr = iph.daddr;
    //pseudohead.dummy = 0;
    //pseudohead.proto = iph.protocol;
    //pseudohead.length = htons(sizeof(struct tcphdr) + packetsize);

    //tcp_chk_construct.pshd = pseudohead;
    //tcp_chk_construct.tcphd = tcph; 
    //memcpy(tcp_chk_construct.tcpdata, buffer, packetsize);

    //tcph.check = in_cksum((unsigned short *) &tcp_chk_construct, sizeof(struct tcp_pseudo) + sizeof(struct tcphdr) + packetsize);

    memcpy((void *)buffer, (void *) &iph , sizeof(struct iphdr)); 
    memcpy((void *)buffer + sizeof(struct iphdr), (void *) &tcph , sizeof(struct tcphdr));
    //memcpy(packetsize + sizeof(struct iphdr) + sizeof(struct tcphdr), buffer, packetsize);

    if (sendto (sockfd, buffer, iph.tot_len, 0, (struct sockaddr *) &serv, sizeof (serv)) < 0)
    {
        printf ("error\n");
    }
    else
    {
        printf ("Packet Send \n");
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM