1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="TCP/IP Library Reference"
23 HREF="tcpip-library-reference.html"><LINK
26 HREF="net-common-tcpip-manpages-getpeername.html"><LINK
29 HREF="net-common-tcpip-manpages-getsockopt.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="net-common-tcpip-manpages-getpeername.html"
65 >Chapter 38. TCP/IP Library Reference</TD
71 HREF="net-common-tcpip-manpages-getsockopt.html"
85 NAME="NET-COMMON-TCPIP-MANPAGES-GETSOCKNAME">getsockname</H1
94 >GETSOCKNAME(2) System Calls Manual GETSOCKNAME(2)
97 getsockname - get socket name
100 #include <sys/types.h>
101 #include <sys/socket.h>
104 getsockname(int s, struct sockaddr *name, socklen_t *namelen);
107 getsockname() returns the locally bound address information for a speci-
110 Common uses of this function are as follows:
112 o When bind(2) is called with a port number of 0 (indicating the kernel
113 should pick an ephemeral port) getsockname() is used to retrieve the
114 kernel-assigned port number.
116 o When a process calls bind(2) on a wildcard IP address, getsockname()
117 is used to retrieve the local IP address for the connection.
119 o When a function wishes to know the address family of a socket,
120 getsockname() can be used.
122 getsockname() takes three parameters:
124 s, Contains the file desriptor for the socket to be looked up.
126 name points to a sockaddr structure which will hold the resulting address
127 information. Normal use requires one to use a structure specific to the
128 protocol family in use, such as sockaddr_in (IPv4) or sockaddr_in6
129 (IPv6), cast to a (struct sockaddr *).
131 For greater portability (such as newer protocol families) the new struc-
132 ture sockaddr_storage exists. sockaddr_storage is large enough to hold
133 any of the other sockaddr_* variants. On return, it should be cast to
134 the correct sockaddr type, according to the current protocol family.
136 namelen Indicates the amount of space pointed to by name, in bytes. Upon
137 return, namelen is set to the actual size of the returned address infor-
140 If the address of the destination socket for a given socket connection is
141 needed, the getpeername(2) function should be used instead.
143 If name does not point to enough space to hold the entire socket address,
144 the result will be truncated to namelen bytes.
147 On success, getsockname() returns a 0, and namelen is set to the actual
148 size of the socket address returned in name. Otherwise, errno is set,
149 and a value of -1 is returned.
152 If getsockname() fails, errno is set to one of the following:
154 [EBADF] The argument s is not a valid descriptor.
156 [ENOTSOCK] The argument s is a file, not a socket.
158 [ENOBUFS] Insufficient resources were available in the system to
159 perform the operation.
161 [EFAULT] The name parameter points to memory not in a valid
162 part of the process address space.
165 accept(2), bind(2), getpeername(2), getpeereid(2), socket(2)
168 Names bound to sockets in the UNIX domain are inaccessible; getsockname
169 returns a zero length name.
172 The getsockname() function call appeared in 4.2BSD.
174 BSD July 17, 1999 BSD
185 SUMMARY="Footer navigation table"
196 HREF="net-common-tcpip-manpages-getpeername.html"
214 HREF="net-common-tcpip-manpages-getsockopt.html"
230 HREF="tcpip-library-reference.html"