LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2010, 02:54 AM   #1
nirucool89
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Rep: Reputation: 0
Problem wid RAW Sockets


Hello.
this is the code that i made for the raw socket at the client side..bt wen i run this code and try to capture the packet through wireshak there is no packet captured...there is no error in the code...

#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/udp.h>
#include<stdio.h>
#include<unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>

struct ipheader
{
unsigned char iph_ihl,iph_ver;
unsigned char iph_tos;
unsigned short iph_len;
unsigned short iph_ident;
unsigned char iph_flag;
unsigned short iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};

struct udpheader
{
unsigned short udph_srcport;
unsigned short udph_destport;
unsigned short udph_len;
unsigned short udph_chksum;
};

struct psd_udp {
struct in_addr src;
struct in_addr dst;
unsigned char pad;
unsigned char proto;
unsigned short udp_len;
struct udpheader udp;
};

unsigned short in_cksum(unsigned short *addr, int len)
{
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;

while (nleft > 1) {
sum += *w++;
nleft -= 2;
}

if (nleft == 1) {
*(unsigned char *) (&answer) = *(unsigned char *) w;
sum += answer;
}

sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}

unsigned short in_cksum_udp(int src, int dst, unsigned short *addr, int len)
{
struct psd_udp buf;

memset(&buf, 0, sizeof(buf));
buf.src.s_addr = src;
buf.dst.s_addr = dst;
buf.pad = 0;
buf.proto = IPPROTO_UDP;
buf.udp_len = htons(len);
memcpy(&(buf.udp), addr, len);
return in_cksum((unsigned short *)&buf, 12 + len);
}

int main(int argc,char **argv)
{
int sd;
char *buffer;
struct ipheader ip;
struct udpheader udp;
struct sockaddr_in sin;
int one = 1,i=0;
const int *val = &one;
buffer=(char*)malloc(60);
if(argc != 6)
{

printf("- Invalid parameters!!!\n");

printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
exit(1);
}
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = atoi(argv[4]);
sin.sin_addr.s_addr = inet_addr(argv[3]);
ip.iph_ihl = 5;
ip.iph_ver = 4;
ip.iph_tos =0;
ip.iph_len =60;
ip.iph_chksum=0;
ip.iph_chksum=in_cksum((unsigned short *)&ip, sizeof(ip));
ip.iph_ident = htons(54321);
ip.iph_offset=0;
ip.iph_flag=0;
ip.iph_ttl = 64; // hops
ip.iph_protocol = 17;
ip.iph_sourceip = inet_addr(argv[1]);
ip.iph_destip = inet_addr(argv[3]);
memcpy(buffer,(char*)&ip,sizeof(ip));
printf("%d \t %d \t %d \t %d\n",sizeof(ip),sizeof(udp),strlen(buffer),sizeof(buffer));
udp.udph_srcport = htons(atoi(argv[2]));
udp.udph_destport = htons(atoi(argv[4]));
udp.udph_len = htons(8);
udp.udph_chksum=0;
udp.udph_chksum=in_cksum_udp(ip.iph_sourceip,ip.iph_destip,(unsigned short *)&udp,sizeof(udp));
memcpy(buffer+sizeof(ip),(char*)&udp,sizeof(udp));
printf("%d \t %d \t %d \t %d\n",sizeof(ip),sizeof(udp),strlen(buffer),sizeof(buffer));
sd=socket(PF_INET,SOCK_RAW,IPPROTO_RAW);
if(sd < 0)
{
perror("socket() error");
exit(1);
}
else
printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("setsockopt() error");
exit(1);
}
else
printf("setsockopt() is OK.\n");
printf("%d\n",sizeof(buffer));
if(sendto(sd,buffer,60,0,(struct sockaddr *)&sin,sizeof(struct sockaddr))<0)
printf("\n Error in sending:");
else printf("\n sent %s",argv[5]);
close(sd);
return 0;
}

sumbudy tell me wat modifications i shud made in the above code...
plzz any one help me...thnx in advance..!
 
Old 06-08-2010, 04:00 AM   #2
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Hi

Welcome to LQ!

The reason that your post is not getting responses is due to the way you're asking your question. Please read the LQ Rules and "How to ask Questions the Smart Way" - see the sticky at the top of this forum.

What you've posted is a pretty substantial C program, and most of the members will not have the time to analyze your code and find the problem for you.

At LQ we encourage people to help themselves; with the assistance of the community. My advice to you is to identify the section of code that is giving you trouble; and explain exactly what the symptoms/problem itself is.
 
Old 06-08-2010, 11:26 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Raw sockets require root privileges. Do you have that? If so, then tell us why you think there is no error in the code. There is an error in the way you posted your code. Please use [CODE] tags, especially for a large volume of code like you have posted.

By the way, you are asking people for help, and at some level that implies that the people are knowledgeable and probably educated. Those people usually like to read language that can be found in a dictionary.

--- rod.

Last edited by theNbomr; 06-08-2010 at 11:32 AM.
 
  


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
raw sockets in c wee-face Programming 1 02-01-2007 10:35 AM
Raw Sockets Srikanth0210 Programming 2 12-05-2005 04:22 AM
Problem sniffing with raw sockets warned Programming 4 08-28-2004 02:10 PM
raw sockets and C wrongman Programming 3 05-04-2004 03:17 PM
raw sockets BashTin Programming 1 06-07-2003 07:34 AM


All times are GMT -5. The time now is 10:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration