bind()--Set Local Address for Socket


  BSD 4.3 Syntax

  #include <sys/types.h>
  #include <sys/socket.h>

 int bind(int socket_descriptor,
          struct sockaddr *local_address,
          int address_length)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes



  UNIX 98 Compatible Syntax
  #define _XOPEN_SOURCE 520
  #include <sys/socket.h>

 int bind(int socket_descriptor,
          const struct sockaddr *local_address,
          socklen_t address_length)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes


The bind() function is used to associate a local address with a socket.

There are two versions of the API, as shown above. The base i5/OS API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.


Parameters

socket_descriptor
(Input) The descriptor of the socket that is to be bound.

local_address
(Input) A pointer to a buffer of type struct sockaddr that contains the local address to which the socket is to be bound. The structure sockaddr is defined in <sys/socket.h>.

The BSD 4.3 structure is:

      struct sockaddr {
         u_short sa_family;
         char    sa_data[14];
      };

The BSD 4.4/UNIX 98 compatible structure is:

      typedef uchar   sa_family_t;

      struct sockaddr {
         uint8_t     sa_len;
         sa_family_t sa_family;
         char        sa_data[14];
      };

The BSD 4.4 sa_len field is the length of the address. The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.

address_length
(Input) The length of the local_address.

Authorities


Return Value

bind() returns an integer. Possible values are:


Error Conditions

When a bind() fails, errno can be set to one of the following:

[EACCES] Permission denied.

The process does not have the appropriate privileges to bind local_address to the socket pointed to by socket_descriptor (for example, if socket_descriptor is a socket with an address family of AF_INET, and the sockaddr_in structure (pointed to by local_address) specified a port that was restricted for use).

[EADDRINUSE] Address already in use.

This error code indicates one of the following:

  • The socket_descriptor points to a socket with an address family of AF_INET, and the address specified in the sockaddr_in structure (pointed to by local_address) has already been assigned to another socket.
  • The socket_descriptor points to a socket with an address family of AF_INET6, and the address specified in the sockaddr_in6 structure (pointed to by local_address) has already been assigned to another socket.
  • The socket_descriptor points to a socket with an address family of AF_UNIX or AF_UNIX_CCSID, and the address specified in the sockaddr_un or sockaddr_unc structure (pointed to by local_address) has already been assigned to another socket.
[EADDRNOTAVAIL] Address not available. This error code indicates one of the following:

  • The socket_descriptor points to a socket with an address family of AF_INET, and the IP address specified in the sockaddr_in structure (pointed to by local_address) is not one defined by the local interfaces.
  • The socket_descriptor points to a socket with an address family of AF_INET6, and the IP address specified in the sockaddr_in6 structure (pointed to by local_address) is not one defined by the local interfaces.
[EAFNOSUPPORT] The type of socket is not supported in this protocol family.

The address family specified in the address structure pointed to by local_address parameter cannot be used with the socket pointed to by the socket_descriptor parameter.

[EBADF] Descriptor not valid.

Start of change [EDESTADDRREQ] The local_address parameter is a null pointer.

This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

End of change
[EFAULT] Bad address.

The system detected an address which was not valid while attempting to access the local_address parameter.

[EINVAL] Parameter not valid. This error code indicates one of the following:

  • The address_length parameter specifies a length that is negative or is not valid for the address family.

  • The socket referenced by socket_descriptor is not a socket of type SOCK_RAW and is already bound to an address.

  • The local address pointed to by the local_address parameter specified an address that was not valid.

  • The socket_descriptor points to a socket with an address family of AF_UNIX_CCSID, and the CCSID specified in sunc_qlg in the sockaddr_unc structure (pointed to by local_address) cannot be converted to the current default CCSID for integrated file system path names.

  • The socket_descriptor points to a socket with an address family of AF_UNIX_CCSID, and there was an incomplete character or shift state sequence at the end of sunc_path in the sockaddr_unc structure (pointed to by local_address).

  • The socket_descriptor points to a socket with an address family of AF_UNIX_CCSID, and the sockaddr_unc structure (pointed to by local_address) was not valid:

    • The sunc_format was not set to SO_UNC_DEFAULT or SO_UNC_USE_QLG.

    • The sunc_zero was not initialized to zeros.

    • The sunc_format field was set to SO_UNC_USE_QLG and the sunc_qlg structure was not valid:

      • The path type was less than 0 or greater than 3.

      • The path length was less than 0 or out of bounds. For example, a single-byte path name was greater than 126 bytes or a double-byte path name was greater than 252 bytes.

      • A reserved field was not initialized to zeros.
[EIO] Input/output error.

[ELOOP] A loop exists in symbolic links encountered during pathname resolution.

This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENAMETOOLONG] File name too long.

This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENOBUFS] There is not enough buffer space for the requested operation.

[ENOENT] No such file or directory.

This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENOSYS] Function not implemented.

This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID or AF_UNIX_CCSID address family.

[ENOTDIR] Not a directory.

This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENOTSOCK] The specified descriptor does not reference a socket.

[EUNKNOWN] Unknown system state.

[EUNATCH] The protocol required to support the specified address family is not available at this time.


Error Messages

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.
CPFA081 E Unable to set return value or error code.


Usage Notes

  1. For sockets that use an address family of AF_UNIX or AF_UNIX_CCSID, the following is applicable:



  2. For sockets that use an address family of AF_INET, the following is applicable:



  3. For sockets that use an address family of AF_INET6, the following is applicable:



  4. When you develop in C-based languages and an application is compiled with the _XOPEN_SOURCE macro defined to the value 520 or greater, the bind() API is mapped to qso_bind98().

Related Information



API introduced: V3R1
Top | UNIX-Type APIs | APIs by category