lwip TCP客户端 tcp_connect函数源码解析

tcp/ip 协议栈版本:lwip-1.4.0

参考阅读:《嵌入式网络那些事》--传输控制协议  章节

TCP初始编程--常用部分函数解析连接:

简介几个概念:
滑动窗口:

TCP的滑动窗口主要有两个作用,

  • 提供TCP的可靠性
  • 提供TCP的流控特性


TCP报文结构:

这里关注两个标志:SYN和FIN 连接建立/关闭


正文:

控制块连接 

对于客户端程序来说,需要执行主动打开操作,就是向服务器端发送一个SYN握手报文段,通过tcp_connect函数实现。

控制快的四条链表:

/* The TCP PCB lists.--四条链表来连接处于不同状态下的控制块 */
/** List of all TCP PCBs bound but not yet (connected || listening) --新创建的且绑定了本地端口号,但还没有连接或者侦听的PCBs*/
struct tcp_pcb *tcp_bound_pcbs;
/** List of all TCP PCBs in LISTEN state --进入LISTEN侦听状态(被动连接)的*/
union tcp_listen_pcbs_t tcp_listen_pcbs;
/** List of all TCP PCBs that are in a state in which--连接所有处于其他状态(另外三个链表之外的)的控制块:稳定状态/活跃的链表
 * they accept or send data. */
struct tcp_pcb *tcp_active_pcbs;
/** List of all TCP PCBs in TIME-WAIT state ---处于TIME-WAIT状态的控制块链表*/
struct tcp_pcb *tcp_tw_pcbs;
一个本地全局数组,指向上诉四条链表指针的指针: 以此减少代码量

/** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
  &tcp_active_pcbs, &tcp_tw_pcbs};


相关宏:

#define NUM_TCP_PCB_LISTS               4 //TCP控制块链表类型数目
#define NUM_TCP_PCB_LISTS_NO_TIME_WAIT  3 //TCP控制块链表类型数目,不带TIME_WAIT那类链表


源码+注释如下:

/**
 * Connects to another host. The function given as the "connected" -连接到另外一个主机。
 * argument will be called when the connection has been established.-当连接已经建立时,函数的参数"connected"参数将被调用
 *主动向服务器发送一个SYN握手报文
 * @param pcb the tcp_pcb used to establish the connection
 * @param ipaddr the remote ip address to connect to
 * @param port the remote tcp port to connect to
 * @param connected callback function to call when connected (or on error)
 * @return ERR_VAL if invalid arguments are given
 *         ERR_OK if connect request has been sent
 *         other err_t values if connect request couldn't be sent
 */
err_t
tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port,
      tcp_connected_fn connected)
{
  err_t ret;
  u32_t iss;
  u16_t old_local_port;

  LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);

  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
  if (ipaddr != NULL) {
    pcb->remote_ip = *ipaddr;//对端ip初始化
  } else {
    return ERR_VAL;
  }
  pcb->remote_port = port;//对端端口号

  /* check if we have a route to the remote host --检查是否有一个路由/途径到远程主机*/
  if (ip_addr_isany(&(pcb->local_ip))) {//本地ip是 无效或者广播?
    /* no local IP address set, yet. 还没有设置本地IP地址。*/
    struct netif *netif = ip_route(&(pcb->remote_ip));//尝试找到一个能够到达目的地址的本地ip
    if (netif == NULL) {//没有就算啦
      /* Don't even try to send a SYN packet if we have no route
         since that will fail. */
      return ERR_RTE;
    }
    /* Use the netif's IP address as local address. */
    ip_addr_copy(pcb->local_ip, netif->ip_addr);//给pcb一个可用的本地ip
  }

  old_local_port = pcb->local_port;
  if (pcb->local_port == 0) {//端口无效,给他找一个
    pcb->local_port = tcp_new_port();
  }
#if SO_REUSE  //这部分详情度娘搜索:SO_REUSEADDR 关键字
  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
    /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure==地址可重用
       now that the 5-tuple is unique. */
    struct tcp_pcb *cpcb;
    int i;
    /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. 仅需要遍历检查tcp_active_pcbs 和 tcp_tw_pcbs链表*/
    for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
      for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
        if ((cpcb->local_port == pcb->local_port) &&
            (cpcb->remote_port == port) &&
            ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
            ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
          /* linux returns EISCONN here, but ERR_USE should be OK for us */
          return ERR_USE;
        }
      }
    }
  }
#endif /* SO_REUSE */
  iss = tcp_next_iss();//初始化  序列号
  pcb->rcv_nxt = 0; //设置发送窗口的各个字段
  pcb->snd_nxt = iss;
  pcb->lastack = iss - 1;
  pcb->snd_lbb = iss - 1;
  pcb->rcv_wnd = TCP_WND;//接收窗口的字段
  pcb->rcv_ann_wnd = TCP_WND;
  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
  pcb->snd_wnd = TCP_WND;
  /* As initial send MSS, we use TCP_MSS but limit it to 536.
     The send MSS is updated when an MSS option is received. */
  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
#if TCP_CALCULATE_EFF_SEND_MSS //根据需要设置有效发送mss字段
  pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr);
#endif /* TCP_CALCULATE_EFF_SEND_MSS */
  pcb->cwnd = 1;
  pcb->ssthresh = pcb->mss * 10;
#if LWIP_CALLBACK_API
  pcb->connected = connected;//注册connected回调函数
#else /* LWIP_CALLBACK_API */  
  LWIP_UNUSED_ARG(connected);
#endif /* LWIP_CALLBACK_API */

  /* Send a SYN together with the MSS option. 拼接带着MSS选项的SYN标志请求 */
  ret = tcp_enqueue_flags(pcb, TCP_SYN);
  if (ret == ERR_OK) {
    /* SYN segment was enqueued, changed the pcbs state now */
    pcb->state = SYN_SENT;// pcb按规则入队,状态置为SYN_SENT。此处放入tcp_active_pcbs
    if (old_local_port != 0) {
      TCP_RMV(&tcp_bound_pcbs, pcb);//状态更新,pcb移出相应链表
    }
    TCP_REG(&tcp_active_pcbs, pcb);
    snmp_inc_tcpactiveopens();//未知

    tcp_output(pcb);//将pcb上连接的报文发送出去
  }
  return ret;
}
关于tcp_enqueue_flags、tcp_output函数,再议。。。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值