File size: 2,496 Bytes
258fb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
 *
 * This file is part of Open5GS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "ogs-tun.h"

#undef OGS_LOG_DOMAIN
#define OGS_LOG_DOMAIN __ogs_sock_domain

ogs_pkbuf_t *ogs_tun_read(ogs_socket_t fd, ogs_pkbuf_pool_t *packet_pool)
{
    ogs_pkbuf_t *recvbuf = NULL;
    int n;

    ogs_assert(fd != INVALID_SOCKET);

    recvbuf = ogs_pkbuf_alloc(packet_pool, OGS_MAX_PKT_LEN);
    ogs_assert(recvbuf);
    ogs_pkbuf_reserve(recvbuf, OGS_TUN_MAX_HEADROOM);
    ogs_pkbuf_put(recvbuf, OGS_MAX_PKT_LEN-OGS_TUN_MAX_HEADROOM);

    n = ogs_read(fd, recvbuf->data, recvbuf->len);
    if (n <= 0) {
        ogs_log_message(OGS_LOG_WARN, ogs_socket_errno, "ogs_read() failed");
        ogs_pkbuf_free(recvbuf);
        return NULL;
    }

    ogs_pkbuf_trim(recvbuf, n);

#if defined(__APPLE__)
    /* Remove Null/Loopback Header (4bytes) */
    ogs_pkbuf_pull(recvbuf, 4);
#endif

    return recvbuf;
}

int ogs_tun_write(ogs_socket_t fd, ogs_pkbuf_t *pkbuf)
{
#if defined(__APPLE__)
    uint8_t version;
    uint32_t family;
#endif

    ogs_assert(fd != INVALID_SOCKET);
    ogs_assert(pkbuf);

#if defined(__APPLE__)
    version = (*((unsigned char *)pkbuf->data) >> 4) & 0xf;

    if (version == 4) {
        family = htobe32(AF_INET);
    } else if (version == 6) {
        family = htobe32(AF_INET6);
    } else {
        ogs_error("Invalid packet [IP version:%d, Packet Length:%d]",
                version, pkbuf->len);
        ogs_log_hexdump(OGS_LOG_ERROR, pkbuf->data, pkbuf->len);
        return OGS_ERROR;
    }

    ogs_pkbuf_push(pkbuf, sizeof(family));
    memcpy(pkbuf->data, &family, sizeof(family));
#endif

    if (ogs_write(fd, pkbuf->data, pkbuf->len) <= 0) {
        ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, "ogs_write() failed");
        return OGS_ERROR;
    }

    return OGS_OK;
}