Skip to content

Commit

Permalink
Use a named constant for "random port number"
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Boasson <[email protected]>
  • Loading branch information
eboasson committed Jul 27, 2023
1 parent 6dcb1e6 commit 7594aba
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/core/ddsi/src/ddsi__tran.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ struct ddsi_domaingv;
/* Flags */
#define DDSI_TRAN_ON_CONNECT 0x0001

/* Magic value for port number argument in create_conn and create_listener to indicate
that a random port number is requested. Note that 0 also happens to be illegal in UDP
and TCP and is DDSI_LOCATOR_PORT_INVALID in the DDSI spec. What a fortunate
coincidence! */
#define DDSI_TRAN_RANDOM_PORT_NUMBER 0

enum ddsi_tran_qos_purpose {
DDSI_TRAN_QOS_XMIT_UC, // will send unicast only
DDSI_TRAN_QOS_XMIT_MC, // may send unicast or multicast
Expand Down Expand Up @@ -252,15 +258,15 @@ inline dds_return_t ddsi_factory_create_conn (struct ddsi_tran_conn **conn, stru
*conn = NULL;
if ((qos->m_interface != NULL) != (qos->m_purpose == DDSI_TRAN_QOS_XMIT_UC || qos->m_purpose == DDSI_TRAN_QOS_XMIT_MC))
return DDS_RETCODE_BAD_PARAMETER;
if (port != 0 && !ddsi_is_valid_port (factory, port))
if (port != DDSI_TRAN_RANDOM_PORT_NUMBER && !ddsi_is_valid_port (factory, port))
return DDS_RETCODE_BAD_PARAMETER;
return factory->m_create_conn_fn (conn, factory, port, qos);
}

/** @component transport */
inline dds_return_t ddsi_factory_create_listener (struct ddsi_tran_listener **listener, struct ddsi_tran_factory * factory, uint32_t port, const struct ddsi_tran_qos *qos) {
*listener = NULL;
if (port != 0 && !ddsi_is_valid_port (factory, port))
if (port != DDSI_TRAN_RANDOM_PORT_NUMBER && !ddsi_is_valid_port (factory, port))
return DDS_RETCODE_BAD_PARAMETER;
return factory->m_create_listener_fn (listener, factory, port, qos);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/ddsi/src/ddsi_debmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ struct ddsi_debug_monitor *ddsi_new_debug_monitor (struct ddsi_domaingv *gv, int
if ((dm->tran_factory = ddsi_factory_find (gv, "tcp")) == NULL)
dm->tran_factory = ddsi_factory_find (gv, "tcp6");

if (port != 0 && !ddsi_is_valid_port (dm->tran_factory, (uint32_t) port))
if (port != DDSI_TRAN_RANDOM_PORT_NUMBER && !ddsi_is_valid_port (dm->tran_factory, (uint32_t) port))
{
GVERROR ("debug monitor port number %"PRId32" is invalid\n", port);
goto err_invalid_port;
Expand Down
10 changes: 5 additions & 5 deletions src/core/ddsi/src/ddsi_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ static enum make_uc_sockets_ret make_uc_sockets (struct ddsi_domaingv *gv, uint3
*pdisc = ddsi_get_port (&gv->config, DDSI_PORT_UNI_DISC, ppid);
*pdata = ddsi_get_port (&gv->config, DDSI_PORT_UNI_DATA, ppid);

if (*pdisc != 0 && !ddsi_is_valid_port (gv->m_factory, *pdisc))
if (*pdisc != DDSI_TRAN_RANDOM_PORT_NUMBER && !ddsi_is_valid_port (gv->m_factory, *pdisc))
return MUSRET_INVALID_PORTS;
if (*pdata != 0 && !ddsi_is_valid_port (gv->m_factory, *pdata))
if (*pdata != DDSI_TRAN_RANDOM_PORT_NUMBER && !ddsi_is_valid_port (gv->m_factory, *pdata))
return MUSRET_INVALID_PORTS;

const struct ddsi_tran_qos qos = { .m_purpose = DDSI_TRAN_QOS_RECV_UC, .m_diffserv = 0, .m_interface = NULL };
Expand Down Expand Up @@ -285,7 +285,7 @@ static int string_to_default_locator (const struct ddsi_domaingv *gv, ddsi_locat
GVERROR ("%s: invalid address kind (%s)\n", string, tag);
return -1;
}
if (port != 0 && !ddsi_is_unspec_locator(loc))
if (port != DDSI_LOCATOR_PORT_INVALID && !ddsi_is_unspec_locator(loc))
loc->port = port;
else
loc->port = DDSI_LOCATOR_PORT_INVALID;
Expand Down Expand Up @@ -1492,8 +1492,8 @@ int ddsi_init (struct ddsi_domaingv *gv)
else
{
if (gv->config.tcp_port < 0)
; /* nop */
else if (gv->config.tcp_port == 0)
; /* no TCP listener */
else if (gv->config.tcp_port == DDSI_TRAN_RANDOM_PORT_NUMBER)
; /* kernel-allocated random port */
else if (!ddsi_is_valid_port (gv->m_factory, (uint32_t) gv->config.tcp_port))
{
Expand Down
8 changes: 6 additions & 2 deletions src/core/ddsi/src/ddsi_portmapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "dds/ddsi/ddsi_config.h"
#include "ddsi__portmapping.h"

// for a static assert on DDSI_TRAN_RANDOM_PORT_NUMBER
#include "ddsi__tran.h"

static bool get_port_int (uint32_t *port, const struct ddsi_portmapping *map, enum ddsi_port which, uint32_t domain_id, int32_t participant_index, char *str_if_overflow, size_t strsize)
{
uint32_t off = UINT32_MAX, ppidx = UINT32_MAX;
Expand All @@ -38,7 +41,7 @@ static bool get_port_int (uint32_t *port, const struct ddsi_portmapping *map, en
if (participant_index == DDSI_PARTICIPANT_INDEX_NONE)
{
/* participant index "none" means unicast ports get chosen by the transport */
*port = 0;
*port = DDSI_TRAN_RANDOM_PORT_NUMBER;
return true;
}
off = map->d1;
Expand All @@ -48,7 +51,7 @@ static bool get_port_int (uint32_t *port, const struct ddsi_portmapping *map, en
if (participant_index == DDSI_PARTICIPANT_INDEX_NONE)
{
/* participant index "none" means unicast ports get chosen by the transport */
*port = 0;
*port = DDSI_TRAN_RANDOM_PORT_NUMBER;
return true;
}
off = map->d3;
Expand All @@ -62,6 +65,7 @@ static bool get_port_int (uint32_t *port, const struct ddsi_portmapping *map, en
/* For the mapping to be valid, the port number must be in range of an unsigned 32 bit integer and must
not be 0 (as that is used for indicating a random port should be selected by the transport). The
transports may limit this further, but at least we won't have to worry about overflow anymore. */
DDSRT_STATIC_ASSERT (DDSI_TRAN_RANDOM_PORT_NUMBER == 0);
*port = (uint32_t) (a + b);
if (a <= UINT32_MAX && b <= UINT32_MAX - a && *port > 0)
return true;
Expand Down

0 comments on commit 7594aba

Please sign in to comment.