【LwIP源码学习8】netbuf源码分析

netbuf结构体

netbuf是lwip中对接收和发送数据的更高一层封装,记录了数据发送方的IP地址和端口号,netbuf结构体如下:

c 复制代码
/** "Network buffer" - contains data and addressing info */
struct netbuf {
  struct pbuf *p, *ptr;
  ip_addr_t addr;
  u16_t port;
};

其中addr是ip地址,port是端口号,ip地址对应的是一个设备,端口号对应一个设备里的一个线程。

指针p指向首个pbuf,是不可改变的,指针ptr可以通过函数netbuf_next()和函数netbuf_first()来移动。用函数netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)获取ptr指向的pbuf中数据的地址dataptr,并获取其数据量len

netbuf_new()

申请一个新的netbuf结构体内存空间并返回。

c 复制代码
struct
netbuf *netbuf_new(void)
{
  struct netbuf *buf;

  buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  if (buf != NULL) {
    memset(buf, 0, sizeof(struct netbuf));
  }
  return buf;
}

netbuf_delete()

删除一个netbuf结构体内存空间,如果删除的netbufptrp指向的pbuf有数据,那么将对应的pbuf也释放掉。

c 复制代码
void
netbuf_delete(struct netbuf *buf)
{
  if (buf != NULL) {
    if (buf->p != NULL) {
      pbuf_free(buf->p);
      buf->p = buf->ptr = NULL;
    }
    memp_free(MEMP_NETBUF, buf);
  }
}

netbuf_alloc()

netbufptrp指向的pbuf申请一块指定大小的内存空间。如果当前netbuf中的pbuf存在数据,则先把数据释放掉。

c 复制代码
void *
netbuf_alloc(struct netbuf *buf, u16_t size)
{
  LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);

  /* Deallocate any previously allocated memory. */
  if (buf->p != NULL) {
    pbuf_free(buf->p);
  }
  buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
  if (buf->p == NULL) {
    return NULL;
  }
  LWIP_ASSERT("check that first pbuf can hold size",
              (buf->p->len >= size));
  buf->ptr = buf->p;
  return buf->p->payload;
}

netbuf_free()

netbuf中的pbuf的内存空间释放掉。

c 复制代码
void
netbuf_free(struct netbuf *buf)
{
  LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
  if (buf->p != NULL) {
    pbuf_free(buf->p);
  }
  buf->p = buf->ptr = NULL;
#if LWIP_CHECKSUM_ON_COPY
  buf->flags = 0;
  buf->toport_chksum = 0;
#endif /* LWIP_CHECKSUM_ON_COPY */
}

netbuf_ref()

netbuf_alloc()类似,只不过这个是申请一个空的pbuf,然后pbuf的数据指针payload指向用户传入的数据地址。
reference:引用,指向。

c 复制代码
err_t
netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
{
  LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
  if (buf->p != NULL) {
    pbuf_free(buf->p);
  }
  buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
  if (buf->p == NULL) {
    buf->ptr = NULL;
    return ERR_MEM;
  }
  ((struct pbuf_rom *)buf->p)->payload = dataptr;
  buf->p->len = buf->p->tot_len = size;
  buf->ptr = buf->p;
  return ERR_OK;
}

netbuf_chain()

将指针tail指向的pbuf添加到h指向的head链表的末尾,然后tail会被释放
pbuf_cat()catConcatenate的缩写,中文意思是 "连接" 或 "拼接"。

c 复制代码
void
netbuf_chain(struct netbuf *head, struct netbuf *tail)
{
  LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);
  LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
  pbuf_cat(head->p, tail->p);
  head->ptr = head->p;
  memp_free(MEMP_NETBUF, tail);
}

netbuf_data()

netbufptr指向的pbuf里数据的起始地址填写到dataptr,并将数据长度填写到len
netbuf中的p指向的是pbuf链表的首个pbufptr指向的是当前pbuf,如果想移动ptr可以用netbuf_next()函数或者netbuf_first()函数。

c 复制代码
err_t
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
{
  LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
  LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
  LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);

  if (buf->ptr == NULL) {
    return ERR_BUF;
  }
  *dataptr = buf->ptr->payload;
  *len = buf->ptr->len;
  return ERR_OK;
}

netbuf_next()

netbufptr移动到pbuf链表的下一个pbuf

c 复制代码
s8_t
netbuf_next(struct netbuf *buf)
{
  LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);
  if (buf->ptr->next == NULL) {
    return -1;
  }
  buf->ptr = buf->ptr->next;
  if (buf->ptr->next == NULL) {
    return 1;
  }
  return 0;
}

netbuf_first()

netbufptr指向pbuf链表的首个pbuf,也就是p指向的pbuf

c 复制代码
void
netbuf_first(struct netbuf *buf)
{
  LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);
  buf->ptr = buf->p;
}
相关推荐
JM丫2 小时前
内网理论知识总结
笔记·网络安全
CHINAHEAO2 小时前
FlyEnv+Bagisto安装遇到的一些问题
android
毕设源码-郭学长2 小时前
【开题答辩全过程】以 基于Android的自习室座位预订系统为例,包含答辩的问题和答案
android
d111111111d2 小时前
STM32外设学习-I2C(细节)--学习笔记
笔记·stm32·单片机·嵌入式硬件·学习
( ˶˙⚇˙˶ )୨⚑︎2 小时前
【学习笔记】DiffFNO: Diffusion Fourier Neural Operator
笔记·神经网络·学习
Topplyz2 小时前
交流耦合同相放大电路、MAX4466音频放大电路功能详解
学习·运算放大器·模电·放大电路·音频放大
chuwengeileyan13 小时前
stm32 光敏电阻 光控灯
stm32·单片机·嵌入式硬件
ModestCoder_3 小时前
【学习笔记】Diffusion Policy for Robotics
论文阅读·人工智能·笔记·学习·机器人·强化学习·具身智能
麦麦大数据3 小时前
F049 知识图谱双算法推荐在线学习系统vue+flask+neo4j之BS架构开题论文全源码
学习·算法·知识图谱·推荐算法·开题报告·学习系统·计算机毕业设计展示