Debian Codegraph @

upstream/debian-unstable/man3/bindresvport.3

raw
1 '\" t
2 .\" Copyright 2007, Michael Kerrisk <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH bindresvport 3 2026-02-08 "Linux man-pages 6.18"
8 .SH NAME
9 bindresvport \- bind a socket to a privileged IP port
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <sys/types.h>
16 .B #include <netinet/in.h>
17 .P
18 .BI "int bindresvport(int " sockfd ", struct sockaddr_in *" sin );
19 .fi
20 .SH DESCRIPTION
21 .BR bindresvport ()
22 is used to bind the socket referred to by the
23 file descriptor
24 .I sockfd
25 to a privileged anonymous IP port,
26 that is, a port number arbitrarily selected from the range 512 to 1023.
27 .\" glibc actually starts searching with a port # in the range 600 to 1023
28 .P
29 If the
30 .BR bind (2)
31 performed by
32 .BR bindresvport ()
33 is successful, and
34 .I sin
35 is not NULL, then
36 .I sin\->sin_port
37 returns the port number actually allocated.
38 .P
39 .I sin
40 can be NULL, in which case
41 .I sin\->sin_family
42 is implicitly taken to be
43 .BR AF_INET .
44 However, in this case,
45 .BR bindresvport ()
46 has no way to return the port number actually allocated.
47 (This information can later be obtained using
48 .BR getsockname (2).)
49 .SH RETURN VALUE
50 .BR bindresvport ()
51 returns 0 on success;
52 otherwise \-1 is returned and
53 .I errno
54 is set to indicate the error.
55 .SH ERRORS
56 .BR bindresvport ()
57 can fail for any of the same reasons as
58 .BR bind (2).
59 In addition, the following errors may occur:
60 .TP
61 .B EACCES
62 The calling process was not privileged
63 (on Linux: the calling process did not have the
64 .B CAP_NET_BIND_SERVICE
65 capability in the user namespace governing its network namespace).
66 .TP
67 .B EADDRINUSE
68 All privileged ports are in use.
69 .TP
70 .BR EAFNOSUPPORT " (" EPFNOSUPPORT " in glibc 2.7 and earlier)"
71 .I sin
72 is not NULL and
73 .I sin\->sin_family
74 is not
75 .BR AF_INET .
76 .SH ATTRIBUTES
77 For an explanation of the terms used in this section, see
78 .BR attributes (7).
79 .TS
80 allbox;
81 lb lb lbx
82 l l l.
83 Interface Attribute Value
84 T{
85 .na
86 .nh
87 .BR bindresvport ()
88 T} Thread safety T{
89 .na
90 .nh
91 glibc\ >=\ 2.17: MT-Safe;
92 .\" commit f6da27e53695ad1cc0e2a9490358decbbfdff5e5
93 glibc\ <\ 2.17: MT-Unsafe
94 T}
95 .TE
96 .P
97 The
98 .BR bindresvport ()
99 function uses a static variable that was not protected by a lock
100 before glibc 2.17, rendering the function MT-Unsafe.
101 .SH VERSIONS
102 Present on the BSDs, Solaris, and many other systems.
103 .SH NOTES
104 Unlike some
105 .BR bindresvport ()
106 implementations,
107 the glibc implementation ignores any value that the caller supplies in
108 .IR sin\->sin_port .
109 .SH STANDARDS
110 BSD.
111 .SH SEE ALSO
112 .BR bind (2),
113 .BR getsockname (2)