2 * misc.c Various miscellaneous functions.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/autoconf.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
42 #include <freeradius-devel/missing.h>
43 #include <freeradius-devel/libradius.h>
50 * Return an IP address in standard dot notation
54 const char *ip_ntoa(char *buffer, uint32_t ipaddr)
56 ipaddr = ntohl(ipaddr);
58 sprintf(buffer, "%d.%d.%d.%d",
59 (ipaddr >> 24) & 0xff,
60 (ipaddr >> 16) & 0xff,
69 * Internal wrapper for locking, to minimize the number of ifdef's
71 * Lock an fd, prefer lockf() over flock()
73 int rad_lockfd(int fd, int lock_len)
75 #if defined(F_LOCK) && !defined(BSD)
76 return lockf(fd, F_LOCK, lock_len);
77 #elif defined(LOCK_EX)
78 return flock(fd, LOCK_EX);
85 fl.l_whence = SEEK_CUR;
86 return fcntl(fd, F_SETLKW, (void *)&fl);
91 * Internal wrapper for locking, to minimize the number of ifdef's
93 * Lock an fd, prefer lockf() over flock()
94 * Nonblocking version.
96 int rad_lockfd_nonblock(int fd, int lock_len)
98 #if defined(F_LOCK) && !defined(BSD)
99 return lockf(fd, F_TLOCK, lock_len);
100 #elif defined(LOCK_EX)
101 return flock(fd, LOCK_EX | LOCK_NB);
108 fl.l_whence = SEEK_CUR;
109 return fcntl(fd, F_SETLK, (void *)&fl);
114 * Internal wrapper for unlocking, to minimize the number of ifdef's
117 * Unlock an fd, prefer lockf() over flock()
119 int rad_unlockfd(int fd, int lock_len)
121 #if defined(F_LOCK) && !defined(BSD)
122 return lockf(fd, F_ULOCK, lock_len);
123 #elif defined(LOCK_EX)
124 return flock(fd, LOCK_UN);
131 fl.l_whence = SEEK_CUR;
132 return fcntl(fd, F_UNLCK, (void *)&fl);
137 * Return an interface-id in standard colon notation
139 char *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid)
141 snprintf(buffer, size, "%x:%x:%x:%x",
142 (ifid[0] << 8) + ifid[1], (ifid[2] << 8) + ifid[3],
143 (ifid[4] << 8) + ifid[5], (ifid[6] << 8) + ifid[7]);
149 * Return an interface-id from
150 * one supplied in standard colon notation.
152 uint8_t *ifid_aton(const char *ifid_str, uint8_t *ifid)
154 static const char xdigits[] = "0123456789abcdef";
156 int num_id = 0, val = 0, idx = 0;
158 for (p = ifid_str; ; ++p) {
159 if (*p == ':' || *p == '\0') {
164 * Drop 'val' into the array.
166 ifid[idx] = (val >> 8) & 0xff;
167 ifid[idx + 1] = val & 0xff;
170 * Must have all entries before
181 } else if ((pch = strchr(xdigits, tolower(*p))) != NULL) {
185 * Dumb version of 'scanf'
188 val |= (pch - xdigits);
196 #ifndef HAVE_INET_PTON
197 static int inet_pton4(const char *src, struct in_addr *dst)
203 static const char digits[] = "0123456789";
209 while (*p && ((off = strchr(digits, *p)) != NULL)) {
211 num += (off - digits);
213 if (num > 255) return 0;
220 * Not a digit, MUST be a dot, else we
232 * End of the string. At the fourth
233 * octet is OK, anything else is an
241 memcpy(dst, &tmp, sizeof(tmp));
247 * inet_pton6(src, dst)
248 * convert presentation level address to network order binary form.
250 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
252 * (1) does not touch `dst' unless it's returning 1.
253 * (2) :: in a full address is silently ignored.
255 * inspired by Mark Andrews.
260 inet_pton6(const char *src, unsigned char *dst)
262 static const char xdigits_l[] = "0123456789abcdef",
263 xdigits_u[] = "0123456789ABCDEF";
264 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
265 const char *xdigits, *curtok;
269 memset((tp = tmp), 0, IN6ADDRSZ);
270 endp = tp + IN6ADDRSZ;
272 /* Leading :: requires some special handling. */
279 while ((ch = *src++) != '\0') {
282 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
283 pch = strchr((xdigits = xdigits_u), ch);
286 val |= (pch - xdigits);
300 if (tp + INT16SZ > endp)
302 *tp++ = (u_char) (val >> 8) & 0xff;
303 *tp++ = (u_char) val & 0xff;
308 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
309 inet_pton4(curtok, tp) > 0) {
312 break; /* '\0' was seen by inet_pton4(). */
317 if (tp + INT16SZ > endp)
319 *tp++ = (u_char) (val >> 8) & 0xff;
320 *tp++ = (u_char) val & 0xff;
322 if (colonp != NULL) {
324 * Since some memmove()'s erroneously fail to handle
325 * overlapping regions, we'll do the shift by hand.
327 const int n = tp - colonp;
330 for (i = 1; i <= n; i++) {
331 endp[- i] = colonp[n - i];
338 /* bcopy(tmp, dst, IN6ADDRSZ); */
339 memcpy(dst, tmp, IN6ADDRSZ);
344 * Utility function, so that the rest of the server doesn't
345 * have ifdef's around IPv6 support
347 int inet_pton(int af, const char *src, void *dst)
350 return inet_pton4(src, dst);
353 if (af == AF_INET6) {
354 return inet_pton6(src, dst);
362 #ifndef HAVE_INET_NTOP
364 * Utility function, so that the rest of the server doesn't
365 * have ifdef's around IPv6 support
367 const char *inet_ntop(int af, const void *src, char *dst, size_t cnt)
370 const uint8_t *ipaddr = src;
372 if (cnt <= INET_ADDRSTRLEN) return NULL;
374 snprintf(dst, cnt, "%d.%d.%d.%d",
375 ipaddr[0], ipaddr[1],
376 ipaddr[2], ipaddr[3]);
381 * If the system doesn't define this, we define it
384 if (af == AF_INET6) {
385 const struct in6_addr *ipaddr = src;
387 if (cnt <= INET6_ADDRSTRLEN) return NULL;
389 snprintf(dst, cnt, "%x:%x:%x:%x:%x:%x:%x:%x",
390 (ipaddr->s6_addr[0] << 8) | ipaddr->s6_addr[1],
391 (ipaddr->s6_addr[2] << 8) | ipaddr->s6_addr[3],
392 (ipaddr->s6_addr[4] << 8) | ipaddr->s6_addr[5],
393 (ipaddr->s6_addr[6] << 8) | ipaddr->s6_addr[7],
394 (ipaddr->s6_addr[8] << 8) | ipaddr->s6_addr[9],
395 (ipaddr->s6_addr[10] << 8) | ipaddr->s6_addr[11],
396 (ipaddr->s6_addr[12] << 8) | ipaddr->s6_addr[13],
397 (ipaddr->s6_addr[14] << 8) | ipaddr->s6_addr[15]);
401 return NULL; /* don't support IPv6 */
407 * Wrappers for IPv4/IPv6 host to IP address lookup.
408 * This API returns only one IP address, of the specified
409 * address family, or the first address (of whatever family),
410 * if AF_UNSPEC is used.
412 int ip_hton(const char *src, int af, lrad_ipaddr_t *dst)
415 struct addrinfo hints, *ai = NULL, *res = NULL;
417 memset(&hints, 0, sizeof(hints));
418 hints.ai_family = af;
420 if ((error = getaddrinfo(src, NULL, &hints, &res)) != 0) {
421 librad_log("ip_nton: %s", gai_strerror(error));
425 for (ai = res; ai; ai = ai->ai_next) {
426 if ((af == ai->ai_family) || (af == AF_UNSPEC))
431 librad_log("ip_hton failed to find requested information for host %.100s", src);
436 switch (ai->ai_family) {
440 &((struct sockaddr_in*)ai->ai_addr)->sin_addr,
441 sizeof(struct in_addr));
447 &((struct sockaddr_in6*)ai->ai_addr)->sin6_addr,
448 sizeof(struct in6_addr));
451 /* Flow should never reach here */
454 librad_log("ip_hton found unusable information for host %.100s", src);
464 * Look IP addreses up, and print names (depending on DNS config)
466 const char *ip_ntoh(const lrad_ipaddr_t *src, char *dst, size_t cnt)
468 struct sockaddr_storage ss;
469 struct sockaddr_in *s4;
476 return inet_ntop(src->af, &(src->ipaddr), dst, cnt);
480 memset(&ss, 0, sizeof(ss));
483 s4 = (struct sockaddr_in *)&ss;
484 len = sizeof(struct sockaddr_in);
485 s4->sin_family = AF_INET;
487 memcpy(&s4->sin_addr, &src->ipaddr.ip4addr, 4);
490 #ifdef HAVE_STRUCT_SOCKADDR_IN6
493 struct sockaddr_in6 *s6;
495 s6 = (struct sockaddr_in6 *)&ss;
496 len = sizeof(struct sockaddr_in6);
497 s6->sin6_family = AF_INET6;
498 s6->sin6_flowinfo = 0;
500 memcpy(&s6->sin6_addr, &src->ipaddr.ip6addr, 16);
509 if ((error = getnameinfo((struct sockaddr *)&ss, len, dst, cnt, NULL, 0,
510 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
511 librad_log("ip_ntoh: %s", gai_strerror(error));
518 static const char *hextab = "0123456789abcdef";
523 * We allow: hex == bin
525 int lrad_hex2bin(const char *hex, uint8_t *bin, int len)
530 for (i = 0; i < len; i++) {
531 if(!(c1 = memchr(hextab, tolower((int) hex[i << 1]), 16)) ||
532 !(c2 = memchr(hextab, tolower((int) hex[(i << 1) + 1]), 16)))
534 bin[i] = ((c1-hextab)<<4) + (c2-hextab);
544 * If the output buffer isn't long enough, we have a buffer overflow.
546 void lrad_bin2hex(const uint8_t *bin, char *hex, int len)
550 for (i = 0; i < len; i++) {
551 hex[0] = hextab[((*bin) >> 4) & 0x0f];
552 hex[1] = hextab[*bin & 0x0f];
562 * So we don't have ifdef's in the rest of the code
564 #ifndef HAVE_CLOSEFROM
565 int closefrom(int fd)
571 maxfd = sysconf(_SC_OPEN_MAX);
577 if (fd > maxfd) return 0;
580 * FIXME: return EINTR?
584 for (i = fd; i < maxfd; i++) {