1BIND(2)                    Linux Programmer's Manual                   BIND(2)
2
3
4

NAME

6       bind - bind a name to a socket
7

SYNOPSIS

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

DESCRIPTION

16       When  a  socket  is  created  with socket(2), it exists in a name space
17       (address family) but has no address assigned to it.  bind() assigns the
18       address  specified  by  addr  to  the  socket  referred  to by the file
19       descriptor sockfd.  addrlen  specifies  the  size,  in  bytes,  of  the
20       address structure pointed to by addr.  Traditionally, this operation is
21       called “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 EXAMPLE below.
42

RETURN VALUE

44       On  success,  zero is returned.  On error, -1 is returned, and errno is
45       set appropriately.
46

ERRORS

48       EACCES The address is protected, and the user is not the superuser.
49
50       EADDRINUSE
51              The given address is already in use.
52
53       EADDRINUSE
54              (Internet domain sockets) The port number was specified as  zero
55              in the socket address structure, but, upon attempting to bind to
56              an ephemeral port, it was determined that all  port  numbers  in
57              the  ephemeral port range are currently in use.  See the discus‐
58              sion of /proc/sys/net/ipv4/ip_local_port_range ip(7).
59
60       EBADF  sockfd is not a valid file descriptor.
61
62       EINVAL The socket is already bound to an address.
63
64       EINVAL addrlen is wrong, or addr  is  not  a  valid  address  for  this
65              socket's domain.
66
67       ENOTSOCK
68              The file descriptor sockfd does not refer to a socket.
69
70       The following errors are specific to UNIX domain (AF_UNIX) sockets:
71
72       EACCES Search  permission  is denied on a component of the path prefix.
73              (See also path_resolution(7).)
74
75       EADDRNOTAVAIL
76              A nonexistent interface was requested or the  requested  address
77              was not local.
78
79       EFAULT addr points outside the user's accessible address space.
80
81       ELOOP  Too many symbolic links were encountered in resolving addr.
82
83       ENAMETOOLONG
84              addr is too long.
85
86       ENOENT A  component in the directory prefix of the socket pathname does
87              not exist.
88
89       ENOMEM Insufficient kernel memory was available.
90
91       ENOTDIR
92              A component of the path prefix is not a directory.
93
94       EROFS  The socket inode would reside on a read-only filesystem.
95

CONFORMING TO

97       POSIX.1-2001, POSIX.1-2008, SVr4,  4.4BSD  (bind()  first  appeared  in
98       4.2BSD).
99

NOTES

101       POSIX.1  does  not  require  the  inclusion  of <sys/types.h>, and this
102       header file is not required on Linux.  However, some  historical  (BSD)
103       implementations  required  this  header file, and portable applications
104       are probably wise to include it.
105
106       For background on the socklen_t type, see accept(2).
107

BUGS

109       The transparent proxy options are not described.
110

EXAMPLE

112       An example of the use of bind() with Internet  domain  sockets  can  be
113       found in getaddrinfo(3).
114
115       The  following  example  shows  how to bind a stream socket in the UNIX
116       (AF_UNIX) domain, and accept connections:
117
118       #include <sys/socket.h>
119       #include <sys/un.h>
120       #include <stdlib.h>
121       #include <stdio.h>
122       #include <string.h>
123
124       #define MY_SOCK_PATH "/somepath"
125       #define LISTEN_BACKLOG 50
126
127       #define handle_error(msg) \
128           do { perror(msg); exit(EXIT_FAILURE); } while (0)
129
130       int
131       main(int argc, char *argv[])
132       {
133           int sfd, cfd;
134           struct sockaddr_un my_addr, peer_addr;
135           socklen_t peer_addr_size;
136
137           sfd = socket(AF_UNIX, SOCK_STREAM, 0);
138           if (sfd == -1)
139               handle_error("socket");
140
141           memset(&my_addr, 0, sizeof(struct sockaddr_un));
142                               /* Clear structure */
143           my_addr.sun_family = AF_UNIX;
144           strncpy(my_addr.sun_path, MY_SOCK_PATH,
145                   sizeof(my_addr.sun_path) - 1);
146
147           if (bind(sfd, (struct sockaddr *) &my_addr,
148                   sizeof(struct sockaddr_un)) == -1)
149               handle_error("bind");
150
151           if (listen(sfd, LISTEN_BACKLOG) == -1)
152               handle_error("listen");
153
154           /* Now we can accept incoming connections one
155              at a time using accept(2) */
156
157           peer_addr_size = sizeof(struct sockaddr_un);
158           cfd = accept(sfd, (struct sockaddr *) &peer_addr,
159                        &peer_addr_size);
160           if (cfd == -1)
161               handle_error("accept");
162
163           /* Code to deal with incoming connection(s)... */
164
165           /* When no longer required, the socket pathname, MY_SOCK_PATH
166              should be deleted using unlink(2) or remove(3) */
167       }
168

SEE ALSO

170       accept(2), connect(2),  getsockname(2),  listen(2),  socket(2),  getad‐
171       drinfo(3),    getifaddrs(3),    ip(7),   ipv6(7),   path_resolution(7),
172       socket(7), unix(7)
173

COLOPHON

175       This page is part of release 5.04 of the Linux  man-pages  project.   A
176       description  of  the project, information about reporting bugs, and the
177       latest    version    of    this    page,    can     be     found     at
178       https://www.kernel.org/doc/man-pages/.
179
180
181
182Linux                             2019-03-06                           BIND(2)
Impressum