Вопрос

I'm working with Linux(Debian) and C.

I have opened a socket to the interfaces - eth0 and eth1 with the following function:

    // Opening the socket
    mSocket = socket(device->ifa_addr->sa_family, SOCK_RAW | SOCK_NONBLOCK,
                htons(ETH_P_ALL));
    if (mSocket == -1)
    {
        throw CException("socket is bad");
    }

    // Adding flag to the socket to work only on a specific interface
    if (setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE,
            (void *) &mIfreq, sizeof(mIfreq)) < 0)
    {
        throw CException(
                "Failed to add binding to specific interface flag");
    }

while ifreq holds the interface name:

    strncpy(mIfreq.ifr_ifrn.ifrn_name,device->ifa_name, IF_NAMESIZE);

The next thing I do is receiving packets using the following code:

    recvSize = recvfrom(mSocket, buffer, ETH_FRAME_LEN, 0, NULL,
                    NULL);

questions:

1) why if both interfaces aren't connected to anything non of the sockets can be opened

2*) why (WLOG) if eth0 is connected and eth1 isn't I can open sockets to each of them

2.1*) why by reading from eth1 socket I get all packets received in eth0

(wireshark on eth1 isn't showing any packet as it should be)

[my guess - is it linux fault?(doing stuff behind my back?)]

Thanks in advance,

Despair

Это было полезно?

Решение

Assuming that you want to capture raw data just as Wireshark does.

socket(7) clearly states that SO_BINDTODEVICE is not applicable to packet sockets. You should bind your socket to sockaddr_ll type address with sll_ifindex field set to the interface number (see packet(7) for details). Interface number can be obtained from its name (like "eth0") using SIOCGIFINDEX ioctl (see netdevice(7)).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top