LWIP源码分析——ip4.c

ipv4是IP栈中重要的一部分,实现功能使用了上千行代码,分析起来可能会稍显复杂,这部分采用的分析的思路是,重点思想总结部分放在前面,剩下的结合代码穿插分析

1.ipv4的实现

2.ipv4源码分析

/*** @file* This is the IPv4 layer implementation for incoming and outgoing IP traffic.** @see ip_frag.c**/

提示我们要看还要看ip_frag.c

/** Copyright (c) 2001-2004 Swedish Institute of Computer Science.* All rights reserved.** Redistribution and use in source and binary forms, with or without modification,* are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,*    this list of conditions and the following disclaimer.* 2. Redistributions in binary form must reproduce the above copyright notice,*    this list of conditions and the following disclaimer in the documentation*    and/or other materials provided with the distribution.* 3. The name of the author may not be used to endorse or promote products*    derived from this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY* OF SUCH DAMAGE.** This file is part of the lwIP TCP/IP stack.** Author: Adam Dunkels <adam@sics.se>**/#include "lwip/opt.h"#if LWIP_IPV4#include "lwip/ip.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/ip4_frag.h"
#include "lwip/inet_chksum.h"
#include "lwip/netif.h"
#include "lwip/icmp.h"
#include "lwip/igmp.h"
#include "lwip/priv/raw_priv.h"
#include "lwip/udp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/autoip.h"
#include "lwip/stats.h"
#include "lwip/prot/iana.h"#include <string.h>

== 分析 start ==

/***k&a*LWIP_HOOK_FILENAME*钩子机制需要的头文件 path/to/my/lwip_hooks.h*涉及到使用的位置分析*除此外,钩子机制是LWIP中引进的一种高级的用法,可以单独开一篇分析*/

== 分析 end==

#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif

== 分析 start==

/***k&a*CHECK_SUM功能*LWIP_INLINE_IP_CHKSUM 如果需要调用额外的函数来生成IP校验和(与动态计算相反),则将此设置为0*这句话是什么意思呢,我们往下看当分别设置为0和1的时候校验和的计算方式有什么不同*LWIP_CHECKSUM_CTRL_PER_NETIF 根据netif开关校验和。注意:如果启用,CHECKSUM_GEN_\*和CHECKSUM_CHECK_\*定义必须启用!*这句的加入稍显复杂,我们先假定该值是0(例子中一般也是0),这样不需要管CHECKSUM_GEN_*,CHECKSUM_CHECK_**这使得#define LWIP_INLINE_IP_CHKSUM   1生效,*实际中在ip4.c中,仅有开头这几处涉及到LWIP_INLINE_IP_CHKSUM,看来其作用是控制CHECKSUM_GEN_IP_INLINE是否有效*这里涉及到CHECKSUM_GEN_IP,=1时在软件中为传出的IP数据包生成校验和*一些案例默认的配置中设置为1,即走软件校验路线*综上 #define CHECKSUM_GEN_IP_INLINE  1生效*我们看一下在实际代码中CHECKSUM_GEN_IP_INLINE如何生效*最简单的方法:CTRL+F搜索,很容易找到,CHECKSUM_GEN_IP_INLINE=1生效后*定义了变量chk_sum,然后应对整个ip头校验,也就是通过校验规则使chk_sum++*这块不再深入,到该位置再说*再捋一下,check_sum的计算是在ip4_output_if_opt_src也就是组ip包的时候,*最后通过iphdr->_chksum = (u16_t)chk_sum;将校验和存在iphdr这个ip结构体中*我们猜想iphdr = (struct ip_hdr *)p->payload;,默认chk_sum会设置为0,否则一定会出现问题(这个猜想当涉及到上层时再做分析)*回来再看Set this to 0 in the rare case of wanting to call an extra function to*generate the IP checksum (in contrast to calculating it on-the-fly)*即=0极少情况下通过额外方式生成校验和,=1动态计算校验和*=1动态计算即通过chk_sum++这种方式计算*=0通过额外方式,目前猜想这里就是通过令校验和为0*/

== 分析 end ==


