libcr copy: Retarget build paths from ../libc to ../libcr and retarget
[dragonfly.git] / lib / libcr / rpc / bindresvport.c
1 /*
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.
8  *
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.
12  *
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.
16  *
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.
20  *
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.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)bindresvport.c 1.8 88/02/08 SMI
30  * @(#)bindresvport.c   2.2 88/07/29 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.12 2000/01/26 09:02:42 shin Exp $
32  * $DragonFly: src/lib/libcr/rpc/Attic/bindresvport.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
33  */
34
35 /*
36  * Copyright (c) 1987 by Sun Microsystems, Inc.
37  *
38  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
39  */
40
41 #include <sys/types.h>
42 #include <sys/errno.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <unistd.h>
46 #include <string.h>
47
48 /*
49  * Bind a socket to a privileged IP port
50  */
51 int
52 bindresvport(sd, sin)
53         int sd;
54         struct sockaddr_in *sin;
55 {
56         return bindresvport_sa(sd, (struct sockaddr *)sin);
57 }
58
59 /*
60  * Bind a socket to a privileged port for whatever protocol.
61  */
62 int
63 bindresvport_sa(sd, sa)
64         int sd;
65         struct sockaddr *sa;
66 {
67         int old, error, af;
68         struct sockaddr_storage myaddr;
69         struct sockaddr_in *sin;
70         struct sockaddr_in6 *sin6;
71         int proto, portrange, portlow;
72         u_int16_t port;
73         int salen;
74
75         if (sa == NULL) {
76                 salen = sizeof(myaddr);
77                 sa = (struct sockaddr *)&myaddr;
78
79                 if (getsockname(sd, sa, &salen) == -1)
80                         return -1;      /* errno is correctly set */
81
82                 af = sa->sa_family;
83                 memset(&myaddr, 0, salen);
84         } else
85                 af = sa->sa_family;
86
87         if (af == AF_INET) {
88                 proto = IPPROTO_IP;
89                 portrange = IP_PORTRANGE;
90                 portlow = IP_PORTRANGE_LOW;
91                 sin = (struct sockaddr_in *)sa;
92                 salen = sizeof(struct sockaddr_in);
93                 port = sin->sin_port;
94         } else if (af == AF_INET6) {
95                 proto = IPPROTO_IPV6;
96                 portrange = IPV6_PORTRANGE;
97                 portlow = IPV6_PORTRANGE_LOW;
98                 sin6 = (struct sockaddr_in6 *)sa;
99                 salen = sizeof(struct sockaddr_in6);
100                 port = sin6->sin6_port;
101         } else {
102                 errno = EPFNOSUPPORT;
103                 return (-1);
104         }
105         sa->sa_family = af;
106         sa->sa_len = salen;
107
108         if (port == 0) {
109                 int oldlen = sizeof(old);
110
111                 error = getsockopt(sd, proto, portrange, &old, &oldlen);
112                 if (error < 0)
113                         return (error);
114
115                 error = setsockopt(sd, proto, portrange, &portlow,
116                     sizeof(portlow));
117                 if (error < 0)
118                         return (error);
119         }
120
121         error = bind(sd, sa, salen);
122
123         if (port == 0) {
124                 int saved_errno = errno;
125
126                 if (error) {
127                         if (setsockopt(sd, proto, portrange, &old,
128                             sizeof(old)) < 0)
129                                 errno = saved_errno;
130                         return (error);
131                 }
132
133                 if (sa != (struct sockaddr *)&myaddr) {
134                         /* Hmm, what did the kernel assign... */
135                         if (getsockname(sd, sa, &salen) < 0)
136                                 errno = saved_errno;
137                         return (error);
138                 }
139         }
140         return (error);
141 }