circuitpython/shared-bindings/ipaddress/IPv4Address.c

195 lines
7.4 KiB
C
Raw Normal View History

2020-08-05 15:53:35 -04:00
/*
* 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.
*/
2020-08-18 20:06:59 -04:00
#include "shared-bindings/ipaddress/IPv4Address.h"
2020-08-05 15:53:35 -04:00
#include <string.h>
#include <stdio.h>
#include "py/objproperty.h"
#include "py/objstr.h"
#include "py/runtime.h"
2020-08-18 20:06:59 -04:00
#include "shared-bindings/ipaddress/__init__.h"
2020-08-05 15:53:35 -04:00
//| class IPv4Address:
//| """Encapsulates an IPv4 address."""
//|
2020-09-03 19:32:12 -04:00
//| def __init__(self, address: Union[int, str, bytes]) -> None:
2020-08-18 20:06:59 -04:00
//| """Create a new IPv4Address object encapsulating the address value.
2020-08-05 15:53:35 -04:00
//|
2020-08-26 20:18:16 -04:00
//| The value itself can either be bytes or a string formatted address."""
2020-08-05 15:53:35 -04:00
//| ...
//|
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) {
2020-08-05 15:53:35 -04:00
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);
2020-08-05 15:53:35 -04:00
const mp_obj_t address = args[ARG_address].u_obj;
2020-08-18 20:06:59 -04:00
uint32_t value;
2021-03-15 09:57:36 -04:00
uint8_t *buf = NULL;
if (mp_obj_get_int_maybe(address, (mp_int_t *)&value)) {
2020-08-18 20:06:59 -04:00
// We're done.
buf = (uint8_t *)&value;
} else if (mp_obj_is_str(address)) {
2020-08-18 20:06:59 -04:00
GET_STR_DATA_LEN(address, str_data, str_len);
2021-03-15 09:57:36 -04:00
if (!ipaddress_parse_ipv4address((const char *)str_data, str_len, &value)) {
2020-09-03 19:32:12 -04:00
mp_raise_ValueError(translate("Not a valid IP string"));
2020-08-18 20:06:59 -04:00
}
buf = (uint8_t *)&value;
2020-08-18 20:06:59 -04:00
} 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;
}
2020-08-05 15:53:35 -04:00
}
2020-08-18 20:06:59 -04:00
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);
2020-08-05 15:53:35 -04:00
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);
const mp_obj_property_getter_t ipaddress_ipv4address_packed_obj = MP_PROPERTY_GETTER(ipaddress_ipv4address_packed_obj,
(mp_obj_t)&ipaddress_ipv4address_get_packed_obj);
2020-08-05 15:53:35 -04:00
//| 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);
const mp_obj_property_getter_t ipaddress_ipv4address_version_obj = MP_PROPERTY_GETTER(ipaddress_ipv4address_version_obj,
(mp_obj_t)&ipaddress_ipv4address_get_version_obj);
2020-08-05 15:53:35 -04:00
//| def __eq__(self, other: object) -> bool:
2020-08-05 15:53:35 -04:00
//| """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)) {
2020-08-05 15:53:35 -04:00
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),
2021-03-15 09:57:36 -04:00
common_hal_ipaddress_ipv4address_get_packed(rhs)));
2020-08-05 15:53:35 -04:00
} else {
return mp_const_false;
}
default:
return MP_OBJ_NULL; // op not supported
}
}
//| def __hash__(self) -> int:
2020-08-18 20:06:59 -04:00
//| """Returns a hash for the IPv4Address data."""
2020-08-05 15:53:35 -04:00
//| ...
//|
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);
2021-03-15 09:57:36 -04:00
const uint8_t *buf = (uint8_t *)buf_info.buf;
2020-08-05 15:53:35 -04:00
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,
2020-08-05 15:53:35 -04:00
.name = MP_QSTR_Address,
.make_new = ipaddress_ipv4address_make_new,
2021-07-07 10:07:00 -04:00
.locals_dict = (mp_obj_dict_t *)&ipaddress_ipv4address_locals_dict,
2020-08-05 15:53:35 -04:00
.print = ipaddress_ipv4address_print,
MP_TYPE_EXTENDED_FIELDS(
2021-07-07 10:07:00 -04:00
.unary_op = ipaddress_ipv4address_unary_op,
.binary_op = ipaddress_ipv4address_binary_op,
)
2020-08-05 15:53:35 -04:00
};