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
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* 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 <stdio.h>
#include <string.h>
#include "py/objtuple.h"
#include "py/objlist.h"
#include "py/runtime.h"
#include "py/mperrno.h"
#include "shared-bindings/ipaddress/__init__.h"
#include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/socketpool/SocketPool.h"
//| class SocketPool:
//| """A pool of socket resources available for the given radio. Only one
//| SocketPool can be created for each radio.
//|
//| SocketPool should be used in place of CPython's socket which provides
//| a pool of sockets provided by the underlying OS."""
//|
STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, false);
socketpool_socketpool_obj_t *s = m_new_obj_with_finaliser(socketpool_socketpool_obj_t);
s->base.type = &socketpool_socketpool_type;
mp_obj_t radio = args[0];
common_hal_socketpool_socketpool_construct(s, radio);
return MP_OBJ_FROM_PTR(s);
}
//| AF_INET: int
//| AF_INET6: int
//| SOCK_STREAM: int
//| SOCK_DGRAM: int
//| SOCK_RAW: int
//|
//| def socket(self, family: int = AF_INET, type: int = SOCK_STREAM) -> socketpool.Socket:
//| """Create a new socket
//|
//| :param ~int family: AF_INET or AF_INET6
//| :param ~int type: SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
//|
//| The ``proto`` (protocol) and ``fileno`` arguments available in ``socket.socket()``
//| in CPython are not supported.
//| """
//| ...
//|
STATIC mp_obj_t socketpool_socketpool_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_family, ARG_type };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_family, MP_ARG_INT, {.u_int = SOCKETPOOL_AF_INET} },
{ MP_QSTR_type, MP_ARG_INT, {.u_int = SOCKETPOOL_SOCK_STREAM} },
};
socketpool_socketpool_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
socketpool_socketpool_addressfamily_t family = args[ARG_family].u_int;
socketpool_socketpool_sock_t type = args[ARG_type].u_int;
return common_hal_socketpool_socket(self, family, type);
}
MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_socket_obj, 1, socketpool_socketpool_socket);
//| def getaddrinfo(self, host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> Tuple[int, int, int, str, Tuple[str, int]]:
//| """Gets the address information for a hostname and port
//|
//| Returns the appropriate family, socket type, socket protocol and
//| address information to call socket.socket() and socket.connect() with,
//| as a tuple."""
//| ...
//|
STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_host, ARG_port, ARG_family, ARG_type, ARG_proto, ARG_flags };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_host, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_port, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_family, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_type, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_port, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_flags, MP_ARG_INT, {.u_int = 0} },
};
socketpool_socketpool_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
const char *host = mp_obj_str_get_str(args[ARG_host].u_obj);
mp_int_t port = args[ARG_port].u_int;
mp_obj_t ip_str = mp_const_none;
if (strlen(host) > 0 && ipaddress_parse_ipv4address(host, strlen(host), NULL)) {
ip_str = args[ARG_host].u_obj;
}
if (ip_str == mp_const_none) {
ip_str = common_hal_socketpool_socketpool_gethostbyname(self, host);
}
if (ip_str == mp_const_none) {
mp_raise_OSError(-2); // socket.EAI_NONAME from CPython
}
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_AF_INET);
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_SOCK_STREAM);
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
mp_obj_tuple_t *sockaddr = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
sockaddr->items[0] = ip_str;
sockaddr->items[1] = MP_OBJ_NEW_SMALL_INT(port);
tuple->items[4] = MP_OBJ_FROM_PTR(sockaddr);
return mp_obj_new_list(1, (mp_obj_t *)&tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_getaddrinfo_obj, 1, socketpool_socketpool_getaddrinfo);
STATIC const mp_rom_map_elem_t socketpool_socketpool_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&socketpool_socketpool_socket_obj) },
{ MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&socketpool_socketpool_getaddrinfo_obj) },
// class constants
{ MP_ROM_QSTR(MP_QSTR_AF_INET), MP_ROM_INT(SOCKETPOOL_AF_INET) },
{ MP_ROM_QSTR(MP_QSTR_AF_INET6), MP_ROM_INT(SOCKETPOOL_AF_INET6) },
{ MP_ROM_QSTR(MP_QSTR_SOCK_STREAM), MP_ROM_INT(SOCKETPOOL_SOCK_STREAM) },
{ MP_ROM_QSTR(MP_QSTR_SOCK_DGRAM), MP_ROM_INT(SOCKETPOOL_SOCK_DGRAM) },
{ MP_ROM_QSTR(MP_QSTR_SOCK_RAW), MP_ROM_INT(SOCKETPOOL_SOCK_RAW) },
};
STATIC MP_DEFINE_CONST_DICT(socketpool_socketpool_locals_dict, socketpool_socketpool_locals_dict_table);
const mp_obj_type_t socketpool_socketpool_type = {
{ &mp_type_type },
.name = MP_QSTR_SocketPool,
.make_new = socketpool_socketpool_make_new,
.locals_dict = (mp_obj_dict_t *)&socketpool_socketpool_locals_dict,
};
|