0

Hi I am trying to send some commands to a board with the help of a simple RAW socket function. Here is the interface setting I am using. In the command line I pass the interface as eth0. My code is as follows:

int CreateRawSocket(int proto)
{
    int i;
    struct timeval timeout;
    printf("\n creating a raw socket for protocol %d",proto);
    if((rawsock=socket(PF_PACKET,SOCK_RAW,htons(proto)))==-1)
    {
        perror("\n cannot create socket");
        return -1;
    }
    printf("\n setting the timeout for the raw socket equal to one microsecond");
    timeout.tv_sec = 0;
    timeout.tv_usec =1;
    setsockopt(rawsock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
    setsockopt(rawsock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
    i = 1;
    setsockopt(rawsock, SOL_SOCKET, SO_DONTROUTE, &i, sizeof(i));
    return rawsock;
}

int BindRawSocketToInterface(char *device,int proto)
{
    struct sockaddr_ll sll;
    struct ifreq ifr;
    printf("\n initiating  socket address and interface address to null");
    bzero(&sll,sizeof(sll));
    bzero(&ifr,sizeof(ifr));
    printf("\n setting up the interface to bind");
    strcpy((char *)ifr.ifr_name, device);
    if((ioctl(rawsock,SIOGIFINDEX, &ifr))==-1)
    {
        printf("\n something went wrong while resetting the interface with name");
        return -1;
    }
    //setting up the flags and index in one step-ifr.ifr_flags = 0;
    strcpy((char *)ifr.ifr_name, device);
    ifr.ifr_flags = ifr.ifr_flags || IFF_PROMISC || IFF_BROADCAST;
    printf("\n setting up the socket for this interface and protocol");
    sll.sll_family = AF_PACKET;
    sll.sll_ifindex =ifr.ifr_ifindex;
    sll.sll_protocol=htons(proto);
    printf("running ioctl");
    if((ioctl(rawsock,SIOGIFINDEX, &ifr))==-1)
    {
        printf("\n something went wrong while setting flags");
        return -1;
    }
    printf("running bind");
    int r;
    if((r = bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))==-1)
    {
        printf("\n socket is not binding properly");
        return -1;
    }
    return r;

   /*you can use this command to make it non blocking
   r = fcntl(rawsock, F_SETFL, fl | O_NDELAY);*/
}

Everthing works fine and send returns me 30 which is correct but I don't see the packet in wireshark. I am sure the wireshark settings are ok. Any suggestions?

PhilMY
  • 2,621
  • 21
  • 29
Mohit Kumar
  • 53
  • 1
  • 8
  • Are you sending ethernet frames? or something else? Maybe wireshark is set to discard non-ethernet frames? – Nim Jun 25 '12 at 08:51
  • 2
    One thing, you should be using | to combine flags and not ||. – Ambroz Bizjak Jul 03 '12 at 22:54
  • In the second ioctl(), you want SIOCSIFFLAGS and not SIOCGIFINDEX, obviously (and not SIOGIFNDEX, which is a typo that happens to work :). Also, I'm not completely sure if SIOCGIFINDEX will actually give you the flags. To change the flags I'd rather do a SIOCGIFFLAGS first. – Ambroz Bizjak Jul 03 '12 at 22:59
  • What are you sending into this socket? Your packets should begin with the 14-byte Ethernet header (destination, source, type). – Ambroz Bizjak Jul 03 '12 at 23:02

1 Answers1

1

I saw another one with a similar problem: Raw socket NOT sending

"The packets were indeed being send but the Wireshark could only show them after a gap of 6 minutes or so".

You could try to check the delay of Wireshark simply by throwing an HTTP request.

Community
  • 1
  • 1
Matteo T.
  • 1,278
  • 11
  • 13