aboutsummaryrefslogtreecommitdiffstats
path: root/src/protocol.c
blob: 1e9991fea5d99c3a47f6aa6d7907dfa7728ded15 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <arpa/inet.h>
#include <errno.h>
#include <glib.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "runes.h"
#include "protocol.h"

#define RUNES_MAX_PACKET_SIZE (1024*1024)

static int runes_protocol_store_u32(GArray *buf, uint32_t i);
static int runes_protocol_store_strvec(GArray *buf, char *vec[]);
static int runes_protocol_store_str(GArray *buf, char *str);
static ssize_t runes_protocol_load_u32(char *buf, size_t len, uint32_t *i);
static ssize_t runes_protocol_load_strvec(char *buf, size_t len, char ***vec);
static ssize_t runes_protocol_load_str(char *buf, size_t len, char **str);
static int runes_protocol_read_bytes(int sock, size_t len, char *outbuf);
static int runes_protocol_write_bytes(int sock, char *buf, size_t len);

int runes_protocol_parse_new_term_message(
    char *buf, size_t len, struct runes_protocol_new_term_message *outmsg)
{
    uint32_t version;
    size_t offset = 0;

#define LOAD(type, var) \
    do { \
        ssize_t incr; \
        incr = runes_protocol_load_##type(buf + offset, len - offset, &var); \
        if (incr < 0) { \
            free(outmsg); \
            return 0; \
        } \
        offset += incr; \
    } while (0)

    LOAD(u32, version);
    if (version != RUNES_PROTOCOL_MESSAGE_VERSION) {
        /* backcompat code can go here eventually */
        runes_warn("unknown protocol version %d", version);
        free(outmsg);
        return 0;
    }

    LOAD(u32,    outmsg->argc);
    LOAD(strvec, outmsg->argv);
    LOAD(strvec, outmsg->envp);
    LOAD(str,    outmsg->cwd);

#undef LOAD

    return 1;
}

int runes_protocol_create_new_term_message(
    struct runes_protocol_new_term_message *msg,
    char **outbuf, size_t *outlen)
{
    GArray *buf;

    buf = g_array_new(FALSE, FALSE, 1);

#define STORE(type, val) \
    do { \
        if (!runes_protocol_store_##type(buf, val)) { \
            g_array_unref(buf); \
            return 0; \
        } \
    } while (0)

    STORE(u32, RUNES_PROTOCOL_MESSAGE_VERSION);

    STORE(u32,    msg->argc);
    STORE(strvec, msg->argv);
    STORE(strvec, msg->envp);
    STORE(str,    msg->cwd);

#undef STORE

    *outbuf = malloc(buf->len);
    memcpy(*outbuf, buf->data, buf->len);
    *outlen = buf->len;

    g_array_unref(buf);

    return 1;
}

void runes_protocol_free_new_term_message(
    struct runes_protocol_new_term_message *msg)
{
    char **p;

    p = msg->argv;
    while (*p) {
        free(*p);
        p++;
    }
    free(msg->argv);

    p = msg->envp;
    while (*p) {
        free(*p);
        p++;
    }
    free(msg->envp);

    free(msg->cwd);
}

int runes_protocol_read_packet(
    int sock, int *outtype, char **outbuf, size_t *outlen)
{
    uint32_t len, type;
    char *buf;

    if (!runes_protocol_read_bytes(sock, sizeof(len), (char *)&len)) {
        return 0;
    }
    len = ntohl(len);

    if (!runes_protocol_read_bytes(sock, sizeof(type), (char *)&type)) {
        return 0;
    }
    type = ntohl(type);

    if (len > RUNES_MAX_PACKET_SIZE) {
        runes_warn("packet of size %d is too big", len);
        return 0;
    }
    buf = malloc(len);

    if (!runes_protocol_read_bytes(sock, len, buf)) {
        free(buf);
        return 0;
    }

    *outlen = len;
    *outbuf = buf;
    *outtype = type;

    return 1;
}

int runes_protocol_send_packet(int sock, int type, char *buf, size_t len)
{
    uint32_t len32 = len, type32 = type;

    if (len > RUNES_MAX_PACKET_SIZE) {
        runes_warn("packet of size %d is too big", len);
        return 0;
    }

    if (type < 0 || type >= RUNES_PROTOCOL_NUM_MESSAGE_TYPES) {
        runes_warn("unknown message type %d", type);
        return 0;
    }

    len32 = htonl(len32);
    if (!runes_protocol_write_bytes(sock, (char *)&len32, sizeof(len32))) {
        runes_warn("failed to write packet to socket");
        return 0;
    }

    type32 = htonl(type32);
    if (!runes_protocol_write_bytes(sock, (char *)&type32, sizeof(type32))) {
        runes_warn("failed to write packet to socket");
        return 0;
    }

    if (!runes_protocol_write_bytes(sock, buf, len)) {
        runes_warn("failed to write packet to socket");
        return 0;
    }

    return 1;
}

