sendmsg()--Send a Message Over a Socket


  BSD 4.3 Syntax
  #include <sys/types.h>
  #include <sys/socket.h>

 int sendmsg(int socket_descriptor,
             struct msghdr *message_structure,
             int flags)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes



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

 ssize_t sendmsg(int socket_descriptor,
                 const struct msghdr *message_structure,
                 int flags)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes

The sendmsg() function is used to send data or descriptors or ancillary data or a combination of these through a connected or unconnected socket.

There are two versions of the API, as shown above. The base IBM i® 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 socket descriptor that is to be written to.

message_structure
(I/O) The pointer to the message structure that contains the following: The structure pointed to by the message_structure parameter is defined in <sys/socket.h>.

The BSD 4.3 structure is:

      struct msghdr {
        caddr_t       msg_name;
        int           msg_namelen;
        struct iovec *msg_iov;
        int           msg_iovlen;
        caddr_t       msg_accrights;
        int           msg_accrightslen;
      };

The BSD 4.4/UNIX 98 compatible structure is:

      struct msghdr {
        void         *msg_name;
        socklen_t     msg_namelen;
        struct iovec *msg_iov;
        int           msg_iovlen;
        void         *msg_control;      /* Set to NULL if not needed  */
        socklen_t     msg_controllen;   /* Set to 0 if not needed     */
        int           msg_flags;
      };

The msg_name and msg_namelen fields contain the address and address length to which the message is sent. For further information about the structure of socket addresses, see the Sockets programming topic collection. If the msg_name field is set to a NULL pointer, the address information is not returned.

The msg_iov and msg_iovlen fields are for scatter/gather I/O.

The BSD 4.3 structure uses the msg_accrights and msg_accrightslen fields to pass descriptors. The msg_accrights field is a list of zero or more descriptors, and msg_accrightslen is the total length (in bytes) of the descriptor list.

The BSD 4.4/UNIX 98 compatible structure uses the msg_control and msg_controllen fields to pass ancillary data. The msg_control field is a pointer to ancillary data (of length msg_controllen) with the form:

     struct cmsghdr {
           socklen_t cmsg_len;    /* # bytes, including this header */
           int       cmsg_level;  /* originating protocol           */
           int       cmsg_type;   /* protocol-specific type         */
                         /* followed by unsigned char cmsg_data[];  */
     };

The cmsg_len field is the total length including this header. cmsg_level is the originating protocol. cmsg_type is the protocol-specific type. If ancillary data and descriptors are not being passed, the msg_control field must be initialized to NULL and the msg_controllen field must be initialized to 0. The following tables list the supported ancillary data types when using the BSD 4.4/UNIX 98 compatible structures.

Ancillary Data Types That Apply to the Socket Layer (where cmsg_level is SOL_SOCKET ):

cmsg_type cmsg_data
SCM_RIGHTS The rest of the buffer is a list of zero or more descriptors to be sent.

This ancillary data type is only supported for sockets with an address family of AF_UNIX or AF_UNIX_CCSID.

Ancillary Data Types That Apply to the IP Layer (where cmsg_level is IPPROTO_IP):

cmsg_type cmsg_data
IP_QOS_CLASSIFICATION_DATA The rest of the buffer is an ip_qos_classification_data structure. This structure is defined in <netinet/ip.h> as:
  struct ip_qos_classification_data {
    int    ip_qos_version;               /* Version of struct    */
    int    ip_qos_classification_scope;  /* Classification scope */
    int    ip_qos_classification_type;   /* Classification type  */
    u_char ip_qos_reserved[12];          /* Reserved             */
    int    ip_qos_appl_token_len;        /* Length of appl data  */
    /* followed by application classification data               */
};
For further information about how this structure should be initialized, see the Quality of Service topic collection.

This ancillary data type is only supported for sockets with an address family of AF_INET and a type of SOCK_STREAM.

Ancillary Data Types That Apply to the IPv6 Layer (where cmsg_level is IPPROTO_IPV6):

cmsg_type cmsg_data
Start of V7R1 changesIPV6_ADDR_PREFERENCES The rest of the buffer is an integer specifying the source address selection preferences that should be used.

