Saturday, 30 August 2014

again modify to me again and send.

#include "my_proto_headers.h"

#define ETH_FRAME_LENX 98

#define ETH_HDRLEN 14  // Ethernet header length
#define IP4_HDRLEN 20  // IPv4 header length
#define ICMP_HDRLEN 8  // ICMP header length for echo request, excludes data

extern char sourceip[30];
extern char destinip[30];
extern int type_replyc;//important field to check before modifing
extern int type_reqc;//important field to check before modifing
extern int seq_num;  //important field to check before modifing
extern int smac[6];
extern int dmac[6];
extern int icmp_typec;
extern int icmp_chksumc;
extern int icmp_idc;
extern int proto_used;
extern char datagram[8096];
extern int datalen;
extern int iptotlen;
extern int iptos;
extern int ipfrag;
extern int sizeofdata;
extern int eth_prototype;
//FILE *fp;

//grandson variables
extern char sourceipxx[30];
extern char destinipxx[30];
extern int type_replycxx;//important field to check before modifing
extern int type_reqcxx;//important field to check before modifing
extern int seq_numxx;  //important field to check before modifing
extern int smacxx[6];
extern int dmacxx[6];
extern int icmp_typecxx;
extern int icmp_chksumcxx;
extern int icmp_idcxx;
extern int proto_usedxx;
extern char datagramxx[8096];
extern int datalenxx;
extern int sizeofdataxx;
extern int eth_prototypexx;
extern int icmp_seqxx;

extern int imp_flag;
//.................





//for getting interface_index number
extern int interface_index ;


char * the_fucker();
char *dup_ip;

//USING RAW SOCKET: FOR SENDING THE NEW MODIFIED PACKET



