📄 uip-tcpconn.c
字号:
/**************************************************************************** * Name: uip_tcpactive() * * Description: * Find a connection structure that is the appropriate * connection to be used withi the provided TCP/IP header * * Assumptions: * This function is called from UIP logic at interrupt level * ****************************************************************************/struct uip_conn *uip_tcpactive(struct uip_tcpip_hdr *buf){ struct uip_conn *conn = (struct uip_conn *)g_active_tcp_connections.head; in_addr_t srcipaddr = uip_ip4addr_conv(buf->srcipaddr); while (conn) { /* Find an open connection matching the tcp input */ if (conn->tcpstateflags != UIP_CLOSED && buf->destport == conn->lport && buf->srcport == conn->rport && uip_ipaddr_cmp(srcipaddr, conn->ripaddr)) { /* Matching connection found.. break out of the loop and return a * reference to it. */ break; } /* Look at the next active connection */ conn = (struct uip_conn *)conn->node.flink; } return conn;}/**************************************************************************** * Name: uip_nexttcpconn() * * Description: * Traverse the list of active TCP connections * * Assumptions: * This function is called from UIP logic at interrupt level (or with * interrupts disabled). * ****************************************************************************/struct uip_conn *uip_nexttcpconn(struct uip_conn *conn){ if (!conn) { return (struct uip_conn *)g_active_tcp_connections.head; } else { return (struct uip_conn *)conn->node.flink; }}/**************************************************************************** * Name: uip_tcplistener() * * Description: * Given a local port number (in network byte order), find the TCP * connection that listens on this this port. * * Primary uses: (1) to determine if a port number is available, (2) to * To idenfity the socket that will accept new connections on a local port. * ****************************************************************************/struct uip_conn *uip_tcplistener(uint16 portno){ struct uip_conn *conn; int i; /* Check if this port number is in use by any active UIP TCP connection */ for (i = 0; i < CONFIG_NET_TCP_CONNS; i++) { conn = &g_tcp_connections[i]; if (conn->tcpstateflags != UIP_CLOSED && conn->lport == portno) { /* The portnumber is in use, return the connection */ return conn; } } return NULL;}/**************************************************************************** * Name: uip_tcpaccept() * * Description: * Called when uip_interupt matches the incoming packet with a connection * in LISTEN. In that case, this function will create a new connection and * initialize it to send a SYNACK in return. * * Assumptions: * This function is called from UIP logic at interrupt level * ****************************************************************************/struct uip_conn *uip_tcpaccept(struct uip_tcpip_hdr *buf){ struct uip_conn *conn = uip_tcpalloc(); if (conn) { /* Fill in the necessary fields for the new connection. */ conn->rto = UIP_RTO; conn->timer = UIP_RTO; conn->sa = 0; conn->sv = 4; conn->nrtx = 0; conn->lport = buf->destport; conn->rport = buf->srcport; uip_ipaddr_copy(conn->ripaddr, uip_ip4addr_conv(buf->srcipaddr)); conn->tcpstateflags = UIP_SYN_RCVD; memcpy(conn->snd_nxt, g_tcp_sequence, 4); conn->len = 1; /* rcv_nxt should be the seqno from the incoming packet + 1. */ memcpy(conn->rcv_nxt, buf->seqno, 4); /* Initialize the list of TCP read-ahead buffers */#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 sq_init(&conn->readahead);#endif /* And, finally, put the connection structure into the active list. * Interrupts should already be disabled in this context. */ dq_addlast(&conn->node, &g_active_tcp_connections); } return conn;}/**************************************************************************** * Name: uip_tcpnextsequence() * * Description: * Increment the TCP/IP sequence number * * Assumptions: * This function is called from the interrupt level * ****************************************************************************/void uip_tcpnextsequence(void){ /* This inplements a byte-by-byte big-endian increment */ if (++g_tcp_sequence[3] == 0) { if (++g_tcp_sequence[2] == 0) { if (++g_tcp_sequence[1] == 0) { ++g_tcp_sequence[0]; } } }}/**************************************************************************** * Name: uip_tcpbind() * * Description: * This function implements the UIP specific parts of the standard TCP * bind() operation. * * Return: * 0 on success or -EADDRINUSE on failure * * Assumptions: * This function is called from normal user level code. * ****************************************************************************/#ifdef CONFIG_NET_IPv6int uip_tcpbind(struct uip_conn *conn, const struct sockaddr_in6 *addr)#elseint uip_tcpbind(struct uip_conn *conn, const struct sockaddr_in *addr)#endif{ irqstate_t flags; int port; /* Verify or select a local port */ flags = irqsave(); port = uip_selectport(ntohs(addr->sin_port)); irqrestore(flags); if (port < 0) { return port; } /* Save the local address in the connection structure. Note that the requested * local IP address is saved but not used. At present, only a single network * interface is supported, the IP address is not of importance. */ conn->lport = addr->sin_port;#if 0 /* Not used */#ifdef CONFIG_NET_IPv6 uip_ipaddr_copy(conn->lipaddr, addr->sin6_addr.in6_u.u6_addr16);#else uip_ipaddr_copy(conn->lipaddr, addr->sin_addr.s_addr);#endif#endif return OK;}/**************************************************************************** * Name: uip_tcpconnect() * * Description: * This function implements the UIP specific parts of the standard * TCP connect() operation: It connects to a remote host using TCP. * * This function is used to start a new connection to the specified * port on the specied host. It uses the connection structure that was * allocated by a preceding socket() call. It sets the connection to * the SYN_SENT state and sets the retransmission timer to 0. This will * cause a TCP SYN segment to be sent out the next time this connection * is periodically processed, which usually is done within 0.5 seconds * after the call to uip_tcpconnect(). * * Assumptions: * This function is called from normal user level code. * ****************************************************************************/#ifdef CONFIG_NET_IPv6int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in6 *addr)#elseint uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in *addr)#endif{ irqstate_t flags; int port; /* The connection is expected to be in the UIP_ALLOCATED state.. i.e., * allocated via up_tcpalloc(), but not yet put into the active connections * list. */ if (!conn || conn->tcpstateflags != UIP_ALLOCATED) { return -EISCONN; } /* If the TCP port has not alread been bound to a local port, then select * one now. */ flags = irqsave(); port = uip_selectport(ntohs(conn->lport)); irqrestore(flags); if (port < 0) { return port; } /* Initialize and return the connection structure, bind it to the port number */ conn->tcpstateflags = UIP_SYN_SENT; memcpy(conn->snd_nxt, g_tcp_sequence, 4); conn->initialmss = conn->mss = UIP_TCP_MSS; conn->len = 1; /* TCP length of the SYN is one. */ conn->nrtx = 0; conn->timer = 1; /* Send the SYN next time around. */ conn->rto = UIP_RTO; conn->sa = 0; conn->sv = 16; /* Initial value of the RTT variance. */ conn->lport = htons((uint16)port); /* The sockaddr port is 16 bits and already in network order */ conn->rport = addr->sin_port; /* The sockaddr address is 32-bits in network order. */ uip_ipaddr_copy(conn->ripaddr, addr->sin_addr.s_addr); /* Initialize the list of TCP read-ahead buffers */#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 sq_init(&conn->readahead);#endif /* And, finally, put the connection structure into the active * list. Because g_active_tcp_connections is accessed from user level and * interrupt level, code, it is necessary to keep interrupts disabled during * this operation. */ flags = irqsave(); dq_addlast(&conn->node, &g_active_tcp_connections); irqrestore(flags); return OK;}#endif /* CONFIG_NET && CONFIG_NET_TCP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -