2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 RCSID("$Id: addr_families.c 22039 2007-11-10 11:47:35Z lha $");
38 struct addr_operations {
40 krb5_address_type atype;
41 size_t max_sockaddr_size;
42 krb5_error_code (*sockaddr2addr)(const struct sockaddr *, krb5_address *);
43 krb5_error_code (*sockaddr2port)(const struct sockaddr *, int16_t *);
44 void (*addr2sockaddr)(const krb5_address *, struct sockaddr *,
45 krb5_socklen_t *sa_size, int port);
46 void (*h_addr2sockaddr)(const char *, struct sockaddr *, krb5_socklen_t *, int);
47 krb5_error_code (*h_addr2addr)(const char *, krb5_address *);
48 krb5_boolean (*uninteresting)(const struct sockaddr *);
49 void (*anyaddr)(struct sockaddr *, krb5_socklen_t *, int);
50 int (*print_addr)(const krb5_address *, char *, size_t);
51 int (*parse_addr)(krb5_context, const char*, krb5_address *);
52 int (*order_addr)(krb5_context, const krb5_address*, const krb5_address*);
53 int (*free_addr)(krb5_context, krb5_address*);
54 int (*copy_addr)(krb5_context, const krb5_address*, krb5_address*);
55 int (*mask_boundary)(krb5_context, const krb5_address*, unsigned long,
56 krb5_address*, krb5_address*);
60 * AF_INET - aka IPv4 implementation
63 static krb5_error_code
64 ipv4_sockaddr2addr (const struct sockaddr *sa, krb5_address *a)
66 const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
69 a->addr_type = KRB5_ADDRESS_INET;
70 memcpy (buf, &sin4->sin_addr, 4);
71 return krb5_data_copy(&a->address, buf, 4);
74 static krb5_error_code
75 ipv4_sockaddr2port (const struct sockaddr *sa, int16_t *port)
77 const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
79 *port = sin4->sin_port;
84 ipv4_addr2sockaddr (const krb5_address *a,
86 krb5_socklen_t *sa_size,
89 struct sockaddr_in tmp;
91 memset (&tmp, 0, sizeof(tmp));
92 tmp.sin_family = AF_INET;
93 memcpy (&tmp.sin_addr, a->address.data, 4);
95 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
96 *sa_size = sizeof(tmp);
100 ipv4_h_addr2sockaddr(const char *addr,
102 krb5_socklen_t *sa_size,
105 struct sockaddr_in tmp;
107 memset (&tmp, 0, sizeof(tmp));
108 tmp.sin_family = AF_INET;
110 tmp.sin_addr = *((const struct in_addr *)addr);
111 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
112 *sa_size = sizeof(tmp);
115 static krb5_error_code
116 ipv4_h_addr2addr (const char *addr,
119 unsigned char buf[4];
121 a->addr_type = KRB5_ADDRESS_INET;
122 memcpy(buf, addr, 4);
123 return krb5_data_copy(&a->address, buf, 4);
127 * Are there any addresses that should be considered `uninteresting'?
131 ipv4_uninteresting (const struct sockaddr *sa)
133 const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
135 if (sin4->sin_addr.s_addr == INADDR_ANY)
142 ipv4_anyaddr (struct sockaddr *sa, krb5_socklen_t *sa_size, int port)
144 struct sockaddr_in tmp;
146 memset (&tmp, 0, sizeof(tmp));
147 tmp.sin_family = AF_INET;
149 tmp.sin_addr.s_addr = INADDR_ANY;
150 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
151 *sa_size = sizeof(tmp);
155 ipv4_print_addr (const krb5_address *addr, char *str, size_t len)
159 memcpy (&ia, addr->address.data, 4);
161 return snprintf (str, len, "IPv4:%s", inet_ntoa(ia));
165 ipv4_parse_addr (krb5_context context, const char *address, krb5_address *addr)
170 p = strchr(address, ':');
173 if(strncasecmp(address, "ip:", p - address) != 0 &&
174 strncasecmp(address, "ip4:", p - address) != 0 &&
175 strncasecmp(address, "ipv4:", p - address) != 0 &&
176 strncasecmp(address, "inet:", p - address) != 0)
180 #ifdef HAVE_INET_ATON
181 if(inet_aton(p, &a) == 0)
183 #elif defined(HAVE_INET_ADDR)
184 a.s_addr = inet_addr(p);
185 if(a.s_addr == INADDR_NONE)
190 addr->addr_type = KRB5_ADDRESS_INET;
191 if(krb5_data_alloc(&addr->address, 4) != 0)
193 _krb5_put_int(addr->address.data, ntohl(a.s_addr), addr->address.length);
198 ipv4_mask_boundary(krb5_context context, const krb5_address *inaddr,
199 unsigned long len, krb5_address *low, krb5_address *high)
202 uint32_t l, h, m = 0xffffffff;
205 krb5_set_error_string(context, "IPv4 prefix too large (%ld)", len);
206 return KRB5_PROG_ATYPE_NOSUPP;
210 _krb5_get_int(inaddr->address.data, &ia, inaddr->address.length);
215 low->addr_type = KRB5_ADDRESS_INET;
216 if(krb5_data_alloc(&low->address, 4) != 0)
218 _krb5_put_int(low->address.data, l, low->address.length);
220 high->addr_type = KRB5_ADDRESS_INET;
221 if(krb5_data_alloc(&high->address, 4) != 0) {
222 krb5_free_address(context, low);
225 _krb5_put_int(high->address.data, h, high->address.length);
232 * AF_INET6 - aka IPv6 implementation
237 static krb5_error_code
238 ipv6_sockaddr2addr (const struct sockaddr *sa, krb5_address *a)
240 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
242 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
243 unsigned char buf[4];
245 a->addr_type = KRB5_ADDRESS_INET;
246 #ifndef IN6_ADDR_V6_TO_V4
247 #ifdef IN6_EXTRACT_V4ADDR
248 #define IN6_ADDR_V6_TO_V4(x) (&IN6_EXTRACT_V4ADDR(x))
250 #define IN6_ADDR_V6_TO_V4(x) ((const struct in_addr *)&(x)->s6_addr[12])
253 memcpy (buf, IN6_ADDR_V6_TO_V4(&sin6->sin6_addr), 4);
254 return krb5_data_copy(&a->address, buf, 4);
256 a->addr_type = KRB5_ADDRESS_INET6;
257 return krb5_data_copy(&a->address,
259 sizeof(sin6->sin6_addr));
263 static krb5_error_code
264 ipv6_sockaddr2port (const struct sockaddr *sa, int16_t *port)
266 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
268 *port = sin6->sin6_port;
273 ipv6_addr2sockaddr (const krb5_address *a,
275 krb5_socklen_t *sa_size,
278 struct sockaddr_in6 tmp;
280 memset (&tmp, 0, sizeof(tmp));
281 tmp.sin6_family = AF_INET6;
282 memcpy (&tmp.sin6_addr, a->address.data, sizeof(tmp.sin6_addr));
283 tmp.sin6_port = port;
284 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
285 *sa_size = sizeof(tmp);
289 ipv6_h_addr2sockaddr(const char *addr,
291 krb5_socklen_t *sa_size,
294 struct sockaddr_in6 tmp;
296 memset (&tmp, 0, sizeof(tmp));
297 tmp.sin6_family = AF_INET6;
298 tmp.sin6_port = port;
299 tmp.sin6_addr = *((const struct in6_addr *)addr);
300 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
301 *sa_size = sizeof(tmp);
304 static krb5_error_code
305 ipv6_h_addr2addr (const char *addr,
308 a->addr_type = KRB5_ADDRESS_INET6;
309 return krb5_data_copy(&a->address, addr, sizeof(struct in6_addr));
317 ipv6_uninteresting (const struct sockaddr *sa)
319 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
320 const struct in6_addr *in6 = (const struct in6_addr *)&sin6->sin6_addr;
323 IN6_IS_ADDR_LINKLOCAL(in6)
324 || IN6_IS_ADDR_V4COMPAT(in6);
328 ipv6_anyaddr (struct sockaddr *sa, krb5_socklen_t *sa_size, int port)
330 struct sockaddr_in6 tmp;
332 memset (&tmp, 0, sizeof(tmp));
333 tmp.sin6_family = AF_INET6;
334 tmp.sin6_port = port;
335 tmp.sin6_addr = in6addr_any;
336 *sa_size = sizeof(tmp);
340 ipv6_print_addr (const krb5_address *addr, char *str, size_t len)
342 char buf[128], buf2[3];
343 #ifdef HAVE_INET_NTOP
344 if(inet_ntop(AF_INET6, addr->address.data, buf, sizeof(buf)) == NULL)
347 /* XXX this is pretty ugly, but better than abort() */
349 unsigned char *p = addr->address.data;
351 for(i = 0; i < addr->address.length; i++) {
352 snprintf(buf2, sizeof(buf2), "%02x", p[i]);
353 if(i > 0 && (i & 1) == 0)
354 strlcat(buf, ":", sizeof(buf));
355 strlcat(buf, buf2, sizeof(buf));
358 return snprintf(str, len, "IPv6:%s", buf);
362 ipv6_parse_addr (krb5_context context, const char *address, krb5_address *addr)
368 p = strchr(address, ':');
371 if(strncasecmp(address, "ip6:", p - address) == 0 ||
372 strncasecmp(address, "ipv6:", p - address) == 0 ||
373 strncasecmp(address, "inet6:", p - address) == 0)
377 ret = inet_pton(AF_INET6, address, &in6.s6_addr);
379 addr->addr_type = KRB5_ADDRESS_INET6;
380 ret = krb5_data_alloc(&addr->address, sizeof(in6.s6_addr));
383 memcpy(addr->address.data, in6.s6_addr, sizeof(in6.s6_addr));
390 ipv6_mask_boundary(krb5_context context, const krb5_address *inaddr,
391 unsigned long len, krb5_address *low, krb5_address *high)
393 struct in6_addr addr, laddr, haddr;
398 krb5_set_error_string(context, "IPv6 prefix too large (%ld)", len);
399 return KRB5_PROG_ATYPE_NOSUPP;
402 if (inaddr->address.length != sizeof(addr)) {
403 krb5_set_error_string(context, "IPv6 addr bad length");
404 return KRB5_PROG_ATYPE_NOSUPP;
407 memcpy(&addr, inaddr->address.data, inaddr->address.length);
409 for (i = 0; i < 16; i++) {
410 sub_len = min(8, len);
412 m = 0xff << (8 - sub_len);
414 laddr.s6_addr[i] = addr.s6_addr[i] & m;
415 haddr.s6_addr[i] = (addr.s6_addr[i] & m) | ~m;
423 low->addr_type = KRB5_ADDRESS_INET6;
424 if (krb5_data_alloc(&low->address, sizeof(laddr.s6_addr)) != 0)
426 memcpy(low->address.data, laddr.s6_addr, sizeof(laddr.s6_addr));
428 high->addr_type = KRB5_ADDRESS_INET6;
429 if (krb5_data_alloc(&high->address, sizeof(haddr.s6_addr)) != 0) {
430 krb5_free_address(context, low);
433 memcpy(high->address.data, haddr.s6_addr, sizeof(haddr.s6_addr));
444 #define KRB5_ADDRESS_ARANGE (-100)
452 arange_parse_addr (krb5_context context,
453 const char *address, krb5_address *addr)
456 krb5_address low0, high0;
460 if(strncasecmp(address, "RANGE:", 6) != 0)
465 p = strrchr(address, '/');
467 krb5_addresses addrmask;
471 if (strlcpy(buf, address, sizeof(buf)) > sizeof(buf))
473 buf[p - address] = '\0';
474 ret = krb5_parse_address(context, buf, &addrmask);
477 if(addrmask.len != 1) {
478 krb5_free_addresses(context, &addrmask);
482 address += p - address + 1;
484 num = strtol(address, &q, 10);
485 if (q == address || *q != '\0' || num < 0) {
486 krb5_free_addresses(context, &addrmask);
490 ret = krb5_address_prefixlen_boundary(context, &addrmask.val[0], num,
492 krb5_free_addresses(context, &addrmask);
497 krb5_addresses low, high;
499 strsep_copy(&address, "-", buf, sizeof(buf));
500 ret = krb5_parse_address(context, buf, &low);
504 krb5_free_addresses(context, &low);
508 strsep_copy(&address, "-", buf, sizeof(buf));
509 ret = krb5_parse_address(context, buf, &high);
511 krb5_free_addresses(context, &low);
515 if(high.len != 1 && high.val[0].addr_type != low.val[0].addr_type) {
516 krb5_free_addresses(context, &low);
517 krb5_free_addresses(context, &high);
521 ret = krb5_copy_address(context, &high.val[0], &high0);
523 ret = krb5_copy_address(context, &low.val[0], &low0);
525 krb5_free_address(context, &high0);
527 krb5_free_addresses(context, &low);
528 krb5_free_addresses(context, &high);
533 krb5_data_alloc(&addr->address, sizeof(*a));
534 addr->addr_type = KRB5_ADDRESS_ARANGE;
535 a = addr->address.data;
537 if(krb5_address_order(context, &low0, &high0) < 0) {
548 arange_free (krb5_context context, krb5_address *addr)
551 a = addr->address.data;
552 krb5_free_address(context, &a->low);
553 krb5_free_address(context, &a->high);
554 krb5_data_free(&addr->address);
560 arange_copy (krb5_context context, const krb5_address *inaddr,
561 krb5_address *outaddr)
564 struct arange *i, *o;
566 outaddr->addr_type = KRB5_ADDRESS_ARANGE;
567 ret = krb5_data_alloc(&outaddr->address, sizeof(*o));
570 i = inaddr->address.data;
571 o = outaddr->address.data;
572 ret = krb5_copy_address(context, &i->low, &o->low);
574 krb5_data_free(&outaddr->address);
577 ret = krb5_copy_address(context, &i->high, &o->high);
579 krb5_free_address(context, &o->low);
580 krb5_data_free(&outaddr->address);
587 arange_print_addr (const krb5_address *addr, char *str, size_t len)
591 size_t l, size, ret_len;
593 a = addr->address.data;
595 l = strlcpy(str, "RANGE:", len);
601 ret = krb5_print_address (&a->low, str + size, len - size, &l);
610 l = strlcat(str + size, "-", len - size);
617 ret = krb5_print_address (&a->high, str + size, len - size, &l);
626 arange_order_addr(krb5_context context,
627 const krb5_address *addr1,
628 const krb5_address *addr2)
630 int tmp1, tmp2, sign;
632 const krb5_address *a2;
634 if(addr1->addr_type == KRB5_ADDRESS_ARANGE) {
635 a = addr1->address.data;
638 } else if(addr2->addr_type == KRB5_ADDRESS_ARANGE) {
639 a = addr2->address.data;
645 if(a2->addr_type == KRB5_ADDRESS_ARANGE) {
646 struct arange *b = a2->address.data;
647 tmp1 = krb5_address_order(context, &a->low, &b->low);
650 return sign * krb5_address_order(context, &a->high, &b->high);
651 } else if(a2->addr_type == a->low.addr_type) {
652 tmp1 = krb5_address_order(context, &a->low, a2);
655 tmp2 = krb5_address_order(context, &a->high, a2);
660 return sign * (addr1->addr_type - addr2->addr_type);
665 addrport_print_addr (const krb5_address *addr, char *str, size_t len)
668 krb5_address addr1, addr2;
670 size_t ret_len = 0, l, size = 0;
673 sp = krb5_storage_from_data((krb5_data*)rk_UNCONST(&addr->address));
674 /* for totally obscure reasons, these are not in network byteorder */
675 krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_LE);
677 krb5_storage_seek(sp, 2, SEEK_CUR); /* skip first two bytes */
678 krb5_ret_address(sp, &addr1);
680 krb5_storage_seek(sp, 2, SEEK_CUR); /* skip two bytes */
681 krb5_ret_address(sp, &addr2);
682 krb5_storage_free(sp);
683 if(addr2.addr_type == KRB5_ADDRESS_IPPORT && addr2.address.length == 2) {
685 _krb5_get_int(addr2.address.data, &value, 2);
688 l = strlcpy(str, "ADDRPORT:", len);
695 ret = krb5_print_address(&addr1, str + size, len - size, &l);
704 ret = snprintf(str + size, len - size, ",PORT=%u", port);
711 static struct addr_operations at[] = {
712 {AF_INET, KRB5_ADDRESS_INET, sizeof(struct sockaddr_in),
716 ipv4_h_addr2sockaddr,
718 ipv4_uninteresting, ipv4_anyaddr, ipv4_print_addr, ipv4_parse_addr,
719 NULL, NULL, NULL, ipv4_mask_boundary },
721 {AF_INET6, KRB5_ADDRESS_INET6, sizeof(struct sockaddr_in6),
725 ipv6_h_addr2sockaddr,
727 ipv6_uninteresting, ipv6_anyaddr, ipv6_print_addr, ipv6_parse_addr,
728 NULL, NULL, NULL, ipv6_mask_boundary } ,
730 {KRB5_ADDRESS_ADDRPORT, KRB5_ADDRESS_ADDRPORT, 0,
731 NULL, NULL, NULL, NULL, NULL,
732 NULL, NULL, addrport_print_addr, NULL, NULL, NULL, NULL },
733 /* fake address type */
734 {KRB5_ADDRESS_ARANGE, KRB5_ADDRESS_ARANGE, sizeof(struct arange),
735 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
736 arange_print_addr, arange_parse_addr,
737 arange_order_addr, arange_free, arange_copy }
740 static int num_addrs = sizeof(at) / sizeof(at[0]);
742 static size_t max_sockaddr_size = 0;
748 static struct addr_operations *
751 struct addr_operations *a;
753 for (a = at; a < at + num_addrs; ++a)
759 static struct addr_operations *
760 find_atype(int atype)
762 struct addr_operations *a;
764 for (a = at; a < at + num_addrs; ++a)
765 if (atype == a->atype)
771 * krb5_sockaddr2address stores a address a "struct sockaddr" sa in
772 * the krb5_address addr.
774 * @param context a Keberos context
775 * @param sa a struct sockaddr to extract the address from
776 * @param addr an Kerberos 5 address to store the address in.
778 * @return Return an error code or 0.
780 * @ingroup krb5_address
783 krb5_error_code KRB5_LIB_FUNCTION
784 krb5_sockaddr2address (krb5_context context,
785 const struct sockaddr *sa, krb5_address *addr)
787 struct addr_operations *a = find_af(sa->sa_family);
789 krb5_set_error_string (context, "Address family %d not supported",
791 return KRB5_PROG_ATYPE_NOSUPP;
793 return (*a->sockaddr2addr)(sa, addr);
797 * krb5_sockaddr2port extracts a port (if possible) from a "struct
800 * @param context a Keberos context
801 * @param sa a struct sockaddr to extract the port from
802 * @param port a pointer to an int16_t store the port in.
804 * @return Return an error code or 0. Will return
805 * KRB5_PROG_ATYPE_NOSUPP in case address type is not supported.
807 * @ingroup krb5_address
810 krb5_error_code KRB5_LIB_FUNCTION
811 krb5_sockaddr2port (krb5_context context,
812 const struct sockaddr *sa, int16_t *port)
814 struct addr_operations *a = find_af(sa->sa_family);
816 krb5_set_error_string (context, "Address family %d not supported",
818 return KRB5_PROG_ATYPE_NOSUPP;
820 return (*a->sockaddr2port)(sa, port);
824 * krb5_addr2sockaddr sets the "struct sockaddr sockaddr" from addr
825 * and port. The argument sa_size should initially contain the size of
826 * the sa and after the call, it will contain the actual length of the
827 * address. In case of the sa is too small to fit the whole address,
828 * the up to *sa_size will be stored, and then *sa_size will be set to
829 * the required length.
831 * @param context a Keberos context
832 * @param addr the address to copy the from
833 * @param sa the struct sockaddr that will be filled in
834 * @param sa_size pointer to length of sa, and after the call, it will
835 * contain the actual length of the address.
836 * @param port set port in sa.
838 * @return Return an error code or 0. Will return
839 * KRB5_PROG_ATYPE_NOSUPP in case address type is not supported.
841 * @ingroup krb5_address
844 krb5_error_code KRB5_LIB_FUNCTION
845 krb5_addr2sockaddr (krb5_context context,
846 const krb5_address *addr,
848 krb5_socklen_t *sa_size,
851 struct addr_operations *a = find_atype(addr->addr_type);
854 krb5_set_error_string (context, "Address type %d not supported",
856 return KRB5_PROG_ATYPE_NOSUPP;
858 if (a->addr2sockaddr == NULL) {
859 krb5_set_error_string (context,
860 "Can't convert address type %d to sockaddr",
862 return KRB5_PROG_ATYPE_NOSUPP;
864 (*a->addr2sockaddr)(addr, sa, sa_size, port);
869 * krb5_max_sockaddr_size returns the max size of the .Li struct
870 * sockaddr that the Kerberos library will return.
872 * @return Return an size_t of the maximum struct sockaddr.
874 * @ingroup krb5_address
877 size_t KRB5_LIB_FUNCTION
878 krb5_max_sockaddr_size (void)
880 if (max_sockaddr_size == 0) {
881 struct addr_operations *a;
883 for(a = at; a < at + num_addrs; ++a)
884 max_sockaddr_size = max(max_sockaddr_size, a->max_sockaddr_size);
886 return max_sockaddr_size;
890 * krb5_sockaddr_uninteresting returns TRUE for all .Fa sa that the
891 * kerberos library thinks are uninteresting. One example are link
894 * @param sa pointer to struct sockaddr that might be interesting.
896 * @return Return a non zero for uninteresting addresses.
898 * @ingroup krb5_address
901 krb5_boolean KRB5_LIB_FUNCTION
902 krb5_sockaddr_uninteresting(const struct sockaddr *sa)
904 struct addr_operations *a = find_af(sa->sa_family);
905 if (a == NULL || a->uninteresting == NULL)
907 return (*a->uninteresting)(sa);
911 * krb5_h_addr2sockaddr initializes a "struct sockaddr sa" from af and
912 * the "struct hostent" (see gethostbyname(3) ) h_addr_list
913 * component. The argument sa_size should initially contain the size
914 * of the sa, and after the call, it will contain the actual length of
917 * @param context a Keberos context
918 * @param af addresses
919 * @param addr address
920 * @param sa returned struct sockaddr
921 * @param sa_size size of sa
922 * @param port port to set in sa.
924 * @return Return an error code or 0.
926 * @ingroup krb5_address
929 krb5_error_code KRB5_LIB_FUNCTION
930 krb5_h_addr2sockaddr (krb5_context context,
932 const char *addr, struct sockaddr *sa,
933 krb5_socklen_t *sa_size,
936 struct addr_operations *a = find_af(af);
938 krb5_set_error_string (context, "Address family %d not supported", af);
939 return KRB5_PROG_ATYPE_NOSUPP;
941 (*a->h_addr2sockaddr)(addr, sa, sa_size, port);
946 * krb5_h_addr2addr works like krb5_h_addr2sockaddr with the exception
947 * that it operates on a krb5_address instead of a struct sockaddr.
949 * @param context a Keberos context
950 * @param af address family
951 * @param haddr host address from struct hostent.
952 * @param addr returned krb5_address.
954 * @return Return an error code or 0.
956 * @ingroup krb5_address
959 krb5_error_code KRB5_LIB_FUNCTION
960 krb5_h_addr2addr (krb5_context context,
962 const char *haddr, krb5_address *addr)
964 struct addr_operations *a = find_af(af);
966 krb5_set_error_string (context, "Address family %d not supported", af);
967 return KRB5_PROG_ATYPE_NOSUPP;
969 return (*a->h_addr2addr)(haddr, addr);
973 * krb5_anyaddr fills in a "struct sockaddr sa" that can be used to
974 * bind(2) to. The argument sa_size should initially contain the size
975 * of the sa, and after the call, it will contain the actual length
978 * @param context a Keberos context
979 * @param af address family
981 * @param sa_size lenght of sa.
982 * @param port for to fill into sa.
984 * @return Return an error code or 0.
986 * @ingroup krb5_address
989 krb5_error_code KRB5_LIB_FUNCTION
990 krb5_anyaddr (krb5_context context,
993 krb5_socklen_t *sa_size,
996 struct addr_operations *a = find_af (af);
999 krb5_set_error_string (context, "Address family %d not supported", af);
1000 return KRB5_PROG_ATYPE_NOSUPP;
1003 (*a->anyaddr)(sa, sa_size, port);
1008 * krb5_print_address prints the address in addr to the string string
1009 * that have the length len. If ret_len is not NULL, it will be filled
1010 * with the length of the string if size were unlimited (not including
1013 * @param addr address to be printed
1014 * @param str pointer string to print the address into
1015 * @param len length that will fit into area pointed to by "str".
1016 * @param ret_len return length the str.
1018 * @return Return an error code or 0.
1020 * @ingroup krb5_address
1023 krb5_error_code KRB5_LIB_FUNCTION
1024 krb5_print_address (const krb5_address *addr,
1025 char *str, size_t len, size_t *ret_len)
1027 struct addr_operations *a = find_atype(addr->addr_type);
1030 if (a == NULL || a->print_addr == NULL) {
1036 l = snprintf(s, len, "TYPE_%d:", addr->addr_type);
1037 if (l < 0 || l >= len)
1041 for(i = 0; i < addr->address.length; i++) {
1042 l = snprintf(s, len, "%02x", ((char*)addr->address.data)[i]);
1043 if (l < 0 || l >= len)
1052 ret = (*a->print_addr)(addr, str, len);
1061 * krb5_parse_address returns the resolved hostname in string to the
1062 * krb5_addresses addresses .
1064 * @param context a Keberos context
1068 * @return Return an error code or 0.
1070 * @ingroup krb5_address
1073 krb5_error_code KRB5_LIB_FUNCTION
1074 krb5_parse_address(krb5_context context,
1076 krb5_addresses *addresses)
1079 struct addrinfo *ai, *a;
1084 addresses->val = NULL;
1086 for(i = 0; i < num_addrs; i++) {
1087 if(at[i].parse_addr) {
1089 if((*at[i].parse_addr)(context, string, &addr) == 0) {
1090 ALLOC_SEQ(addresses, 1);
1091 if (addresses->val == NULL) {
1092 krb5_set_error_string(context, "malloc: out of memory");
1095 addresses->val[0] = addr;
1101 error = getaddrinfo (string, NULL, NULL, &ai);
1104 krb5_set_error_string (context, "%s: %s", string, gai_strerror(error));
1105 return krb5_eai_to_heim_errno(error, save_errno);
1109 for (a = ai; a != NULL; a = a->ai_next)
1112 ALLOC_SEQ(addresses, n);
1113 if (addresses->val == NULL) {
1114 krb5_set_error_string(context, "malloc: out of memory");
1120 for (a = ai, i = 0; a != NULL; a = a->ai_next) {
1121 if (krb5_sockaddr2address (context, ai->ai_addr, &addresses->val[i]))
1123 if(krb5_address_search(context, &addresses->val[i], addresses))
1133 * krb5_address_order compares the addresses addr1 and addr2 so that
1134 * it can be used for sorting addresses. If the addresses are the same
1135 * address krb5_address_order will return 0. Behavies like memcmp(2).
1137 * @param context a Keberos context
1138 * @param addr1 krb5_address to compare
1139 * @param addr2 krb5_address to compare
1141 * @return < 0 if address addr1 in "less" then addr2. 0 if addr1 and
1142 * addr2 is the same address, > 0 if addr2 is "less" then addr1.
1144 * @ingroup krb5_address
1147 int KRB5_LIB_FUNCTION
1148 krb5_address_order(krb5_context context,
1149 const krb5_address *addr1,
1150 const krb5_address *addr2)
1152 /* this sucks; what if both addresses have order functions, which
1153 should we call? this works for now, though */
1154 struct addr_operations *a;
1155 a = find_atype(addr1->addr_type);
1157 krb5_set_error_string (context, "Address family %d not supported",
1159 return KRB5_PROG_ATYPE_NOSUPP;
1161 if(a->order_addr != NULL)
1162 return (*a->order_addr)(context, addr1, addr2);
1163 a = find_atype(addr2->addr_type);
1165 krb5_set_error_string (context, "Address family %d not supported",
1167 return KRB5_PROG_ATYPE_NOSUPP;
1169 if(a->order_addr != NULL)
1170 return (*a->order_addr)(context, addr1, addr2);
1172 if(addr1->addr_type != addr2->addr_type)
1173 return addr1->addr_type - addr2->addr_type;
1174 if(addr1->address.length != addr2->address.length)
1175 return addr1->address.length - addr2->address.length;
1176 return memcmp (addr1->address.data,
1177 addr2->address.data,
1178 addr1->address.length);
1182 * krb5_address_compare compares the addresses addr1 and addr2.
1183 * Returns TRUE if the two addresses are the same.
1185 * @param context a Keberos context
1186 * @param addr1 address to compare
1187 * @param addr2 address to compare
1189 * @return Return an TRUE is the address are the same FALSE if not
1191 * @ingroup krb5_address
1194 krb5_boolean KRB5_LIB_FUNCTION
1195 krb5_address_compare(krb5_context context,
1196 const krb5_address *addr1,
1197 const krb5_address *addr2)
1199 return krb5_address_order (context, addr1, addr2) == 0;
1203 * krb5_address_search checks if the address addr is a member of the
1204 * address set list addrlist .
1206 * @param context a Keberos context.
1207 * @param addr address to search for.
1208 * @param addrlist list of addresses to look in for addr.
1210 * @return Return an error code or 0.
1212 * @ingroup krb5_address
1215 krb5_boolean KRB5_LIB_FUNCTION
1216 krb5_address_search(krb5_context context,
1217 const krb5_address *addr,
1218 const krb5_addresses *addrlist)
1222 for (i = 0; i < addrlist->len; ++i)
1223 if (krb5_address_compare (context, addr, &addrlist->val[i]))
1229 * krb5_free_address frees the data stored in the address that is
1230 * alloced with any of the krb5_address functions.
1232 * @param context a Keberos context
1233 * @param address addresss to be freed.
1235 * @return Return an error code or 0.
1237 * @ingroup krb5_address
1240 krb5_error_code KRB5_LIB_FUNCTION
1241 krb5_free_address(krb5_context context,
1242 krb5_address *address)
1244 struct addr_operations *a = find_atype (address->addr_type);
1245 if(a != NULL && a->free_addr != NULL)
1246 return (*a->free_addr)(context, address);
1247 krb5_data_free (&address->address);
1248 memset(address, 0, sizeof(*address));
1253 * krb5_free_addresses frees the data stored in the address that is
1254 * alloced with any of the krb5_address functions.
1256 * @param context a Keberos context
1257 * @param addresses addressses to be freed.
1259 * @return Return an error code or 0.
1261 * @ingroup krb5_address
1264 krb5_error_code KRB5_LIB_FUNCTION
1265 krb5_free_addresses(krb5_context context,
1266 krb5_addresses *addresses)
1269 for(i = 0; i < addresses->len; i++)
1270 krb5_free_address(context, &addresses->val[i]);
1271 free(addresses->val);
1273 addresses->val = NULL;
1278 * krb5_copy_address copies the content of address
1279 * inaddr to outaddr.
1281 * @param context a Keberos context
1282 * @param inaddr pointer to source address
1283 * @param outaddr pointer to destination address
1285 * @return Return an error code or 0.
1287 * @ingroup krb5_address
1290 krb5_error_code KRB5_LIB_FUNCTION
1291 krb5_copy_address(krb5_context context,
1292 const krb5_address *inaddr,
1293 krb5_address *outaddr)
1295 struct addr_operations *a = find_af (inaddr->addr_type);
1296 if(a != NULL && a->copy_addr != NULL)
1297 return (*a->copy_addr)(context, inaddr, outaddr);
1298 return copy_HostAddress(inaddr, outaddr);
1302 * krb5_copy_addresses copies the content of addresses
1303 * inaddr to outaddr.
1305 * @param context a Keberos context
1306 * @param inaddr pointer to source addresses
1307 * @param outaddr pointer to destination addresses
1309 * @return Return an error code or 0.
1311 * @ingroup krb5_address
1314 krb5_error_code KRB5_LIB_FUNCTION
1315 krb5_copy_addresses(krb5_context context,
1316 const krb5_addresses *inaddr,
1317 krb5_addresses *outaddr)
1320 ALLOC_SEQ(outaddr, inaddr->len);
1321 if(inaddr->len > 0 && outaddr->val == NULL)
1323 for(i = 0; i < inaddr->len; i++)
1324 krb5_copy_address(context, &inaddr->val[i], &outaddr->val[i]);
1329 * krb5_append_addresses adds the set of addresses in source to
1330 * dest. While copying the addresses, duplicates are also sorted out.
1332 * @param context a Keberos context
1333 * @param dest destination of copy operation
1334 * @param source adresses that are going to be added to dest
1336 * @return Return an error code or 0.
1338 * @ingroup krb5_address
1341 krb5_error_code KRB5_LIB_FUNCTION
1342 krb5_append_addresses(krb5_context context,
1343 krb5_addresses *dest,
1344 const krb5_addresses *source)
1347 krb5_error_code ret;
1349 if(source->len > 0) {
1350 tmp = realloc(dest->val, (dest->len + source->len) * sizeof(*tmp));
1352 krb5_set_error_string(context, "realloc: out of memory");
1356 for(i = 0; i < source->len; i++) {
1357 /* skip duplicates */
1358 if(krb5_address_search(context, &source->val[i], dest))
1360 ret = krb5_copy_address(context,
1362 &dest->val[dest->len]);
1372 * Create an address of type KRB5_ADDRESS_ADDRPORT from (addr, port)
1374 * @param context a Keberos context
1375 * @param res built address from addr/port
1376 * @param addr address to use
1377 * @param port port to use
1379 * @return Return an error code or 0.
1381 * @ingroup krb5_address
1384 krb5_error_code KRB5_LIB_FUNCTION
1385 krb5_make_addrport (krb5_context context,
1386 krb5_address **res, const krb5_address *addr, int16_t port)
1388 krb5_error_code ret;
1389 size_t len = addr->address.length + 2 + 4 * 4;
1392 *res = malloc (sizeof(**res));
1394 krb5_set_error_string(context, "malloc: out of memory");
1397 (*res)->addr_type = KRB5_ADDRESS_ADDRPORT;
1398 ret = krb5_data_alloc (&(*res)->address, len);
1400 krb5_set_error_string(context, "malloc: out of memory");
1405 p = (*res)->address.data;
1408 *p++ = (addr->addr_type ) & 0xFF;
1409 *p++ = (addr->addr_type >> 8) & 0xFF;
1411 *p++ = (addr->address.length ) & 0xFF;
1412 *p++ = (addr->address.length >> 8) & 0xFF;
1413 *p++ = (addr->address.length >> 16) & 0xFF;
1414 *p++ = (addr->address.length >> 24) & 0xFF;
1416 memcpy (p, addr->address.data, addr->address.length);
1417 p += addr->address.length;
1421 *p++ = (KRB5_ADDRESS_IPPORT ) & 0xFF;
1422 *p++ = (KRB5_ADDRESS_IPPORT >> 8) & 0xFF;
1425 *p++ = (2 >> 8) & 0xFF;
1426 *p++ = (2 >> 16) & 0xFF;
1427 *p++ = (2 >> 24) & 0xFF;
1429 memcpy (p, &port, 2);
1436 * Calculate the boundary addresses of `inaddr'/`prefixlen' and store
1437 * them in `low' and `high'.
1439 * @param context a Keberos context
1440 * @param inaddr address in prefixlen that the bondery searched
1441 * @param prefixlen width of boundery
1442 * @param low lowest address
1443 * @param high highest address
1445 * @return Return an error code or 0.
1447 * @ingroup krb5_address
1450 krb5_error_code KRB5_LIB_FUNCTION
1451 krb5_address_prefixlen_boundary(krb5_context context,
1452 const krb5_address *inaddr,
1453 unsigned long prefixlen,
1457 struct addr_operations *a = find_atype (inaddr->addr_type);
1458 if(a != NULL && a->mask_boundary != NULL)
1459 return (*a->mask_boundary)(context, inaddr, prefixlen, low, high);
1460 krb5_set_error_string(context, "Address family %d doesn't support "
1461 "address mask operation", inaddr->addr_type);
1462 return KRB5_PROG_ATYPE_NOSUPP;