dnxUdp.c File Reference

Implements the DNX UDP Transport Layer. More...

#include "dnxUdp.h"
#include "dnxTSPI.h"
#include "dnxTransport.h"
#include "dnxError.h"
#include "dnxDebug.h"
#include "dnxLogging.h"
#include "dnxProtocol.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>

Go to the source code of this file.

Data Structures

struct  iDnxUdpChannel_
 The implementation of the UDP low-level I/O transport. More...

Defines

#define HOST_NAME_MAX   256

Typedefs

typedef struct iDnxUdpChannel_ iDnxUdpChannel
 The implementation of the UDP low-level I/O transport.

Functions

static int dnxUdpOpen (iDnxChannel *icp, int mode)
 Open a UDP channel object.
static int dnxUdpClose (iDnxChannel *icp)
 Close a UDP channel object.
static int dnxUdpRead (iDnxChannel *icp, char *buf, int *size, int timeout, char *src)
 Read data from a UDP channel object.
static int dnxUdpWrite (iDnxChannel *icp, char *buf, int size, int timeout, char *dst)
 Write data to a UDP channel object.
static void dnxUdpDelete (iDnxChannel *icp)
 Delete a UDP channel object.
static void dnxUdpGetStats (iDnxChannel *icp, DnxTransStats *tsp)
 Return the read/write/error stats of this channel object.
static void dnxUdpResetStats (iDnxChannel *icp)
 Reset the read/write/error stats on this channel object.
static int dnxUdpNew (char *url, iDnxChannel **icpp)
 Create a new UDP transport.
int dnxUdpInit (int(**ptxAlloc)(char *url, iDnxChannel **icpp))
 Initialize the UDP transport sub-system; return UDP channel contructor.
void dnxUdpDeInit (void)
 Clean up global resources allocated by the UDP transport sub-system.

Variables

static pthread_mutex_t udpMutex


Detailed Description

Implements the DNX UDP Transport Layer.

Author:
Robert W. Ingraham (dnx-devel@lists.sourceforge.net)
Attention:
Please submit patches to http://dnx.sourceforge.net

Definition in file dnxUdp.c.


Define Documentation

#define HOST_NAME_MAX   256

Definition at line 55 of file dnxUdp.c.


Typedef Documentation

The implementation of the UDP low-level I/O transport.


Function Documentation

static int dnxUdpClose ( iDnxChannel icp  )  [static]

Close a UDP channel object.

Parameters:
[in] icp - the UDP channel object to be closed.
Returns:
Always returns zero.

Definition at line 202 of file dnxUdp.c.

References DNX_OK, and iDnxUdpChannel_::socket.

Referenced by dnxUdpNew().

void dnxUdpDeInit ( void   ) 

Clean up global resources allocated by the UDP transport sub-system.

Definition at line 537 of file dnxUdp.c.

References DNX_PT_MUTEX_DESTROY, and udpMutex.

static void dnxUdpDelete ( iDnxChannel icp  )  [static]

Delete a UDP channel object.

Parameters:
[in] icp - the UDP channel object to be deleted.

Definition at line 404 of file dnxUdp.c.

References iDnxUdpChannel_::host, iDnxUdpChannel_::socket, and xfree.

Referenced by dnxUdpNew().

static void dnxUdpGetStats ( iDnxChannel icp,
DnxTransStats tsp 
) [static]

Return the read/write/error stats of this channel object.

Parameters:
[in] icp - the channel for which stats should be returned.
[out] tsp - the stats structure to be filled.

Definition at line 422 of file dnxUdp.c.

References iDnxUdpChannel_::stats.

Referenced by dnxUdpNew().

int dnxUdpInit ( int(**)(char *url, iDnxChannel **icpp)  ptxAlloc  ) 

Initialize the UDP transport sub-system; return UDP channel contructor.

Parameters:
[out] ptxAlloc - the address of storage in which to return the address of the UDP channel object constructor (dnxUdpNew).
Returns:
Always returns zero.

Definition at line 524 of file dnxUdp.c.

References DNX_OK, DNX_PT_MUTEX_INIT, dnxUdpNew(), and udpMutex.

static int dnxUdpNew ( char *  url,
iDnxChannel **  icpp 
) [static]

Create a new UDP transport.

Parameters:
[in] url - the URL containing the host name and port number.
[out] icpp - the address of storage for returning the new low- level UDP transport object (as a generic transport object).
Returns:
Zero on success, or a non-zero error value.

Definition at line 458 of file dnxUdp.c.

References DNX_ERR_BADURL, DNX_ERR_MEMORY, DNX_OK, dnxUdpClose(), dnxUdpDelete(), dnxUdpGetStats(), dnxUdpOpen(), dnxUdpRead(), dnxUdpResetStats(), dnxUdpWrite(), iDnxUdpChannel_::host, HOST_NAME_MAX, iDnxUdpChannel_::ichan, iDnxUdpChannel_::port, iDnxChannel_::txClose, iDnxChannel_::txDelete, iDnxChannel_::txGetStats, iDnxChannel_::txOpen, iDnxChannel_::txRead, iDnxChannel_::txResetStats, iDnxChannel_::txWrite, xcalloc, xfree, and xmalloc.

Referenced by dnxUdpInit().

static int dnxUdpOpen ( iDnxChannel icp,
int  mode 
) [static]

Open a UDP channel object.

If DNX_MODE_ACTIVE is passed in mode, the channel is considered a client-side connection, and the address stored in icp is interpreted as a remote peer address to be connected to. Active mode connections must provide a resolvable internet address.

