17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * lib/krb5/os/localaddr.c
37c478bd9Sstevel@tonic-gate  *
4*505d05c7Sgtb  * Copyright 1990,1991,2000,2001,2002,2004 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate  * All Rights Reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
247c478bd9Sstevel@tonic-gate  * or implied warranty.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Return the protocol addresses supported by this host.
28*505d05c7Sgtb  * Exports from this file:
29*505d05c7Sgtb  *   krb5int_foreach_localaddr (does callbacks)
30*505d05c7Sgtb  *   krb5int_local_addresses (includes krb5.conf extra_addresses)
31*505d05c7Sgtb  *   krb5_os_localaddr (doesn't)
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * XNS support is untested, but "Should just work".  (Hah!)
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
36*505d05c7Sgtb #include "k5-int.h"
37*505d05c7Sgtb 
38*505d05c7Sgtb #if !defined(_WIN32)
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* needed for solaris, harmless elsewhere... */
417c478bd9Sstevel@tonic-gate #define BSD_COMP
427c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
437c478bd9Sstevel@tonic-gate #include <sys/time.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <stddef.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate 
48*505d05c7Sgtb #if defined(TEST) || defined(DEBUG)
49*505d05c7Sgtb # include "fake-addrinfo.h"
50*505d05c7Sgtb #endif
51*505d05c7Sgtb 
52*505d05c7Sgtb #include "foreachaddr.h"
53*505d05c7Sgtb 
54*505d05c7Sgtb /* Note: foreach_localaddr is exported from the library through
55*505d05c7Sgtb    krb5int_accessor, for the KDC to use.
56*505d05c7Sgtb 
57*505d05c7Sgtb    This function iterates over all the addresses it can find for the
58*505d05c7Sgtb    local system, in one or two passes.  In each pass, and between the
59*505d05c7Sgtb    two, it can invoke callback functions supplied by the caller.  The
60*505d05c7Sgtb    two passes should operate on the same information, though not
61*505d05c7Sgtb    necessarily in the same order each time.  Duplicate and local
62*505d05c7Sgtb    addresses should be eliminated.  Storage passed to callback
63*505d05c7Sgtb    functions should not be assumed to be valid after foreach_localaddr
64*505d05c7Sgtb    returns.
65*505d05c7Sgtb 
66*505d05c7Sgtb    The int return value is an errno value (XXX or krb5_error_code
67*505d05c7Sgtb    returned for a socket error) if something internal to
68*505d05c7Sgtb    foreach_localaddr fails.  If one of the callback functions wants to
69*505d05c7Sgtb    indicate an error, it should store something via the 'data' handle.
70*505d05c7Sgtb    If any callback function returns a non-zero value,
71*505d05c7Sgtb    foreach_localaddr will clean up and return immediately.
72*505d05c7Sgtb 
73*505d05c7Sgtb    Multiple definitions are provided below, dependent on various
74*505d05c7Sgtb    system facilities for extracting the necessary information.  */
75*505d05c7Sgtb 
76*505d05c7Sgtb /* Now, on to the implementations, and heaps of debugging code.  */
77*505d05c7Sgtb 
78*505d05c7Sgtb #ifdef TEST
79*505d05c7Sgtb # define Tprintf(X) printf X
80*505d05c7Sgtb # define Tperror(X) perror(X)
81*505d05c7Sgtb #else
82*505d05c7Sgtb # define Tprintf(X) (void) X
83*505d05c7Sgtb # define Tperror(X) (void)(X)
84*505d05c7Sgtb #endif
85*505d05c7Sgtb 
86*505d05c7Sgtb /*
87*505d05c7Sgtb  * The SIOCGIF* ioctls require a socket.
88*505d05c7Sgtb  * It doesn't matter *what* kind of socket they use, but it has to be
89*505d05c7Sgtb  * a socket.
90*505d05c7Sgtb  *
91*505d05c7Sgtb  * Of course, you can't just ask the kernel for a socket of arbitrary
92*505d05c7Sgtb  * type; you have to ask for one with a valid type.
93*505d05c7Sgtb  *
94*505d05c7Sgtb  */
95*505d05c7Sgtb #ifdef HAVE_NETINET_IN_H
96*505d05c7Sgtb #include <netinet/in.h>
97*505d05c7Sgtb #ifndef USE_AF
98*505d05c7Sgtb #define USE_AF AF_INET
99*505d05c7Sgtb #define USE_TYPE SOCK_DGRAM
100*505d05c7Sgtb #define USE_PROTO 0
101