diff --git a/unix/modsocket.c b/unix/modsocket.c index 371c0057b8..0b0b171e5f 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -493,11 +493,36 @@ STATIC mp_obj_t mod_socket_getaddrinfo(mp_uint_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 6, mod_socket_getaddrinfo); +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; + mp_obj_tuple_t *t = mp_obj_new_tuple(3, NULL); + 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)); + return t; + } + default: { + struct sockaddr *sa = (struct sockaddr*)bufinfo.buf; + mp_obj_tuple_t *t = mp_obj_new_tuple(2, NULL); + 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)); + return t; + } + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_sockaddr_obj, mod_socket_sockaddr); + STATIC const mp_map_elem_t mp_module_socket_globals_table[] = { { 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 }, { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_socket_getaddrinfo_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_inet_pton), (mp_obj_t)&mod_socket_inet_pton_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sockaddr), (mp_obj_t)&mod_socket_sockaddr_obj }, #if MICROPY_SOCKET_EXTRA { MP_OBJ_NEW_QSTR(MP_QSTR_htons), (mp_obj_t)&mod_socket_htons_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gethostbyname), (mp_obj_t)&mod_socket_gethostbyname_obj }, diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h index daa694228b..f6ba9e4106 100644 --- a/unix/qstrdefsport.h +++ b/unix/qstrdefsport.h @@ -79,7 +79,7 @@ Q(ticks_us) Q(ticks_diff) Q(socket) -Q(sockaddr_in) +Q(sockaddr) Q(htons) Q(inet_pton) Q(gethostbyname)