#include "tcp.h" #include #include #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); }