* RAW socket prgmming
@ 2005-01-06 3:55 neha agrawal
0 siblings, 0 replies; only message in thread
From: neha agrawal @ 2005-01-06 3:55 UTC (permalink / raw)
To: linux-kernel
[-- 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>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2005-01-06 3:55 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-06 3:55 RAW socket prgmming neha agrawal
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®