2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-05-14 08:51:57 -04:00
|
|
|
* Copyright (c) 2014-2018 Paul Sokolovsky
|
2019-05-17 04:02:44 -04:00
|
|
|
* Copyright (c) 2014-2019 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-18 16:47:44 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-04-19 19:00:12 -04:00
|
|
|
#include <fcntl.h>
|
2014-03-16 02:24:34 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-01-18 16:47:44 -05:00
|
|
|
#include <sys/socket.h>
|
2014-03-16 02:24:34 -04:00
|
|
|
#include <netinet/in.h>
|
2014-01-18 16:47:44 -05:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2018-10-07 13:58:42 -04:00
|
|
|
#include <math.h>
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2015-01-01 15:40:19 -05:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
#include "py/builtin.h"
|
2016-10-06 23:09:59 -04:00
|
|
|
#include "py/mphal.h"
|
2020-01-22 19:14:03 -05:00
|
|
|
#include "py/mpthread.h"
|
2022-03-08 20:32:44 -05:00
|
|
|
#include "extmod/vfs.h"
|
2021-09-16 21:48:37 -04:00
|
|
|
#include <poll.h>
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
#if MICROPY_PY_SOCKET
|
|
|
|
|
2014-05-30 18:48:26 -04:00
|
|
|
/*
|
|
|
|
The idea of this module is to implement reasonable minimum of
|
|
|
|
socket-related functions to write typical clients and servers.
|
2014-10-09 13:43:10 -04:00
|
|
|
The module named "usocket" on purpose, to allow to make
|
2014-05-30 18:48:26 -04:00
|
|
|
Python-level module more (or fully) compatible with CPython
|
|
|
|
"socket", e.g.:
|
|
|
|
---- socket.py ----
|
2014-10-09 13:43:10 -04:00
|
|
|
from usocket import *
|
2014-05-30 18:48:26 -04:00
|
|
|
from socket_more_funcs import *
|
|
|
|
from socket_more_funcs2 import *
|
|
|
|
-------------------
|
|
|
|
I.e. this module should stay lean, and more functions (if needed)
|
2017-05-29 03:08:14 -04:00
|
|
|
should be add to separate modules (C or Python level).
|
2014-05-30 18:48:26 -04:00
|
|
|
*/
|
|
|
|
|
2016-08-06 18:21:42 -04:00
|
|
|
// This type must "inherit" from mp_obj_fdfile_t, i.e. matching subset of
|
|
|
|
// fields should have the same layout.
|
2014-01-18 16:47:44 -05:00
|
|
|
typedef struct _mp_obj_socket_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int fd;
|
2018-10-07 13:58:42 -04:00
|
|
|
bool blocking;
|
2014-01-18 16:47:44 -05:00
|
|
|
} mp_obj_socket_t;
|
|
|
|
|
2016-08-06 18:21:42 -04:00
|
|
|
const mp_obj_type_t mp_type_socket;
|
2014-01-18 16:47:44 -05:00
|
|
|
|
|
|
|
// Helper functions
|
2015-07-13 18:46:40 -04:00
|
|
|
static inline mp_obj_t mp_obj_from_sockaddr(const struct sockaddr *addr, socklen_t len) {
|
|
|
|
return mp_obj_new_bytes((const byte *)addr, len);
|
|
|
|
}
|
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_socket_t *socket_new(int fd) {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_socket_t *o = mp_obj_malloc(mp_obj_socket_t, &mp_type_socket);
|
2014-01-18 16:47:44 -05:00
|
|
|
o->fd = fd;
|
2018-10-07 13:58:42 -04:00
|
|
|
o->blocking = true;
|
2014-01-18 16:47:44 -05:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<_socket %d>", self->fd);
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
2014-07-27 17:38:58 -04:00
|
|
|
STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2020-01-23 00:54:38 -05:00
|
|
|
ssize_t r;
|
|
|
|
MP_HAL_RETRY_SYSCALL(r, read(o->fd, buf, size), {
|
2018-10-07 13:58:42 -04:00
|
|
|
// On blocking socket, we get EAGAIN in case SO_RCVTIMEO/SO_SNDTIMEO
|
|
|
|
// timed out, and need to convert that to ETIMEDOUT.
|
|
|
|
if (err == EAGAIN && o->blocking) {
|
|
|
|
err = MP_ETIMEDOUT;
|
2016-09-02 01:45:13 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 13:58:42 -04:00
|
|
|
*errcode = err;
|
2014-07-27 17:38:58 -04:00
|
|
|
return MP_STREAM_ERROR;
|
2020-01-23 00:54:38 -05:00
|
|
|
});
|
|
|
|
return (mp_uint_t)r;
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
2014-07-27 17:38:58 -04:00
|
|
|
STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2020-01-23 00:54:38 -05:00
|
|
|
ssize_t r;
|
|
|
|
MP_HAL_RETRY_SYSCALL(r, write(o->fd, buf, size), {
|
2018-10-07 13:58:42 -04:00
|
|
|
// On blocking socket, we get EAGAIN in case SO_RCVTIMEO/SO_SNDTIMEO
|
|
|
|
// timed out, and need to convert that to ETIMEDOUT.
|
|
|
|
if (err == EAGAIN && o->blocking) {
|
|
|
|
err = MP_ETIMEDOUT;
|
2016-09-02 01:45:13 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 13:58:42 -04:00
|
|
|
*errcode = err;
|
2014-07-27 17:38:58 -04:00
|
|
|
return MP_STREAM_ERROR;
|
2020-01-23 00:54:38 -05:00
|
|
|
});
|
|
|
|
return (mp_uint_t)r;
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:48:53 -05:00
|
|
|
STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(o_in);
|
|
|
|
(void)arg;
|
|
|
|
switch (request) {
|
|
|
|
case MP_STREAM_CLOSE:
|
|
|
|
// There's a POSIX drama regarding return value of close in general,
|
|
|
|
// and EINTR error in particular. See e.g.
|
|
|
|
// http://lwn.net/Articles/576478/
|
|
|
|
// http://austingroupbugs.net/view.php?id=529
|
|
|
|
// The rationale MicroPython follows is that close() just releases
|
|
|
|
// file descriptor. If you're interested to catch I/O errors before
|
|
|
|
// closing fd, fsync() it.
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2018-03-07 01:48:53 -05:00
|
|
|
close(self->fd);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2018-03-07 01:48:53 -05:00
|
|
|
return 0;
|
|
|
|
|
2018-07-19 23:09:49 -04:00
|
|
|
case MP_STREAM_GET_FILENO:
|
|
|
|
return self->fd;
|
|
|
|
|
2021-09-16 21:48:37 -04:00
|
|
|
#if MICROPY_PY_USELECT
|
|
|
|
case MP_STREAM_POLL: {
|
|
|
|
mp_uint_t ret = 0;
|
|
|
|
uint8_t pollevents = 0;
|
|
|
|
if (arg & MP_STREAM_POLL_RD) {
|
|
|
|
pollevents |= POLLIN;
|
|
|
|
}
|
|
|
|
if (arg & MP_STREAM_POLL_WR) {
|
|
|
|
pollevents |= POLLOUT;
|
|
|
|
}
|
|
|
|
struct pollfd pfd = { .fd = self->fd, .events = pollevents };
|
|
|
|
if (poll(&pfd, 1, 0) > 0) {
|
|
|
|
if (pfd.revents & POLLIN) {
|
|
|
|
ret |= MP_STREAM_POLL_RD;
|
|
|
|
}
|
|
|
|
if (pfd.revents & POLLOUT) {
|
|
|
|
ret |= MP_STREAM_POLL_WR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-07 01:48:53 -05:00
|
|
|
default:
|
|
|
|
*errcode = MP_EINVAL;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t socket_fileno(mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->fd);
|
2014-02-08 14:15:46 -05:00
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_fileno_obj, socket_fileno);
|
2014-02-08 14:15:46 -05:00
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-18 17:31:24 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 17:59:24 -04:00
|
|
|
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
|
2020-01-23 00:54:38 -05:00
|
|
|
|
|
|
|
// special case of PEP 475 to retry only if blocking so we can't use
|
|
|
|
// MP_HAL_RETRY_SYSCALL() here
|
|
|
|
for (;;) {
|
|
|
|
MP_THREAD_GIL_EXIT();
|
|
|
|
int r = connect(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
|
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
if (r == -1) {
|
|
|
|
int err = errno;
|
|
|
|
if (self->blocking) {
|
|
|
|
if (err == EINTR) {
|
|
|
|
mp_handle_pending(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// EINPROGRESS on a blocking socket means the operation timed out
|
|
|
|
if (err == EINPROGRESS) {
|
|
|
|
err = MP_ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_raise_OSError(err);
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
2019-05-28 03:22:54 -04:00
|
|
|
}
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-18 17:31:24 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 17:59:24 -04:00
|
|
|
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-18 16:47:44 -05:00
|
|
|
int r = bind(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 16:47:44 -05:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2022-02-16 11:44:17 -05:00
|
|
|
// method socket.listen([backlog])
|
|
|
|
STATIC mp_obj_t socket_listen(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
2022-04-04 09:07:35 -04:00
|
|
|
int backlog = MICROPY_PY_USOCKET_LISTEN_BACKLOG_DEFAULT;
|
2022-02-16 11:44:17 -05:00
|
|
|
if (n_args > 1) {
|
|
|
|
backlog = (int)mp_obj_get_int(args[1]);
|
|
|
|
backlog = (backlog < 0) ? 0 : backlog;
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2022-02-16 11:44:17 -05:00
|
|
|
int r = listen(self->fd, backlog);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 16:47:44 -05:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-02-16 11:44:17 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_listen_obj, 1, 2, socket_listen);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-01-21 16:53:47 -05:00
|
|
|
// sockaddr_storage isn't stack-friendly (129 bytes or so)
|
|
|
|
// struct sockaddr_storage addr;
|
|
|
|
byte addr[32];
|
2014-01-18 16:47:44 -05:00
|
|
|
socklen_t addr_len = sizeof(addr);
|
2020-01-23 00:54:38 -05:00
|
|
|
int fd;
|
|
|
|
MP_HAL_RETRY_SYSCALL(fd, accept(self->fd, (struct sockaddr *)&addr, &addr_len), {
|
2019-05-28 03:22:54 -04:00
|
|
|
// EAGAIN on a blocking socket means the operation timed out
|
2020-01-23 00:54:38 -05:00
|
|
|
if (self->blocking && err == EAGAIN) {
|
|
|
|
err = MP_ETIMEDOUT;
|
|
|
|
}
|
|
|
|
mp_raise_OSError(err);
|
|
|
|
});
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
|
|
|
t->items[0] = MP_OBJ_FROM_PTR(socket_new(fd));
|
2014-01-18 16:47:44 -05:00
|
|
|
t->items[1] = mp_obj_new_bytearray(addr_len, &addr);
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-05-06 18:51:35 -04:00
|
|
|
// Note: besides flag param, this differs from read() in that
|
|
|
|
// this does not swallow blocking errors (EAGAIN, EWOULDBLOCK) -
|
|
|
|
// these would be thrown as exceptions.
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t socket_recv(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-01-20 12:42:39 -05:00
|
|
|
int sz = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (n_args > 2) {
|
|
|
|
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
|
2014-08-10 14:20:40 -04:00
|
|
|
byte *buf = m_new(byte, sz);
|
2020-01-23 00:54:38 -05:00
|
|
|
ssize_t out_sz;
|
|
|
|
MP_HAL_RETRY_SYSCALL(out_sz, recv(self->fd, buf, sz, flags), mp_raise_OSError(err));
|
2014-08-10 14:20:40 -04:00
|
|
|
mp_obj_t ret = mp_obj_new_str_of_type(&mp_type_bytes, buf, out_sz);
|
2014-05-25 17:27:57 -04:00
|
|
|
m_del(char, buf, sz);
|
|
|
|
return ret;
|
2014-01-20 12:42:39 -05:00
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_recv_obj, 2, 3, socket_recv);
|
2014-01-20 12:42:39 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t socket_recvfrom(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2015-07-13 18:46:40 -04:00
|
|
|
int sz = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (n_args > 2) {
|
|
|
|
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
|
|
|
|
byte *buf = m_new(byte, sz);
|
2020-01-23 00:54:38 -05:00
|
|
|
ssize_t out_sz;
|
|
|
|
MP_HAL_RETRY_SYSCALL(out_sz, recvfrom(self->fd, buf, sz, flags, (struct sockaddr *)&addr, &addr_len),
|
|
|
|
mp_raise_OSError(err));
|
2015-07-13 18:46:40 -04:00
|
|
|
mp_obj_t buf_o = mp_obj_new_str_of_type(&mp_type_bytes, buf, out_sz);
|
|
|
|
m_del(char, buf, sz);
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
2015-07-13 18:46:40 -04:00
|
|
|
t->items[0] = buf_o;
|
|
|
|
t->items[1] = mp_obj_from_sockaddr((struct sockaddr *)&addr, addr_len);
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-07-13 18:46:40 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_recvfrom_obj, 2, 3, socket_recvfrom);
|
|
|
|
|
2014-05-06 18:51:35 -04:00
|
|
|
// Note: besides flag param, this differs from write() in that
|
|
|
|
// this does not swallow blocking errors (EAGAIN, EWOULDBLOCK) -
|
|
|
|
// these would be thrown as exceptions.
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t socket_send(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-01-20 12:42:39 -05:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (n_args > 2) {
|
|
|
|
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
|
2014-04-25 22:53:02 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
|
2020-01-23 00:54:38 -05:00
|
|
|
ssize_t out_sz;
|
|
|
|
MP_HAL_RETRY_SYSCALL(out_sz, send(self->fd, bufinfo.buf, bufinfo.len, flags),
|
|
|
|
mp_raise_OSError(err));
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(out_sz);
|
2014-01-20 12:42:39 -05:00
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_send_obj, 2, 3, socket_send);
|
2014-01-20 12:42:39 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t socket_sendto(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2015-07-12 06:51:55 -04:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
mp_obj_t dst_addr = args[2];
|
|
|
|
if (n_args > 3) {
|
|
|
|
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
dst_addr = args[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_buffer_info_t bufinfo, addr_bi;
|
|
|
|
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
|
|
|
|
mp_get_buffer_raise(dst_addr, &addr_bi, MP_BUFFER_READ);
|
2020-01-23 00:54:38 -05:00
|
|
|
ssize_t out_sz;
|
|
|
|
MP_HAL_RETRY_SYSCALL(out_sz, sendto(self->fd, bufinfo.buf, bufinfo.len, flags,
|
|
|
|
(struct sockaddr *)addr_bi.buf, addr_bi.len), mp_raise_OSError(err));
|
2015-07-12 06:51:55 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(out_sz);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_sendto_obj, 3, 4, socket_sendto);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args; // always 4
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-01-25 18:59:52 -05:00
|
|
|
int level = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
int option = mp_obj_get_int(args[2]);
|
|
|
|
|
|
|
|
const void *optval;
|
|
|
|
socklen_t optlen;
|
2015-10-10 07:59:26 -04:00
|
|
|
int val;
|
2019-01-30 06:05:48 -05:00
|
|
|
if (mp_obj_is_int(args[3])) {
|
2015-10-10 07:59:26 -04:00
|
|
|
val = mp_obj_int_get_truncated(args[3]);
|
2014-01-25 18:59:52 -05:00
|
|
|
optval = &val;
|
|
|
|
optlen = sizeof(val);
|
|
|
|
} else {
|
2014-04-18 17:31:24 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 17:59:24 -04:00
|
|
|
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
|
2014-01-25 18:59:52 -05:00
|
|
|
optval = bufinfo.buf;
|
|
|
|
optlen = bufinfo.len;
|
|
|
|
}
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-25 18:59:52 -05:00
|
|
|
int r = setsockopt(self->fd, level, option, optval, optlen);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-25 18:59:52 -05:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
|
2014-01-25 18:59:52 -05:00
|
|
|
|
2014-04-19 19:00:12 -04:00
|
|
|
STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-19 19:00:12 -04:00
|
|
|
int val = mp_obj_is_true(flag_in);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-04-19 19:00:12 -04:00
|
|
|
int flags = fcntl(self->fd, F_GETFL, 0);
|
2020-01-22 19:14:03 -05:00
|
|
|
if (flags == -1) {
|
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
RAISE_ERRNO(flags, errno);
|
|
|
|
}
|
2014-04-19 19:00:12 -04:00
|
|
|
if (val) {
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
} else {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
flags = fcntl(self->fd, F_SETFL, flags);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-04-19 19:00:12 -04:00
|
|
|
RAISE_ERRNO(flags, errno);
|
2018-10-07 13:58:42 -04:00
|
|
|
self->blocking = val;
|
2014-04-19 19:00:12 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
|
|
|
|
|
2016-09-02 01:45:13 -04:00
|
|
|
STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
|
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
struct timeval tv = {0, };
|
2018-10-07 13:58:42 -04:00
|
|
|
bool new_blocking = true;
|
|
|
|
|
2019-05-06 17:24:22 -04:00
|
|
|
// Timeout of None means no timeout, which in POSIX is signified with 0 timeout,
|
|
|
|
// and that's how 'tv' is initialized above
|
|
|
|
if (timeout_in != mp_const_none) {
|
2016-09-02 01:45:13 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2018-10-07 13:58:42 -04:00
|
|
|
mp_float_t val = mp_obj_get_float(timeout_in);
|
2020-04-09 03:05:48 -04:00
|
|
|
mp_float_t ipart;
|
2020-04-13 14:56:31 -04:00
|
|
|
tv.tv_usec = (time_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.));
|
|
|
|
tv.tv_sec = (suseconds_t)ipart;
|
2018-10-07 13:58:42 -04:00
|
|
|
#else
|
|
|
|
tv.tv_sec = mp_obj_get_int(timeout_in);
|
2016-09-02 01:45:13 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-07 13:58:42 -04:00
|
|
|
// For SO_RCVTIMEO/SO_SNDTIMEO, zero timeout means infinity, but
|
|
|
|
// for Python API it means non-blocking.
|
|
|
|
if (tv.tv_sec == 0 && tv.tv_usec == 0) {
|
|
|
|
new_blocking = false;
|
|
|
|
}
|
2016-09-02 01:45:13 -04:00
|
|
|
}
|
2018-10-07 13:58:42 -04:00
|
|
|
|
2019-05-06 17:24:22 -04:00
|
|
|
if (new_blocking) {
|
|
|
|
int r;
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2019-05-06 17:24:22 -04:00
|
|
|
r = setsockopt(self->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
|
2020-01-22 19:14:03 -05:00
|
|
|
if (r == -1) {
|
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
}
|
2019-05-06 17:24:22 -04:00
|
|
|
r = setsockopt(self->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2019-05-06 17:24:22 -04:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
}
|
|
|
|
|
2018-10-07 13:58:42 -04:00
|
|
|
if (self->blocking != new_blocking) {
|
|
|
|
socket_setblocking(self_in, mp_obj_new_bool(new_blocking));
|
|
|
|
}
|
|
|
|
|
2016-09-02 01:45:13 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_settimeout_obj, socket_settimeout);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) {
|
2014-05-24 14:24:37 -04:00
|
|
|
// TODO: CPython explicitly says that closing returned object doesn't close
|
|
|
|
// the original socket (Python2 at all says that fd is dup()ed). But we
|
|
|
|
// save on the bloat.
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-05-24 14:24:37 -04:00
|
|
|
mp_obj_t *new_args = alloca(n_args * sizeof(mp_obj_t));
|
|
|
|
memcpy(new_args + 1, args + 1, (n_args - 1) * sizeof(mp_obj_t));
|
2014-07-31 05:49:14 -04:00
|
|
|
new_args[0] = MP_OBJ_NEW_SMALL_INT(self->fd);
|
2022-03-08 20:32:44 -05:00
|
|
|
return mp_vfs_open(n_args, new_args, (mp_map_t *)&mp_const_empty_map);
|
2014-05-24 14:24:37 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile);
|
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)type_in;
|
|
|
|
(void)n_kw;
|
|
|
|
|
2014-01-18 16:47:44 -05:00
|
|
|
int family = AF_INET;
|
|
|
|
int type = SOCK_STREAM;
|
|
|
|
int proto = 0;
|
|
|
|
|
|
|
|
if (n_args > 0) {
|
2019-01-30 06:05:48 -05:00
|
|
|
assert(mp_obj_is_small_int(args[0]));
|
2014-01-18 16:47:44 -05:00
|
|
|
family = MP_OBJ_SMALL_INT_VALUE(args[0]);
|
|
|
|
if (n_args > 1) {
|
2019-01-30 06:05:48 -05:00
|
|
|
assert(mp_obj_is_small_int(args[1]));
|
2014-01-18 16:47:44 -05:00
|
|
|
type = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
if (n_args > 2) {
|
2019-01-30 06:05:48 -05:00
|
|
|
assert(mp_obj_is_small_int(args[2]));
|
2014-01-18 16:47:44 -05:00
|
|
|
proto = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-18 16:47:44 -05:00
|
|
|
int fd = socket(family, type, proto);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 16:47:44 -05:00
|
|
|
RAISE_ERRNO(fd, errno);
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(socket_new(fd));
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t usocket_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_fileno), MP_ROM_PTR(&socket_fileno_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_makefile), MP_ROM_PTR(&socket_makefile_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_connect), MP_ROM_PTR(&socket_connect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socket_bind_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_recv), MP_ROM_PTR(&socket_recv_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_recvfrom), MP_ROM_PTR(&socket_recvfrom_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socket_send_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socket_sendto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socket_setsockopt_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
|
2016-09-02 01:45:13 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socket_settimeout_obj) },
|
2018-03-07 01:48:53 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
2014-01-18 16:47:44 -05:00
|
|
|
};
|
|
|
|
|
2014-10-09 13:43:10 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(usocket_locals_dict, usocket_locals_dict_table);
|
2014-03-26 17:47:19 -04:00
|
|
|
|
2014-10-09 13:43:10 -04:00
|
|
|
STATIC const mp_stream_p_t usocket_stream_p = {
|
2014-04-05 18:02:23 -04:00
|
|
|
.read = socket_read,
|
|
|
|
.write = socket_write,
|
2018-03-07 01:48:53 -05:00
|
|
|
.ioctl = socket_ioctl,
|
2014-04-05 18:02:23 -04:00
|
|
|
};
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
mp_type_socket,
|
|
|
|
MP_QSTR_socket,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
socket_make_new,
|
|
|
|
print, socket_print,
|
|
|
|
getiter, NULL,
|
|
|
|
iternext, NULL,
|
|
|
|
protocol, &usocket_stream_p,
|
|
|
|
locals_dict, (mp_obj_dict_t *)&usocket_locals_dict
|
|
|
|
);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2015-07-14 17:05:49 -04:00
|
|
|
#define BINADDR_MAX_LEN sizeof(struct in6_addr)
|
|
|
|
STATIC mp_obj_t mod_socket_inet_pton(mp_obj_t family_in, mp_obj_t addr_in) {
|
|
|
|
int family = mp_obj_get_int(family_in);
|
|
|
|
byte binaddr[BINADDR_MAX_LEN];
|
|
|
|
int r = inet_pton(family, mp_obj_str_get_str(addr_in), binaddr);
|
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
if (r == 0) {
|
2016-10-06 23:05:15 -04:00
|
|
|
mp_raise_OSError(MP_EINVAL);
|
2015-07-14 17:05:49 -04:00
|
|
|
}
|
|
|
|
int binaddr_len = 0;
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
binaddr_len = sizeof(struct in_addr);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
binaddr_len = sizeof(struct in6_addr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return mp_obj_new_bytes(binaddr, binaddr_len);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_pton_obj, mod_socket_inet_pton);
|
|
|
|
|
2016-01-11 00:37:59 -05:00
|
|
|
STATIC mp_obj_t mod_socket_inet_ntop(mp_obj_t family_in, mp_obj_t binaddr_in) {
|
|
|
|
int family = mp_obj_get_int(family_in);
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(binaddr_in, &bufinfo, MP_BUFFER_READ);
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN);
|
|
|
|
if (inet_ntop(family, bufinfo.buf, vstr.buf, vstr.len) == NULL) {
|
2016-10-06 23:05:15 -04:00
|
|
|
mp_raise_OSError(errno);
|
2016-01-11 00:37:59 -05:00
|
|
|
}
|
|
|
|
vstr.len = strlen(vstr.buf);
|
2022-08-23 04:35:37 -04:00
|
|
|
return mp_obj_new_str_from_utf8_vstr(&vstr);
|
2016-01-11 00:37:59 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_ntop_obj, mod_socket_inet_ntop);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mod_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) {
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-01-22 09:35:10 -05:00
|
|
|
const char *host = mp_obj_str_get_str(args[0]);
|
2014-01-18 16:47:44 -05:00
|
|
|
const char *serv = NULL;
|
2014-06-22 12:14:12 -04:00
|
|
|
struct addrinfo hints;
|
2015-10-10 07:59:26 -04:00
|
|
|
char buf[6];
|
2014-06-22 12:14:12 -04:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2014-01-18 16:47:44 -05:00
|
|
|
// getaddrinfo accepts port in string notation, so however
|
|
|
|
// it may seem stupid, we need to convert int to str
|
2019-01-30 06:05:48 -05:00
|
|
|
if (mp_obj_is_small_int(args[1])) {
|
2015-07-10 17:05:46 -04:00
|
|
|
unsigned port = (unsigned short)MP_OBJ_SMALL_INT_VALUE(args[1]);
|
2015-11-21 10:17:25 -05:00
|
|
|
snprintf(buf, sizeof(buf), "%u", port);
|
2014-01-18 16:47:44 -05:00
|
|
|
serv = buf;
|
2014-06-22 12:14:12 -04:00
|
|
|
hints.ai_flags = AI_NUMERICSERV;
|
2014-06-24 14:20:38 -04:00
|
|
|
#ifdef __UCLIBC_MAJOR__
|
2014-06-22 12:14:12 -04:00
|
|
|
#if __UCLIBC_MAJOR__ == 0 && (__UCLIBC_MINOR__ < 9 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ <= 32))
|
2014-09-06 12:46:52 -04:00
|
|
|
// "warning" requires -Wno-cpp which is a relatively new gcc option, so we choose not to use it.
|
|
|
|
// #warning Working around uClibc bug with numeric service name
|
2014-06-22 12:14:12 -04:00
|
|
|
// Older versions og uClibc have bugs when numeric ports in service
|
|
|
|
// arg require also hints.ai_socktype (or hints.ai_protocol) != 0
|
|
|
|
// This actually was fixed in 0.9.32.1, but uClibc doesn't allow to
|
|
|
|
// test for that.
|
|
|
|
// http://git.uclibc.org/uClibc/commit/libc/inet/getaddrinfo.c?id=bc3be18145e4d5
|
|
|
|
// Note that this is crude workaround, precluding UDP socket addresses
|
|
|
|
// to be returned. TODO: set only if not set by Python args.
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2014-06-24 14:20:38 -04:00
|
|
|
#endif
|
2014-06-22 12:14:12 -04:00
|
|
|
#endif
|
2014-01-18 16:47:44 -05:00
|
|
|
} else {
|
2014-01-22 09:35:10 -05:00
|
|
|
serv = mp_obj_str_get_str(args[1]);
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-09 18:48:12 -04:00
|
|
|
if (n_args > 2) {
|
|
|
|
hints.ai_family = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
if (n_args > 3) {
|
|
|
|
hints.ai_socktype = MP_OBJ_SMALL_INT_VALUE(args[3]);
|
2022-08-14 15:12:24 -04:00
|
|
|
if (n_args > 4) {
|
|
|
|
hints.ai_protocol = MP_OBJ_SMALL_INT_VALUE(args[4]);
|
|
|
|
if (n_args > 5) {
|
|
|
|
hints.ai_flags = MP_OBJ_SMALL_INT_VALUE(args[5]);
|
|
|
|
}
|
|
|
|
}
|
2015-07-09 18:48:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 12:11:34 -04:00
|
|
|
struct addrinfo *addr_list;
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-06-22 12:14:12 -04:00
|
|
|
int res = getaddrinfo(host, serv, &hints, &addr_list);
|
2020-01-22 19:14:03 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 16:47:44 -05:00
|
|
|
|
|
|
|
if (res != 0) {
|
2014-04-21 16:58:39 -04:00
|
|
|
// CPython: socket.gaierror
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("[addrinfo error %d]"), res);
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
2014-06-22 12:11:34 -04:00
|
|
|
assert(addr_list);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-03-31 11:28:13 -04:00
|
|
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
2014-06-22 12:11:34 -04:00
|
|
|
for (struct addrinfo *addr = addr_list; addr; addr = addr->ai_next) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
|
2014-07-31 05:49:14 -04:00
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(addr->ai_family);
|
|
|
|
t->items[1] = MP_OBJ_NEW_SMALL_INT(addr->ai_socktype);
|
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT(addr->ai_protocol);
|
2014-01-18 16:47:44 -05:00
|
|
|
// "canonname will be a string representing the canonical name of the host
|
|
|
|
// if AI_CANONNAME is part of the flags argument; else canonname will be empty." ??
|
|
|
|
if (addr->ai_canonname) {
|
2014-01-21 16:40:13 -05:00
|
|
|
t->items[3] = MP_OBJ_NEW_QSTR(qstr_from_str(addr->ai_canonname));
|
2014-01-18 16:47:44 -05:00
|
|
|
} else {
|
|
|
|
t->items[3] = mp_const_none;
|
|
|
|
}
|
|
|
|
t->items[4] = mp_obj_new_bytearray(addr->ai_addrlen, addr->ai_addr);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_list_append(list, MP_OBJ_FROM_PTR(t));
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
2014-06-22 12:11:34 -04:00
|
|
|
freeaddrinfo(addr_list);
|
2014-01-18 16:47:44 -05:00
|
|
|
return list;
|
|
|
|
}
|
2022-08-14 15:12:24 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 6, mod_socket_getaddrinfo);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2015-11-20 18:38:59 -05:00
|
|
|
STATIC mp_obj_t mod_socket_sockaddr(mp_obj_t sockaddr_in) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(sockaddr_in, &bufinfo, MP_BUFFER_READ);
|
|
|
|
switch (((struct sockaddr *)bufinfo.buf)->sa_family) {
|
|
|
|
case AF_INET: {
|
|
|
|
struct sockaddr_in *sa = (struct sockaddr_in *)bufinfo.buf;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
|
2015-11-20 18:38:59 -05:00
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(AF_INET);
|
|
|
|
t->items[1] = mp_obj_new_bytes((byte *)&sa->sin_addr, sizeof(sa->sin_addr));
|
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT(ntohs(sa->sin_port));
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-11-20 18:38:59 -05:00
|
|
|
}
|
2016-01-27 10:23:57 -05:00
|
|
|
case AF_INET6: {
|
|
|
|
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)bufinfo.buf;
|
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
|
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(AF_INET6);
|
|
|
|
t->items[1] = mp_obj_new_bytes((byte *)&sa->sin6_addr, sizeof(sa->sin6_addr));
|
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT(ntohs(sa->sin6_port));
|
|
|
|
t->items[3] = MP_OBJ_NEW_SMALL_INT(ntohl(sa->sin6_flowinfo));
|
|
|
|
t->items[4] = MP_OBJ_NEW_SMALL_INT(ntohl(sa->sin6_scope_id));
|
|
|
|
return MP_OBJ_FROM_PTR(t);
|
|
|
|
}
|
2015-11-20 18:38:59 -05:00
|
|
|
default: {
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)bufinfo.buf;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
2015-11-20 18:38:59 -05:00
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(sa->sa_family);
|
|
|
|
t->items[1] = mp_obj_new_bytes((byte *)sa->sa_data, bufinfo.len - offsetof(struct sockaddr, sa_data));
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-11-20 18:38:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_sockaddr_obj, mod_socket_sockaddr);
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_socket_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usocket) },
|
2016-08-06 18:21:42 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_type_socket) },
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&mod_socket_getaddrinfo_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_inet_pton), MP_ROM_PTR(&mod_socket_inet_pton_obj) },
|
2016-01-11 00:37:59 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_inet_ntop), MP_ROM_PTR(&mod_socket_inet_ntop_obj) },
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sockaddr), MP_ROM_PTR(&mod_socket_sockaddr_obj) },
|
2014-01-25 13:13:17 -05:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
#define C(name) { MP_ROM_QSTR(MP_QSTR_##name), MP_ROM_INT(name) }
|
2014-01-25 13:13:17 -05:00
|
|
|
C(AF_UNIX),
|
|
|
|
C(AF_INET),
|
|
|
|
C(AF_INET6),
|
|
|
|
C(SOCK_STREAM),
|
|
|
|
C(SOCK_DGRAM),
|
|
|
|
C(SOCK_RAW),
|
2014-01-25 13:31:34 -05:00
|
|
|
|
|
|
|
C(MSG_DONTROUTE),
|
|
|
|
C(MSG_DONTWAIT),
|
|
|
|
|
|
|
|
C(SOL_SOCKET),
|
|
|
|
C(SO_BROADCAST),
|
|
|
|
C(SO_ERROR),
|
|
|
|
C(SO_KEEPALIVE),
|
|
|
|
C(SO_LINGER),
|
|
|
|
C(SO_REUSEADDR),
|
2014-04-17 13:34:04 -04:00
|
|
|
#undef C
|
2014-01-25 13:13:17 -05:00
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_socket_globals, mp_module_socket_globals_table);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-04-17 13:34:04 -04:00
|
|
|
const mp_obj_module_t mp_module_socket = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_socket_globals,
|
|
|
|
};
|
2022-04-20 07:24:01 -04:00
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_usocket, mp_module_socket);
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_SOCKET
|