2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)bindresvport.c 1.8 88/02/08 SMI
30 * @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC
31 * $NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $
32 * $FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.16 2004/10/16 06:11:34 obrien Exp $
33 * $DragonFly: src/lib/libc/rpc/bindresvport.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
37 * Copyright (c) 1987 by Sun Microsystems, Inc.
39 * Portions Copyright(C) 1996, Jason Downs. All rights reserved.
42 #include "namespace.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
55 #include "un-namespace.h"
58 * Bind a socket to a privileged IP port
61 bindresvport(int sd
, struct sockaddr_in
*sin
)
63 return bindresvport_sa(sd
, (struct sockaddr
*)sin
);
67 * Bind a socket to a privileged IP port
70 bindresvport_sa(int sd
, struct sockaddr
*sa
)
73 struct sockaddr_storage myaddr
;
74 struct sockaddr_in
*sin
;
76 struct sockaddr_in6
*sin6
;
78 int proto
, portrange
, portlow
;
83 salen
= sizeof(myaddr
);
84 sa
= (struct sockaddr
*)&myaddr
;
86 if (_getsockname(sd
, sa
, &salen
) == -1)
87 return -1; /* errno is correctly set */
97 portrange
= IP_PORTRANGE
;
98 portlow
= IP_PORTRANGE_LOW
;
99 sin
= (struct sockaddr_in
*)sa
;
100 salen
= sizeof(struct sockaddr_in
);
101 portp
= &sin
->sin_port
;
105 proto
= IPPROTO_IPV6
;
106 portrange
= IPV6_PORTRANGE
;
107 portlow
= IPV6_PORTRANGE_LOW
;
108 sin6
= (struct sockaddr_in6
*)sa
;
109 salen
= sizeof(struct sockaddr_in6
);
110 portp
= &sin6
->sin6_port
;
114 errno
= EPFNOSUPPORT
;
121 socklen_t oldlen
= sizeof(old
);
123 error
= _getsockopt(sd
, proto
, portrange
, &old
, &oldlen
);
127 error
= _setsockopt(sd
, proto
, portrange
, &portlow
,
133 error
= _bind(sd
, sa
, salen
);
136 int saved_errno
= errno
;
139 if (_setsockopt(sd
, proto
, portrange
, &old
,
145 if (sa
!= (struct sockaddr
*)&myaddr
) {
146 /* Hmm, what did the kernel assign? */
147 if (_getsockname(sd
, sa
, &salen
) < 0)