当前位置:   article > 正文

setsockopt()函数在VxWorks中的使用说明_vxworks setsockopt

vxworks setsockopt

setsockopt( )

NAME
setsockopt( ) - set socket options
SYNOPSIS
STATUS setsockopt
    (
    int  s,        /* target socket */
    int  level,    /* protocol level of option */
    int  optname,  /* option name */
    char *optval,  /* pointer to option value */
    int  optlen    /* option length */
    )
DESCRIPTION

This routine sets the options associated with a socket. To manipulate options at the "socket" level, level should be SOL_SOCKET. Any other levels should use the appropriate protocol number.

OPTIONS FOR STREAM SOCKETS

The following sections discuss the socket options available for stream sockets.

SO_LINGER -- Closing a Connection

Although SO_LINGER is a socket-level option, it presently affects only the TCP and SCTP protocols. The SO_LINGER option is used to specify whether they should perform a "graceful" close. The first part of this section deals primarily with TCP while the SCTP specifics are at the end of the section.

    setsockopt (sock, SOL_SOCKET, SO_LINGER, &optval, sizeof (optval));

For a "graceful" close in response to the shutdown of a connection, TCP tries to make sure that all the unacknowledged data in the transmission channel are acknowledged, and the peer is shut down properly, by going through an elaborate set of state transitions.

The value at optval indicates the amount of time to linger if there is unacknowledged data, using struct linger, defined in the header sys/socket.h. The lingerstructure has two members: l_onoff and l_lingerl_onoff can be set to 1 to turn on the SO_LINGER option, or set to 0 to turn off the SO_LINGER option. l_lingerspecifies a time-out value in seconds.

The default behavior (SO_LINGER disabled, l_onoff = 0) is for a close( ) operation on a socket to return immediately without waiting for outstanding data to be acknowledged by the peer. However, although close( ) returns immediately, TCP does perform a graceful close on the endpoint, retransmitting unacknowledged data and the final FIN as necessary until they have been acknowledged, or until the connection times out. Since the socket has been closed, the application cannot know in this case whether all data were eventually successfully delivered to the peer.

When SO_LINGER is enabled (l_onoff = 1), the behavior upon a close( ) depends upon the value of l_linger. If l_linger is zero, then instead of performing a graceful close, TCP aborts the connection, sending a RST segment to the peer. On the other hand, if l_linger is non-zero, TCP performs a graceful close, and theclose( ) call blocks until either the peer acknowledges any outstanding data and the final FIN segment from the local side, or until the connection times out, or until the timeout specified in l_linger expires, whichever occurs first. Negative or excessively large values of l_linger are treated as infinite time-outs. (In prior VxWorks releases, l_linger was significant only in whether it was zero or non-zero; if non-zero, it was effectively treated as an infinite time-out. This traditional behavior may be restored by defining the configuration parameter SO_LINGER_INIFINITE_CFG of the INCLUDE_BSD_SOCKET component to be TRUE.)

If SO_LINGER is enabled and the time-out specified by l_linger expires before the local side's FIN is acknowledged, the close( ) call will return ERROR with errno set to ETIMEDOUT; the socket has nevertheless been closed. In this case, if the SO_LINGERRESET socket option is enabled, then the local side will abort the connection by sending a TCP RST to the peer. Otherwise, if SO_LINGERRESET is not enabled, the local TCP continues with the graceful shutdown attempt.

A special case applies when the SO_LINGER option is applied to a listening endpoint. In this case, the SO_LINGER option is inherited by the accepted child sockets of the listening parent; as is the parent's l_linger value, if it is nonzero. However, if the parent's l_linger value is zero, the child sockets are given the default value TCP_LINGERTIME (specified in netinet/tcp_timer.h) rather than zero. A child endpoint must explicitly use setsockopt( ) to set its linger time to zero if it wants a close( ) call to abort its connection.

For SCTP the processing is similar to TCP. The option is enabled by setting l_onoff to 1. If the l_linger value is then set to 0 calling close will result in an ABORT instead of a graceful shutdown. If l_linger is set to a positive value a graceful close will be attempted. If it does not complete within the period specified by l_linger the close( ) call will return but the graceful shutdown will continue.

OPTIONS FOR TCP SOCKETS

The following sections discuss the socket options available for TCP (stream) sockets.

SO_KEEPALIVE -- Detecting a Dead Connection

Specify the SO_KEEPALIVE option to make the transport protocol (TCP) initiate a timer to detect a dead connection:

    setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof (optval));

This prevents an application from hanging on an invalid connection. The value at optval for this option is an integer (type int), either 1 (on) or 0 (off).

