7.2.11.3 Network Socket Address

A socket address object identifies a socket endpoint for communication. In the case of AF_INET for instance, the socket address object comprises the host address (or interface on the host) and a port number which specifies a particular open socket in a running client or server process. A socket address object can be created with,

Scheme Procedure: make-socket-address AF_INET ipv4addr port
Scheme Procedure: make-socket-address AF_INET6 ipv6addr port [flowinfo [scopeid]]
Scheme Procedure: make-socket-address AF_UNIX path
C Function: scm_make_socket_address (family, address, arglist)

Return a new socket address object. The first argument is the address family, one of the AF constants, then the arguments vary according to the family.

For AF_INET the arguments are an IPv4 network address number (see Network Address Conversion), and a port number.

For AF_INET6 the arguments are an IPv6 network address number and a port number. Optional flowinfo and scopeid arguments may be given (both integers, default 0).

For AF_UNIX the argument is a filename (a string).

The C function scm_make_socket_address takes the family and address arguments directly, then arglist is a list of further arguments, being the port for IPv4, port and optional flowinfo and scopeid for IPv6, or the empty list SCM_EOL for Unix domain.

The following functions access the fields of a socket address object,

Scheme Procedure: sockaddr:fam sa

Return the address family from socket address object sa. This is one of the AF constants (e.g. AF_INET).

Scheme Procedure: sockaddr:path sa

For an AF_UNIX socket address object sa, return the filename.

Scheme Procedure: sockaddr:addr sa

For an AF_INET or AF_INET6 socket address object sa, return the network address number.

Scheme Procedure: sockaddr:port sa

For an AF_INET or AF_INET6 socket address object sa, return the port number.

Scheme Procedure: sockaddr:flowinfo sa

For an AF_INET6 socket address object sa, return the flowinfo value.

Scheme Procedure: sockaddr:scopeid sa

For an AF_INET6 socket address object sa, return the scope ID value.

The functions below convert to and from the C struct sockaddr (see Address Formats in The GNU C Library Reference Manual). That structure is a generic type, an application can cast to or from struct sockaddr_in, struct sockaddr_in6 or struct sockaddr_un according to the address family.

In a struct sockaddr taken or returned, the byte ordering in the fields follows the C conventions (see Byte Order Conversion in The GNU C Library Reference Manual). This means network byte order for AF_INET host address (sin_addr.s_addr) and port number (sin_port), and AF_INET6 port number (sin6_port). But at the Scheme level these values are taken or returned in host byte order, so the port is an ordinary integer, and the host address likewise is an ordinary integer (as described in Network Address Conversion).

C Function: struct sockaddr * scm_c_make_socket_address (SCM family, SCM address, SCM args, size_t *outsize)

Return a newly-malloced struct sockaddr created from arguments like those taken by scm_make_socket_address above.

The size (in bytes) of the struct sockaddr return is stored into *outsize. An application must call free to release the returned structure when no longer required.

C Function: SCM scm_from_sockaddr (const struct sockaddr *address, unsigned address_size)

Return a Scheme socket address object from the C address structure. address_size is the size in bytes of address.

C Function: struct sockaddr * scm_to_sockaddr (SCM address, size_t *address_size)

Return a newly-malloced struct sockaddr from a Scheme level socket address object.

The size (in bytes) of the struct sockaddr return is stored into *outsize. An application must call free to release the returned structure when no longer required.