//.....................................
void create_modi_reply_pack()
{
 
 
  int rd2;
  int minus;
  char copy_data[1000];
  printf("IN CREATE_MODIFIED _ REPLY_PACKET \n");
  rd2=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
  int i,j;
   int xsize;
 
 //   fp=fopen("/home/sourav/Desktop/table","r");
  
  /*
   printf("*****destinip: %s \n",destinip);
   printf("*****sourceip: %s \n",sourceip);
    printf("icmp_typec :%d \n",icmp_typec);
  */
    void *send_buffer=(void*)malloc(ETH_FRAME_LENX);//for creation of modified packet
  //  unsigned char* etherhead = send_buffer + 14; /*userdata in ethernet frame*/
    struct sockaddr_ll socket_address;
 
 
  //constructing thernet header
    struct ethhdr *eth2 = (struct ethhdr *)send_buffer;
   
   
    eth2->h_source[0]=dmac[0];
    eth2->h_source[1]=dmac[1];
    eth2->h_source[2]=dmac[2];
    eth2->h_source[3]=dmac[3];
    eth2->h_source[4]=dmac[4];
    eth2->h_source[5]=dmac[5];
 
  // for(i=0,j=0;i<6,j<6;i++,j++)
  //{
    eth2->h_dest[0]=smacxx[0];
    eth2->h_dest[1]=smacxx[1];
    eth2->h_dest[2]=smacxx[2];
    eth2->h_dest[3]=smacxx[3];
    eth2->h_dest[4]=smacxx[4];
    eth2->h_dest[5]=smacxx[5];
   
   
    //}
  eth2->h_proto=eth_prototype;

 
 
 
 
 
 
 
 
  //socket_address********************************************************************************************************
 
  socket_address.sll_family   = PF_PACKET;   
/*we don't use a protocoll above ethernet layer
  ->just use anything here*/
socket_address.sll_protocol = htons(ETH_P_IP);   


socket_address.sll_ifindex  = interface_index;

/*ARP hardware identifier is ethernet*/
socket_address.sll_hatype   = 0;
   
/*target is another host*/
socket_address.sll_pkttype  = 0;

/*address length*/
socket_address.sll_halen    = ETH_ALEN;

/*MAC - begin*/
socket_address.sll_addr[0]  = smacxx[0];       
socket_address.sll_addr[1]  = smacxx[1];       
socket_address.sll_addr[2]  = smacxx[2];
socket_address.sll_addr[3]  = smacxx[3];
socket_address.sll_addr[4]  = smacxx[4];
socket_address.sll_addr[5]  = smacxx[5];
/*MAC - end*/
socket_address.sll_addr[6]  = 0x00;/*not used*/
socket_address.sll_addr[7]  = 0x00;/*not used*/

 
 //******************************************************************************************
 
 
  //printf("source address : %.x-%.x-%.x-%.x-%.x-%.x \n",eth2->h_source[0],eth2->h_source[1],eth2->h_source[2],eth2->h_source[3],eth2->h_source[4],eth2->h_source[5]); 
  //printf("destination address : %.x-%.x-%.x-%.x-%.x-%.x \n", eth2->h_dest[0] ,eth2->h_dest[1],eth2->h_dest[2],eth2->h_dest[3],eth2->h_dest[4],eth2->h_dest[5]); 
   
    struct iphdr *iph2 = (struct iphdr *)(send_buffer + sizeof(struct ethhdr));
      //constructiing new ip header
        iph2->version=4;
       iph2->ihl=5;
        iph2->tos=iptos;
        iph2->ttl=64;
        iph2->tot_len=sizeof(struct iphdr)+sizeof(struct icmpheader);
        iph2->id=htons(54321);
        iph2->frag_off=0x00;
        //iph2->ident=htons(54321);
        iph2->protocol=1;//for icmp
        printf("ok 0 \n");
       // dup_ip=the_fucker()    ;
       // printf("dup_ip: %s \n",dup_ip);
        iph2->saddr=inet_addr(sourceipxx);//source add changed
        iph2->daddr=inet_addr(destinipxx);
        iph2->check=in_cksum((unsigned short *)iph2, sizeof(struct iphdr));
       printf("ok 2 \n");
       
 struct icmpheader *icmphdr2=(struct icmpheader *)(send_buffer+sizeof(struct iphdr)+sizeof(struct ethhdr));       
        //constructing the icmp header
     
        icmphdr2->icmp_code=0;
        icmphdr2->icmp_id=icmp_idc;
        icmphdr2->icmp_seq=icmp_seqxx+2;
        icmphdr2->icmp_type= 0; //icmp_typec;
        icmphdr2->icmp_cksum= 0;
       
        xsize=sizeof(struct icmpheader)+sizeofdata;
      
         
       
        //copying the older datagram
          minus=(sizeof(struct icmpheader)+sizeof(struct iphdr)+sizeof( struct ethhdr));
        bzero(copy_data,1000);
        printf("datagram in create_modi_pack reply: %s : \n",datagram);
        strcpy(copy_data,datagram);
        strcpy(send_buffer+datalen,copy_data);
         icmphdr2->icmp_cksum=in_cksum((unsigned short *)icmphdr2, xsize);

        
        
        //sending this packet through the second socket
       
       
         if(sendto(rd2, send_buffer, ETH_FRAME_LENX, 0, (struct sockaddr *)&socket_address, sizeof(socket_address)) < 0)
         {
         
           printf("ok 3 \n");
           perror("sendto");
          }
          else
          {
        printf("server %d :\n",socket_address.sll_ifindex);
       
        printf("send successs");
          }
       
       close(rd2);
   


}

/*the fucker function
char * the_fucker()
{
 
  int offset;
  char * ip_string;
  char *str_chr;

  if(fp<= 0)
  {
    perror("fopen");
  }
  else
    printf("file got: fp= %d \n",fp);
 
  if(fscanf(fp,"%s",ip_string))
  {
    printf("yes \n");
    printf("%s \n",ip_string);
    return ip_string;
 
  }
  else
    printf("fscanf failed \n");
    fclose(fp);
    return 0;
 
}
*/

create_modi_pack

#include "my_proto_headers.h"

#define ETH_FRAME_LENX 98

#define ETH_HDRLEN 14  // Ethernet header length
#define IP4_HDRLEN 20  // IPv4 header length
#define ICMP_HDRLEN 8  // ICMP header length for echo request, excludes data

extern char sourceip[30];
extern char destinip[30];
extern int type_replyc;//important field to check before modifing
extern int type_reqc;//important field to check before modifing
extern int seq_num;  //important field to check before modifing
extern int smac[6];
extern int dmac[6];
extern int icmp_typec;
extern int icmp_chksumc;
extern int icmp_idc;
extern int proto_used;
extern char datagram[8096];
extern int datalen;
extern int iptotlen;
extern int iptos;
extern int ipfrag;
extern int sizeofdata;
extern int eth_prototype;
FILE *fp;

