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"
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_message(context, KRB5_PROG_ATYPE_NOSUPP,
206 "IPv4 prefix too large (%ld)", len);
207 return KRB5_PROG_ATYPE_NOSUPP;
211 _krb5_get_int(inaddr->address.data, &ia, inaddr->address.length);
216 low->addr_type = KRB5_ADDRESS_INET;
217 if(krb5_data_alloc(&low->address, 4) != 0)
219 _krb5_put_int(low->address.data, l, low->address.length);
221 high->addr_type = KRB5_ADDRESS_INET;
222 if(krb5_data_alloc(&high->address, 4) != 0) {
223 krb5_free_address(context, low);
226 _krb5_put_int(high->address.data, h, high->address.length);
233 * AF_INET6 - aka IPv6 implementation
238 static krb5_error_code
239 ipv6_sockaddr2addr (const struct sockaddr *sa, krb5_address *a)
241 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
243 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
244 unsigned char buf[4];
246 a->addr_type = KRB5_ADDRESS_INET;
247 #ifndef IN6_ADDR_V6_TO_V4
248 #ifdef IN6_EXTRACT_V4ADDR
249 #define IN6_ADDR_V6_TO_V4(x) (&IN6_EXTRACT_V4ADDR(x))
251 #define IN6_ADDR_V6_TO_V4(x) ((const struct in_addr *)&(x)->s6_addr[12])
254 memcpy (buf, IN6_ADDR_V6_TO_V4(&sin6->sin6_addr), 4);
255 return krb5_data_copy(&a->address, buf, 4);
257 a->addr_type = KRB5_ADDRESS_INET6;
258 return krb5_data_copy(&a->address,
260 sizeof(sin6->sin6_addr));
264 static krb5_error_code
265 ipv6_sockaddr2port (const struct sockaddr *sa, int16_t *port)
267 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
269 *port = sin6->sin6_port;
274 ipv6_addr2sockaddr (const krb5_address *a,
276 krb5_socklen_t *sa_size,
279 struct sockaddr_in6 tmp;
281 memset (&tmp, 0, sizeof(tmp));
282 tmp.sin6_family = AF_INET6;
283 memcpy (&tmp.sin6_addr, a->address.data, sizeof(tmp.sin6_addr));
284 tmp.sin6_port = port;
285 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
286 *sa_size = sizeof(tmp);
290 ipv6_h_addr2sockaddr(const char *addr,
292 krb5_socklen_t *sa_size,
295 struct sockaddr_in6 tmp;
297 memset (&tmp, 0, sizeof(tmp));
298 tmp.sin6_family = AF_INET6;
299 tmp.sin6_port = port;
300 tmp.sin6_addr = *((const struct in6_addr *)addr);
301 memcpy(sa, &tmp, min(sizeof(tmp), *sa_size));
302 *sa_size = sizeof(tmp);
305 static krb5_error_code
306 ipv6_h_addr2addr (const char *addr,
309 a->addr_type = KRB5_ADDRESS_INET6;
310 return krb5_data_copy(&a->address, addr, sizeof(struct in6_addr));
318 ipv6_uninteresting (const struct sockaddr *sa)
320 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
321 const struct in6_addr *in6 = (const struct in6_addr *)&sin6->sin6_addr;
324 IN6_IS_ADDR_LINKLOCAL(in6)
325 || IN6_IS_ADDR_V4COMPAT(in6);
329 ipv6_anyaddr (struct sockaddr *sa, krb5_socklen_t *sa_size, int port)
331 struct sockaddr_in6 tmp;
333 memset (&tmp, 0, sizeof(tmp));
334 tmp.sin6_family = AF_INET6;
335 tmp.sin6_port = port;
336 tmp.sin6_addr = in6addr_any;
337 *sa_size = sizeof(tmp);
341 ipv6_print_addr (const krb5_address *addr, char *str, size_t len)
343 char buf[128], buf2[3];
344 #ifdef HAVE_INET_NTOP
345 if(inet_ntop(AF_INET6, addr->address.data, buf, sizeof(buf)) == NULL)
348 /* XXX this is pretty ugly, but better than abort() */
350 unsigned char *p = addr->address.data;
352 for(i = 0; i < addr->address.length; i++) {
353 snprintf(buf2, sizeof(buf2), "%02x", p[i]);
354 if(i > 0 && (i & 1) == 0)
355 strlcat(buf, ":", sizeof(buf));
356 strlcat(buf, buf2, sizeof(buf));
359 return snprintf(str, len, "IPv6:%s", buf);
363 ipv6_parse_addr (krb5_context context, const char *address, krb5_address *addr)
369 p = strchr(address, ':');
372 if(strncasecmp(address, "ip6:", p - address) == 0 ||
373 strncasecmp(address, "ipv6:", p - address) == 0 ||
374 strncasecmp(address, "inet6:", p - address) == 0)
378 ret = inet_pton(AF_INET6, address, &in6.s6_addr);
380 addr->addr_type = KRB5_ADDRESS_INET6;
381 ret = krb5_data_alloc(&addr->address, sizeof(in6.s6_addr));
384 memcpy(addr->address.data, in6.s6_addr, sizeof(in6.s6_addr));
391 ipv6_mask_boundary(krb5_context context, const krb5_address *inaddr,
392 unsigned long len, krb5_address *low, krb5_address *high)
394 struct in6_addr addr, laddr, haddr;
399 krb5_set_error_message(context, KRB5_PROG_ATYPE_NOSUPP,
400 "IPv6 prefix too large (%ld)", len);
401 return KRB5_PROG_ATYPE_NOSUPP;
404 if (inaddr->address.length != sizeof(addr)) {
405 krb5_set_error_message(context, KRB5_PROG_ATYPE_NOSUPP,
406 "IPv6 addr bad length");
407 return KRB5_PROG_ATYPE_NOSUPP;
410 memcpy(&addr, inaddr->address.data, inaddr->address.length);
412 for (i = 0; i < 16; i++) {
413 sub_len = min(8, len);
415 m = 0xff << (8 - sub_len);
417 laddr.s6_addr[i] = addr.s6_addr[i] & m;
418 haddr.s6_addr[i] = (addr.s6_addr[i] & m) | ~m;
426 low->addr_type = KRB5_ADDRESS_INET6;
427 if (krb5_data_alloc(&low->address, sizeof(laddr.s6_addr)) != 0)
429 memcpy(low->address.data, laddr.s6_addr, sizeof(laddr.s6_addr));
431 high->addr_type = KRB5_ADDRESS_INET6;
432 if (krb5_data_alloc(&high->address, sizeof(haddr.s6_addr)) != 0) {
433 krb5_free_address(context, low);
436 memcpy(high->address.data, haddr.s6_addr, sizeof(haddr.s6_addr));
447 #define KRB5_ADDRESS_ARANGE (-100)
455 arange_parse_addr (krb5_context context,
456 const char *address, krb5_address *addr)
459 krb5_address low0, high0;
463 if(strncasecmp(address, "RANGE:", 6) != 0)
468 p = strrchr(address, '/');
470 krb5_addresses addrmask;
474 if (strlcpy(buf, address, sizeof(buf)) > sizeof(buf))
476 buf[p - address] = '\0';
477 ret = krb5_parse_address(context, buf, &addrmask);
480 if(addrmask.len != 1) {
481 krb5_free_addresses(context, &addrmask);
485 address += p - address + 1;
487 num = strtol(address, &q, 10);
488 if (q == address || *q != '\0' || num < 0) {
489 krb5_free_addresses(context, &addrmask);
493 ret = krb5_address_prefixlen_boundary(context, &addrmask.val[0], num,
495 krb5_free_addresses(context, &addrmask);
500 krb5_addresses low, high;
502 strsep_copy(&address, "-", buf, sizeof(buf));
503 ret = krb5_parse_address(context, buf, &low);
507 krb5_free_addresses(context, &low);
511 strsep_copy(&address, "-", buf, sizeof(buf));
512 ret = krb5_parse_address(context, buf, &high);
514 krb5_free_addresses(context, &low);
518 if(high.len != 1 && high.val[0].addr_type != low.val[0].addr_type) {
519 krb5_free_addresses(context, &low);
520 krb5_free_addresses(context, &high);
524 ret = krb5_copy_address(context, &high.val[0], &high0);
526 ret = krb5_copy_address(context, &low.val[0], &low0);
528 krb5_free_address(context, &high0);
530 krb5_free_addresses(context, &low);
531 krb5_free_addresses(context, &high);
536 krb5_data_alloc(&addr->address, sizeof(*a));
537 addr->addr_type = KRB5_ADDRESS_ARANGE;
538 a = addr->address.data;
540 if(krb5_address_order(context, &low0, &high0) < 0) {
551 arange_free (krb5_context context, krb5_address *addr)
554 a = addr->address.data;
555 krb5_free_address(context, &a->low);
556 krb5_free_address(context, &a->high);
557 krb5_data_free(&addr->address);
563 arange_copy (krb5_context context, const krb5_address *inaddr,
564 krb5_address *outaddr)
567 struct arange *i, *o;
569 outaddr->addr_type = KRB5_ADDRESS_ARANGE;
570 ret = krb5_data_alloc(&outaddr->address, sizeof(*o));
573 i = inaddr->address.data;
574 o = outaddr->address.data;
575 ret = krb5_copy_address(context, &i->low, &o->low);
577 krb5_data_free(&outaddr->address);
580 ret = krb5_copy_address(context, &i->high, &o->high);
582 krb5_free_address(context, &o->low);
583 krb5_data_free(&outaddr->address);
590 arange_print_addr (const krb5_address *addr, char *str, size_t len)
594 size_t l, size, ret_len;
596 a = addr->address.data;
598 l = strlcpy(str, "RANGE:", len);
604 ret = krb5_print_address (&a->low, str + size, len - size, &l);
613 l = strlcat(str + size, "-", len - size);
620 ret = krb5_print_address (&a->high, str + size, len - size, &l);
629 arange_order_addr(krb5_context context,
630 const krb5_address *addr1,
631 const krb5_address *addr2)
633 int tmp1, tmp2, sign;
635 const krb5_address *a2;
637 if(addr1->addr_type == KRB5_ADDRESS_ARANGE) {
638 a = addr1->address.data;
641 } else if(addr2->addr_type == KRB5_ADDRESS_ARANGE) {
642 a = addr2->address.data;
648 if(a2->addr_type == KRB5_ADDRESS_ARANGE) {
649 struct arange *b = a2->address.data;
650 tmp1 = krb5_address_order(context, &a->low, &b->low);
653 return sign * krb5_address_order(context, &a->high, &b->high);
654 } else if(a2->addr_type == a->low.addr_type) {
655 tmp1 = krb5_address_order(context, &a->low, a2);
658 tmp2 = krb5_address_order(context, &a->high, a2);
663 return sign * (addr1->addr_type - addr2->addr_type);
668 addrport_print_addr (const krb5_address *addr, char *str, size_t len)
671 krb5_address addr1, addr2;
673 size_t ret_len = 0, l, size = 0;
676 sp = krb5_storage_from_data((krb5_data*)rk_UNCONST(&addr->address));
677 /* for totally obscure reasons, these are not in network byteorder */
678 krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_LE);
680 krb5_storage_seek(sp, 2, SEEK_CUR); /* skip first two bytes */
681 krb5_ret_address(sp, &addr1);
683 krb5_storage_seek(sp, 2, SEEK_CUR); /* skip two bytes */
684 krb5_ret_address(sp, &addr2);
685 krb5_storage_free(sp);
686 if(addr2.addr_type == KRB5_ADDRESS_IPPORT && addr2.address.length == 2) {
688 _krb5_get_int(addr2.address.data, &value, 2);
691 l = strlcpy(str, "ADDRPORT:", len);
698 ret = krb5_print_address(&addr1, str + size, len - size, &l);
707 ret = snprintf(str + size, len - size, ",PORT=%u", port);
714 static struct addr_operations at[] = {
715 {AF_INET, KRB5_ADDRESS_INET, sizeof(struct sockaddr_in),
719 ipv4_h_addr2sockaddr,
721 ipv4_uninteresting, ipv4_anyaddr, ipv4_print_addr, ipv4_parse_addr,
722 NULL, NULL, NULL, ipv4_mask_boundary },
724 {AF_INET6, KRB5_ADDRESS_INET6, sizeof(struct sockaddr_in6),
728 ipv6_h_addr2sockaddr,
730 ipv6_uninteresting, ipv6_anyaddr, ipv6_print_addr, ipv6_parse_addr,
731 NULL, NULL, NULL, ipv6_mask_boundary } ,
733 {KRB5_ADDRESS_ADDRPORT, KRB5_ADDRESS_ADDRPORT, 0,
734 NULL, NULL, NULL, NULL, NULL,
735 NULL, NULL, addrport_print_addr, NULL, NULL, NULL, NULL },
736 /* fake address type */
737 {KRB5_ADDRESS_ARANGE, KRB5_ADDRESS_ARANGE, sizeof(struct arange),
738 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
739 arange_print_addr, arange_parse_addr,
740 arange_order_addr, arange_free, arange_copy }
743 static int num_addrs = sizeof(at) / sizeof(at[0]);
745 static size_t max_sockaddr_size = 0;
751 static struct addr_operations *
754 struct addr_operations *a;
756 for (a = at; a < at + num_addrs; ++a)
762 static struct addr_operations *
763 find_atype(int atype)
765 struct addr_operations *a;
767 for (a = at; a < at + num_addrs; ++a)
768 if (atype == a->atype)
774 * krb5_sockaddr2address stores a address a "struct sockaddr" sa in
775 * the krb5_address addr.
777 * @param context a Keberos context
778 * @param sa a struct sockaddr to extract the address from
779 * @param addr an Kerberos 5 address to store the address in.
781 * @return Return an error code or 0.
783 * @ingroup krb5_address
786 krb5_error_code KRB5_LIB_FUNCTION
787 krb5_sockaddr2address (krb5_context context,
788 const struct sockaddr *sa, krb5_address *addr)
790 struct addr_operations *a = find_af(sa->sa_family);
792 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
793 "Address family %d not supported",
795 return KRB5_PROG_ATYPE_NOSUPP;
797 return (*a->sockaddr2addr)(sa, addr);
801 * krb5_sockaddr2port extracts a port (if possible) from a "struct
804 * @param context a Keberos context
805 * @param sa a struct sockaddr to extract the port from
806 * @param port a pointer to an int16_t store the port in.
808 * @return Return an error code or 0. Will return
809 * KRB5_PROG_ATYPE_NOSUPP in case address type is not supported.
811 * @ingroup krb5_address
814 krb5_error_code KRB5_LIB_FUNCTION
815 krb5_sockaddr2port (krb5_context context,
816 const struct sockaddr *sa, int16_t *port)
818 struct addr_operations *a = find_af(sa->sa_family);
820 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
821 "Address family %d not supported",
823 return KRB5_PROG_ATYPE_NOSUPP;
825 return (*a->sockaddr2port)(sa, port);
829 * krb5_addr2sockaddr sets the "struct sockaddr sockaddr" from addr
830 * and port. The argument sa_size should initially contain the size of
831 * the sa and after the call, it will contain the actual length of the
832 * address. In case of the sa is too small to fit the whole address,
833 * the up to *sa_size will be stored, and then *sa_size will be set to
834 * the required length.
836 * @param context a Keberos context
837 * @param addr the address to copy the from
838 * @param sa the struct sockaddr that will be filled in
839 * @param sa_size pointer to length of sa, and after the call, it will
840 * contain the actual length of the address.
841 * @param port set port in sa.
843 * @return Return an error code or 0. Will return
844 * KRB5_PROG_ATYPE_NOSUPP in case address type is not supported.
846 * @ingroup krb5_address
849 krb5_error_code KRB5_LIB_FUNCTION
850 krb5_addr2sockaddr (krb5_context context,
851 const krb5_address *addr,
853 krb5_socklen_t *sa_size,
856 struct addr_operations *a = find_atype(addr->addr_type);
859 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
860 "Address type %d not supported",
862 return KRB5_PROG_ATYPE_NOSUPP;
864 if (a->addr2sockaddr == NULL) {
865 krb5_set_error_message (context,
866 KRB5_PROG_ATYPE_NOSUPP,
867 "Can't convert address type %d to sockaddr",
869 return KRB5_PROG_ATYPE_NOSUPP;
871 (*a->addr2sockaddr)(addr, sa, sa_size, port);
876 * krb5_max_sockaddr_size returns the max size of the .Li struct
877 * sockaddr that the Kerberos library will return.
879 * @return Return an size_t of the maximum struct sockaddr.
881 * @ingroup krb5_address
884 size_t KRB5_LIB_FUNCTION
885 krb5_max_sockaddr_size (void)
887 if (max_sockaddr_size == 0) {
888 struct addr_operations *a;
890 for(a = at; a < at + num_addrs; ++a)
891 max_sockaddr_size = max(max_sockaddr_size, a->max_sockaddr_size);
893 return max_sockaddr_size;
897 * krb5_sockaddr_uninteresting returns TRUE for all .Fa sa that the
898 * kerberos library thinks are uninteresting. One example are link
901 * @param sa pointer to struct sockaddr that might be interesting.
903 * @return Return a non zero for uninteresting addresses.
905 * @ingroup krb5_address
908 krb5_boolean KRB5_LIB_FUNCTION
909 krb5_sockaddr_uninteresting(const struct sockaddr *sa)
911 struct addr_operations *a = find_af(sa->sa_family);
912 if (a == NULL || a->uninteresting == NULL)
914 return (*a->uninteresting)(sa);
918 * krb5_h_addr2sockaddr initializes a "struct sockaddr sa" from af and
919 * the "struct hostent" (see gethostbyname(3) ) h_addr_list
920 * component. The argument sa_size should initially contain the size
921 * of the sa, and after the call, it will contain the actual length of
924 * @param context a Keberos context
925 * @param af addresses
926 * @param addr address
927 * @param sa returned struct sockaddr
928 * @param sa_size size of sa
929 * @param port port to set in sa.
931 * @return Return an error code or 0.
933 * @ingroup krb5_address
936 krb5_error_code KRB5_LIB_FUNCTION
937 krb5_h_addr2sockaddr (krb5_context context,
939 const char *addr, struct sockaddr *sa,
940 krb5_socklen_t *sa_size,
943 struct addr_operations *a = find_af(af);
945 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
946 "Address family %d not supported", af);
947 return KRB5_PROG_ATYPE_NOSUPP;
949 (*a->h_addr2sockaddr)(addr, sa, sa_size, port);
954 * krb5_h_addr2addr works like krb5_h_addr2sockaddr with the exception
955 * that it operates on a krb5_address instead of a struct sockaddr.
957 * @param context a Keberos context
958 * @param af address family
959 * @param haddr host address from struct hostent.
960 * @param addr returned krb5_address.
962 * @return Return an error code or 0.
964 * @ingroup krb5_address
967 krb5_error_code KRB5_LIB_FUNCTION
968 krb5_h_addr2addr (krb5_context context,
970 const char *haddr, krb5_address *addr)
972 struct addr_operations *a = find_af(af);
974 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
975 "Address family %d not supported", af);
976 return KRB5_PROG_ATYPE_NOSUPP;
978 return (*a->h_addr2addr)(haddr, addr);
982 * krb5_anyaddr fills in a "struct sockaddr sa" that can be used to
983 * bind(2) to. The argument sa_size should initially contain the size
984 * of the sa, and after the call, it will contain the actual length
987 * @param context a Keberos context
988 * @param af address family
990 * @param sa_size lenght of sa.
991 * @param port for to fill into sa.
993 * @return Return an error code or 0.
995 * @ingroup krb5_address
998 krb5_error_code KRB5_LIB_FUNCTION
999 krb5_anyaddr (krb5_context context,
1001 struct sockaddr *sa,
1002 krb5_socklen_t *sa_size,
1005 struct addr_operations *a = find_af (af);
1008 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
1009 "Address family %d not supported", af);
1010 return KRB5_PROG_ATYPE_NOSUPP;
1013 (*a->anyaddr)(sa, sa_size, port);
1018 * krb5_print_address prints the address in addr to the string string
1019 * that have the length len. If ret_len is not NULL, it will be filled
1020 * with the length of the string if size were unlimited (not including
1023 * @param addr address to be printed
1024 * @param str pointer string to print the address into
1025 * @param len length that will fit into area pointed to by "str".
1026 * @param ret_len return length the str.
1028 * @return Return an error code or 0.
1030 * @ingroup krb5_address
1033 krb5_error_code KRB5_LIB_FUNCTION
1034 krb5_print_address (const krb5_address *addr,
1035 char *str, size_t len, size_t *ret_len)
1037 struct addr_operations *a = find_atype(addr->addr_type);
1040 if (a == NULL || a->print_addr == NULL) {
1046 l = snprintf(s, len, "TYPE_%d:", addr->addr_type);
1047 if (l < 0 || l >= len)
1051 for(i = 0; i < addr->address.length; i++) {
1052 l = snprintf(s, len, "%02x", ((char*)addr->address.data)[i]);
1053 if (l < 0 || l >= len)
1062 ret = (*a->print_addr)(addr, str, len);
1071 * krb5_parse_address returns the resolved hostname in string to the
1072 * krb5_addresses addresses .
1074 * @param context a Keberos context
1078 * @return Return an error code or 0.
1080 * @ingroup krb5_address
1083 krb5_error_code KRB5_LIB_FUNCTION
1084 krb5_parse_address(krb5_context context,
1086 krb5_addresses *addresses)
1089 struct addrinfo *ai, *a;
1094 addresses->val = NULL;
1096 for(i = 0; i < num_addrs; i++) {
1097 if(at[i].parse_addr) {
1099 if((*at[i].parse_addr)(context, string, &addr) == 0) {
1100 ALLOC_SEQ(addresses, 1);
1101 if (addresses->val == NULL) {
1102 krb5_set_error_message(context, ENOMEM,
1103 "malloc: out of memory");
1106 addresses->val[0] = addr;
1112 error = getaddrinfo (string, NULL, NULL, &ai);
1114 krb5_error_code ret2;
1116 ret2 = krb5_eai_to_heim_errno(error, save_errno);
1117 krb5_set_error_message (context, ret2, "%s: %s",
1118 string, gai_strerror(error));
1123 for (a = ai; a != NULL; a = a->ai_next)
1126 ALLOC_SEQ(addresses, n);
1127 if (addresses->val == NULL) {
1128 krb5_set_error_message(context, ENOMEM,
1129 "malloc: out of memory");
1135 for (a = ai, i = 0; a != NULL; a = a->ai_next) {
1136 if (krb5_sockaddr2address (context, ai->ai_addr, &addresses->val[i]))
1138 if(krb5_address_search(context, &addresses->val[i], addresses))
1148 * krb5_address_order compares the addresses addr1 and addr2 so that
1149 * it can be used for sorting addresses. If the addresses are the same
1150 * address krb5_address_order will return 0. Behavies like memcmp(2).
1152 * @param context a Keberos context
1153 * @param addr1 krb5_address to compare
1154 * @param addr2 krb5_address to compare
1156 * @return < 0 if address addr1 in "less" then addr2. 0 if addr1 and
1157 * addr2 is the same address, > 0 if addr2 is "less" then addr1.
1159 * @ingroup krb5_address
1162 int KRB5_LIB_FUNCTION
1163 krb5_address_order(krb5_context context,
1164 const krb5_address *addr1,
1165 const krb5_address *addr2)
1167 /* this sucks; what if both addresses have order functions, which
1168 should we call? this works for now, though */
1169 struct addr_operations *a;
1170 a = find_atype(addr1->addr_type);
1172 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
1173 "Address family %d not supported",
1175 return KRB5_PROG_ATYPE_NOSUPP;
1177 if(a->order_addr != NULL)
1178 return (*a->order_addr)(context, addr1, addr2);
1179 a = find_atype(addr2->addr_type);
1181 krb5_set_error_message (context, KRB5_PROG_ATYPE_NOSUPP,
1182 "Address family %d not supported",
1184 return KRB5_PROG_ATYPE_NOSUPP;
1186 if(a->order_addr != NULL)
1187 return (*a->order_addr)(context, addr1, addr2);
1189 if(addr1->addr_type != addr2->addr_type)
1190 return addr1->addr_type - addr2->addr_type;
1191 if(addr1->address.length != addr2->address.length)
1192 return addr1->address.length - addr2->address.length;
1193 return memcmp (addr1->address.data,
1194 addr2->address.data,
1195 addr1->address.length);
1199 * krb5_address_compare compares the addresses addr1 and addr2.
1200 * Returns TRUE if the two addresses are the same.
1202 * @param context a Keberos context
1203 * @param addr1 address to compare
1204 * @param addr2 address to compare
1206 * @return Return an TRUE is the address are the same FALSE if not
1208 * @ingroup krb5_address
1211 krb5_boolean KRB5_LIB_FUNCTION
1212 krb5_address_compare(krb5_context context,
1213 const krb5_address *addr1,
1214 const krb5_address *addr2)
1216 return krb5_address_order (context, addr1, addr2) == 0;
1220 * krb5_address_search checks if the address addr is a member of the
1221 * address set list addrlist .
1223 * @param context a Keberos context.
1224 * @param addr address to search for.
1225 * @param addrlist list of addresses to look in for addr.
1227 * @return Return an error code or 0.
1229 * @ingroup krb5_address
1232 krb5_boolean KRB5_LIB_FUNCTION
1233 krb5_address_search(krb5_context context,
1234 const krb5_address *addr,
1235 const krb5_addresses *addrlist)
1239 for (i = 0; i < addrlist->len; ++i)
1240 if (krb5_address_compare (context, addr, &addrlist->val[i]))
1246 * krb5_free_address frees the data stored in the address that is
1247 * alloced with any of the krb5_address functions.
1249 * @param context a Keberos context
1250 * @param address addresss to be freed.
1252 * @return Return an error code or 0.
1254 * @ingroup krb5_address
1257 krb5_error_code KRB5_LIB_FUNCTION
1258 krb5_free_address(krb5_context context,
1259 krb5_address *address)
1261 struct addr_operations *a = find_atype (address->addr_type);
1262 if(a != NULL && a->free_addr != NULL)
1263 return (*a->free_addr)(context, address);
1264 krb5_data_free (&address->address);
1265 memset(address, 0, sizeof(*address));
1270 * krb5_free_addresses frees the data stored in the address that is
1271 * alloced with any of the krb5_address functions.
1273 * @param context a Keberos context
1274 * @param addresses addressses to be freed.
1276 * @return Return an error code or 0.
1278 * @ingroup krb5_address
1281 krb5_error_code KRB5_LIB_FUNCTION
1282 krb5_free_addresses(krb5_context context,
1283 krb5_addresses *addresses)
1286 for(i = 0; i < addresses->len; i++)
1287 krb5_free_address(context, &addresses->val[i]);
1288 free(addresses->val);
1290 addresses->val = NULL;
1295 * krb5_copy_address copies the content of address
1296 * inaddr to outaddr.
1298 * @param context a Keberos context
1299 * @param inaddr pointer to source address
1300 * @param outaddr pointer to destination address
1302 * @return Return an error code or 0.
1304 * @ingroup krb5_address
1307 krb5_error_code KRB5_LIB_FUNCTION
1308 krb5_copy_address(krb5_context context,
1309 const krb5_address *inaddr,
1310 krb5_address *outaddr)
1312 struct addr_operations *a = find_af (inaddr->addr_type);
1313 if(a != NULL && a->copy_addr != NULL)
1314 return (*a->copy_addr)(context, inaddr, outaddr);
1315 return copy_HostAddress(inaddr, outaddr);
1319 * krb5_copy_addresses copies the content of addresses
1320 * inaddr to outaddr.
1322 * @param context a Keberos context
1323 * @param inaddr pointer to source addresses
1324 * @param outaddr pointer to destination addresses
1326 * @return Return an error code or 0.
1328 * @ingroup krb5_address
1331 krb5_error_code KRB5_LIB_FUNCTION
1332 krb5_copy_addresses(krb5_context context,
1333 const krb5_addresses *inaddr,
1334 krb5_addresses *outaddr)
1337 ALLOC_SEQ(outaddr, inaddr->len);
1338 if(inaddr->len > 0 && outaddr->val == NULL)
1340 for(i = 0; i < inaddr->len; i++)
1341 krb5_copy_address(context, &inaddr->val[i], &outaddr->val[i]);
1346 * krb5_append_addresses adds the set of addresses in source to
1347 * dest. While copying the addresses, duplicates are also sorted out.
1349 * @param context a Keberos context
1350 * @param dest destination of copy operation
1351 * @param source adresses that are going to be added to dest
1353 * @return Return an error code or 0.
1355 * @ingroup krb5_address
1358 krb5_error_code KRB5_LIB_FUNCTION
1359 krb5_append_addresses(krb5_context context,
1360 krb5_addresses *dest,
1361 const krb5_addresses *source)
1364 krb5_error_code ret;
1366 if(source->len > 0) {
1367 tmp = realloc(dest->val, (dest->len + source->len) * sizeof(*tmp));
1369 krb5_set_error_message (context, ENOMEM,
1370 "realloc: out of memory");
1374 for(i = 0; i < source->len; i++) {
1375 /* skip duplicates */
1376 if(krb5_address_search(context, &source->val[i], dest))
1378 ret = krb5_copy_address(context,
1380 &dest->val[dest->len]);
1390 * Create an address of type KRB5_ADDRESS_ADDRPORT from (addr, port)
1392 * @param context a Keberos context
1393 * @param res built address from addr/port
1394 * @param addr address to use
1395 * @param port port to use
1397 * @return Return an error code or 0.
1399 * @ingroup krb5_address
1402 krb5_error_code KRB5_LIB_FUNCTION
1403 krb5_make_addrport (krb5_context context,
1404 krb5_address **res, const krb5_address *addr, int16_t port)
1406 krb5_error_code ret;
1407 size_t len = addr->address.length + 2 + 4 * 4;
1410 *res = malloc (sizeof(**res));
1412 krb5_set_error_message (context, ENOMEM,
1413 "malloc: out of memory");
1416 (*res)->addr_type = KRB5_ADDRESS_ADDRPORT;
1417 ret = krb5_data_alloc (&(*res)->address, len);
1419 krb5_set_error_message (context, ret,
1420 "malloc: out of memory");
1425 p = (*res)->address.data;
1428 *p++ = (addr->addr_type ) & 0xFF;
1429 *p++ = (addr->addr_type >> 8) & 0xFF;
1431 *p++ = (addr->address.length ) & 0xFF;
1432 *p++ = (addr->address.length >> 8) & 0xFF;
1433 *p++ = (addr->address.length >> 16) & 0xFF;
1434 *p++ = (addr->address.length >> 24) & 0xFF;
1436 memcpy (p, addr->address.data, addr->address.length);
1437 p += addr->address.length;
1441 *p++ = (KRB5_ADDRESS_IPPORT ) & 0xFF;
1442 *p++ = (KRB5_ADDRESS_IPPORT >> 8) & 0xFF;
1445 *p++ = (2 >> 8) & 0xFF;
1446 *p++ = (2 >> 16) & 0xFF;
1447 *p++ = (2 >> 24) & 0xFF;
1449 memcpy (p, &port, 2);
1456 * Calculate the boundary addresses of `inaddr'/`prefixlen' and store
1457 * them in `low' and `high'.
1459 * @param context a Keberos context
1460 * @param inaddr address in prefixlen that the bondery searched
1461 * @param prefixlen width of boundery
1462 * @param low lowest address
1463 * @param high highest address
1465 * @return Return an error code or 0.
1467 * @ingroup krb5_address
1470 krb5_error_code KRB5_LIB_FUNCTION
1471 krb5_address_prefixlen_boundary(krb5_context context,
1472 const krb5_address *inaddr,
1473 unsigned long prefixlen,
1477 struct addr_operations *a = find_atype (inaddr->addr_type);
1478 if(a != NULL && a->mask_boundary != NULL)
1479 return (*a->mask_boundary)(context, inaddr, prefixlen, low, high);
1480 krb5_set_error_message(context, KRB5_PROG_ATYPE_NOSUPP,
1481 "Address family %d doesn't support "
1482 "address mask operation", inaddr->addr_type);
1483 return KRB5_PROG_ATYPE_NOSUPP;