static void telnet_accept(struct net_context *client, struct sockaddr *addr, socklen_t addrlen, int error, void *user_data) { if (error) { LOG_ERR("Error %d", error); goto error; } if (client_cnx) { LOG_WRN("A telnet client is already in."); goto error; } if (net_context_recv(client, telnet_recv, 0, NULL)) { LOG_ERR("Unable to setup reception (family %u)", net_context_get_family(client)); goto error; } LOG_DBG("Telnet client connected (family AF_INET%s)", net_context_get_family(client) == AF_INET ? "" : "6"); orig_printk_hook = __printk_get_hook(); __printk_hook_install(telnet_console_out); client_cnx = client; k_timer_start(&send_timer, TELNET_TIMEOUT, TELNET_TIMEOUT); return; error: net_context_put(client); }
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 void tcp_accepted(struct net_context *context, struct sockaddr *addr, socklen_t addrlen, int error, void *user_data) { int ret; NET_DBG("Accept called, context %p error %d", context, error); ret = net_context_recv(context, tcp_received, 0, NULL); if (ret < 0) { printk("Cannot receive TCP packet (family %d)", net_context_get_family(context)); } }
static void telnet_recv(struct net_context *client, struct net_pkt *pkt, int status, void *user_data) { if (!pkt || status) { telnet_end_client_connection(); SYS_LOG_DBG("Telnet client dropped (AF_INET%s) status %d", net_context_get_family(client) == AF_INET ? "" : "6", status); return; } telnet_handle_input(pkt); net_pkt_unref(pkt); }