The integrity of a connection is verified by transmitting zero-length TCP segments triggered by a timer, to force a response from a peer node. If the peer does not respond after repeated transmissions of the KEEPALIVE segments, the connection is dropped, all protocol data structures are reclaimed, and processes sleeping on the connection are awakened with an ETIMEDOUT error.

The ETIMEDOUT timeout can happen in two ways. If the connection is not yet established, the KEEPALIVE timer expires after idling for TCPTV_KEEP_INIT. If the connection is established, the KEEPALIVE timer starts up when there is no traffic for TCPTV_KEEP_IDLE. If no response is received from the peer after sending the KEEPALIVE segment TCPTV_KEEPCNT times with interval TCPTV_KEEPINTVL, TCP assumes that the connection is invalid. The parametersTCPTV_KEEP_INITTCPTV_KEEP_IDLETCPTV_KEEPCNT, and TCPTV_KEEPINTVL are defined in the file target/h/net/tcp_timer.h.

SO_LINGERRESET -- Action on SO_LINGER Timeout

Although SO_LINGERRESET is a socket-level option, it presently affects only the TCP protocol. This socket option has effect only if the SO_LINGER option is enabled with a non-zero linger time, close( ) is called, and the linger time expires before all outstanding data and the final FIN are acknowledged. In this case, ifSO_LINGERRESET has been enabled (integer optval nonzero) the local side will abort the connection, sending a reset segment to the peer. Otherwise, TCP continues with the graceful close attempt after close( ) returns. By default, SO_LINGERRESET is disabled. To enable it:

    int optval = 1;
    setsockopt (sock, SOL_SOCKET, SO_LINGERRESET, &optval, sizeof (optval));

See the discussion of SO_LINGER.

TCP_NODELAY -- Delivering Messages Immediately

Specify the TCP_NODELAY option for real-time protocols, such as the X Window System Protocol, that require immediate delivery of many small messages:

setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof (optval));

The value at optval is an integer (type int) set to either 1 (on) or 0 (off).

By default, the VxWorks TCP implementation employs an algorithm that attempts to avoid the congestion that can be produced by a large number of small TCP segments. This typically arises with virtual terminal applications (such as telnet or rlogin) across networks that have low bandwidth and long delays. The algorithm attempts to have no more than one outstanding unacknowledged segment in the transmission channel while queueing up the rest of the smaller segments for later transmission. Another segment is sent only if enough new data is available to make up a maximum sized segment, or if the outstanding data is acknowledged.

This congestion-avoidance algorithm works well for virtual terminal protocols and bulk data transfer protocols such as FTP without any noticeable side effects. However, real-time protocols that require immediate delivery of many small messages, such as the X Window System Protocol, need to defeat this facility to guarantee proper responsiveness in their operation.

TCP_NODELAY is a mechanism to turn off the use of this algorithm. If this option is turned on and there is data to be sent out, TCP bypasses the congestion-avoidance algorithm: any available data segments are sent out if there is enough space in the send window.

TCP_MAXSEG -- Changing TCP MSS for the connection

Specify the TCP_MAXSEG option to decrease the maximum allowable size of an outgoing TCP segment. This option cannot be used to increase the MSS.

setsockopt (sock, IPPROTO_TCP, TCP_MAXSEG, &optval, sizeof (optval));

The value at optval is an integer set to the desired MSS (eg. 1024).

When a TCP socket is created, the MSS is initialized to the default MSS value which is determined by the configuration parameter TCP_MSS_DFLT (512 by default). When a connection request is received from the other end with an MSS option, the MSS is modified depending on the value of the received MSS and on the results of Path MTU Discovery (which is enabled by default). The MSS may be set as high as the outgoing interface MTU (1460 for an Ethernet). Therefore, after a call to socket but before a connection is established, an application can only decrease the MSS from its default of 512. After a connection is established, the application can decrease the MSS from whatever value was selected.

SO_DEBUG -- Debugging the underlying protocol

Specify the SO_DEBUG option to let the underlying protocol module record debug information.

    setsockopt (sock, SOL_SOCKET, SO_DEBUG, &optval, sizeof (optval));

The value at optval for this option is an integer (type int), either 1 (on) or 0 (off).

OPTION FOR DATAGRAM SOCKETS

The following section discusses an option for datagram (UDP) sockets.

SO_BROADCAST -- Sending to Multiple Destinations

Specify the SO_BROADCAST option when an application needs to send data to a broadcast address.

    setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval));

The value at optval is an integer (type int), either 1 (on) or 0 (off).

OPTION FOR RAW SOCKETS

The following section discusses options for RAW sockets.

IPV6_CHECKSUM - compute the checksum on an IPv6 packet

