2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1990, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * Portions of this source code were derived from Berkeley 4.3 BSD
2N/A * under license from the Regents of the University of California.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <errno.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <netinet/tcp.h>
2N/A#include <netinet/udp.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A
2N/A#ifdef SYSV
2N/A#define bzero(s, len) (void) memset((s), 0, (len))
2N/A#endif
2N/A
2N/A
2N/A/*
2N/A * Bind a socket to a privileged IP port
2N/A */
2N/Aint
2N/Abindresvport(int sd, struct sockaddr_in *sin)
2N/A{
2N/A struct sockaddr_in myaddr;
2N/A struct sockaddr_in *bindaddr;
2N/A int level, optname;
2N/A int optval;
2N/A socklen_t len;
2N/A int ret;
2N/A
2N/A bindaddr = sin;
2N/A if (bindaddr == (struct sockaddr_in *)0) {
2N/A bindaddr = &myaddr;
2N/A bzero(bindaddr, sizeof (*bindaddr));
2N/A bindaddr->sin_family = AF_INET;
2N/A } else if (bindaddr->sin_family != AF_INET) {
2N/A errno = EPFNOSUPPORT;
2N/A return (-1);
2N/A }
2N/A
2N/A len = sizeof (optval);
2N/A if (getsockopt(sd, SOL_SOCKET, SO_TYPE, &optval, &len) < 0) {
2N/A return (-1);
2N/A }
2N/A /*
2N/A * Use *_ANONPRIVBIND to ask the kernel to pick a port in the
2N/A * priviledged range for us.
2N/A */
2N/A if (optval == SOCK_STREAM) {
2N/A level = IPPROTO_TCP;
2N/A optname = TCP_ANONPRIVBIND;
2N/A } else if (optval == SOCK_DGRAM) {
2N/A level = IPPROTO_UDP;
2N/A optname = UDP_ANONPRIVBIND;
2N/A } else {
2N/A errno = EPROTONOSUPPORT;
2N/A return (-1);
2N/A }
2N/A
2N/A optval = 1;
2N/A if (setsockopt(sd, level, optname, &optval, sizeof (optval)) < 0) {
2N/A return (-1);
2N/A }
2N/A
2N/A bindaddr->sin_port = 0;
2N/A ret = bind(sd, (struct sockaddr *)bindaddr,
2N/A sizeof (struct sockaddr_in));
2N/A
2N/A /*
2N/A * Always turn off the option when we are done. Note that by doing
2N/A * this, if the caller has set this option before calling
2N/A * bindresvport(), it will be unset. But this should never happen...
2N/A */
2N/A optval = 0;
2N/A (void) setsockopt(sd, level, optname, &optval, sizeof (optval));
2N/A
2N/A if (ret >= 0 && sin != NULL) {
2N/A /*
2N/A * Historical note:
2N/A *
2N/A * Past versions of this bindresvport() code have
2N/A * returned with the reserved port number bound
2N/A * filled in its "sin" parameter (if passed in), perhaps
2N/A * "accidently" because of the structure of historical code.
2N/A *
2N/A * This is not documented but the behavior is
2N/A * explicitly retained here for compatibility to minimize
2N/A * risk to applications, even though it is not clear if this
2N/A * was a design intent.
2N/A */
2N/A len = sizeof (struct sockaddr_in);
2N/A (void) getsockname(sd, (struct sockaddr *)bindaddr, &len);
2N/A }
2N/A return (ret);
2N/A}