summaryrefslogtreecommitdiffstats
path: root/newlib/libc/sys/linux/net/getnameinfo.3
blob: d7afa2f4096748ad5b0f0888f4d3e10690275c3e (plain)
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
GETNAMEINFO(3)                     BSD Library Functions Manual                    GETNAMEINFO(3)

NAME
     getnameinfo -- address-to-nodename translation in protocol-independent manner

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <sys/types.h>
     #include <sys/socket.h>
     #include <netdb.h>

     int
     getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen,
         char *serv, size_t servlen, int flags);

DESCRIPTION
     The getnameinfo() function is defined for protocol-independent address-to-nodename transla-
     tion.  Its functionality is a reverse conversion of getaddrinfo(3), and implements similar
     functionality with gethostbyaddr(3) and getservbyport(3) in more sophisticated manner.

     This function looks up an IP address and port number provided by the caller in the DNS and
     system-specific database, and returns text strings for both in buffers provided by the
     caller.  The function indicates successful completion by a zero return value; a non-zero re-
     turn value indicates failure.

     The first argument, sa, points to either a sockaddr_in structure (for IPv4) or a
     sockaddr_in6 structure (for IPv6) that holds the IP address and port number.  The salen ar-
     gument gives the length of the sockaddr_in or sockaddr_in6 structure.

     The function returns the nodename associated with the IP address in the buffer pointed to by
     the host argument.  The caller provides the size of this buffer via the hostlen argument.
     The service name associated with the port number is returned in the buffer pointed to by
     serv, and the servlen argument gives the length of this buffer.  The caller specifies not to
     return either string by providing a zero value for the hostlen or servlen arguments.  Other-
     wise, the caller must provide buffers large enough to hold the nodename and the service
     name, including the terminating null characters.

     Unfortunately most systems do not provide constants that specify the maximum size of either
     a fully-qualified domain name or a service name.  Therefore to aid the application in allo-
     cating buffers for these two returned strings the following constants are defined in
     <netdb.h>:

     #define NI_MAXHOST  1025
     #define NI_MAXSERV    32

     The first value is actually defined as the constant MAXDNAME in recent versions of BIND's
     <arpa/nameser.h> header (older versions of BIND define this constant to be 256) and the sec-
     ond is a guess based on the services listed in the current Assigned Numbers RFC.

     The final argument is a flag that changes the default actions of this function.  By default
     the fully-qualified domain name (FQDN) for the host is looked up in the DNS and returned.
     If the flag bit NI_NOFQDN is set, only the nodename portion of the FQDN is returned for lo-
     cal hosts.

     If the flag bit NI_NUMERICHOST is set, or if the host's name cannot be located in the DNS,
     the numeric form of the host's address is returned instead of its name (e.g., by calling
     inet_ntop() instead of getnodebyaddr()).  If the flag bit NI_NAMEREQD is set, an error is
     returned if the host's name cannot be located in the DNS.

     If the flag bit NI_NUMERICSERV is set, the numeric form of the service address is returned
     (e.g., its port number) instead of its name.  The two NI_NUMERICxxx flags are required to
     support the -n flag that many commands provide.

     A fifth flag bit, NI_DGRAM, specifies that the service is a datagram service, and causes
     getservbyport() to be called with a second argument of "udp" instead of its default of
     "tcp".  This is required for the few ports (512-514) that have different services for UDP
     and TCP.

     These NI_xxx flags are defined in <netdb.h>.

EXTENSION
     The implementation allows experimental numeric IPv6 address notation with scope identifier.
     IPv6 link-local address will appear as string like "fe80::1%ne0", if NI_WITHSCOPEID bit is
     enabled in flags argument.  Refer to getaddrinfo(3) for the notation.

EXAMPLES
     The following code tries to get numeric hostname, and service name, for given socket ad-
     dress.  Observe that there is no hardcoded reference to particular address family.

           struct sockaddr *sa;    /* input */
           char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

           if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
               sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
                   errx(1, "could not get numeric hostname");
                   /*NOTREACHED*/
           }
           printf("host=%s, serv=%s\n", hbuf, sbuf);

     The following version checks if the socket address has reverse address mapping.

           struct sockaddr *sa;    /* input */
           char hbuf[NI_MAXHOST];

           if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
               NI_NAMEREQD)) {
                   errx(1, "could not resolve hostname");
                   /*NOTREACHED*/
           }
           printf("host=%s\n", hbuf);

FILES
     /etc/hosts
     /etc/nsswitch.conf
     /etc/resolv.conf

DIAGNOSTICS
     The function indicates successful completion by a zero return value; a non-zero return value
     indicates failure.  Error codes are as below:

     [EAI_AGAIN]        The name could not be resolved at this time.  Future attempts may suc-
                        ceed.

     [EAI_BADFLAGS]     The flags had an invalid value.

     [EAI_FAIL]         A non-recoverable error occurred.

     [EAI_FAMILY]       The address family was not recognized or the address length was invalid
                        for the specified family.

     [EAI_MEMORY]       There was a memory allocation failure.

     [EAI_NONAME]       The name does not resolve for the supplied parameters.  NI_NAMEREQD is
                        set and the host's name cannot be located, or both nodename and servname
                        were null.

     [EAI_SYSTEM]       A system error occurred.  The error code can be found in errno.

SEE ALSO
     getaddrinfo(3), gethostbyaddr(3), getservbyport(3), hosts(5), services(5), hostname(7),
     named(8)

     R. Gilligan, S. Thomson, J. Bound, and W. Stevens, Basic Socket Interface Extensions for
     IPv6, RFC2553, March 1999.

     Tatsuya Jinmei and Atsushi Onoe, An Extension of Format for IPv6 Scoped Addresses, internet
     draft, draft-ietf-ipngwg-scopedaddr-format-02.txt, work in progress material.

     Craig Metz, "Protocol Independence Using the Sockets API", Proceedings of the freenix track:
     2000 USENIX annual technical conference, June 2000.

HISTORY
     The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.

STANDARDS
     The getaddrinfo() function is defined in IEEE Std 1003.1g-2000 ("POSIX.1"), and documented
     in "Basic Socket Interface Extensions for IPv6" (RFC2553).

BUGS
     The current implementation is not thread-safe.

     The text was shamelessly copied from RFC2553.

     The type of the 2nd argument should be socklen_t for RFC2553 conformance.  The current code
     is based on pre-RFC2553 specification.

BSD                                        May 25, 1995                                       BSD