Enable/disable checksum computation on IPv6 packets.

    setsockopt (sock, IPPROTO_IPV6, IPV6_CHECKSUM, &optval, sizeof (optval));

The value optval is an integer (type int). A value of -1 (default) causes checksum computation to be skipped (except for ICMPv6 packets for which the checksum is always computed). Any other value will result in the checksum being computed and inserted in outgoing packets and also verified for incoming packets. optvalspecifies an offset into the user data where the checksum is located.

ICMP6_FILTER -- filter ICMPv6 packets

Allows filtering ICMPv6 packets received on a raw socket based on the ICMPv6 type field.

    setsockopt (sock, IPPROTO_ICMPV6, ICMP6_FILTER, (char *) &filter, sizeof (filter));

filter is of type struct icmp6_filter and it defines the filter for the socket. Various macros such as ICMP6_FILTER_SETPASS and ICMP6_FILTER_SETBLOCK are available to configure the filter to accept or reject specific ICMPv6 packets. When a raw socket is created, it will pass all ICMPv6 message types to the application by default.

OPTIONS FOR DATAGRAM AND RAW SOCKETS

The following section discusses options for multicasting on UDP and RAW sockets.

IP_ADD_MEMBERSHIP -- Join a Multicast Group

Specify the IP_ADD_MEMBERSHIP option when a process needs to join multicast group:

    setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq,
                sizeof (ipMreq));

The value of ipMreq is an ip_mreq structure. ipMreq.imr_multiaddr.s_addr is the internet multicast address ipMreq.imr_interface.s_addr is the internet unicast address of the interface through which the multicast packet needs to pass.

IP_DROP_MEMBERSHIP -- Leave a Multicast Group

Specify the IP_DROP_MEMBERSHIP option when a process needs to leave a previously joined multicast group:

    setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&ipMreq,
                sizeof (ipMreq));

The value of ipMreq is an ip_mreq structure. ipMreq.imr_multiaddr.s_addr is the internet multicast address. ipMreq.imr_interface.s_addr is the internet unicast address of the interface to which the multicast address was bound.

IPV6_JOIN_GROUP -- join an IPv6 multicast group

Specify the IPV6_JOIN_GROUP option when an application needs to join an IPv6 multicast group:

    setsockopt (sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *) &ip6Mreq,
                sizeof (ip6Mreq));

ip6Mreq is an ipv6_mreq structure. ip6Mreq.ipv6mr_multiaddr.s6_addr is the IPv6 multicast address. ip6Mreq.ipv6mr_interface is the interface index of the interface through which the multicast packet needs to pass. A value of 0 specifies the default multicast interface.

IPV6_LEAVE_GROUP -- leave an IPv6 multicast group

Specify the IPV6_LEAVE_GROUP option when an application needs to join an IPv6 multicast group:

    setsockopt (sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (char *) &ip6Mreq,
                sizeof (ip6Mreq));

ip6Mreq is an ipv6_mreq structure. ip6Mreq.ipv6mr_multiaddr.s6_addr is the IPv6 multicast address. ip6Mreq.ipv6mr_interface is the interface index of the interface to which the multicast address was bound.

IP_MULTICAST_IF -- Select a Default Interface for Outgoing Multicasts

Specify the IP_MULTICAST_IF option when an application needs to specify an outgoing network interface through which all multicast packets are sent:

    setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&ifAddr,
                sizeof (mCastAddr));

The value of ifAddr is an in_addr structure. ifAddr.s_addr is the internet network interface address.

IPV6_MULTICAST_IF -- Select a default interface for outgoing multicasts

Specify the IPV6_MULTICAST_IF option when an application needs to specify an outgoing network interface through which all IPv6 multicast packets are sent:

    setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *) &outIfIdx,
                sizeof (outIfIdx));

outIfIdx is the interface index of the desired interface and is an unsigned integer (type unsigned int).

IP_MULTICAST_TTL -- Select a Default TTL

Specify the IP_MULTICAST_TTL option when an application needs to select a default TTL (time to live) for outgoing multicast packets:

    setsockopt (sock, IPPROTO_IP, IP_MULTICAST_TTL, &optval, sizeof(optval));

The value at optval is an integer (type int), time to live value.

optval(TTL)ApplicationScope

0 same interface
1 same subnet
31local event video 
32 same site
63local event audio 
64 same region
95IETF channel 2 video 
127IETF channel 1 video 
128 same continent
159IETF channel 2 audio 
191IETF channel 1 audio 
223IETF channel 2 low-rate audio 
255IETF channel 1 low-rate audio 
 unrestricted in scope 
IPV6_MULTICAST_HOPS -- Select a hoplimit for outgoing multicasts

