int uip_udpconnect(struct uip_udp_conn *conn, const struct sockaddr_in *addr) #endif { /* Has this address already been bound to a local port (lport)? */ if (!conn->lport) { /* No.. Find an unused local port number and bind it to the * connection structure. */ conn->lport = htons(uip_selectport()); } /* Is there a remote port (rport) */ if (addr) { conn->rport = addr->sin_port; uip_ipaddr_copy(conn->ripaddr, addr->sin_addr.s_addr); } else { conn->rport = 0; uip_ipaddr_copy(conn->ripaddr, g_allzeroaddr); } conn->ttl = UIP_TTL; return OK; }
int uip_tcpbind(struct uip_conn *conn, const struct sockaddr_in *addr) #endif { uip_lock_t flags; int port; /* Verify or select a local port */ flags = uip_lock(); port = uip_selectport(ntohs(addr->sin_port)); uip_unlock(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; }
int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in *addr) #endif { int ret = -EADDRINUSE; uip_lock_t flags; /* Is the user requesting to bind to any port? */ if (!addr->sin_port) { /* Yes.. Find an unused local port number */ conn->lport = htons(uip_selectport()); ret = OK; } else { /* Interrupts must be disabled while access the UDP connection list */ flags = uip_lock(); /* Is any other UDP connection bound to this port? */ if (!uip_find_conn(addr->sin_port)) { /* No.. then bind the socket to the port */ conn->lport = addr->sin_port; ret = OK; } uip_unlock(flags); } return ret; }
int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in *addr) #endif { uip_lock_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 = uip_lock(); port = uip_selectport(ntohs(conn->lport)); uip_unlock(flags); if (port < 0) { return port; } /* Initialize and return the connection structure, bind it to the port number */ conn->tcpstateflags = UIP_SYN_SENT; uip_tcpinitsequence(conn->sndseq); conn->initialmss = conn->mss = UIP_TCP_MSS; conn->unacked = 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_t)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 = uip_lock(); dq_addlast(&conn->node, &g_active_tcp_connections); uip_unlock(flags); return OK; }