How to remove incoming packets to a raw socket?

I am writing a C / C ++ application for Linux that reads data from a raw socket (for ICMP packets). Question: Is there a way to discard all data that is still queued on the socket?

The problem is that after sleep for a while there is data queuing for a socket that is not interesting to me; so it would be best to tell the socket to "forget all the data that you have buffered right now," so if I go into the select () / recvfrom () loop, I only get the data that I’ve received recently.

Is there a better way than the first poll () / recvfrom () loop? Maybe some kind of API socket? Portable, even ?:-)

+3
source share
7 answers

recvfrom() ?

+1

, :

 int optval = 0; /* May need to be 1 on some platforms */

 setsockopt(sockDesc, SOL_SOCKET, SO_RCVBUF, (char *)(&optval), sizeof(optval));

, "optval" (, 4096).

+5

. select .

while (1)
{

    FD_ZERO (&sockets);
    FD_SET (raw_socket, &sockets);

    timeout.tv_sec = 1;
    timeout.tv_usec = 0;

    if (select (raw_socket + 1, &sockets, NULL, NULL, &timeout))
    {
    if (FD_ISSET (raw_socket, &sockets))
    {
        // handle the packet
    }
    }
    else
    {
    /* Select Timed Out */
    fprintf(stderr, "Timed out");
    }
}  

, , icmp.

+2

, , , - .

+1

, ( , ), : . , , , , . .

, , , , . , - , , , , . , , , .

0

- ?

for(n=0;n<=MAX_BUFFER_SIZE;n++)
{
recv_buffer[n] = 0;
}
0

- IO , , . - , . , .

This is the TIBCO Rendezvous architecture used in many real-time market data and enterprise messaging systems. The caveat is that you usually want to limit the size of the queue so that the application does not receive from the OOM manager. The protocol between the I / O stream and the application layer can range from a simple asynchronous queue to more complex filtering of objects, priority lists, and support for thread pools for parallel decoding of incoming data.

0
source

Source: https://habr.com/ru/post/1698359/


All Articles