kern_sosetopt Kernel Service

Purpose

Sets the option associated with the socket, either at the socket level or at the protocol level.

Syntax

#include <sys/kern_socket.h>
int  sosetopt( ksocket_t  so,  
int level, int optname, 
struct mbuf *mp )

Parameters

Item Description
so The socket that will be used to set the option.
level The socket level (e.g. SOL_SOCKET) or protocol level (IPPROTO_TCP)
optname The option name to set. Socket options can be found in <sys/socket.h> and the TCP options can be found in <netinet/tcp.h>.
mp The mbuf that contains the option value and will be used to modify the field specified by the option name. The mp->m_len should be the size of the value. The caller must allocate mbuf memory before calling the function.

Description

The kern_sosettopt kernel service sets the option associated with the socket, either at the socket level, or at the protocol level.

Execution Environment

The kern_sosetopt kernel service can be called from the process environment.

Examples

ksocket_t    so;
struct mbuf *mp = NULL;
struct linger *linger;
int rc;
rc = kern_socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP); 		 
if (rc != 0 )  		 
{ 		 		 
       return(-1); 		 
}  		 
mp = m_get(M_DONTWAIT, MT_SOOPTS); /* Caller of kern_sosetopt needs to allocated mbuf  memory */ 		 
if (mp == NULL) 		 
{         		 
       return (-1);     		 
}		 		   		 
mp->m_len = sizeof(struct linger); 		 
linger = mtod(mp, struct linger *); 		 
linger->l_linger = 5; 		 
linger->l_onoff = 1; 		 
rc = kern_sosetopt(so, SOL_SOCKET, SO_LINGER, mp); 		 
if (rc != 0 )  		 
{ 		 		 
       return(-1); 		 
}

Return Values

Item Description
0 Upon Success
>0 Error

The nonzero return value is the error number that is defined in the /usr/include/sys/errno.h file.