static int runes_protocol_store_u32(GArray *buf, uint32_t i)
{
    uint32_t ni = htonl(i);

    g_array_append_vals(buf, (void *)&ni, sizeof(ni));

    return 1;
}

static int runes_protocol_store_strvec(GArray *buf, char *vec[])
{
    size_t len, i;

    for (len = 0; vec[len]; ++len);
    if (len > RUNES_MAX_PACKET_SIZE) {
        runes_warn("string vector of size %d is too big", len);
        return 0;
    }

#define STORE(type, val) \
    do { \
        if (!runes_protocol_store_##type(buf, val)) { \
            return 0; \
        } \
    } while (0)

    STORE(u32, len);
    for (i = 0; i < len; ++i) {
        STORE(str, vec[i]);
    }

#undef STORE

    return 1;
}

static int runes_protocol_store_str(GArray *buf, char *str)
{
    size_t len = strlen(str);

    if (len > RUNES_MAX_PACKET_SIZE) {
        runes_warn("string of size %d is too big", len);
        return 0;
    }

#define STORE(type, val) \
    do { \
        if (!runes_protocol_store_##type(buf, val)) { \
            return 0; \
        } \
    } while (0)

    STORE(u32, len);

    g_array_append_vals(buf, str, len);

#undef STORE

    return 1;
}

static ssize_t runes_protocol_load_u32(char *buf, size_t len, uint32_t *i)
{
    uint32_t hi;

    if (len < sizeof(uint32_t)) {
        return -1;
    }

    hi = ((uint32_t *)buf)[0];
    *i = ntohl(hi);

    return sizeof(uint32_t);
}

static ssize_t runes_protocol_load_strvec(char *buf, size_t len, char ***vec)
{
    ssize_t offset = 0;
    uint32_t veclen = 0;
    size_t i;

    *vec = NULL;

#define LOAD(type, var) \
    do { \
        ssize_t incr; \
        incr = runes_protocol_load_##type(buf + offset, len - offset, &var); \
        if (incr < 0) { \
            free(*vec); \
            for (i = 0; i < veclen; ++i) { \
                free(*vec[i]); \
            } \
            return -1; \
        } \
        offset += incr; \
    } while (0)

    LOAD(u32, veclen);
    if (veclen > len) {
        runes_warn("string of size %d is too big", strlen);
        return -1;
    }

    *vec = calloc(veclen + 1, sizeof(char *));

    for (i = 0; i < veclen; ++i) {
        LOAD(str, (*vec)[i]);
    }
    (*vec)[veclen] = NULL;

#undef LOAD

    return offset;
}

static ssize_t runes_protocol_load_str(char *buf, size_t len, char **str)
{
    ssize_t offset = 0;
    uint32_t strlen;

    *str = NULL;

#define LOAD(type, var) \
    do { \
        ssize_t incr; \
        incr = runes_protocol_load_##type(buf + offset, len - offset, &var); \
        if (incr < 0) { \
            free(*str); \
            return -1; \
        } \
        offset += incr; \
    } while (0)

    LOAD(u32, strlen);
    if (strlen > len) {
        runes_warn("string of size %d is too big", strlen);
        return -1;
    }

    *str = malloc(strlen + 1);

    memcpy(*str, buf + offset, strlen);
    (*str)[strlen] = '\0';
    offset += strlen;

#undef LOAD

    return offset;
}

static int runes_protocol_write_bytes(int sock, char *buf, size_t len)
{
    size_t bytes = 0;

    while (bytes < len) {
        ssize_t sent;

        sent = send(sock, buf + bytes, len - bytes, 0);
        if (sent <= 0 && errno != EINTR) {
            runes_warn(
                "couldn't write %d bytes to socket: %s",
                len, strerror(errno));
            return 0;
        }
        bytes += sent;
    }

    return 1;
}

static int runes_protocol_read_bytes(int sock, size_t len, char *outbuf)
{
    size_t bytes = 0;

    while (bytes < len) {
        ssize_t got;

        got = recv(sock, outbuf + bytes, len - bytes, 0);
        if (got <= 0 && errno != EINTR) {
            runes_warn(
                "couldn't read %d bytes from socket: %s",
                len, strerror(errno));
            return 0;
        }
        bytes += got;
    }

    return 1;
}