xref: /illumos-gate/usr/src/lib/print/libpapi-common/common/misc.c (revision dd51520e127b452179a2ce4ea3bd8dee949f9afe)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: misc.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*LINTLIBRARY*/
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <sys/types.h>
40 #include <papi.h>
41 #include <uri.h>
42 #include <config-site.h>
43 
44 /*
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
49  */
50 
51 #ifndef HAVE_STRLCPY
52 size_t
53 strlcpy(char *dst, const char *src, size_t len)
54 {
55 	size_t slen = strlen(src);
56 	size_t copied;
57 
58 	if (len == 0)
59 		return (slen);
60 
61 	if (slen >= len)
62 		copied = len - 1;
63 	else
64 		copied = slen;
65 	(void) memcpy(dst, src, copied);
66 	dst[copied] = '\0';
67 	return (slen);
68 }
69 #endif
70 
71 #ifndef HAVE_STRLCAT
72 size_t
73 strlcat(char *dst, const char *src, size_t dstsize)
74 {
75 	char *df = dst;
76 	size_t left = dstsize;
77 	size_t l1;
78 	size_t l2 = strlen(src);
79 	size_t copied;
80 
81 	while (left-- != 0 && *df != '\0')
82 		df++;
83 	l1 = df - dst;
84 	if (dstsize == l1)
85 		return (l1 + l2);
86 
87 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
88 	(void) memcpy(dst + l1, src, copied);
89 	dst[l1+copied] = '\0';
90 	return (l1 + l2);
91 }
92 #endif
93 
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>
99 #include <net/if.h>
100 #include <netinet/in.h>
101 #include <arpa/inet.h>
102 #include <netdb.h>
103 
104 static struct in6_addr **
105 local_interfaces()
106 {
107 	struct in6_addr **result = NULL;
108 	int s;
109 	struct lifnum n;
110 	struct lifconf c;
111 	struct lifreq *r;
112 	int count;
113 
114 	/* we need a socket to get the interfaces */
115 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
116 		return (0);
117 
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) {
122 		close(s);
123 		return (0);	/* no interfaces */
124 	}
125 
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) {
132 		free(c.lifc_buf);
133 		close(s);
134 		return (0);	/* can't get interface(s) configuration */
135 	}
136 	close(s);
137 
138 	r = c.lifc_req;
139 	for (count = c.lifc_len / sizeof (struct lifreq);
140 	    count > 0; count--, r++) {
141 		struct in6_addr v6[1], *addr = NULL;
142 
143 		switch (r->lifr_addr.ss_family) {
144 		case AF_INET: {
145 			struct sockaddr_in *s =
146 			    (struct sockaddr_in *)&r->lifr_addr;
147 			IN6_INADDR_TO_V4MAPPED(&s->sin_addr, v6);
148 			addr = v6;
149 			}
150 			break;
151 		case AF_INET6: {
152 			struct sockaddr_in6 *s =
153 			    (struct sockaddr_in6 *)&r->lifr_addr;
154 			addr = &s->sin6_addr;
155 			}
156 			break;
157 		}
158 
159 		if (addr != NULL) {
160 			struct in6_addr *a = malloc(sizeof (*a));
161 
162 			memcpy(a, addr, sizeof (*a));
163 			list_append(&result, a);
164 		}
165 	}
166 	free(c.lifc_buf);
167 
168 	return (result);
169 }
170 
171 static int
172 match_interfaces(char *host)
173 {
174 	struct in6_addr **lif = local_interfaces();
175 	struct hostent *hp;
176 	int rc = 0;
177 	int errnum;
178 
179 	/* are there any local interfaces */
180 	if (lif == NULL)
181 		return (0);
182 
183 	/* cycle through the host db addresses */
184 	hp = getipnodebyname(host, AF_INET6, AI_ALL|AI_V4MAPPED, &errnum);
185 	if (hp != NULL) {
186 		struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list;
187 		int i;
188 
189 		for (i = 0; ((rc == 0) && (tmp[i] != NULL)); i++) {
190 			int j;
191 
192 			for (j = 0; ((rc == 0) && (lif[j] != NULL)); j++)
193 				if (memcmp(tmp[i], lif[j],
194 				    sizeof (struct in6_addr)) == 0)
195 					rc = 1;
196 		}
197 	}
198 	free(lif);
199 
200 	return (rc);
201 }
202 #endif
203 
204 int
205 is_localhost(char *host)
206 {
207 	char hostname[BUFSIZ];
208 
209 	/* is it "localhost" */
210 	if (strncasecmp(host, "localhost", 10) == 0)
211 		return (1);
212 
213 	/* is it the {nodename} */
214 	sysinfo(SI_HOSTNAME, hostname, sizeof (hostname));
215 	if (strncasecmp(host, hostname, strlen(hostname)) == 0)
216 		return (1);
217 
218 #if defined(__sun) && defined(__SVR4)
219 	/* does it match one of the host's configured interfaces */
220 	if (match_interfaces(host) != 0)
221 		return (1);
222 #endif
223 	return (0);
224 }
225