/* $NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 2009, Sun Microsystems, Inc.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions are met:10* - Redistributions of source code must retain the above copyright notice,11* this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15* - Neither the name of Sun Microsystems, Inc. nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE23* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR24* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF25* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS26* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN27* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE29* POSSIBILITY OF SUCH DAMAGE.30*/3132/* from: $OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp $ */33/*34* Copyright (c) 1987 by Sun Microsystems, Inc.35*36* Portions Copyright(C) 1996, Jason Downs. All rights reserved.37*/3839#include "namespace.h"40#include <sys/types.h>41#include <sys/socket.h>4243#include <netinet/in.h>4445#include <errno.h>46#include <string.h>47#include <unistd.h>4849#include <rpc/rpc.h>5051#include <string.h>52#include "un-namespace.h"5354/*55* Bind a socket to a privileged IP port56*/57int58bindresvport(int sd, struct sockaddr_in *sin)59{60return bindresvport_sa(sd, (struct sockaddr *)sin);61}6263/*64* Bind a socket to a privileged IP port65*/66int67bindresvport_sa(int sd, struct sockaddr *sa)68{69int old, error, af;70struct sockaddr_storage myaddr;71struct sockaddr_in *sin;72#ifdef INET673struct sockaddr_in6 *sin6;74#endif75int proto, portrange, portlow;76u_int16_t *portp;77socklen_t salen;7879if (sa == NULL) {80salen = sizeof(myaddr);81sa = (struct sockaddr *)&myaddr;8283if (_getsockname(sd, sa, &salen) == -1)84return -1; /* errno is correctly set */8586af = sa->sa_family;87memset(sa, 0, salen);88} else89af = sa->sa_family;9091switch (af) {92case AF_INET:93proto = IPPROTO_IP;94portrange = IP_PORTRANGE;95portlow = IP_PORTRANGE_LOW;96sin = (struct sockaddr_in *)sa;97salen = sizeof(struct sockaddr_in);98portp = &sin->sin_port;99break;100#ifdef INET6101case AF_INET6:102proto = IPPROTO_IPV6;103portrange = IPV6_PORTRANGE;104portlow = IPV6_PORTRANGE_LOW;105sin6 = (struct sockaddr_in6 *)sa;106salen = sizeof(struct sockaddr_in6);107portp = &sin6->sin6_port;108break;109#endif110default:111errno = EPFNOSUPPORT;112return (-1);113}114sa->sa_family = af;115sa->sa_len = salen;116117if (*portp == 0) {118socklen_t oldlen = sizeof(old);119120error = _getsockopt(sd, proto, portrange, &old, &oldlen);121if (error < 0)122return (error);123124error = _setsockopt(sd, proto, portrange, &portlow,125sizeof(portlow));126if (error < 0)127return (error);128}129130error = _bind(sd, sa, salen);131132if (*portp == 0) {133int saved_errno = errno;134135if (error < 0) {136if (_setsockopt(sd, proto, portrange, &old,137sizeof(old)) < 0)138errno = saved_errno;139return (error);140}141142if (sa != (struct sockaddr *)&myaddr) {143/* Hmm, what did the kernel assign? */144if (_getsockname(sd, sa, &salen) < 0)145errno = saved_errno;146return (error);147}148}149return (error);150}151152153