/** Set this to 0 in the rare case of wanting to call an extra function to* generate the IP checksum (in contrast to calculating it on-the-fly). */
#ifndef LWIP_INLINE_IP_CHKSUM
#if LWIP_CHECKSUM_CTRL_PER_NETIF
#define LWIP_INLINE_IP_CHKSUM   0
#else /* LWIP_CHECKSUM_CTRL_PER_NETIF */
#define LWIP_INLINE_IP_CHKSUM   1
#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
#endif#if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
#define CHECKSUM_GEN_IP_INLINE  1
#else
#define CHECKSUM_GEN_IP_INLINE  0
#endif#if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
#define IP_ACCEPT_LINK_LAYER_ADDRESSING 1/** Some defines for DHCP to let link-layer-addressed packets through while the* netif is down.* To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port)* to return 1 if the port is accepted and 0 if the port is not accepted.*/
#if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
/* accept DHCP client port and custom port */
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) \|| (LWIP_IP_ACCEPT_UDP_PORT(port)))
#elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
/* accept custom port only */
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
#else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
/* accept DHCP client port only */
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT))
#endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */#else /* LWIP_DHCP */
#define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
#endif /* LWIP_DHCP *//** The IP header ID of the next outgoing IP packet */
static u16_t ip_id;#if LWIP_MULTICAST_TX_OPTIONS
/** The default netif used for multicast */
static struct netif *ip4_default_multicast_netif;/*** @ingroup ip4* Set a default netif for IPv4 multicast. */
void
ip4_set_default_multicast_netif(struct netif *default_multicast_netif)
{ip4_default_multicast_netif = default_multicast_netif;
}
#endif /* LWIP_MULTICAST_TX_OPTIONS */#ifdef LWIP_HOOK_IP4_ROUTE_SRC
/*** Source based IPv4 routing must be fully implemented in* LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides the parameters.*/
struct netif *
ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest)
{if (src != NULL) {/* when src==NULL, the hook is called from ip4_route(dest) */struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(src, dest);if (netif != NULL) {return netif;}}return ip4_route(dest);
}
#endif /* LWIP_HOOK_IP4_ROUTE_SRC *//*** Finds the appropriate network interface for a given IP address. It* searches the list of network interfaces linearly. A match is found* if the masked IP address of the network interface equals the masked* IP address given to the function.** @param dest the destination IP address for which to find the route* @return the netif on which to send to reach dest*/
struct netif *
ip4_route(const ip4_addr_t *dest)
{#if !LWIP_SINGLE_NETIFstruct netif *netif;LWIP_ASSERT_CORE_LOCKED();#if LWIP_MULTICAST_TX_OPTIONS/* Use administratively selected interface for multicast by default */if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {return ip4_default_multicast_netif;}
#endif /* LWIP_MULTICAST_TX_OPTIONS *//* bug #54569: in case LWIP_SINGLE_NETIF=1 and LWIP_DEBUGF() disabled, the following loop is optimized away */LWIP_UNUSED_ARG(dest);/* iterate through netifs */NETIF_FOREACH(netif) {/* is the netif up, does it have a link and a valid address? */if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) {/* network mask matches? */if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) {/* return netif on which to forward IP packet */return netif;}/* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_cmp(dest, netif_ip4_gw(netif))) {/* return netif on which to forward IP packet */return netif;}}}#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF/* loopif is disabled, looopback traffic is passed through any netif */if (ip4_addr_isloopback(dest)) {/* don't check for link on loopback traffic */if (netif_default != NULL && netif_is_up(netif_default)) {return netif_default;}/* default netif is not up, just use any netif for loopback traffic */NETIF_FOREACH(netif) {if (netif_is_up(netif)) {return netif;}}return NULL;}
#endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */#ifdef LWIP_HOOK_IP4_ROUTE_SRCnetif = LWIP_HOOK_IP4_ROUTE_SRC(NULL, dest);if (netif != NULL) {return netif;}
#elif defined(LWIP_HOOK_IP4_ROUTE)netif = LWIP_HOOK_IP4_ROUTE(dest);if (netif != NULL) {return netif;}
#endif
#endif /* !LWIP_SINGLE_NETIF */if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) ||ip4_addr_isany_val(*netif_ip4_addr(netif_default)) || ip4_addr_isloopback(dest)) {/* No matching netif found and default netif is not usable.If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));IP_STATS_INC(ip.rterr);MIB2_STATS_INC(mib2.ipoutnoroutes);return NULL;}return netif_default;
}#if IP_FORWARD
/*** Determine whether an IP address is in a reserved set of addresses* that may not be forwarded, or whether datagrams to that destination* may be forwarded.* @param p the packet to forward* @return 1: can forward 0: discard*/
static int
ip4_canforward(struct pbuf *p)
{u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr()));#ifdef LWIP_HOOK_IP4_CANFORWARDint ret = LWIP_HOOK_IP4_CANFORWARD(p, addr);if (ret >= 0) {return ret;}
#endif /* LWIP_HOOK_IP4_CANFORWARD */if (p->flags & PBUF_FLAG_LLBCAST) {/* don't route link-layer broadcasts */return 0;}if ((p->flags & PBUF_FLAG_LLMCAST) || IP_MULTICAST(addr)) {/* don't route link-layer multicasts (use LWIP_HOOK_IP4_CANFORWARD instead) */return 0;}if (IP_EXPERIMENTAL(addr)) {return 0;}if (IP_CLASSA(addr)) {u32_t net = addr & IP_CLASSA_NET;if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {/* don't route loopback packets */return 0;}}return 1;
}/*** Forwards an IP packet. It finds an appropriate route for the* packet, decrements the TTL value of the packet, adjusts the* checksum and outputs the packet on the appropriate interface.** @param p the packet to forward (p->payload points to IP header)* @param iphdr the IP header of the input packet* @param inp the netif on which this packet was received*/
static void
ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
{struct netif *netif;PERF_START;LWIP_UNUSED_ARG(inp);if (!ip4_canforward(p)) {goto return_noroute;}/* RFC3927 2.7: do not forward link-local addresses */if (ip4_addr_islinklocal(ip4_current_dest_addr())) {LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));goto return_noroute;}/* Find network interface where to forward this IP packet to. */netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr());if (netif == NULL) {LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));/* @todo: send ICMP_DUR_NET? */goto return_noroute;}
#if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF/* Do not forward packets onto the same network interface on which* they arrived. */if (netif == inp) {LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n"));goto return_noroute;}
#endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF *//* decrement TTL */IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);/* send ICMP if TTL == 0 */if (IPH_TTL(iphdr) == 0) {MIB2_STATS_INC(mib2.ipinhdrerrors);
#if LWIP_ICMP/* Don't send ICMP messages in response to ICMP messages */if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {icmp_time_exceeded(p, ICMP_TE_TTL);}
#endif /* LWIP_ICMP */return;}/* Incrementally update the IP checksum. */if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1));} else {IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100)));}LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));IP_STATS_INC(ip.fw);MIB2_STATS_INC(mib2.ipforwdatagrams);IP_STATS_INC(ip.xmit);PERF_STOP("ip4_forward");/* don't fragment if interface has mtu set to 0 [loopif] */if (netif->mtu && (p->tot_len > netif->mtu)) {if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {#if IP_FRAGip4_frag(p, netif, ip4_current_dest_addr());
#else /* IP_FRAG *//* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
#endif /* IP_FRAG */} else {#if LWIP_ICMP/* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */icmp_dest_unreach(p, ICMP_DUR_FRAG);
#endif /* LWIP_ICMP */}return;}/* transmit pbuf on chosen interface */netif->output(netif, p, ip4_current_dest_addr());return;
return_noroute:MIB2_STATS_INC(mib2.ipoutnoroutes);
}
#endif /* IP_FORWARD *//** Return true if the current input packet should be accepted on this netif */
static int
ip4_input_accept(struct netif *netif)
{LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n",ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));/* interface is up and configured? */if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {/* unicast to this interface address? */if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||/* or broadcast on this interface network address? */ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF|| (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
#endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */) {LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n",netif->name[0], netif->name[1]));/* accept on this netif */return 1;}
#if LWIP_AUTOIP/* connections to link-local addresses must persist after changingthe netif's address (RFC3927 ch. 1.9) */if (autoip_accept_packet(netif, ip4_current_dest_addr())) {LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n",netif->name[0], netif->name[1]));/* accept on this netif */return 1;}
#endif /* LWIP_AUTOIP */}return 0;
}/*** This function is called by the network interface device driver when* an IP packet is received. The function does the basic checks of the* IP header such as packet size being at least larger than the header* size etc. If the packet was not destined for us, the packet is* forwarded (using ip_forward). The IP checksum is always checked.** Finally, the packet is sent to the upper layer protocol input function.** @param p the received IP packet (p->payload points to IP header)* @param inp the netif on which this packet was received* @return ERR_OK if the packet was processed (could return ERR_* if it wasn't*         processed, but currently always returns ERR_OK)*/
err_t
ip4_input(struct pbuf *p, struct netif *inp)
{const struct ip_hdr *iphdr;struct netif *netif;u16_t iphdr_hlen;u16_t iphdr_len;
#if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMPint check_ip_src = 1;
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
#if LWIP_RAWraw_input_state_t raw_status;
#endif /* LWIP_RAW */LWIP_ASSERT_CORE_LOCKED();IP_STATS_INC(ip.recv);MIB2_STATS_INC(mib2.ipinreceives);/* identify the IP header */iphdr = (struct ip_hdr *)p->payload;if (IPH_V(iphdr) != 4) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));ip4_debug_print(p);pbuf_free(p);IP_STATS_INC(ip.err);IP_STATS_INC(ip.drop);MIB2_STATS_INC(mib2.ipinhdrerrors);return ERR_OK;}#ifdef LWIP_HOOK_IP4_INPUTif (LWIP_HOOK_IP4_INPUT(p, inp)) {/* the packet has been eaten */return ERR_OK;}
#endif/* obtain IP header length in bytes */iphdr_hlen = IPH_HL_BYTES(iphdr);/* obtain ip length in bytes */iphdr_len = lwip_ntohs(IPH_LEN(iphdr));/* Trim pbuf. This is especially required for packets < 60 bytes. */if (iphdr_len < p->tot_len) {pbuf_realloc(p, iphdr_len);}/* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {if (iphdr_hlen < IP_HLEN) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));}if (iphdr_hlen > p->len) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",iphdr_hlen, p->len));}if (iphdr_len > p->tot_len) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",iphdr_len, p->tot_len));}/* free (drop) packet pbufs */pbuf_free(p);IP_STATS_INC(ip.lenerr);IP_STATS_INC(ip.drop);MIB2_STATS_INC(mib2.ipindiscards);return ERR_OK;}/* verify checksum */
#if CHECKSUM_CHECK_IPIF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {if (inet_chksum(iphdr, iphdr_hlen) != 0) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));ip4_debug_print(p);pbuf_free(p);IP_STATS_INC(ip.chkerr);IP_STATS_INC(ip.drop);MIB2_STATS_INC(mib2.ipinhdrerrors);return ERR_OK;}}
#endif/* copy IP addresses to aligned ip_addr_t */ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);/* match packet against an interface, i.e. is this packet for us? */if (ip4_addr_ismulticast(ip4_current_dest_addr())) {#if LWIP_IGMPif ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {/* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */ip4_addr_t allsystems;IP4_ADDR(&allsystems, 224, 0, 0, 1);if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&ip4_addr_isany(ip4_current_src_addr())) {check_ip_src = 0;}netif = inp;} else {netif = NULL;}
#else /* LWIP_IGMP */if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {netif = inp;} else {netif = NULL;}
#endif /* LWIP_IGMP */} else {/* start trying with inp. if that's not acceptable, start walking thelist of configured netifs. */if (ip4_input_accept(inp)) {netif = inp;} else {netif = NULL;
#if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF/* Packets sent to the loopback address must not be accepted on an* interface that does not have the loopback address assigned to it,* unless a non-loopback interface is used for loopback traffic. */if (!ip4_addr_isloopback(ip4_current_dest_addr()))
#endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */{#if !LWIP_SINGLE_NETIFNETIF_FOREACH(netif) {if (netif == inp) {/* we checked that before already */continue;}if (ip4_input_accept(netif)) {break;}}
#endif /* !LWIP_SINGLE_NETIF */}}}#if IP_ACCEPT_LINK_LAYER_ADDRESSING/* Pass DHCP messages regardless of destination address. DHCP traffic is addressed* using link layer addressing (such as Ethernet MAC) so we must not filter on IP.* According to RFC 1542 section 3.1.1, referred by RFC 2131).** If you want to accept private broadcast communication while a netif is down,* define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:** #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))*/if (netif == NULL) {/* remote port is DHCP server? */if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen);LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",lwip_ntohs(udphdr->dest)));if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));netif = inp;check_ip_src = 0;}}}
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING *//* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
#if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSINGif (check_ip_src
#if IP_ACCEPT_LINK_LAYER_ADDRESSING/* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */&& !ip4_addr_isany_val(*ip4_current_src_addr())
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */)
#endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */{if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||(ip4_addr_ismulticast(ip4_current_src_addr()))) {/* packet source is not valid */LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));/* free (drop) packet pbufs */pbuf_free(p);IP_STATS_INC(ip.drop);MIB2_STATS_INC(mib2.ipinaddrerrors);MIB2_STATS_INC(mib2.ipindiscards);return ERR_OK;}}/* packet not for us? */if (netif == NULL) {/* packet not for us, route or discard */LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
#if IP_FORWARD/* non-broadcast packet? */if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {/* try to forward IP packet on (other) interfaces */ip4_forward(p, (struct ip_hdr *)p->payload, inp);} else
#endif /* IP_FORWARD */{IP_STATS_INC(ip.drop);MIB2_STATS_INC(mib2.ipinaddrerrors);MIB2_STATS_INC(mib2.ipindiscards);}pbuf_free(p);return ERR_OK;}/* packet consists of multiple fragments? */if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {#if IP_REASSEMBLY /* packet fragment reassembly code present? */LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip4_reass()\n",lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) * 8)));/* reassemble the packet*/p = ip4_reass(p);/* packet not fully reassembled yet? */if (p == NULL) {return ERR_OK;}iphdr = (const struct ip_hdr *)p->payload;
#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */pbuf_free(p);LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",lwip_ntohs(IPH_OFFSET(iphdr))));IP_STATS_INC(ip.opterr);IP_STATS_INC(ip.drop);/* unsupported protocol feature */MIB2_STATS_INC(mib2.ipinunknownprotos);return ERR_OK;
#endif /* IP_REASSEMBLY */}#if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */#if LWIP_IGMP/* there is an extra "router alert" option in IGMP messages which we allow for but do not police */if ((iphdr_hlen > IP_HLEN) &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {#elseif (iphdr_hlen > IP_HLEN) {#endif /* LWIP_IGMP */LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));pbuf_free(p);IP_STATS_INC(ip.opterr);IP_STATS_INC(ip.drop);/* unsupported protocol feature */MIB2_STATS_INC(mib2.ipinunknownprotos);return ERR_OK;}
#endif /* IP_OPTIONS_ALLOWED == 0 *//* send to upper layers */LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));ip4_debug_print(p);LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));ip_data.current_netif = netif;ip_data.current_input_netif = inp;ip_data.current_ip4_header = iphdr;ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr);#if LWIP_RAW/* raw input did not eat the packet? */raw_status = raw_input(p, inp);if (raw_status != RAW_INPUT_EATEN)
#endif /* LWIP_RAW */{pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */switch (IPH_PROTO(iphdr)) {#if LWIP_UDPcase IP_PROTO_UDP:
#if LWIP_UDPLITEcase IP_PROTO_UDPLITE:
#endif /* LWIP_UDPLITE */MIB2_STATS_INC(mib2.ipindelivers);udp_input(p, inp);break;
#endif /* LWIP_UDP */
#if LWIP_TCPcase IP_PROTO_TCP:MIB2_STATS_INC(mib2.ipindelivers);tcp_input(p, inp);break;
#endif /* LWIP_TCP */
#if LWIP_ICMPcase IP_PROTO_ICMP:MIB2_STATS_INC(mib2.ipindelivers);icmp_input(p, inp);break;
#endif /* LWIP_ICMP */
#if LWIP_IGMPcase IP_PROTO_IGMP:igmp_input(p, inp, ip4_current_dest_addr());break;
#endif /* LWIP_IGMP */default:
#if LWIP_RAWif (raw_status == RAW_INPUT_DELIVERED) {MIB2_STATS_INC(mib2.ipindelivers);} else
#endif /* LWIP_RAW */{#if LWIP_ICMP/* send ICMP destination protocol unreachable unless is was a broadcast */if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&!ip4_addr_ismulticast(ip4_current_dest_addr())) {pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */icmp_dest_unreach(p, ICMP_DUR_PROTO);}
#endif /* LWIP_ICMP */LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));IP_STATS_INC(ip.proterr);IP_STATS_INC(ip.drop);MIB2_STATS_INC(mib2.ipinunknownprotos);}pbuf_free(p);break;}}/* @todo: this is not really necessary... */ip_data.current_netif = NULL;ip_data.current_input_netif = NULL;ip_data.current_ip4_header = NULL;ip_data.current_ip_header_tot_len = 0;ip4_addr_set_any(ip4_current_src_addr());ip4_addr_set_any(ip4_current_dest_addr());return ERR_OK;
}/*** Sends an IP packet on a network interface. This function constructs* the IP header and calculates the IP header checksum. If the source* IP address is NULL, the IP address of the outgoing network* interface is filled in as source address.* If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already* include an IP header and p->payload points to it instead of the data.** @param p the packet to send (p->payload points to the data, e.g. nextprotocol header; if dest == LWIP_IP_HDRINCL, p already includes anIP header and p->payload points to that IP header)* @param src the source IP address to send from (if src == IP4_ADDR_ANY, the*         IP  address of the netif used to send is used as source address)* @param dest the destination IP address to send the packet to* @param ttl the TTL value to be set in the IP header* @param tos the TOS value to be set in the IP header* @param proto the PROTOCOL to be set in the IP header* @param netif the netif on which to send this packet* @return ERR_OK if the packet was sent OK*         ERR_BUF if p doesn't have enough space for IP/LINK headers*         returns errors returned by netif->output** @note ip_id: RFC791 "some host may be able to simply use*  unique identifiers independent of destination"*/
err_t
ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,u8_t ttl, u8_t tos,u8_t proto, struct netif *netif)
{#if IP_OPTIONS_SENDreturn ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
}/*** Same as ip_output_if() but with the possibility to include IP options:** @ param ip_options pointer to the IP options, copied into the IP header* @ param optlen length of ip_options*/
err_t
ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,u16_t optlen)
{#endif /* IP_OPTIONS_SEND */const ip4_addr_t *src_used = src;if (dest != LWIP_IP_HDRINCL) {if (ip4_addr_isany(src)) {src_used = netif_ip4_addr(netif);}}#if IP_OPTIONS_SENDreturn ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,ip_options, optlen);
#else /* IP_OPTIONS_SEND */return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
#endif /* IP_OPTIONS_SEND */
}/*** Same as ip_output_if() but 'src' address is not replaced by netif address* when it is 'any'.*/
err_t
ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,u8_t ttl, u8_t tos,u8_t proto, struct netif *netif)
{#if IP_OPTIONS_SENDreturn ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
}/*** Same as ip_output_if_opt() but 'src' address is not replaced by netif address* when it is 'any'.*/
err_t
ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,u16_t optlen)
{#endif /* IP_OPTIONS_SEND */struct ip_hdr *iphdr;ip4_addr_t dest_addr;
#if CHECKSUM_GEN_IP_INLINEu32_t chk_sum = 0;
#endif /* CHECKSUM_GEN_IP_INLINE */LWIP_ASSERT_CORE_LOCKED();LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);MIB2_STATS_INC(mib2.ipoutrequests);/* Should the IP header be generated or is it already included in p? */if (dest != LWIP_IP_HDRINCL) {u16_t ip_hlen = IP_HLEN;
#if IP_OPTIONS_SENDu16_t optlen_aligned = 0;if (optlen != 0) {#if CHECKSUM_GEN_IP_INLINEint i;
#endif /* CHECKSUM_GEN_IP_INLINE */if (optlen > (IP_HLEN_MAX - IP_HLEN)) {/* optlen too long */LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n"));IP_STATS_INC(ip.err);MIB2_STATS_INC(mib2.ipoutdiscards);return ERR_VAL;}/* round up to a multiple of 4 */optlen_aligned = (u16_t)((optlen + 3) & ~3);ip_hlen = (u16_t)(ip_hlen + optlen_aligned);/* First write in the IP options */if (pbuf_add_header(p, optlen_aligned)) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));IP_STATS_INC(ip.err);MIB2_STATS_INC(mib2.ipoutdiscards);return ERR_BUF;}MEMCPY(p->payload, ip_options, optlen);if (optlen < optlen_aligned) {/* zero the remaining bytes */memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen));}
#if CHECKSUM_GEN_IP_INLINEfor (i = 0; i < optlen_aligned / 2; i++) {chk_sum += ((u16_t *)p->payload)[i];}
#endif /* CHECKSUM_GEN_IP_INLINE */}
#endif /* IP_OPTIONS_SEND *//* generate IP header */if (pbuf_add_header(p, IP_HLEN)) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));IP_STATS_INC(ip.err);MIB2_STATS_INC(mib2.ipoutdiscards);return ERR_BUF;}iphdr = (struct ip_hdr *)p->payload;LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",(p->len >= sizeof(struct ip_hdr)));IPH_TTL_SET(iphdr, ttl);IPH_PROTO_SET(iphdr, proto);
#if CHECKSUM_GEN_IP_INLINEchk_sum += PP_NTOHS(proto | (ttl << 8));
#endif /* CHECKSUM_GEN_IP_INLINE *//* dest cannot be NULL here */ip4_addr_copy(iphdr->dest, *dest);
#if CHECKSUM_GEN_IP_INLINEchk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
#endif /* CHECKSUM_GEN_IP_INLINE */IPH_VHL_SET(iphdr, 4, ip_hlen / 4);IPH_TOS_SET(iphdr, tos);
#if CHECKSUM_GEN_IP_INLINEchk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
#endif /* CHECKSUM_GEN_IP_INLINE */IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
#if CHECKSUM_GEN_IP_INLINEchk_sum += iphdr->_len;
#endif /* CHECKSUM_GEN_IP_INLINE */IPH_OFFSET_SET(iphdr, 0);IPH_ID_SET(iphdr, lwip_htons(ip_id));
#if CHECKSUM_GEN_IP_INLINEchk_sum += iphdr->_id;
#endif /* CHECKSUM_GEN_IP_INLINE */++ip_id;if (src == NULL) {ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);} else {/* src cannot be NULL here */ip4_addr_copy(iphdr->src, *src);}#if CHECKSUM_GEN_IP_INLINEchk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);chk_sum = (chk_sum >> 16) + chk_sum;chk_sum = ~chk_sum;IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {iphdr->_chksum = (u16_t)chk_sum; /* network order */}
#if LWIP_CHECKSUM_CTRL_PER_NETIFelse {IPH_CHKSUM_SET(iphdr, 0);}
#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
#else /* CHECKSUM_GEN_IP_INLINE */IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IPIF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));}
#endif /* CHECKSUM_GEN_IP */
#endif /* CHECKSUM_GEN_IP_INLINE */} else {/* IP header already included in p */if (p->len < IP_HLEN) {LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n"));IP_STATS_INC(ip.err);MIB2_STATS_INC(mib2.ipoutdiscards);return ERR_BUF;}iphdr = (struct ip_hdr *)p->payload;ip4_addr_copy(dest_addr, iphdr->dest);dest = &dest_addr;}IP_STATS_INC(ip.xmit);LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));ip4_debug_print(p);#if ENABLE_LOOPBACKif (ip4_addr_cmp(dest, netif_ip4_addr(netif))
#if !LWIP_HAVE_LOOPIF|| ip4_addr_isloopback(dest)
#endif /* !LWIP_HAVE_LOOPIF */) {/* Packet to self, enqueue it for loopback */LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));return netif_loop_output(netif, p);}
#if LWIP_MULTICAST_TX_OPTIONSif ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {netif_loop_output(netif, p);}
#endif /* LWIP_MULTICAST_TX_OPTIONS */
#endif /* ENABLE_LOOPBACK */
#if IP_FRAG/* don't fragment if interface has mtu set to 0 [loopif] */if (netif->mtu && (p->tot_len > netif->mtu)) {return ip4_frag(p, netif, dest);}
#endif /* IP_FRAG */LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));return netif->output(netif, p, dest);
}/*** Simple interface to ip_output_if. It finds the outgoing network* interface and calls upon ip_output_if to do the actual work.** @param p the packet to send (p->payload points to the data, e.g. nextprotocol header; if dest == LWIP_IP_HDRINCL, p already includes anIP header and p->payload points to that IP header)* @param src the source IP address to send from (if src == IP4_ADDR_ANY, the*         IP  address of the netif used to send is used as source address)* @param dest the destination IP address to send the packet to* @param ttl the TTL value to be set in the IP header* @param tos the TOS value to be set in the IP header* @param proto the PROTOCOL to be set in the IP header** @return ERR_RTE if no route is found*         see ip_output_if() for more return values*/
err_t
ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,u8_t ttl, u8_t tos, u8_t proto)
{struct netif *netif;LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);if ((netif = ip4_route_src(src, dest)) == NULL) {LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));IP_STATS_INC(ip.rterr);return ERR_RTE;}return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
}#if LWIP_NETIF_USE_HINTS
/** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint*  before calling ip_output_if.** @param p the packet to send (p->payload points to the data, e.g. nextprotocol header; if dest == LWIP_IP_HDRINCL, p already includes anIP header and p->payload points to that IP header)* @param src the source IP address to send from (if src == IP4_ADDR_ANY, the*         IP  address of the netif used to send is used as source address)* @param dest the destination IP address to send the packet to* @param ttl the TTL value to be set in the IP header* @param tos the TOS value to be set in the IP header* @param proto the PROTOCOL to be set in the IP header* @param netif_hint netif output hint pointer set to netif->hint before*        calling ip_output_if()** @return ERR_RTE if no route is found*         see ip_output_if() for more return values*/
err_t
ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
{struct netif *netif;err_t err;LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);if ((netif = ip4_route_src(src, dest)) == NULL) {LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));IP_STATS_INC(ip.rterr);return ERR_RTE;}NETIF_SET_HINTS(netif, netif_hint);err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);NETIF_RESET_HINTS(netif);return err;
}
#endif /* LWIP_NETIF_USE_HINTS*/#if IP_DEBUG
/* Print an IP header by using LWIP_DEBUGF* @param p an IP packet, p->payload pointing to the IP header*/
void
ip4_debug_print(struct pbuf *p)
{struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",(u16_t)IPH_V(iphdr),(u16_t)IPH_HL(iphdr),(u16_t)IPH_TOS(iphdr),lwip_ntohs(IPH_LEN(iphdr))));LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",lwip_ntohs(IPH_ID(iphdr)),(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",(u16_t)IPH_TTL(iphdr),(u16_t)IPH_PROTO(iphdr),lwip_ntohs(IPH_CHKSUM(iphdr))));LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",ip4_addr1_16_val(iphdr->src),ip4_addr2_16_val(iphdr->src),ip4_addr3_16_val(iphdr->src),ip4_addr4_16_val(iphdr->src)));LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",ip4_addr1_16_val(iphdr->dest),ip4_addr2_16_val(iphdr->dest),ip4_addr3_16_val(iphdr->dest),ip4_addr4_16_val(iphdr->dest)));LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
}
#endif /* IP_DEBUG */#endif /* LWIP_IPV4 */

