ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#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.
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?
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
Distribution: Fedora Core 5 Test 2 / FreeBSD 6 / Windows XP SP2
Posts: 17
Rep:
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.
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.
#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
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?
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.