kern_soreceive Kernel Service

Purpose

The routine processes one record per call and returns the number of bytes requested.

Syntax

#include <sys/kern_socket.h>
int  kern_soreceive( ksocket_t   so, 
struct mbuf **paddr, 
long len, 
struct mbuf **mp, 
struct mbuf **controlp, 
int *flagp )

Parameters

Item Description
so The socket to receive the data.
paddr The foreign socket address information is returned in this pointer. Caller should pass in address of struct mbuf * . Caller needs to free this mbuf after the function returns successfully. Caller can pass in NULL if caller doesn't need foreign address information.
len The length of the data to be received.
mp The mbuf pointer so that data can be returned in an mbuf chain. The caller must pass in the address of struct mbuf *. The caller must free this mbuf after the function returns.
controlp Pointer to an mbuf containing the control information. Caller should pass in address of struct mbuf * . Caller needs to free this mbuf after the function returns successfully. Caller can pass in NULL if there is no control information.
flagp If flagp is not NULL, caller can pass in actual flag. The flags are defined in the <sys/socket.h> file. The kern_soreceive routine will use flags set in flagp. The caller can set the flagsp to MSG_WAITALL or MSG_NONBLOCK. On return, it will set flagp to MSG_TRUNC, MSG_OOB wherever applicable.

Description

The kern_soreceive kernel service processes one record per call and returns the number of bytes that are requested. If there is data in the socket receive buffer, the kern_soreceive kernel service returns up to < len> bytes as a mbuf chain. The actual number of bytes returned is computed by adding the m_len fields of each mbuf in the chain. If there is no data, but the connection is still established, the kern_soreceive kernel service either returns EAGAIN with *mp set to NULL (if MSG_NONBLOCK is set) or returns wait for data to arrive (if MSG_NONBLOCK is not set). If the connection is closed before the call or while waiting for data, the *mp is set to NULL and 0 is returned. Waiting may be interrupted, in which case kern_soreceive returns EAGAIN, EINTR, or ERESTART and *mp is undefined. The application might return EINTR, but calls the kern_soreceive kernel service again.

Execution Environment

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

Examples

ksocket_t   so;
struct mbuf *data = NULL;
struct sockaddr_in faddr;
long        len = 512; 
int         flags = 0;
int		       rc;
rc = kern_socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP); 		 
if (rc != 0 )  		 
{ 		 		 
			return(-1); 		 
}		   		 
bzero(&faddr, sizeof(struct sockaddr_in)); 		 
faddr.sin_family = AF_INET; 		 
faddr.sin_port = 23456; 		 
faddr.sin_len = sizeof(struct sockaddr_in); 		 
faddr.sin_addr.s_addr = inet_addr("9.3.108.210"); 		 
rc = kern_soconnect(so, (struct sockaddr *) &faddr); 		 
if (rc != 0 )  		 
{ 		 		 
		 return(-1); 		 
}		   		 
do 		 
{ 		 		 
			rc = kern_soreceive(so, NULL, len, &data, NULL, &flags); 		 
}  while (rc == EAGAIN || rc == EINTR || rc == ERESTART); 		  		 
if ((rc == 0) && data) 		 
{ 		 		 
		/* process the data */ 		 		 
		... 		 		
  m_freem(data); /* Caller needs to free the mbuf after kern_soreceive. */ 		 
} 		 
else 		 
{ 		 		 
	  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.