Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 1 | /* This file has be modified from the original OpenBSD source */ |
| 2 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 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 | |
| 32 | #include "config.h" |
| 33 | |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 34 | #ifndef HAVE_BINDRESVPORT_SA |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 35 | |
| 36 | #if defined(LIBC_SCCS) && !defined(lint) |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 37 | static char *rcsid = "$OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $"; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 38 | #endif /* LIBC_SCCS and not lint */ |
| 39 | |
| 40 | /* |
| 41 | * Copyright (c) 1987 by Sun Microsystems, Inc. |
| 42 | * |
| 43 | * Portions Copyright(C) 1996, Jason Downs. All rights reserved. |
| 44 | */ |
| 45 | |
| 46 | #include "includes.h" |
| 47 | |
| 48 | #define STARTPORT 600 |
| 49 | #define ENDPORT (IPPORT_RESERVED - 1) |
| 50 | #define NPORTS (ENDPORT - STARTPORT + 1) |
| 51 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 52 | /* |
| 53 | * Bind a socket to a privileged IP port |
| 54 | */ |
| 55 | int |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 56 | bindresvport_sa(sd, sa) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 57 | int sd; |
| 58 | struct sockaddr *sa; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 59 | { |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 60 | int error, af; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 61 | struct sockaddr_storage myaddr; |
| 62 | struct sockaddr_in *sin; |
| 63 | struct sockaddr_in6 *sin6; |
| 64 | u_int16_t *portp; |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 65 | u_int16_t port; |
Ben Lindstrom | 0d5af60 | 2001-01-09 00:50:29 +0000 | [diff] [blame] | 66 | socklen_t salen; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 67 | int i; |
| 68 | |
| 69 | if (sa == NULL) { |
| 70 | memset(&myaddr, 0, sizeof(myaddr)); |
| 71 | sa = (struct sockaddr *)&myaddr; |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 72 | |
| 73 | if (getsockname(sd, sa, &salen) == -1) |
| 74 | return -1; /* errno is correctly set */ |
| 75 | |
| 76 | af = sa->sa_family; |
| 77 | memset(&myaddr, 0, salen); |
| 78 | } else |
| 79 | af = sa->sa_family; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 80 | |
| 81 | if (af == AF_INET) { |
| 82 | sin = (struct sockaddr_in *)sa; |
| 83 | salen = sizeof(struct sockaddr_in); |
| 84 | portp = &sin->sin_port; |
| 85 | } else if (af == AF_INET6) { |
| 86 | sin6 = (struct sockaddr_in6 *)sa; |
| 87 | salen = sizeof(struct sockaddr_in6); |
| 88 | portp = &sin6->sin6_port; |
| 89 | } else { |
| 90 | errno = EPFNOSUPPORT; |
| 91 | return (-1); |
| 92 | } |
| 93 | sa->sa_family = af; |
| 94 | |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 95 | port = ntohs(*portp); |
| 96 | if (port == 0) |
| 97 | port = (arc4random() % NPORTS) + STARTPORT; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 98 | |
Damien Miller | a64b57a | 2001-01-17 10:44:13 +1100 | [diff] [blame] | 99 | /* Avoid warning */ |
| 100 | error = -1; |
| 101 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 102 | for(i = 0; i < NPORTS; i++) { |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 103 | *portp = htons(port); |
| 104 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 105 | error = bind(sd, sa, salen); |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 106 | |
Damien Miller | b9b94a7 | 2000-01-17 09:52:46 +1100 | [diff] [blame] | 107 | /* Terminate on success */ |
| 108 | if (error == 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 109 | break; |
| 110 | |
Damien Miller | b9b94a7 | 2000-01-17 09:52:46 +1100 | [diff] [blame] | 111 | /* Terminate on errors, except "address already in use" */ |
Damien Miller | 19fe9c7 | 2000-01-17 15:23:01 +1100 | [diff] [blame] | 112 | if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL))) |
Damien Miller | b9b94a7 | 2000-01-17 09:52:46 +1100 | [diff] [blame] | 113 | break; |
| 114 | |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 115 | port++; |
| 116 | if (port > ENDPORT) |
| 117 | port = STARTPORT; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return (error); |
| 121 | } |
| 122 | |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 123 | #endif /* HAVE_BINDRESVPORT_SA */ |