extern int imp_flag;



//for getting interface_index number
extern int interface_index ;


char * the_fucker();
char *dup_ip;

//USING RAW SOCKET: FOR SENDING THE NEW MODIFIED PACKET

//checksum for icmp
// Build IPv4 ICMP pseudo-header and call checksum function.

unsigned short in_cksum(unsigned short *addr, int len)
{
    register int sum = 0;
    u_short answer = 0;
    register u_short *w = addr;
    register int nleft = len;
  
    /*
     * Our algorithm is simple, using a 32 bit accumulator (sum), we add
     * sequential 16 bit words to it, and at the end, fold back all the
     * carry bits from the top 16 bits into the lower 16 bits.
     */
    while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }
    /* mop up an odd byte, if necessary */
    if (nleft == 1)
    {
      *(u_char *) (&answer) = *(u_char *) w;
      sum += answer;
    }
    /* add back carry outs from top 16 bits to low 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);       /* add hi 16 to low 16 */
    sum += (sum >> 16);               /* add carry */
    answer = ~sum;              /* truncate to 16 bits */
    return (answer);
}

//.....................................
void create_modi_pack()
{
 
 
  int rd2;
  int minus;
  char copy_data[1000];
  printf("IN CREATE_MODIFIED _PACKET \n");
  rd2=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
  int i,j;
   int xsize;
 
   imp_flag=0;//very important flag
   
   // fp=fopen("/home/sourav/Desktop/table","r");
  
  /*
   printf("*****destinip: %s \n",destinip);
   printf("*****sourceip: %s \n",sourceip);
    printf("icmp_typec :%d \n",icmp_typec);
  */
    void *send_buffer=(void*)malloc(ETH_FRAME_LENX);//for creation of modified packet
  //  unsigned char* etherhead = send_buffer + 14; /*userdata in ethernet frame*/
    struct sockaddr_ll socket_address;
 
 
  //constructing thernet header
    struct ethhdr *eth2 = (struct ethhdr *)send_buffer;
   
   
    eth2->h_source[0]=smac[0];
    eth2->h_source[1]=smac[1];
    eth2->h_source[2]=smac[2];
    eth2->h_source[3]=smac[3];
    eth2->h_source[4]=smac[4];
    eth2->h_source[5]=smac[5];
 
  // for(i=0,j=0;i<6,j<6;i++,j++)
  //{
    eth2->h_dest[0]=0xff;
    eth2->h_dest[1]=0xff;
    eth2->h_dest[2]=0xff;
    eth2->h_dest[3]=0xff;
    eth2->h_dest[4]=0xff;
    eth2->h_dest[5]=0xff;
   
   
    //}
  eth2->h_proto=eth_prototype;

 
 
 
 
 
 
 
 
  //socket_address********************************************************************************************************
 
  socket_address.sll_family   = PF_PACKET;   
/*we don't use a protocoll above ethernet layer
  ->just use anything here*/
socket_address.sll_protocol = htons(ETH_P_IP);   


socket_address.sll_ifindex  = interface_index;

/*ARP hardware identifier is ethernet*/
socket_address.sll_hatype   = 0;
   
/*target is another host*/
socket_address.sll_pkttype  = 0;

/*address length*/
socket_address.sll_halen    = ETH_ALEN;

/*MAC - begin*/
socket_address.sll_addr[0]  = 0xff;       
socket_address.sll_addr[1]  = 0xff;       
socket_address.sll_addr[2]  = 0xff;
socket_address.sll_addr[3]  = 0xff;
socket_address.sll_addr[4]  = 0xff;
socket_address.sll_addr[5]  = 0xff;
/*MAC - end*/
socket_address.sll_addr[6]  = 0x00;/*not used*/
socket_address.sll_addr[7]  = 0x00;/*not used*/

 
 //******************************************************************************************
 
 
  //printf("source address : %.x-%.x-%.x-%.x-%.x-%.x \n",eth2->h_source[0],eth2->h_source[1],eth2->h_source[2],eth2->h_source[3],eth2->h_source[4],eth2->h_source[5]); 
  //printf("destination address : %.x-%.x-%.x-%.x-%.x-%.x \n", eth2->h_dest[0] ,eth2->h_dest[1],eth2->h_dest[2],eth2->h_dest[3],eth2->h_dest[4],eth2->h_dest[5]); 
   
    struct iphdr *iph2 = (struct iphdr *)(send_buffer + sizeof(struct ethhdr));
      //constructiing new ip header
        iph2->version=4;
       iph2->ihl=5;
        iph2->tos=iptos;
        iph2->ttl=64;
        iph2->tot_len=sizeof(struct iphdr)+sizeof(struct icmpheader);
        iph2->id=htons(54321);
        iph2->frag_off=0x00;
        //iph2->ident=htons(54321);
        iph2->protocol=1;//for icmp
        printf("ok 0 \n");
       // dup_ip=the_fucker()    ;
       // printf("dup_ip: %s \n",dup_ip);
        iph2->saddr=inet_addr("49.205.64.1");//source add changed
        iph2->daddr=inet_addr("8.8.8.8");//google
        iph2->check=in_cksum((unsigned short *)iph2, sizeof(struct iphdr));
       printf("ok 2 \n");
       
 struct icmpheader *icmphdr2=(struct icmpheader *)(send_buffer+sizeof(struct iphdr)+sizeof(struct ethhdr));       
        //constructing the icmp header
     
        icmphdr2->icmp_code=0;
        icmphdr2->icmp_id=icmp_idc;
        icmphdr2->icmp_seq=seq_num+2;
        icmphdr2->icmp_type= 8; //icmp_typec;
        icmphdr2->icmp_cksum= 0;
       
        xsize=sizeof(struct icmpheader)+sizeofdata;
      
         
       
        //copying the older datagram
          minus=(sizeof(struct icmpheader)+sizeof(struct iphdr)+sizeof( struct ethhdr));
        bzero(copy_data,1000);
        printf("datagram in create_modi_pack: %s : \n",datagram);
        strcpy(copy_data,datagram);
        strcpy(send_buffer+datalen,copy_data);
         icmphdr2->icmp_cksum=in_cksum((unsigned short *)icmphdr2, xsize);

        
        
        //sending this packet through the second socket
       
       
         if(sendto(rd2, send_buffer, ETH_FRAME_LENX, 0, (struct sockaddr *)&socket_address, sizeof(socket_address)) < 0)
         {
         
           printf("ok 3 \n");
           perror("sendto");
          }
          else
          {
        printf("server %d :\n",socket_address.sll_ifindex);
       
        printf("send successs");
          }
       
       close(rd2);
   


}

