Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未知类型名称‘;sockaddr’;_C_Linux_Sockets - Fatal编程技术网

未知类型名称‘;sockaddr’;

未知类型名称‘;sockaddr’;,c,linux,sockets,C,Linux,Sockets,当我跑的时候 cc-c-o rawudp.o rawudp.c 我明白了 在rawudp.c中包含的文件中:1:0: rawudp.h:48:23:错误:未知类型名称“sockaddr” make:[rawudp.o]错误1 这是我使用的头文件 rawudp.h \ifndef RAWUDP\H_ #定义RAWUDP_H_值 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 //数据包长度 #定义PCKT_LEN 8192 //IP头常量 #定义IP_头_IHL 5

当我跑的时候

cc-c-o rawudp.o rawudp.c

我明白了

在rawudp.c中包含的文件中:1:0:
rawudp.h:48:23:错误:未知类型名称“sockaddr”
make:[rawudp.o]错误1

这是我使用的头文件

rawudp.h
\ifndef RAWUDP\H_
#定义RAWUDP_H_值
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//数据包长度
#定义PCKT_LEN 8192
//IP头常量
#定义IP_头_IHL 5;
#定义IP_头_版本4;
#定义IP_头_TOS 16;
#定义IP_头_TTL 64;
#定义IP_头_协议17;
typedef结构ipheader{
未签名字符iph\u ihl:5,iph\u版本:4;
未签名字符iph_tos;
无符号短整数iph_len;
无符号短整数识别码;
无符号字符iph_标志;
无符号短整数iph_偏移量;
无符号字符iph_ttl;
无符号字符iph_协议;
无符号短整数iph_chksum;
未签名的int-iph\u-sourceip;
未签名的int iph_destip;
}ipheader;
//UDP报头的结构
类型定义结构udpheader{
无符号短int-udph\u srcport;
未签名的短int-udph\u destport;
无符号短int-udph_len;
无符号短int-udph_-chksum;
}udpheader;
#恩迪夫
rawudp.c
#包括“rawudp.h”
//用于校验和计算的函数。来自RFC,
//校验和算法是:
//“校验和字段是1的16位1的补码
//报头中所有16位字的补码和。用于
//计算校验和时,校验和字段的值为零。”
无符号短csum(无符号短*buf,整数nwords)
{       //
无符号长和;
对于(总和=0;nwords>0;nwords--)
总和+=*buf++;
总和=(总和>>16)+(总和&0xffff);
总和+=(总和>>16);
返回值(无符号短)(~sum);
}
//使用UDP协议创建原始套接字
int udp_socket_init(){
int-socket\u描述符;
套接字描述符=套接字(PF_INET、SOCK_RAW、IPPROTO_UDP);
if(套接字描述符<0)
{
perror(“套接字()错误”);
//如果有什么不对劲就退出
出口(-1);
}
其他的
返回套接字描述符;
//printf(“socket()-使用SOCK_原始套接字和UDP协议是可以的。\n”);
}
int ip_socket_init(sockaddr_in*udp_socket,char*端口,char*ip_地址){
udp_套接字->sin_family=AF_INET;
//转换并设置端口号
如果(!(udp_套接字->sin_端口=htons(atoi(端口))){
printf(“无法设置端口号\n”);
出口(-1);
}
//转换和设置IP地址
如果(!(udp_套接字->sin_addr.s_addr=inet_addr(ip_地址)))
{
printf(“无法设置IP地址\n”);
出口(-1);
}
返回1;
}
int ip_header_init(ipheader*ip,char*source\u ip,char*dest\u ip,size\t size){
//制作IP头或者我们可以使用
//标准的头结构,但分配我们自己的值。
ip->iph\U ihl=ip\U头\U ihl;
ip->iph\u ver=ip\u头\u版本;
ip->iph\u tos=ip\u头\u tos;//低延迟
ip->iph_len=尺寸;
ip->iph_ident=htons(54321);
ip->iph\u ttl=ip\u头\u ttl;//跳数
ip->iph_协议=ip_头_协议;//UDP
//源IP地址,可以在这里使用伪造地址!!!
ip->iph\u源ip=inet\u addr(源ip);
//目标IP地址
ip->iph_destip=inet_addr(dest_ip);
}
int udp_header_init(udpheader*udp,char*source_端口,char*dest_端口){
//制作UDP标头。源端口号,冗余
udp->udph_srcport=htons(atoi(源_端口));
//目的地端口号
udp->udph_destport=htons(atoi(dest_port));
udp->udph_len=htons(sizeof(struct udpheader));
}
//来自命令行参数的源IP、源端口、目标IP、目标端口
int main(int argc,char*argv[])
{
int-socket\u描述符;
//没有数据/有效载荷,只有数据报
字符缓冲区[PCKT_LEN];
//我们自己的头结构
结构ipheader*ip=(结构ipheader*)缓冲区;
结构udpheader*udp=(结构udpheader*)(缓冲区+sizeof(结构ipheader));
//源和目标地址:IP和端口
sin中的结构sockaddr_;
din中的结构sockaddr_;
socket_descriptor=udp_socket_init();
int-one=1;
常量int*val=&one;
bzero(缓冲器,PCKT_LEN);
如果(argc!=5)
{
printf(“-无效参数!!!\n”);
printf(“-Usage%s\n”,argv[0]);
出口(-1);
}
ip_套接字_init(&sin,argv[2],argv[1]);
ip_套接字_init(&din,argv[4],argv[3]);
ip_头_init(ip,argv[1],argv[3],sizeof(struct ipheader)+sizeof(struct udpheader));
udp_头_init(udp,argv[2],argv[4]);
//计算完整性校验和
ip->iph_chksum=csum((无符号短*)缓冲区,sizeof(struct ipheader)+sizeof(struct udpheader));
//通知内核不要填充数据包结构。我们将建立自己的。。。
if(setsockopt(套接字描述符、IPPROTO\u IP、IP\u HDRINCL、val、sizeof(一))小于0)
{
perror(“setsockopt()错误”);
出口(-1);
}
其他的
printf(“setsockopt()正常。\n”);
//发送循环,每2秒发送100次
printf(“正在尝试…\n”);
printf(“使用原始套接字和UDP协议”);
printf(“使用源IP:%s端口:%u,目标IP:%s端口:%u。\n”、argv[1]、atoi(argv[2])、argv[3]、atoi(argv[4]);
整数计数;
对于(count=1;count iph_len,0,(struct sockaddr*)和sin,sizeof(sin))<0)
//核实
{
perror(“sendto()错误”);
出口(-1);
#ifndef RAWUDP_H_
#define RAWUDP_H_ value

#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

// The packet length
#define PCKT_LEN 8192

//IP Header Constants
#define IP_HEADER_IHL       5;
#define IP_HEADER_VERSION   4;
#define IP_HEADER_TOS       16;
#define IP_HEADER_TTL       64;
#define IP_HEADER_PROTOCOL  17;

typedef struct ipheader {
 unsigned char      iph_ihl:5, 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;
} ipheader;

// UDP header's structure
typedef struct udpheader {
 unsigned short int udph_srcport;
 unsigned short int udph_destport;
 unsigned short int udph_len;
 unsigned short int udph_chksum;
} udpheader;
#endif
#include "rawudp.h"

// 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);
}

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

int ip_socket_init(sockaddr_in* udp_socket, char* port, char* ip_address){
        udp_socket->sin_family = AF_INET;
        //Convert and set port number
        if (!(udp_socket->sin_port = htons(atoi(port)))){
                printf("Could not set port number\n");
                exit(-1);
        }
        //Convert and set IP Address
        if (!(udp_socket->sin_addr.s_addr = inet_addr(ip_address)))
        {
                printf("Could not set IP address\n");
                exit(-1);
        }
        return 1;
}

int ip_header_init(ipheader* ip, char* source_ip, char* dest_ip, size_t size){
        // Fabricate the IP header or we can use the
        // standard header structures but assign our own values.
        ip->iph_ihl = IP_HEADER_IHL;
        ip->iph_ver = IP_HEADER_VERSION;
        ip->iph_tos = IP_HEADER_TOS; // Low delay
        ip->iph_len = size;
        ip->iph_ident = htons(54321);
        ip->iph_ttl = IP_HEADER_TTL; // hops
        ip->iph_protocol = IP_HEADER_PROTOCOL; // UDP
        // Source IP address, can use spoofed address here!!!
        ip->iph_sourceip = inet_addr(source_ip);
        // The destination IP address
        ip->iph_destip = inet_addr(dest_ip);
}

int udp_header_init(udpheader* udp, char* source_port, char* dest_port){
        // Fabricate the UDP header. Source port number, redundant
        udp->udph_srcport = htons(atoi(source_port));
        // Destination port number
        udp->udph_destport = htons(atoi(dest_port));
        udp->udph_len = htons(sizeof(struct udpheader));
}

// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
        int socket_descriptor;
        // 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;
        struct sockaddr_in din;

        socket_descriptor = udp_socket_init();


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

        bzero(buffer, 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);
        }

        ip_socket_init(&sin, argv[2], argv[1]);
        ip_socket_init(&din, argv[4], argv[3]);

        ip_header_init(ip, argv[1],  argv[3], sizeof(struct ipheader) + sizeof(struct udpheader));

        udp_header_init(udp, argv[2], argv[4]);

        // 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(socket_descriptor, 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;
        for(count = 1; count <=20; count++)
        {
                if(sendto(socket_descriptor, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                // Verify
                {
                        perror("sendto() error");
                        exit(-1);
                }
                else
                {
                        printf("Count #%u - sendto() is OK.\n", count);
                        sleep(2);
                }
        }
        close(socket_descriptor);
        return 0;
}
struct sockaddr_in foo;

int bar(struct sockaddr_in);