/** @ingroup tftp * Initialize TFTP server. * @param ctx TFTP callback struct */ err_t tftp_init(const struct tftp_context *ctx) { err_t ret; struct udp_pcb *pcb = udp_new_ip_type(IPADDR_TYPE_ANY); if (pcb == NULL) { return ERR_MEM; } ret = udp_bind(pcb, IP_ANY_TYPE, TFTP_PORT); if (ret != ERR_OK) { udp_remove(pcb); return ret; } tftp_state.handle = NULL; tftp_state.port = 0; tftp_state.ctx = ctx; tftp_state.timer = 0; tftp_state.last_data = NULL; tftp_state.upcb = pcb; udp_recv(pcb, recv, NULL); return ERR_OK; }
/** * Initialize TFTP client/server. * @param mode TFTP mode (client/server) * @param ctx TFTP callback struct */ err_t tftp_init_common(u8_t mode, const struct tftp_context *ctx) { err_t ret; /* LWIP_ASSERT_CORE_LOCKED(); is checked by udp_new() */ struct udp_pcb *pcb = udp_new_ip_type(IPADDR_TYPE_ANY); if (pcb == NULL) { return ERR_MEM; } ret = udp_bind(pcb, IP_ANY_TYPE, TFTP_PORT); if (ret != ERR_OK) { udp_remove(pcb); return ret; } tftp_state.handle = NULL; tftp_state.port = 0; tftp_state.ctx = ctx; tftp_state.timer = 0; tftp_state.last_data = NULL; tftp_state.upcb = pcb; tftp_state.tftp_mode = mode; udp_recv(pcb, recv, NULL); return ERR_OK; }
/** * @ingroup zepif * Set up a raw 6LowPAN netif and surround it with input- and output * functions for ZEP */ err_t zepif_init(struct netif *netif) { err_t err; struct zepif_init *init_state = (struct zepif_init *)netif->state; struct zepif_state *state = (struct zepif_state *)mem_malloc(sizeof(struct zepif_state)); LWIP_ASSERT("zepif needs an input callback", netif->input != NULL); if (state == NULL) { return ERR_MEM; } memset(state, 0, sizeof(struct zepif_state)); if (init_state != NULL) { memcpy(&state->init, init_state, sizeof(struct zepif_init)); } if (state->init.zep_src_udp_port == 0) { state->init.zep_src_udp_port = ZEPIF_DEFAULT_UDP_PORT; } if (state->init.zep_dst_udp_port == 0) { state->init.zep_dst_udp_port = ZEPIF_DEFAULT_UDP_PORT; } #if LWIP_IPV4 if (state->init.zep_dst_ip_addr == NULL) { /* With IPv4 enabled, default to broadcasting packets if no address is set */ state->init.zep_dst_ip_addr = IP_ADDR_BROADCAST; } #endif /* LWIP_IPV4 */ netif->state = NULL; state->pcb = udp_new_ip_type(IPADDR_TYPE_ANY); if (state->pcb == NULL) { err = ERR_MEM; goto err_ret; } err = udp_bind(state->pcb, state->init.zep_src_ip_addr, state->init.zep_src_udp_port); if (err != ERR_OK) { goto err_ret; } if (state->init.zep_netif != NULL) { udp_bind_netif(state->pcb, state->init.zep_netif); } LWIP_ASSERT("udp_bind(lowpan6_broadcast_pcb) failed", err == ERR_OK); ip_set_option(state->pcb, SOF_BROADCAST); udp_recv(state->pcb, zepif_udp_recv, netif); err = lowpan6_if_init(netif); LWIP_ASSERT("lowpan6_if_init set a state", netif->state == NULL); if (err == ERR_OK) { netif->state = state; netif->hwaddr_len = 6; if (init_state != NULL) { memcpy(netif->hwaddr, init_state->addr, 6); } else { u8_t i; for (i = 0; i < 6; i++) { netif->hwaddr[i] = i; } netif->hwaddr[0] &= 0xfc; } netif->linkoutput = zepif_linkoutput; if (!zep_lowpan_timer_running) { sys_timeout(LOWPAN6_TMR_INTERVAL, zep_lowpan_timer, NULL); zep_lowpan_timer_running = 1; } return ERR_OK; } err_ret: if (state->pcb != NULL) { udp_remove(state->pcb); } mem_free(state); return err; }