Specify the IPV6_MULTICAST_HOPS option when an application needs to select a default hoplimit for outgoing IPv6 multicast packets:

    setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &optval, sizeof (optval));

The value at optval is an integer (type int), which specifies the default hoplimit.

IP_MULTICAST_LOOP -- Enable or Disable Loopback

Enable or disable loopback of outgoing multicasts.

    setsockopt (sock, IPPROTO_IP, IP_MULTICAST_LOOP, &optval, sizeof(optval));

The value at optval is an integer (type int), either 1(on) or 0 (off).

IPV6_MULTICAST_LOOP -- Enable or disable loopback

Enable or disable loopback of outgoing IPv6 multicasts.

    setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &optval, sizeof (optval));

The value at optval is an unsigned integer (type unsigned int), either 1 (on) or 0 (off).

IPV6_RECVPKTINFO -- receive interface information for IPv6 packets

Allows the application to receive interface information about incoming packets.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval, sizeof (optval));

Here optval is a boolean (type int). When it is set to 1, the application can receive the destination IPv6 address and arriving interface index using recvmsg( ). This information is available in a struct in6_pktinfo which is part of a control structure with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_PKTINFO.

IPV6_RECVHOPLIMIT -- receive hop-limit information

Allows the application to receive packet hop-limit information.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval, sizeof (optval));

Here optval is a boolean (type int). Setting it to 1 will make the packet hoplimit value available to the user in the form of socket control information usingrecvmsg( ). The hoplimit will be returned as an integer in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_HOPLIMIT.

IPV6_RECVHOPOPTS -- receive hop-by-hop options

Allows the application to receive hop-by-hop options for an incoming IPv6 packet.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &optval, sizeof (optval));

Here optval is a boolean (type int). Setting it to 1 will make the packet hop-by-hop options available to the user in the form of socket control information usingrecvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_HOPOPTS.

IPV6_RECVDSTOPTS -- receive destination options

Allows the application to receive destination options for an incoming IPv6 packet.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &optval, sizeof (optval));

Here optval is a boolean (type int). Setting it to 1 will make the packet destination options available to the user in the form of socket control information usingrecvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_DSTOPTS.

IPV6_RECVTCLASS -- receive the traffic class

Allows the application to receive the traffic class for an incoming IPv6 packet.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVTCLASS, &optval, sizeof (optval));

Here optval is a boolean (type int). Setting it to 1 will make the traffic class available to the application in the form of socket control information using recvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_TCLASS.

IPV6_RECVRTHDR -- receive routing header

Allows the application to receive the routing header for an incoming IPv6 packet.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVRTHDR, &optval, sizeof (optval));

Here optval is a boolean (type int). Setting it to 1 will make the packet routing header available to the user in the form of socket control information usingrecvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_RTHDR.

IPV6_RECVPATHMTU -- determine the path MTU

Determine the path MTU for PMTU discovery.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RECVPATHMTU, &optval, sizeof (optval));

Here optval is an integer (type int). Setting it to 1 will enable this option. The MTU information is received using the ancillary data item IPV6_PATHMTU which is delivered to recvmsg( ) without any actual data.

When the application is sending packets too big for the path MTU recvmsg( ) will return zero (indicating no data) but there will be a cmsghdr with cmsg_type set to IPV6_PATHMTU, and cmsg_len will indicate that cmsg_data is sizeof(struct ip6_mtuinfo) bytes long. This can happen when the sending node receives a corresponding ICMPv6 packet too big error, or when the packet is sent from a socket with the IPV6_DONTFRAG option being on and the packet size is larger than the MTU of the outgoing interface.

IPV6_DONTFRAG -- send without fragmentation

Prevents automatic insertion of fragment header while sending.

    setsockopt (sock, IPPROTO_IPV6, IPV6_DONTFRAG, &optval, sizeof (optval));

Here optval is an integer (type int). Setting it to 1 will turn off the automatic insertion of a fragment header for UDP and raw sockets if the packet is too large for the path MTU. This can be useful for applications like traceroute which want to avoid fragmentation to discover the actual path MTU. When the data size is larger than the MTU of the outgoing interface, the packet will be discarded. Applications can know the result by enabling the IPV6_RECVPATHMTU option and receiving the corresponding ancillary data items.

This option can also be sent as ancillary data. In the cmsghdr structure containing this ancillary data, the cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will be IPV6_DONTFRAG, and the first byte of cmsg_data[] will be the first byte of the integer.

IPV6_NEXTHOP -- specify the next hop address for the datagram

Allows the application to specify the next hop for the datagram.

    setsockopt (sock, IPPROTO_IPV6, IPV6_NEXTHOP, &addr, sizeof (addr));

