]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/bindrsvprt.c
iconv, localedef: avoid floating point rounding differences [BZ #24372]
[thirdparty/glibc.git] / sunrpc / bindrsvprt.c
CommitLineData
28f540f4 1/*
a7ab6ec8 2 * Copyright (c) 2010, Oracle America, Inc.
cb636bb2 3 *
a7ab6ec8
UD
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
cb636bb2 7 *
a7ab6ec8
UD
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
c4029823 17 *
a7ab6ec8
UD
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28f540f4
RM
30 */
31
f8afba91 32#include <errno.h>
e7fd8a39
UD
33#include <unistd.h>
34#include <string.h>
28f540f4 35#include <sys/types.h>
28f540f4
RM
36#include <sys/socket.h>
37#include <netinet/in.h>
ec999b8e 38#include <libc-lock.h>
f6da27e5
PH
39
40/*
41 * Locks the static variables in this file.
42 */
43__libc_lock_define_initialized (static, lock);
28f540f4
RM
44
45/*
46 * Bind a socket to a privileged IP port
47 */
e7fd8a39
UD
48int
49bindresvport (int sd, struct sockaddr_in *sin)
28f540f4 50{
e7fd8a39
UD
51 static short port;
52 struct sockaddr_in myaddr;
e7fd8a39 53 int i;
28f540f4
RM
54
55#define STARTPORT 600
8fd2bb35 56#define LOWPORT 512
28f540f4
RM
57#define ENDPORT (IPPORT_RESERVED - 1)
58#define NPORTS (ENDPORT - STARTPORT + 1)
3a0cd663 59 static short startport = STARTPORT;
28f540f4 60
e7fd8a39
UD
61 if (sin == (struct sockaddr_in *) 0)
62 {
63 sin = &myaddr;
d99431e5 64 memset (sin, 0, sizeof (*sin));
e7fd8a39
UD
65 sin->sin_family = AF_INET;
66 }
67 else if (sin->sin_family != AF_INET)
68 {
173b756d 69 __set_errno (EAFNOSUPPORT);
e7fd8a39
UD
70 return -1;
71 }
f8afba91 72
e7fd8a39
UD
73 if (port == 0)
74 {
50304ef0 75 port = (__getpid () % NPORTS) + STARTPORT;
e7fd8a39 76 }
8fd2bb35
UD
77
78 /* Initialize to make gcc happy. */
79 int res = -1;
f8afba91 80
3a0cd663 81 int nports = ENDPORT - startport + 1;
15a493c3 82 int endport = ENDPORT;
f6da27e5
PH
83
84 __libc_lock_lock (lock);
85
3a0cd663 86 again:
8fd2bb35 87 for (i = 0; i < nports; ++i)
e7fd8a39
UD
88 {
89 sin->sin_port = htons (port++);
15a493c3
UD
90 if (port > endport)
91 port = startport;
b2bffca2 92 res = __bind (sd, sin, sizeof (struct sockaddr_in));
8fd2bb35
UD
93 if (res >= 0 || errno != EADDRINUSE)
94 break;
e7fd8a39 95 }
f8afba91 96
3a0cd663
UD
97 if (i == nports && startport != LOWPORT)
98 {
99 startport = LOWPORT;
15a493c3 100 endport = STARTPORT - 1;
3a0cd663 101 nports = STARTPORT - LOWPORT;
15a493c3 102 port = LOWPORT + port % (STARTPORT - LOWPORT);
3a0cd663
UD
103 goto again;
104 }
105
f6da27e5
PH
106 __libc_lock_unlock (lock);
107
e7fd8a39 108 return res;
28f540f4 109}
a585ba22 110libc_hidden_def (bindresvport)