Raw socket with device bind using setsockopt() system is not working in Fedora core 6(2.6.18-1.2798.fc6)

前端 未结 2 1395
自闭症患者
自闭症患者 2020-12-18 12:11

Please any one could help on this issue. Please

In the below sample code,we had bind raw sock with eth0. but while running the program the recvfrom of raw sock is re

相关标签:
2条回答
  • 2020-12-18 12:38

    In the linux man page (http://linux.die.net/man/7/socket) :

    SO_BINDTODEVICE

    Bind this socket to a particular device like "eth0", as specified in the passed interface name. If the name is an empty string or the option length is zero, the socket device binding is removed. The passed option is a variable-length null-terminated interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an interface, only packets received from that particular interface are processed by the socket. Note that this only works for some socket types, particularly AF_INET sockets. It is not supported for packet sockets (use normal bind(2) there).

    So, try the bind instead.

    0 讨论(0)
  • 2020-12-18 12:48

    Thanks all,thanks you very much for valuable time. it is worked with bind approach The code segments had helped me.

    setsockopt() is bug in the fedora 2.6.18

    alternative approach is below.

    void BindToInterface(int raw , char *device , int protocol) { 
        struct sockaddr_ll sll;
        struct ifreq ifr; bzero(&sll , sizeof(sll));
        bzero(&ifr , sizeof(ifr)); 
        strncpy((char *)ifr.ifr_name ,device , IFNAMSIZ); 
        //copy device name to ifr 
        if((ioctl(raw , SIOCGIFINDEX , &ifr)) == -1)
        { 
            perror("Unable to find interface index");
            exit(-1); 
        }
        sll.sll_family = AF_PACKET; 
        sll.sll_ifindex = ifr.ifr_ifindex; 
        sll.sll_protocol = htons(protocol); 
        if((bind(raw , (struct sockaddr *)&sll , sizeof(sll))) ==-1)
        {
            perror("bind: ");
            exit(-1);
        }
        return 0;
    } 
    
    0 讨论(0)
提交回复
热议问题