From ffa01655e5afe355d3308811e87824beb2f99e24 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Wed, 14 Dec 2005 11:37:28 +0000 Subject: [PATCH] Added mention of AF_INET6 address family. Added discussion of sockaddr structure and an example in the Unix domain. --- man2/bind.2 | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/man2/bind.2 b/man2/bind.2 index 20f369e39..74fa1cf14 100644 --- a/man2/bind.2 +++ b/man2/bind.2 @@ -101,6 +101,10 @@ the manual entries in Section 7 for detailed information. For see .BR ip (7), for +.B AF_INET6 +see +.BR ipv6 (7), +for .B AF_UNIX see .BR unix (7), @@ -121,6 +125,56 @@ and for see .BR netlink (7). +The actual structure passed for the +.I my_addr +argument will depend on the address family. +The +.I sockaddr +structure is defined as something like: +.in +0.25in +.nf + +struct sockaddr { + sa_family_t sa_family; + char sa_data[14]; +} + +.fi +.in -0.25in +The only purpose of this structure is to cast the structure +pointer passed in +.I my_addr +in order to avoid compiler warnings. +The following example shows how this is done when binding a socket +in the Unix +.RB ( AF_UNIX ) +domain: + +.in +0.25in +.nf +#include +#include + +int sfd; +struct sockaddr_un addr; + +sfd = socket(AF_UNIX, SOCK_STREAM, 0); +if (sfd == -1) { perror("socket"); exit(EXIT_FAILURE); } + +memset(&addr, 0, sizeof(struct sockaddr_un)); + /* Clear structure */ +addr.sun_family = AF_UNIX; +strncpy(addr.sun_path, MY_SOCK_PATH, + sizeof(addr.sun_path) - 1); + +if (bind(sfd, (struct sockaddr *) &addr, + sizeof(struct sockaddr_un)) == -1) { + perror("bind"); + exit(EXIT_FAILURE); +} + +.fi +.in -0.25in .SH "RETURN VALUE" On success, zero is returned. On error, \-1 is returned, and .I errno @@ -218,4 +272,6 @@ See also .BR socket (2), .BR getaddrinfo (3), .BR ip (7), -.BR socket (7) +.BR ipv6 (7), +.BR socket (7), +.BR unix (7)