Doxygen and indentation for sock.

This commit is contained in:
Marcelo Roberto Jimenez
2010-10-20 09:05:42 -02:00
parent 6128296e5f
commit cc472bc2cd
2 changed files with 221 additions and 339 deletions

View File

@@ -29,97 +29,52 @@
* *
**************************************************************************/ **************************************************************************/
/*!
/************************************************************************ * \file
* Purpose: This file implements the sockets functionality *
************************************************************************/ * \brief Implements the sockets functionality.
*/
#include "config.h" #include "config.h"
#include "sock.h" #include "sock.h"
#include "unixutil.h" /* for socklen_t, EAFNOSUPPORT */
#include "unixutil.h" /* for socklen_t, EAFNOSUPPORT */
#include "upnp.h" #include "upnp.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#ifndef MSG_NOSIGNAL #ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0 #define MSG_NOSIGNAL 0
#endif #endif
/************************************************************************ int sock_init(OUT SOCKINFO *info, IN SOCKET sockfd)
* Function : sock_init
*
* Parameters :
* OUT SOCKINFO* info ; Socket Information Object
* IN SOCKET sockfd ; Socket Descriptor
*
* Description : Assign the passed in socket descriptor to socket
* descriptor in the SOCKINFO structure.
*
* Return : int;
* UPNP_E_SUCCESS
* UPNP_E_OUTOF_MEMORY
* UPNP_E_SOCKET_ERROR
*
* Note :
************************************************************************/
int
sock_init( OUT SOCKINFO * info,
IN SOCKET sockfd )
{ {
assert( info ); assert(info);
memset( info, 0, sizeof( SOCKINFO ) ); memset(info, 0, sizeof(SOCKINFO));
info->socket = sockfd;
info->socket = sockfd; return UPNP_E_SUCCESS;
return UPNP_E_SUCCESS;
} }
/************************************************************************ int sock_init_with_ip(OUT SOCKINFO *info, IN SOCKET sockfd,
* Function : sock_init_with_ip IN struct sockaddr *foreign_sockaddr)
*
* Parameters :
* OUT SOCKINFO* info ; Socket Information Object
* IN SOCKET sockfd ; Socket Descriptor
* IN struct sockaddr* foreign_sockaddr; remote socket address.
*
* Description : Calls the sock_init function and assigns the passed in
* IP address and port to the IP address and port in the SOCKINFO
* structure.
*
* Return : int;
* UPNP_E_SUCCESS
* UPNP_E_OUTOF_MEMORY
* UPNP_E_SOCKET_ERROR
*
* Note :
************************************************************************/
int
sock_init_with_ip( OUT SOCKINFO * info,
IN SOCKET sockfd,
IN struct sockaddr* foreign_sockaddr )
{ {
int ret; int ret;
ret = sock_init( info, sockfd ); ret = sock_init(info, sockfd);
if( ret != UPNP_E_SUCCESS ) { if (ret != UPNP_E_SUCCESS) {
return ret; return ret;
} }
memcpy( &info->foreign_sockaddr, foreign_sockaddr, memcpy(&info->foreign_sockaddr, foreign_sockaddr,
sizeof( info->foreign_sockaddr) ); sizeof(info->foreign_sockaddr));
return UPNP_E_SUCCESS; return UPNP_E_SUCCESS;
} }
int sock_destroy(INOUT SOCKINFO *info, int ShutdownMethod) int sock_destroy(INOUT SOCKINFO *info, int ShutdownMethod)
@@ -128,7 +83,7 @@ int sock_destroy(INOUT SOCKINFO *info, int ShutdownMethod)
if (info->socket != -1) { if (info->socket != -1) {
shutdown(info->socket, ShutdownMethod); shutdown(info->socket, ShutdownMethod);
if(sock_close(info->socket) == -1) { if (sock_close(info->socket) == -1) {
ret = UPNP_E_SOCKET_ERROR; ret = UPNP_E_SOCKET_ERROR;
} }
info->socket = -1; info->socket = -1;
@@ -137,178 +92,123 @@ int sock_destroy(INOUT SOCKINFO *info, int ShutdownMethod)
return ret; return ret;
} }
/************************************************************************ /*!
* Function : sock_read_write * \brief Receives or sends data. Also returns the time taken to receive or
* * send data.
* Parameters : *
* IN SOCKINFO *info ; Socket Information Object * \return
* OUT char* buffer ; Buffer to get data to or send data from * \li \c numBytes - On Success, no of bytes received or sent or
* IN size_t bufsize ; Size of the buffer * \li \c UPNP_E_TIMEDOUT - Timeout
* IN int *timeoutSecs ; timeout value * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls
* IN xboolean bRead ; Boolean value specifying read or write option */
* static int sock_read_write(
* Description : Receives or sends data. Also returns the time taken /*! Socket Information Object. */
* to receive or send data. IN SOCKINFO *info,
* /*! Buffer to get data to or send data from. */
* Return :int ; OUT char *buffer,
* numBytes - On Success, no of bytes received or sent /*! Size of the buffer. */
* UPNP_E_TIMEDOUT - Timeout IN size_t bufsize,
* UPNP_E_SOCKET_ERROR - Error on socket calls /*! timeout value. */
* IN int *timeoutSecs,
* Note : /*! Boolean value specifying read or write option. */
************************************************************************/ IN xboolean bRead)
static int
sock_read_write( IN SOCKINFO * info,
OUT char *buffer,
IN size_t bufsize,
IN int *timeoutSecs,
IN xboolean bRead )
{ {
int retCode; int retCode;
fd_set readSet; fd_set readSet;
fd_set writeSet; fd_set writeSet;
struct timeval timeout; struct timeval timeout;
int numBytes; int numBytes;
time_t start_time = time( NULL ); time_t start_time = time(NULL);
SOCKET sockfd = info->socket; SOCKET sockfd = info->socket;
long bytes_sent = 0, long bytes_sent = 0, byte_left = 0, num_written;
byte_left = 0,
num_written;
if( *timeoutSecs < 0 ) {
return UPNP_E_TIMEDOUT;
}
FD_ZERO( &readSet );
FD_ZERO( &writeSet );
if( bRead ) {
FD_SET( sockfd, &readSet );
} else {
FD_SET( sockfd, &writeSet );
}
timeout.tv_sec = *timeoutSecs;
timeout.tv_usec = 0;
while( TRUE ) {
if( *timeoutSecs == 0 ) {
retCode =
select( sockfd + 1, &readSet, &writeSet, NULL, NULL );
} else {
retCode =
select( sockfd + 1, &readSet, &writeSet, NULL, &timeout );
}
if( retCode == 0 ) {
return UPNP_E_TIMEDOUT;
}
if( retCode == -1 ) {
if( errno == EINTR )
continue;
return UPNP_E_SOCKET_ERROR; // error
} else {
break; // read or write
}
}
if (*timeoutSecs < 0) {
return UPNP_E_TIMEDOUT;
}
FD_ZERO(&readSet);
FD_ZERO(&writeSet);
if (bRead) {
FD_SET(sockfd, &readSet);
} else {
FD_SET(sockfd, &writeSet);
}
timeout.tv_sec = *timeoutSecs;
timeout.tv_usec = 0;
while (TRUE) {
if (*timeoutSecs == 0) {
retCode = select(sockfd + 1, &readSet, &writeSet,
NULL, NULL);
} else {
retCode = select(sockfd + 1, &readSet, &writeSet,
NULL, &timeout);
}
if (retCode == 0) {
return UPNP_E_TIMEDOUT;
}
if (retCode == -1) {
if (errno == EINTR)
continue;
return UPNP_E_SOCKET_ERROR;
} else {
/* read or write. */
break;
}
}
#ifdef SO_NOSIGPIPE #ifdef SO_NOSIGPIPE
{ {
int old; int old;
int set = 1; int set = 1;
socklen_t olen = sizeof(old); socklen_t olen = sizeof(old);
getsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &old, &olen); getsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &old, &olen);
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(set)); setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(set));
#endif #endif
if (bRead) {
if( bRead ) { /* read data. */
// read data numBytes = recv(sockfd, buffer, bufsize, MSG_NOSIGNAL);
numBytes = recv( sockfd, buffer, bufsize,MSG_NOSIGNAL); } else {
} else { byte_left = bufsize;
byte_left = bufsize; bytes_sent = 0;
bytes_sent = 0; while (byte_left > 0) {
while( byte_left > 0 ) { /* write data. */
// write data num_written = send(sockfd,
num_written = buffer + bytes_sent, byte_left,
send( sockfd, buffer + bytes_sent, byte_left, MSG_DONTROUTE | MSG_NOSIGNAL);
MSG_DONTROUTE|MSG_NOSIGNAL); if (num_written == -1) {
if( num_written == -1 ) {
#ifdef SO_NOSIGPIPE #ifdef SO_NOSIGPIPE
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &old, olen); setsockopt(sockfd, SOL_SOCKET,
SO_NOSIGPIPE, &old, olen);
#endif #endif
return num_written; return num_written;
} }
byte_left = byte_left - num_written;
byte_left = byte_left - num_written; bytes_sent += num_written;
bytes_sent += num_written; }
} numBytes = bytes_sent;
}
numBytes = bytes_sent;
}
#ifdef SO_NOSIGPIPE #ifdef SO_NOSIGPIPE
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &old, olen); setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &old, olen);
} }
#endif #endif
if (numBytes < 0) {
return UPNP_E_SOCKET_ERROR;
}
/* subtract time used for reading/writing. */
if (*timeoutSecs != 0) {
*timeoutSecs -= time(NULL) - start_time;
}
if( numBytes < 0 ) { return numBytes;
return UPNP_E_SOCKET_ERROR;
}
// subtract time used for reading/writing
if( *timeoutSecs != 0 ) {
*timeoutSecs -= time( NULL ) - start_time;
}
return numBytes;
} }
/************************************************************************ int sock_read(IN SOCKINFO *info, OUT char *buffer, IN size_t bufsize,
* Function : sock_read INOUT int *timeoutSecs)
*
* Parameters :
* IN SOCKINFO *info ; Socket Information Object
* OUT char* buffer ; Buffer to get data to
* IN size_t bufsize ; Size of the buffer
* IN int *timeoutSecs ; timeout value
*
* Description : Calls sock_read_write() for reading data on the
* socket
*
* Return : int;
* Values returned by sock_read_write()
*
* Note :
************************************************************************/
int
sock_read( IN SOCKINFO * info,
OUT char *buffer,
IN size_t bufsize,
INOUT int *timeoutSecs )
{ {
return sock_read_write( info, buffer, bufsize, timeoutSecs, TRUE ); return sock_read_write(info, buffer, bufsize, timeoutSecs, TRUE);
} }
/************************************************************************ int sock_write(IN SOCKINFO *info, IN char *buffer, IN size_t bufsize,
* Function : sock_write INOUT int *timeoutSecs)
*
* Parameters :
* IN SOCKINFO *info ; Socket Information Object
* IN char* buffer ; Buffer to send data from
* IN size_t bufsize ; Size of the buffer
* IN int *timeoutSecs ; timeout value
*
* Description : Calls sock_read_write() for writing data on the
* socket
*
* Return : int;
* sock_read_write()
*
* Note :
************************************************************************/
int
sock_write( IN SOCKINFO * info,
IN char *buffer,
IN size_t bufsize,
INOUT int *timeoutSecs )
{ {
return sock_read_write( info, buffer, bufsize, timeoutSecs, FALSE ); return sock_read_write(info, buffer, bufsize, timeoutSecs, FALSE);
} }

