aboutsummaryrefslogtreecommitdiffstats
path: root/src/socket.c
blob: 3d0a3aeae1a64300d0a9f1fbe58f86f0abdef140 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>

#include "runes.h"
#include "socket.h"

#define MAX_SOCKET_PATH_LEN \
    (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path))

static void runes_socket_populate_sockaddr(
    char *path, struct sockaddr_un *addr);

int runes_socket_client_open(char *path)
{
    int s;
    struct sockaddr_un client;

    runes_socket_populate_sockaddr(path, &client);

    s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s < 0) {
        runes_die("couldn't create socket: %s", strerror(errno));
    }

    if (connect(s, (struct sockaddr*)(&client), sizeof(struct sockaddr_un))) {
        runes_die(
            "couldn't connect to socket at %s: %s", path, strerror(errno));
    }

    return s;
}

int runes_socket_server_open(char *path)
{
    char *dir, *slash;
    int s;
    struct sockaddr_un server;

    runes_socket_populate_sockaddr(path, &server);

    dir = strdup(path);
    slash = strrchr(dir, '/');
    if (slash == NULL) {
        runes_die("socket path %s must be an absolute path", path);
    }
    *slash = '\0';
    runes_mkdir_p(dir);
    free(dir);

    unlink(path);

    s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s < 0) {
        runes_die("couldn't create socket: %s", strerror(errno));
    }

    if (bind(s, (struct sockaddr*)(&server), sizeof(struct sockaddr_un))) {
        runes_die(
            "couldn't bind to socket %s: %s", path, strerror(errno));
    }

    if (chmod(path, S_IRUSR|S_IWUSR)) {
        runes_die(
            "couldn't chmod socket %s: %s", path, strerror(errno));
    }

    if (listen(s, 5)) {
        runes_die(
            "couldn't listen on socket %s: %s", path, strerror(errno));
    }

    return s;
}

int runes_socket_server_accept(int ss)
{
    struct sockaddr_un client;
    socklen_t len = sizeof(client);
    int cs;

    cs = accept(ss, (struct sockaddr*)(&client), &len);
    if (cs < 0) {
        runes_die("couldn't accept connection: %s", strerror(errno));
    }

    return cs;
}

void runes_socket_client_close(int s)
{
    close(s);
}

void runes_socket_server_close(int s, char *path)
{
    close(s);
    unlink(path);
}

static void runes_socket_populate_sockaddr(
    char *path, struct sockaddr_un *addr)
{
    size_t name_len = strlen(path) + 1; // including the nul byte

    if (name_len > MAX_SOCKET_PATH_LEN) {
        runes_die("socket path %s is too long", path);
    }

    addr->sun_family = AF_UNIX;
    strncpy(addr->sun_path, path, name_len);
}