static void telnet_setup_server(struct net_context **ctx, sa_family_t family, struct sockaddr *addr, socklen_t addrlen) { if (net_context_get(family, SOCK_STREAM, IPPROTO_TCP, ctx)) { SYS_LOG_ERR("No context available"); goto error; } if (net_context_bind(*ctx, addr, addrlen)) { SYS_LOG_ERR("Cannot bind on family AF_INET%s", family == AF_INET ? "" : "6"); goto error; } if (net_context_listen(*ctx, 0)) { SYS_LOG_ERR("Cannot listen on"); goto error; } if (net_context_accept(*ctx, telnet_accept, 0, NULL)) { SYS_LOG_ERR("Cannot accept"); goto error; } SYS_LOG_DBG("Telnet console enabled on AF_INET%s", family == AF_INET ? "" : "6"); return; error: SYS_LOG_ERR("Unable to start telnet on AF_INET%s", family == AF_INET ? "" : "6"); if (*ctx) { net_context_put(*ctx); *ctx = NULL; } }
static inline bool get_context(struct net_context **udp_recv6, struct net_context **tcp_recv6, struct net_context **mcast_recv6) { int ret; struct sockaddr_in6 mcast_addr6 = { 0 }; struct sockaddr_in6 my_addr6 = { 0 }; net_ipaddr_copy(&mcast_addr6.sin6_addr, &in6addr_mcast); mcast_addr6.sin6_family = AF_INET6; my_addr6.sin6_family = AF_INET6; my_addr6.sin6_port = htons(MY_PORT); ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, udp_recv6); if (ret < 0) { printk("Cannot get network context for IPv6 UDP (%d)", ret); return false; } ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6, sizeof(struct sockaddr_in6)); if (ret < 0) { printk("Cannot bind IPv6 UDP port %d (%d)", ntohs(my_addr6.sin6_port), ret); return false; } ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, mcast_recv6); if (ret < 0) { printk("Cannot get receiving IPv6 mcast " "network context (%d)", ret); return false; } ret = net_context_bind(*mcast_recv6, (struct sockaddr *)&mcast_addr6, sizeof(struct sockaddr_in6)); if (ret < 0) { printk("Cannot bind IPv6 mcast (%d)", ret); return false; } ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, tcp_recv6); if (ret < 0) { printk("Cannot get network context for IPv6 TCP (%d)", ret); return false; } net_context_setup_pools(*tcp_recv6, tx_tcp_pool, data_tcp_pool); ret = net_context_bind(*tcp_recv6, (struct sockaddr *)&my_addr6, sizeof(struct sockaddr_in6)); if (ret < 0) { printk("Cannot bind IPv6 TCP port %d (%d)", ntohs(my_addr6.sin6_port), ret); return false; } ret = net_context_listen(*tcp_recv6, 0); if (ret < 0) { printk("Cannot listen IPv6 TCP (%d)", ret); return false; } return true; }
static inline bool get_context(struct net_context **udp_recv4, struct net_context **udp_recv6, struct net_context **tcp_recv4, struct net_context **tcp_recv6) { int ret; #if defined(CONFIG_NET_IPV6) struct sockaddr_in6 my_addr6 = { 0 }; #endif #if defined(CONFIG_NET_IPV4) struct sockaddr_in my_addr4 = { 0 }; #endif #if defined(CONFIG_NET_IPV6) #if !NET_BIND_ANY_ADDR net_ipaddr_copy(&my_addr6.sin6_addr, &in6addr_my); #endif my_addr6.sin6_family = AF_INET6; my_addr6.sin6_port = htons(MY_PORT); #endif #if defined(CONFIG_NET_IPV4) #if !NET_BIND_ANY_ADDR net_ipaddr_copy(&my_addr4.sin_addr, &in4addr_my); #endif my_addr4.sin_family = AF_INET; my_addr4.sin_port = htons(MY_PORT); #endif #if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_UDP) ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, udp_recv6); if (ret < 0) { NET_ERR("Cannot get network context for IPv6 UDP (%d)", ret); return false; } net_context_setup_pools(*udp_recv6, tx_udp_slab, data_udp_pool); ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6, sizeof(struct sockaddr_in6)); if (ret < 0) { NET_ERR("Cannot bind IPv6 UDP port %d (%d)", ntohs(my_addr6.sin6_port), ret); return false; } #endif #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_UDP) ret = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_UDP, udp_recv4); if (ret < 0) { NET_ERR("Cannot get network context for IPv4 UDP (%d)", ret); return false; } net_context_setup_pools(*udp_recv4, tx_udp_slab, data_udp_pool); ret = net_context_bind(*udp_recv4, (struct sockaddr *)&my_addr4, sizeof(struct sockaddr_in)); if (ret < 0) { NET_ERR("Cannot bind IPv4 UDP port %d (%d)", ntohs(my_addr4.sin_port), ret); return false; } #endif #if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_TCP) if (tcp_recv6) { ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, tcp_recv6); if (ret < 0) { NET_ERR("Cannot get network context " "for IPv6 TCP (%d)", ret); return false; } net_context_setup_pools(*tcp_recv6, tx_tcp_slab, data_tcp_pool); ret = net_context_bind(*tcp_recv6, (struct sockaddr *)&my_addr6, sizeof(struct sockaddr_in6)); if (ret < 0) { NET_ERR("Cannot bind IPv6 TCP port %d (%d)", ntohs(my_addr6.sin6_port), ret); return false; } ret = net_context_listen(*tcp_recv6, 0); if (ret < 0) { NET_ERR("Cannot listen IPv6 TCP (%d)", ret); return false; } } #endif #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_TCP) if (tcp_recv4) { ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, tcp_recv4); if (ret < 0) { NET_ERR("Cannot get network context for IPv4 TCP"); return false; } net_context_setup_pools(*tcp_recv4, tx_tcp_slab, data_tcp_pool); ret = net_context_bind(*tcp_recv4, (struct sockaddr *)&my_addr4, sizeof(struct sockaddr_in)); if (ret < 0) { NET_ERR("Cannot bind IPv4 TCP port %d", ntohs(my_addr4.sin_port)); return false; } ret = net_context_listen(*tcp_recv4, 0); if (ret < 0) { NET_ERR("Cannot listen IPv4 TCP"); return false; } } #endif return true; }