netif_add 网口 添加/修改/配置 源码分析 1

本文探讨了如何使用netif_add函数添加网络接口,并详细分析了netif_set_addr系列函数,包括设置IP地址、子网掩码和网关的方法。同时介绍了ethernetif_init函数和etharp_output在配置网络接口过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加netif_add函数 

struct netif *
netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
  struct ip_addr *gw,
  void *state,
  err_t (* init)(struct netif *netif),
  err_t (* input)(struct pbuf *p, struct netif *netif))
{
  static u8_t netifnum = 0;
/// 初始化各个参数
  /* reset new interface configuration state */
  netif->ip_addr.addr = 0;
  netif->netmask.addr = 0;
  netif->gw.addr = 0;
  netif->flags = 0;
#if LWIP_DHCP
  /* netif not under DHCP control by default */
  netif->dhcp = NULL;
#endif /* LWIP_DHCP */
#if LWIP_AUTOIP 
  /* netif not under AutoIP control by default */
  netif->autoip = NULL;
#endif /* LWIP_AUTOIP */
#if LWIP_NETIF_STATUS_CALLBACK
  netif->status_callback = NULL;
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
  netif->link_callback = NULL;
#endif /* LWIP_NETIF_LINK_CALLBACK */
#if LWIP_IGMP
  netif->igmp_mac_filter = NULL;
#endif /* LWIP_IGMP */
#if ENABLE_LOOPBACK
  netif->loop_first = NULL;
  netif->loop_last = NULL;
#endif /* ENABLE_LOOPBACK */

  /* remember netif specific state information data */
  netif->state = state;
  netif->num = netifnum++;
  netif->input = input;
#if LWIP_NETIF_HWADDRHINT
  netif->addr_hint = NULL;
#endif /* LWIP_NETIF_HWADDRHINT*/
#if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
  netif->loop_cnt_current = 0;
#endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
/ 初始化结束

  netif_set_addr(netif, ipaddr, netmask, gw);

  /* call user specified initialization function for netif */
  if (init(netif) != ERR_OK) {
    return NULL;
  }

  /* add this netif to the list */
  netif->next = netif_list;
  netif_list = netif;
  snmp_inc_iflist();

#if LWIP_IGMP
  /* start IGMP processing */
  if (netif->flags & NETIF_FLAG_IGMP) {
    igmp_start( netif);
  }
#endif /* LWIP_IGMP */

  LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %
`netifapi_netif_add` 和 `netif_add` 是用于在网络栈中添加网络接口的函数,主要用于嵌入式系统和网络设备驱动程序中。它们的主要区别在于 `netifapi_netif_add` 是通过网络接口API(Netif API)调用的,而 `netif_add` 是直接调用的。 ### `netif_add` `netif_add` 是直接添加网络接口的函数。它的原型如下: ```c struct netif *netif_add(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input); ``` - `netif`:指向要添加网络接口的指针。 - `ipaddr`:网络接口的IP地址。 - `netmask`:网络接口的子网掩码。 - `gw`:网络接口的默认网关。 - `state`:网络接口的私有状态数据。 - `init`:初始化函数。 - `input`:输入函数。 ### `netifapi_netif_add` `netifapi_netif_add` 是通过Netif API调用的函数。它的原型如下: ```c err_t netifapi_netif_add(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input); ``` - 参数与 `netif_add` 相同。 ### 主要区别 1. **调用方式**: `netif_add` 是直接调用,而 `netifapi_netif_add` 是通过Netif API调用。 2. **线程安全**: `netifapi_netif_add` 通常是线程安全的,因为它是通过Netif API调用的,而 `netif_add` 可能不是线程安全的。 ### 使用场景 - 如果你需要在多线程环境中添加网络接口,建议使用 `netifapi_netif_add`。 - 如果你确定在单线程环境中操作,可以使用 `netif_add`。 ### 示例代码 ```c #include "lwip/netif.h" #include "lwip/api.h" struct netif my_netif; // 使用 netif_add struct netif *netif_add_result = netif_add(&my_netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input); // 使用 netifapi_netif_add err_t netifapi_netif_add_result = netifapi_netif_add(&my_netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值