bindresvport.c revision 1.17
11.17Sitojun/*	$NetBSD: bindresvport.c,v 1.17 2000/01/26 13:20:25 itojun Exp $	*/
21.3Scgd
31.1Scgd/*
41.1Scgd * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
51.1Scgd * unrestricted use provided that this legend is included on all tape
61.1Scgd * media and as a part of the software program in whole or part.  Users
71.1Scgd * may copy or modify Sun RPC without charge, but are not authorized
81.1Scgd * to license or distribute it to anyone else except as part of a product or
91.1Scgd * program developed by the user.
101.1Scgd *
111.1Scgd * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
121.1Scgd * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
131.1Scgd * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
141.1Scgd *
151.1Scgd * Sun RPC is provided with no support and without any obligation on the
161.1Scgd * part of Sun Microsystems, Inc. to assist in its use, correction,
171.1Scgd * modification or enhancement.
181.1Scgd *
191.1Scgd * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
201.1Scgd * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
211.1Scgd * OR ANY PART THEREOF.
221.1Scgd *
231.1Scgd * In no event will Sun Microsystems, Inc. be liable for any lost revenue
241.1Scgd * or profits or other special, indirect and consequential damages, even if
251.1Scgd * Sun has been advised of the possibility of such damages.
261.1Scgd *
271.1Scgd * Sun Microsystems, Inc.
281.1Scgd * 2550 Garcia Avenue
291.1Scgd * Mountain View, California  94043
301.1Scgd */
311.1Scgd
321.7Schristos#include <sys/cdefs.h>
331.1Scgd#if defined(LIBC_SCCS) && !defined(lint)
341.7Schristos#if 0
351.7Schristosstatic char *sccsid = "@(#)bindresvport.c 1.8 88/02/08 SMI";
361.7Schristosstatic char *sccsid = "@(#)bindresvport.c	2.2 88/07/29 4.0 RPCSRC";
371.7Schristos#else
381.17Sitojun__RCSID("$NetBSD: bindresvport.c,v 1.17 2000/01/26 13:20:25 itojun Exp $");
391.7Schristos#endif
401.1Scgd#endif
411.1Scgd
421.1Scgd/*
431.1Scgd * Copyright (c) 1987 by Sun Microsystems, Inc.
441.1Scgd */
451.1Scgd
461.8Sjtc#include "namespace.h"
471.12Slukem
481.1Scgd#include <sys/types.h>
491.1Scgd#include <sys/socket.h>
501.12Slukem
511.1Scgd#include <netinet/in.h>
521.12Slukem
531.12Slukem#include <errno.h>
541.12Slukem#include <string.h>
551.12Slukem#include <unistd.h>
561.12Slukem
571.7Schristos#include <rpc/rpc.h>
581.8Sjtc
591.8Sjtc#ifdef __weak_alias
601.16Smycroft__weak_alias(bindresvport,_bindresvport)
611.8Sjtc#endif
621.1Scgd
631.1Scgd/*
641.1Scgd * Bind a socket to a privileged IP port
651.1Scgd */
661.6Sjtcint
671.1Scgdbindresvport(sd, sin)
681.1Scgd	int sd;
691.1Scgd	struct sockaddr_in *sin;
701.1Scgd{
711.17Sitojun	return bindresvport_sa(sd, (struct sockaddr *)sin);
721.17Sitojun}
731.17Sitojun
741.17Sitojun/*
751.17Sitojun * Bind a socket to a privileged IP port
761.17Sitojun */
771.17Sitojunint
781.17Sitojunbindresvport_sa(sd, sa)
791.17Sitojun	int sd;
801.17Sitojun	struct sockaddr *sa;
811.17Sitojun{
821.17Sitojun	int error, old;
831.17Sitojun	struct sockaddr_storage myaddr;
841.17Sitojun	struct sockaddr_in *sin;
851.17Sitojun#ifdef INET6
861.17Sitojun	struct sockaddr_in6 *sin6;
871.17Sitojun#endif
881.17Sitojun	int proto, portrange, portlow;
891.17Sitojun	u_int16_t *portp;
901.17Sitojun	socklen_t salen;
911.17Sitojun	int af;
921.17Sitojun
931.17Sitojun	if (sa == NULL) {
941.17Sitojun		salen = sizeof(myaddr);
951.17Sitojun		sa = (struct sockaddr *)&myaddr;
961.17Sitojun
971.17Sitojun		if (getsockname(sd, sa, &salen) == -1)
981.17Sitojun			return -1;	/* errno is correctly set */
991.17Sitojun
1001.17Sitojun		af = sa->sa_family;
1011.17Sitojun		memset(sa, 0, salen);
1021.17Sitojun	} else
1031.17Sitojun		af = sa->sa_family;
1041.17Sitojun
1051.17Sitojun	switch (af) {
1061.17Sitojun	case AF_INET:
1071.17Sitojun		proto = IPPROTO_IP;
1081.17Sitojun		portrange = IP_PORTRANGE;
1091.17Sitojun		portlow = IP_PORTRANGE_LOW;
1101.17Sitojun		sin = (struct sockaddr_in *)sa;
1111.17Sitojun		salen = sizeof(struct sockaddr_in);
1121.17Sitojun		portp = &sin->sin_port;
1131.17Sitojun		break;
1141.17Sitojun#ifdef INET6
1151.17Sitojun	case AF_INET6:
1161.17Sitojun		proto = IPPROTO_IPV6;
1171.17Sitojun		portrange = IPV6_PORTRANGE;
1181.17Sitojun		portlow = IPV6_PORTRANGE_LOW;
1191.17Sitojun		sin6 = (struct sockaddr_in6 *)sa;
1201.17Sitojun		salen = sizeof(struct sockaddr_in6);
1211.17Sitojun		portp = &sin6->sin6_port;
1221.17Sitojun		break;
1231.17Sitojun#endif
1241.17Sitojun	default:
1251.1Scgd		errno = EPFNOSUPPORT;
1261.1Scgd		return (-1);
1271.1Scgd	}
1281.17Sitojun	sa->sa_family = af;
1291.17Sitojun	sa->sa_len = salen;
1301.9Slukem
1311.17Sitojun	if (*portp == 0) {
1321.15Schristos		socklen_t oldlen = sizeof(old);
1331.9Slukem
1341.17Sitojun		error = getsockopt(sd, proto, portrange, &old, &oldlen);
1351.17Sitojun		if (error < 0)
1361.17Sitojun			return (error);
1371.17Sitojun		error = setsockopt(sd, proto, portrange, &portlow,
1381.17Sitojun		    sizeof(portlow));
1391.17Sitojun		if (error < 0)
1401.17Sitojun			return (error);
1411.1Scgd	}
1421.9Slukem
1431.17Sitojun	error = bind(sd, sa, salen);
1441.9Slukem
1451.17Sitojun	if (*portp == 0) {
1461.9Slukem		int saved_errno = errno;
1471.9Slukem
1481.17Sitojun		if (error < 0) {
1491.17Sitojun			if (setsockopt(sd, proto, portrange, &old,
1501.17Sitojun			    sizeof(old)) < 0)
1511.9Slukem				errno = saved_errno;
1521.17Sitojun			return (error);
1531.9Slukem		}
1541.9Slukem
1551.17Sitojun		if (sa != (struct sockaddr *)&myaddr) {
1561.17Sitojun			/* What did the kernel assign? */
1571.17Sitojun			if (getsockname(sd, sa, &salen) < 0)
1581.9Slukem				errno = saved_errno;
1591.17Sitojun			return (error);
1601.1Scgd		}
1611.1Scgd	}
1621.17Sitojun	return (error);
1631.1Scgd}
164