Programming with sockets

Passing sockets between applications

Within the UNIX environment, it is common practice to pass open sockets between applications using exec. With a mixture of IPv4 and IPv6 applications running on the same host, passing open sockets becomes more complicated. An IPv4 socket references the sockadd_in structure, but an IPv6 socket references sockaddr_in6. To combat this problem a new socket option, IPV6_ADDRFORM, has been introduced.

IPV6_ADDRFORM provides socket transformation between IPv4 and IPv6, enabling sockets to be passed between IPv4 and IPv6 applications. All IPv4/IPv6 socket transformation should 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, and vice versa. This will ensure that your application will work effectively in a mixed IPv4/IPv6 environment.

IPV6_ADDRFORM has one argument, an integer pointer. This pointer must point 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 IPv4 address structures (sockaddr_in).

   int addrform_in;
   int addrform_out = PF_INET;
   size_t len = sizeof(int);

/* Find out if the open socket is already an IPv4 socket */

if (getsockopt(s, IPPROTO_IP, IPV6_ADDRFORM, &addrform_in, &len) == -1) perror ("getsockopt IPV6_ADDRFORM");

/* If the open socket is PF_INET6, convert it */

if (addrform_in == PF_INET6) if (setsockopt(s, IPPROTO_IP, IPV6_ADDRFORM, &addrform_out, sizeof(addrform_out)) == -1) perror ("setsockopt IPV6_ADDFORM");

NOTE: At the moment, the only IPv6 addresses that are used are IPv4-mapped. This means that the getsockopt and setsockopt functions can determine both the IPv4 and IPv6 address from just one type of address. This in turn enables the two functions to transform sockets. However, once true IPv6 addresses start to be used, a problem arises because there is no longer a direct relationship between IPv6 and IPv4 addresses, that is, not all IPv6 addresses would be convertable to IPv4. To check what sort of address you have, use the IN6_IS_ADDR_V4MAPPED macro which is described on the inet(S) manual page.

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005