Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / openssh / openbsd-compat / bindresvport.c
1 /* This file has be modified from the original OpenBSD source */
2
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  * 
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  * 
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  * 
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  * 
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  * 
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  *
31  * $OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $
32  */
33
34 #include "includes.h"
35
36 #ifndef HAVE_BINDRESVPORT_SA
37
38 /*
39  * Copyright (c) 1987 by Sun Microsystems, Inc.
40  *
41  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
42  */
43
44 #include "includes.h"
45
46 #define STARTPORT 600
47 #define ENDPORT (IPPORT_RESERVED - 1)
48 #define NPORTS  (ENDPORT - STARTPORT + 1)
49
50 /*
51  * Bind a socket to a privileged IP port
52  */
53 int
54 bindresvport_sa(sd, sa)
55         int sd;
56         struct sockaddr *sa;
57 {
58         int error, af;
59         struct sockaddr_storage myaddr;
60         struct sockaddr_in *sin;
61         struct sockaddr_in6 *sin6;
62         u_int16_t *portp;
63         u_int16_t port;
64         socklen_t salen;
65         int i;
66
67         if (sa == NULL) {
68                 memset(&myaddr, 0, sizeof(myaddr));
69                 sa = (struct sockaddr *)&myaddr;
70
71                 if (getsockname(sd, sa, &salen) == -1)
72                         return -1;      /* errno is correctly set */
73
74                 af = sa->sa_family;
75                 memset(&myaddr, 0, salen);
76         } else
77                 af = sa->sa_family;
78
79         if (af == AF_INET) {
80                 sin = (struct sockaddr_in *)sa;
81                 salen = sizeof(struct sockaddr_in);
82                 portp = &sin->sin_port;
83         } else if (af == AF_INET6) {
84                 sin6 = (struct sockaddr_in6 *)sa;
85                 salen = sizeof(struct sockaddr_in6);
86                 portp = &sin6->sin6_port;
87         } else {
88                 errno = EPFNOSUPPORT;
89                 return (-1);
90         }
91         sa->sa_family = af;
92
93         port = ntohs(*portp);
94         if (port == 0)
95                 port = (arc4random() % NPORTS) + STARTPORT;
96
97         /* Avoid warning */
98         error = -1;
99
100         for(i = 0; i < NPORTS; i++) {
101                 *portp = htons(port);
102                 
103                 error = bind(sd, sa, salen);
104
105                 /* Terminate on success */
106                 if (error == 0)
107                         break;
108                         
109                 /* Terminate on errors, except "address already in use" */
110                 if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
111                         break;
112                         
113                 port++;
114                 if (port > ENDPORT)
115                         port = STARTPORT;
116         }
117
118         return (error);
119 }
120
121 #endif /* HAVE_BINDRESVPORT_SA */