unix: Add socket.inet_ntop function

This commit is contained in:
Dave Hylands 2016-01-10 21:37:59 -08:00 committed by Paul Sokolovsky
parent 5b3f0b7f39
commit 11b97d7ec5
2 changed files with 16 additions and 0 deletions

View File

@ -421,6 +421,20 @@ STATIC mp_obj_t mod_socket_inet_pton(mp_obj_t family_in, mp_obj_t addr_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_pton_obj, mod_socket_inet_pton);
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) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
}
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);
STATIC mp_obj_t mod_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) {
// TODO: Implement all args
assert(n_args >= 2 && n_args <= 4);
@ -522,6 +536,7 @@ STATIC const mp_rom_map_elem_t mp_module_socket_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&usocket_type) },
{ 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) },
{ MP_ROM_QSTR(MP_QSTR_inet_ntop), MP_ROM_PTR(&mod_socket_inet_ntop_obj) },
{ MP_ROM_QSTR(MP_QSTR_sockaddr), MP_ROM_PTR(&mod_socket_sockaddr_obj) },
#if MICROPY_SOCKET_EXTRA
{ MP_ROM_QSTR(MP_QSTR_htons), MP_ROM_PTR(&mod_socket_htons_obj) },

View File

@ -88,6 +88,7 @@ Q(socket)
Q(sockaddr)
Q(htons)
Q(inet_pton)
Q(inet_ntop)
Q(gethostbyname)
Q(getaddrinfo)
Q(usocket)