The valid source address selection preference flags are defined in <in.h> as:

      #define IPV6_PREFER_SRC_HOME   0x00000001 /* Prefer Home addr    */
      #define IPV6_PREFER_SRC_COA    0x00000002 /* Prefer Care-of addr */
      #define IPV6_PREFER_SRC_TMP    0x00000004 /* Prefer Temp addr    */
      #define IPV6_PREFER_SRC_PUBLIC 0x00000008 /* Prefer Public addr  */
      #define IPV6_PREFER_SRC_CGA    0x00000010 /* Prefer CGA addr     */
      #define IPV6_PREFER_SRC_NONCGA 0x00000020 /* Prefer non-CGA addr */

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.End of V7R1 changes

IPV6_DSTOPTS The rest of the buffer is an ip6_dest structure. This structure is defined in <netinet/ip6.h> as:
   struct ip6_dest {
      uint8_t  ip6d_nxt;        /* next header */
      uint8_t  ip6d_len;        /* length in units of 8 octets */
      /* followed by options */
   };
    The ip6d_nxt field specifies the option type of the next header in the chain of headers.
    The ip6d_len field specifies the length of the destination header in units of 8 octets, not including the first 8 octets.
    For further information about how this structure should be initialized, see inet6_opt_init() and inet6_opt_append().

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_HOPOPTS The rest of the buffer is an ip6_hbh structure. This structure is defined in <netinet/ip6.h> as:
   struct ip6_hbh {
      uint8_t  ip6h_nxt;        /* next header */
      uint8_t  ip6h_len;        /* length in units of 8 octets */
      /* followed by options */
   };
    The ip6h_nxt field specifies the offset of the next header in the chain of headers.
    The ip6h_len field specifies the length of the hop-by-hop header in units of 8 octets, not including the first 8 octets.
    For further information about how this structure should be initialized, see inet6_opt_init() and inet6_opt_append().

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_HOPLIMIT The rest of the buffer is an integer specifying the hop limit.

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_NEXTHOP The rest of the buffer is an sockaddr_in6 structure. This structure is defined in <netinet/in.h>.
For further information about how this structure should be initialized, see AF_INET6 address family.

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_PKTINFO The rest of the buffer is an in6_pktinfo structure. This structure is defined in <netinet/in.h> as:
  struct in6_pktinfo {
    struct in6_addr ipi6_addr;        /* Src/dst IPv6 address      */
    unsigned int    ipi6_ifindex;     /* Send/recv interface index */
  };
    The ipi6_addr field specifies the source IPv6 address that the outgoing packet should use.
    The ipi6_ifindex field specifies the sending interface index that the outgoing packet should use.

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_RTHDR The rest of the buffer is an ip6_rthdr structure. This structure is defined in <netinet/ip6.h> as:
  struct ip6_rthdr {
    uint8_t  ip6r_nxt;        /* next header */
    uint8_t  ip6r_len;        /* length in units of 8 octets */
    uint8_t  ip6r_type;       /* routing type */
    uint8_t  ip6r_segleft;    /* segments left */
    /* followed by routing type specific data */
  };
    The ip6r_nxt field specifies the offset of the next header in the chain of headers.
    The ip6r_len field specifies the length of the routing header in units of 8 octets, not including the first 8 octets.
    The ip6r_type field specifies the routing type.
    The ip6r_segleft field that is returned is the number of hops remaining to reach the destination.
    For further information about how this structure should be initialized, see inet6_rth_init() and inet6_rth_add().

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_RTHDRDSTOPTS The rest of the buffer is an ip6_dest structure. This structure is defined in <netinet/ip6.h> as:
   struct ip6_dest {
      uint8_t  ip6d_nxt;        /* next header */
      uint8_t  ip6d_len;        /* length in units of 8 octets */
      /* followed by options */
   };
    The ip6d_nxt field specifies the option type of the next header in the chain of headers.
    The ip6d_len field specifies the length of the destination header in units of 8 octets, not including the first 8 octets.
    For further information about how this structure should be initialized, see inet6_rth_init() and inet6_rth_add().

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_TCLASS The rest of the buffer is an integer specifying the traffic class.

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

