mirror of https://github.com/ipxe/ipxe.git
Port the UDP port allocation code to TCP
parent
de0c36a98a
commit
065a2a486d
|
@ -529,35 +529,59 @@ int tcp_close ( struct tcp_connection *conn ) {
|
||||||
return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
|
return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind TCP connection to local port
|
||||||
|
*
|
||||||
|
* @v conn TCP connection
|
||||||
|
* @v local_port Local port, in network byte order
|
||||||
|
* @ret rc Return status code
|
||||||
|
*/
|
||||||
|
int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
|
||||||
|
struct tcp_connection *existing;
|
||||||
|
|
||||||
|
list_for_each_entry ( existing, &tcp_conns, list ) {
|
||||||
|
if ( existing->local_port == local_port )
|
||||||
|
return -EADDRINUSE;
|
||||||
|
}
|
||||||
|
conn->local_port = local_port;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listen for a packet
|
* Listen for a packet
|
||||||
*
|
*
|
||||||
* @v conn TCP connection
|
* @v conn TCP connection
|
||||||
* @v port Local port, in network byte order
|
* @v local_port Local port, in network byte order
|
||||||
*
|
*
|
||||||
* This function adds the connection to a list of registered tcp connections. If
|
* This function adds the connection to a list of registered tcp
|
||||||
* the local port is 0, the connection is assigned the lowest available port
|
* connections. If the local port is 0, the connection is assigned an
|
||||||
* between MIN_TCP_PORT and 65535.
|
* available port between MIN_TCP_PORT and 65535.
|
||||||
*/
|
*/
|
||||||
int tcp_listen ( struct tcp_connection *conn, uint16_t port ) {
|
int tcp_listen ( struct tcp_connection *conn, uint16_t local_port ) {
|
||||||
struct tcp_connection *cconn;
|
static uint16_t try_port = 1024;
|
||||||
if ( port != 0 ) {
|
int rc;
|
||||||
list_for_each_entry ( cconn, &tcp_conns, list ) {
|
|
||||||
if ( cconn->local_port == port ) {
|
/* If no port specified, find the first available port */
|
||||||
DBG ( "Error listening to %d\n",
|
if ( ! local_port ) {
|
||||||
ntohs ( port ) );
|
for ( ; try_port ; try_port++ ) {
|
||||||
return -EISCONN;
|
if ( try_port < 1024 )
|
||||||
}
|
continue;
|
||||||
}
|
if ( tcp_listen ( conn, htons ( try_port ) ) == 0 )
|
||||||
/* Add the connection to the list of registered connections */
|
|
||||||
conn->local_port = port;
|
|
||||||
list_add ( &conn->list, &tcp_conns );
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/* Assigning lowest port not supported */
|
return -EADDRINUSE;
|
||||||
DBG ( "Assigning lowest port not implemented\n");
|
}
|
||||||
return -ENOSYS;
|
|
||||||
|
/* Attempt bind to local port */
|
||||||
|
if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 )
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
/* Add to TCP connection list */
|
||||||
|
list_add ( &conn->list, &tcp_conns );
|
||||||
|
DBG ( "TCP opened %p on port %d\n", conn, ntohs ( local_port ) );
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue