LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 04-20-2012, 02:06 PM   #1
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Rep: Reputation: Disabled
Unhappy Sending and Receiving IP Packet


Hello everyone,

I am new with programming for networking in Linux. I know about the basics of making kernel modules and some of the concepts of related to my work (recently studied). I am working on a project where i don't have any experience but i am trying and looking from forums.

My supervisor asked me to make a program to send IP packet to the other network.

I want to struct a packet and send it from source to destination.

I have no idea about how to do this. any body can suggest me any sample program for this? I searched on internet but may be i am not well known of this field so i couldn't use the proper terms for searching. if you guys can suggest me some links related to this work than this will be great help for me..

As this is my startup in new field, so i would prefer to follow a a complete example that i can get whats going on each step. It would be better for my future work in this project.
thanks
 
Old 04-21-2012, 12:54 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,128

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Do you have to do this in kernel mode? It would be simpler to use user mode socket programming. There's lots of good documentation to do it there.

I don't know much about kernel level network programming, so I'd advise checking out the networking chapters of a Linux kernel book -- I've been partial to Robert Love's books, personally.
 
1 members found this post helpful.
Old 04-21-2012, 04:29 AM   #3
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Thank you btmiller.

and i have found a way to struct ip header and udp header and to send the packet from source to destination, for starting i think it is a good step. but i am facing a problem, after i send packet to loopback i dont receive it using sniffer.c. I am worried that either it is possible to receive sent packets on loop back or not!? if it is possible to send then why i am not receiving packet count there.

////////////////////here is my code///////////////////
Quote:
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

#define PCKT_LEN 8192



//Create the IP header
struct ipheader {
unsigned char iph_ihl:4, iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned char iph_flags;
unsigned short int iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};

//Create the UDP Header
struct udpheader {
unsigned short int udph_srcport;
unsigned short int udph_destport;
unsigned short int udph_len;
unsigned short int udph_chksum;
}; /* total udp header length: 8 bytes (= 64 bits) */


// Function for checksum calculation
unsigned short 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 (unsigned short)(~sum);
}

// Source IP, source port, target IP, target port from command line
int main(int argc, char *argv[]){
int sd;
char buffer[PCKT_LEN];
struct ipheader *ip = (struct ipheader *) buffer;
struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader));

// Source and destination addresses: IP and port
struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;
memset(buffer, 0, PCKT_LEN);

//
if(argc != 5)
{
printf("- Invalid parameters!!!\n");
printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
exit(-1);
}


// Create a raw socket with UDP protocol
sd = socket (PF_INET, SOCK_RAW, IPPROTO_UDP);

if (sd < 0)
{
perror("socket() error");
exit (-1);
}
else
printf("socket() - UDP and using raw socket is OKAY");

// Address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;

//port numbers
sin.sin_port = htons(atoi(argv[2]));
din.sin_port = htons(atoi(argv[4]));

// Fabricate the IP header
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16; // Low delay
ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
ip->iph_ident = htons(54321);
ip->iph_ttl = 64; // hops
ip->iph_protocol = 17; // UDP
ip->iph_sourceip = inet_addr(argv[1]); // source IP address
ip->iph_destip = inet_addr(argv[3]); // destination IP header

// Fabricate the UDP header
udp->udph_srcport=htons (atoi(argv[2]));
udp->udph_destport = htons(atoi(argv[4]));
udp->udph_len = htons(sizeof(struct udpheader));
ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader));

// Inform the kernel do not fill up the packet structure
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("socketpot() error");
exit (-1);
}

else
printf("socketpot() is OKAY");

printf("trying...\n");
printf("Using raw socket and UDP protocol\n");
printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));

int count;

for(count = 1; count <=20; count++)
{
if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("sendto() error");// Verify
exit(-1);
}
else
{
printf("Count #%u - sendto() is OK.\n", count);
sleep(2);
}
}
close (sd);
return 0;
}

This is perfectly fine, and sending 20 packets. But unfortunately in monitoring the packet reception i don't receive any packet for loopback.
NOTE: I am using this format for loopback correct me if i am wrong anywhere:
Quote:
# ./myProgram 127.0.0.1 22 127.127.0.0.1 8008
i have also tried give same port number for both also but its not working in that way which i want.

I am newbie if i am making some stupid mistake then please make me correct..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LINUX packet receiving process vahu002 Linux - Networking 0 12-08-2010 03:30 PM
qmail not sending or receiving zibusiso Linux - Newbie 1 10-28-2009 04:20 AM
Help...sending email but not receiving fiona333 Linux - Newbie 9 09-07-2007 08:28 AM
packet receiving munna_dude Linux - Networking 1 03-18-2007 04:05 PM
raw packet sending & receiving sjcoder Programming 7 01-11-2006 04:07 AM


All times are GMT -5. The time now is 10:37 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration