~donmcc/ood

ref: 1182412fd4ece0cb55c4b3ebefee78b22c8c15bd ood/src/libood/ood/socket.c -rw-r--r-- 901 bytes
1182412fDon McCaughey Add `ood_halt()` function. 7 months ago
                                                                                
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
#include "socket.h"

#include <fcntl.h>
#include <sys/socket.h>


enum ood_result
ood_socket_set_blocking(int fd)
{
    int flags = fcntl(fd, F_GETFL);
    int result = fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
    return result < 0 ? ood_error : ood_okay;
}


enum ood_result
ood_socket_set_non_blocking(int fd)
{
    int flags = fcntl(fd, F_GETFL);
    int result = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    return result < 0 ? ood_error : ood_okay;
}


enum ood_result
ood_socket_set_reuse_address(int fd)
{
    int reuse_address = 1;
    int result = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_address, sizeof reuse_address);
    return result < 0 ? ood_error : ood_okay;
}


enum ood_result
ood_socket_set_reuse_port(int fd)
{
    int reuse_port = 1;
    int result = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse_port, sizeof reuse_port);
    return result < 0 ? ood_error : ood_okay;
}