Here addr is a structure of type sockaddr. It contains the address of the next hop for the outgoing IPv6 packet.

An alternative to using this option is to specify this information as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal toIPPROTO_IPV6 and cmsg_type equal to IPV6_NEXTHOP.

IPV6_TCLASS -- specify the traffic class

Allows the application to specify the traffic class for the outgoing IPv6 packet.

    setsockopt (sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof (tclass));

Here tclass is an unsigned integer (type u_int).

An alternative to using this option is to specify this information as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal toIPPROTO_IPV6 and cmsg_type equal to IPV6_TCLASS.

IPV6_RTHDR -- send routing header

Allows the application to specify a routing header for the outgoing IPv6 packet.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RTHDR, &ip6rth, sizeof (ip6rth));

Here ip6rth is a structure of type ip6_rthdr. It contains the routing header that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option.

An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_RTHDR.

IPV6_HOPOPTS -- send hop-by-hop options

Allows the application to send hop-by-hop options.

    setsockopt (sock, IPPROTO_IPV6, IPV6_HOPOPTS, &ip6hbh, sizeof (ip6hbh));

Here ip6hbh is a structure of type ip6_hbh. It contains the hop-by-hop options that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option.

An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_HOPOPTS.

IPV6_RTHDRDSTOPTS -- send destination options

Allows the application to send destination options that precede a routing header.

    setsockopt (sock, IPPROTO_IPV6, IPV6_RTHDRDSTOPTS, &ip6dst, sizeof (ip6dst));

Here ip6dst is a structure of type ip6_dest. It contains the destination options that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option.

An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_RTHDRDSTOPTS.

IPV6_DSTOPTS -- send destination options

Allows the application to send destination options that follow a routing header.

    setsockopt (sock, IPPROTO_IPV6, IPV6_DSTOPTS, &ip6dst, sizeof (ip6dst));

Here ip6dst is a structure of type ip6_dest. It contains the destination options that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option. This option is also used when there is no routing header.

An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_DSTOPTS.

OPTIONS FOR DATAGRAM, STREAM AND RAW SOCKETS

The following section discusses options for RAW, DGRAM or STREAM sockets.

IP_OPTIONS -- set options to be included in outgoing datagrams

Sets the IP options sent from this socket with every packet.

    setsockopt (sock, IPPROTO_IP, IP_OPTIONS, optbuf, optbuflen);

Here optbuf is a buffer containing the options.

IP_TOS-- set options to be included in outgoing datagrams

Sets the Type-Of-Service field for each packet sent from this socket.

    setsockopt (sock, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));

Here optval is an integer (type int). This integer can be set to IPTOS_LOWDELAYIPTOS_THROUGHPUTIPTOS_RELIABILITY, or IPTOS_MINCOST, to indicate how the packets sent on this socket should be prioritized.

IP_TTL-- set the time-to-live field in outgoing datagrams

Sets the Time-To-Live field for each packet sent from this socket.

    setsockopt (sock, IPPROTO_IP, IP_TTL, &optval, sizeof(optval));

Here optval is an integer (type int), indicating the number of hops a packet can take before it is discarded.

IPV6_UNICAST_HOPS -- set the hop-limit in outgoing IPv6 packets

Sets the hop-limit field for each packet sent from this socket.

    setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &optval, sizeof (optval));

Here optval is an integer (type int), indicating the number of hops a packet can take before it is discarded.

This option is only available for AF_INET6 sockets. It is similar to the IP_TTL option for AF_INET sockets. Unless the IPV6_V6ONLY option has been set on this socket, this operation also sets the outgoing TTL for the socket since the socket also supports IPv4 and IPv4-mapped addresses.

IP_RECVRETOPTS -- [un-]set queueing of reversed source route

Sets whether or not reversed source route queueing will be enabled for incoming datagrams. (Not implemented)

    setsockopt (sock, IPPROTO_IP, IP_RECVRETOPTS, &optval, sizeof(optval));

Here optval is a boolean (type int). However, this option is currently not implemented, so setting it will not change the behavior of the system.

IP_RECVDSTADDR -- [un-]set queueing of IP destination address

Sets whether or not the socket will receive the IP address of the destination of an incoming datagram in control data.

    setsockopt (sock, IPPROTO_IP, IP_RECVDSTADDR, &optval, sizeof(optval));

Here optval is a boolean (type int).

IP_PORTRANGE -- select range of ephemeral ports

Allows the application to control how ephemeral ports are allocated for AF_INET sockets.

    setsockopt (sock, IPPROTO_IP, IP_PORTRANGE, &optval, sizeof (optval));

