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