|
|
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>s = socket(AF_INET6, SOCK_DGRAM, 0); s = socket(AF_INET6, SOCK_STREAM, 0);
#include <paths.h> #include <fcntl.h> #include <netinet/if.h> #include <netinet/ip_var.h>
fd = open(_PATH_UDPIPV6, flags); fd = open(_PATH_TCPIPV6, flags);
IPv6 options are set with setsockopt(3sock) and examined with getsockopt(3sock), at the IPPROTO_IPV6 level. (NOTE: IPv4 options are described in ip(7tcp).)
Several generic options are supported at the IPv6 level:
Applications already developed using IPv4 will have no knowledge of this socket option; this implies that all IPv4/IPv6 socket transformation should only be performed by IPv6 applications. However, during the transition between IPv4 and IPv6, you may still need to develop pure IPv4 applications. Use IPV6_ADDRFORM to check, and if necessary transform any incoming IPv6 sockets into IPv4 sockets. This will ensure that that your application will work effectively in a mixed IPv4/IPv6 environment.
IPV6_ADDRFORM has one argument, a pointer to a value of either PF_INET or PF_INET6. The example below shows how to check an open socket, and if necessary, convert it so that subsequent system calls using that socket will return IPv6 address structures (sockaddr_in6).
int addrform_in; int addrform_out = PF_INET6; size_t len = sizeof(int);/* Find out if the open socket is PF_INET or PF_INET6 */
if (getsockopt(s, IPPROTO_IP6, IPV6_ADDRFORM, &addrform_in, &len) == -1) perror ("getsockopt IPV6_ADDRFORM");
/* If the open socket is PF_INET, convert it */
if (addrform_in == PF_INET) if (setsockopt(s, IPPROTO_IP6, IPV6_ADDRFORM, &addrform_out, sizeof(addrform_out)) == -1) perror ("setsockopt IPV6_ADDFORM");
int hoplimit = 10;When the IPV6_UNICAST_HOPS option is set with setsockopt(3sock), the specified option value is used as the hop limit for all subsequent unicast packets sent via that socket. If the option is not set, the system selects a default value. The integer hop limit value is interpreted as follows:if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hoplimit, sizeof(hoplimit)) == -1) perror("setsockopt IPV6_UNICAST_HOPS");
int hoplimit; int len = sizeof(hoplimit);if (getsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hoplimit, &len) == -1) perror("getsockopt IPV6_UNICAST_HOPS"); else printf("Current hop limit : %d\n",hoplimit);
struct ipv6_mreq { struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast address */ unsigned int ipv6mr_interface; /* interface index */ };See if(3N) for a description of interface indexes.
RFC 1883, RFC 1884, RFC 1933, RFC 2133