STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct sockaddr *sockaddr) { // We employ the fact that port and address offsets are the same for IPv4 & IPv6 struct sockaddr_in *sockaddr_in = (struct sockaddr_in*)sockaddr; mp_obj_t *addr_items; mp_obj_get_array_fixed_n(addr_in, 2, &addr_items); sockaddr_in->sin_family = net_context_get_family((void*)socket->ctx); RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr)); sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1])); }
static inline void init_app(void) { NET_INFO("Run echo server"); k_sem_init(&quit_lock, 0, UINT_MAX); #if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_APP_MY_IPV6_ADDR) if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &in6addr_my) < 0) { NET_ERR("Invalid IPv6 address %s", CONFIG_NET_APP_MY_IPV6_ADDR); } #endif do { struct net_if_addr *ifaddr; ifaddr = net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0); } while (0); #endif #if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_DHCPV4) net_dhcpv4_start(net_if_get_default()); #else #if defined(CONFIG_NET_APP_MY_IPV4_ADDR) if (net_addr_pton(AF_INET, CONFIG_NET_APP_MY_IPV4_ADDR, &in4addr_my) < 0) { NET_ERR("Invalid IPv4 address %s", CONFIG_NET_APP_MY_IPV4_ADDR); } net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0); #endif #endif /* CONFIG_NET_DHCPV4 */ #endif /* CONFIG_NET_IPV4 */ }
static void setup_ipv6(struct net_if *iface) { char hr_addr[NET_IPV6_ADDR_LEN]; struct in6_addr addr; if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &addr)) { NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV6_ADDR); return; } net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); NET_INFO("IPv6 address: %s", net_addr_ntop(AF_INET6, &addr, hr_addr, NET_IPV6_ADDR_LEN)); if (net_addr_pton(AF_INET6, MCAST_IP6ADDR, &addr)) { NET_ERR("Invalid address: %s", MCAST_IP6ADDR); return; } net_if_ipv6_maddr_add(iface, &addr); }
static void setup_device(void) { char hr_addr[NET_IPV6_ADDR_LEN]; struct net_if *iface; struct in6_addr addr; struct device *dev; dev = device_get_binding(CONFIG_IEEE802154_UPIPE_DRV_NAME); if (!dev) { NET_INFO("Cannot get UPIPE device\n"); return; } iface = net_if_lookup_by_dev(dev); if (!iface) { NET_INFO("Cannot get UPIPE network interface\n"); return; } if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &addr)) { NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV6_ADDR); return; } net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); NET_INFO("IPv6 address: %s", net_addr_ntop(AF_INET6, &addr, hr_addr, NET_IPV6_ADDR_LEN)); if (net_addr_pton(AF_INET6, MCAST_IP6ADDR, &addr)) { NET_ERR("Invalid address: %s", MCAST_IP6ADDR); return; } NET_INFO("802.15.4 device up and running\n"); }
static void setup_ipv4(struct net_if *iface) { char hr_addr[NET_IPV4_ADDR_LEN]; struct in_addr addr; if (net_addr_pton(AF_INET, CONFIG_NET_APP_MY_IPV4_ADDR, &addr)) { NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV4_ADDR); return; } net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); NET_INFO("IPv4 address: %s", net_addr_ntop(AF_INET, &addr, hr_addr, NET_IPV4_ADDR_LEN)); }
static inline int init_app(void) { #if defined(CONFIG_NET_APP_MY_IPV6_ADDR) if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &server_addr) < 0) { mbedtls_printf("Invalid IPv6 address %s", CONFIG_NET_APP_MY_IPV6_ADDR); } #endif if (!net_if_ipv6_addr_add(net_if_get_default(), &server_addr, NET_ADDR_MANUAL, 0)) { return -EIO; } net_if_ipv6_maddr_add(net_if_get_default(), &mcast_addr); return 0; }
static inline void init_app(void) { printk("Run IPSP sample"); k_sem_init(&quit_lock, 0, UINT_MAX); if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &in6addr_my) < 0) { printk("Invalid IPv6 address %s", CONFIG_NET_APP_MY_IPV6_ADDR); } do { struct net_if_addr *ifaddr; ifaddr = net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0); } while (0); net_if_ipv6_maddr_add(net_if_get_default(), &in6addr_mcast); }
static inline int set_remote_addr(struct http_client_ctx *ctx, const char *server, u16_t server_port) { int ret; #if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4) ret = net_addr_pton(AF_INET6, server, &net_sin6(&ctx->tcp.remote)->sin6_addr); if (ret < 0) { /* Could be hostname, try DNS if configured. */ #if !defined(CONFIG_DNS_RESOLVER) NET_ERR("Invalid IPv6 address %s", server); return -EINVAL; #else ret = resolve_name(ctx, server, DNS_QUERY_TYPE_AAAA); if (ret < 0) { NET_ERR("Cannot resolve %s (%d)", server, ret); return ret; } #endif } net_sin6(&ctx->tcp.remote)->sin6_port = htons(server_port); net_sin6(&ctx->tcp.remote)->sin6_family = AF_INET6; #endif /* IPV6 && !IPV4 */ #if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6) ret = net_addr_pton(AF_INET, server, &net_sin(&ctx->tcp.remote)->sin_addr); if (ret < 0) { /* Could be hostname, try DNS if configured. */ #if !defined(CONFIG_DNS_RESOLVER) NET_ERR("Invalid IPv4 address %s", server); return -EINVAL; #else ret = resolve_name(ctx, server, DNS_QUERY_TYPE_A); if (ret < 0) { NET_ERR("Cannot resolve %s (%d)", server, ret); return ret; } #endif } net_sin(&ctx->tcp.remote)->sin_port = htons(server_port); net_sin(&ctx->tcp.remote)->sin_family = AF_INET; #endif /* IPV6 && !IPV4 */ #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_IPV6) ret = net_addr_pton(AF_INET, server, &net_sin(&ctx->tcp.remote)->sin_addr); if (ret < 0) { ret = net_addr_pton(AF_INET6, server, &net_sin6(&ctx->tcp.remote)->sin6_addr); if (ret < 0) { /* Could be hostname, try DNS if configured. */ #if !defined(CONFIG_DNS_RESOLVER) NET_ERR("Invalid IPv4 or IPv6 address %s", server); return -EINVAL; #else ret = resolve_name(ctx, server, DNS_QUERY_TYPE_A); if (ret < 0) { ret = resolve_name(ctx, server, DNS_QUERY_TYPE_AAAA); if (ret < 0) { NET_ERR("Cannot resolve %s (%d)", server, ret); return ret; } goto ipv6; } goto ipv4; #endif /* !CONFIG_DNS_RESOLVER */ } else { #if defined(CONFIG_DNS_RESOLVER) ipv6: #endif net_sin6(&ctx->tcp.remote)->sin6_port = htons(server_port); net_sin6(&ctx->tcp.remote)->sin6_family = AF_INET6; } } else { #if defined(CONFIG_DNS_RESOLVER) ipv4: #endif net_sin(&ctx->tcp.remote)->sin_port = htons(server_port); net_sin(&ctx->tcp.remote)->sin_family = AF_INET; } #endif /* IPV4 && IPV6 */ /* If we have not yet figured out what is the protocol family, * then we cannot continue. */ if (ctx->tcp.remote.family == AF_UNSPEC) { NET_ERR("Unknown protocol family."); return -EPFNOSUPPORT; } return 0; }