//the fucker function
char * the_fucker()
{
 
  int offset;
  char * ip_string;
  char *str_chr;

  if(fp<= 0)
  {
    perror("fopen");
  }
  else
    printf("file got: fp= %d \n",fp);
 
  if(fscanf(fp,"%s",ip_string))
  {
    printf("yes \n");
    printf("%s \n",ip_string);
    return ip_string;
 
  }
  else
    printf("fscanf failed \n");
    fclose(fp);
    return 0;
 
}

simple type wireshark

#include "my_proto_headers.h"
//#include <net/if.h>

//       #include <netinet/in.h>
  //     #include <arpa/inet.h>

extern void create_modi_pack();
extern void create_modi_reply_pack();
void check_interface();
/*to monitor all packets i need to get all the packets from the ethernet interface..for that i need a raw socket to get access to the datalink layer
 * (i.e. the device interface) */
// this is only for eth1 interface(make only one interface up)
//1st socket: to receive and then send
//2nd socket :to send and revceive

#define ETH_FRAME_LENX 98

char sourceip[30];
char destinip[30];
int type_replyc=0;//important field to check before modifing
int type_reqc=8;//important field to check before modifing
int seq_num;  //important field to check before modifing
int smac[6];
int dmac[6];
int icmp_typec;
int icmp_chksumc;
int icmp_idc;
int proto_used;
char datagram[8096];
int datalen;
int sizeofdata;
int eth_prototype;

//commmon
int iptotlen;
int iptos;
int ipfrag;


//global_variable for interface index number
int interface_index;

