Commit a88c30c6 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

unix socket: Add setsockopt() method.

parent 9d95a2b2
......@@ -162,6 +162,29 @@ static mp_obj_t socket_send(uint n_args, const mp_obj_t *args) {
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_send_obj, 2, 3, socket_send);
static mp_obj_t socket_setsockopt(uint n_args, const mp_obj_t *args) {
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])) {
int val = mp_obj_int_get(args[3]);
optval = &val;
optlen = sizeof(val);
} else {
buffer_info_t bufinfo;
get_buffer(args[3], &bufinfo);
optval = bufinfo.buf;
optlen = bufinfo.len;
}
int r = setsockopt(self->fd, level, option, optval, optlen);
RAISE_ERRNO(r, errno);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
static mp_obj_t socket_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
int family = AF_INET;
int type = SOCK_STREAM;
......@@ -196,6 +219,7 @@ static const mp_method_t rawsocket_type_methods[] = {
{ "accept", &socket_accept_obj },
{ "recv", &socket_recv_obj },
{ "send", &socket_send_obj },
{ "setsockopt", &socket_setsockopt_obj },
{ "close", &socket_close_obj },
#if MICROPY_SOCKET_EXTRA
{ "recv", &mp_stream_read_obj },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment