LinuxQuestions.org
Review your favorite Linux distribution.
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 01-30-2011, 03:47 AM   #1
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Rep: Reputation: 20
build ip packet with C,socket programming


Hi,
How can we build a packet using C?
we have a structure called sockaddr_in which is use to for IPv4,
so that we can define address,port and etc in this way:
Code:
struct sockaddr_in sock;
sock.sin_family=AF_INET;
sock.sin_port=htons(25);
and
..
..
does ip here:
Code:
struct ip ipheader
refer to any standard structure which is dealing with ip packet (like sockaddr_in which deals with ipv4)?
if yes
can we define ip headers this way [not using a pointer,is it even possible??]:
Code:
struct ip iph;
iph.v = 4;
iph.tos = 0;
ip.ttl = 255;
etc 
..
..
instead of this way:
Code:
struct ip *iph = (struct ip *) datagram;
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 */
  etc
  ..
thanks in advance
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-30-2011, 08:48 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It doesn't really matter how you construct the IP header, as long as it is ultimately included in the datagram that you wish to send.

I've typically seen where the datagram is allocated on the stack, with the number of bytes being allocated equal to the sum of the sizes of the IP header, the TCP (or UDP) header, and the message.

So, something like the following:
Code:
unsigned char* datagram = malloc(sizeof(struct ip) + sizeof(struct tcphdr) + messageLength);

struct ip* ip = (struct ip*) datagram;

...  /* fill in ip header */

struct tcphdr* tcph = (struct tcphdr*)(datagram + (ip->ip_hl << 2));        /* compute offset where tcp header begins */

... /* fill in tcp header */

unsigned char* msg  = datagram + (ip->ip_hl << 2) + sizeof(struct tcphdr);  /* compute offset where message begins */

... /* copy message */

... /* compute checksum(s) */

... /* send away! */

P.S. You should consider parting with some spare change to procure this book.

Last edited by dwhitney67; 01-30-2011 at 09:03 AM.
 
Old 01-30-2011, 02:34 PM   #3
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
thanks dwhitney67,
i read about pointers
i know
Code:
struct ip *iph
will make a pointer called iph but i can't just figure this out,
Code:
(struct ip*)datagram
what does
Code:
(struct ip*)
mean?

Last edited by z99; 01-30-2011 at 02:39 PM.
 
Old 01-30-2011, 03:53 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by dwhitney67 View Post
You should consider parting with some spare change to procure this book.
I agree, sort of. But as that page itself hints, buy the third edition instead.
 
Old 01-30-2011, 04:23 PM   #5
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
well,as my country is under prohibition,i can't buy from Amazon,
so would you please just give me a brief description?
 
Old 01-31-2011, 03:47 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The (struct ip*) is merely a cast, so that the datagram variable, which is declared as an unsigned char*, can be interpreted differently.
 
2 members found this post helpful.
  


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
Receive a packet from L2 header using socket murugan86 Programming 1 03-26-2009 10:48 PM
raw socket/ip packet help shouup Programming 14 04-24-2006 04:54 PM
Capturing a Socket and Injecting a Packet Chryzmo Programming 5 12-07-2005 01:17 AM
Packet socket and socket filtring Baran Linux - Newbie 4 10-09-2003 07:16 AM

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

All times are GMT -5. The time now is 12:45 PM.

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