Network Socket Library

## Files

file | UpnpInet.h
| Provides a platform independent way to include TCP/IP types and functions.
file | sock.c
| Implements the sockets functionality.
file | sock.h

## Data Structures

struct | SOCKINFO

## Macros

#define | INVALID_SOCKET (-1)
#define | SOCKET_ERROR (-1)
#define | UpnpCloseSocket close
#define | OPTION_VALUE_CAST const void *
#define | MSG_NOSIGNAL 0
#define | SD_RECEIVE 0x00
#define | SD_SEND 0x01
#define | SD_BOTH 0x02

## Typedefs

typedef int | SOCKET

## Functions

int | sock_init ( SOCKINFO *info, SOCKET sockfd)
| Assign the passed in socket descriptor to socket descriptor in the SOCKINFO structure.
int | sock_init_with_ip ( SOCKINFO *info, SOCKET sockfd, struct sockaddr *foreign_sockaddr)
| Calls the sock_init function and assigns the passed in IP address and port to the IP address and port in the SOCKINFO structure.
int | sock_destroy ( SOCKINFO *info, int ShutdownMethod)
| Associates an SSL object with the socket and begins the client-side SSL/TLS handshake.
static int | sock_read_write (SOCKINFO *info, char *buffer, size_t bufsize, int *timeoutSecs, int bRead)
| Receives or sends data. Also returns the time taken to receive or send data.
int | sock_read ( SOCKINFO *info, char *buffer, size_t bufsize, int *timeoutSecs)
| Reads data on socket in sockinfo.
int | sock_write ( SOCKINFO *info, const char *buffer, size_t bufsize, int *timeoutSecs)
| Writes data on the socket in sockinfo.
int | sock_make_blocking ( SOCKET sock)
| Make socket blocking.
int | sock_make_no_blocking ( SOCKET sock)
| Make socket non-blocking.
static UPNP_INLINE int | sock_close (SOCKET sock)
| Closes the socket if it is different from -1.

## Variables

SOCKET | SOCKINFO::socket
struct sockaddr_storage | SOCKINFO::foreign_sockaddr

## Detailed Description

## Macro Definition Documentation

## INVALID_SOCKET

#define INVALID_SOCKET (-1)

INVALID_SOCKET is unsigned on win32.

## OPTION_VALUE_CAST

#define OPTION_VALUE_CAST const void *

Winsock declares setsockopt() like this:

int setsockopt( [in] SOCKET s, [in] int level, [in] int optname, [in] const char *optval, [in] int optlen );

While POSIX declares it like this:

#include <sys/socket.h>

int setsockopt( int socket, int level, int option_name, const void *option_value, socklen_t option_len );

They diverge on the declaration of option_value, which causes troubles on Windows compilation. The following define addresses this issue.

## SOCKET_ERROR

#define SOCKET_ERROR (-1)

select() returns SOCKET_ERROR on win32.

## UpnpCloseSocket

#define UpnpCloseSocket close

Alias to close() to make code more WIN32 tolerant.

## Typedef Documentation

## SOCKET

typedef int SOCKET

This typedef makes the code slightly more WIN32 tolerant. On WIN32 systems, SOCKET is unsigned and is not a file descriptor.

## Function Documentation

## sock_close()

static UPNP_INLINE int sock_close | ( | SOCKET | sock | ) |

static

Closes the socket if it is different from -1.

Returns
  -1 if an error occurred or if the socket is -1.

Parameters

  sock | Socket descriptor.

References INVALID_SOCKET, and UpnpCloseSocket.

Referenced by get_miniserver_stopsock(), RunMiniServer(), sock_destroy(), StartMiniServer(), and StopMiniServer().

## sock_destroy()

int sock_destroy | ( | SOCKINFO * | info,
|  | int | ShutdownMethod
| ) |  |

Associates an SSL object with the socket and begins the client-side SSL/TLS handshake.

Returns
  Integer:
  - `UPNP_E_SUCCESS`
  - `UPNP_E_SOCKET_ERROR`
  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.

Returns
  Integer:
  - `UPNP_E_SOCKET_ERROR` on failure.
  - `UPNP_E_SUCCESS` on success.

Parameters

  [in,out] | info | Socket Information Object.
  [in] | ShutdownMethod | How to shutdown the socket. Used by sockets's shutdown().

References INVALID_SOCKET, sock_close(), SOCKINFO::socket, UPNP_E_SOCKET_ERROR, UPNP_E_SUCCESS, and UpnpPrintf().

Referenced by http_CloseHttpConnection(), http_OpenHttpConnection(), and notify_send_and_recv().

## sock_init()

int sock_init | ( | SOCKINFO * | info,
|  | SOCKET | sockfd
| ) |  |

Assign the passed in socket descriptor to socket descriptor in the SOCKINFO structure.

