xref: /illumos-gate/usr/src/cmd/krb5/krb5kdc/sock2p.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate /*
3*7c478bd9Sstevel@tonic-gate  * kdc/sock2p.c
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * Copyright 2000 by the Massachusetts Institute of Technology.
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
8*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
9*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
10*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
14*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
15*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
16*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
17*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
18*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
19*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
20*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
21*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
22*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
23*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
24*7c478bd9Sstevel@tonic-gate  * or implied warranty.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Network code for Kerberos v5 KDC.
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #define NEED_SOCKETS
31*7c478bd9Sstevel@tonic-gate #include "k5-int.h"
32*7c478bd9Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifndef HAVE_INET_NTOP
38*7c478bd9Sstevel@tonic-gate char *
inet_ntop(int family,const void * address,char * buf,size_t bufsiz)39*7c478bd9Sstevel@tonic-gate inet_ntop (int family, const void *address, char *buf, size_t bufsiz)
40*7c478bd9Sstevel@tonic-gate {
41*7c478bd9Sstevel@tonic-gate     char *p;
42*7c478bd9Sstevel@tonic-gate     switch (family) {
43*7c478bd9Sstevel@tonic-gate     case AF_INET:
44*7c478bd9Sstevel@tonic-gate     {
45*7c478bd9Sstevel@tonic-gate 	p = inet_ntoa (*(const struct in_addr *)address);
46*7c478bd9Sstevel@tonic-gate     try:
47*7c478bd9Sstevel@tonic-gate 	if (strlen (p) >= bufsiz)
48*7c478bd9Sstevel@tonic-gate 	    return 0;
49*7c478bd9Sstevel@tonic-gate 	strcpy (buf, p);
50*7c478bd9Sstevel@tonic-gate 	break;
51*7c478bd9Sstevel@tonic-gate     }
52*7c478bd9Sstevel@tonic-gate #ifdef KRB5_USE_INET6
53*7c478bd9Sstevel@tonic-gate     case AF_INET6:
54*7c478bd9Sstevel@tonic-gate     {
55*7c478bd9Sstevel@tonic-gate 	char abuf[46];
56*7c478bd9Sstevel@tonic-gate 	const unsigned char *byte = (const unsigned char *)
57*7c478bd9Sstevel@tonic-gate 	    &((const struct in6_addr *)address)->s6_addr;
58*7c478bd9Sstevel@tonic-gate 	sprintf (abuf, "%x:%x:%x:%x:%x:%x:%x:%x",
59*7c478bd9Sstevel@tonic-gate 		 byte[0] * 256 + byte[1],
60*7c478bd9Sstevel@tonic-gate 		 byte[2] * 256 + byte[3],
61*7c478bd9Sstevel@tonic-gate 		 byte[4] * 256 + byte[5],
62*7c478bd9Sstevel@tonic-gate 		 byte[6] * 256 + byte[7],
63*7c478bd9Sstevel@tonic-gate 		 byte[8] * 256 + byte[9],
64*7c478bd9Sstevel@tonic-gate 		 byte[10] * 256 + byte[11],
65*7c478bd9Sstevel@tonic-gate 		 byte[12] * 256 + byte[13],
66*7c478bd9Sstevel@tonic-gate 		 byte[14] * 256 + byte[15]);
67*7c478bd9Sstevel@tonic-gate 	p = abuf;
68*7c478bd9Sstevel@tonic-gate 	goto try;
69*7c478bd9Sstevel@tonic-gate     }
70*7c478bd9Sstevel@tonic-gate #endif /* KRB5_USE_INET6 */
71*7c478bd9Sstevel@tonic-gate     default:
72*7c478bd9Sstevel@tonic-gate 	return 0;
73*7c478bd9Sstevel@tonic-gate     }
74*7c478bd9Sstevel@tonic-gate     return buf;
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate #endif
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate void
sockaddr2p(const struct sockaddr * s,char * buf,size_t bufsiz,int * port_p)79*7c478bd9Sstevel@tonic-gate sockaddr2p (const struct sockaddr *s, char *buf, size_t bufsiz, int *port_p)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate     const void *addr;
82*7c478bd9Sstevel@tonic-gate     int port;
83*7c478bd9Sstevel@tonic-gate     switch (s->sa_family) {
84*7c478bd9Sstevel@tonic-gate     case AF_INET:
85*7c478bd9Sstevel@tonic-gate 	addr = &((const struct sockaddr_in *)s)->sin_addr;
86*7c478bd9Sstevel@tonic-gate 	port = ((const struct sockaddr_in *)s)->sin_port;
87*7c478bd9Sstevel@tonic-gate 	break;
88*7c478bd9Sstevel@tonic-gate #ifdef KRB5_USE_INET6
89*7c478bd9Sstevel@tonic-gate     case AF_INET6:
90*7c478bd9Sstevel@tonic-gate 	addr = &((const struct sockaddr_in6 *)s)->sin6_addr;
91*7c478bd9Sstevel@tonic-gate 	port = ((const struct sockaddr_in6 *)s)->sin6_port;
92*7c478bd9Sstevel@tonic-gate 	break;
93*7c478bd9Sstevel@tonic-gate #endif
94*7c478bd9Sstevel@tonic-gate     default:
95*7c478bd9Sstevel@tonic-gate 	if (bufsiz >= 2)
96*7c478bd9Sstevel@tonic-gate 	    strcpy (buf, "?");
97*7c478bd9Sstevel@tonic-gate 	if (port_p)
98*7c478bd9Sstevel@tonic-gate 	    *port_p = -1;
99*7c478bd9Sstevel@tonic-gate 	return;
100*7c478bd9Sstevel@tonic-gate     }
101*7c478bd9Sstevel@tonic-gate     if (inet_ntop (s->sa_family, addr, buf, bufsiz) == 0 && bufsiz >= 2)
102*7c478bd9Sstevel@tonic-gate 	strcpy (buf, "?");
103*7c478bd9Sstevel@tonic-gate     if (port_p)
104*7c478bd9Sstevel@tonic-gate 	*port_p = port;
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate #endif /* INET */
108