If DNX_MODE_PASSIVE is passed in mode, the channel is considered a server-side binding, and the address stored in icp is interpreted as a local address to be bound to. Passive mode connections may use the address of a specific interface on the local host, or the values "0", "0.0.0.0", or "INADDR_ANY" to specify that all interfaces should be bound on this connection (the actual local address of a received message is determined by which interface actually receives it.

Parameters:
[in] icp - the UDP channel object to be opened.
[in] mode - DNX_MODE_ACTIVE indicates the transport will be used in active mode (as a client); DNX_MODE_PASSIVE indicates the transport will be used in passive mode (as a server listen point).
Returns:
Zero on success, or a non-zero error value.

Definition at line 98 of file dnxUdp.c.

References DNX_ERR_ADDRESS, DNX_ERR_OPEN, DNX_MODE_ACTIVE, DNX_OK, DNX_PT_MUTEX_LOCK, DNX_PT_MUTEX_UNLOCK, dnxLog(), iDnxUdpChannel_::host, iDnxUdpChannel_::port, iDnxUdpChannel_::socket, and udpMutex.

Referenced by dnxUdpNew().

static int dnxUdpRead ( iDnxChannel icp,
char *  buf,
int *  size,
int  timeout,
char *  src 
) [static]

Read data from a UDP channel object.

Posix.1g specifies that a connected socket cannot receive the address of the remote peer, and that connected sockets should only use the recv call. Our architecture only gives us one way of determining if this socket is connected or not: If the src parameter is null, this socket is presumed to be an active, connected, or client-side socket being used to receive datagrams from a server in response to a prior request. If src is NOT null, this socket is assumed to be a server- side socket bound to a specific address, but unconnected. Thus it's important that higher layers pass appropriate values for src or the wrong functions will be called.

mlen can be zero - sending a zero-length packet is allowed, but unlikely mlen can't be greater than DNX_MAX_MSG however because we only allows *size bytes to be received, and the upper layers don't ask for larger than that.

Parameters:
[in] icp - the UDP channel object from which to read data.
[out] buf - the address of storage into which data should be read.
[in,out] size - on entry, the maximum number of bytes that may be read into buf; on exit, returns the number of bytes stored in buf.
[in] timeout - the maximum number of seconds we're willing to wait for data to become available on icp without returning a timeout error.
[out] src - the address of storage for the sender's address if desired. This parameter is optional, and may be passed as NULL. If non-NULL, the buffer pointed to by src must be at least the size of a sockaddr_in structure.
Returns:
Zero on success, or a non-zero error value.

Definition at line 248 of file dnxUdp.c.

References DNX_ERR_ADDRESS, DNX_ERR_RECEIVE, DNX_ERR_TIMEOUT, DNX_OK, dnxDebug(), dnxLog(), DnxTransStats_::rderrs, DnxTransStats_::reads, iDnxUdpChannel_::socket, and iDnxUdpChannel_::stats.

Referenced by dnxUdpNew().

static void dnxUdpResetStats ( iDnxChannel icp  )  [static]

Reset the read/write/error stats on this channel object.

Parameters:
[in] icp - the channel for which stats should be reset.

Definition at line 438 of file dnxUdp.c.

References iDnxUdpChannel_::stats.

Referenced by dnxUdpNew().

static int dnxUdpWrite ( iDnxChannel icp,
char *  buf,
int  size,
int  timeout,
char *  dst 
) [static]

Write data to a UDP channel object.

Posix.1g specifies that a connected socket cannot send to a specific address. The sendto method may be used on a connected socket, but the toaddr argument must be null and the toaddrsz argument is ignored. The effect of calling this method with a non-null dst argument on a connected socket is not clear on some platforms. Our architecture only allows us to determine if a socket was created as active or passive based on the value of the dst parameter. Thus, this function relies on higher layers properly calling this function with a null dst argument for active, connected, client-side sockets, and with a non-null dst argument for passive, bound, server-side sockets.

Since this module doesn't create non-blocking sockets, writes and reads will block until timeout occurs, or until all bytes are read or written. Thus, it's not possible to send more than zero, but less than requested.

Parameters:
[in] icp - the UDP channel object on which to write data.
[in] buf - a pointer to the data to be written.
[in] size - the number of bytes to be written on icp.
[in] timeout - the maximum number of seconds to wait for the write operation to complete without returning a timeout error.
[in] dst - the address to which the data in buf should be sent using this channel. This parameter is ignored for virtual connection based channels. This parameter is optional and may be passed as NULL.
Returns:
Zero on success, or a non-zero error value.
Note:
If this is a stream oriented channel, or if NULL is passed for the dst parameter, The channel destination address is used.

Definition at line 342 of file dnxUdp.c.

References DNX_ERR_SEND, DNX_ERR_TIMEOUT, DNX_OK, dnxDebug(), dnxLog(), iDnxUdpChannel_::socket, iDnxUdpChannel_::stats, DnxTransStats_::wrerrs, and DnxTransStats_::writes.

Referenced by dnxUdpNew().


Variable Documentation

pthread_mutex_t udpMutex [static]

Todo:
Use GNU reentrant resolver extensions on platforms where available.

Definition at line 70 of file dnxUdp.c.

Referenced by dnxUdpDeInit(), dnxUdpInit(), and dnxUdpOpen().


Generated on Tue Apr 13 15:48:08 2010 for DNX by  doxygen 1.5.6