Blame sunrpc/bindrsvprt.c

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