From: neha agrawal <nehavrce@yahoo.co.in>
To: linux-kernel@vger.kernel.org
Subject: RAW socket prgmming
Date: Thu, 6 Jan 2005 03:55:22 +0000 (GMT) [thread overview]
Message-ID: <20050106035522.46252.qmail@web8404.mail.in.yahoo.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1187 bytes --]
hello!
i want to implement smtp protocol on the same
machine.i have a libpcap trace file..in which i have
captured mail traffic..(single session).and i want to
develop a program which can read this trace file..and
communicate with the smtp server...
first packet is Sync packet in trace file..so i
want to send it to smtp server...and then i want my
progrem to handle the reply sent by server..(say it
sent Syn-Ack) anf then my prgm should reply
accordingly..
for doing this i am using PF_PACKET , SOCK_RAW
and using sendto function...i am sending the
packet..im able to see it through tcpdump...but
theserver is not replying...why??i am wrking with
linux Redhat 9 kernel 2.4.20-8
(...if u r familiar with flowreplay tool..i
want to do something similar ...but mine is single
session..so less complicated...)
attching source code...
do let me know..if i am on correct track...
thnks
Neha
________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony
[-- Attachment #2: tcp1.c --]
[-- Type: application/octet-stream, Size: 3266 bytes --]
#include "tcp.h"
#include<sys/ioctl.h>
#include<net/if.h>
#define PROTO htons(ETH_P_ALL)
int main()
{
//unsigned char packet[74];
struct sockaddr_ll mysocket,to,from;
int fromlen;
struct tcphdr *tcp;
int sockd, sd,on = 1;
unsigned char rbuf[1500];
int i,n;
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);
/* capture */
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("Packet type %d\n",from.sll_pkttype);
printf("Packet hatype %d\n",from.sll_hatype);
eptr = (struct ether_header *) rbuf;
if (ntohs (eptr->ether_type) == ETHERTYPE_IP)
{
printf("Ethernet type hex:%x dec:%d is an IP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}
else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)
{
printf("Ethernet type hex:%x dec:%d is an ARP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}
else
{
printf("Ethernet type %x not IP", ntohs(eptr->ether_type));
exit(1);
}
ip = (struct iphdr *)rbuf;
printf("Version %d\n",ip->version);
printf("Protocol %d\n",ip->protocol);
//printf("Source IP: %s \t Destn IP: %s \n",inet_ntoa(ip->saddr) , inet_ntoa(ip->daddr));
tcp = (struct tcphdr *) rbuf;
printf("Souce %04d and destn port %04d\n", ntohs(tcp->source), ntohs(tcp->dest));
exit(0);
}
[-- Attachment #3: tcp.h --]
[-- Type: application/octet-stream, Size: 385 bytes --]
#include <stdlib.h>
#include <stdio.h>
#include<pcap.h>
#include<errno.h>
//#include <libnd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <netinet/if_ether.h>
#include<netpacket/packet.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
reply other threads:[~2005-01-06 3:55 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050106035522.46252.qmail@web8404.mail.in.yahoo.com \
--to=nehavrce@yahoo.co.in \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®