12264Sjacobs /*
22264Sjacobs * CDDL HEADER START
32264Sjacobs *
42264Sjacobs * The contents of this file are subject to the terms of the
52264Sjacobs * Common Development and Distribution License (the "License").
62264Sjacobs * You may not use this file except in compliance with the License.
72264Sjacobs *
82264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs * or http://www.opensolaris.org/os/licensing.
102264Sjacobs * See the License for the specific language governing permissions
112264Sjacobs * and limitations under the License.
122264Sjacobs *
132264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs *
192264Sjacobs * CDDL HEADER END
202264Sjacobs */
212264Sjacobs
222264Sjacobs /*
23*7132Sps29005 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
242264Sjacobs * Use is subject to license terms.
252264Sjacobs *
262264Sjacobs */
272264Sjacobs
282264Sjacobs /* $Id: misc.c 146 2006-03-24 00:26:54Z njacobs $ */
292264Sjacobs
302264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI"
312264Sjacobs
322264Sjacobs /*LINTLIBRARY*/
332264Sjacobs
34*7132Sps29005 #include <stdio.h>
35*7132Sps29005 #include <stdlib.h>
36*7132Sps29005 #include <unistd.h>
372264Sjacobs #include <string.h>
38*7132Sps29005 #include <ctype.h>
39*7132Sps29005 #include <sys/types.h>
402264Sjacobs #include <papi.h>
41*7132Sps29005 #include <uri.h>
422264Sjacobs #include <config-site.h>
432264Sjacobs
442264Sjacobs /*
452264Sjacobs * The implementations of strlcpy() and strlcat() have been taken directly
462264Sjacobs * from OpenSolaris. The contents of this file originated from
472264Sjacobs * usr/src/lib/libc/port/gen/strlcpy.c
482264Sjacobs * usr/src/lib/libc/port/gen/strcat.c
492264Sjacobs */
502264Sjacobs
512264Sjacobs #ifndef HAVE_STRLCPY
522264Sjacobs size_t
strlcpy(char * dst,const char * src,size_t len)532264Sjacobs strlcpy(char *dst, const char *src, size_t len)
542264Sjacobs {
552264Sjacobs size_t slen = strlen(src);
562264Sjacobs size_t copied;
572264Sjacobs
582264Sjacobs if (len == 0)
592264Sjacobs return (slen);
602264Sjacobs
612264Sjacobs if (slen >= len)
622264Sjacobs copied = len - 1;
632264Sjacobs else
642264Sjacobs copied = slen;
652264Sjacobs (void) memcpy(dst, src, copied);
662264Sjacobs dst[copied] = '\0';
672264Sjacobs return (slen);
682264Sjacobs }
692264Sjacobs #endif
702264Sjacobs
712264Sjacobs #ifndef HAVE_STRLCAT
722264Sjacobs size_t
strlcat(char * dst,const char * src,size_t dstsize)732264Sjacobs strlcat(char *dst, const char *src, size_t dstsize)
742264Sjacobs {
752264Sjacobs char *df = dst;
762264Sjacobs size_t left = dstsize;
772264Sjacobs size_t l1;
782264Sjacobs size_t l2 = strlen(src);
792264Sjacobs size_t copied;
802264Sjacobs
812264Sjacobs while (left-- != 0 && *df != '\0')
822264Sjacobs df++;
832264Sjacobs l1 = df - dst;
842264Sjacobs if (dstsize == l1)
852264Sjacobs return (l1 + l2);
862264Sjacobs
872264Sjacobs copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
882264Sjacobs (void) memcpy(dst + l1, src, copied);
892264Sjacobs dst[l1+copied] = '\0';
902264Sjacobs return (l1 + l2);
912264Sjacobs }
922264Sjacobs #endif
93*7132Sps29005
94*7132Sps29005 #if defined(__sun) && defined(__SVR4)
95*7132Sps29005 #include <sys/systeminfo.h>
96*7132Sps29005 #include <sys/socket.h>
97*7132Sps29005 #include <sys/ioctl.h>
98*7132Sps29005 #include <sys/sockio.h>
99*7132Sps29005 #include <net/if.h>
100*7132Sps29005 #include <netinet/in.h>
101*7132Sps29005 #include <arpa/inet.h>
102*7132Sps29005 #include <netdb.h>
103*7132Sps29005
104*7132Sps29005 static struct in6_addr **
local_interfaces()105*7132Sps29005 local_interfaces()
106*7132Sps29005 {
107*7132Sps29005 struct in6_addr **result = NULL;
108*7132Sps29005 int s;
109*7132Sps29005 struct lifnum n;
110*7132Sps29005 struct lifconf c;
111*7132Sps29005 struct lifreq *r;
112*7132Sps29005 int count;
113*7132Sps29005
114*7132Sps29005 /* we need a socket to get the interfaces */
115*7132Sps29005 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
116*7132Sps29005 return (0);
117*7132Sps29005
118*7132Sps29005 /* get the number of interfaces */
119*7132Sps29005 memset(&n, 0, sizeof (n));
120*7132Sps29005 n.lifn_family = AF_UNSPEC;
121*7132Sps29005 if (ioctl(s, SIOCGLIFNUM, (char *)&n) < 0) {
122*7132Sps29005 close(s);
123*7132Sps29005 return (0); /* no interfaces */
124*7132Sps29005 }
125*7132Sps29005
126*7132Sps29005 /* get the interface(s) configuration */
127*7132Sps29005 memset(&c, 0, sizeof (c));
128*7132Sps29005 c.lifc_family = AF_UNSPEC;
129*7132Sps29005 c.lifc_buf = calloc(n.lifn_count, sizeof (struct lifreq));
130*7132Sps29005 c.lifc_len = (n.lifn_count * sizeof (struct lifreq));
131*7132Sps29005 if (ioctl(s, SIOCGLIFCONF, (char *)&c) < 0) {
132*7132Sps29005 free(c.lifc_buf);
133*7132Sps29005 close(s);
134*7132Sps29005 return (0); /* can't get interface(s) configuration */
135*7132Sps29005 }
136*7132Sps29005 close(s);
137*7132Sps29005
138*7132Sps29005 r = c.lifc_req;
139*7132Sps29005 for (count = c.lifc_len / sizeof (struct lifreq);
140*7132Sps29005 count > 0; count--, r++) {
141*7132Sps29005 struct in6_addr v6[1], *addr = NULL;
142*7132Sps29005
143*7132Sps29005 switch (r->lifr_addr.ss_family) {
144*7132Sps29005 case AF_INET: {
145*7132Sps29005 struct sockaddr_in *s =
146*7132Sps29005 (struct sockaddr_in *)&r->lifr_addr;
147*7132Sps29005 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, v6);
148*7132Sps29005 addr = v6;
149*7132Sps29005 }
150*7132Sps29005 break;
151*7132Sps29005 case AF_INET6: {
152*7132Sps29005 struct sockaddr_in6 *s =
153*7132Sps29005 (struct sockaddr_in6 *)&r->lifr_addr;
154*7132Sps29005 addr = &s->sin6_addr;
155*7132Sps29005 }
156*7132Sps29005 break;
157*7132Sps29005 }
158*7132Sps29005
159*7132Sps29005 if (addr != NULL) {
160*7132Sps29005 struct in6_addr *a = malloc(sizeof (*a));
161*7132Sps29005
162*7132Sps29005 memcpy(a, addr, sizeof (*a));
163*7132Sps29005 list_append(&result, a);
164*7132Sps29005 }
165*7132Sps29005 }
166*7132Sps29005 free(c.lifc_buf);
167*7132Sps29005
168*7132Sps29005 return (result);
169*7132Sps29005 }
170*7132Sps29005
171*7132Sps29005 static int
match_interfaces(char * host)172*7132Sps29005 match_interfaces(char *host)
173*7132Sps29005 {
174*7132Sps29005 struct in6_addr **lif = local_interfaces();
175*7132Sps29005 struct hostent *hp;
176*7132Sps29005 int rc = 0;
177*7132Sps29005 int errnum;
178*7132Sps29005
179*7132Sps29005 /* are there any local interfaces */
180*7132Sps29005 if (lif == NULL)
181*7132Sps29005 return (0);
182*7132Sps29005
183*7132Sps29005 /* cycle through the host db addresses */
184*7132Sps29005 hp = getipnodebyname(host, AF_INET6, AI_ALL|AI_V4MAPPED, &errnum);
185*7132Sps29005 if (hp != NULL) {
186*7132Sps29005 struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list;
187*7132Sps29005 int i;
188*7132Sps29005
189*7132Sps29005 for (i = 0; ((rc == 0) && (tmp[i] != NULL)); i++) {
190*7132Sps29005 int j;
191*7132Sps29005
192*7132Sps29005 for (j = 0; ((rc == 0) && (lif[j] != NULL)); j++)
193*7132Sps29005 if (memcmp(tmp[i], lif[j],
194*7132Sps29005 sizeof (struct in6_addr)) == 0)
195*7132Sps29005 rc = 1;
196*7132Sps29005 }
197*7132Sps29005 }
198*7132Sps29005 free(lif);
199*7132Sps29005
200*7132Sps29005 return (rc);
201*7132Sps29005 }
202*7132Sps29005 #endif
203*7132Sps29005
204*7132Sps29005 int
is_localhost(char * host)205*7132Sps29005 is_localhost(char *host)
206*7132Sps29005 {
207*7132Sps29005 char hostname[BUFSIZ];
208*7132Sps29005
209*7132Sps29005 /* is it "localhost" */
210*7132Sps29005 if (strncasecmp(host, "localhost", 10) == 0)
211*7132Sps29005 return (1);
212*7132Sps29005
213*7132Sps29005 /* is it the {nodename} */
214*7132Sps29005 sysinfo(SI_HOSTNAME, hostname, sizeof (hostname));
215*7132Sps29005 if (strncasecmp(host, hostname, strlen(hostname)) == 0)
216*7132Sps29005 return (1);
217*7132Sps29005
218*7132Sps29005 #if defined(__sun) && defined(__SVR4)
219*7132Sps29005 /* does it match one of the host's configured interfaces */
220*7132Sps29005 if (match_interfaces(host) != 0)
221*7132Sps29005 return (1);
222*7132Sps29005 #endif
223*7132Sps29005 return (0);
224*7132Sps29005 }
225