1 /* net/getaddr6.c - Determine the IPv6 address of a socket
2 * Copyright (C) 2004,2005 Bruce Guenter <bruce@untroubled.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "sysdeps.h"
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include "socket.h"
25
26 /** Determine the IPv6 address of a socket. */
socket_getaddr6(int sock,ipv6addr * addr,ipv6port * port)27 int socket_getaddr6(int sock, ipv6addr* addr, ipv6port* port)
28 {
29 #if HASIPV6
30 struct sockaddr_in6 sa;
31 socklen_t size;
32 size = sizeof sa;
33 if (getsockname(sock, (struct sockaddr*)&sa, &size) == -1) return 0;
34 if (sa.sin6_family != AF_INET6) return 0;
35 if (size != sizeof sa) return 0;
36 memcpy(&addr->addr, &sa.sin6_addr, 16);
37 *port = ntohs(sa.sin6_port);
38 return 1;
39 #else
40 errno = EPROTONOSUPPORT;
41 return 0;
42 #endif
43 }
44