IPV6_USE_MIN_MTU The rest of the buffer is an integer specifying if path MTU discovery is to be used.

This ancillary data type is only supported for sockets with an address family of AF_INET6 and a type of SOCK_DGRAM or SOCK_RAW.

Macros are provided for navigating these structures.

The BSD 4.4/UNIX 98 msg_flags field is ignored for sendmsg().

flags
(Input) A flag value that controls the transmission of the data. The flags value is either zero, or is obtained by performing an OR operation on one or more of the following constants:
MSG_EOR Terminate a record, if supported by the protocol.

MSG_OOB Send data as out-of-band data. Valid only for sockets with an address family of AF_INET or AF_INET6 and type SOCK_STREAM.

MSG_DONTROUTE Bypass routing. Valid only for sockets with address family of AF_INET. It is ignored for other address families.

Authorities

When the address family of the socket identified by the socket_descriptor is AF_INET and is running IP over SNA, the thread must have retrieve, insert, delete, and update authority to the APPC device. When the thread does not have this level of authority, an errno of EACCES is returned.

The user profile for the thread must have *IOSYSCFG special authority to send ancillary data when:

Return Value

sendmsg() returns an integer. Possible values are:


Error Conditions

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

[EACCES] Permission denied.

The process does not have the appropriate privileges to the destination address.

[EADDRNOTAVAIL] Address not available.

A socket with an address family of AF_INET or AF_INET6 is using a connectionless transport service, the socket was not bound. The system tried to bind the socket but could not because a port was not available.

[EBADF] Descriptor not valid.

[ECONNREFUSED] The destination socket refused an attempted connect operation.

This error code can only be returned on sockets that use a connectionless transport service.

[EDESTADDRREQ] Operation requires destination address.

A destination address has not been associated with the socket pointed to by the socket_descriptor parameter and a destination address was not set in the msghdr structure (pointed to by the message_structure parameter). This error code can only be returned on sockets that use a connectionless transport service.

[EFAULT] Bad address.

The system detected an address which was not valid while attempting to access the message_structure parameter or a field within the structure pointed to by the message_structure parameter.

[EHOSTDOWN] A remote host is not available.

This error code can only be returned on sockets that use a connectionless transport service.

[EHOSTUNREACH] A route to the remote host is not available.

This error code can only be returned on sockets that use a connectionless transport service.

[EINTR] Interrupted function call.

[EINVAL] Parameter not valid.

This error code indicates one of the following:

  • The msg_iovlen field or the iov_len field in a iovec structure specifies a negative value. The fields are contained in the msghdr structure (pointed to by the message_structure parameter).

  • The msg_namelen field in the msghdr structure (pointed to by the message_structure parameter) specifies a length that is not valid for the address family.

  • The msg_accrightslen field in the msghdr structure specifies a negative value or is not large enough when msg_accrights was specified.

  • The msg_controllen field in the msghdr structure specifies a negative value or is not large enough when msg_control was specified.

  • 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.
  • Start of V7R1 changesAn invalid flag combination was supplied for IPV6_ADDR_PREFERENCES.End of V7R1 changes

[EIO] Input/output error.

[EISCONN] A connection has already been established.

A destination address was set, but the socket pointed to by the socket_descriptor parameter already has a destination address associated with it.

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

This error code refers to the destination address, and can only be returned by sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[EMSGSIZE] Message size out of range.

This error code indicates one of the following:

  • The data to be sent could not be sent atomically because the total size of the data to be sent is too large.

  • The msg_iovlen field in the msghdr structure (pointed to by the message_structure parameter) specifies a value that is greater than [MSG_MAXIOVLEN] (defined in <sys/socket.h>).

[ENAMETOOLONG] File name too long.

This error code refers to the destination address, and can only be returned by sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENETDOWN] The network is not currently available.

This error code can only be returned on sockets that use a connectionless transport service.

[ENETUNREACH] Cannot reach the destination network.

This error code can only be returned on sockets that use a connectionless transport service.

[ENOBUFS] There is not enough buffer space for the requested operation.
[ENOENT] No such file or directory.

This error code refers to the destination address, and can only be returned by sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENOSYS] Function not implemented.

This error code refers to the destination address, and can only be returned by sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

