2017-03-30 15:04:31 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Linaro Limited
|
|
|
|
*
|
|
|
|
* 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 "py/mpconfig.h"
|
|
|
|
#ifdef MICROPY_PY_USOCKET
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2017-04-25 16:05:54 -06:00
|
|
|
#include "py/stream.h"
|
2017-03-30 15:04:31 -06:00
|
|
|
|
2017-04-01 00:20:07 -06:00
|
|
|
#include <stdio.h>
|
2017-03-30 15:04:31 -06:00
|
|
|
#include <zephyr.h>
|
2017-04-05 04:34:37 -06:00
|
|
|
// Zephyr's generated version header
|
|
|
|
#include <version.h>
|
2017-03-30 15:04:31 -06:00
|
|
|
#include <net/net_context.h>
|
2017-05-13 06:47:47 -06:00
|
|
|
#include <net/net_pkt.h>
|
2017-05-13 07:42:35 -06:00
|
|
|
#include <net/dns_resolve.h>
|
2017-08-07 12:41:03 -06:00
|
|
|
#ifdef CONFIG_NET_SOCKETS
|
|
|
|
#include <net/socket.h>
|
|
|
|
#endif
|
2017-03-30 15:04:31 -06:00
|
|
|
|
2017-05-25 02:41:02 -06:00
|
|
|
#define DEBUG_PRINT 0
|
|
|
|
#if DEBUG_PRINT // print debugging info
|
2017-04-01 00:20:07 -06:00
|
|
|
#define DEBUG_printf printf
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2017-03-30 15:04:31 -06:00
|
|
|
typedef struct _socket_obj_t {
|
|
|
|
mp_obj_base_t base;
|
2017-08-09 04:19:22 -06:00
|
|
|
int ctx;
|
2017-04-02 07:27:09 -06:00
|
|
|
|
|
|
|
#define STATE_NEW 0
|
|
|
|
#define STATE_CONNECTING 1
|
|
|
|
#define STATE_CONNECTED 2
|
|
|
|
#define STATE_PEER_CLOSED 3
|
|
|
|
int8_t state;
|
2017-03-30 15:04:31 -06:00
|
|
|
} socket_obj_t;
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t socket_type;
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }
|
2017-08-07 12:41:03 -06:00
|
|
|
#define RAISE_SOCK_ERRNO(x) { if ((int)(x) == -1) mp_raise_OSError(errno); }
|
2017-03-30 15:04:31 -06:00
|
|
|
|
2017-03-31 00:10:30 -06:00
|
|
|
STATIC void socket_check_closed(socket_obj_t *socket) {
|
2017-08-09 04:19:22 -06:00
|
|
|
if (socket->ctx == -1) {
|
2017-03-31 00:10:30 -06:00
|
|
|
// already closed
|
|
|
|
mp_raise_OSError(EBADF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct sockaddr *sockaddr) {
|
|
|
|
// We employ the fact that port and address offsets are the same for IPv4 & IPv6
|
|
|
|
struct sockaddr_in *sockaddr_in = (struct sockaddr_in*)sockaddr;
|
|
|
|
|
|
|
|
mp_obj_t *addr_items;
|
|
|
|
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
|
2017-08-09 04:19:22 -06:00
|
|
|
sockaddr_in->sin_family = net_context_get_family((void*)socket->ctx);
|
2017-03-31 00:10:30 -06:00
|
|
|
RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr));
|
|
|
|
sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1]));
|
|
|
|
}
|
|
|
|
|
2017-05-13 07:42:35 -06:00
|
|
|
STATIC mp_obj_t format_inet_addr(struct sockaddr *addr, mp_obj_t port) {
|
|
|
|
// We employ the fact that port and address offsets are the same for IPv4 & IPv6
|
|
|
|
struct sockaddr_in6 *sockaddr_in6 = (struct sockaddr_in6*)addr;
|
|
|
|
char buf[40];
|
2017-08-19 02:45:21 -06:00
|
|
|
net_addr_ntop(addr->sa_family, &sockaddr_in6->sin6_addr, buf, sizeof(buf));
|
|
|
|
mp_obj_tuple_t *tuple = mp_obj_new_tuple(addr->sa_family == AF_INET ? 2 : 4, NULL);
|
2017-05-13 07:42:35 -06:00
|
|
|
|
2017-11-15 19:17:51 -07:00
|
|
|
tuple->items[0] = mp_obj_new_str(buf, strlen(buf));
|
2017-05-13 07:42:35 -06:00
|
|
|
// We employ the fact that port offset is the same for IPv4 & IPv6
|
|
|
|
// not filled in
|
|
|
|
//tuple->items[1] = mp_obj_new_int(ntohs(((struct sockaddr_in*)addr)->sin_port));
|
|
|
|
tuple->items[1] = port;
|
|
|
|
|
2017-08-19 02:45:21 -06:00
|
|
|
if (addr->sa_family == AF_INET6) {
|
2017-05-13 07:42:35 -06:00
|
|
|
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); // flow_info
|
|
|
|
tuple->items[3] = MP_OBJ_NEW_SMALL_INT(sockaddr_in6->sin6_scope_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(tuple);
|
|
|
|
}
|
|
|
|
|
2017-04-03 21:28:14 -06:00
|
|
|
socket_obj_t *socket_new(void) {
|
|
|
|
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
|
|
|
|
socket->base.type = (mp_obj_t)&socket_type;
|
|
|
|
socket->state = STATE_NEW;
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2017-03-30 15:04:31 -06:00
|
|
|
// Methods
|
|
|
|
|
|
|
|
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
socket_obj_t *self = self_in;
|
2017-08-09 04:19:22 -06:00
|
|
|
if (self->ctx == -1) {
|
2017-03-30 15:04:31 -06:00
|
|
|
mp_printf(print, "<socket NULL>");
|
|
|
|
} else {
|
2017-08-09 04:19:22 -06:00
|
|
|
struct net_context *ctx = (void*)self->ctx;
|
2017-03-30 15:04:31 -06:00
|
|
|
mp_printf(print, "<socket %p type=%d>", ctx, net_context_get_type(ctx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t socket_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, 0, 4, false);
|
|
|
|
|
2017-04-03 21:28:14 -06:00
|
|
|
socket_obj_t *socket = socket_new();
|
2017-03-30 15:04:31 -06:00
|
|
|
|
|
|
|
int family = AF_INET;
|
|
|
|
int socktype = SOCK_STREAM;
|
|
|
|
int proto = -1;
|
|
|
|
|
|
|
|
if (n_args >= 1) {
|
|
|
|
family = mp_obj_get_int(args[0]);
|
|
|
|
if (n_args >= 2) {
|
|
|
|
socktype = mp_obj_get_int(args[1]);
|
|
|
|
if (n_args >= 3) {
|
|
|
|
proto = mp_obj_get_int(args[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proto == -1) {
|
|
|
|
proto = IPPROTO_TCP;
|
|
|
|
if (socktype != SOCK_STREAM) {
|
|
|
|
proto = IPPROTO_UDP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 04:19:22 -06:00
|
|
|
socket->ctx = zsock_socket(family, socktype, proto);
|
2017-08-07 12:41:03 -06:00
|
|
|
RAISE_SOCK_ERRNO(socket->ctx);
|
2017-03-30 15:04:31 -06:00
|
|
|
|
2017-04-03 21:19:23 -06:00
|
|
|
return MP_OBJ_FROM_PTR(socket);
|
2017-03-30 15:04:31 -06:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:10:30 -06:00
|
|
|
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
socket_check_closed(socket);
|
|
|
|
|
|
|
|
struct sockaddr sockaddr;
|
|
|
|
parse_inet_addr(socket, addr_in, &sockaddr);
|
|
|
|
|
2017-08-08 03:31:59 -06:00
|
|
|
int res = zsock_bind(socket->ctx, &sockaddr, sizeof(sockaddr));
|
|
|
|
RAISE_SOCK_ERRNO(res);
|
|
|
|
|
2017-03-31 00:10:30 -06:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
|
|
|
|
|
|
|
|
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
socket_check_closed(socket);
|
|
|
|
|
|
|
|
struct sockaddr sockaddr;
|
|
|
|
parse_inet_addr(socket, addr_in, &sockaddr);
|
|
|
|
|
2017-08-08 03:31:59 -06:00
|
|
|
int res = zsock_connect(socket->ctx, &sockaddr, sizeof(sockaddr));
|
|
|
|
RAISE_SOCK_ERRNO(res);
|
|
|
|
|
2017-03-31 00:10:30 -06:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
|
|
|
|
2017-04-07 07:47:10 -06:00
|
|
|
STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
socket_check_closed(socket);
|
|
|
|
|
|
|
|
mp_int_t backlog = mp_obj_get_int(backlog_in);
|
2017-08-08 03:31:59 -06:00
|
|
|
int res = zsock_listen(socket->ctx, backlog);
|
|
|
|
RAISE_SOCK_ERRNO(res);
|
|
|
|
|
2017-04-07 07:47:10 -06:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
|
|
|
|
|
2017-04-07 15:33:09 -06:00
|
|
|
STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
socket_check_closed(socket);
|
|
|
|
|
2017-08-08 03:31:59 -06:00
|
|
|
struct sockaddr sockaddr;
|
|
|
|
socklen_t addrlen = sizeof(sockaddr);
|
2017-08-09 04:19:22 -06:00
|
|
|
int ctx = zsock_accept(socket->ctx, &sockaddr, &addrlen);
|
2017-04-07 15:33:09 -06:00
|
|
|
|
|
|
|
socket_obj_t *socket2 = socket_new();
|
|
|
|
socket2->ctx = ctx;
|
|
|
|
|
|
|
|
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
|
|
|
|
client->items[0] = MP_OBJ_FROM_PTR(socket2);
|
|
|
|
// TODO
|
|
|
|
client->items[1] = mp_const_none;
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(client);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
|
|
|
|
|
2017-04-25 16:05:54 -06:00
|
|
|
STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
|
2017-03-31 14:13:12 -06:00
|
|
|
socket_obj_t *socket = self_in;
|
2017-08-09 01:17:15 -06:00
|
|
|
if (socket->ctx == -1) {
|
2017-04-25 16:05:54 -06:00
|
|
|
// already closed
|
|
|
|
*errcode = EBADF;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2017-03-31 14:13:12 -06:00
|
|
|
|
2017-08-09 01:17:15 -06:00
|
|
|
ssize_t len = zsock_send(socket->ctx, buf, size, 0);
|
|
|
|
if (len == -1) {
|
|
|
|
*errcode = errno;
|
2017-04-25 16:05:54 -06:00
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2017-03-31 14:13:12 -06:00
|
|
|
|
2017-04-25 16:05:54 -06:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
|
|
|
|
int err = 0;
|
|
|
|
mp_uint_t len = sock_write(self_in, bufinfo.buf, bufinfo.len, &err);
|
|
|
|
if (len == MP_STREAM_ERROR) {
|
|
|
|
mp_raise_OSError(err);
|
|
|
|
}
|
2017-04-14 10:46:27 -06:00
|
|
|
return mp_obj_new_int_from_uint(len);
|
2017-03-31 14:13:12 -06:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
|
|
|
|
|
2017-04-25 23:42:19 -06:00
|
|
|
STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *errcode) {
|
2017-04-01 00:20:07 -06:00
|
|
|
socket_obj_t *socket = self_in;
|
2017-08-09 04:19:22 -06:00
|
|
|
if (socket->ctx == -1) {
|
2017-04-25 23:42:19 -06:00
|
|
|
// already closed
|
|
|
|
*errcode = EBADF;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2017-04-01 00:20:07 -06:00
|
|
|
|
2017-08-09 01:22:41 -06:00
|
|
|
ssize_t recv_len = zsock_recv(socket->ctx, buf, max_len, 0);
|
|
|
|
if (recv_len == -1) {
|
|
|
|
*errcode = errno;
|
|
|
|
return MP_STREAM_ERROR;
|
2017-04-25 23:42:19 -06:00
|
|
|
}
|
2017-04-02 07:27:09 -06:00
|
|
|
|
2017-04-25 23:42:19 -06:00
|
|
|
return recv_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
|
|
|
|
mp_int_t max_len = mp_obj_get_int(len_in);
|
|
|
|
vstr_t vstr;
|
|
|
|
// +1 to accommodate for trailing \0
|
|
|
|
vstr_init_len(&vstr, max_len + 1);
|
|
|
|
|
|
|
|
int err;
|
|
|
|
mp_uint_t len = sock_read(self_in, vstr.buf, max_len, &err);
|
|
|
|
|
|
|
|
if (len == MP_STREAM_ERROR) {
|
|
|
|
vstr_clear(&vstr);
|
|
|
|
mp_raise_OSError(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
vstr_clear(&vstr);
|
|
|
|
return mp_const_empty_bytes;
|
2017-04-01 00:20:07 -06:00
|
|
|
}
|
|
|
|
|
2017-04-25 23:42:19 -06:00
|
|
|
vstr.len = len;
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2017-04-01 00:20:07 -06:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);
|
|
|
|
|
2017-05-12 12:54:32 -06:00
|
|
|
STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args; // always 4
|
|
|
|
mp_warning("setsockopt() not implemented");
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
|
|
|
|
|
2017-04-27 05:57:49 -06:00
|
|
|
STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args;
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile);
|
|
|
|
|
2017-03-30 15:04:31 -06:00
|
|
|
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
2017-08-07 12:41:03 -06:00
|
|
|
if (socket->ctx != -1) {
|
|
|
|
int res = zsock_close(socket->ctx);
|
|
|
|
RAISE_SOCK_ERRNO(res);
|
|
|
|
socket->ctx = -1;
|
2017-03-30 15:04:31 -06:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
|
|
|
|
|
2017-08-21 05:34:23 -06:00
|
|
|
STATIC const mp_rom_map_elem_t socket_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socket_bind_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socket_connect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socket_listen_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socket_accept_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socket_send_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&socket_recv_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socket_setsockopt_obj) },
|
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_makefile), MP_ROM_PTR(&socket_makefile_obj) },
|
2017-03-30 15:04:31 -06:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
|
|
|
|
|
2017-04-25 16:05:54 -06:00
|
|
|
STATIC const mp_stream_p_t socket_stream_p = {
|
2017-04-25 23:42:19 -06:00
|
|
|
.read = sock_read,
|
2017-04-25 16:05:54 -06:00
|
|
|
.write = sock_write,
|
|
|
|
//.ioctl = sock_ioctl,
|
|
|
|
};
|
|
|
|
|
2017-03-30 15:04:31 -06:00
|
|
|
STATIC const mp_obj_type_t socket_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_socket,
|
|
|
|
.print = socket_print,
|
|
|
|
.make_new = socket_make_new,
|
2017-04-25 16:05:54 -06:00
|
|
|
.protocol = &socket_stream_p,
|
2017-03-30 15:04:31 -06:00
|
|
|
.locals_dict = (mp_obj_t)&socket_locals_dict,
|
|
|
|
};
|
|
|
|
|
2017-05-13 07:42:35 -06:00
|
|
|
//
|
|
|
|
// getaddrinfo() implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
typedef struct _getaddrinfo_state_t {
|
|
|
|
mp_obj_t result;
|
|
|
|
struct k_sem sem;
|
|
|
|
mp_obj_t port;
|
2017-05-24 10:54:26 -06:00
|
|
|
int status;
|
2017-05-13 07:42:35 -06:00
|
|
|
} getaddrinfo_state_t;
|
|
|
|
|
|
|
|
void dns_resolve_cb(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data) {
|
|
|
|
getaddrinfo_state_t *state = user_data;
|
2017-05-24 10:54:26 -06:00
|
|
|
DEBUG_printf("dns status: %d\n", status);
|
2017-05-13 07:42:35 -06:00
|
|
|
|
|
|
|
if (info == NULL) {
|
2017-05-24 10:54:26 -06:00
|
|
|
if (status == DNS_EAI_ALLDONE) {
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
state->status = status;
|
2017-05-13 07:42:35 -06:00
|
|
|
k_sem_give(&state->sem);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL);
|
|
|
|
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(info->ai_family);
|
|
|
|
// info->ai_socktype not filled
|
|
|
|
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM);
|
|
|
|
// info->ai_protocol not filled
|
|
|
|
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(IPPROTO_TCP);
|
|
|
|
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
|
|
|
tuple->items[4] = format_inet_addr(&info->ai_addr, state->port);
|
|
|
|
mp_obj_list_append(state->result, MP_OBJ_FROM_PTR(tuple));
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_t host_in = args[0], port_in = args[1];
|
|
|
|
const char *host = mp_obj_str_get_str(host_in);
|
|
|
|
mp_int_t family = 0;
|
|
|
|
if (n_args > 2) {
|
|
|
|
family = mp_obj_get_int(args[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
getaddrinfo_state_t state;
|
|
|
|
// Just validate that it's int
|
|
|
|
(void)mp_obj_get_int(port_in);
|
|
|
|
state.port = port_in;
|
|
|
|
state.result = mp_obj_new_list(0, NULL);
|
|
|
|
k_sem_init(&state.sem, 0, UINT_MAX);
|
|
|
|
|
|
|
|
for (int i = 2; i--;) {
|
|
|
|
int type = (family != AF_INET6 ? DNS_QUERY_TYPE_A : DNS_QUERY_TYPE_AAAA);
|
2017-05-16 15:21:22 -06:00
|
|
|
RAISE_ERRNO(dns_get_addr_info(host, type, NULL, dns_resolve_cb, &state, 3000));
|
2017-05-13 07:42:35 -06:00
|
|
|
k_sem_take(&state.sem, K_FOREVER);
|
|
|
|
if (family != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
family = AF_INET6;
|
|
|
|
}
|
|
|
|
|
2017-05-24 10:54:26 -06:00
|
|
|
// Raise error only if there's nothing to return, otherwise
|
|
|
|
// it may be IPv4 vs IPv6 differences.
|
2017-06-03 07:10:12 -06:00
|
|
|
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(state.result));
|
|
|
|
if (state.status != 0 && len == 0) {
|
2017-05-24 10:54:26 -06:00
|
|
|
mp_raise_OSError(state.status);
|
|
|
|
}
|
|
|
|
|
2017-05-13 07:42:35 -06:00
|
|
|
return state.result;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_getaddrinfo_obj, 2, 3, mod_getaddrinfo);
|
|
|
|
|
|
|
|
|
2017-05-13 06:47:47 -06:00
|
|
|
STATIC mp_obj_t pkt_get_info(void) {
|
|
|
|
struct k_mem_slab *rx, *tx;
|
|
|
|
struct net_buf_pool *rx_data, *tx_data;
|
|
|
|
net_pkt_get_info(&rx, &tx, &rx_data, &tx_data);
|
2017-05-13 06:22:22 -06:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
|
2017-05-13 06:47:47 -06:00
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(rx));
|
|
|
|
t->items[1] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(tx));
|
2017-05-13 06:22:22 -06:00
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT(rx_data->avail_count);
|
|
|
|
t->items[3] = MP_OBJ_NEW_SMALL_INT(tx_data->avail_count);
|
|
|
|
return MP_OBJ_FROM_PTR(t);
|
|
|
|
}
|
2017-05-13 06:47:47 -06:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pkt_get_info_obj, pkt_get_info);
|
2017-05-13 06:22:22 -06:00
|
|
|
|
2017-08-21 05:34:23 -06:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_usocket_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usocket) },
|
2017-03-30 15:04:31 -06:00
|
|
|
// objects
|
2017-08-21 05:34:23 -06:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&socket_type) },
|
2017-03-30 15:04:31 -06:00
|
|
|
// class constants
|
2017-08-21 05:34:23 -06:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AF_INET), MP_ROM_INT(AF_INET) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AF_INET6), MP_ROM_INT(AF_INET6) },
|
2017-03-30 15:04:31 -06:00
|
|
|
|
2017-08-21 05:34:23 -06:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SOCK_STREAM), MP_ROM_INT(SOCK_STREAM) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SOCK_DGRAM), MP_ROM_INT(SOCK_DGRAM) },
|
2017-05-12 12:55:42 -06:00
|
|
|
|
2017-08-21 05:34:23 -06:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SOL_SOCKET), MP_ROM_INT(1) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SO_REUSEADDR), MP_ROM_INT(2) },
|
2017-05-13 06:22:22 -06:00
|
|
|
|
2017-08-21 05:34:23 -06:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&mod_getaddrinfo_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pkt_get_info), MP_ROM_PTR(&pkt_get_info_obj) },
|
2017-03-30 15:04:31 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_usocket = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_usocket_globals,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_USOCKET
|