xref: /illumos-gate/usr/src/lib/print/libpapi-common/common/misc.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 /*LINTLIBRARY*/
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <sys/types.h>
38 #include <papi.h>
39 #include <uri.h>
40 #include <config-site.h>
41 
42 /*
43  * The implementations of strlcpy() and strlcat() have been taken directly
44  * from OpenSolaris.  The contents of this file originated from
45  *     usr/src/lib/libc/port/gen/strlcpy.c
46  *     usr/src/lib/libc/port/gen/strcat.c
47  */
48 
49 #ifndef HAVE_STRLCPY
50 size_t
51 strlcpy(char *dst, const char *src, size_t len)
52 {
53 	size_t slen = strlen(src);
54 	size_t copied;
55 
56 	if (len == 0)
57 		return (slen);
58 
59 	if (slen >= len)
60 		copied = len - 1;
61 	else
62 		copied = slen;
63 	(void) memcpy(dst, src, copied);
64 	dst[copied] = '\0';
65 	return (slen);
66 }
67 #endif
68 
69 #ifndef HAVE_STRLCAT
70 size_t
71 strlcat(char *dst, const char *src, size_t dstsize)
72 {
73 	char *df = dst;
74 	size_t left = dstsize;
75 	size_t l1;
76 	size_t l2 = strlen(src);
77 	size_t copied;
78 
79 	while (left-- != 0 && *df != '\0')
80 		df++;
81 	l1 = df - dst;
82 	if (dstsize == l1)
83 		return (l1 + l2);
84 
85 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
86 	(void) memcpy(dst + l1, src, copied);
87 	dst[l1+copied] = '\0';
88 	return (l1 + l2);
89 }
90 #endif
91 
92 #if defined(__sun) && defined(__SVR4)
93 #include <sys/systeminfo.h>
94 #include <sys/socket.h>
95 #include <sys/ioctl.h>
96 #include <sys/sockio.h>
97 #include <net/if.h>
98 #include <netinet/in.h>
99 #include <arpa/inet.h>
100 #include <netdb.h>
101 
102 static struct in6_addr **
103 local_interfaces()
104 {
105 	struct in6_addr **result = NULL;
106 	int s;
107 	struct lifnum n;
108 	struct lifconf c;
109 	struct lifreq *r;
110 	int count;
111 
112 	/* we need a socket to get the interfaces */
113 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
114 		return (0);
115 
116 	/* get the number of interfaces */
117 	memset(&n, 0, sizeof (n));
118 	n.lifn_family = AF_UNSPEC;
119 	if (ioctl(s, SIOCGLIFNUM, (char *)&n) < 0) {
120 		close(s);
121 		return (0);	/* no interfaces */
122 	}
123 
124 	/* get the interface(s) configuration */
125 	memset(&c, 0, sizeof (c));
126 	c.lifc_family = AF_UNSPEC;
127 	c.lifc_buf = calloc(n.lifn_count, sizeof (struct lifreq));
128 	c.lifc_len = (n.lifn_count * sizeof (struct lifreq));
129 	if (ioctl(s, SIOCGLIFCONF, (char *)&c) < 0) {
130 		free(c.lifc_buf);
131 		close(s);
132 		return (0);	/* can't get interface(s) configuration */
133 	}
134 	close(s);
135 
136 	r = c.lifc_req;
137 	for (count = c.lifc_len / sizeof (struct lifreq);
138 	    count > 0; count--, r++) {
139 		struct in6_addr v6[1], *addr = NULL;
140 
141 		switch (r->lifr_addr.ss_family) {
142 		case AF_INET: {
143 			struct sockaddr_in *s =
144 			    (struct sockaddr_in *)&r->lifr_addr;
145 			IN6_INADDR_TO_V4MAPPED(&s->sin_addr, v6);
146 			addr = v6;
147 			}
148 			break;
149 		case AF_INET6: {
150 			struct sockaddr_in6 *s =
151 			    (struct sockaddr_in6 *)&r->lifr_addr;
152 			addr = &s->sin6_addr;
153 			}
154 			break;
155 		}
156 
157 		if (addr != NULL) {
158 			struct in6_addr *a = malloc(sizeof (*a));
159 
160 			memcpy(a, addr, sizeof (*a));
161 			list_append(&result, a);
162 		}
163 	}
164 	free(c.lifc_buf);
165 
166 	return (result);
167 }
168 
169 static int
170 match_interfaces(char *host)
171 {
172 	struct in6_addr **lif = local_interfaces();
173 	struct hostent *hp;
174 	int rc = 0;
175 	int errnum;
176 
177 	/* are there any local interfaces */
178 	if (lif == NULL)
179 		return (0);
180 
181 	/* cycle through the host db addresses */
182 	hp = getipnodebyname(host, AF_INET6, AI_ALL|AI_V4MAPPED, &errnum);
183 	if (hp != NULL) {
184 		struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list;
185 		int i;
186 
187 		for (i = 0; ((rc == 0) && (tmp[i] != NULL)); i++) {
188 			int j;
189 
190 			for (j = 0; ((rc == 0) && (lif[j] != NULL)); j++)
191 				if (memcmp(tmp[i], lif[j],
192 				    sizeof (struct in6_addr)) == 0)
193 					rc = 1;
194 		}
195 	}
196 	free(lif);
197 
198 	return (rc);
199 }
200 #endif
201 
202 int
203 is_localhost(char *host)
204 {
205 	char hostname[BUFSIZ];
206 
207 	/* is it "localhost" */
208 	if (strncasecmp(host, "localhost", 10) == 0)
209 		return (1);
210 
211 	/* is it the {nodename} */
212 	sysinfo(SI_HOSTNAME, hostname, sizeof (hostname));
213 	if (strncasecmp(host, hostname, strlen(hostname)) == 0)
214 		return (1);
215 
216 #if defined(__sun) && defined(__SVR4)
217 	/* does it match one of the host's configured interfaces */
218 	if (match_interfaces(host) != 0)
219 		return (1);
220 #endif
221 	return (0);
222 }
223