Sunday, 14 November 2010

Understanding udp_get_next_priv_port()

udp_get_next_priv_port() from http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/inet/udp/udp.c

Assuming the system is unlabeled, what port will be returned when we're called (hint: it isn't IPPORT_RESERVED-1)?

No doubt this will be trivial for the C guru, but it had me scratching my head for a little while. It's probably time that I went back and read Programming 101...



/*
* Return the next anonymous port in the privileged port range for
* bind checking.
*/
static in_port_t
udp_get_next_priv_port(udp_t *udp)
{
static in_port_t next_priv_port = IPPORT_RESERVED - 1;
in_port_t nextport;
boolean_t restart = B_FALSE;
udp_stack_t *us = udp->udp_us;

retry:
if (next_priv_port < us->us_min_anonpriv_port ||
next_priv_port >= IPPORT_RESERVED) {
next_priv_port = IPPORT_RESERVED - 1;
if (restart)
return (0);
restart = B_TRUE;
}

if (isystem_labeled() &&
(nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred),
next_priv_port, IPPROTO_UDP, B_FALSE)) != 0) {
next_priv_port = nextport;
goto retry;
}

return (next_priv_port--);
}

1 comment:

  1. Have you highlighted the clue in green? ;-)

    ReplyDelete