-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.html
432 lines (314 loc) · 11.2 KB
/
bind.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:27:07 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>BIND</title>
</head>
<body>
<h1 align="center">BIND</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">bind - bind a
name to a socket</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/types.h></b> /* See NOTES */ <b><br>
#include <sys/socket.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
bind(int</b> <i>sockfd</i><b>, const struct sockaddr
*</b><i>addr</i><b>, <br>
socklen_t</b> <i>addrlen</i><b>);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">When a socket
is created with <b>socket</b>(2), it exists in a name space
(address family) but has no address assigned to it.
<b>bind</b>() assigns the address specified by <i>addr</i>
to the socket referred to by the file descriptor
<i>sockfd</i>. <i>addrlen</i> specifies the size, in bytes,
of the address structure pointed to by <i>addr</i>.
Traditionally, this operation is called “assigning a
name to a socket”.</p>
<p style="margin-left:11%; margin-top: 1em">It is normally
necessary to assign a local address using <b>bind</b>()
before a <b>SOCK_STREAM</b> socket may receive connections
(see <b>accept</b>(2)).</p>
<p style="margin-left:11%; margin-top: 1em">The rules used
in name binding vary between address families. Consult the
manual entries in Section 7 for detailed information. For
<b>AF_INET</b>, see <b>ip</b>(7); for <b>AF_INET6</b>, see
<b>ipv6</b>(7); for <b>AF_UNIX</b>, see <b>unix</b>(7); for
<b>AF_APPLETALK</b>, see <b>ddp</b>(7); for
<b>AF_PACKET</b>, see <b>packet</b>(7); for <b>AF_X25</b>,
see <b>x25</b>(7); and for <b>AF_NETLINK</b>, see
<b>netlink</b>(7).</p>
<p style="margin-left:11%; margin-top: 1em">The actual
structure passed for the <i>addr</i> argument will depend on
the address family. The <i>sockaddr</i> structure is defined
as something like:</p>
<p style="margin-left:17%; margin-top: 1em">struct sockaddr
{ <br>
sa_family_t sa_family; <br>
char sa_data[14]; <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">The only
purpose of this structure is to cast the structure pointer
passed in <i>addr</i> in order to avoid compiler warnings.
See EXAMPLE below.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success,
zero is returned. On error, -1 is returned, and <i>errno</i>
is set appropriately.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EACCES</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">The address is protected, and
the user is not the superuser.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>EADDRINUSE</b></p>
<p style="margin-left:22%;">The given address is already in
use.</p>
<p style="margin-left:11%;"><b>EADDRINUSE</b></p>
<p style="margin-left:22%;">(Internet domain sockets) The
port number was specified as zero in the socket address
structure, but, upon attempting to bind to an ephemeral
port, it was determined that all port numbers in the
ephemeral port range are currently in use. See the
discussion of <i>/proc/sys/net/ipv4/ip_local_port_range</i>
<b>ip</b>(7).</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EBADF</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>sockfd</i> is not a valid file descriptor.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The socket is already bound to an address.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>addrlen</i> is wrong, or <i>addr</i> is not a valid
address for this socket’s domain.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>ENOTSOCK</b></p>
<p style="margin-left:22%;">The file descriptor
<i>sockfd</i> does not refer to a socket.</p>
<p style="margin-left:11%; margin-top: 1em">The following
errors are specific to UNIX domain (<b>AF_UNIX</b>)
sockets:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EACCES</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Search permission is denied on a component of the path
prefix. (See also <b>path_resolution</b>(7).)</p></td></tr>
</table>
<p style="margin-left:11%;"><b>EADDRNOTAVAIL</b></p>
<p style="margin-left:22%;">A nonexistent interface was
requested or the requested address was not local.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>addr</i> points outside the user’s accessible
address space.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ELOOP</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Too many symbolic links were encountered in resolving
<i>addr</i>.</p> </td></tr>
</table>
<p style="margin-left:11%;"><b>ENAMETOOLONG</b></p>
<p style="margin-left:22%;"><i>addr</i> is too long.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOENT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>A component in the directory prefix of the socket
pathname does not exist.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Insufficient kernel memory was available.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>ENOTDIR</b></p>
<p style="margin-left:22%;">A component of the path prefix
is not a directory.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EROFS</b></p></td>
<td width="4%"></td>
<td width="78%">
<p>The socket inode would reside on a read-only
filesystem.</p> </td></tr>
</table>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">POSIX.1-2001,
POSIX.1-2008, SVr4, 4.4BSD (<b>bind</b>() first appeared in
4.2BSD).</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">POSIX.1 does
not require the inclusion of <i><sys/types.h></i>, and
this header file is not required on Linux. However, some
historical (BSD) implementations required this header file,
and portable applications are probably wise to include
it.</p>
<p style="margin-left:11%; margin-top: 1em">For background
on the <i>socklen_t</i> type, see <b>accept</b>(2).</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The transparent
proxy options are not described.</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">An example of
the use of <b>bind</b>() with Internet domain sockets can be
found in <b>getaddrinfo</b>(3).</p>
<p style="margin-left:11%; margin-top: 1em">The following
example shows how to bind a stream socket in the UNIX
(<b>AF_UNIX</b>) domain, and accept connections:</p>
<p style="margin-left:11%; margin-top: 1em">#include
<sys/socket.h> <br>
#include <sys/un.h> <br>
#include <stdlib.h> <br>
#include <stdio.h> <br>
#include <string.h></p>
<p style="margin-left:11%; margin-top: 1em">#define
MY_SOCK_PATH "/somepath" <br>
#define LISTEN_BACKLOG 50</p>
<p style="margin-left:11%; margin-top: 1em">#define
handle_error(msg) \ <br>
do { perror(msg); exit(EXIT_FAILURE); } while (0)</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
int sfd, cfd; <br>
struct sockaddr_un my_addr, peer_addr; <br>
socklen_t peer_addr_size;</p>
<p style="margin-left:11%; margin-top: 1em">sfd =
socket(AF_UNIX, SOCK_STREAM, 0); <br>
if (sfd == -1) <br>
handle_error("socket");</p>
<p style="margin-left:11%; margin-top: 1em">memset(&my_addr,
0, sizeof(struct sockaddr_un)); <br>
/* Clear structure */ <br>
my_addr.sun_family = AF_UNIX; <br>
strncpy(my_addr.sun_path, MY_SOCK_PATH, <br>
sizeof(my_addr.sun_path) - 1);</p>
<p style="margin-left:11%; margin-top: 1em">if (bind(sfd,
(struct sockaddr *) &my_addr, <br>
sizeof(struct sockaddr_un)) == -1) <br>
handle_error("bind");</p>
<p style="margin-left:11%; margin-top: 1em">if (listen(sfd,
LISTEN_BACKLOG) == -1) <br>
handle_error("listen");</p>
<p style="margin-left:11%; margin-top: 1em">/* Now we can
accept incoming connections one <br>
at a time using accept(2) */</p>
<p style="margin-left:11%; margin-top: 1em">peer_addr_size
= sizeof(struct sockaddr_un); <br>
cfd = accept(sfd, (struct sockaddr *) &peer_addr, <br>
&peer_addr_size); <br>
if (cfd == -1) <br>
handle_error("accept");</p>
<p style="margin-left:11%; margin-top: 1em">/* Code to deal
with incoming connection(s)... */</p>
<p style="margin-left:11%; margin-top: 1em">/* When no
longer required, the socket pathname, MY_SOCK_PATH <br>
should be deleted using unlink(2) or remove(3) */ <br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>accept</b>(2),
<b>connect</b>(2), <b>getsockname</b>(2), <b>listen</b>(2),
<b>socket</b>(2), <b>getaddrinfo</b>(3),
<b>getifaddrs</b>(3), <b>ip</b>(7), <b>ipv6</b>(7),
<b>path_resolution</b>(7), <b>socket</b>(7),
<b>unix</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>