//grandson variables
char sourceipxx[30];
char destinipxx[30];
int type_replycxx=0;//important field to check before modifing
int type_reqcxx=8;//important field to check before modifing
int seq_numxx;  //important field to check before modifing
int smacxx[6];
int dmacxx[6];
int icmp_typecxx;
int icmp_chksumcxx;
int icmp_idcxx;
int proto_usedxx;
char datagramxx[8096];
int datalenxx;
int sizeofdataxx;
int eth_prototypexx;
int icmp_seqxx;

int imp_flag=0;
//.................



int main()
{
  int rd; //raw socket to datalink layer
 
 
  int length=0;
  socklen_t sizeeth;
  int minus;
  int proto_used;
  int i=0,j;
int cnt=0;

//...........................................................................................................
/*Required data for modification in icmp packet
 * source ip,destination ip,the whole icmp packet,source mac-address
 
  special sequence number and code to match for a specific reply.
 */
//required fields




bzero(sourceip,30);
bzero(destinip,30);

//gettting interface ifindex
check_interface();



 
 
//............................................................................................................. 
  struct sockaddr_ll sock_address;//target address
  void *buffer=(void*)malloc(ETH_FRAME_LENX);//allocating buffer for ethernet header
 
 

//unsigned char* etherhead = buffer + 14; /*userdata in ethernet frame*/






/*construction of the raw socket */
 rd=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));

  if(rd<0)
  {
    perror("socket");
  }
 
 

/*
sock_address.sll_family=AF_PACKET;
sock_address.sll_protocol=htons(ETH_P_IP);
sock_address.sll_ifindex  = 2;
sock_address.sll_hatype   = ARPHRD_ETHER;
sock_address.sll_pkttype  = PACKET_OTHERHOST;

sock_address.sll_addr[6]  = 0x00;//not used
sock_address.sll_addr[7]  = 0x00;//not used

sizeeth=sizeof(sock_address);
*/