Here optval is an integer (type int) which determines which range of ports will be used. Acceptable values are IP_PORTRANGE_DEFAULT (default range of 1024-5000), IP_PORTRANGE_HIGH (49152-65535) and IP_PORTRANGE_LOW (600-1023). All of these range values can be modified using Sysctl.

IPV6_PORTRANGE -- select range of ephemeral ports

Allows the application to control how ephemeral ports are allocated for AF_INET6 sockets.

    setsockopt (sock, IPPROTO_IPV6, IPV6_PORTRANGE, &optval, sizeof (optval));

Here optval is an integer (type int) which determines which range of ports will be used. Acceptable values are IPV6_PORTRANGE_DEFAULT (default range of 1024-5000), IPV6_PORTRANGE_HIGH (49152-65535) and IPV6_PORTRANGE_LOW (600-1023). All of these range values can be modified using Sysctl.

IPV6_V6ONLY -- configure IPv4 support on an IPv6 socket

Enables/disables support for receiving IPv4 traffic on an AF_INET6 socket.

    setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof (optval));

Here optval is a boolean (type int). Setting it to 1 causes the socket to receive IPv6 traffic only. Setting it to 0 (default) restores support for IPv4 packets (using IPv4-mapped addresses). This call must be made before a call to bind.

IPV6_USE_MIN_MTU -- avoid Path MTU Discovery

Allows an application to avoid Path MTU Discovery on the socket.

    setsockopt (sock, IPPROTO_IPV6, IPV6_USE_MIN_MTU, &optval, sizeof (optval));

The value at optval is an integer (type int). If the value is -1, PMTU discovery is performed for unicast destinations but not for multicast. If the value is 0, PMTU discovery is always done. A value of 1 indicates that PMTU discovery should always be performed.

PMTU discovery is avoided by sending at the minimum IPv6 MTU. If the packet is larger than the minimum MTU and this feature has been enabled the IP layer will fragment to the minimum MTU.

This option can also be sent as ancillary data. In the cmsghdr structure containing this ancillary data, the cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will be IPV6_USE_MIN_MTU, and the first byte of cmsg_data[] will be the first byte of the integer.

OPTIONS FOR BOTH STREAM AND DATAGRAM SOCKETS

The following sections describe options that can be used with either stream or datagram sockets.

SO_REUSEADDR -- Reusing a Socket Address

Specify the SO_REUSEADDR option to bind a stream socket to a local port that may be still bound to another stream socket:

    setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));

The value at optval is an integer (type int), either 1 (on) or 0 (off).

When the SO_REUSEADDR option is turned on, applications may bind a stream socket to a local port even if it is still bound to another stream socket, if that other socket is associated with a "zombie" protocol control block context not yet freed from previous sessions. The uniqueness of port number combinations for each connection is still preserved through sanity checks performed at actual connection setup time. If this option is not turned on and an application attempts to bind to a port which is being used by a zombie protocol control block, the bind( ) call fails.

SO_REUSEPORT -- Reusing a Socket address and port

This option is similar to the SO_REUSEADDR option but it allows binding to the same local address and port combination.

    setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof (optval));

The value at optval is an integer (type int), either 1 (on) or 0 (off).

The SO_REUSEPORT option is mainly required by multicast applications where a number of applications need to bind to the same multicast address and port to receive multicast data. Unlike SO_REUSEADDR where only the later applications need to set this option, with SO_REUSEPORT all applications including the first to bind to the port are required to set this option. For multicast addresses SO_REUSEADDR and SO_REUSEPORT show the same behavior so SO_REUSEADDRcan be used instead.

SO_OOBINLINE -- Placing Urgent Data in the Normal Data Stream

Specify the SO_OOBINLINE option to place urgent data within the normal receive data stream:

    setsockopt (sock, SOL_SOCKET, SO_OOBINLINE, &optval, sizeof (optval));

TCP provides an expedited data service which does not conform to the normal constraints of sequencing and flow control of data streams. The expedited service delivers "out-of-band" (urgent) data ahead of other "normal" data to provide interrupt-like services (for example, when you hit a CTRL-C during telnet or rlogin session while data is being displayed on the screen.)

TCP does not actually maintain a separate stream to support the urgent data. Instead, urgent data delivery is implemented as a pointer (in the TCP header) which points to the sequence number of the octet following the urgent data. If more than one transmission of urgent data is received from the peer, they are all put into the normal stream. This is intended for applications that cannot afford to miss out on any urgent data but are usually too slow to respond to them promptly.

OPTIONS FOR STREAM, DATAGRAM AND SEQUENTIAL PACKET SOCKETS
SO_SNDBUF -- Specifying the Size of the Send Buffer

Specify the SO_SNDBUF option to adjust the maximum size of the socket-level send buffer:

    setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof (optval));

