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,84 +29,39 @@
* *
**************************************************************************/ **************************************************************************/
/*!
/************************************************************************ * \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
*
* 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) IN struct sockaddr *foreign_sockaddr)
{ {
int ret; int ret;
@@ -137,31 +92,25 @@ 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 : * \return
* IN SOCKINFO *info ; Socket Information Object * \li \c numBytes - On Success, no of bytes received or sent or
* OUT char* buffer ; Buffer to get data to or send data from * \li \c UPNP_E_TIMEDOUT - Timeout
* IN size_t bufsize ; Size of the buffer * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls
* IN int *timeoutSecs ; timeout value */
* IN xboolean bRead ; Boolean value specifying read or write option static int sock_read_write(
* /*! Socket Information Object. */
* Description : Receives or sends data. Also returns the time taken IN SOCKINFO *info,
* to receive or send data. /*! Buffer to get data to or send data from. */
*
* Return :int ;
* numBytes - On Success, no of bytes received or sent
* UPNP_E_TIMEDOUT - Timeout
* UPNP_E_SOCKET_ERROR - Error on socket calls
*
* Note :
************************************************************************/
static int
sock_read_write( IN SOCKINFO * info,
OUT char *buffer, OUT char *buffer,
/*! Size of the buffer. */
IN size_t bufsize, IN size_t bufsize,
/*! timeout value. */
IN int *timeoutSecs, IN int *timeoutSecs,
/*! Boolean value specifying read or write option. */
IN xboolean bRead) IN xboolean bRead)
{ {
int retCode; int retCode;
@@ -171,14 +120,11 @@ sock_read_write( IN SOCKINFO * info,
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) { if (*timeoutSecs < 0) {
return UPNP_E_TIMEDOUT; return UPNP_E_TIMEDOUT;
} }
FD_ZERO(&readSet); FD_ZERO(&readSet);
FD_ZERO(&writeSet); FD_ZERO(&writeSet);
if (bRead) { if (bRead) {
@@ -186,31 +132,28 @@ sock_read_write( IN SOCKINFO * info,
} else { } else {
FD_SET(sockfd, &writeSet); FD_SET(sockfd, &writeSet);
} }
timeout.tv_sec = *timeoutSecs; timeout.tv_sec = *timeoutSecs;
timeout.tv_usec = 0; timeout.tv_usec = 0;
while (TRUE) { while (TRUE) {
if (*timeoutSecs == 0) { if (*timeoutSecs == 0) {
retCode = retCode = select(sockfd + 1, &readSet, &writeSet,
select( sockfd + 1, &readSet, &writeSet, NULL, NULL ); NULL, NULL);
} else { } else {
retCode = retCode = select(sockfd + 1, &readSet, &writeSet,
select( sockfd + 1, &readSet, &writeSet, NULL, &timeout ); NULL, &timeout);
} }
if (retCode == 0) { if (retCode == 0) {
return UPNP_E_TIMEDOUT; return UPNP_E_TIMEDOUT;
} }
if (retCode == -1) { if (retCode == -1) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
return UPNP_E_SOCKET_ERROR; // error return UPNP_E_SOCKET_ERROR;
} else { } else {
break; // read or write /* read or write. */
break;
} }
} }
#ifdef SO_NOSIGPIPE #ifdef SO_NOSIGPIPE
{ {
int old; int old;
@@ -219,41 +162,37 @@ sock_read_write( IN SOCKINFO * info,
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 = num_written = send(sockfd,
send( sockfd, buffer + bytes_sent, byte_left, 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) { if (numBytes < 0) {
return UPNP_E_SOCKET_ERROR; return UPNP_E_SOCKET_ERROR;
} }
// subtract time used for reading/writing /* subtract time used for reading/writing. */
if (*timeoutSecs != 0) { if (*timeoutSecs != 0) {
*timeoutSecs -= time(NULL) - start_time; *timeoutSecs -= time(NULL) - start_time;
} }
@@ -261,54 +200,15 @@ sock_read_write( IN SOCKINFO * info,
return numBytes; return numBytes;
} }
/************************************************************************ int sock_read(IN SOCKINFO *info, OUT char *buffer, IN size_t bufsize,
* Function : sock_read
*
* 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) 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
*
* 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) 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
* Function : sock_init * SOCKINFO structure.
* *
* Parameters : * \return Integer:
* OUT SOCKINFO* info ; Socket Information Object * \li \c UPNP_E_SUCCESS
* IN SOCKET sockfd ; Socket Descriptor * \li \c UPNP_E_OUTOF_MEMORY
* * \li \c UPNP_E_SOCKET_ERROR
* Description : Assign the passed in socket descriptor to socket */
* descriptor in the SOCKINFO structure. int sock_init(
* /*! Socket Information Object. */
* 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(
OUT SOCKINFO *info, OUT SOCKINFO *info,
/*! Socket Descriptor. */
IN SOCKET sockfd);
/*!
* \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.
*
* \return Integer:
* \li \c UPNP_E_SUCCESS
* \li \c UPNP_E_OUTOF_MEMORY
* \li \c UPNP_E_SOCKET_ERROR
*/
int sock_init_with_ip(
/*! Socket Information Object. */
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 : * \return Integer:
* IN SOCKINFO *info ; Socket Information Object * \li \c numBytes - On Success, no of bytes received.
* OUT char* buffer ; Buffer to get data to * \li \c UPNP_E_TIMEDOUT - Timeout.
* IN size_t bufsize ; Size of the buffer * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls.
* IN int *timeoutSecs ; timeout value */
* int sock_read(
* Description : Reads data on socket in sockinfo /*! Socket Information Object. */
* IN SOCKINFO *info,
* Return : int; /*! Buffer to get data to. */
* numBytes - On Success, no of bytes received OUT char* buffer,
* UPNP_E_TIMEDOUT - Timeout /*! Size of the buffer. */
* UPNP_E_SOCKET_ERROR - Error on socket calls IN size_t bufsize,
* /*! timeout value. */
* Note :
************************************************************************/
int sock_read( IN SOCKINFO *info, OUT char* buffer, IN size_t bufsize,
INOUT int *timeoutSecs); INOUT int *timeoutSecs);
/************************************************************************ /*!
* Function : sock_write * \brief Writes data on the socket in sockinfo.
* *
* Parameters : * \return Integer:
* IN SOCKINFO *info ; Socket Information Object * \li \c numBytes - On Success, no of bytes received.
* IN char* buffer ; Buffer to send data from * \li \c UPNP_E_TIMEDOUT - Timeout.
* IN size_t bufsize ; Size of the buffer * \li \c UPNP_E_SOCKET_ERROR - Error on socket calls.
* IN int *timeoutSecs ; timeout value */
* int sock_write(
* Description : Writes data on the socket in sockinfo /*! Socket Information Object. */
* IN SOCKINFO *info,
* Return : int; /*! Buffer to send data from. */
* numBytes - On Success, no of bytes sent IN char* buffer,
* UPNP_E_TIMEDOUT - Timeout /*! Size of the buffer. */
* UPNP_E_SOCKET_ERROR - Error on socket calls IN size_t bufsize,
* /*! timeout value. */
* Note :
************************************************************************/
int sock_write( IN SOCKINFO *info, IN char* buffer, IN size_t bufsize,
INOUT int *timeoutSecs); INOUT int *timeoutSecs);
/************************************************************************ /*!
* Function: sock_destroy * \brief Shutsdown the socket using the ShutdownMethod to indicate whether
* sends and receives on the socket will be dis-allowed.
* *
* Parameters: * After shutting down the socket, closesocket is called to release system
* INOUT SOCKINFO* info ; Socket Information Object * resources used by the socket calls.
* int ShutdownMethod ; How to shutdown the socket. Used by
* sockets's shutdown()
* *
* Description: Shutsdown the socket using the ShutdownMethod to * \return Integer:
* indicate whether sends and receives on the socket will be * \li \c UPNP_E_SOCKET_ERROR on failure.
* dis-allowed. After shutting down the socket, closesocket is called * \li \c UPNP_E_SUCCESS on success.
* to release system resources used by the socket calls. */
* int sock_destroy(
* Return: int; /*! Socket Information Object. */
* UPNP_E_SOCKET_ERROR on failure INOUT SOCKINFO* info,
* UPNP_E_SUCCESS on success /*! How to shutdown the socket. Used by sockets's shutdown(). */
* int ShutdownMethod);
* Note:
************************************************************************/
int sock_destroy(INOUT SOCKINFO* info, int);
/*!
static inline int sock_close(int sock) * \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