]>
Commit | Line | Data |
---|---|---|
382fe2fa | 1 | /* This file has be substantially modified from the original OpenBSD source */ |
2 | ||
295034ce | 3 | /* $OpenBSD: bindresvport.c,v 1.16 2005/04/01 07:44:03 otto Exp $ */ |
d7d5f0b2 | 4 | |
48e671d5 | 5 | /* |
382fe2fa | 6 | * Copyright 1996, Jason Downs. All rights reserved. |
7 | * Copyright 1998, Theo de Raadt. All rights reserved. | |
8 | * Copyright 2000, Damien Miller. All rights reserved. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
48e671d5 | 29 | */ |
30 | ||
c9d7b187 | 31 | /* OPENBSD ORIGINAL: lib/libc/rpc/bindresvport.c */ |
32 | ||
17962c40 | 33 | #include "includes.h" |
48e671d5 | 34 | |
d7d5f0b2 | 35 | #ifndef HAVE_BINDRESVPORT_SA |
48e671d5 | 36 | |
48e671d5 | 37 | #include "includes.h" |
38 | ||
fec71b2f | 39 | #include <errno.h> |
28cb0a43 | 40 | #include <string.h> |
fec71b2f | 41 | |
48e671d5 | 42 | #define STARTPORT 600 |
43 | #define ENDPORT (IPPORT_RESERVED - 1) | |
44 | #define NPORTS (ENDPORT - STARTPORT + 1) | |
45 | ||
48e671d5 | 46 | /* |
47 | * Bind a socket to a privileged IP port | |
48 | */ | |
49 | int | |
295034ce | 50 | bindresvport_sa(int sd, struct sockaddr *sa) |
48e671d5 | 51 | { |
d7d5f0b2 | 52 | int error, af; |
48e671d5 | 53 | struct sockaddr_storage myaddr; |
54 | struct sockaddr_in *sin; | |
55 | struct sockaddr_in6 *sin6; | |
56 | u_int16_t *portp; | |
1e64903d | 57 | u_int16_t port; |
f1c4659d | 58 | socklen_t salen; |
48e671d5 | 59 | int i; |
60 | ||
61 | if (sa == NULL) { | |
62 | memset(&myaddr, 0, sizeof(myaddr)); | |
63 | sa = (struct sockaddr *)&myaddr; | |
d7d5f0b2 | 64 | |
65 | if (getsockname(sd, sa, &salen) == -1) | |
66 | return -1; /* errno is correctly set */ | |
67 | ||
68 | af = sa->sa_family; | |
69 | memset(&myaddr, 0, salen); | |
70 | } else | |
71 | af = sa->sa_family; | |
48e671d5 | 72 | |
73 | if (af == AF_INET) { | |
74 | sin = (struct sockaddr_in *)sa; | |
75 | salen = sizeof(struct sockaddr_in); | |
76 | portp = &sin->sin_port; | |
77 | } else if (af == AF_INET6) { | |
78 | sin6 = (struct sockaddr_in6 *)sa; | |
79 | salen = sizeof(struct sockaddr_in6); | |
80 | portp = &sin6->sin6_port; | |
81 | } else { | |
82 | errno = EPFNOSUPPORT; | |
83 | return (-1); | |
84 | } | |
85 | sa->sa_family = af; | |
86 | ||
1e64903d | 87 | port = ntohs(*portp); |
88 | if (port == 0) | |
89 | port = (arc4random() % NPORTS) + STARTPORT; | |
48e671d5 | 90 | |
717057b6 | 91 | /* Avoid warning */ |
92 | error = -1; | |
93 | ||
48e671d5 | 94 | for(i = 0; i < NPORTS; i++) { |
1e64903d | 95 | *portp = htons(port); |
96 | ||
48e671d5 | 97 | error = bind(sd, sa, salen); |
d7d5f0b2 | 98 | |
f914c7fb | 99 | /* Terminate on success */ |
100 | if (error == 0) | |
101 | break; | |
102 | ||
103 | /* Terminate on errors, except "address already in use" */ | |
f4a7cf29 | 104 | if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL))) |
48e671d5 | 105 | break; |
106 | ||
1e64903d | 107 | port++; |
108 | if (port > ENDPORT) | |
109 | port = STARTPORT; | |
48e671d5 | 110 | } |
111 | ||
112 | return (error); | |
113 | } | |
114 | ||
d7d5f0b2 | 115 | #endif /* HAVE_BINDRESVPORT_SA */ |