📄 netif.c
字号:
* @param netif the network interface to change * @param gw the new default gateway * * @note call netif_set_addr() if you also want to change ip address and netmask */voidnetif_set_gw(struct netif *netif, ip_addr_t *gw){ ip_addr_set(&(netif->gw), gw); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], ip4_addr1_16(&netif->gw), ip4_addr2_16(&netif->gw), ip4_addr3_16(&netif->gw), ip4_addr4_16(&netif->gw)));}/** * Change the netmask of a network interface * * @param netif the network interface to change * @param netmask the new netmask * * @note call netif_set_addr() if you also want to change ip address and * default gateway */voidnetif_set_netmask(struct netif *netif, ip_addr_t *netmask){ snmp_delete_iprteidx_tree(0, netif); /* set new netmask to netif */ ip_addr_set(&(netif->netmask), netmask); snmp_insert_iprteidx_tree(0, netif); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], ip4_addr1_16(&netif->netmask), ip4_addr2_16(&netif->netmask), ip4_addr3_16(&netif->netmask), ip4_addr4_16(&netif->netmask)));}/** * Set a network interface as the default network interface * (used to output all packets for which no specific route is found) * * @param netif the default network interface */voidnetif_set_default(struct netif *netif){ if (netif == NULL) { /* remove default route */ snmp_delete_iprteidx_tree(1, netif); } else { /* install default route */ snmp_insert_iprteidx_tree(1, netif); } netif_default = netif; LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n", netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));}/** * Bring an interface up, available for processing * traffic. * * @note: Enabling DHCP on a down interface will make it come * up once configured. * * @see dhcp_start() */ void netif_set_up(struct netif *netif){ if (!(netif->flags & NETIF_FLAG_UP)) { netif->flags |= NETIF_FLAG_UP; #if LWIP_SNMP snmp_get_sysuptime(&netif->ts);#endif /* LWIP_SNMP */ NETIF_STATUS_CALLBACK(netif); if (netif->flags & NETIF_FLAG_LINK_UP) {#if LWIP_ARP /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ if (netif->flags & (NETIF_FLAG_ETHARP)) { etharp_gratuitous(netif); }#endif /* LWIP_ARP */#if LWIP_IGMP /* resend IGMP memberships */ if (netif->flags & NETIF_FLAG_IGMP) { igmp_report_groups( netif); }#endif /* LWIP_IGMP */ } }}/** * Bring an interface down, disabling any traffic processing. * * @note: Enabling DHCP on a down interface will make it come * up once configured. * * @see dhcp_start() */ void netif_set_down(struct netif *netif){ if (netif->flags & NETIF_FLAG_UP) { netif->flags &= ~NETIF_FLAG_UP;#if LWIP_SNMP snmp_get_sysuptime(&netif->ts);#endif NETIF_STATUS_CALLBACK(netif); }}#if LWIP_NETIF_STATUS_CALLBACK/** * Set callback to be called when interface is brought up/down */void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback){ if (netif) { netif->status_callback = status_callback; }}#endif /* LWIP_NETIF_STATUS_CALLBACK *//** * Called by a driver when its link goes up */void netif_set_link_up(struct netif *netif ){ if (!(netif->flags & NETIF_FLAG_LINK_UP)) { netif->flags |= NETIF_FLAG_LINK_UP;#if LWIP_DHCP if (netif->dhcp) { dhcp_network_changed(netif); }#endif /* LWIP_DHCP */#if LWIP_AUTOIP if (netif->autoip) { autoip_network_changed(netif); }#endif /* LWIP_AUTOIP */ if (netif->flags & NETIF_FLAG_UP) {#if LWIP_ARP /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ if (netif->flags & NETIF_FLAG_ETHARP) { etharp_gratuitous(netif); }#endif /* LWIP_ARP */#if LWIP_IGMP /* resend IGMP memberships */ if (netif->flags & NETIF_FLAG_IGMP) { igmp_report_groups( netif); }#endif /* LWIP_IGMP */ } NETIF_LINK_CALLBACK(netif); }}/** * Called by a driver when its link goes down */void netif_set_link_down(struct netif *netif ){ if (netif->flags & NETIF_FLAG_LINK_UP) { netif->flags &= ~NETIF_FLAG_LINK_UP; NETIF_LINK_CALLBACK(netif); }}#if LWIP_NETIF_LINK_CALLBACK/** * Set callback to be called when link is brought up/down */void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback){ if (netif) { netif->link_callback = link_callback; }}#endif /* LWIP_NETIF_LINK_CALLBACK */#if ENABLE_LOOPBACK/** * Send an IP packet to be received on the same netif (loopif-like). * The pbuf is simply copied and handed back to netif->input. * In multithreaded mode, this is done directly since netif->input must put * the packet on a queue. * In callback mode, the packet is put on an internal queue and is fed to * netif->input by netif_poll(). * * @param netif the lwip network interface structure * @param p the (IP) packet to 'send' * @param ipaddr the ip address to send the packet to (not used) * @return ERR_OK if the packet has been sent * ERR_MEM if the pbuf used to copy the packet couldn't be allocated */err_tnetif_loop_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr){ struct pbuf *r; err_t err; struct pbuf *last;#if LWIP_LOOPBACK_MAX_PBUFS u8_t clen = 0;#endif /* LWIP_LOOPBACK_MAX_PBUFS */ /* If we have a loopif, SNMP counters are adjusted for it, * if not they are adjusted for 'netif'. */#if LWIP_SNMP#if LWIP_HAVE_LOOPIF struct netif *stats_if = &loop_netif;#else /* LWIP_HAVE_LOOPIF */ struct netif *stats_if = netif;#endif /* LWIP_HAVE_LOOPIF */#endif /* LWIP_SNMP */ SYS_ARCH_DECL_PROTECT(lev); LWIP_UNUSED_ARG(ipaddr); /* Allocate a new pbuf */ r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); if (r == NULL) { LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); snmp_inc_ifoutdiscards(stats_if); return ERR_MEM; }#if LWIP_LOOPBACK_MAX_PBUFS clen = pbuf_clen(r); /* check for overflow or too many pbuf on queue */ if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) || ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) { pbuf_free(r); LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); snmp_inc_ifoutdiscards(stats_if); return ERR_MEM; } netif->loop_cnt_current += clen;#endif /* LWIP_LOOPBACK_MAX_PBUFS */ /* Copy the whole pbuf queue p into the single pbuf r */ if ((err = pbuf_copy(r, p)) != ERR_OK) { pbuf_free(r); LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); snmp_inc_ifoutdiscards(stats_if); return err; } /* Put the packet on a linked list which gets emptied through calling netif_poll(). */ /* let last point to the last pbuf in chain r */ for (last = r; last->next != NULL; last = last->next); SYS_ARCH_PROTECT(lev); if(netif->loop_first != NULL) { LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL); netif->loop_last->next = r; netif->loop_last = last; } else { netif->loop_first = r; netif->loop_last = last; } SYS_ARCH_UNPROTECT(lev); LINK_STATS_INC(link.xmit); snmp_add_ifoutoctets(stats_if, p->tot_len); snmp_inc_ifoutucastpkts(stats_if);#if LWIP_NETIF_LOOPBACK_MULTITHREADING /* For multithreading environment, schedule a call to netif_poll */ tcpip_callback((tcpip_callback_fn)netif_poll, netif);#endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */ return ERR_OK;}/** * Call netif_poll() in the main loop of your application. This is to prevent * reentering non-reentrant functions like tcp_input(). Packets passed to * netif_loop_output() are put on a list that is passed to netif->input() by * netif_poll(). */voidnetif_poll(struct netif *netif){ struct pbuf *in; /* If we have a loopif, SNMP counters are adjusted for it, * if not they are adjusted for 'netif'. */#if LWIP_SNMP#if LWIP_HAVE_LOOPIF struct netif *stats_if = &loop_netif;#else /* LWIP_HAVE_LOOPIF */ struct netif *stats_if = netif;#endif /* LWIP_HAVE_LOOPIF */#endif /* LWIP_SNMP */ SYS_ARCH_DECL_PROTECT(lev); do { /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */ SYS_ARCH_PROTECT(lev); in = netif->loop_first; if (in != NULL) { struct pbuf *in_end = in;#if LWIP_LOOPBACK_MAX_PBUFS u8_t clen = pbuf_clen(in); /* adjust the number of pbufs on queue */ LWIP_ASSERT("netif->loop_cnt_current underflow", ((netif->loop_cnt_current - clen) < netif->loop_cnt_current)); netif->loop_cnt_current -= clen;#endif /* LWIP_LOOPBACK_MAX_PBUFS */ while (in_end->len != in_end->tot_len) { LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL); in_end = in_end->next; } /* 'in_end' now points to the last pbuf from 'in' */ if (in_end == netif->loop_last) { /* this was the last pbuf in the list */ netif->loop_first = netif->loop_last = NULL; } else { /* pop the pbuf off the list */ netif->loop_first = in_end->next; LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL); } /* De-queue the pbuf from its successors on the 'loop_' list. */ in_end->next = NULL; } SYS_ARCH_UNPROTECT(lev); if (in != NULL) { LINK_STATS_INC(link.recv); snmp_add_ifinoctets(stats_if, in->tot_len); snmp_inc_ifinucastpkts(stats_if); /* loopback packets are always IP packets! */ if (ip_input(in, netif) != ERR_OK) { pbuf_free(in); } /* Don't reference the packet any more! */ in = NULL; } /* go on while there is a packet on the list */ } while (netif->loop_first != NULL);}#if !LWIP_NETIF_LOOPBACK_MULTITHREADING/** * Calls netif_poll() for every netif on the netif_list. */voidnetif_poll_all(void){ struct netif *netif = netif_list; /* loop through netifs */ while (netif != NULL) { netif_poll(netif); /* proceed to next network interface */ netif = netif->next; }}#endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */#endif /* ENABLE_LOOPBACK */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -