]>
Commit | Line | Data |
---|---|---|
382fe2fa | 1 | /* This file has be substantially modified from the original OpenBSD source */ |
2 | ||
3 | /* $OpenBSD: bindresvport.c,v 1.15 2003/05/20 22:42:35 deraadt 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 | ||
17962c40 | 31 | #include "includes.h" |
48e671d5 | 32 | |
d7d5f0b2 | 33 | #ifndef HAVE_BINDRESVPORT_SA |
48e671d5 | 34 | |
48e671d5 | 35 | #include "includes.h" |
36 | ||
37 | #define STARTPORT 600 | |
38 | #define ENDPORT (IPPORT_RESERVED - 1) | |
39 | #define NPORTS (ENDPORT - STARTPORT + 1) | |
40 | ||
48e671d5 | 41 | /* |
42 | * Bind a socket to a privileged IP port | |
43 | */ | |
44 | int | |
d7d5f0b2 | 45 | bindresvport_sa(sd, sa) |
48e671d5 | 46 | int sd; |
47 | struct sockaddr *sa; | |
48e671d5 | 48 | { |
d7d5f0b2 | 49 | int error, af; |
48e671d5 | 50 | struct sockaddr_storage myaddr; |
51 | struct sockaddr_in *sin; | |
52 | struct sockaddr_in6 *sin6; | |
53 | u_int16_t *portp; | |
1e64903d | 54 | u_int16_t port; |
f1c4659d | 55 | socklen_t salen; |
48e671d5 | 56 | int i; |
57 | ||
58 | if (sa == NULL) { | |
59 | memset(&myaddr, 0, sizeof(myaddr)); | |
60 | sa = (struct sockaddr *)&myaddr; | |
d7d5f0b2 | 61 | |
62 | if (getsockname(sd, sa, &salen) == -1) | |
63 | return -1; /* errno is correctly set */ | |
64 | ||
65 | af = sa->sa_family; | |
66 | memset(&myaddr, 0, salen); | |
67 | } else | |
68 | af = sa->sa_family; | |
48e671d5 | 69 | |
70 | if (af == AF_INET) { | |
71 | sin = (struct sockaddr_in *)sa; | |
72 | salen = sizeof(struct sockaddr_in); | |
73 | portp = &sin->sin_port; | |
74 | } else if (af == AF_INET6) { | |
75 | sin6 = (struct sockaddr_in6 *)sa; | |
76 | salen = sizeof(struct sockaddr_in6); | |
77 | portp = &sin6->sin6_port; | |
78 | } else { | |
79 | errno = EPFNOSUPPORT; | |
80 | return (-1); | |
81 | } | |
82 | sa->sa_family = af; | |
83 | ||
1e64903d | 84 | port = ntohs(*portp); |
85 | if (port == 0) | |
86 | port = (arc4random() % NPORTS) + STARTPORT; | |
48e671d5 | 87 | |
717057b6 | 88 | /* Avoid warning */ |
89 | error = -1; | |
90 | ||
48e671d5 | 91 | for(i = 0; i < NPORTS; i++) { |
1e64903d | 92 | *portp = htons(port); |
93 | ||
48e671d5 | 94 | error = bind(sd, sa, salen); |
d7d5f0b2 | 95 | |
f914c7fb | 96 | /* Terminate on success */ |
97 | if (error == 0) | |
98 | break; | |
99 | ||
100 | /* Terminate on errors, except "address already in use" */ | |
f4a7cf29 | 101 | if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL))) |
48e671d5 | 102 | break; |
103 | ||
1e64903d | 104 | port++; |
105 | if (port > ENDPORT) | |
106 | port = STARTPORT; | |
48e671d5 | 107 | } |
108 | ||
109 | return (error); | |
110 | } | |
111 | ||
d7d5f0b2 | 112 | #endif /* HAVE_BINDRESVPORT_SA */ |