LWIP源码分析——ip4.c相关推荐

  1. LwIP源码分析(2):tcpip_init和tcpip_thread函数分析

    环境:FreeRTOS & LwIP 2.2.0 文章中的所有参数检测的断言代码都删除以使代码更清晰 LwIP通过调用tcpip_init来初始化TCPIP协议栈,函数如下所示,函数中代码的含 ...

  2. lwip源码分析 之 DHCP协议实现(一)

    文章目录 一,dhcp协议简介 二,源码解析 2.1 dhcp结构体 2.2 开始广播 2.3 回调接口 a,发送请求 b,等待ack 2.4 其他情况 一,dhcp协议简介 dhcp协议是动态主机配 ...

  3. LwIP源码分析(3):内存堆和内存池代码详解

    文章目录 1 内存堆 1.1 mem_init 1.2 mem_malloc 1.3 mem_free 2 内存池 2.1 memp_init 2.2 memp_malloc 2.3 memp_fre ...

  4. LWIP源码结构分析

    LWIP(Light weight internet protocol)主要模块包括:配置模块,初始化模块,NetIf模块,mem(memp)模块,netarp模块,ip模块,udp模块,icmp模 ...

  5. live555 源码分析:子会话 SDP 行生成

    如我们在前文 live555 源码分析:ServerMediaSession 中看到的,H264VideoFileServerMediaSubsession 的继承层次体系如下图: 在这个继承层次体系 ...

  6. live555 源码分析: DESCRIBE 的处理

    前面在 live555 源码分析:RTSPServer 中分析了 live555 中处理 RTSP 请求的大体流程,并分析了处理起来没有那么复杂的一些方法,如 OPTIONS,GET_PARAMETE ...

  7. live555源码分析(四)RTSPServer分析

    live555源码分析系列 live555源码分析(一)live555初体验 live555源码分析(二)基本组件上 live555源码分析(三)基本组件下 live555源码分析(四)RTSPSer ...

  8. belle-sip源码分析

    belle-sip源码分析 一. 源码下载: http://www.linphone.org/technical-corner/belle-sip/downloads 安装 sudo apt-get ...

  9. live555 源码分析:RTSPServer

    live555 使用 RTSP/RTP/RTCP 协议来实现流媒体的传输,其中使用 RTSP 来建立流媒体会话,并对流媒体会话进行控制.在 live555 中,通过类 RTSPServerSuppor ...

