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