aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-bindings/ipaddress/IPv4Address.c
blob: 23204329d3460a46879674312bd59a015ea44c9d (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
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Dan Halbert for Adafruit Industries
 * Copyright (c) 2018 Artur Pacholec
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "shared-bindings/ipaddress/IPv4Address.h"

#include <string.h>
#include <stdio.h>

#include "py/objproperty.h"
#include "py/objstr.h"
#include "py/runtime.h"
#include "shared-bindings/ipaddress/__init__.h"

//| class IPv4Address:
//|     """Encapsulates an IPv4 address."""
//|

//|     def __init__(self, address: Union[int, str, bytes]) -> None:
//|         """Create a new IPv4Address object encapsulating the address value.
//|
//|            The value itself can either be bytes or a string formatted address."""
//|         ...
//|
STATIC mp_obj_t ipaddress_ipv4address_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
    enum { ARG_address };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_address, MP_ARG_OBJ | MP_ARG_REQUIRED },
    };

    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    const mp_obj_t address = args[ARG_address].u_obj;

    uint32_t value;
    uint8_t *buf = NULL;
    if (mp_obj_get_int_maybe(address, (mp_int_t *)&value)) {
        // We're done.
        buf = (uint8_t *)&value;
    } else if (mp_obj_is_str(address)) {
        GET_STR_DATA_LEN(address, str_data, str_len);
        if (!ipaddress_parse_ipv4address((const char *)str_data, str_len, &value)) {
            mp_raise_ValueError(translate("Not a valid IP string"));
        }
        buf = (uint8_t *)&value;
    } else {
        mp_buffer_info_t buf_info;
        if (mp_get_buffer(address, &buf_info, MP_BUFFER_READ)) {
            if (buf_info.len != 4) {
                mp_raise_ValueError_varg(translate("Address must be %d bytes long"), 4);
            }
            buf = buf_info.buf;
        }
    }


    ipaddress_ipv4address_obj_t *self = m_new_obj(ipaddress_ipv4address_obj_t);
    self->base.type = &ipaddress_ipv4address_type;

    common_hal_ipaddress_ipv4address_construct(self, buf, 4);

    return MP_OBJ_FROM_PTR(self);
}

//|     packed: bytes
//|     """The bytes that make up the address (read-only)."""
//|
STATIC mp_obj_t ipaddress_ipv4address_get_packed(mp_obj_t self_in) {
    ipaddress_ipv4address_obj_t *self = MP_OBJ_TO_PTR(self_in);

    return common_hal_ipaddress_ipv4address_get_packed(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(ipaddress_ipv4address_get_packed_obj, ipaddress_ipv4address_get_packed);

MP_PROPERTY_GETTER(ipaddress_ipv4address_packed_obj,
    (mp_obj_t)&ipaddress_ipv4address_get_packed_obj);

//|     version: int
//|     """4 for IPv4, 6 for IPv6"""
//|
STATIC mp_obj_t ipaddress_ipv4address_get_version(mp_obj_t self_in) {
    ipaddress_ipv4address_obj_t *self = MP_OBJ_TO_PTR(self_in);
    mp_buffer_info_t buf_info;
    mp_obj_t address_bytes = common_hal_ipaddress_ipv4address_get_packed(self);
    mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
    mp_int_t version = 6;
    if (buf_info.len == 4) {
        version = 4;
    }

    return MP_OBJ_NEW_SMALL_INT(version);
}
MP_DEFINE_CONST_FUN_OBJ_1(ipaddress_ipv4address_get_version_obj, ipaddress_ipv4address_get_version);

MP_PROPERTY_GETTER(ipaddress_ipv4address_version_obj,
    (mp_obj_t)&ipaddress_ipv4address_get_version_obj);

//|     def __eq__(self, other: object) -> bool:
//|         """Two Address objects are equal if their addresses and address types are equal."""
//|         ...
//|
STATIC mp_obj_t ipaddress_ipv4address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    switch (op) {
        // Two Addresses are equal if their address bytes and address_type are equal
        case MP_BINARY_OP_EQUAL:
            if (mp_obj_is_type(rhs_in, &ipaddress_ipv4address_type)) {
                ipaddress_ipv4address_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
                ipaddress_ipv4address_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
                return mp_obj_new_bool(
                    mp_obj_equal(common_hal_ipaddress_ipv4address_get_packed(lhs),
                        common_hal_ipaddress_ipv4address_get_packed(rhs)));

            } else {
                return mp_const_false;
            }

        default:
            return MP_OBJ_NULL; // op not supported
    }
}

//|     def __hash__(self) -> int:
//|         """Returns a hash for the IPv4Address data."""
//|         ...
//|
STATIC mp_obj_t ipaddress_ipv4address_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
    switch (op) {
        // Two Addresses are equal if their address bytes and address_type are equal
        case MP_UNARY_OP_HASH: {
            mp_obj_t bytes = common_hal_ipaddress_ipv4address_get_packed(MP_OBJ_TO_PTR(self_in));
            GET_STR_HASH(bytes, h);
            if (h == 0) {
                GET_STR_DATA_LEN(bytes, data, len);
                h = qstr_compute_hash(data, len);
            }
            return MP_OBJ_NEW_SMALL_INT(h);
        }
        default:
            return MP_OBJ_NULL; // op not supported
    }
}

STATIC void ipaddress_ipv4address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
    ipaddress_ipv4address_obj_t *self = MP_OBJ_TO_PTR(self_in);
    mp_buffer_info_t buf_info;
    mp_obj_t address_bytes = common_hal_ipaddress_ipv4address_get_packed(self);
    mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);

    const uint8_t *buf = (uint8_t *)buf_info.buf;
    mp_printf(print, "%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]);
}

STATIC const mp_rom_map_elem_t ipaddress_ipv4address_locals_dict_table[] = {
    { MP_ROM_QSTR(MP_QSTR_packed),                 MP_ROM_PTR(&ipaddress_ipv4address_packed_obj) },
};

STATIC MP_DEFINE_CONST_DICT(ipaddress_ipv4address_locals_dict, ipaddress_ipv4address_locals_dict_table);

const mp_obj_type_t ipaddress_ipv4address_type = {
    { &mp_type_type },
    .flags = MP_TYPE_FLAG_EXTENDED,
    .name = MP_QSTR_Address,
    .make_new = ipaddress_ipv4address_make_new,
    .locals_dict = (mp_obj_dict_t *)&ipaddress_ipv4address_locals_dict,
    .print = ipaddress_ipv4address_print,
    MP_TYPE_EXTENDED_FIELDS(
        .unary_op = ipaddress_ipv4address_unary_op,
        .binary_op = ipaddress_ipv4address_binary_op,
        )
};