2 * lib/krb5/os/locate_kdc.c
4 * Copyright 1990 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
24 * get socket addresses for KDC.
32 extern char *krb5_kdc_udp_portname;
33 extern char *krb5_kdc_sec_udp_portname;
37 * returns count of number of addresses found
40 krb5_error_code INTERFACE
41 krb5_locate_kdc(context, realm, addr_pp, naddrs)
43 const krb5_data *realm;
44 struct sockaddr **addr_pp;
50 struct sockaddr *addr_p;
51 struct sockaddr_in *sin_p;
55 u_short udpport = htons(KRB5_DEFAULT_PORT);
56 u_short sec_udpport = htons(KRB5_DEFAULT_SEC_PORT);
61 if (code = krb5_get_krbhst (context, realm, &hostlist))
65 if (sp = getservbyname(krb5_kdc_udp_portname, "udp"))
67 if (krb5_kdc_sec_udp_portname)
68 if (sp = getservbyname(krb5_kdc_sec_udp_portname, "udp")) {
69 #ifdef KRB5_TRY_SECONDARY_PORT_FIRST
70 sec_udpport = udpport;
73 sec_udpport = sp->s_port;
79 while (hostlist[count])
92 addr_p = (struct sockaddr *)malloc (sizeof (struct sockaddr) * count);
94 for (i=0, out=0; hostlist[i]; i++) {
95 hp = gethostbyname(hostlist[i]);
97 switch (hp->h_addrtype) {
100 if (udpport) { /* must have gotten a port # */
101 for (j=0; hp->h_addr_list[j]; j++) {
102 sin_p = (struct sockaddr_in *) &addr_p[out++];
103 memset ((char *)sin_p, 0, sizeof(struct sockaddr));
104 sin_p->sin_family = hp->h_addrtype;
105 sin_p->sin_port = udpport;
106 memcpy((char *)&sin_p->sin_addr,
107 (char *)hp->h_addr_list[j],
108 sizeof(struct in_addr));
111 addr_p = (struct sockaddr *)
112 realloc ((char *)addr_p,
113 sizeof(struct sockaddr) * count);
116 addr_p[out] = addr_p[out-1];
117 sin_p = (struct sockaddr_in *) &addr_p[out++];
118 sin_p->sin_port = sec_udpport;
121 addr_p = (struct sockaddr *)
122 realloc ((char *)addr_p,
123 sizeof(struct sockaddr) * count);
137 free ((char *)hostlist);
139 if (out == 0) { /* Couldn't resolve any KDC names */
141 return KRB5_REALM_CANT_RESOLVE;