c - TCP 数据包发送方错误

标签 c compiler-errors

我一直在尝试使用以下代码在 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");
    }
       }

我使用 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);

对于 tcp header 依此类推。

最佳答案

一次一个。

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 中。多变的。将行更改为

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");

为此,包括标题 #include <arpa/inet.h>


关于c - TCP 数据包发送方错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33315870/

相关文章:

c - 如何在没有主线程的情况下运行静态并行for循环

asp.net - appharbor 在推送后编译时出错

c++ - 错误 MSB8020 : Platform Toolset set to V140, 但错误提示找不到 V110

c - 使用 `GCCs` 预处理器作为汇编程序

c - 帮忙解决以下情况

c - 计算错误 C uint64_t

c - 如何从 C 代码生成 UML 序列图?

java - 为什么我需要在这里添加类型,有没有更好的方法?

c++ - C++ 错误 : a expected initializer before [function name]

android - Android Studio并未将minSdkVersion的不可用方法标记为错误(问题ID : NewApi)