bindresvport.c revision 1.23
11.23Smatt/*	$NetBSD: bindresvport.c,v 1.23 2012/03/20 17:14:50 matt 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.23Smatt__RCSID("$NetBSD: bindresvport.c,v 1.23 2012/03/20 17:14:50 matt 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.23Smattbindresvport(int sd, struct sockaddr_in *brsin)
691.1Scgd{
701.20Slukem	return bindresvport_sa(sd, (struct sockaddr *)(void *)brsin);
711.17Sitojun}
721.17Sitojun
731.17Sitojun/*
741.17Sitojun * Bind a socket to a privileged IP port
751.17Sitojun */
761.17Sitojunint
771.23Smattbindresvport_sa(int sd, struct sockaddr *sa)
781.17Sitojun{
791.17Sitojun	int error, old;
801.17Sitojun	struct sockaddr_storage myaddr;
811.20Slukem	struct sockaddr_in *brsin;
821.17Sitojun#ifdef INET6
831.20Slukem	struct sockaddr_in6 *brsin6;
841.17Sitojun#endif
851.17Sitojun	int proto, portrange, portlow;
861.17Sitojun	u_int16_t *portp;
871.17Sitojun	socklen_t salen;
881.17Sitojun	int af;
891.17Sitojun
901.17Sitojun	if (sa == NULL) {
911.17Sitojun		salen = sizeof(myaddr);
921.19Schristos		sa = (struct sockaddr *)(void *)&myaddr;
931.17Sitojun
941.17Sitojun		if (getsockname(sd, sa, &salen) == -1)
951.17Sitojun			return -1;	/* errno is correctly set */
961.17Sitojun
971.17Sitojun		af = sa->sa_family;
981.17Sitojun		memset(sa, 0, salen);
991.17Sitojun	} else
1001.17Sitojun		af = sa->sa_family;
1011.17Sitojun
1021.17Sitojun	switch (af) {
1031.17Sitojun	case AF_INET:
1041.17Sitojun		proto = IPPROTO_IP;
1051.17Sitojun		portrange = IP_PORTRANGE;
1061.17Sitojun		portlow = IP_PORTRANGE_LOW;
1071.20Slukem		brsin = (struct sockaddr_in *)(void *)sa;
1081.17Sitojun		salen = sizeof(struct sockaddr_in);
1091.20Slukem		portp = &brsin->sin_port;
1101.17Sitojun		break;
1111.17Sitojun#ifdef INET6
1121.17Sitojun	case AF_INET6:
1131.17Sitojun		proto = IPPROTO_IPV6;
1141.17Sitojun		portrange = IPV6_PORTRANGE;
1151.17Sitojun		portlow = IPV6_PORTRANGE_LOW;
1161.20Slukem		brsin6 = (struct sockaddr_in6 *)(void *)sa;
1171.17Sitojun		salen = sizeof(struct sockaddr_in6);
1181.20Slukem		portp = &brsin6->sin6_port;
1191.17Sitojun		break;
1201.17Sitojun#endif
1211.17Sitojun	default:
1221.1Scgd		errno = EPFNOSUPPORT;
1231.1Scgd		return (-1);
1241.1Scgd	}
1251.17Sitojun	sa->sa_family = af;
1261.17Sitojun	sa->sa_len = salen;
1271.9Slukem
1281.17Sitojun	if (*portp == 0) {
1291.15Schristos		socklen_t oldlen = sizeof(old);
1301.9Slukem
1311.17Sitojun		error = getsockopt(sd, proto, portrange, &old, &oldlen);
1321.17Sitojun		if (error < 0)
1331.17Sitojun			return (error);
1341.17Sitojun		error = setsockopt(sd, proto, portrange, &portlow,
1351.22Schristos		    (socklen_t)sizeof(portlow));
1361.17Sitojun		if (error < 0)
1371.17Sitojun			return (error);
1381.1Scgd	}
1391.9Slukem
1401.17Sitojun	error = bind(sd, sa, salen);
1411.9Slukem
1421.17Sitojun	if (*portp == 0) {
1431.9Slukem		int saved_errno = errno;
1441.9Slukem
1451.17Sitojun		if (error < 0) {
1461.17Sitojun			if (setsockopt(sd, proto, portrange, &old,
1471.22Schristos			    (socklen_t)sizeof(old)) < 0)
1481.9Slukem				errno = saved_errno;
1491.17Sitojun			return (error);
1501.9Slukem		}
1511.9Slukem
1521.19Schristos		if (sa != (struct sockaddr *)(void *)&myaddr) {
1531.17Sitojun			/* What did the kernel assign? */
1541.17Sitojun			if (getsockname(sd, sa, &salen) < 0)
1551.9Slukem				errno = saved_errno;
1561.17Sitojun			return (error);
1571.1Scgd		}
1581.1Scgd	}
1591.17Sitojun	return (error);
1601.1Scgd}
161