bindresvport.c revision 1.12
11.12Slukem/*	$NetBSD: bindresvport.c,v 1.12 1998/02/13 05:52:14 lukem 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.12Slukem__RCSID("$NetBSD: bindresvport.c,v 1.12 1998/02/13 05:52:14 lukem 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.8Sjtc__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.9Slukem	int res, old;
721.1Scgd	struct sockaddr_in myaddr;
731.9Slukem	int sinlen = sizeof(struct sockaddr_in);
741.1Scgd
751.9Slukem	if (sin == NULL) {
761.1Scgd		sin = &myaddr;
771.9Slukem		memset(sin, 0, sinlen);
781.9Slukem		sin->sin_len = sinlen;
791.1Scgd		sin->sin_family = AF_INET;
801.1Scgd	} else if (sin->sin_family != AF_INET) {
811.1Scgd		errno = EPFNOSUPPORT;
821.1Scgd		return (-1);
831.1Scgd	}
841.9Slukem
851.9Slukem	if (sin->sin_port == 0) {
861.9Slukem		int on, oldlen = sizeof(old);
871.9Slukem
881.9Slukem		res = getsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &old, &oldlen);
891.9Slukem		if (res < 0)
901.9Slukem			return(res);
911.9Slukem		on = IP_PORTRANGE_LOW;
921.9Slukem		res = setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on));
931.9Slukem		if (res < 0)
941.9Slukem			return(res);
951.1Scgd	}
961.9Slukem
971.9Slukem	res = bind(sd, (struct sockaddr *)sin, sinlen);
981.9Slukem
991.9Slukem	if (sin->sin_port == 0) {
1001.9Slukem		int saved_errno = errno;
1011.9Slukem
1021.9Slukem		if (res < 0) {
1031.9Slukem			if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
1041.9Slukem			    &old, sizeof(old)) < 0)
1051.9Slukem				errno = saved_errno;
1061.9Slukem			return (res);
1071.9Slukem		}
1081.9Slukem
1091.9Slukem		if (sin != &myaddr) {	/* What did the kernel assign? */
1101.9Slukem			if (getsockname(sd, (struct sockaddr *)sin, &sinlen)
1111.9Slukem			    < 0)
1121.9Slukem				errno = saved_errno;
1131.9Slukem			return (res);
1141.1Scgd		}
1151.1Scgd	}
1161.1Scgd	return (res);
1171.1Scgd}
118