最新文章

  1. 高中生也能读懂的Docker入门教程
  2. 【opencv+机器学习】error C3083: 'ml': the symbol to the left of a '::' must be a type问题原因
  3. shell去掉字符两端字符_Shell脚本8种字符串截取方法总结
  4. 物理内存和虚拟内存的区别 及 什么是虚拟地址空间
  5. Java 8:从PermGen到元空间
  6. Windows Phone 模拟器 (WPR Alpha 0.0.1 WP7/8模拟器) XAP XNA文件使用教程
  7. Tomcat免安装版+Eclipse配置
  8. Silverlight载入动画(简易)
  9. 计算机word2010试题,对口升学计算机Word2010练习题
  10. windows搭建wordpress方法-windows搭建wordpress教程
  11. 城市大脑与超级智能城市建设规范研究
  12. NOIP2018游记
  13. 使用 IDEA 翻译插件
  14. 魅族设置语音录音服务器,魅族手机留言录音功能使用方法介绍
  15. 色彩RGBA值写作代码
  16. 分享一款好用的微信相册小程序《群相册大师》
  17. matlab如何进行批量计算,什么是matlab中逐列相关的快速计算方法
  18. 5.3 用户注册与登录
  19. word2vec中数学原理详解以及原理思考
  20. 经济订货批量模型(EOQ)

热门文章

  1. 鸟哥的Linux私房菜(服务器)- 第十一章、远程联机服务器SSH / XDMCP / VNC / RDP
  2. Android虚拟sdcard
  3. 先验概率、后验概率、似然函数概念的区分
  4. java输出格林威治时间_Java之格林威治时间格式转换成北京时间格式
  5. 移动互联网安全技术研究
  6. 小白学 Python(1):开篇
  7. 这些东西适合做什么?
  8. 【重要】 性能测试设计能力解决方案
  9. 批量提取CAD中文字
  10. Linova and Kingdom