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