while(1)
{
  length=0;
  length=recvfrom(rd,buffer,ETH_FRAME_LENX,0,NULL, NULL);
 
  if(length>35)
  {
    cnt++;
   
  }
  printf("packet reveived size %d :no %d \n",length,cnt);
 
//extracting the ethernet frame 
  struct ethhdr *eth=(struct ethhdr *)buffer;

 
 
 

 

 
  //printf("the ethernet header of the received packet no %d \n",cnt);
  //printf("destination address : %.x-%.x-%.x-%.x-%.x-%.x \n", eth->h_dest[0] ,eth->h_dest[1],eth->h_dest[2],eth->h_dest[3],eth->h_dest[4],eth->h_dest[5]);
 // printf("source address : %.x-%.x-%.x-%.x-%.x-%.x \n",eth->h_source[0],eth->h_source[1],eth->h_source[2],eth->h_source[3],eth->h_source[4],eth->h_source[5]);
  //printf("upper layer protocol in the ethernet frame: %d \n",eth->h_proto);

  for(i=0,j=0;i<6,j<6;i++,j++)
  {
    smac[i]=eth->h_source[j];
  }
   //printf("source address : %.x-%.x-%.x-%.x-%.x-%.x \n",smac[0],smac[1],smac[2],smac[3],smac[4],smac[5]);
 
   for(i=0,j=0;i<6,j<6;i++,j++)
  {
    dmac[i]=eth->h_dest[j];
   
  }
 
  eth_prototype=(int)eth->h_proto;
 //extracting ip header

  struct iphdr *iph=(struct iphdr *) (buffer+sizeof(struct ethhdr));
  /*
  printf("ip header length: %d \n",(int )iph->ihl);
  printf("ip header id %d \n",(int )iph->id);
  printf("protocl encapsulated in the ip header %d \n",(int )iph->protocol);
 
  printf("ip version %d \n",(int )iph->version);
  printf("ip type of serveice %d \n",(int )iph->tos);
  printf("ip frag_offset %d \n",(int )iph->frag_off);
  printf("ip header checksum %d \n",ntohs(iph->check));
  printf("ip total length :%d \n",(int )iph->tot_len);
  printf("destination ip %s \n",inet_ntoa(iph->daddr));
  printf("*****destinip: %s \n",destinip);
  printf("source ip %s \n",inet_ntoa(iph->saddr));
  printf("*****sourceip: %s \n",sourceip);
*/
  ipfrag=(int )iph->frag_off;
  iptos=(int )iph->tos;
  iptotlen=(int )iph->tot_len;
 strcpy(sourceip,(char *)inet_ntoa(iph->saddr));
  strcpy(destinip,(char *)inet_ntoa(iph->daddr));
  proto_used=(int )iph->protocol;

//checking type of protocol it is using(icmp/tcp/udp/......etc)
switch(proto_used)
{
  case 1: printf("it is an icmp packet **** \n");
      struct icmpheader *icmphdr=(struct icmpheader *)(buffer+sizeof(struct iphdr)+sizeof(struct ethhdr));
     /*
      printf("icmp type :%d \n",(int)icmphdr->icmp_type);
      printf("icmp_code:%d \n",(int)icmphdr->icmp_code);
      printf("icmp_seq:%d \n",(int)icmphdr->icmp_seq);
   
      printf("icmp_cksum %d \n",(int)icmphdr->icmp_cksum);
      printf("icmp_id:%d \n",(int)icmphdr->icmp_id);
    */
   
        icmp_idc=(int)icmphdr->icmp_id;
        icmp_chksumc=(int)icmphdr->icmp_cksum;
        icmp_typec=(int)icmphdr->icmp_type;
        printf("icmp_typec :%d \n",icmp_typec);
        seq_num=(int)icmphdr->icmp_seq;
      //finding the data out
      minus=(sizeof(struct icmpheader)+sizeof(struct iphdr)+sizeof( struct ethhdr));
     // printf("minus : %d \n",minus);
     printf("total header size(icmpheader+iphdr+ethhdr)= %d +data(%d)\n",minus,length-minus);
      datalen=minus;
     minus=length-minus;
     sizeofdata=minus;
      printf("the DATAGRAM \n");
      printf("DATGRAM: %x \n",(int)buffer+minus);
      strcpy(datagram,buffer+minus);
      printf("datagram %s :\n",datagram);
      //check the source ipp look for 192.168.2.43 //testing 192.168.2.8
      //strcmp(sourceip,"192.168.2.25")
      printf("sourceip: %s \n",sourceip);
      printf("destinationip : %s \n",destinip);
     
      if(icmp_typec==8 && !(strcmp(sourceip,"49.205.90.202")))
      {
        imp_flag=1;
        //printf("inside if TRUE *****************************************************************************************\n");
        printf("success a request packet\n");
        //............................................grandson variables initialisation.....................
      if(imp_flag==1)
      { 
          for(i=0,j=0;i<6,j<6;i++,j++)
          {
            smacxx[i]=smac[j];
          }   
          strcpy(sourceipxx,sourceip);
          strcpy(destinipxx,destinip);
          icmp_seqxx=seq_num;
      }
     //..................................................grandson variables initialisation ends................
        //logic for modifiying the received packet
       
        //constructing ethernet header
        //check if nedded: sourav
        create_modi_pack();
      }
      else
      {
        printf(" if condition modi reply FAILED   ****\n");
      }
     
//changing the modified packet reply

      if(icmp_typec==0 && !(strcmp(sourceip,"49.205.64.1")))
      {
        //printf("inside if TRUE *****************************************************************************************\n");
        printf("success a reply packet\n");
        //logic for modifiying the received_icmp_reply packet
       
        //check if nedded: sourav
        create_modi_reply_pack();
      }
      else
      {
        printf(" if condition modi_reply FAILED   ****\n");
      }



     
     
        break;
     
  case 17:printf("it is a udp packet ****\n");
      break;
  case 6:printf("its a tcp packet ***\n");
      break;
 
  default:break;
 
}

printf("\n");
}
  cnt=0;
}



////*****program for finding the interface:/its ifindex:/etc
  //1st check for eth1

