Chapter: Network Programming and Management : Socket Options, Elementary UDP SOC Sockets

Socket Options

There are a number of options like – send buffer size, bypass table look up, get socket type, to name a few - that needs to be set. Also these needs to be got back also. Towards realizing this, there are ways to get and set socket options.

SOCKET OPTIONS

 

There are a number of options like – send buffer size, bypass table look up, get socket type, to name a few - that needs to be set. Also these needs to be got back also. Towards realizing this, there are ways to get and set socket options. These are:

 

         The getsockopt and setsockopt functions

 

         The fcntl functions and

 

           The ioctl functions

 

The options are different for IPv4, IPv6, TCP and for generic categories. These options are briefly discussed in this chapter.

 

Fcntl is the Posix way to set a socket for non blocking I/O, signal driven I/O and to set the owner of a socket. Ioctl is similar to fcntl but also has added functionalities that are not defined in the fcntl.

 

The syntax for  the  getsockopt        and              setsockopt is

givenbelow:

 

#include <sys / socket.h>

 

int getsockopt( int sockfd, int level, int optname, void *optval, socklen_t, *optlen);

 

int setsockopt( int sockfd, int level, int optname, const void *optval, socklen_t, *optlen); both return : 0 if OK –1 on error.

 

The sockfd refers to an open socket descriptor. The level specifies the code in the system to interpret the option. The general socket code, or some protocol specific (IPv4 or IPv6 or TCP etc) The optval is a pointer to a variable from which the new value of the option is fetched by setsockopt, or into which the current value of the option is stored by the getsockopt. The size of this variable is pecified by the final argument, as a value result for getsockopt.

 

The list of options that can be set and get by the socket options are listed in the fig 7.1 (page 179.) These options, in additions being classified based on the protocol, they are classified as either binary options (shown as flags) that enable or disable a certain features and other options that fetch and return specific values that we can either set or examine.

 

 

 

 

 

[Here are the functions for examining and modifying socket options. They are declared in sys/socket.h.

 

— Function: int getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen-ptr)

 

The getsockopt function gets information about the value of option optname at level level for socket socket.

 

The option value is stored in a buffer that optval points to. Before the call, you should supply in *optlen-ptr the size of this buffer; on return, it contains the number of bytes of information actually stored in the buffer.

Most options interpret the optval buffer as a single intvalue.

 

The actual return value of getsockoptis 0on success and -1on failure. The following errnoerror conditions are defined:

EBADF

 

The socket argument is not a valid file descriptor.

 

ENOTSOCK

The descriptor socket is not a socket.

 

ENOPROTOOPT

The optname doesn't make sense for the given level.

Function: int setsockopt (int socket, int level, int optname, void *optval, socklen_t optlen)

 

This function is used to set the socket option optname at level level for socket socket. The value of the option is passed in the buffer optval of size optlen. ]

 

The data type column shows the data types of what the optval pointer must point to for each option. The two braces notation {} is used to indicate a structure.

 

There are two basic types of options : binary options that enables or disables a certain features (flag), and options that fetch and return specific values that we can either set or examine values. The column labeled ―flag‖ specifies if the option is a flag option. When calling getsockopt for these flag options, optval is an integer. The value returned in optval is zero if the option is disabled, or nonzero if the options is enabled. Similarly, setsocket requires a nonzero optval to turn the options on and a zero value to turn the option off. If the flag column does not contain a * then the options is to turn the options is used to pass a value of the specified datatype between user process and the system.



The code in 7.2, 7.3 and 7.4 provides a method to to find out if the given option is supported and if so to print the default value.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Network Programming and Management : Socket Options, Elementary UDP SOC Sockets : Socket Options |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.