Blame sunrpc/bindrsvprt.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Locks the static variables in this file.
Packit 6c4009
 */
Packit 6c4009
__libc_lock_define_initialized (static, lock);
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Bind a socket to a privileged IP port
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
bindresvport (int sd, struct sockaddr_in *sin)
Packit 6c4009
{
Packit 6c4009
  static short port;
Packit 6c4009
  struct sockaddr_in myaddr;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
#define STARTPORT 600
Packit 6c4009
#define LOWPORT 512
Packit 6c4009
#define ENDPORT (IPPORT_RESERVED - 1)
Packit 6c4009
#define NPORTS	(ENDPORT - STARTPORT + 1)
Packit 6c4009
  static short startport = STARTPORT;
Packit 6c4009
Packit 6c4009
  if (sin == (struct sockaddr_in *) 0)
Packit 6c4009
    {
Packit 6c4009
      sin = &myaddr;
Packit 6c4009
      memset (sin, 0, sizeof (*sin));
Packit 6c4009
      sin->sin_family = AF_INET;
Packit 6c4009
    }
Packit 6c4009
  else if (sin->sin_family != AF_INET)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EAFNOSUPPORT);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (port == 0)
Packit 6c4009
    {
Packit 6c4009
      port = (__getpid () % NPORTS) + STARTPORT;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Initialize to make gcc happy.  */
Packit 6c4009
  int res = -1;
Packit 6c4009
Packit 6c4009
  int nports = ENDPORT - startport + 1;
Packit 6c4009
  int endport = ENDPORT;
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
 again:
Packit 6c4009
  for (i = 0; i < nports; ++i)
Packit 6c4009
    {
Packit 6c4009
      sin->sin_port = htons (port++);
Packit 6c4009
      if (port > endport)
Packit 6c4009
	port = startport;
Packit 6c4009
      res = __bind (sd, sin, sizeof (struct sockaddr_in));
Packit 6c4009
      if (res >= 0 || errno != EADDRINUSE)
Packit 6c4009
	break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (i == nports && startport != LOWPORT)
Packit 6c4009
    {
Packit 6c4009
      startport = LOWPORT;
Packit 6c4009
      endport = STARTPORT - 1;
Packit 6c4009
      nports = STARTPORT - LOWPORT;
Packit 6c4009
      port = LOWPORT + port % (STARTPORT - LOWPORT);
Packit 6c4009
      goto again;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return res;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (bindresvport)