Returns
  Integer:
  - `UPNP_E_SUCCESS`
  - `UPNP_E_OUTOF_MEMORY`
  - `UPNP_E_SOCKET_ERROR`

Parameters

  [out] | info | Socket Information Object.
  [in] | sockfd | Socket Descriptor.

References SOCKINFO::socket, and UPNP_E_SUCCESS.

Referenced by http_OpenHttpConnection(), notify_send_and_recv(), and sock_init_with_ip().

## sock_init_with_ip()

int sock_init_with_ip | ( | SOCKINFO * | info,
|  | SOCKET | sockfd,
|  | struct sockaddr * | foreign_sockaddr
| ) |  |

Calls the sock_init function and assigns the passed in IP address and port to the IP address and port in the SOCKINFO structure.

Returns
  Integer:
  - `UPNP_E_SUCCESS`
  - `UPNP_E_OUTOF_MEMORY`
  - `UPNP_E_SOCKET_ERROR`

Parameters

  [out] | info | Socket Information Object.
  [in] | sockfd | Socket Descriptor.
  [in] | foreign_sockaddr | Remote socket address.

References SOCKINFO::foreign_sockaddr, sock_init(), and UPNP_E_SUCCESS.

## sock_make_blocking()

int sock_make_blocking | ( | SOCKET | sock | ) |

Make socket blocking.

Returns
  0 if successful, -1 otherwise.

Referenced by Check_Connect_And_Wait_Connection().

## sock_make_no_blocking()

int sock_make_no_blocking | ( | SOCKET | sock | ) |

Make socket non-blocking.

Returns
  0 if successful, -1 otherwise.

Referenced by Check_Connect_And_Wait_Connection(), and create_ssdp_sock_reqv4().

## sock_read()

int sock_read | ( | SOCKINFO * | info,
|  | char * | buffer,
|  | size_t | bufsize,
|  | int * | timeoutSecs
| ) |  |

Reads data on socket in sockinfo.

Returns
  Integer:
  - `numBytes` - On Success, no of bytes received.
  - `UPNP_E_TIMEDOUT` - Timeout.
  - `UPNP_E_SOCKET_ERROR` - Error on socket calls.

Parameters

  [in] | info | Socket Information Object.
  [out] | buffer | Buffer to get data to.
  [in] | bufsize | Size of the buffer.
  [in,out] | timeoutSecs | timeout value.

References sock_read_write().

Referenced by http_ReadHttpResponse(), http_RecvMessage(), http_RecvPostMessage(), and ReadResponseLineAndHeaders().

## sock_read_write()

static int sock_read_write | ( | SOCKINFO * | info,
|  | char * | buffer,
|  | size_t | bufsize,
|  | int * | timeoutSecs,
|  | int | bRead
| ) |  |

static

Receives or sends data. Also returns the time taken to receive or send data.

Returns
  - `numBytes` - On Success, no of bytes received or sent or
  - `UPNP_E_TIMEDOUT` - Timeout
  - `UPNP_E_SOCKET_ERROR` - Error on socket calls

Parameters

  [in] | info | Socket Information Object.
  [out] | buffer | Buffer to get data to or send data from.
  [in] | bufsize | Size of the buffer.
  [in] | timeoutSecs | timeout value.
  [in] | bRead | Boolean value specifying read or write option.

References SOCKINFO::socket, UPNP_E_SOCKET_ERROR, and UPNP_E_TIMEDOUT.

Referenced by sock_read(), and sock_write().

## sock_write()

int sock_write | ( | SOCKINFO * | info,
|  | const char * | buffer,
|  | size_t | bufsize,
|  | int * | timeoutSecs
| ) |  |

Writes data on the socket in sockinfo.

Returns
  Integer:
  - `numBytes` - On Success, no of bytes received.
  - `UPNP_E_TIMEDOUT` - Timeout.
  - `UPNP_E_SOCKET_ERROR` - Error on socket calls.

Parameters

  [in] | info | Socket Information Object.
  [in] | buffer | Buffer to send data from.
  [in] | bufsize | Size of the buffer.
  [in,out] | timeoutSecs | timeout value.

References sock_read_write().

Referenced by http_EndHttpRequest(), http_SendMessage(), and http_WriteHttpRequest().

## Variable Documentation

## foreign_sockaddr

struct sockaddr_storage SOCKINFO::foreign_sockaddr

The following two fields are filled only in incoming requests.

Referenced by gena_process_subscription_renewal_request(), gena_process_subscription_request(), gena_process_unsubscribe_request(), gena_validate_delivery_urls(), handle_invoke_action(), handle_query_variable(), process_request(), soap_device_callback(), and sock_init_with_ip().

## socket

SOCKET SOCKINFO::socket

Handle/descriptor to a socket.

Referenced by sock_destroy(), sock_init(), and sock_read_write().