bindresvport.c revision 1.19
11.19Schristos/*	$NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos 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.19Schristos__RCSID("$NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos 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.18Sfvdl__weak_alias(bindresvport_sa,_bindresvport_sa)
621.8Sjtc#endif
631.1Scgd
641.1Scgd/*
651.1Scgd * Bind a socket to a privileged IP port
661.1Scgd */
671.6Sjtcint
681.1Scgdbindresvport(sd, sin)
691.1Scgd	int sd;
701.1Scgd	struct sockaddr_in *sin;
711.1Scgd{
721.19Schristos	return bindresvport_sa(sd, (struct sockaddr *)(void *)sin);
731.17Sitojun}
741.17Sitojun
751.17Sitojun/*
761.17Sitojun * Bind a socket to a privileged IP port
771.17Sitojun */
781.17Sitojunint
791.17Sitojunbindresvport_sa(sd, sa)
801.17Sitojun	int sd;
811.17Sitojun	struct sockaddr *sa;
821.17Sitojun{
831.17Sitojun	int error, old;
841.17Sitojun	struct sockaddr_storage myaddr;
851.17Sitojun	struct sockaddr_in *sin;
861.17Sitojun#ifdef INET6
871.17Sitojun	struct sockaddr_in6 *sin6;
881.17Sitojun#endif
891.17Sitojun	int proto, portrange, portlow;
901.17Sitojun	u_int16_t *portp;
911.17Sitojun	socklen_t salen;
921.17Sitojun	int af;
931.17Sitojun
941.17Sitojun	if (sa == NULL) {
951.17Sitojun		salen = sizeof(myaddr);
961.19Schristos		sa = (struct sockaddr *)(void *)&myaddr;
971.17Sitojun
981.17Sitojun		if (getsockname(sd, sa, &salen) == -1)
991.17Sitojun			return -1;	/* errno is correctly set */
1001.17Sitojun
1011.17Sitojun		af = sa->sa_family;
1021.17Sitojun		memset(sa, 0, salen);
1031.17Sitojun	} else
1041.17Sitojun		af = sa->sa_family;
1051.17Sitojun
1061.17Sitojun	switch (af) {
1071.17Sitojun	case AF_INET:
1081.17Sitojun		proto = IPPROTO_IP;
1091.17Sitojun		portrange = IP_PORTRANGE;
1101.17Sitojun		portlow = IP_PORTRANGE_LOW;
1111.19Schristos		sin = (struct sockaddr_in *)(void *)sa;
1121.17Sitojun		salen = sizeof(struct sockaddr_in);
1131.17Sitojun		portp = &sin->sin_port;
1141.17Sitojun		break;
1151.17Sitojun#ifdef INET6
1161.17Sitojun	case AF_INET6:
1171.17Sitojun		proto = IPPROTO_IPV6;
1181.17Sitojun		portrange = IPV6_PORTRANGE;
1191.17Sitojun		portlow = IPV6_PORTRANGE_LOW;
1201.19Schristos		sin6 = (struct sockaddr_in6 *)(void *)sa;
1211.17Sitojun		salen = sizeof(struct sockaddr_in6);
1221.17Sitojun		portp = &sin6->sin6_port;
1231.17Sitojun		break;
1241.17Sitojun#endif
1251.17Sitojun	default:
1261.1Scgd		errno = EPFNOSUPPORT;
1271.1Scgd		return (-1);
1281.1Scgd	}
1291.17Sitojun	sa->sa_family = af;
1301.17Sitojun	sa->sa_len = salen;
1311.9Slukem
1321.17Sitojun	if (*portp == 0) {
1331.15Schristos		socklen_t oldlen = sizeof(old);
1341.9Slukem
1351.17Sitojun		error = getsockopt(sd, proto, portrange, &old, &oldlen);
1361.17Sitojun		if (error < 0)
1371.17Sitojun			return (error);
1381.17Sitojun		error = setsockopt(sd, proto, portrange, &portlow,
1391.17Sitojun		    sizeof(portlow));
1401.17Sitojun		if (error < 0)
1411.17Sitojun			return (error);
1421.1Scgd	}
1431.9Slukem
1441.17Sitojun	error = bind(sd, sa, salen);
1451.9Slukem
1461.17Sitojun	if (*portp == 0) {
1471.9Slukem		int saved_errno = errno;
1481.9Slukem
1491.17Sitojun		if (error < 0) {
1501.17Sitojun			if (setsockopt(sd, proto, portrange, &old,
1511.17Sitojun			    sizeof(old)) < 0)
1521.9Slukem				errno = saved_errno;
1531.17Sitojun			return (error);
1541.9Slukem		}
1551.9Slukem
1561.19Schristos		if (sa != (struct sockaddr *)(void *)&myaddr) {
1571.17Sitojun			/* What did the kernel assign? */
1581.17Sitojun			if (getsockname(sd, sa, &salen) < 0)
1591.9Slukem				errno = saved_errno;
1601.17Sitojun			return (error);
1611.1Scgd		}
1621.1Scgd	}
1631.17Sitojun	return (error);
1641.1Scgd}
165