aboutsummaryrefslogtreecommitdiff
path: root/client/libacron/net.c
blob: 7ac3bda67fb4a6975ba20844e767546f48d3f5f0 (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
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
371
372
373
374
375
376
377
378
/*
 * Created by yuuta on 7/13/22.
 */

#include "win32.h"
#include "config.h"
#include "net.h"
#include "connection.h"
#include "log.h"
#include "helpers.h"
#include "serializer.h"

/* TODO: wic logging is not yet supported */
#include "wic.h"

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>

#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define errno_sock WSAGetLastError()
#else
#define errno_sock errno
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#endif

static void conn_free(struct ac_connection *conn) {
    if (conn->url) free(conn->url);
    if (conn->parameters.id) free(conn->parameters.id);
    if (conn->parameters.host) free(conn->parameters.host);
    if (conn->parameters.token) free(conn->parameters.token);
    free(conn);
}

static void on_open_handler(struct wic_inst *inst) {
    struct ac_connection *conn = wic_get_app(inst);
    conn->established = true;
}

static void on_send_handler(struct wic_inst *inst,
                            const void *data,
                            size_t size,
                            enum wic_buffer type) {
    struct ac_connection *conn = wic_get_app(inst);
    size_t pos;
    const uint8_t *ptr = data;
    int retval;

    for (pos = 0U; pos < size; pos += retval) {
        size_t n = size - pos;
        LOGDV("write(%d, %p[%u, %u])", conn->fd, data, pos, n);
        retval = (int) send(conn->fd, &ptr[pos], (int) n, 0);
        if (retval <= 0) {
            /* There's no way to abort the process. */
            int e = errno_sock;
            LOGEV("Cannot write to socket: %s (%d).",
                  strerror2(errno),
                  e);
            break;
        }
    }
    free((void *) data);
}

static void *on_buffer_handler(struct wic_inst *inst,
                               size_t min_size,
                               enum wic_buffer type,
                               size_t *max_size) {
    void *buf;
    if ((alloc(NULL,
               1,
               *max_size = (min_size ? min_size : 1024),
               false,
               &buf))) {
        return NULL;
    }
    return buf;
}

static bool on_message_handler(struct wic_inst *inst,
                               enum wic_encoding encoding,
                               bool fin,
                               const char *data,
                               uint16_t size) {
    struct ac_connection *conn = wic_get_app(inst);
    if (encoding != WIC_ENCODING_UTF8) {
        LOGE("Invalid encoding received from server. Only text frames are supported.");
        return true;
    }
    /* TODO: Only parse when fin = true? */
    struct ac_result *res = &conn->result;
    int r;
    if ((r = deserialize(data, size, &res->obj))) {
        if (r == AC_E_SERIALIZER_CONTINUE) {
            if (fin) {
                serializer_finalize();
                LOGE("Server returned an incomplete response.");
                res->res = AC_E_INVALID_RESPONSE;
            } else {
                res->res = AC_E_OK;
            }
        } else {
            res->res = r;
        }
    } else {
        res->has_result = true;
        res->res = r;
    }
    LOGDV("Post deserializing: { has_result = %d, res = %d, obj = %p }",
          res->has_result,
          res->res,
          res->obj);

    return true;
}

int ac_connect(ac_connection_parameters_t parameters, void **out) {
    AC_CHECK_INIT;

    if (!parameters.id ||
        !parameters.host ||
        !parameters.token) {
        LOGE("Required parameters are missing.");
        return AC_E_INVALID_REQUEST;
    }

    struct ac_connection *conn;
    int r;
    if ((r = SALLOC(struct ac_connection, &conn))) {
        return r;
    }
    /* Do not use memcpy() here because we want to keep track of copied and not-copied strings. */
    conn->parameters.port = parameters.port;
    if ((r = strdup2(parameters.token, &conn->parameters.token)) ||
            (r = strdup2(parameters.host, &conn->parameters.host)) ||
            (r = strdup2(parameters.id, &conn->parameters.id))) {
        conn_free(conn);
        return r;
    }

    if ((r = alloc(NULL,
                   5 /* ws:// */ +
                   strlen(parameters.host) +
                   1 /* : */ +
                   /* Without the () the result seems to be strange. */
                           (parameters.port ? ((int) floor(log10(parameters.port)) + 1) : 1) +
                   3 /* /ws */ +
                   4 /* ?id= */ +
                   strlen(parameters.id) +
                   7 /* &token = */ +
                   strlen(parameters.token) +
                   9 /* &version= */ +
                           /* Without the () the result seems to be strange. */
                           (parameters.version ? ((int) floor(log10(parameters.version)) + 1) : 1) +
                   1 /* NULL */,
                   sizeof(char),
                   false,
                   (void **) &conn->url))) {
        conn_free(conn);
        return r;
    }
    sprintf(conn->url, "ws://%s:%u/ws?id=%s&token=%s&version=%u",
            parameters.host,
            parameters.port,
            parameters.id,
            parameters.token,
            parameters.version);

    static uint8_t rx[1000];
    struct wic_inst *inst = &conn->inst;
    struct wic_init_arg arg = {0};

    arg.rx = rx;
    arg.rx_max = sizeof(rx);

    arg.on_send = on_send_handler;
    arg.on_open = on_open_handler;
    arg.on_message = on_message_handler;
    arg.on_buffer = on_buffer_handler;

    arg.app = conn;
    arg.url = conn->url;
    arg.role = WIC_ROLE_CLIENT;

    if (!wic_init(inst, &arg)) {
        LOGE("wic_init");
        conn_free(conn);
        return AC_E_INTERNAL;
    }

    struct addrinfo *res;
    char service[6];

    const enum wic_schema schema = wic_get_url_schema(inst);
    const uint16_t port = wic_get_url_port(inst);
    snprintf(service, 6, "%u", port);
    const char *host = wic_get_url_hostname(inst);

    switch (schema) {
        case WIC_SCHEMA_WS:
            break;
        case WIC_SCHEMA_WSS:
            LOGE("WSS is not supported yet.");
            conn_free(conn);
            return AC_E_INVALID_REQUEST;
        default:
            LOGE("Unsupported protocol. The URL must be ws://");
            conn_free(conn);
            return AC_E_INVALID_REQUEST;
    }

    if ((r = getaddrinfo(host, service, NULL, &res))) {
        /* gai_strerror() seems to cause an linkage error. */
#ifdef WIN32
        LOGEV("Resolve host: %d.", r);
#else
        LOGEV("Resolve host: %s.", gai_strerror(r));
#endif
        r = AC_E_NET;
        conn_free(conn);
        return r;
    }

    SOCKET fd;
    if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) <= 0) {
        const int e = errno_sock;
        LOGEV("Cannot create socket: %s (%d).",
              strerror2(e),
              e);
        freeaddrinfo(res);
        conn_free(conn);
        return AC_E_NET;
    }
    if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
        const int e = errno_sock;
        LOGEV("Cannot connect to socket: %s (%d).",
              strerror2(e),
              e);
        closesocket(fd);
        freeaddrinfo(res);
        conn_free(conn);
        return AC_E_NET;
    }
    freeaddrinfo(res);

    conn->fd = fd;
    if (wic_start(inst) != WIC_STATUS_SUCCESS) {
        LOGE("Cannot start the WIC client.");
        closesocket(fd);
        conn_free(conn);
        return AC_E_NET;
    }

    /* Wait until established (ready to send). */
    while (!conn->established) {
        ac_obj_t *obj = NULL;
        r = ac_receive(conn, &obj, 0);
        if (obj) {
            LOGW("Received an object before connection is established. Dropping.");
            ac_object_free(obj);
        }
        if (r) {
            ac_disconnect(conn);
            return r;
        }
    }
    *out = conn;
    return AC_E_OK;
}

