1*84d9c625SLionel Sambuc /* $NetBSD: bindresvport.c,v 1.25 2013/03/11 20:19:28 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)bindresvport.c 1.8 88/02/08 SMI";
382fe8fb19SBen Gras static char *sccsid = "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: bindresvport.c,v 1.25 2013/03/11 20:19:28 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * Copyright (c) 1987 by Sun Microsystems, Inc.
462fe8fb19SBen Gras */
472fe8fb19SBen Gras
482fe8fb19SBen Gras #include "namespace.h"
492fe8fb19SBen Gras
502fe8fb19SBen Gras #include <sys/types.h>
512fe8fb19SBen Gras #include <sys/socket.h>
522fe8fb19SBen Gras
532fe8fb19SBen Gras #include <netinet/in.h>
542fe8fb19SBen Gras
552fe8fb19SBen Gras #include <errno.h>
562fe8fb19SBen Gras #include <string.h>
572fe8fb19SBen Gras #include <unistd.h>
582fe8fb19SBen Gras
592fe8fb19SBen Gras #include <rpc/rpc.h>
60*84d9c625SLionel Sambuc #include "svc_fdset.h"
612fe8fb19SBen Gras
622fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(bindresvport,_bindresvport)632fe8fb19SBen Gras __weak_alias(bindresvport,_bindresvport)
642fe8fb19SBen Gras __weak_alias(bindresvport_sa,_bindresvport_sa)
652fe8fb19SBen Gras #endif
662fe8fb19SBen Gras
672fe8fb19SBen Gras /*
682fe8fb19SBen Gras * Bind a socket to a privileged IP port
692fe8fb19SBen Gras */
702fe8fb19SBen Gras int
71f14fb602SLionel Sambuc bindresvport(int sd, struct sockaddr_in *brsin)
722fe8fb19SBen Gras {
732fe8fb19SBen Gras return bindresvport_sa(sd, (struct sockaddr *)(void *)brsin);
742fe8fb19SBen Gras }
752fe8fb19SBen Gras
762fe8fb19SBen Gras /*
772fe8fb19SBen Gras * Bind a socket to a privileged IP port
782fe8fb19SBen Gras */
792fe8fb19SBen Gras int
bindresvport_sa(int sd,struct sockaddr * sa)80f14fb602SLionel Sambuc bindresvport_sa(int sd, struct sockaddr *sa)
812fe8fb19SBen Gras {
822fe8fb19SBen Gras int error, old;
832fe8fb19SBen Gras struct sockaddr_storage myaddr;
842fe8fb19SBen Gras struct sockaddr_in *brsin;
852fe8fb19SBen Gras #ifdef INET6
862fe8fb19SBen Gras struct sockaddr_in6 *brsin6;
872fe8fb19SBen Gras #endif
882fe8fb19SBen Gras int proto, portrange, portlow;
892fe8fb19SBen Gras u_int16_t *portp;
902fe8fb19SBen Gras socklen_t salen;
912fe8fb19SBen Gras int af;
922fe8fb19SBen Gras
932fe8fb19SBen Gras if (sa == NULL) {
942fe8fb19SBen Gras salen = sizeof(myaddr);
952fe8fb19SBen Gras sa = (struct sockaddr *)(void *)&myaddr;
962fe8fb19SBen Gras
972fe8fb19SBen Gras if (getsockname(sd, sa, &salen) == -1)
982fe8fb19SBen Gras return -1; /* errno is correctly set */
992fe8fb19SBen Gras
1002fe8fb19SBen Gras af = sa->sa_family;
1012fe8fb19SBen Gras memset(sa, 0, salen);
1022fe8fb19SBen Gras } else
1032fe8fb19SBen Gras af = sa->sa_family;
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras switch (af) {
1062fe8fb19SBen Gras case AF_INET:
1072fe8fb19SBen Gras proto = IPPROTO_IP;
1082fe8fb19SBen Gras portrange = IP_PORTRANGE;
1092fe8fb19SBen Gras portlow = IP_PORTRANGE_LOW;
1102fe8fb19SBen Gras brsin = (struct sockaddr_in *)(void *)sa;
1112fe8fb19SBen Gras salen = sizeof(struct sockaddr_in);
1122fe8fb19SBen Gras portp = &brsin->sin_port;
1132fe8fb19SBen Gras break;
1142fe8fb19SBen Gras #ifdef INET6
1152fe8fb19SBen Gras case AF_INET6:
1162fe8fb19SBen Gras proto = IPPROTO_IPV6;
1172fe8fb19SBen Gras portrange = IPV6_PORTRANGE;
1182fe8fb19SBen Gras portlow = IPV6_PORTRANGE_LOW;
1192fe8fb19SBen Gras brsin6 = (struct sockaddr_in6 *)(void *)sa;
1202fe8fb19SBen Gras salen = sizeof(struct sockaddr_in6);
1212fe8fb19SBen Gras portp = &brsin6->sin6_port;
1222fe8fb19SBen Gras break;
1232fe8fb19SBen Gras #endif
1242fe8fb19SBen Gras default:
1252fe8fb19SBen Gras errno = EPFNOSUPPORT;
1262fe8fb19SBen Gras return (-1);
1272fe8fb19SBen Gras }
1282fe8fb19SBen Gras sa->sa_family = af;
1292fe8fb19SBen Gras sa->sa_len = salen;
1302fe8fb19SBen Gras
1312fe8fb19SBen Gras if (*portp == 0) {
1322fe8fb19SBen Gras socklen_t oldlen = sizeof(old);
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras error = getsockopt(sd, proto, portrange, &old, &oldlen);
1352fe8fb19SBen Gras if (error < 0)
1362fe8fb19SBen Gras return (error);
1372fe8fb19SBen Gras error = setsockopt(sd, proto, portrange, &portlow,
138f14fb602SLionel Sambuc (socklen_t)sizeof(portlow));
1392fe8fb19SBen Gras if (error < 0)
1402fe8fb19SBen Gras return (error);
1412fe8fb19SBen Gras }
1422fe8fb19SBen Gras
1432fe8fb19SBen Gras error = bind(sd, sa, salen);
1442fe8fb19SBen Gras
1452fe8fb19SBen Gras if (*portp == 0) {
1462fe8fb19SBen Gras int saved_errno = errno;
1472fe8fb19SBen Gras
1482fe8fb19SBen Gras if (error < 0) {
1492fe8fb19SBen Gras if (setsockopt(sd, proto, portrange, &old,
150f14fb602SLionel Sambuc (socklen_t)sizeof(old)) < 0)
1512fe8fb19SBen Gras errno = saved_errno;
1522fe8fb19SBen Gras return (error);
1532fe8fb19SBen Gras }
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras if (sa != (struct sockaddr *)(void *)&myaddr) {
1562fe8fb19SBen Gras /* What did the kernel assign? */
1572fe8fb19SBen Gras if (getsockname(sd, sa, &salen) < 0)
1582fe8fb19SBen Gras errno = saved_errno;
1592fe8fb19SBen Gras return (error);
1602fe8fb19SBen Gras }
1612fe8fb19SBen Gras }
1622fe8fb19SBen Gras return (error);
1632fe8fb19SBen Gras }
164