当前位置: 首页 > 工具软件 > CPIP > 使用案例 >

LWIP源码分析——ip.c

家弘业
2023-12-01

LWIP源码分析——ip.c

LWIP作为轻量级tcpip栈,ip协议的处理当然是最主要的任务,也是站在协议角度理解LWIP的入口。
ip.c的内容很少,因此先分析,代码放在最后
整体看,ip.c只是一个通用的接口,真正主体是ip4.c,ip6.c以及ip.h头文件定义的几个结构体。

1. 宏

    ip.c函数中用两个宏控制:LWIP_IPV4 LWIP_IPV6分别是控制ip栈走ipv4还是ipv6的协议
    首个位置lwip/opt.hLWIP所有宏定义及默认值的位置,基本上所有代码都会引用这个文件,这就使得在改文件中的更改可以控制整个协议栈的使用。
    如果#if LWIP_IPV4 || LWIP_IPV6也就是至少支持ipv4ipv6这个文件才刚开始起作用,否则在编译过程中整段代码都不被编译。
    随后,仅引用了两个头文件,定义了一个全局变量,又出现第二道门槛#if LWIP_IPV4 && LWIP_IPV6,意味着仅当同时支持v4v6才会调用下面的入口函数,否则…,根据经验我们猜想一定是在某处用宏定义的方式将下面的入口函数定义为特定版本的函数。

2. 代码分析

#include "lwip/opt.h"

opt.h为宏开关的文件,此处只是LWIP_IPV4,LWIP_IPV4两个宏的引用用到。(后续会单独分析,链接至此)

#if LWIP_IPV4 || LWIP_IPV6

开关(或)

struct ip_globals ip_data;

名字上看用途很明显,是一个全局ip结构体,至于存储的内容,分析ip.h时再分析

#if LWIP_IPV4 && LWIP_IPV6

这里用了一个&&的方式,意味着只有同时支持v4v6才往下走

const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;

这里这段便是需要ip_addr.h中定义的内容,这里又定义了一个结构体,直观看像是定义了一个本地地址,当前文件中也没有用到,先记下

char *ipaddr_ntoa(const ip_addr_t *addr)

这是ip地址转换的函数,将一个数字形式的ip转化为ASCII码类型,感觉更直接的方式就是转化为一个字符串
比如192:168:1:1192.168.1.1
目前用到最多的时候就是打印ip地址时候,相信用处不止于此,记下

char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)

注解中说的是同上一个函数ipaddr_ntoa作用相同,只不过是可重载?这个函数目前没接触过,学到了再补充

ipaddr_aton(const char *cp, ip_addr_t *addr)

这个函数的作用就是将ip字符串转化为一个数值化的ip地址,就是ipaddr_ntoa的逆过程

ip_input(struct pbuf *p, struct netif *inp)

这是真正走协议的函数,从下层链路层接收数据道ip层做对应的处理。

这部分基本就这些代码,内容不多但是有一些没看到如何使用的
另外这样分析感觉有点零散,下一节考虑改善一下

3. 源代码

/**
 * @file
 * Common IPv4 and IPv6 code
 *
 * @defgroup ip IP
 * @ingroup callbackstyle_api
 *
 * @defgroup ip4 IPv4
 * @ingroup ip
 *
 * @defgroup ip6 IPv6
 * @ingroup ip
 *
 * @defgroup ipaddr IP address handling
 * @ingroup infrastructure
 *
 * @defgroup ip4addr IPv4 only
 * @ingroup ipaddr
 *
 * @defgroup ip6addr IPv6 only
 * @ingroup ipaddr
 */

/*
 * 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 || LWIP_IPV6

#include "lwip/ip_addr.h"
#include "lwip/ip.h"

/** Global data for both IPv4 and IPv6 */
struct ip_globals ip_data;

#if LWIP_IPV4 && LWIP_IPV6

const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;

/**
 * @ingroup ipaddr
 * Convert numeric IP address (both versions) into ASCII representation.
 * returns ptr to static buffer; not reentrant!
 *
 * @param addr ip address in network order to convert
 * @return pointer to a global static (!) buffer that holds the ASCII
 *         representation of addr
 */
char *ipaddr_ntoa(const ip_addr_t *addr)
{
  if (addr == NULL) {
    return NULL;
  }
  if (IP_IS_V6(addr)) {
    return ip6addr_ntoa(ip_2_ip6(addr));
  } else {
    return ip4addr_ntoa(ip_2_ip4(addr));
  }
}

/**
 * @ingroup ipaddr
 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
 *
 * @param addr ip address in network order to convert
 * @param buf target buffer where the string is stored
 * @param buflen length of buf
 * @return either pointer to buf which now holds the ASCII
 *         representation of addr or NULL if buf was too small
 */
char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
{
  if (addr == NULL) {
    return NULL;
  }
  if (IP_IS_V6(addr)) {
    return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen);
  } else {
    return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen);
  }
}

/**
 * @ingroup ipaddr
 * Convert IP address string (both versions) to numeric.
 * The version is auto-detected from the string.
 *
 * @param cp IP address string to convert
 * @param addr conversion result is stored here
 * @return 1 on success, 0 on error
 */
int
ipaddr_aton(const char *cp, ip_addr_t *addr)
{
  if (cp != NULL) {
    const char *c;
    for (c = cp; *c != 0; c++) {
      if (*c == ':') {
        /* contains a colon: IPv6 address */
        if (addr) {
          IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6);
        }
        return ip6addr_aton(cp, ip_2_ip6(addr));
      } else if (*c == '.') {
        /* contains a dot: IPv4 address */
        break;
      }
    }
    /* call ip4addr_aton as fallback or if IPv4 was found */
    if (addr) {
      IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4);
    }
    return ip4addr_aton(cp, ip_2_ip4(addr));
  }
  return 0;
}

/**
 * @ingroup lwip_nosys
 * If both IP versions are enabled, this function can dispatch packets to the correct one.
 * Don't call directly, pass to netif_add() and call netif->input().
 */
err_t
ip_input(struct pbuf *p, struct netif *inp)
{
  if (p != NULL) {
    if (IP_HDR_GET_VERSION(p->payload) == 6) {
      return ip6_input(p, inp);
    }
    return ip4_input(p, inp);
  }
  return ERR_VAL;
}

#endif /* LWIP_IPV4 && LWIP_IPV6 */

#endif /* LWIP_IPV4 || LWIP_IPV6 */

 类似资料: