2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-05-14 13:51:57 +01:00
|
|
|
* Copyright (c) 2014-2018 Paul Sokolovsky
|
2019-05-17 09:02:44 +01:00
|
|
|
* Copyright (c) 2014-2019 Damien P. George
|
2014-05-03 23:27:38 +01: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 21:47:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-04-20 00:00:12 +01:00
|
|
|
#include <fcntl.h>
|
2014-03-16 06:24:34 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-01-18 21:47:44 +00:00
|
|
|
#include <sys/socket.h>
|
2014-03-16 06:24:34 +00:00
|
|
|
#include <netinet/in.h>
|
2014-01-18 21:47:44 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2018-10-07 18:58:42 +01:00
|
|
|
#include <math.h>
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2015-01-01 20:40:19 +00:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
#include "py/builtin.h"
|
2016-10-07 04:09:59 +01:00
|
|
|
#include "py/mphal.h"
|
2020-01-23 00:14:03 +00:00
|
|
|
#include "py/mpthread.h"
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-05-30 23:48:26 +01:00
|
|
|
/*
|
|
|
|
The idea of this module is to implement reasonable minimum of
|
|
|
|
socket-related functions to write typical clients and servers.
|
2014-10-09 18:43:10 +01:00
|
|
|
The module named "usocket" on purpose, to allow to make
|
2014-05-30 23:48:26 +01:00
|
|
|
Python-level module more (or fully) compatible with CPython
|
|
|
|
"socket", e.g.:
|
|
|
|
---- socket.py ----
|
2014-10-09 18:43:10 +01:00
|
|
|
from usocket import *
|
2014-05-30 23:48:26 +01: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 08:08:14 +01:00
|
|
|
should be add to separate modules (C or Python level).
|
2014-05-30 23:48:26 +01:00
|
|
|
*/
|
|
|
|
|
2016-08-06 23:21:42 +01:00
|
|
|
// This type must "inherit" from mp_obj_fdfile_t, i.e. matching subset of
|
|
|
|
// fields should have the same layout.
|
2014-01-18 21:47:44 +00:00
|
|
|
typedef struct _mp_obj_socket_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int fd;
|
2018-10-07 18:58:42 +01:00
|
|
|
bool blocking;
|
2014-01-18 21:47:44 +00:00
|
|
|
} mp_obj_socket_t;
|
|
|
|
|
2016-08-06 23:21:42 +01:00
|
|
|
const mp_obj_type_t mp_type_socket;
|
2014-01-18 21:47:44 +00:00
|
|
|
|
|
|
|
// Helper functions
|
2015-07-13 23:46:40 +01: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 18:25:53 +01:00
|
|
|
STATIC mp_obj_socket_t *socket_new(int fd) {
|
2014-01-18 21:47:44 +00:00
|
|
|
mp_obj_socket_t *o = m_new_obj(mp_obj_socket_t);
|
2016-08-06 23:21:42 +01:00
|
|
|
o->base.type = &mp_type_socket;
|
2014-01-18 21:47:44 +00:00
|
|
|
o->fd = fd;
|
2018-10-07 18:58:42 +01:00
|
|
|
o->blocking = true;
|
2014-01-18 21:47:44 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<_socket %d>", self->fd);
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 22:38:58 +01:00
|
|
|
STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_int_t r = read(o->fd, buf, size);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 21:47:44 +00:00
|
|
|
if (r == -1) {
|
2018-10-07 18:58:42 +01:00
|
|
|
int err = errno;
|
|
|
|
// 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 06:45:13 +01:00
|
|
|
}
|
|
|
|
|
2018-10-07 18:58:42 +01:00
|
|
|
*errcode = err;
|
2014-07-27 22:38:58 +01:00
|
|
|
return MP_STREAM_ERROR;
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-27 22:38:58 +01:00
|
|
|
STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_int_t r = write(o->fd, buf, size);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 21:47:44 +00:00
|
|
|
if (r == -1) {
|
2018-10-07 18:58:42 +01:00
|
|
|
int err = errno;
|
|
|
|
// 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 06:45:13 +01:00
|
|
|
}
|
|
|
|
|
2018-10-07 18:58:42 +01:00
|
|
|
*errcode = err;
|
2014-07-27 22:38:58 +01:00
|
|
|
return MP_STREAM_ERROR;
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-03-07 06:48:53 +00: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-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2018-03-07 06:48:53 +00:00
|
|
|
close(self->fd);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2018-03-07 06:48:53 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-20 04:09:49 +01:00
|
|
|
case MP_STREAM_GET_FILENO:
|
|
|
|
return self->fd;
|
|
|
|
|
2018-03-07 06:48:53 +00:00
|
|
|
default:
|
|
|
|
*errcode = MP_EINVAL;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC mp_obj_t socket_fileno(mp_obj_t self_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-31 10:49:14 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->fd);
|
2014-02-08 19:15:46 +00:00
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_fileno_obj, socket_fileno);
|
2014-02-08 19:15:46 +00:00
|
|
|
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-18 22:31:24 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 22:59:24 +01:00
|
|
|
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-18 21:47:44 +00:00
|
|
|
int r = connect(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2019-05-28 08:22:54 +01:00
|
|
|
int err = errno;
|
|
|
|
if (r == -1 && self->blocking && err == EINPROGRESS) {
|
|
|
|
// EINPROGRESS on a blocking socket means the operation timed out
|
|
|
|
err = MP_ETIMEDOUT;
|
|
|
|
}
|
|
|
|
RAISE_ERRNO(r, err);
|
2014-01-18 21:47:44 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-18 22:31:24 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 22:59:24 +01:00
|
|
|
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-18 21:47:44 +00:00
|
|
|
int r = bind(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 21:47:44 +00:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-18 21:47:44 +00:00
|
|
|
int r = listen(self->fd, MP_OBJ_SMALL_INT_VALUE(backlog_in));
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 21:47:44 +00:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-01-21 21:53:47 +00:00
|
|
|
// sockaddr_storage isn't stack-friendly (129 bytes or so)
|
|
|
|
//struct sockaddr_storage addr;
|
|
|
|
byte addr[32];
|
2014-01-18 21:47:44 +00:00
|
|
|
socklen_t addr_len = sizeof(addr);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2016-01-21 21:53:47 +00:00
|
|
|
int fd = accept(self->fd, (struct sockaddr*)&addr, &addr_len);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2019-05-28 08:22:54 +01:00
|
|
|
int err = errno;
|
|
|
|
if (fd == -1 && self->blocking && err == EAGAIN) {
|
|
|
|
// EAGAIN on a blocking socket means the operation timed out
|
|
|
|
err = MP_ETIMEDOUT;
|
|
|
|
}
|
|
|
|
RAISE_ERRNO(fd, err);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2015-11-27 17:01:44 +00: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 21:47:44 +00:00
|
|
|
t->items[1] = mp_obj_new_bytearray(addr_len, &addr);
|
|
|
|
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-05-06 23:51:35 +01: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 14:21:40 +00:00
|
|
|
STATIC mp_obj_t socket_recv(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-01-20 17:42:39 +00: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 19:20:40 +01:00
|
|
|
byte *buf = m_new(byte, sz);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-20 17:42:39 +00:00
|
|
|
int out_sz = recv(self->fd, buf, sz, flags);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-20 17:42:39 +00:00
|
|
|
RAISE_ERRNO(out_sz, errno);
|
|
|
|
|
2014-08-10 19:20:40 +01:00
|
|
|
mp_obj_t ret = mp_obj_new_str_of_type(&mp_type_bytes, buf, out_sz);
|
2014-05-25 22:27:57 +01:00
|
|
|
m_del(char, buf, sz);
|
|
|
|
return ret;
|
2014-01-20 17:42:39 +00:00
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_recv_obj, 2, 3, socket_recv);
|
2014-01-20 17:42:39 +00:00
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t socket_recvfrom(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2015-07-13 23:46:40 +01: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:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2015-07-13 23:46:40 +01:00
|
|
|
int out_sz = recvfrom(self->fd, buf, sz, flags, (struct sockaddr*)&addr, &addr_len);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2015-07-13 23:46:40 +01:00
|
|
|
RAISE_ERRNO(out_sz, errno);
|
|
|
|
|
|
|
|
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 17:01:44 +00:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
2015-07-13 23:46:40 +01:00
|
|
|
t->items[0] = buf_o;
|
|
|
|
t->items[1] = mp_obj_from_sockaddr((struct sockaddr*)&addr, addr_len);
|
|
|
|
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-07-13 23:46:40 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_recvfrom_obj, 2, 3, socket_recvfrom);
|
|
|
|
|
2014-05-06 23:51:35 +01: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 14:21:40 +00:00
|
|
|
STATIC mp_obj_t socket_send(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-01-20 17:42:39 +00:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (n_args > 2) {
|
|
|
|
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
|
2014-04-26 03:53:02 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-04-26 03:53:02 +01:00
|
|
|
int out_sz = send(self->fd, bufinfo.buf, bufinfo.len, flags);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-20 17:42:39 +00:00
|
|
|
RAISE_ERRNO(out_sz, errno);
|
|
|
|
|
2014-07-31 10:49:14 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(out_sz);
|
2014-01-20 17:42:39 +00:00
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_send_obj, 2, 3, socket_send);
|
2014-01-20 17:42:39 +00:00
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t socket_sendto(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2015-07-12 11:51:55 +01: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:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2015-07-12 11:51:55 +01:00
|
|
|
int out_sz = sendto(self->fd, bufinfo.buf, bufinfo.len, flags,
|
|
|
|
(struct sockaddr *)addr_bi.buf, addr_bi.len);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2015-07-12 11:51:55 +01:00
|
|
|
RAISE_ERRNO(out_sz, errno);
|
|
|
|
|
|
|
|
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 14:21:40 +00:00
|
|
|
STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)n_args; // always 4
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-01-25 23:59:52 +00: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 12:59:26 +01:00
|
|
|
int val;
|
2019-01-30 11:05:48 +00:00
|
|
|
if (mp_obj_is_int(args[3])) {
|
2015-10-10 12:59:26 +01:00
|
|
|
val = mp_obj_int_get_truncated(args[3]);
|
2014-01-25 23:59:52 +00:00
|
|
|
optval = &val;
|
|
|
|
optlen = sizeof(val);
|
|
|
|
} else {
|
2014-04-18 22:31:24 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 22:59:24 +01:00
|
|
|
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
|
2014-01-25 23:59:52 +00:00
|
|
|
optval = bufinfo.buf;
|
|
|
|
optlen = bufinfo.len;
|
|
|
|
}
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-25 23:59:52 +00:00
|
|
|
int r = setsockopt(self->fd, level, option, optval, optlen);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-25 23:59:52 +00:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 18:25:53 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
|
2014-01-25 23:59:52 +00:00
|
|
|
|
2014-04-20 00:00:12 +01:00
|
|
|
STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-20 00:00:12 +01:00
|
|
|
int val = mp_obj_is_true(flag_in);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-04-20 00:00:12 +01:00
|
|
|
int flags = fcntl(self->fd, F_GETFL, 0);
|
2020-01-23 00:14:03 +00:00
|
|
|
if (flags == -1) {
|
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
RAISE_ERRNO(flags, errno);
|
|
|
|
}
|
2014-04-20 00:00:12 +01:00
|
|
|
if (val) {
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
} else {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
flags = fcntl(self->fd, F_SETFL, flags);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-04-20 00:00:12 +01:00
|
|
|
RAISE_ERRNO(flags, errno);
|
2018-10-07 18:58:42 +01:00
|
|
|
self->blocking = val;
|
2014-04-20 00:00:12 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
|
|
|
|
|
2016-09-02 06:45:13 +01: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 18:58:42 +01:00
|
|
|
bool new_blocking = true;
|
|
|
|
|
2019-05-06 22:24:22 +01: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 06:45:13 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2018-10-07 18:58:42 +01:00
|
|
|
mp_float_t val = mp_obj_get_float(timeout_in);
|
|
|
|
double ipart;
|
|
|
|
tv.tv_usec = round(modf(val, &ipart) * 1000000);
|
|
|
|
tv.tv_sec = ipart;
|
|
|
|
#else
|
|
|
|
tv.tv_sec = mp_obj_get_int(timeout_in);
|
2016-09-02 06:45:13 +01:00
|
|
|
#endif
|
|
|
|
|
2018-10-07 18:58:42 +01: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 06:45:13 +01:00
|
|
|
}
|
2018-10-07 18:58:42 +01:00
|
|
|
|
2019-05-06 22:24:22 +01:00
|
|
|
if (new_blocking) {
|
|
|
|
int r;
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2019-05-06 22:24:22 +01:00
|
|
|
r = setsockopt(self->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
|
2020-01-23 00:14:03 +00:00
|
|
|
if (r == -1) {
|
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
}
|
2019-05-06 22:24:22 +01:00
|
|
|
r = setsockopt(self->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2019-05-06 22:24:22 +01:00
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
}
|
|
|
|
|
2018-10-07 18:58:42 +01:00
|
|
|
if (self->blocking != new_blocking) {
|
|
|
|
socket_setblocking(self_in, mp_obj_new_bool(new_blocking));
|
|
|
|
}
|
|
|
|
|
2016-09-02 06:45:13 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_settimeout_obj, socket_settimeout);
|
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) {
|
2014-05-24 19:24:37 +01: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 17:01:44 +00:00
|
|
|
mp_obj_socket_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-05-24 19:24:37 +01: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 10:49:14 +01:00
|
|
|
new_args[0] = MP_OBJ_NEW_SMALL_INT(self->fd);
|
2016-04-04 21:43:16 +01:00
|
|
|
return mp_builtin_open(n_args, new_args, (mp_map_t*)&mp_const_empty_map);
|
2014-05-24 19:24:37 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile);
|
|
|
|
|
2016-01-03 15:55:55 +00: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 12:47:20 +00:00
|
|
|
(void)type_in;
|
|
|
|
(void)n_kw;
|
|
|
|
|
2014-01-18 21:47:44 +00:00
|
|
|
int family = AF_INET;
|
|
|
|
int type = SOCK_STREAM;
|
|
|
|
int proto = 0;
|
|
|
|
|
|
|
|
if (n_args > 0) {
|
2019-01-30 11:05:48 +00:00
|
|
|
assert(mp_obj_is_small_int(args[0]));
|
2014-01-18 21:47:44 +00:00
|
|
|
family = MP_OBJ_SMALL_INT_VALUE(args[0]);
|
|
|
|
if (n_args > 1) {
|
2019-01-30 11:05:48 +00:00
|
|
|
assert(mp_obj_is_small_int(args[1]));
|
2014-01-18 21:47:44 +00:00
|
|
|
type = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
if (n_args > 2) {
|
2019-01-30 11:05:48 +00:00
|
|
|
assert(mp_obj_is_small_int(args[2]));
|
2014-01-18 21:47:44 +00:00
|
|
|
proto = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-01-18 21:47:44 +00:00
|
|
|
int fd = socket(family, type, proto);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 21:47:44 +00:00
|
|
|
RAISE_ERRNO(fd, errno);
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(socket_new(fd));
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:38:15 +00: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 06:45:13 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socket_settimeout_obj) },
|
2018-03-07 06:48:53 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
2014-01-18 21:47:44 +00:00
|
|
|
};
|
|
|
|
|
2014-10-09 18:43:10 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(usocket_locals_dict, usocket_locals_dict_table);
|
2014-03-26 21:47:19 +00:00
|
|
|
|
2014-10-09 18:43:10 +01:00
|
|
|
STATIC const mp_stream_p_t usocket_stream_p = {
|
2014-04-05 23:02:23 +01:00
|
|
|
.read = socket_read,
|
|
|
|
.write = socket_write,
|
2018-03-07 06:48:53 +00:00
|
|
|
.ioctl = socket_ioctl,
|
2014-04-05 23:02:23 +01:00
|
|
|
};
|
|
|
|
|
2016-08-06 23:21:42 +01:00
|
|
|
const mp_obj_type_t mp_type_socket = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_socket,
|
2014-01-18 21:47:44 +00:00
|
|
|
.print = socket_print,
|
|
|
|
.make_new = socket_make_new,
|
|
|
|
.getiter = NULL,
|
|
|
|
.iternext = NULL,
|
2016-06-18 16:19:24 +01:00
|
|
|
.protocol = &usocket_stream_p,
|
2015-11-27 17:01:44 +00:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&usocket_locals_dict,
|
2014-01-18 21:47:44 +00:00
|
|
|
};
|
|
|
|
|
2015-07-14 22:05:49 +01: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-07 04:05:15 +01:00
|
|
|
mp_raise_OSError(MP_EINVAL);
|
2015-07-14 22:05:49 +01: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 05:37:59 +00: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-07 04:05:15 +01:00
|
|
|
mp_raise_OSError(errno);
|
2016-01-11 05:37:59 +00:00
|
|
|
}
|
|
|
|
vstr.len = strlen(vstr.buf);
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_ntop_obj, mod_socket_inet_ntop);
|
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t mod_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) {
|
2017-07-12 02:43:35 +01:00
|
|
|
// TODO: Implement 5th and 6th args
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-01-22 14:35:10 +00:00
|
|
|
const char *host = mp_obj_str_get_str(args[0]);
|
2014-01-18 21:47:44 +00:00
|
|
|
const char *serv = NULL;
|
2014-06-22 17:14:12 +01:00
|
|
|
struct addrinfo hints;
|
2015-10-10 12:59:26 +01:00
|
|
|
char buf[6];
|
2014-06-22 17:14:12 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2014-01-18 21:47:44 +00:00
|
|
|
// getaddrinfo accepts port in string notation, so however
|
|
|
|
// it may seem stupid, we need to convert int to str
|
2019-01-30 11:05:48 +00:00
|
|
|
if (mp_obj_is_small_int(args[1])) {
|
2015-07-10 22:05:46 +01:00
|
|
|
unsigned port = (unsigned short)MP_OBJ_SMALL_INT_VALUE(args[1]);
|
2015-11-21 15:17:25 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%u", port);
|
2014-01-18 21:47:44 +00:00
|
|
|
serv = buf;
|
2014-06-22 17:14:12 +01:00
|
|
|
hints.ai_flags = AI_NUMERICSERV;
|
2014-06-24 19:20:38 +01:00
|
|
|
#ifdef __UCLIBC_MAJOR__
|
2014-06-22 17:14:12 +01:00
|
|
|
#if __UCLIBC_MAJOR__ == 0 && (__UCLIBC_MINOR__ < 9 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ <= 32))
|
2014-09-06 17:46:52 +01: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 17:14:12 +01: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 19:20:38 +01:00
|
|
|
#endif
|
2014-06-22 17:14:12 +01:00
|
|
|
#endif
|
2014-01-18 21:47:44 +00:00
|
|
|
} else {
|
2014-01-22 14:35:10 +00:00
|
|
|
serv = mp_obj_str_get_str(args[1]);
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 23:48:12 +01: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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:11:34 +01:00
|
|
|
struct addrinfo *addr_list;
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-06-22 17:14:12 +01:00
|
|
|
int res = getaddrinfo(host, serv, &hints, &addr_list);
|
2020-01-23 00:14:03 +00:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-01-18 21:47:44 +00:00
|
|
|
|
|
|
|
if (res != 0) {
|
2014-04-21 21:58:39 +01:00
|
|
|
// CPython: socket.gaierror
|
2020-02-11 00:48:28 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_OSError, "[addrinfo error %d]", res);
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
2014-06-22 17:11:34 +01:00
|
|
|
assert(addr_list);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-03-31 16:28:13 +01:00
|
|
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
2014-06-22 17:11:34 +01:00
|
|
|
for (struct addrinfo *addr = addr_list; addr; addr = addr->ai_next) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
|
2014-07-31 10:49:14 +01: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 21:47:44 +00: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 21:40:13 +00:00
|
|
|
t->items[3] = MP_OBJ_NEW_QSTR(qstr_from_str(addr->ai_canonname));
|
2014-01-18 21:47:44 +00:00
|
|
|
} else {
|
|
|
|
t->items[3] = mp_const_none;
|
|
|
|
}
|
|
|
|
t->items[4] = mp_obj_new_bytearray(addr->ai_addrlen, addr->ai_addr);
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_list_append(list, MP_OBJ_FROM_PTR(t));
|
2014-01-18 21:47:44 +00:00
|
|
|
}
|
2014-06-22 17:11:34 +01:00
|
|
|
freeaddrinfo(addr_list);
|
2014-01-18 21:47:44 +00:00
|
|
|
return list;
|
|
|
|
}
|
2017-07-12 02:43:35 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 4, mod_socket_getaddrinfo);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2015-11-20 23:38:59 +00: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 17:01:44 +00:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
|
2015-11-20 23:38:59 +00: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 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-11-20 23:38:59 +00:00
|
|
|
}
|
2016-01-27 15:23:57 +00: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 23:38:59 +00:00
|
|
|
default: {
|
|
|
|
struct sockaddr *sa = (struct sockaddr*)bufinfo.buf;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
2015-11-20 23:38:59 +00: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 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-11-20 23:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_sockaddr_obj, mod_socket_sockaddr);
|
|
|
|
|
2015-11-27 13:38:15 +00: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 23:21:42 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_type_socket) },
|
2015-11-27 13:38:15 +00: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 05:37:59 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_inet_ntop), MP_ROM_PTR(&mod_socket_inet_ntop_obj) },
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sockaddr), MP_ROM_PTR(&mod_socket_sockaddr_obj) },
|
2014-01-25 18:13:17 +00:00
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
#define C(name) { MP_ROM_QSTR(MP_QSTR_ ## name), MP_ROM_INT(name) }
|
2014-01-25 18:13:17 +00:00
|
|
|
C(AF_UNIX),
|
|
|
|
C(AF_INET),
|
|
|
|
C(AF_INET6),
|
|
|
|
C(SOCK_STREAM),
|
|
|
|
C(SOCK_DGRAM),
|
|
|
|
C(SOCK_RAW),
|
2014-01-25 18:31:34 +00: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 18:34:04 +01:00
|
|
|
#undef C
|
2014-01-25 18:13:17 +00:00
|
|
|
};
|
|
|
|
|
2014-11-29 14:39:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_socket_globals, mp_module_socket_globals_table);
|
2014-01-18 21:47:44 +00:00
|
|
|
|
2014-04-17 18:34:04 +01:00
|
|
|
const mp_obj_module_t mp_module_socket = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_socket_globals,
|
|
|
|
};
|