The value at optval is an integer (type int) that specifies the size of the socket-level send buffer to be allocated.

When stream, datagram or sequential packet sockets are created, each transport protocol reserves a set amount of space at the socket level for use when the sockets are attached to a protocol. For TCP, the default size of the send buffer is 8192 bytes. For UDP, the default size of the send buffer is 9216 bytes. For COMP, it is 64kbytes. Socket-level buffers are allocated dynamically from the mbuf pool.

The effect of setting the maximum size of buffers (for both SO_SNDBUF and SO_RCVBUF, described below) is not actually to allocate the mbufs from the mbuf pool, but to set the high-water mark in the protocol data structure which is used later to limit the amount of mbuf allocation. Thus, the maximum size specified for the socket level send and receive buffers can affect the performance of bulk data transfers. For example, the size of the TCP receive windows is limited by the remaining socket-level buffer space. These parameters must be adjusted to produce the optimal result for a given application.

SO_RCVBUF -- Specifying the Size of the Receive Buffer

Specify the SO_RCVBUF option to adjust the maximum size of the socket-level receive buffer:

    setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof (optval));

The value at optval is an integer (type int) that specifies the size of the socket-level receive buffer to be allocated.

When stream, datagram or sequential packet sockets are created, each transport protocol reserves a set amount of space at the socket level for use when the sockets are attached to a protocol. For TCP, the default size is 8192 bytes. UDP reserves 41600 bytes, enough space for up to forty incoming datagrams (1 Kbyte each). COMP reserves 64kbytes, which can be any arrangement of packets of any size (64k one-byte packets to one 64kbytes packet).

See the SO_SNDBUF discussion above for a discussion of the impact of buffer size on application performance.

SO_SNDTIMEO -- Specifying the send timeout

Specify the SO_SNDTIMEO option to set a timeout value for output operations:

    setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, &optval, sizeof (optval));

The value at optval is a timeval structure with the number of seconds and microseconds used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if no data was sent. The timer is restarted each time additional data is delivered to the protocol.

SO_RCVTIMEO -- Specifying the receive timeout

Specify the SO_RCVTIMEO option to set a timeout value for input operations:

    setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &optval, sizeof (optval));

The value at optval is a timeval structure with the number of seconds and microseconds used to limit waits for input operations to complete. If a receive operation has blocked for this much time, it returns with a short count or with the error EWOULDBLOCK if no data was received. The timer is restarted each time additional data is received from the protocol. Thus, the limit is, in effect, an inactivity timer.

OPTIONS FOR SCTP SOCKETS

The following sections discuss the socket options available for SCTP (stream) sockets.

SCTP_RTOINFO -- Setting Retransmission Parameters

Specify the SCTP_RTOINFO option to tune the retransmission parameters for SCTP.

   setsockopt (sock, IPPROTO_SCTP, SCTP_RTOINFO, (char *)&sctpRTOInfo,
               sizeof(sctpRTOInfo));

sctpRTOInfo is a sctp_rtoinfo structure. Parameters to initialize, and bound retransmission timeouts (RTO) can be set using this option.

SCTP_ASSOCINFO -- Setting Association Parameters.

Specify the SCTP_ASSOCINFO option to set association and endpoint parameters.

   setsockopt (sock, IPPROTO_SCTP, SCTP_ASSOCINFO, (char *)&sctpAssocInfo,
               sizeof(sctpAssocInfo));

sctpAssocInfo is a sctp_assocparams structure.

SCTP_INITMSG -- Setting Default Association Initialization Parameters.

Specify the SCTP_INITMSG option to set default association initialization parameters.

   setsockopt (sock, IPPROTO_SCTP, SCTP_INITMSG, (char *)&sctpInitMsg,
               sizeof(sctpInitMsg));

sctpInitMsg is a sctp_initmsg structure. This option allows the values used when creating a new association to be set.

SCTP_NODELAY -- Delivering Messages Immediately

Specify the SCTP_NODELAY option to enable or disable any Nagle like algorithm.

   setsockopt (sock, IPPROTO_SCTP, SCTP_NODELAY, (char *)&optval,
               sizeof(optval));

optval is an integer (type int), either 1 (on) or 0 (off). If delays are disabled messages will be sent as soon as possible without adding any delays. If delays are allowed messages may be delayed slightly in order to consolidate them into a smaller number of packets.

SCTP_SET_PEER_PRIMARY_ADDR - Set Primary Address on Peer

Specify the SCTP_SET_PEER_PRIMARY_ADDRESS to change the primary address used by the peer for an association.

   setsockopt (sock, IPPROTO_SCTP, SCTP_SET_PEER_PRIMARY_ADDR,
               (char *)&sctpPeerPrimary, sizeof(sctpPeerPrimary));

