Why under the Linux kernel V5, the UDP connect and unconnect doesn't work?

Viewed 129

I have an application that has been working for a very long time, under various distributions, under the kernel v2, v3 and v4 and I never had problem. Now I'm trying to make it work on Ubuntu with a kernel V5 and I have a problem. It is base on UDP sockets and there is a discovery mechanism that uses broadcast packets. The way it works, a client send a broadcast UPD packet and all servers answer to this broadcast. After that, the client get information from each servers by sending directed UPD packet to every servers that had responded to the broadcast. On the server side, when an UDP packet is received, I call connect with the address of the client so I don't have to follow the client address and port any further in the code. Once the answer has been sent, I "unconnect" the socket by calling connect with an AF_UNSPEC sin_family.

This was working fine but not with a V5 kernel. After I unconnect, it seems that the server doesn't receive directed communications. It receives broadcast UPD fine but not the one that are specifically addressed the it.

Just in case other factors would be the cause of the problem (firewall, etc...), I made a small test code and see the same thing; this code is include in my post. If I don't use the connect/unconnect in the server code, it works fine. But not with the connect/unconnect. And if I use a kernel 4 and lower, it works fine with the connect/unconnect. For the kernel V5, I tested on 5.2.9 and 5.3.

I don't understand because this is a valid and documented way of working with UDP sockets. I googled a lot and I haven't found anything that would suggest the it doesn't work with Linux kernel V5.x.

Does somebody have any idea about this problem?

Thank you.

Frank

Server code

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>

#define PORT     20000 
#define MAXLINE 512 

// Driver code 
int main() { 
    int sockfd; 
    char buffer[MAXLINE]; 
    char ifName[50] = "enp2s0";
    struct sockaddr_in servaddr, cliaddr; 

    char resp[50];
    fd_set  rset;

    struct sockaddr hwaddr;
    struct sockaddr_in  curr_ipaddr, ipaddr;
    struct sockaddr_in  udp_addr;
    struct timeval      toval, *ptoval;

    int     maxfdp1, errcode;
    int     on = 1;

    resp[0] = 0xaa;
    resp[1] = 0x55;
    resp[2] = 0x80;
    resp[3] = 0x7f;
    resp[4] = 0xd0;
    resp[5] = 0x00;
    resp[6] = 0x09;
    resp[7] = 0xaa;
    resp[8] = 0x00;
    resp[9] = 0x02;
    resp[10] = 0x2c;
    resp[11] = 0x08;
    resp[12] = 0x00;
    resp[13] = 0x40;
    resp[14] = 0x10;
    resp[15] = 0x92;

    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
        perror("socket creation failed"); 
        exit(EXIT_FAILURE); 
    } 

    // Allow broadcast
    setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

    memset(&servaddr, 0, sizeof(servaddr)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 

    // Filling server information 
    servaddr.sin_family    = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = INADDR_ANY; 
    servaddr.sin_port = htons(PORT); 

    // Bind the socket with the server address 
    if ( bind(sockfd, (const struct sockaddr *)&servaddr,  
            sizeof(servaddr)) < 0 ) 
    { 
        perror("bind failed"); 
        exit(EXIT_FAILURE); 
    } 

    int len, n; 

    len = sizeof(cliaddr);  //len is value/resuslt 

    maxfdp1 = sockfd+1;
    FD_ZERO(&rset);

    for ( ; ; )
        {
        FD_SET(sockfd, &rset);

        toval.tv_sec = 0;
        toval.tv_usec = 500000;
        ptoval = &toval;

        if ((errcode = select(maxfdp1, &rset, NULL, NULL, ptoval)) < 0)
            {
            if (errno == EINTR)
                {
                continue;
                }
            else
                {
                printf("Select error %d", errno);
                }
            }

        if (FD_ISSET(sockfd, &rset))
            {
            n = recvfrom(sockfd, (char *)buffer, MAXLINE,  
                MSG_WAITALL, ( struct sockaddr *) &cliaddr, 
                &len); 
            buffer[n] = '\0'; 

            printf("received =\n");
            for (int i=0; i<n; i++) printf("%02x ", (unsigned char)buffer[i]);
            printf("\n");

            // Connect the socket; this way it can be used as a TCP socket
            connect(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));

            if ((unsigned char)buffer[4] == 0xc0)
                {
                // This is to answerr to the broadcast so I can receive the
                // the next directed packet
                printf("sending ans\n");
                sendto(sockfd, (const char *)resp, 16,  
                    MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
                        len); 
                }

            // Unconnect the UDP socket
            struct sockaddr_in dumaddr; 
            // Filling server information 
            dumaddr.sin_family    = AF_UNSPEC; // IPv4 
            dumaddr.sin_addr.s_addr = INADDR_ANY; 
            dumaddr.sin_port = htons(PORT); 
            int cerr = connect(sockfd, (struct sockaddr *)&dumaddr, sizeof(dumaddr));
            printf("cerr = %d\n", cerr);
            }      
        }
    return 0; 
} 

Client code

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>

#define PORT     20000 
#define MAXLINE 512 

// Driver code 
int main() { 
    int sockfd; 
    char buffer[MAXLINE]; 
    struct sockaddr_in servaddr, cliaddr; 

    char cmdc0[20], cmdc1[20];

    int     on = 1;

    cmdc0[0] = 0xaa;
    cmdc0[1] = 0x55;
    cmdc0[2] = 0x80;
    cmdc0[3] = 0x7f;
    cmdc0[4] = 0xc0;
    cmdc0[5] = 0x00;
    cmdc0[6] = 0x00;
    cmdc0[7] = 0x00;
    cmdc0[8] = 0x00;
    cmdc0[9] = 0x00;

    cmdc1[0] = 0xaa;
    cmdc1[1] = 0x55;
    cmdc1[2] = 0x80;
    cmdc1[3] = 0x7f;
    cmdc1[4] = 0xc1;
    cmdc1[5] = 0x00;
    cmdc1[6] = 0x00;
    cmdc1[7] = 0x00;
    cmdc1[8] = 0x00;
    cmdc1[9] = 0x00;


    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
        perror("socket creation failed"); 
        exit(EXIT_FAILURE); 
    } 

    // Allow broadcast
    setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

    memset(&servaddr, 0, sizeof(servaddr)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 

    // Filling server information 
    servaddr.sin_family    = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = INADDR_BROADCAST; 
    servaddr.sin_port = htons(PORT); 

    int len, n; 

    len = sizeof(servaddr);  //len is value/resuslt 

    sendto(sockfd, (const char *)cmdc0, 7,  
        MSG_CONFIRM, (const struct sockaddr *) &servaddr, 
            len); 

    while (1 == 1)
        {
        int clen=sizeof(cliaddr);

        n = recvfrom(sockfd, buffer, MAXLINE-1, 0, (struct sockaddr *)&cliaddr, &clen);
        buffer[n] = '\0';

        char *baddr=(char *)&(cliaddr.sin_addr);
        printf("received from = %d-%d-%d-%d\n", (unsigned char)baddr[0], (unsigned char)baddr[1],
                                                (unsigned char)baddr[2], (unsigned char)baddr[3]);
        for (int i=0; i<n; i++) printf("%02x ", (unsigned char)buffer[i]);
        printf("\n");

        if ((unsigned char)buffer[4] == 0xd0)
            {
            // Send another command but directed this time
            printf("Sending C1...\n");
            sendto(sockfd, cmdc1, 7, 0, (const struct sockaddr *)&cliaddr, clen);
            }
        }

    return 0; 
} 
0 Answers
Related