void check_interface()
 {

   struct ifreq ifr;
   int fd;
  
   if((fd=socket(PF_PACKET,SOCK_RAW,0))<0)
    {
    perror("socket");
    }
//finding ifindex of eth1   
strncpy(ifr.ifr_name,"eth1",sizeof("eth1"));
if(ioctl(fd,SIOCGIFFLAGS,&ifr)<0)
    {
    perror("ioctl");
    }
//printf("ifr_index :%d \n",(int)ifr.ifr_ifindex);   

      if((ifr.ifr_flags & IFF_UP))
      {
           if(ioctl(fd,SIOCGIFINDEX,&ifr)<0)
           {
          perror("ioctl");
           }
            interface_index=ifr.ifr_ifindex;
        printf("INDEX number:  %d \n",interface_index);
       
       }
      else
      {
        printf("ETH1 IS DOWN \n");
      }


//for interface eth1
strncpy(ifr.ifr_name,"eth0",sizeof("eth0"));
    if(ioctl(fd,SIOCGIFFLAGS,&ifr)<0)
        {
        perror("ioctl");
        }
    //printf("ifr_flags :%d ",ifr.ifr_flags);
    if((ifr.ifr_flags & IFF_UP))
    {
       
           if(ioctl(fd,SIOCGIFINDEX,&ifr)<0)
           {
          perror("ioctl");
           }
            interface_index=ifr.ifr_ifindex;
        printf("INDEX number:  %d \n",interface_index);
    }
    else
    {
      printf("ETH0 IS DOWN \n");
    }
     
 close(fd);
      
}







2nd header file

#include"4juneraw.h"
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <unistd.h>
#include<strings.h>
#include <string.h>
#include<linux/sockios.h>
#include <linux/if.h>
//#include <arpa/inet.h>
#include <net/ethernet.h>


header 1st

the headers...

4juneraw.h (headders.h)

#include<stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>




//the ip header structure
struct iphdrr
{
 unsigned char iph_ihl:5;
 unsigned char iph_ver:4;
 unsigned char      iph_tos;
 unsigned short int iph_len;
 unsigned short int iph_ident;
 unsigned char      iph_flag;
 unsigned short int iph_offset;
 unsigned char      iph_ttl;
 unsigned char      iph_protocol;
 unsigned short int iph_chksum;
 unsigned int       iph_sourceip;
 unsigned int       iph_destip;
 
};

//tcp header structure

struct tcphdr
{
 unsigned short int th_sport;
 unsigned short int th_dport;
 unsigned int th_seq;
 unsigned int th_ack;
 unsigned char th_x2:4, th_off:4;
 unsigned char th_flags;
 unsigned short int th_win;
 unsigned short int th_sum;
 unsigned short int th_urp;
 
};




// UDP header's structure

struct udpheader {

  unsigned short int udph_srcport;
  unsigned short int udph_destport;
  unsigned short int udph_len;
  unsigned short int udph_chksum;

};

//icmp header structure

struct icmpheader {
 unsigned char icmp_type;
 unsigned char icmp_code;
 unsigned short int icmp_cksum;
/* The following data structures are ICMP type specific */
 unsigned short int icmp_id;
 unsigned short int icmp_seq;
};

//function to calculate generic checksum

/*unsigned short csum(unsigned short *buf, int nwords)

{      
        unsigned long sum;

        for(sum=0; nwords>0; nwords--)
    sum += *buf++;
    sum = (sum >> 16) + (sum &0xffff);
    sum += (sum >> 16);
    return (unsigned short)(~sum);

}
*/



I had a problem "i could only access internet only on one private ip due to my idiot admin"....:-(
so , i thought of writing a program (experiment )which could provide me internet on my ip directly.....that would be really cool..kind of nat

so i started with only ping...so that i could at least get ping from ,lets say google..
this following program is just for ping..and may contain small errors..

you can verify that..

so how it will work as per my planning:--

1.let  pc A have an ip 192.168.4.25 has internet facility gw 192.168.4.2
2.And me pc B is stupid sitting alone hopeing to get internet with ip  192.168.4.43

so what will really happen , i hope you read the program and work accordingly
....
there will be server running in 192.168.4.25 a kind of simple wireshark..debug are there so you can see all packets incomming and outgoing.

it will check incoming packets tcp,upp,icmp and tear down all ackets headers..when an icmp packet(ping) comes it will check for your ip and save some compareble data of your packet ,
and construct a new packet with your given details but changing its ip to 192.168.4.5..
hence will get a reply from google
 all these are static so u can only give on website in program and one ip..
......
when the server gets a reply from it will again construct a new packet and send it to you using previous information.

and thus u get a PING...ha hahahaha

i hope it works for tcp in udp to but data cutting and all those must be done careully coz in case of icmp(ping) data doesnt matters hence it was easy.


*) just set your default gw as 192.168.4.5