sctpPeerPrimary is a sctp_setpeerprim structure. This option allows the local node to request that the peer mark the enclosed address as the primary for this association. The enclosed address must be one of the association's locally bound addresses.

SCTP_PRIMARY_ADDR - Set Local Primary Address

Specify the SCTP_PRIMARY_ADDRESS to change the local primary address used for an association.

   setsockopt (sock, IPPROTO_SCTP, SCTP_PRIMARY_ADDR,
               (char *)&sctpPrimary, sizeof(sctpPrimary));

sctpPrimary is a sctp_setprim structure. This option allows the user to request that the enclosed address is used locally as the primary address for this association. The enclosed address must be one of the association's locally bound addresses.

SCTP_ADAPTION_LAYER -- Set Adapation Layer Indication

Specify SCTP_ADAPTION_LAYER to set the value used in any outgoing adaption layer indication parameters.

   setsockopt (sock, IPPROTO_SCTP, SCTP_ADAPTION_LAYER,
               (char *)&sctpAdaptInd, sizeof(sctpAdaptInd));

sctpAdaptInd is a sctp_setadaption structure.

SCTP_DISABLE_FRAGMENTS -- Enable or Disable Fragmentation

Specify SCTP_DISABLE_FRAGMENTS to enable or disable fragmentation of messages that exceed the MTU.

   setsockopt (sock, IPPROTO_SCTP, SCTP_DISABLE_FRAGMENTS, (char *)&optval,
               sizeof(optval));

optval is an integer (type int), either 1 (on) or 0 (off). If on no SCTP message fragmentation will be performed. If a message exceeds the current PMTU size it will not be sent and an error will be generated.

SCTP_PEER_ADDR_PARAMS -- Set Parameters on Peer Addresses

Specify SCTP_PEER_ADDR_PARAMS to modify the parameters associated with peer addresses.

   setsockopt (sock, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS,
               (char *)&sctpPeerAddrParams, sizeof(sctpPeerAddrParams));

sctpPeerAddrParams is a sctp_paddrparams structure. This option allows the user to configure reachability parameters for a peer's address. The parameters include if heartbeats should be enabled or disabled, the heartbeat interval and the maximum number of retransmission required before an address is considered unreachable.

SCTP_DEFAULT_SEND_PARAM -- Set Default Send Parameters

Specify SCTP_DEFAULT_SEND_PARAM to modify the default parameters used by the sendto function for an association.

   setsockopt (sock, IPPROTO_SCTP, SCTP_DEFAULT_SEND_PARAM,
               (char *)&sctpSndRcvInfo, sizeof(sctpSndRcvInfo));

sctpSndRcvInfo is a sctp_sndrcvinfo structure. This option allows the user to set the parameters that would normally be supplied through ancillary data.

SCTP_EVENTS -- Request Notification and Ancillary Events

Specify SCTP_EVENTS to choose which notification and ancillary data to receive.

   setsockopt (sock, IPPROTO_SCTP, SCTP_EVENTS, (char *)&sctpEvents,
               sizeof(sctpEvents));

sctpEvents is a sctp_event_subscribe structure. Setting a field in this structure to 1 causes the specified notifications or additional infomation to be returned by the recvmsg function.

SCTP_MAXSEG -- Set the Maximum Segment Size

Specify SCTP_MAXSEG in order to set the maximum size to put into a data chunk.

   setsockopt (sock, IPPROTO_SCTP, SCTP_MAXSEG, (char *)&optval,
               sizeof(optval));

optval is an integer (type int). A value of 0 indicates no specific limit, in which case the PMTU will be the limit on the size of a data chunk. Otherwise the size limit will be the lesser of optval and the PMTU.

RETURNS

OK, or ERROR if there is an invalid socket, an unknown option, an option length greater than MLEN, insufficient mbufs, or the call is unable to set the specified option.

ERRNO

Not Available

SEE ALSO

sockLib


以上是官方给的使用说明,我在这里要讲的是在配置发送选项的时候,倒数第二个参数不给初值的话,发送就会出现阻塞!

例如:

struct sockaddr_in  serverAddr;
int nRet = 0;
int nVal;/*/不给初值,发送就会出现阻塞*/
assert(defaultServer != NULL);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(defaultPort);
serverAddr.sin_addr.s_addr = inet_addr(defaultServer);
clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
setsockopt(m_clientsock, SOL_SOCKET, SO_SNDBUF, (char *)&nVal, sizeof(int));//设置发送BUFF

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/188900
推荐阅读
相关标签
  

闽ICP备14008679号