2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * lib/krb5/os/hostaddr.c
2N/A *
2N/A * Copyright 1990,1991,2008 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A * This routine returns a list of krb5 addresses given a hostname.
2N/A *
2N/A */
2N/A
2N/A#include "k5-int.h"
2N/A
2N/A#include "fake-addrinfo.h"
2N/A
2N/Akrb5_error_code
2N/Akrb5_os_hostaddr(krb5_context context, const char *name, krb5_address ***ret_addrs)
2N/A{
2N/A krb5_error_code retval;
2N/A krb5_address **addrs;
2N/A int i, j, r;
2N/A struct addrinfo hints, *ai, *aip;
2N/A
2N/A if (!name)
2N/A return KRB5_ERR_BAD_HOSTNAME;
2N/A
2N/A memset (&hints, 0, sizeof (hints));
2N/A hints.ai_flags = AI_NUMERICHOST;
2N/A /* We don't care what kind at this point, really, but without
2N/A this, we can get back multiple sockaddrs per address, for
2N/A SOCK_DGRAM, SOCK_STREAM, and SOCK_RAW. I haven't checked if
2N/A that's what the spec indicates. */
2N/A hints.ai_socktype = SOCK_DGRAM;
2N/A
2N/A r = getaddrinfo (name, 0, &hints, &ai);
2N/A if (r && AI_NUMERICHOST != 0) {
2N/A hints.ai_flags &= ~AI_NUMERICHOST;
2N/A r = getaddrinfo (name, 0, &hints, &ai);
2N/A }
2N/A if (r)
2N/A return KRB5_ERR_BAD_HOSTNAME;
2N/A
2N/A for (i = 0, aip = ai; aip; aip = aip->ai_next) {
2N/A switch (aip->ai_addr->sa_family) {
2N/A case AF_INET:
2N/A#ifdef KRB5_USE_INET6
2N/A case AF_INET6:
2N/A#endif
2N/A i++;
2N/A default:
2N/A /* Ignore addresses of unknown families. */
2N/A ;
2N/A }
2N/A }
2N/A
2N/A addrs = malloc ((i+1) * sizeof(*addrs));
2N/A if (!addrs)
2N/A return ENOMEM;
2N/A
2N/A for (j = 0; j < i + 1; j++)
2N/A addrs[j] = 0;
2N/A
2N/A for (i = 0, aip = ai; aip; aip = aip->ai_next) {
2N/A void *ptr;
2N/A size_t addrlen;
2N/A int atype;
2N/A
2N/A switch (aip->ai_addr->sa_family) {
2N/A case AF_INET:
2N/A addrlen = sizeof (struct in_addr);
2N/A ptr = &((struct sockaddr_in *)aip->ai_addr)->sin_addr;
2N/A atype = ADDRTYPE_INET;
2N/A break;
2N/A#ifdef KRB5_USE_INET6
2N/A case AF_INET6:
2N/A addrlen = sizeof (struct in6_addr);
2N/A ptr = &((struct sockaddr_in6 *)aip->ai_addr)->sin6_addr;
2N/A atype = ADDRTYPE_INET6;
2N/A break;
2N/A#endif
2N/A default:
2N/A continue;
2N/A }
2N/A addrs[i] = (krb5_address *) malloc(sizeof(krb5_address));
2N/A if (!addrs[i]) {
2N/A retval = ENOMEM;
2N/A goto errout;
2N/A }
2N/A addrs[i]->magic = KV5M_ADDRESS;
2N/A addrs[i]->addrtype = atype;
2N/A addrs[i]->length = addrlen;
2N/A addrs[i]->contents = malloc(addrs[i]->length);
2N/A if (!addrs[i]->contents) {
2N/A retval = ENOMEM;
2N/A goto errout;
2N/A }
2N/A memcpy (addrs[i]->contents, ptr, addrs[i]->length);
2N/A i++;
2N/A }
2N/A
2N/A *ret_addrs = addrs;
2N/A if (ai)
2N/A freeaddrinfo(ai);
2N/A return 0;
2N/A
2N/Aerrout:
2N/A /* Solaris Kerberos: fix double free bug */
2N/A if (addrs)
2N/A krb5_free_addresses(context, addrs);
2N/A if (ai)
2N/A freeaddrinfo(ai);
2N/A return retval;
2N/A
2N/A}