Networking Forums

Networking Forums > Computer Networking > Linux Networking > send and receive raw packets with PF_PACKET and bypassing the kernel

Reply
Thread Tools Display Modes

send and receive raw packets with PF_PACKET and bypassing the kernel

 
 
nehavrce@yahoo.co.in
Guest
Posts: n/a

 
      01-06-2005, 07:37 AM
hi!
i want to send and receive raw packets(etherne hdr + ip+ tcp +
payload) to port 25..and handle the incoming packets sent by port 25
myself bypassing the kernel..iam using PF_PACKET and RAW_SOCK as i want
to send and receive ..
folowing code sends the sync packet(whicj i am reading from trace
file...) and i see the packet going to network by tcpdump..but i am
seeing any syn-ack packet coming back...
please do help...
im having hard time since weeks..
also i dont need to worry that the Sync-ack packet which will be sent
by server ..may get handled by kernal..as PF_PACKET ensures direct
passing of packet to the application... am i correct???

please........do guide..

Thanks


source....


int main()
{

struct sockaddr_ll mysocket,to,from;
int fromlen;
struct tcphdr *tcp;
int sockd, i,n,sd,on = 1;
unsigned char rbuf[1500];
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr; /* pcap.h */
struct ether_header *eptr; /* net/ethernet.h */
u_char *ptr; /* printing out hardware header info */
struct iphdr *ip;
struct ifreq ifr;

descr = pcap_open_offline("/root/s1.trace",errbuf);
if(descr == NULL)
{
printf("pcap_open_offline(): %s\n",errbuf);
exit(1);
}
packet = pcap_next(descr,&hdr);
if(packet == NULL)
{
printf("Didn't grab packet\n");
exit(1);
}
if((sockd = socket(PF_PACKET,SOCK_RAW,PROTO)) < 0)
{
perror("socket");
exit(1);
}

strncpy(ifr.ifr_name, "lo", sizeof(ifr.ifr_name));
if (ioctl(sockd, SIOCGIFINDEX, &ifr) == -1)
{

fprintf(stderr, "SIOCGIFINDEX on %s failed: %s\n",
"lo",strerror(errno));
printf("my error\n");
return 1;
}
memset(&mysocket,'\0',sizeof(mysocket));
mysocket.sll_family = AF_PACKET;
mysocket.sll_protocol =htons(ETH_P_ALL);
mysocket.sll_ifindex = ifr.ifr_ifindex;

if (bind(sockd,(struct sockaddr *)&mysocket, sizeof(mysocket)) <0)
{
perror("Bind");
exit(1);
}
printf("Bind succecced \n");

memset(&to , 0,sizeof(to));
to.sll_family = AF_PACKET;
to.sll_protocol =htons(ETH_P_ALL);
to.sll_ifindex = ifr.ifr_ifindex;
to.sll_halen = ETH_ALEN;
memcpy(to.sll_addr,packet,ETH_ALEN);


ip = (struct iphdr *) packet;
printf("Id1 %d\n",ip->ttl);
ip->id=htons(getuid());
printf("ID2 %d\n",(ip->id));
n = sendto(sockd,packet,74,0x0,(struct sockaddr *)&to,sizeof(to));
if(n < 0)
{
perror("sendto");
exit(1);
}
else printf("Packet send %d bytes \n",n);


printf("Now recev\n");
bzero(rbuf,sizeof(rbuf));
fromlen = sizeof(from);
n = recvfrom(sockd,rbuf,sizeof(rbuf),0,(struct sockaddr
*)&from,&fromlen);

if(n < 0) printf("ERROR\n");
else printf("RECEived %dbytes\n",n);

printf("Souce %04d and destn port %04d\n", ntohs(tcp->source),
ntohs(tcp->dest));

exit(0);
}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get data in kernel SEND buffer for a TCP socket from a netfilter based kernel module Rohit Linux Networking 0 05-10-2007 11:27 AM
Ex-wanadoo - can now receive but not send in OE Rose28 Broadband 4 09-04-2006 08:38 PM
I can send packets but I receive nothing, routing problem riviereg Linux Networking 8 09-06-2004 12:49 PM
I can send packets but I receive nothing, routing problem riviereg Linux Networking 0 09-03-2004 09:44 AM
Can't send only receive! Magnus Wireless Internet 1 05-31-2004 11:25 PM



1 2 3 4 5 6 7 8 9 10 11