1769 dladm show-link truncates OVER field
[unleashed.git] / usr / src / cmd / ssh / libopenbsd-compat / common / bindresvport.c
blob8432233bb63ed4fdf04cc1d1cc233665c8567f8a
1 /* This file has be modified from the original OpenBSD source */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
32 #include "includes.h"
34 #ifndef HAVE_BINDRESVPORT_SA
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *rcsid = "$OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $";
38 #endif /* LIBC_SCCS and not lint */
41 * Copyright (c) 1987 by Sun Microsystems, Inc.
43 * Portions Copyright(C) 1996, Jason Downs. All rights reserved.
46 #include "includes.h"
48 #define STARTPORT 600
49 #define ENDPORT (IPPORT_RESERVED - 1)
50 #define NPORTS (ENDPORT - STARTPORT + 1)
53 * Bind a socket to a privileged IP port
55 int
56 bindresvport_sa(sd, sa)
57 int sd;
58 struct sockaddr *sa;
60 int error, af;
61 struct sockaddr_storage myaddr;
62 struct sockaddr_in *sin;
63 struct sockaddr_in6 *sin6;
64 u_int16_t *portp;
65 u_int16_t port;
66 socklen_t salen;
67 int i;
69 if (sa == NULL) {
70 memset(&myaddr, 0, sizeof(myaddr));
71 sa = (struct sockaddr *)&myaddr;
73 if (getsockname(sd, sa, &salen) == -1)
74 return -1; /* errno is correctly set */
76 af = sa->sa_family;
77 memset(&myaddr, 0, salen);
78 } else
79 af = sa->sa_family;
81 if (af == AF_INET) {
82 /* LINTED */
83 sin = (struct sockaddr_in *)sa;
84 salen = sizeof(struct sockaddr_in);
85 portp = &sin->sin_port;
86 } else if (af == AF_INET6) {
87 /* LINTED */
88 sin6 = (struct sockaddr_in6 *)sa;
89 salen = sizeof(struct sockaddr_in6);
90 portp = &sin6->sin6_port;
91 } else {
92 errno = EPFNOSUPPORT;
93 return (-1);
95 sa->sa_family = af;
97 port = ntohs(*portp);
98 if (port == 0)
99 port = (arc4random() % NPORTS) + STARTPORT;
101 /* Avoid warning */
102 error = -1;
104 for(i = 0; i < NPORTS; i++) {
105 *portp = htons(port);
107 error = bind(sd, sa, salen);
109 /* Terminate on success */
110 if (error == 0)
111 break;
113 /* Terminate on errors, except "address already in use" */
114 if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
115 break;
117 port++;
118 if (port > ENDPORT)
119 port = STARTPORT;
122 return (error);
125 #endif /* HAVE_BINDRESVPORT_SA */
127 #pragma ident "%Z%%M% %I% %E% SMI"