1355b4669Sjacobs /*
2355b4669Sjacobs * CDDL HEADER START
3355b4669Sjacobs *
4355b4669Sjacobs * The contents of this file are subject to the terms of the
5355b4669Sjacobs * Common Development and Distribution License (the "License").
6355b4669Sjacobs * You may not use this file except in compliance with the License.
7355b4669Sjacobs *
8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs * See the License for the specific language governing permissions
11355b4669Sjacobs * and limitations under the License.
12355b4669Sjacobs *
13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs *
19355b4669Sjacobs * CDDL HEADER END
20355b4669Sjacobs */
21355b4669Sjacobs
22355b4669Sjacobs /*
23*a18dc42fSps29005 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24355b4669Sjacobs * Use is subject to license terms.
25355b4669Sjacobs *
26355b4669Sjacobs */
27355b4669Sjacobs
28355b4669Sjacobs /* $Id: misc.c 146 2006-03-24 00:26:54Z njacobs $ */
29355b4669Sjacobs
30355b4669Sjacobs /*LINTLIBRARY*/
31355b4669Sjacobs
32*a18dc42fSps29005 #include <stdio.h>
33*a18dc42fSps29005 #include <stdlib.h>
34*a18dc42fSps29005 #include <unistd.h>
35355b4669Sjacobs #include <string.h>
36*a18dc42fSps29005 #include <ctype.h>
37*a18dc42fSps29005 #include <sys/types.h>
38355b4669Sjacobs #include <papi.h>
39*a18dc42fSps29005 #include <uri.h>
40355b4669Sjacobs #include <config-site.h>
41355b4669Sjacobs
42355b4669Sjacobs /*
43355b4669Sjacobs * The implementations of strlcpy() and strlcat() have been taken directly
44355b4669Sjacobs * from OpenSolaris. The contents of this file originated from
45355b4669Sjacobs * usr/src/lib/libc/port/gen/strlcpy.c
46355b4669Sjacobs * usr/src/lib/libc/port/gen/strcat.c
47355b4669Sjacobs */
48355b4669Sjacobs
49355b4669Sjacobs #ifndef HAVE_STRLCPY
50355b4669Sjacobs size_t
strlcpy(char * dst,const char * src,size_t len)51355b4669Sjacobs strlcpy(char *dst, const char *src, size_t len)
52355b4669Sjacobs {
53355b4669Sjacobs size_t slen = strlen(src);
54355b4669Sjacobs size_t copied;
55355b4669Sjacobs
56355b4669Sjacobs if (len == 0)
57355b4669Sjacobs return (slen);
58355b4669Sjacobs
59355b4669Sjacobs if (slen >= len)
60355b4669Sjacobs copied = len - 1;
61355b4669Sjacobs else
62355b4669Sjacobs copied = slen;
63355b4669Sjacobs (void) memcpy(dst, src, copied);
64355b4669Sjacobs dst[copied] = '\0';
65355b4669Sjacobs return (slen);
66355b4669Sjacobs }
67355b4669Sjacobs #endif
68355b4669Sjacobs
69355b4669Sjacobs #ifndef HAVE_STRLCAT
70355b4669Sjacobs size_t
strlcat(char * dst,const char * src,size_t dstsize)71355b4669Sjacobs strlcat(char *dst, const char *src, size_t dstsize)
72355b4669Sjacobs {
73355b4669Sjacobs char *df = dst;
74355b4669Sjacobs size_t left = dstsize;
75355b4669Sjacobs size_t l1;
76355b4669Sjacobs size_t l2 = strlen(src);
77355b4669Sjacobs size_t copied;
78355b4669Sjacobs
79355b4669Sjacobs while (left-- != 0 && *df != '\0')
80355b4669Sjacobs df++;
81355b4669Sjacobs l1 = df - dst;
82355b4669Sjacobs if (dstsize == l1)
83355b4669Sjacobs return (l1 + l2);
84355b4669Sjacobs
85355b4669Sjacobs copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
86355b4669Sjacobs (void) memcpy(dst + l1, src, copied);
87355b4669Sjacobs dst[l1+copied] = '\0';
88355b4669Sjacobs return (l1 + l2);
89355b4669Sjacobs }
90355b4669Sjacobs #endif
91*a18dc42fSps29005
92*a18dc42fSps29005 #if defined(__sun) && defined(__SVR4)
93*a18dc42fSps29005 #include <sys/systeminfo.h>
94*a18dc42fSps29005 #include <sys/socket.h>
95*a18dc42fSps29005 #include <sys/ioctl.h>
96*a18dc42fSps29005 #include <sys/sockio.h>
97*a18dc42fSps29005 #include <net/if.h>
98*a18dc42fSps29005 #include <netinet/in.h>
99*a18dc42fSps29005 #include <arpa/inet.h>
100*a18dc42fSps29005 #include <netdb.h>
101*a18dc42fSps29005
102*a18dc42fSps29005 static struct in6_addr **
local_interfaces()103*a18dc42fSps29005 local_interfaces()
104*a18dc42fSps29005 {
105*a18dc42fSps29005 struct in6_addr **result = NULL;
106*a18dc42fSps29005 int s;
107*a18dc42fSps29005 struct lifnum n;
108*a18dc42fSps29005 struct lifconf c;
109*a18dc42fSps29005 struct lifreq *r;
110*a18dc42fSps29005 int count;
111*a18dc42fSps29005
112*a18dc42fSps29005 /* we need a socket to get the interfaces */
113*a18dc42fSps29005 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
114*a18dc42fSps29005 return (0);
115*a18dc42fSps29005
116*a18dc42fSps29005 /* get the number of interfaces */
117*a18dc42fSps29005 memset(&n, 0, sizeof (n));
118*a18dc42fSps29005 n.lifn_family = AF_UNSPEC;
119*a18dc42fSps29005 if (ioctl(s, SIOCGLIFNUM, (char *)&n) < 0) {
120*a18dc42fSps29005 close(s);
121*a18dc42fSps29005 return (0); /* no interfaces */
122*a18dc42fSps29005 }
123*a18dc42fSps29005
124*a18dc42fSps29005 /* get the interface(s) configuration */
125*a18dc42fSps29005 memset(&c, 0, sizeof (c));
126*a18dc42fSps29005 c.lifc_family = AF_UNSPEC;
127*a18dc42fSps29005 c.lifc_buf = calloc(n.lifn_count, sizeof (struct lifreq));
128*a18dc42fSps29005 c.lifc_len = (n.lifn_count * sizeof (struct lifreq));
129*a18dc42fSps29005 if (ioctl(s, SIOCGLIFCONF, (char *)&c) < 0) {
130*a18dc42fSps29005 free(c.lifc_buf);
131*a18dc42fSps29005 close(s);
132*a18dc42fSps29005 return (0); /* can't get interface(s) configuration */
133*a18dc42fSps29005 }
134*a18dc42fSps29005 close(s);
135*a18dc42fSps29005
136*a18dc42fSps29005 r = c.lifc_req;
137*a18dc42fSps29005 for (count = c.lifc_len / sizeof (struct lifreq);
138*a18dc42fSps29005 count > 0; count--, r++) {
139*a18dc42fSps29005 struct in6_addr v6[1], *addr = NULL;
140*a18dc42fSps29005
141*a18dc42fSps29005 switch (r->lifr_addr.ss_family) {
142*a18dc42fSps29005 case AF_INET: {
143*a18dc42fSps29005 struct sockaddr_in *s =
144*a18dc42fSps29005 (struct sockaddr_in *)&r->lifr_addr;
145*a18dc42fSps29005 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, v6);
146*a18dc42fSps29005 addr = v6;
147*a18dc42fSps29005 }
148*a18dc42fSps29005 break;
149*a18dc42fSps29005 case AF_INET6: {
150*a18dc42fSps29005 struct sockaddr_in6 *s =
151*a18dc42fSps29005 (struct sockaddr_in6 *)&r->lifr_addr;
152*a18dc42fSps29005 addr = &s->sin6_addr;
153*a18dc42fSps29005 }
154*a18dc42fSps29005 break;
155*a18dc42fSps29005 }
156*a18dc42fSps29005
157*a18dc42fSps29005 if (addr != NULL) {
158*a18dc42fSps29005 struct in6_addr *a = malloc(sizeof (*a));
159*a18dc42fSps29005
160*a18dc42fSps29005 memcpy(a, addr, sizeof (*a));
161*a18dc42fSps29005 list_append(&result, a);
162*a18dc42fSps29005 }
163*a18dc42fSps29005 }
164*a18dc42fSps29005 free(c.lifc_buf);
165*a18dc42fSps29005
166*a18dc42fSps29005 return (result);
167*a18dc42fSps29005 }
168*a18dc42fSps29005
169*a18dc42fSps29005 static int
match_interfaces(char * host)170*a18dc42fSps29005 match_interfaces(char *host)
171*a18dc42fSps29005 {
172*a18dc42fSps29005 struct in6_addr **lif = local_interfaces();
173*a18dc42fSps29005 struct hostent *hp;
174*a18dc42fSps29005 int rc = 0;
175*a18dc42fSps29005 int errnum;
176*a18dc42fSps29005
177*a18dc42fSps29005 /* are there any local interfaces */
178*a18dc42fSps29005 if (lif == NULL)
179*a18dc42fSps29005 return (0);
180*a18dc42fSps29005
181*a18dc42fSps29005 /* cycle through the host db addresses */
182*a18dc42fSps29005 hp = getipnodebyname(host, AF_INET6, AI_ALL|AI_V4MAPPED, &errnum);
183*a18dc42fSps29005 if (hp != NULL) {
184*a18dc42fSps29005 struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list;
185*a18dc42fSps29005 int i;
186*a18dc42fSps29005
187*a18dc42fSps29005 for (i = 0; ((rc == 0) && (tmp[i] != NULL)); i++) {
188*a18dc42fSps29005 int j;
189*a18dc42fSps29005
190*a18dc42fSps29005 for (j = 0; ((rc == 0) && (lif[j] != NULL)); j++)
191*a18dc42fSps29005 if (memcmp(tmp[i], lif[j],
192*a18dc42fSps29005 sizeof (struct in6_addr)) == 0)
193*a18dc42fSps29005 rc = 1;
194*a18dc42fSps29005 }
195*a18dc42fSps29005 }
196*a18dc42fSps29005 free(lif);
197*a18dc42fSps29005
198*a18dc42fSps29005 return (rc);
199*a18dc42fSps29005 }
200*a18dc42fSps29005 #endif
201*a18dc42fSps29005
202*a18dc42fSps29005 int
is_localhost(char * host)203*a18dc42fSps29005 is_localhost(char *host)
204*a18dc42fSps29005 {
205*a18dc42fSps29005 char hostname[BUFSIZ];
206*a18dc42fSps29005
207*a18dc42fSps29005 /* is it "localhost" */
208*a18dc42fSps29005 if (strncasecmp(host, "localhost", 10) == 0)
209*a18dc42fSps29005 return (1);
210*a18dc42fSps29005
211*a18dc42fSps29005 /* is it the {nodename} */
212*a18dc42fSps29005 sysinfo(SI_HOSTNAME, hostname, sizeof (hostname));
213*a18dc42fSps29005 if (strncasecmp(host, hostname, strlen(hostname)) == 0)
214*a18dc42fSps29005 return (1);
215*a18dc42fSps29005
216*a18dc42fSps29005 #if defined(__sun) && defined(__SVR4)
217*a18dc42fSps29005 /* does it match one of the host's configured interfaces */
218*a18dc42fSps29005 if (match_interfaces(host) != 0)
219*a18dc42fSps29005 return (1);
220*a18dc42fSps29005 #endif
221*a18dc42fSps29005 return (0);
222*a18dc42fSps29005 }
223