const char *LWIP::get_ip_address() { if (!default_interface) { return NULL; } const ip_addr_t *addr = get_ip_addr(true, &default_interface->netif); if (!addr) { return NULL; } #if LWIP_IPV6 if (IP_IS_V6(addr)) { return ip6addr_ntoa_r(ip_2_ip6(addr), ip_address, sizeof(ip_address)); } #endif #if LWIP_IPV4 if (IP_IS_V4(addr)) { return ip4addr_ntoa_r(ip_2_ip4(addr), ip_address, sizeof(ip_address)); } #endif #if LWIP_IPV6 && LWIP_IPV4 return NULL; #endif }
char *LWIP::Interface::get_gateway(char *buf, nsapi_size_t buflen) { #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_gw(&netif); if (!ip4_addr_isany(addr)) { return ip4addr_ntoa_r(addr, buf, buflen); } else { return NULL; } #else return NULL; #endif }
const char *mbed_lwip_get_netmask(char *buf, nsapi_size_t buflen) { #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_netmask(&lwip_netif); if (!ip4_addr_isany(addr)) { return ip4addr_ntoa_r(addr, buf, buflen); } else { return NULL; } #else return NULL; #endif }
char *mbed_lwip_get_ip_address(char *buf, nsapi_size_t buflen) { const ip_addr_t *addr = mbed_lwip_get_ip_addr(true, &lwip_netif); if (!addr) { return NULL; } #if LWIP_IPV6 if (IP_IS_V6(addr)) { return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen); } #endif #if LWIP_IPV4 if (IP_IS_V4(addr)) { return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen); } #endif return NULL; }
char *LWIP::Interface::get_ip_address(char *buf, nsapi_size_t buflen) { const ip_addr_t *addr = LWIP::get_ip_addr(true, &netif); if (!addr) { return NULL; } #if LWIP_IPV6 if (IP_IS_V6(addr)) { return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen); } #endif #if LWIP_IPV4 if (IP_IS_V4(addr)) { return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen); } #endif #if LWIP_IPV6 && LWIP_IPV4 return NULL; #endif }
/** * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used. * * @param addr ip6 address in network order to convert * @param buf target buffer where the string is stored * @param buflen length of buf * @return either pointer to buf which now holds the ASCII * representation of addr or NULL if buf was too small */ char * ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen) { u32_t current_block_index, current_block_value, next_block_value; s32_t i; u8_t zero_flag, empty_block_flag; #if LWIP_IPV4 if (ip6_addr_isipv4mappedipv6(addr)) { /* This is an IPv4 mapped address */ ip4_addr_t addr4; char *ret; #define IP4MAPPED_HEADER "::FFFF:" char *buf_ip4 = buf + sizeof(IP4MAPPED_HEADER) - 1; int buflen_ip4 = buflen - sizeof(IP4MAPPED_HEADER) + 1; if (buflen < (int)sizeof(IP4MAPPED_HEADER)) { return NULL; } memcpy(buf, IP4MAPPED_HEADER, sizeof(IP4MAPPED_HEADER)); addr4.addr = addr->addr[3]; ret = ip4addr_ntoa_r(&addr4, buf_ip4, buflen_ip4); if (ret != buf_ip4) { return NULL; } return buf; } #endif /* LWIP_IPV4 */ i = 0; empty_block_flag = 0; /* used to indicate a zero chain for "::' */ for (current_block_index = 0; current_block_index < 8; current_block_index++) { /* get the current 16-bit block */ current_block_value = lwip_htonl(addr->addr[current_block_index >> 1]); if ((current_block_index & 0x1) == 0) { current_block_value = current_block_value >> 16; } current_block_value &= 0xffff; /* Check for empty block. */ if (current_block_value == 0) { if (current_block_index == 7 && empty_block_flag == 1) { /* special case, we must render a ':' for the last block. */ buf[i++] = ':'; if (i >= buflen) { return NULL; } break; } if (empty_block_flag == 0) { /* generate empty block "::", but only if more than one contiguous zero block, * according to current formatting suggestions RFC 5952. */ next_block_value = lwip_htonl(addr->addr[(current_block_index + 1) >> 1]); if ((current_block_index & 0x1) == 0x01) { next_block_value = next_block_value >> 16; } next_block_value &= 0xffff; if (next_block_value == 0) { empty_block_flag = 1; buf[i++] = ':'; if (i >= buflen) { return NULL; } continue; /* move on to next block. */ } } else if (empty_block_flag == 1) {
/** * Convert numeric IP address into decimal dotted ASCII representation. * returns ptr to static buffer; not reentrant! * * @param addr ip address in network order to convert * @return pointer to a global static (!) buffer that holds the ASCII * representation of addr */ char* ip4addr_ntoa(const ip4_addr_t *addr) { static char str[IP4ADDR_STRLEN_MAX]; return ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX); }
STATIC mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { // TODO support additional args beyond the first two struct addrinfo *res = NULL; _socket_getaddrinfo2(args[0], args[1], &res); mp_obj_t ret_list = mp_obj_new_list(0, NULL); for (struct addrinfo *resi = res; resi; resi = resi->ai_next) { mp_obj_t addrinfo_objs[5] = { mp_obj_new_int(resi->ai_family), mp_obj_new_int(resi->ai_socktype), mp_obj_new_int(resi->ai_protocol), mp_obj_new_str(resi->ai_canonname, strlen(resi->ai_canonname)), mp_const_none }; if (resi->ai_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)resi->ai_addr; // This looks odd, but it's really just a u32_t ip4_addr_t ip4_addr = { .addr = addr->sin_addr.s_addr }; char buf[16]; ip4addr_ntoa_r(&ip4_addr, buf, sizeof(buf)); mp_obj_t inaddr_objs[2] = { mp_obj_new_str(buf, strlen(buf)), mp_obj_new_int(ntohs(addr->sin_port)) }; addrinfo_objs[4] = mp_obj_new_tuple(2, inaddr_objs); } mp_obj_list_append(ret_list, mp_obj_new_tuple(5, addrinfo_objs)); } if (res) lwip_freeaddrinfo(res); return ret_list; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_socket_getaddrinfo_obj, 2, 6, esp_socket_getaddrinfo); STATIC mp_obj_t esp_socket_initialize() { static int initialized = 0; if (!initialized) { ESP_LOGI("modsocket", "Initializing"); tcpip_adapter_init(); initialized = 1; } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_socket_initialize_obj, esp_socket_initialize); 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___init__), (mp_obj_t)&esp_socket_initialize_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&get_socket_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&esp_socket_getaddrinfo_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) }, { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_RAW), MP_OBJ_NEW_SMALL_INT(SOCK_RAW) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_TCP), MP_OBJ_NEW_SMALL_INT(IPPROTO_TCP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_UDP), MP_OBJ_NEW_SMALL_INT(IPPROTO_UDP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_IP), MP_OBJ_NEW_SMALL_INT(IPPROTO_IP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(SOL_SOCKET) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(SO_REUSEADDR) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IP_ADD_MEMBERSHIP), MP_OBJ_NEW_SMALL_INT(IP_ADD_MEMBERSHIP) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_socket_globals, mp_module_socket_globals_table); const mp_obj_module_t mp_module_usocket = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&mp_module_socket_globals, };