[Avila] Raw sockets

project2 sivakumar at deccantechnosoft.com
Thu Apr 17 00:00:02 EDT 2008


Hi !

Am Siva!
I have a raw socket code which works fine with the gateworks 2348
ethernet interfaces. But the code doesnt respond to the atheros radio
working with madwifi drivers. The data at the receiving end does  not
appear when it says send. Whereas I can receive ping packets... The code
fowards any packet comming from eth0 to ath0 and vice versa.( simple
routing) ! Please help to make it working ! Thanks in advance.

Test case 1
Machine1----- Gateworks 1eth0____Gateworks1 ath0        ---> Gateworks2
ath0____Gateworks 2eth0----- Machine2
1)Ping 1 to 2 not working
2) ping Gateworks 1 ath0 to Machine 2 working  eg: ping -I ath0
192.168.1.10.
3)ping Gateworks 2 ath0 to Machine 1 working  eg: ping -I ath0
192.168.1.11.

Test case 2
Machine1----- Gateworks 1eth0_____Gateworks 1eth1----- Machine2
change in code change all ath0 to eth1
1)Ping 1 to 2  working fine

Thanks in advance 
Sivakumar Mohan

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<features.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<linux/ip.h>
#include<string.h>

struct sockaddr_ll packet_info;
struct ifreq ifr;        // This guy helps ioctls to do conversions to
interface name and index,both ways !
int raw;
struct packet_mreq pr;



int CreateRawSocket(int protocol_to_sniff)
{
    int rawsock;

    if((rawsock = socket(PF_PACKET, SOCK_RAW,
htons(protocol_to_sniff)))== -1)
    {
        perror("Error creating raw socket: ");
        exit(-1);
    }
    strncpy((char *)ifr.ifr_name, "eth0", IFNAMSIZ);
    if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
    {
        printf("Error getting Interface index !\n");
        exit(-1);
    }



    pr.mr_ifindex = ifr.ifr_ifindex;
    pr.mr_type = PACKET_MR_PROMISC;

    setsockopt(rawsock, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
           &pr, sizeof(struct packet_mreq));

    strncpy((char *)ifr.ifr_name, "ath0", IFNAMSIZ);
    if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
    {
        printf("Error getting Interface index !\n");
        exit(-1);
    }

    pr.mr_ifindex = ifr.ifr_ifindex;
    pr.mr_type = PACKET_MR_PROMISC;


    setsockopt(rawsock, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
           &pr, sizeof(struct packet_mreq));
    return rawsock;
}

int BindRawSocketToInterface(char *device, int rawsock, int protocol)
{
    
    
    /* First Get the Interface Index  */
    strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
    if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
    {
        printf("Error getting Interface index !\n");
        exit(-1);
    }



    /* Bind our raw socket to this interface */

    packet_info.sll_family = AF_PACKET;
    packet_info.sll_ifindex = ifr.ifr_ifindex;
    packet_info.sll_protocol = htons(protocol); 
    

    //if((bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))== -1)
    //{
    //    perror("Error binding raw socket to interface\n");
    //    exit(-1);
    //}

    return 1;
    
}

void PrintPacketInHex(unsigned char *packet, int len)
{
    unsigned char *p = packet;

    printf("\n\n---------Packet---Starts----\n\n");
    
    while(len--)
    {
        printf("%.2x ", *p);
        p++;
    }

    printf("\n\n--------Packet---Ends-----\n\n");

}


PrintInHex(char *mesg, unsigned char *p, int len)
{
    printf(mesg);

    while(len--)
    {
        printf("%.2X ", *p);
        p++;
    }

}


ParseEthernetHeader(unsigned char *packet, int len)
{
    struct ethhdr *ethernet_header;

    if(len > sizeof(struct ethhdr))
    {
        ethernet_header = (struct ethhdr *)packet;

        /* First set of 6 bytes are Destination MAC */

        PrintInHex("Destination MAC: ", ethernet_header->h_dest, 6);
        printf("\n");
        
        /* Second set of 6 bytes are Source MAC */

        PrintInHex("Source MAC: ", ethernet_header->h_source, 6);
        printf("\n");

        /* Last 2 bytes in the Ethernet header are the protocol it
carries */

        PrintInHex("Protocol: ",(void *)&ethernet_header->h_proto, 2);
        printf("\n");

        
    }
    else
    {
        printf("Packet size too small !\n");
    }
}
char * Load_MyMAC( void )
{
    int i=0,j;
    char c;
    FILE* ptr;
    static char MAC [20], MyMAC[20];
    char * MCMPMyMAC = ( char *) malloc ( sizeof( char ) * 19 );
    system("ifconfig |grep eth0  |cut -c39-55 >MACADDR.txt");
    ptr=fopen("MACADDR.txt","r");
    if((fgets(MAC,18, ptr)) == (void *)-1){
        printf( " That was an error ");
        exit(0);
    }
    //printf(" \n MAC: %s \n",MAC);
    for( i = 1, j = 0; i < 18 ; i++ )
    {
        if(( i % 3 ) != 0 ){ 
            if( MAC[i-1] < 0x40   ){

                MyMAC[j++] = MAC[i-1] - 0x30;
            }
            else{
                MyMAC[j++] = MAC[i-1] - 0x37;
            }
        }
    }
    for( i = 0, j = 0; i < 12 ; i+=2 )
    { 
        
            MCMPMyMAC[j++] =  (MyMAC[i] << 4) | MyMAC[ i+ 1 ];
    }
    printf(" \n My MAC: %s \n",MAC);
 return ( MCMPMyMAC );    
}    
int sendthepacket(char *device, int rawsock, int protocol, char
*MCMPFrame_TX,int  MCMPFrameSize_TX)
{
    int send_result;
    struct sockaddr_ll sll;
    struct ifreq ifr;

    bzero(&sll, sizeof(sll));
    bzero(&ifr, sizeof(ifr));
    
    /* First Get the Interface Index  */

    
    strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
    if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
    {
        printf("Error getting Interface index !\n");
        exit(-1);
    }

    /* Bind our raw socket to this interface */

    sll.sll_family = AF_PACKET;
    sll.sll_ifindex = ifr.ifr_ifindex;
    sll.sll_protocol = htons(protocol); 
    printf("\n The sending device is: %s\n", device );
    
    send_result = sendto(raw, MCMPFrame_TX,MCMPFrameSize_TX, 0,(struct
sockaddr*)&sll, sizeof(sll) );//len+14 //ETH_DATA_LEN1+14

    if ( (send_result == -1) || ( send_result != MCMPFrameSize_TX) ){ 
        perror( " Sending: " );
        printf("\n MCMP TRANSMIT ERROR 2 \n");
        
    }
    else
        printf(" \npacket send \n");
    
}

