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