View File

@@ -29,151 +29,134 @@
* *
**************************************************************************/ **************************************************************************/
#ifndef GENLIB_NET_SOCK_H #ifndef GENLIB_NET_SOCK_H
#define GENLIB_NET_SOCK_H #define GENLIB_NET_SOCK_H
/*!
* \file
*/
#include "util.h" #include "util.h"
#ifdef WIN32 #ifdef WIN32
/* Do not #include <netinet/in.h> */ /* Do not #include <netinet/in.h> */
#else #else
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
/* The following are not defined under winsock.h */
/* Following variable is not defined under winsock.h */
#ifndef SD_RECEIVE #ifndef SD_RECEIVE
#define SD_RECEIVE 0x00 #define SD_RECEIVE 0x00
#define SD_SEND 0x01 #define SD_SEND 0x01
#define SD_BOTH 0x02 #define SD_BOTH 0x02
#endif #endif
/*! */
typedef struct typedef struct
{ {
/* handle/descriptor to a socket */ /*! Handle/descriptor to a socket. */
SOCKET socket; SOCKET socket;
/*! The following two fields are filled only in incoming requests. */
/* the following two fields are filled only in incoming requests; */
struct sockaddr_storage foreign_sockaddr; struct sockaddr_storage foreign_sockaddr;
} SOCKINFO; } SOCKINFO;
#ifdef __cplusplus #ifdef __cplusplus
#extern "C" { #extern "C" {
#endif #endif
/*!
* \brief Assign the passed in socket descriptor to socket descriptor in the
* SOCKINFO structure.
*
* \return Integer:
* \li \c UPNP_E_SUCCESS
* \li \c UPNP_E_OUTOF_MEMORY
* \li \c UPNP_E_SOCKET_ERROR
*/
int sock_init(
/*! Socket Information Object. */
OUT SOCKINFO *info,
/*! Socket Descriptor. */
IN SOCKET sockfd);
/************************************************************************ /*!
* Function : sock_init * \brief Calls the sock_init function and assigns the passed in IP address
* * and port to the IP address and port in the SOCKINFO structure.
* Parameters : *
* OUT SOCKINFO* info ; Socket Information Object * \return Integer:
* IN SOCKET sockfd ; Socket Descriptor * \li \c UPNP_E_SUCCESS
* * \li \c UPNP_E_OUTOF_MEMORY
* Description : Assign the passed in socket descriptor to socket * \li \c UPNP_E_SOCKET_ERROR
* descriptor in the SOCKINFO structure. */
*
* Return : int;
* UPNP_E_SUCCESS
* UPNP_E_OUTOF_MEMORY
* UPNP_E_SOCKET_ERROR
* Note :
************************************************************************/
int sock_init(OUT SOCKINFO* info, IN SOCKET sockfd);
/************************************************************************
* Function : sock_init_with_ip
*
* Parameters :
* OUT SOCKINFO* info ; Socket Information Object
* IN SOCKET sockfd ; Socket Descriptor
* IN struct sockaddr* foreign_sockaddr; Remote socket address
*
* Description : Calls the sock_init function and assigns the passed in
* IP address and port to the IP address and port in the SOCKINFO
* structure.
*
* Return : int;
* UPNP_E_SUCCESS
* UPNP_E_OUTOF_MEMORY
* UPNP_E_SOCKET_ERROR
*
* Note :
************************************************************************/
int sock_init_with_ip( int sock_init_with_ip(
/*! Socket Information Object. */
OUT SOCKINFO* info, OUT SOCKINFO* info,
/*! Socket Descriptor. */
IN SOCKET sockfd, IN SOCKET sockfd,
/*! Remote socket address. */
IN struct sockaddr *foreign_sockaddr); IN struct sockaddr *foreign_sockaddr);
/************************************************************************ /*!
* Function : sock_read * \brief Reads data on socket in sockinfo.
*
* Parameters :
* IN SOCKINFO *info ; Socket Information Object
* OUT char* buffer ; Buffer to get data to
* IN size_t bufsize ; Size of the buffer
* IN int *timeoutSecs ; timeout value
*
* Description : Reads data on socket in sockinfo
*
* Return : int;
* numBytes - On Success, no of bytes received
* UPNP_E_TIMEDOUT - Timeout
* UPNP_E_SOCKET_ERROR - Error on socket calls
*
* Note :
************************************************************************/
int sock_read( IN SOCKINFO *info, OUT char* buffer, IN size_t bufsize,
INOUT int *timeoutSecs );
/************************************************************************
* Function : sock_write
*
* Parameters :
* IN SOCKINFO *info ; Socket Information Object
* IN char* buffer ; Buffer to send data from
* IN size_t bufsize ; Size of the buffer
* IN int *timeoutSecs ; timeout value
*
* Description : Writes data on the socket in sockinfo
*
* Return : int;
* numBytes - On Success, no of bytes sent
* UPNP_E_TIMEDOUT - Timeout
* UPNP_E_SOCKET_ERROR - Error on socket calls
*
* Note :
************************************************************************/
int sock_write( IN SOCKINFO *info, IN char* buffer, IN size_t bufsize,
INOUT int *timeoutSecs );
/************************************************************************
* Function: sock_destroy
* *
* Parameters: * \return Integer:
* INOUT SOCKINFO* info ; Socket Information Object * \li \c numBytes - On Success, no of bytes received.
* int ShutdownMethod ; How to shutdown the socket. Used by * \li \c UPNP_E_TIMEDOUT - Timeout.
* sockets's shutdown() * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls.
* */
* Description: Shutsdown the socket using the ShutdownMethod to int sock_read(
* indicate whether sends and receives on the socket will be /*! Socket Information Object. */
* dis-allowed. After shutting down the socket, closesocket is called IN SOCKINFO *info,
* to release system resources used by the socket calls. /*! Buffer to get data to. */
* OUT char* buffer,
* Return: int; /*! Size of the buffer. */
* UPNP_E_SOCKET_ERROR on failure IN size_t bufsize,
* UPNP_E_SUCCESS on success /*! timeout value. */
* INOUT int *timeoutSecs);
* Note:
************************************************************************/
int sock_destroy(INOUT SOCKINFO* info, int);
/*!
* \brief Writes data on the socket in sockinfo.
*
* \return Integer:
* \li \c numBytes - On Success, no of bytes received.
* \li \c UPNP_E_TIMEDOUT - Timeout.
* \li \c UPNP_E_SOCKET_ERROR - Error on socket calls.
*/
int sock_write(
/*! Socket Information Object. */
IN SOCKINFO *info,
/*! Buffer to send data from. */
IN char* buffer,
/*! Size of the buffer. */
IN size_t bufsize,
/*! timeout value. */
INOUT int *timeoutSecs);
static inline int sock_close(int sock) /*!
* \brief Shutsdown the socket using the ShutdownMethod to indicate whether
* sends and receives on the socket will be dis-allowed.
*
* After shutting down the socket, closesocket is called to release system
* resources used by the socket calls.
*
* \return Integer:
* \li \c UPNP_E_SOCKET_ERROR on failure.
* \li \c UPNP_E_SUCCESS on success.
*/
int sock_destroy(
/*! Socket Information Object. */
INOUT SOCKINFO* info,
/*! How to shutdown the socket. Used by sockets's shutdown(). */
int ShutdownMethod);
/*!
* \brief Closes the socket if it is different from -1.
*
* \return -1 if an error occurred or if the socket is -1.
*/
static inline int sock_close(
/*! Socket descriptor. */
int sock)
{ {
int ret = -1; int ret = -1;
@@ -184,7 +167,6 @@ static inline int sock_close(int sock)
return ret; return ret;
} }
#ifdef __cplusplus #ifdef __cplusplus
} /* #extern "C" */ } /* #extern "C" */
#endif #endif