4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: misc.c 146 2006-03-24 00:26:54Z njacobs $ */
30 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <sys/types.h>
42 #include <config-site.h>
45 * The implementations of strlcpy() and strlcat() have been taken directly
46 * from OpenSolaris. The contents of this file originated from
47 * usr/src/lib/libc/port/gen/strlcpy.c
48 * usr/src/lib/libc/port/gen/strcat.c
53 strlcpy(char *dst
, const char *src
, size_t len
)
55 size_t slen
= strlen(src
);
65 (void) memcpy(dst
, src
, copied
);
73 strlcat(char *dst
, const char *src
, size_t dstsize
)
76 size_t left
= dstsize
;
78 size_t l2
= strlen(src
);
81 while (left
-- != 0 && *df
!= '\0')
87 copied
= l1
+ l2
>= dstsize
? dstsize
- l1
- 1 : l2
;
88 (void) memcpy(dst
+ l1
, src
, copied
);
89 dst
[l1
+copied
] = '\0';
94 #if defined(__sun) && defined(__SVR4)
95 #include <sys/systeminfo.h>
96 #include <sys/socket.h>
97 #include <sys/ioctl.h>
98 #include <sys/sockio.h>
100 #include <netinet/in.h>
101 #include <arpa/inet.h>
104 static struct in6_addr
**
107 struct in6_addr
**result
= NULL
;
114 /* we need a socket to get the interfaces */
115 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
118 /* get the number of interfaces */
119 memset(&n
, 0, sizeof (n
));
120 n
.lifn_family
= AF_UNSPEC
;
121 if (ioctl(s
, SIOCGLIFNUM
, (char *)&n
) < 0) {
123 return (0); /* no interfaces */
126 /* get the interface(s) configuration */
127 memset(&c
, 0, sizeof (c
));
128 c
.lifc_family
= AF_UNSPEC
;
129 c
.lifc_buf
= calloc(n
.lifn_count
, sizeof (struct lifreq
));
130 c
.lifc_len
= (n
.lifn_count
* sizeof (struct lifreq
));
131 if (ioctl(s
, SIOCGLIFCONF
, (char *)&c
) < 0) {
134 return (0); /* can't get interface(s) configuration */
139 for (count
= c
.lifc_len
/ sizeof (struct lifreq
);
140 count
> 0; count
--, r
++) {
141 struct in6_addr v6
[1], *addr
= NULL
;
143 switch (r
->lifr_addr
.ss_family
) {
145 struct sockaddr_in
*s
=
146 (struct sockaddr_in
*)&r
->lifr_addr
;
147 IN6_INADDR_TO_V4MAPPED(&s
->sin_addr
, v6
);
152 struct sockaddr_in6
*s
=
153 (struct sockaddr_in6
*)&r
->lifr_addr
;
154 addr
= &s
->sin6_addr
;
160 struct in6_addr
*a
= malloc(sizeof (*a
));
162 memcpy(a
, addr
, sizeof (*a
));
163 list_append(&result
, a
);
172 match_interfaces(char *host
)
174 struct in6_addr
**lif
= local_interfaces();
179 /* are there any local interfaces */
183 /* cycle through the host db addresses */
184 hp
= getipnodebyname(host
, AF_INET6
, AI_ALL
|AI_V4MAPPED
, &errnum
);
186 struct in6_addr
**tmp
= (struct in6_addr
**)hp
->h_addr_list
;
189 for (i
= 0; ((rc
== 0) && (tmp
[i
] != NULL
)); i
++) {
192 for (j
= 0; ((rc
== 0) && (lif
[j
] != NULL
)); j
++)
193 if (memcmp(tmp
[i
], lif
[j
],
194 sizeof (struct in6_addr
)) == 0)
205 is_localhost(char *host
)
207 char hostname
[BUFSIZ
];
209 /* is it "localhost" */
210 if (strncasecmp(host
, "localhost", 10) == 0)
213 /* is it the {nodename} */
214 sysinfo(SI_HOSTNAME
, hostname
, sizeof (hostname
));
215 if (strncasecmp(host
, hostname
, strlen(hostname
)) == 0)
218 #if defined(__sun) && defined(__SVR4)
219 /* does it match one of the host's configured interfaces */
220 if (match_interfaces(host
) != 0)