bindresvport.c revision 1.24
11.24Schristos/* $NetBSD: bindresvport.c,v 1.24 2013/03/05 19:55:22 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.24Schristos__RCSID("$NetBSD: bindresvport.c,v 1.24 2013/03/05 19:55:22 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.24Schristos#include "svc_fdset.h" 591.8Sjtc 601.8Sjtc#ifdef __weak_alias 611.16Smycroft__weak_alias(bindresvport,_bindresvport) 621.18Sfvdl__weak_alias(bindresvport_sa,_bindresvport_sa) 631.8Sjtc#endif 641.1Scgd 651.1Scgd/* 661.1Scgd * Bind a socket to a privileged IP port 671.1Scgd */ 681.6Sjtcint 691.23Smattbindresvport(int sd, struct sockaddr_in *brsin) 701.1Scgd{ 711.20Slukem return bindresvport_sa(sd, (struct sockaddr *)(void *)brsin); 721.17Sitojun} 731.17Sitojun 741.17Sitojun/* 751.17Sitojun * Bind a socket to a privileged IP port 761.17Sitojun */ 771.17Sitojunint 781.23Smattbindresvport_sa(int sd, struct sockaddr *sa) 791.17Sitojun{ 801.17Sitojun int error, old; 811.17Sitojun struct sockaddr_storage myaddr; 821.20Slukem struct sockaddr_in *brsin; 831.17Sitojun#ifdef INET6 841.20Slukem struct sockaddr_in6 *brsin6; 851.17Sitojun#endif 861.17Sitojun int proto, portrange, portlow; 871.17Sitojun u_int16_t *portp; 881.17Sitojun socklen_t salen; 891.17Sitojun int af; 901.17Sitojun 911.17Sitojun if (sa == NULL) { 921.17Sitojun salen = sizeof(myaddr); 931.19Schristos sa = (struct sockaddr *)(void *)&myaddr; 941.17Sitojun 951.17Sitojun if (getsockname(sd, sa, &salen) == -1) 961.17Sitojun return -1; /* errno is correctly set */ 971.17Sitojun 981.17Sitojun af = sa->sa_family; 991.17Sitojun memset(sa, 0, salen); 1001.17Sitojun } else 1011.17Sitojun af = sa->sa_family; 1021.17Sitojun 1031.17Sitojun switch (af) { 1041.17Sitojun case AF_INET: 1051.17Sitojun proto = IPPROTO_IP; 1061.17Sitojun portrange = IP_PORTRANGE; 1071.17Sitojun portlow = IP_PORTRANGE_LOW; 1081.20Slukem brsin = (struct sockaddr_in *)(void *)sa; 1091.17Sitojun salen = sizeof(struct sockaddr_in); 1101.20Slukem portp = &brsin->sin_port; 1111.17Sitojun break; 1121.17Sitojun#ifdef INET6 1131.17Sitojun case AF_INET6: 1141.17Sitojun proto = IPPROTO_IPV6; 1151.17Sitojun portrange = IPV6_PORTRANGE; 1161.17Sitojun portlow = IPV6_PORTRANGE_LOW; 1171.20Slukem brsin6 = (struct sockaddr_in6 *)(void *)sa; 1181.17Sitojun salen = sizeof(struct sockaddr_in6); 1191.20Slukem portp = &brsin6->sin6_port; 1201.17Sitojun break; 1211.17Sitojun#endif 1221.17Sitojun default: 1231.1Scgd errno = EPFNOSUPPORT; 1241.1Scgd return (-1); 1251.1Scgd } 1261.17Sitojun sa->sa_family = af; 1271.17Sitojun sa->sa_len = salen; 1281.9Slukem 1291.17Sitojun if (*portp == 0) { 1301.15Schristos socklen_t oldlen = sizeof(old); 1311.9Slukem 1321.17Sitojun error = getsockopt(sd, proto, portrange, &old, &oldlen); 1331.17Sitojun if (error < 0) 1341.17Sitojun return (error); 1351.17Sitojun error = setsockopt(sd, proto, portrange, &portlow, 1361.22Schristos (socklen_t)sizeof(portlow)); 1371.17Sitojun if (error < 0) 1381.17Sitojun return (error); 1391.1Scgd } 1401.9Slukem 1411.17Sitojun error = bind(sd, sa, salen); 1421.9Slukem 1431.17Sitojun if (*portp == 0) { 1441.9Slukem int saved_errno = errno; 1451.9Slukem 1461.17Sitojun if (error < 0) { 1471.17Sitojun if (setsockopt(sd, proto, portrange, &old, 1481.22Schristos (socklen_t)sizeof(old)) < 0) 1491.9Slukem errno = saved_errno; 1501.17Sitojun return (error); 1511.9Slukem } 1521.9Slukem 1531.19Schristos if (sa != (struct sockaddr *)(void *)&myaddr) { 1541.17Sitojun /* What did the kernel assign? */ 1551.17Sitojun if (getsockname(sd, sa, &salen) < 0) 1561.9Slukem errno = saved_errno; 1571.17Sitojun return (error); 1581.1Scgd } 1591.1Scgd } 1601.17Sitojun return (error); 1611.1Scgd} 162