1BIND(2) Linux Programmer's Manual BIND(2)
2
3
4
6 bind - bind a name to a socket
7
9 #include <sys/types.h> /* See NOTES */
10 #include <sys/socket.h>
11
12 int bind(int sockfd, const struct sockaddr *addr,
13 socklen_t addrlen);
14
16 When a socket is created with socket(2), it exists in a name space (ad‐
17 dress family) but has no address assigned to it. bind() assigns the
18 address specified by addr to the socket referred to by the file de‐
19 scriptor sockfd. addrlen specifies the size, in bytes, of the address
20 structure pointed to by addr. Traditionally, this operation is called
21 “assigning a name to a socket”.
22
23 It is normally necessary to assign a local address using bind() before
24 a SOCK_STREAM socket may receive connections (see accept(2)).
25
26 The rules used in name binding vary between address families. Consult
27 the manual entries in Section 7 for detailed information. For AF_INET,
28 see ip(7); for AF_INET6, see ipv6(7); for AF_UNIX, see unix(7); for
29 AF_APPLETALK, see ddp(7); for AF_PACKET, see packet(7); for AF_X25, see
30 x25(7); and for AF_NETLINK, see netlink(7).
31
32 The actual structure passed for the addr argument will depend on the
33 address family. The sockaddr structure is defined as something like:
34
35 struct sockaddr {
36 sa_family_t sa_family;
37 char sa_data[14];
38 }
39
40 The only purpose of this structure is to cast the structure pointer
41 passed in addr in order to avoid compiler warnings. See EXAMPLES be‐
42 low.
43
45 On success, zero is returned. On error, -1 is returned, and errno is
46 set appropriately.
47
49 EACCES The address is protected, and the user is not the superuser.
50
51 EADDRINUSE
52 The given address is already in use.
53
54 EADDRINUSE
55 (Internet domain sockets) The port number was specified as zero
56 in the socket address structure, but, upon attempting to bind to
57 an ephemeral port, it was determined that all port numbers in
58 the ephemeral port range are currently in use. See the discus‐
59 sion of /proc/sys/net/ipv4/ip_local_port_range ip(7).
60
61 EBADF sockfd is not a valid file descriptor.
62
63 EINVAL The socket is already bound to an address.
64
65 EINVAL addrlen is wrong, or addr is not a valid address for this
66 socket's domain.
67
68 ENOTSOCK
69 The file descriptor sockfd does not refer to a socket.
70
71 The following errors are specific to UNIX domain (AF_UNIX) sockets:
72
73 EACCES Search permission is denied on a component of the path prefix.
74 (See also path_resolution(7).)
75
76 EADDRNOTAVAIL
77 A nonexistent interface was requested or the requested address
78 was not local.
79
80 EFAULT addr points outside the user's accessible address space.
81
82 ELOOP Too many symbolic links were encountered in resolving addr.
83
84 ENAMETOOLONG
85 addr is too long.
86
87 ENOENT A component in the directory prefix of the socket pathname does
88 not exist.
89
90 ENOMEM Insufficient kernel memory was available.
91
92 ENOTDIR
93 A component of the path prefix is not a directory.
94
95 EROFS The socket inode would reside on a read-only filesystem.
96
98 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (bind() first appeared in
99 4.2BSD).
100
102 POSIX.1 does not require the inclusion of <sys/types.h>, and this
103 header file is not required on Linux. However, some historical (BSD)
104 implementations required this header file, and portable applications
105 are probably wise to include it.
106
107 For background on the socklen_t type, see accept(2).
108
110 The transparent proxy options are not described.
111
113 An example of the use of bind() with Internet domain sockets can be
114 found in getaddrinfo(3).
115
116 The following example shows how to bind a stream socket in the UNIX
117 (AF_UNIX) domain, and accept connections:
118
119 #include <sys/socket.h>
120 #include <sys/un.h>
121 #include <stdlib.h>
122 #include <stdio.h>
123 #include <string.h>
124
125 #define MY_SOCK_PATH "/somepath"
126 #define LISTEN_BACKLOG 50
127
128 #define handle_error(msg) \
129 do { perror(msg); exit(EXIT_FAILURE); } while (0)
130
131 int
132 main(int argc, char *argv[])
133 {
134 int sfd, cfd;
135 struct sockaddr_un my_addr, peer_addr;
136 socklen_t peer_addr_size;
137
138 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
139 if (sfd == -1)
140 handle_error("socket");
141
142 memset(&my_addr, 0, sizeof(my_addr));
143 /* Clear structure */
144 my_addr.sun_family = AF_UNIX;
145 strncpy(my_addr.sun_path, MY_SOCK_PATH,
146 sizeof(my_addr.sun_path) - 1);
147
148 if (bind(sfd, (struct sockaddr *) &my_addr,
149 sizeof(my_addr)) == -1)
150 handle_error("bind");
151
152 if (listen(sfd, LISTEN_BACKLOG) == -1)
153 handle_error("listen");
154
155 /* Now we can accept incoming connections one
156 at a time using accept(2) */
157
158 peer_addr_size = sizeof(peer_addr);
159 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
160 &peer_addr_size);
161 if (cfd == -1)
162 handle_error("accept");
163
164 /* Code to deal with incoming connection(s)... */
165
166 /* When no longer required, the socket pathname, MY_SOCK_PATH
167 should be deleted using unlink(2) or remove(3) */
168 }
169
171 accept(2), connect(2), getsockname(2), listen(2), socket(2), getad‐
172 drinfo(3), getifaddrs(3), ip(7), ipv6(7), path_resolution(7),
173 socket(7), unix(7)
174
176 This page is part of release 5.10 of the Linux man-pages project. A
177 description of the project, information about reporting bugs, and the
178 latest version of this page, can be found at
179 https://www.kernel.org/doc/man-pages/.
180
181
182
183Linux 2020-11-01 BIND(2)