[ENOTCONN] Requested operation requires a connection.

This error code can only be returned on sockets that use a connection-oriented transport service.

[ENOTDIR] Not a directory.

This error code refers to the destination address, and can only be returned by sockets that use the AF_UNIX or AF_UNIX_CCSID address family.

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

[EOPNOTSUPP] Operation not supported.

This error code indicates one of the following:

  • The flags parameter specifies a value that includes the MSG_OOB flag, but the socket_descriptor parameter points to a connectionless socket.

  • The flags parameter specifies a value that includes the MSG_OOB flag, but the socket_descriptor parameter points to a socket that does not have an address family of AF_INET or AF_INET6.

  • The msg_accrights and msg_accrightslen (or the BSD 4.4/UNIX 98 compatible fields msg_control and msg_controllen) were specified and the underlying instance represented by the descriptor does not support the passing of access rights.

[EPIPE] Broken pipe.

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

[EUNKNOWN] Unknown system state.

[EWOULDBLOCK] Operation would have caused the thread to be suspended.


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. The passing of descriptors is only supported over sockets that have an address family of AF_UNIX or AF_UNIX_CCSID. The msg_accrightslen and the msg_accrights fields (or the BSD 4.4/UNIX 98 compatible fields msg_control and msg_controllen) are ignored if the socket has any other address family. When you use sendmsg() and recvmsg() to pass descriptors, the target job must be running with either of the following: If the target job closes the receiving end of the UNIX domain socket while a descriptor is in transit, the descriptor is reclaimed by the system, and the resource that it represented is closed. For files and directories, the ability to pass descriptors using sendmsg() and recvmsg() is only supported for objects in Root, QOpenSys, User-defined file systems (UDFS), and Network File System (NFS).

  2. sendmsg() is an atomic operation in that it produces one packet of data each time the call is issued on a connectionless socket. For example, a sendmsg() to a datagram socket will result in a single datagram.

  3. A destination address cannot be specified if the socket pointed to by the socket_descriptor parameter already has a destination address associated with it. To not specify an address, users must set the msg_name field to NULL or set the msg_namelen field to zero. (Not specifying an address by setting the msg_namelen field to zero is an IBM® extension.)

    Note: The msg_name and msg_namelen fields are ignored if the socket is using a connection-oriented transport service.

  4. If the socket is using a connectionless transport device, the socket is not bound to an address, and the socket type is SOCK_DGRAM, the system automatically selects an address (INADDR_ANY or in6addr_any and an available port number) and binds it to the socket before sending the data. The IPV6_ADDR_PREFERECES socket option flags set on setsockopt() will be taken into consideration when selecting the source address on which to send the outgoing data.

  5. To broadcast on an AF_INET socket, the socket option SO_BROADCAST must be set (with a setsockopt()).

  6. When using a connection-oriented transport service, all errors except [EUNATCH] and [EUNKNOWN] are mapped to [EPIPE] on an output operation when either of the following occurs: To get the actual error, use getsockopt() with the SO_ERROR option, or perform an input operation (for example, read()).

  7. If the socket is using an address family of AF_UNIX, the destination address (which is a path name) is assumed to be in the default coded character set identifier (CCSID) currently in effect for the job. For AF_UNIX_CCSID, the destination address is assumed to be in the format and coded character set identifier (CCSID) specified in the sockaddr_unc.

  8. For AF_INET sockets over SNA, type SOCK_DGRAM, if a datagram can not be delivered, no errors are returned. (As an example, a datagram might not be delivered if there is no datagram application at the remote host listening at the requested port.)

  9. 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 sendmsg() API is mapped to qso_sendmsg98().

  10. The following functions can be used to build a routing header for IPV6_RTHDR:

  11. The following functions can be used to build an options header for IPV6_HOPOPTS, IPV6_DSTOPTS, or IPV6_RTHDRDSTOPTS:

  12. Data options set using ancillary data on sendmsg() are known as non-sticky options. Non-sticky options apply for only a single datagram. Ancillary data only applies to sockets of type SOCK_DGRAM and SOCK_RAW. For the datagram being sent, non-sticky options will override any sticky options that have been set on the socket using setsockopt().

Related Information



API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]