当我运行时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_
#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
Rawudp.c
#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;
}
我相信我已经包含了正确的头文件,但是无法找到sockaddr不包含在头文件中的原因。我也尝试过sockaddr_in,仍然没有骰子。
最佳答案
如您所说,感谢所有帮助人员。
“sockaddr_in”不是typedef
使用它时,您需要这样指定(也在函数声明中指定)
struct sockaddr_in foo;
int bar(struct sockaddr_in);