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);
}