2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 01:44:45 -04:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
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>
|
|
|
|
|
2014-05-02 11:03:04 -04:00
|
|
|
#include "mpconfig.h"
|
2014-01-18 16:47:44 -05:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2014-01-18 16:47:44 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "objtuple.h"
|
|
|
|
#include "objarray.h"
|
2014-08-10 14:20:40 -04:00
|
|
|
#include "objstr.h"
|
2014-01-18 16:47:44 -05:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "stream.h"
|
2014-05-24 14:24:37 -04:00
|
|
|
#include "builtin.h"
|
2014-01-18 16:47:44 -05:00
|
|
|
|
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)
|
|
|
|
should be add to seperate modules (C or Python level).
|
|
|
|
*/
|
|
|
|
|
2014-01-18 16:47:44 -05:00
|
|
|
#define MICROPY_SOCKET_EXTRA (0)
|
|
|
|
|
|
|
|
typedef struct _mp_obj_socket_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int fd;
|
|
|
|
} mp_obj_socket_t;
|
|
|
|
|
2014-10-09 13:43:10 -04:00
|
|
|
STATIC const mp_obj_type_t usocket_type;
|
2014-01-18 16:47:44 -05:00
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
#define RAISE_ERRNO(err_flag, error_val) \
|
|
|
|
{ if (err_flag == -1) \
|
2014-07-31 05:49:14 -04:00
|
|
|
{ nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_val))); } }
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_socket_t *socket_new(int fd) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *o = m_new_obj(mp_obj_socket_t);
|
2014-10-09 13:43:10 -04:00
|
|
|
o->base.type = &usocket_type;
|
2014-01-18 16:47:44 -05:00
|
|
|
o->fd = fd;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC void socket_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *self = self_in;
|
|
|
|
print(env, "<_socket %d>", self->fd);
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *o = o_in;
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t r = read(o->fd, buf, size);
|
2014-01-18 16:47:44 -05:00
|
|
|
if (r == -1) {
|
|
|
|
*errcode = errno;
|
2014-07-27 17:38:58 -04:00
|
|
|
return MP_STREAM_ERROR;
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *o = o_in;
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t r = write(o->fd, buf, size);
|
2014-01-18 16:47:44 -05:00
|
|
|
if (r == -1) {
|
|
|
|
*errcode = errno;
|
2014-07-27 17:38:58 -04:00
|
|
|
return MP_STREAM_ERROR;
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *self = self_in;
|
|
|
|
close(self->fd);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
|
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) {
|
2014-02-08 14:15:46 -05:00
|
|
|
mp_obj_socket_t *self = 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) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *self = 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);
|
2014-01-18 16:47:44 -05:00
|
|
|
int r = connect(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
|
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
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) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *self = 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);
|
2014-01-18 16:47:44 -05:00
|
|
|
int r = bind(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
|
|
|
|
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
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *self = self_in;
|
|
|
|
int r = listen(self->fd, MP_OBJ_SMALL_INT_VALUE(backlog_in));
|
|
|
|
RAISE_ERRNO(r, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, 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) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_socket_t *self = self_in;
|
|
|
|
struct sockaddr addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
int fd = accept(self->fd, &addr, &addr_len);
|
|
|
|
RAISE_ERRNO(fd, errno);
|
|
|
|
|
|
|
|
mp_obj_tuple_t *t = mp_obj_new_tuple(2, NULL);
|
|
|
|
t->items[0] = socket_new(fd);
|
|
|
|
t->items[1] = mp_obj_new_bytearray(addr_len, &addr);
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
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.
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t socket_recv(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-01-20 12:42:39 -05:00
|
|
|
mp_obj_socket_t *self = args[0];
|
|
|
|
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);
|
2014-01-20 12:42:39 -05:00
|
|
|
int out_sz = recv(self->fd, buf, sz, flags);
|
|
|
|
RAISE_ERRNO(out_sz, errno);
|
|
|
|
|
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
|
|
|
|
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.
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t socket_send(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-01-20 12:42:39 -05:00
|
|
|
mp_obj_socket_t *self = args[0];
|
|
|
|
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);
|
|
|
|
int out_sz = send(self->fd, bufinfo.buf, bufinfo.len, flags);
|
2014-01-20 12:42:39 -05:00
|
|
|
RAISE_ERRNO(out_sz, errno);
|
|
|
|
|
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
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-01-25 18:59:52 -05:00
|
|
|
mp_obj_socket_t *self = args[0];
|
|
|
|
int level = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
int option = mp_obj_get_int(args[2]);
|
|
|
|
|
|
|
|
const void *optval;
|
|
|
|
socklen_t optlen;
|
|
|
|
if (MP_OBJ_IS_INT(args[3])) {
|
2014-12-05 18:13:52 -05:00
|
|
|
int 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;
|
|
|
|
}
|
|
|
|
int r = setsockopt(self->fd, level, option, optval, optlen);
|
|
|
|
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) {
|
|
|
|
mp_obj_socket_t *self = self_in;
|
|
|
|
int val = mp_obj_is_true(flag_in);
|
|
|
|
int flags = fcntl(self->fd, F_GETFL, 0);
|
|
|
|
RAISE_ERRNO(flags, errno);
|
|
|
|
if (val) {
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
} else {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
flags = fcntl(self->fd, F_SETFL, flags);
|
|
|
|
RAISE_ERRNO(flags, errno);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t socket_makefile(mp_uint_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.
|
|
|
|
mp_obj_socket_t *self = args[0];
|
|
|
|
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);
|
2014-10-04 02:51:33 -04:00
|
|
|
mp_map_t kwargs;
|
|
|
|
mp_map_init(&kwargs, 0);
|
|
|
|
return mp_builtin_open(n_args, new_args, &kwargs);
|
2014-05-24 14:24:37 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile);
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-01-18 16:47:44 -05:00
|
|
|
int family = AF_INET;
|
|
|
|
int type = SOCK_STREAM;
|
|
|
|
int proto = 0;
|
|
|
|
|
|
|
|
if (n_args > 0) {
|
|
|
|
assert(MP_OBJ_IS_SMALL_INT(args[0]));
|
|
|
|
family = MP_OBJ_SMALL_INT_VALUE(args[0]);
|
|
|
|
if (n_args > 1) {
|
|
|
|
assert(MP_OBJ_IS_SMALL_INT(args[1]));
|
|
|
|
type = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
if (n_args > 2) {
|
|
|
|
assert(MP_OBJ_IS_SMALL_INT(args[2]));
|
|
|
|
proto = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = socket(family, type, proto);
|
|
|
|
RAISE_ERRNO(fd, errno);
|
|
|
|
return socket_new(fd);
|
|
|
|
}
|
|
|
|
|
2014-10-09 13:43:10 -04:00
|
|
|
STATIC const mp_map_elem_t usocket_locals_dict_table[] = {
|
2014-03-26 17:47:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_fileno), (mp_obj_t)&socket_fileno_obj },
|
2014-05-24 14:24:37 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj },
|
2014-03-26 17:47:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
|
2014-10-18 15:44:07 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj },
|
2014-03-26 17:47:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj },
|
2014-04-19 19:00:12 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_setblocking), (mp_obj_t)&socket_setblocking_obj },
|
2014-03-26 17:47:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_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,
|
|
|
|
};
|
|
|
|
|
2014-10-09 13:43:10 -04:00
|
|
|
STATIC const mp_obj_type_t usocket_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_socket,
|
2014-01-18 16:47:44 -05:00
|
|
|
.print = socket_print,
|
|
|
|
.make_new = socket_make_new,
|
|
|
|
.getiter = NULL,
|
|
|
|
.iternext = NULL,
|
2014-10-09 13:43:10 -04:00
|
|
|
.stream_p = &usocket_stream_p,
|
|
|
|
.locals_dict = (mp_obj_t)&usocket_locals_dict,
|
2014-01-18 16:47:44 -05:00
|
|
|
};
|
|
|
|
|
2014-05-11 18:21:50 -04:00
|
|
|
#if MICROPY_SOCKET_EXTRA
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t mod_socket_htons(mp_obj_t arg) {
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(htons(MP_OBJ_SMALL_INT_VALUE(arg)));
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_htons_obj, mod_socket_htons);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t mod_socket_inet_aton(mp_obj_t arg) {
|
2014-03-29 09:43:38 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(arg, &mp_type_str));
|
2014-01-22 09:35:10 -05:00
|
|
|
const char *s = mp_obj_str_get_str(arg);
|
2014-01-18 16:47:44 -05:00
|
|
|
struct in_addr addr;
|
|
|
|
if (!inet_aton(s, &addr)) {
|
2014-04-21 16:58:39 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(EINVAL)));
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_obj_new_int(addr.s_addr);
|
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_inet_aton_obj, mod_socket_inet_aton);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC mp_obj_t mod_socket_gethostbyname(mp_obj_t arg) {
|
2014-03-29 09:43:38 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(arg, &mp_type_str));
|
2014-01-22 09:35:10 -05:00
|
|
|
const char *s = mp_obj_str_get_str(arg);
|
2014-01-18 16:47:44 -05:00
|
|
|
struct hostent *h = gethostbyname(s);
|
|
|
|
if (h == NULL) {
|
2014-04-21 16:58:39 -04:00
|
|
|
// CPython: socket.herror
|
2014-07-31 05:49:14 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(h_errno)));
|
2014-01-18 16:47:44 -05:00
|
|
|
}
|
|
|
|
assert(h->h_length == 4);
|
|
|
|
return mp_obj_new_int(*(int*)*h->h_addr_list);
|
|
|
|
}
|
2014-04-04 13:25:53 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_gethostbyname_obj, mod_socket_gethostbyname);
|
2014-05-11 18:21:50 -04:00
|
|
|
#endif // MICROPY_SOCKET_EXTRA
|
2014-01-18 16:47:44 -05:00
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t mod_socket_getaddrinfo(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-01-18 16:47:44 -05:00
|
|
|
// TODO: Implement all args
|
|
|
|
assert(n_args == 2);
|
2014-01-22 09:35:10 -05:00
|
|
|
assert(MP_OBJ_IS_STR(args[0]));
|
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;
|
|
|
|
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
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(args[1])) {
|
2014-02-10 10:58:30 -05:00
|
|
|
int port = (short)MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
|
|
char buf[6];
|
2014-01-18 16:47:44 -05:00
|
|
|
sprintf(buf, "%d", port);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-22 12:11:34 -04:00
|
|
|
struct addrinfo *addr_list;
|
2014-06-22 12:14:12 -04:00
|
|
|
int res = getaddrinfo(host, serv, &hints, &addr_list);
|
2014-01-18 16:47:44 -05:00
|
|
|
|
|
|
|
if (res != 0) {
|
2014-04-21 16:58:39 -04:00
|
|
|
// CPython: socket.gaierror
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[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) {
|
2014-01-18 16:47:44 -05:00
|
|
|
mp_obj_tuple_t *t = 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);
|
2014-03-31 11:28:13 -04:00
|
|
|
mp_obj_list_append(list, 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;
|
|
|
|
}
|
2014-04-04 13:25:53 -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
|
|
|
|
|
|
|
extern mp_obj_type_t sockaddr_in_type;
|
|
|
|
|
2014-04-17 13:34:04 -04:00
|
|
|
STATIC const mp_map_elem_t mp_module_socket_globals_table[] = {
|
2014-10-09 13:43:10 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&usocket_type },
|
2014-04-17 13:34:04 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_socket_getaddrinfo_obj },
|
|
|
|
#if MICROPY_SOCKET_EXTRA
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sockaddr_in), (mp_obj_t)&sockaddr_in_type },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_htons), (mp_obj_t)&mod_socket_htons_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_inet_aton), (mp_obj_t)&mod_socket_inet_aton_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_gethostbyname), (mp_obj_t)&mod_socket_gethostbyname_obj },
|
|
|
|
#endif
|
2014-01-25 13:13:17 -05:00
|
|
|
|
2014-04-17 13:34:04 -04:00
|
|
|
#define C(name) { MP_OBJ_NEW_QSTR(MP_QSTR_ ## name), MP_OBJ_NEW_SMALL_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 },
|
2014-10-09 13:43:10 -04:00
|
|
|
.name = MP_QSTR_usocket,
|
2014-04-17 13:34:04 -04:00
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_socket_globals,
|
|
|
|
};
|