4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 #pragma ident "%Z%%M% %I% %E% SMI"
27 * Get client's IP address via ioctl. This avoids using the NIS.
28 * Copyright (C) 1990, Sun Microsystems, Inc.
31 #include <rpc/types.h>
32 #include <rpc/pmap_prot.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
37 #include <sys/ioctl.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
48 * don't use gethostbyname, which would invoke NIS
51 get_myaddress(struct sockaddr_in
*addr
)
55 struct ifreq ifreq
, *ifr
;
59 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
60 syslog(LOG_ERR
, "get_myaddress: socket: %m");
65 ret
= ioctl(s
, SIOCGIFNUM
, (char *)&numifs
);
66 } while (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
68 syslog(LOG_ERR
, "get_myaddress: ioctl: %m");
72 ifc
.ifc_len
= numifs
* sizeof (struct ifreq
);
73 if ((ifc
.ifc_buf
= (caddr_t
)malloc(ifc
.ifc_len
)) == NULL
) {
74 syslog(LOG_ERR
, "get_myaddress: malloc: %m");
79 ret
= ioctl(s
, SIOCGIFCONF
, (char *)&ifc
);
80 } while (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
83 "get_myaddress: ioctl (get interface configuration): %m");
88 * set default to loopback in case nothing is found.
90 addr
->sin_family
= AF_INET
;
91 addr
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
92 addr
->sin_port
= htons(PMAPPORT
);
95 for (len
= ifc
.ifc_len
; len
> 0; len
-= sizeof (ifreq
), ifr
++) {
98 ret
= ioctl(s
, SIOCGIFFLAGS
, (char *)&ifreq
);
99 } while (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
101 syslog(LOG_ERR
, "get_myaddress: ioctl: %m");
104 if (ifr
->ifr_addr
.sa_family
!= AF_INET
)
106 if ((ifreq
.ifr_flags
& IFF_UP
) == 0)
108 if (ifreq
.ifr_flags
& IFF_LOOPBACK
)
110 if ((ifreq
.ifr_flags
& (IFF_MULTICAST
| IFF_BROADCAST
)) == 0)
112 *addr
= *((struct sockaddr_in
*)&ifr
->ifr_addr
);
113 addr
->sin_port
= htons(PMAPPORT
);