I have a server listening on multiple devices (LAN, WiFi and, possibly, some VPNs).
Listening binds a socket (AF_INET) binds INADDR_ANY to be exposed on all interfaces.
A client then connects and interchange starts.
At this point, how can I get information about which device was used for connection?
Equivalently: how can I get the IP address client used to connect?
EDIT: Following @Barmar advice I tried the following:
#include <sys/socket.h>
#include <netinet/ip.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define handle_error(msg) \
        do { perror(msg); exit(EXIT_FAILURE); } while (0)
void error(char *msg)
{
    perror(msg);
    exit(1);
}
int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno;
    unsigned clilen;
    struct sockaddr_in serv_addr, cli_addr;
    struct sockaddr_in myaddr;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR creating socket");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5666;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error("ERROR on binding");
    listen(sockfd, 5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    if (newsockfd < 0)
        error("ERROR on accept");
    clilen = sizeof(myaddr);
    if (getsockname(newsockfd, (struct sockaddr *)&myaddr, &clilen))
        error("ERROR in getsockname");
    unsigned int i = myaddr.sin_addr.s_addr;
    int p = myaddr.sin_port;
    int a = i & 0xff;
    i >>= 8;
    int b = i & 0xff;
    i >>= 8;
    int c = i & 0xff;
    i >>= 8;
    int d = i & 0xff;
    printf("my address is: %d.%d.%d.%d:%d\n", a, b,c, d, p);
    return 0;
}
... and (after a bit of debugging) it works.
Note that Port is not the value expected (5666) but consistently 8726.