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 11-30-2005, 02:16 PM   #1
Srikanth0210
LQ Newbie
 
Registered: Aug 2004
Location: india
Posts: 5

Rep: Reputation: 0
TCP HDR variables


hi
i m new to this network programming .i was supposed to work on raw sockets in my academics.i m trying to get familiar with it by goin thru some codes available on net i got one code which is as follows




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 error that i m getting is that
testraw.c:58: error: ‘struct tcphdr’ has no member named ‘th_sport’
testraw.c:59: error: ‘struct tcphdr’ has no member named ‘th_dport’
testraw.c:60: error: ‘struct tcphdr’ has no member named ‘th_seq’
testraw.c:61: error: ‘struct tcphdr’ has no member named ‘th_ack’
testraw.c:62: error: ‘struct tcphdr’ has no member named ‘th_x2’
testraw.c:63: error: ‘struct tcphdr’ has no member named ‘th_off’
testraw.c:64: error: ‘struct tcphdr’ has no member named ‘th_flags’
testraw.c:64: error: ‘TH_SYN’ undeclared (first use in this function)
testraw.c:64: error: (Each undeclared identifier is reported only once
testraw.c:64: error: for each function it appears in.)
testraw.c:65: error: ‘struct tcphdr’ has no member named ‘th_win’
testraw.c:66: error: ‘struct tcphdr’ has no member named ‘th_sum’
testraw.c:68: error: ‘struct tcphdr’ has no member named ‘th_u

its working fine when i change the tcph->sport to tcph->source
and changing the remaining some variables but i couldnt able to change all as i m not very much familiar with the raw sockets
if some one can help me regarding this issue will b very helpful for me in my academics assignments...
thnx in advance
 
Old 11-30-2005, 02:44 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You have two versions of IP and TCP structures. You include the right definition by using right #define before you include <netinet/tcp.h> In your case you use BSD version, so put
Code:
#define __FAVOR_BSD
at the beginning of you file (before includes).
 
Old 11-30-2005, 03:02 PM   #3
Srikanth0210
LQ Newbie
 
Registered: Aug 2004
Location: india
Posts: 5

Original Poster
Rep: Reputation: 0
thnq very much for the reply....its working fine ...now...
 
Old 11-30-2005, 05:08 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
The correct way is:
#define _BSD_SOURCE

See <features.h>
__FAVOR_BSD is defined there if _BSD_SOURCE is defined before. If you define it yourself, you may see error messages about redefinitions... GNU didn't help matters by prohibiting the direct definition of these macros.
 
  


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
YUM and .hdr ... does anyone know the specifications? _UnPrEdictAbLe_ Programming 0 08-05-2005 07:31 PM
Woody 3.0 Open Ports 1470/tcp/uaiact 1518/tcp/vpvd What for?How can I remove them? alexxxis Debian 5 07-05-2004 05:18 PM
.hdr files and .rpms ravalox Linux - Software 2 06-30-2004 09:00 PM
close port 6000/tcp 515/tcp SchwipSchwap Linux - Newbie 1 09-12-2002 08:24 AM
.hdr files sts_cat Linux - Newbie 2 04-11-2002 03:08 PM

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

All times are GMT -5. The time now is 10:32 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