summaryrefslogtreecommitdiffstats
path: root/docs/socket-auto-port.c
blob: 75dffafe75f35c0aa2143e62f16ba8aa1c2804de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int s;
    struct sockaddr_storage sa;
    socklen_t salen;
    uint16_t port;
    
    if ((s = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
        if (errno == EAFNOSUPPORT)
            s = socket(PF_INET, SOCK_STREAM, 0);
    
        if (s < 0) {
            perror("socket()");
            return 1;
        }
    }

    if (listen(s, 2) < 0) {
        perror("listen()");
        return 2;
    }

    salen = sizeof(sa);
    if (getsockname(s, (struct sockaddr*) &sa, &salen) < 0) {
        perror("getsockname()");
        return 3;
    }

    if (((struct sockaddr*) &sa)->sa_family == AF_INET)
        port = ((struct sockaddr_in*) &sa)->sin_port;
    else
        port = ((struct sockaddr_in6*) &sa)->sin6_port;

    printf("Selected port number %u\n", ntohs(port));

    /* ... hic sunt leones ... */

    sleep(60);
    
    return 0;
}