ParseIpHeader(unsigned char *packet, int len)
{
    struct ethhdr *ethernet_header;
    struct iphdr *ip_header;
    char *    destip ;
    /* First Check if the packet contains an IP header using
       the Ethernet header                                */

    ethernet_header = (struct ethhdr *)packet;
    //bzero(ethernet_header, sizeof(struct ethhdr));
    printf("Header: %x\n", ntohs(ethernet_header->h_proto));

    
        
            
            //bzero(ip_header, sizeof(struct iphdr));
            /* print the Source and Destination IP address */

            if(ntohs(ethernet_header->h_proto) == ETH_P_IP){
                if(len >= (sizeof(struct ethhdr) + sizeof(struct
iphdr))){

                ip_header = (struct iphdr*)(packet + sizeof(struct
ethhdr));
                printf("Source IP address: %s\n",
inet_ntoa(ip_header->saddr));
                printf("TTL = %d\n", ip_header->ttl);
                destip = (char *) inet_ntoa(ip_header->daddr);
                printf("Dest IP address: %s\n",destip  );
                }

                else{    /* Not an IP packet */
                    printf("IP packet too small");
                }
            }

                //"eth0"
                

    
}


main(int argc, char **argv)
{
    
    unsigned char packet_buffer[2048]; 
    int len;
    int packets_to_sniff;
    char * device;
    int packet_info_size = sizeof(packet_info);
    
    bzero(&packet_info, sizeof(packet_info));
    bzero(&ifr, sizeof(ifr));


    /* create the raw socket */

    raw = CreateRawSocket(ETH_P_ALL);

    /* Bind socket to interface */

    

    /* Get number of packets to sniff from user */

    packets_to_sniff = atoi(argv[2]);

    /* Start Sniffing and print Hex of every packet */
    
    while(packets_to_sniff--)
    {
        packet_info_size = sizeof(packet_info);
        if((len = recvfrom(raw, packet_buffer, 2048, 0, (struct
sockaddr*)&packet_info, &packet_info_size)) == -1)
        {
            perror("Recv from returned -1: ");
            exit(-1);
        }
        else
        {
        
            ifr.ifr_ifindex = packet_info.sll_ifindex;
        if((ioctl(raw, SIOCGIFNAME, &ifr)) == -1)
        {
            perror(" Ioctl ");
            exit(-1);
        }

        printf("\n This interface is: %s \n",ifr.ifr_name);    // And
hereis your interface name
        
            /* Packet has been received successfully !! */
            //printf("\n Test \n");
            PrintPacketInHex(packet_buffer, len);

            /* Parse Ethernet Header */
            
            ParseEthernetHeader(packet_buffer, len);
            ParseIpHeader(packet_buffer, len );
        if( ( strcmp(ifr.ifr_name,"eth0") == 0 ) ){
            /* Parse IP Header */
            device = "ath0";
        }
        else if( ( strcmp(ifr.ifr_name,"ath0") == 0 ) ){
                
            device = "eth0";

            }
        
        sendthepacket(device,raw, ETH_P_ALL,  packet_buffer,len);    
        }
    }
    
    
    return 0;
}
Warm regards



Sivakumar Mohan
Associate Developer
Deccan Technosoft ( India ) PRIVATE LIMITED
Kochi, Kerala State
India.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.gateworks.com/pipermail/avila/attachments/20080417/87ba27e0/attachment-0001.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: deccan-home_01.gif
Type: image/gif
Size: 7800 bytes
Desc: not available
Url : http://lists.gateworks.com/pipermail/avila/attachments/20080417/87ba27e0/attachment-0001.gif 


More information about the Avila mailing list