int ac_disconnect(void *connection) {
    AC_CHECK_INIT;
    struct ac_connection *conn = connection;
    if (!conn->fd) {
        LOGE("Trying to disconnect an already disconnected connection.");
        return AC_E_INVALID_REQUEST;
    }
    LOGD("Disconnecting...");
    wic_close(&conn->inst);
    closesocket(conn->fd);
    conn_free(conn);
    return AC_E_OK;
}

int ac_receive(void *connection, ac_obj_t **response) {
    AC_CHECK_INIT;
    struct ac_connection *conn = connection;
    struct wic_inst *inst = &conn->inst;

    static uint8_t buffer[1000U];
    int bytes;
    size_t retval, pos;

    if ((bytes = (int) recv(conn->fd, buffer, sizeof(buffer), 0)) <= 0) {
        LOGDV("recv(%d) = %d", conn->fd, bytes);
        if (bytes < 0) {
            const int e = errno_sock;
            if (e == EAGAIN) {
                /* Timeout */
                *response = NULL;
                return AC_E_OK;
            }
            LOGEV("Failed to receive from socket : %s (%d).",
                  strerror2(e),
                  e);
        } else {
            LOGI("Peer abnormally shutdown.");
        }
        wic_close_with_reason(inst, WIC_CLOSE_ABNORMAL_2, NULL, 0);
        return AC_E_NET;
    }
    LOGDV("recv(%d) = %d", conn->fd, bytes);

    /* In case the deserializer does not run at all. */
    memset(&conn->result, 0, sizeof(struct ac_result));
    for (pos = 0U; pos < bytes; pos += retval) {
        retval = wic_parse(inst, &buffer[pos], bytes - pos);
        if (wic_get_state(inst) == WIC_STATE_CLOSED) {
            LOGE("Connection closed.");
            return AC_E_NET;
        }
    }

    struct ac_result *res = &conn->result;
    LOGDV("res { has_result = %d, res = %d, obj = %p }",
          res->has_result,
          res->res,
          res->obj);
    *response = NULL;
    if (res->has_result) {
        if (res->res) {
            return res->res;
        } else {
            *response = res->obj;
            return AC_E_OK;
        }
    } else {
        *response = NULL;
    }

    return AC_E_OK;
}

int ac_request(void *connection, const ac_request_t *request) {
    AC_CHECK_INIT;
    struct ac_connection *conn = connection;
    struct wic_inst *inst = &conn->inst;
    if (wic_get_state(inst) != WIC_STATE_OPEN) {
        LOGE("Invalid state.");
        return AC_E_INVALID_REQUEST;
    }
    int r;
    json_object *obj;
    if ((r = serialize_request(request, &obj))) {
        return r;
    }
    const char *str;
    size_t len;
    if (!(str = json_object_to_json_string_length(obj, JSON_C_TO_STRING_PLAIN, &len))) {
        json_object_put(obj);
        LOGE("Cannot serialize JSON.");
        return AC_E_INTERNAL;
    }
    if (wic_send_text(inst, true, str, len) != WIC_STATUS_SUCCESS) {
        LOGE("Cannot send.");
        json_object_put(obj);
        return AC_E_NET;
    }
    json_object_put(obj);
    return AC_E_OK;
}