librnr  1.14.5
RoadNarrows Robotics Common Library 1
sock.h File Reference

Socket services declarations. More...

#include <sys/types.h>
#include <sys/socket.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/simplebuf.h"

Go to the source code of this file.

Macros

#define USE_IPV6
 
#define SOCK_RC_OK   0
 no error
 
#define SOCK_RC_EFAIL   (-1)
 general, unspecified error
 
#define SOCK_RC_IODONE   (-2)
 finished current I/O
 
#define SOCK_RC_EBUFFULL   (-3)
 buffer is full
 
#define SOCK_RC_EBADSOCK   (-4)
 socket is bad
 
#define SOCK_RC_ENOBUF   (-5)
 no buffer
 
#define SOCK_RC_EBADSD   (-6)
 bad/closed socket descriptor
 
#define SOCK_RC_ESYSERR   (-7)
 system error occurred
 
#define SOCK_IO_READ   0
 read index
 
#define SOCK_IO_WRITE   1
 write index
 
#define SOCK_IO_NUMOF   2
 number of indices
 
#define SOCK_CHK_IO(io, ...)   CHKEXPR_INT(io, ((io) >= 0) && ((io) < SOCK_IO_NUMOF), __VA_ARGS__)
 Check I/O index. More...
 
#define SOCK_SD_CLOSED   (-1)
 closed socket descriptor value
 
#define SOCK_PORT_NONE   (-1)
 no port number specified
 

Typedefs

typedef struct socket_t Socket_T
 the Socket
 

Enumerations

enum  SockFlavor {
  SockFlavorUndef = 0x0000,
  SockFlavorDomainIPv4 = 0x0001,
  SockFlavorDomainIPv6 = 0x0002,
  SockFlavorDomainUnix = 0x0003,
  SockFlavorDomainMask = 0x0003,
  SockFlavorProtocolTcp = 0x0004,
  SockFlavorProtocolUdp = 0x0008,
  SockFlavorProtocolMask = 0x000C,
  SockFlavorFuncListener = 0x0010,
  SockFlavorFuncConnection = 0x0020,
  SockFlavorFuncMask = 0x0030
}
 < unit tests More...
 

Functions

char * SocketThisHostName ()
 Get the host string name of this network node (computer). More...
 
Socket_TSocketNew ()
 Allocate a new, unconnected socket. More...
 
void SocketDelete (Socket_T *pSocket)
 Delate an allocated socket. More...
 
SimpleBuf_TSocketBufNewBuf (Socket_T *pSocket, int eIOBuf, size_t nBufSize)
 Allocate a socket buffer and attach to Socket. More...
 
SimpleBuf_TSocketBufSetBuf (Socket_T *pSocket, int eIOBuf, byte_t *pBuf, size_t nBufSize)
 Attach a buffer to Socket. More...
 
SimpleBuf_TSocketBufGetBuf (Socket_T *pSocket, int eIOBuf)
 Get Socket buffer. More...
 
byte_tSocketBufGetRawBuf (Socket_T *pSocket, int eIOBuf)
 Get Socket raw buffer. More...
 
size_t SocketBufGetSize (Socket_T *pSocket, int eIOBuf)
 Get Socket buffer size. More...
 
size_t SocketBufGetLen (Socket_T *pSocket, int eIOBuf)
 Get number of bytes in Socket buffer. More...
 
void SocketBufClear (Socket_T *pSocket, int eIOBuf)
 Clear a Socket buffer. More...
 
void * SocketAttrGetClientData (Socket_T *pSocket)
 Get Socket client user data. More...
 
void SocketAttrSetClientData (Socket_T *pSocket, void *pClientData)
 Get Socket client user data. More...
 
const char * SocketAttrGetLocalName (Socket_T *pSocket)
 Get Socket local name. More...
 
void SocketAttrSetLocalName (Socket_T *pSocket, const char *sHostName)
 Set Socket local name. More...
 
const char * SocketAttrGetRemoteName (Socket_T *pSocket)
 Get Socket remote name. More...
 
void SocketAttrSetRemoteName (Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
 Set Socket remote name. More...
 
int SocketAttrGetSd (Socket_T *pSocket)
 Get Socket socket (file) descriptor. More...
 
void SocketAttrSetSd (Socket_T *pSocket, int sd)
 Set Socket socket (file) descriptor. More...
 
int SocketAttrGetPort (Socket_T *pSocket)
 Get Socket port number. More...
 
void SocketAttrSetPort (Socket_T *pSocket, int ipPort)
 Set Socket port number. More...
 
SockFlavor SocketAttrGetFlavor (Socket_T *pSocket)
 Get Socket flavor. More...
 
void SocketAttrSetFlavor (Socket_T *pSocket, SockFlavor eFlavor)
 Set Socket flavor. More...
 
bool_t SocketAttrHasFlavor (Socket_T *pSocket, int nMask, int nFlavor)
 Check if Socket has an assigned flavor. More...
 
INLINE_IN_H bool_t SocketAttrIsListener (Socket_T *pSocket)
 Test if socket is a listener flavor. More...
 
INLINE_IN_H bool_t SocketAttrIsConnection (Socket_T *pSocket)
 Test if socket is connection flavor. More...
 
INLINE_IN_H bool_t SocketAttrIsConnectionless (Socket_T *pSocket)
 Test if socket is connectionless flavor. More...
 
int SocketAttrSetNonBlocking (Socket_T *pSocket)
 Set Socket for non-blockiong I/O. More...
 
bool_t SocketStateIsOpen (Socket_T *pSocket)
 Check if Socket is open. More...
 
bool_t SocketStateIsErrored (Socket_T *pSocket)
 Check if Socket is in some kind of errored state. More...
 
Socket_TSocketOpenTcpListener (int ipPortListener, const char *sLocalHostName)
 Create and open a new TCP/IP passive listener socket on this host. More...
 
Socket_TSocketOpenTcpConnection (const char *sRemoteHostName, int ipPortRemote)
 Create and open a new TCP/IP socket to the given remote host and port. More...
 
Socket_TSocketOpenUdp (int ipPortUdp, const char *sLocalHostName)
 Create and open a new UDP/IP connectionless socket. port. More...
 
Socket_TSocketOpenUnix (int ipPortUdp, int nType)
 Create and open a new UNIX connectionless socket. More...
 
Socket_TSocketOpenUnixPair (int nType)
 Create and open a new UNIX pair socket. More...
 
Socket_TSocketAccept (Socket_T *pSocketListener, bool_t bNonBlock)
 Accept a client connection requst from the listener socket. More...
 
int SocketClose (Socket_T *pSocket)
 Close a Socket. More...
 
ssize_t SocketRead (Socket_T *pSocket)
 Reads the next available bytes from the Socket into in the Socket's read buffer. More...
 
ssize_t SocketWrite (Socket_T *pSocket)
 Writes the bytes from the Socket's write buffer to the Socket connection. More...
 
ssize_t SocketWriteAll (Socket_T *pSocket)
 Writes all of the bytes from the socket's write buffer until no more bytes or error. More...
 

Detailed Description

Socket services declarations.

These socket functions were inspired by the camserv socket.c functions written by Jon Travis (see below).

Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
rnr/sock.h
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
License
MIT
EULA
See the README and EULA files for any copyright and licensing information.


Original Source Comment Block
Original Author
Jon Travis (jtrav.nosp@m.is@p.nosp@m.00p.o.nosp@m.rg)
Original Copyright
N/A
Original Header
N/A

Definition in file sock.h.

Macro Definition Documentation

#define SOCK_CHK_IO (   io,
  ... 
)    CHKEXPR_INT(io, ((io) >= 0) && ((io) < SOCK_IO_NUMOF), __VA_ARGS__)

Check I/O index.

Parameters
ioI/O buffer index

Definition at line 138 of file sock.h.

Referenced by SocketBufClear(), SocketBufGetBuf(), SocketBufGetLen(), SocketBufGetRawBuf(), SocketBufGetSize(), SocketBufNewBuf(), SocketBufSetBuf(), and SockSetIterFirst().

#define USE_IPV6

Allow IPV6

Definition at line 49 of file sock.h.

Enumeration Type Documentation

enum SockFlavor

< unit tests

< general debugging

Socket Flavors (a distillation of the various POSIX domains, families, protocals, and types).

Enumerator
SockFlavorUndef 

undefined

SockFlavorDomainIPv4 

IPv4.

SockFlavorDomainIPv6 

IPv6.

SockFlavorDomainUnix 

UNIX (local)

SockFlavorDomainMask 

mask

SockFlavorProtocolTcp 

TCP.

SockFlavorProtocolUdp 

UDP (DGRAM)

SockFlavorProtocolMask 

mask

SockFlavorFuncListener 

listener socket

SockFlavorFuncConnection 

connection/connectionless

SockFlavorFuncMask 

mask

Definition at line 88 of file sock.h.

89 {
90  SockFlavorUndef = 0x0000, ///< undefined
91 
92  // Domain (Application Family)
93  SockFlavorDomainIPv4 = 0x0001, ///< IPv4
94  SockFlavorDomainIPv6 = 0x0002, ///< IPv6
95  SockFlavorDomainUnix = 0x0003, ///< UNIX (local)
96  SockFlavorDomainMask = 0x0003, ///< mask
97 
98  // Protocol
99  SockFlavorProtocolTcp = 0x0004, ///< TCP
100  SockFlavorProtocolUdp = 0x0008, ///< UDP (DGRAM)
101  SockFlavorProtocolMask = 0x000C, ///< mask
102 
103  // Function
104  SockFlavorFuncListener = 0x0010, ///< listener socket
105  SockFlavorFuncConnection = 0x0020, ///< connection/connectionless
106  SockFlavorFuncMask = 0x0030 ///< mask
107 
108 } SockFlavor;
connection/connectionless
Definition: sock.h:105
SockFlavor
< unit tests
Definition: sock.h:88
listener socket
Definition: sock.h:104
UDP (DGRAM)
Definition: sock.h:100
UNIX (local)
Definition: sock.h:95
undefined
Definition: sock.h:90

Function Documentation

Socket_T* SocketAccept ( Socket_T pSocketListener,
bool_t  bNonBlock 
)

Accept a client connection requst from the listener socket.

The new socket will be set to non-blocking I/O mode if requested.

Parameters
pSocketListenerPointer to open listener socket.
bNonBlockDo [not] set socket to non-blocking.

Return Value: Newly allocated and accepted open Socket_T pointer on success, NULL on failure.

Definition at line 1240 of file sock.c.

References LOGDIAG4, LOGDIAG4CALL, LOGERROR, LOGSYSERROR, socket_t::m_sd, socket_t::m_sLocalName, NULL, SocketAttrGetRemoteName(), SocketAttrSetFlavor(), SocketAttrSetLocalName(), SocketAttrSetNonBlocking(), SocketAttrSetPort(), SocketAttrSetRemoteName(), SocketAttrSetSd(), SocketClose(), SocketDelete(), SocketNew(), SockFlavorDomainIPv4, SockFlavorFuncConnection, and SockFlavorProtocolTcp.

Referenced by SocketAttrIsConnectionless().

1241 {
1242  int sd;
1243  struct sockaddr saddr;
1244  unsigned int addrlen = sizeof(saddr);
1245  Socket_T *pSocket;
1246 
1247  LOGDIAG4CALL("pSocketListener->%s", pSocketListener->m_sLocalName);
1248 
1249  // accept incoming connection
1250  if((sd = accept(pSocketListener->m_sd, &saddr, (socklen_t *)&addrlen)) == -1)
1251  {
1252  LOGSYSERROR("accept(%d,...)", pSocketListener->m_sd);
1253  return NULL;
1254  }
1255 
1256  //
1257  // Now create a new socket and set the appropriate attributes
1258  //
1259  pSocket = SocketNew();
1260 
1261  SocketAttrSetSd(pSocket, sd);
1262  SocketAttrSetPort(pSocket, ((struct sockaddr_in*)&saddr)->sin_port);
1263  SocketAttrSetFlavor(pSocket,
1265  SocketAttrSetLocalName(pSocket, NULL);
1266  SocketAttrSetRemoteName(pSocket, &saddr, NULL);
1267 
1268  // set non-blocking
1269  if( bNonBlock )
1270  {
1271  if( SocketAttrSetNonBlocking(pSocket) == -1 )
1272  {
1273  LOGERROR("SockAttrSetNonBlocking() failed");
1274  SocketClose(pSocket);
1275  SocketDelete(pSocket);
1276  return NULL;
1277  }
1278  }
1279 
1280  LOGDIAG4("Accepted connection from %s", SocketAttrGetRemoteName(pSocket));
1281 
1282  return pSocket;
1283 }
Socket_T * SocketNew()
Allocate a new, unconnected socket.
Definition: sock.c:433
const char * SocketAttrGetRemoteName(Socket_T *pSocket)
Get Socket remote name.
Definition: sock.c:682
void SocketDelete(Socket_T *pSocket)
Delate an allocated socket.
Definition: sock.c:457
connection/connectionless
Definition: sock.h:105
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
#define NULL
null pointer
Definition: rnrconfig.h:199
int SocketAttrSetNonBlocking(Socket_T *pSocket)
Set Socket for non-blockiong I/O.
Definition: sock.c:837
void SocketAttrSetSd(Socket_T *pSocket, int sd)
Set Socket socket (file) descriptor.
Definition: sock.c:747
void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor)
Set Socket flavor.
Definition: sock.c:800
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
char * m_sLocalName
local name
Definition: sock.c:141
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
void SocketAttrSetPort(Socket_T *pSocket, int ipPort)
Set Socket port number.
Definition: sock.c:775
Definition: sock.c:136
int SocketClose(Socket_T *pSocket)
Close a Socket.
Definition: sock.c:1292
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
int m_sd
(open) socket descriptor
Definition: sock.c:138
void* SocketAttrGetClientData ( Socket_T pSocket)

Get Socket client user data.

Parameters
pSocketPointer to Socket.
Returns
Returns pointer to user defined data.

Definition at line 607 of file sock.c.

References CHKPTR, socket_t::m_pClientData, and NULL.

608 {
609  CHKPTR(pSocket, NULL);
610  return pSocket->m_pClientData;
611 }
void * m_pClientData
client data
Definition: sock.c:144
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
SockFlavor SocketAttrGetFlavor ( Socket_T pSocket)

Get Socket flavor.

Parameters
pSocketPointer to Socket.
Returns
Returns flavor enumeration.

Definition at line 788 of file sock.c.

References CHKPTR, socket_t::m_eFlavor, and SockFlavorUndef.

789 {
790  CHKPTR(pSocket, SockFlavorUndef);
791  return pSocket->m_eFlavor;
792 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
SockFlavor m_eFlavor
socket type
Definition: sock.c:140
undefined
Definition: sock.h:90
const char* SocketAttrGetLocalName ( Socket_T pSocket)

Get Socket local name.

Parameters
pSocketPointer to Socket.
Returns
Returns string.

Definition at line 632 of file sock.c.

References CHKPTR, socket_t::m_sLocalName, and NULL.

Referenced by SocketClose(), SocketOpenTcpListener(), SocketOpenUdp(), SocketWriteAll(), SockSetAdd(), SockSetChkSd(), and SockSetRemove().

633 {
634  CHKPTR(pSocket, NULL);
635  return pSocket->m_sLocalName;
636 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
char * m_sLocalName
local name
Definition: sock.c:141
int SocketAttrGetPort ( Socket_T pSocket)

Get Socket port number.

Parameters
pSocketPointer to Socket.
Returns
Returns port number.

Definition at line 763 of file sock.c.

References CHKPTR, socket_t::m_ipPort, and SOCK_PORT_NONE.

764 {
765  CHKPTR(pSocket, SOCK_PORT_NONE);
766  return pSocket->m_ipPort;
767 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define SOCK_PORT_NONE
no port number specified
Definition: sock.h:145
int m_ipPort
socket port
Definition: sock.c:139
const char* SocketAttrGetRemoteName ( Socket_T pSocket)

Get Socket remote name.

Parameters
pSocketPointer to Socket.
Returns
Returns string.

Definition at line 682 of file sock.c.

References CHKPTR, socket_t::m_sRemoteName, and NULL.

Referenced by SocketAccept(), SocketClose(), SocketOpenTcpConnection(), and SocketWriteAll().

683 {
684  CHKPTR(pSocket, NULL);
685  return pSocket->m_sRemoteName;
686 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
char * m_sRemoteName
remote name
Definition: sock.c:142
int SocketAttrGetSd ( Socket_T pSocket)

Get Socket socket (file) descriptor.

Parameters
pSocketPointer to Socket.
Returns
Returns socket descriptor.

Definition at line 735 of file sock.c.

References CHKPTR, socket_t::m_sd, and SOCK_SD_CLOSED.

Referenced by SockSetChkSd().

736 {
737  CHKPTR(pSocket, SOCK_SD_CLOSED);
738  return pSocket->m_sd;
739 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define SOCK_SD_CLOSED
closed socket descriptor value
Definition: sock.h:144
int m_sd
(open) socket descriptor
Definition: sock.c:138
bool_t SocketAttrHasFlavor ( Socket_T pSocket,
int  nMask,
int  nFlavor 
)

Check if Socket has an assigned flavor.

Parameters
pSocketPointer to Socket.
nMaskFlavor mask of relevant bits.
nFlavorFlavor bits.
Returns
Returns true or false.

Definition at line 815 of file sock.c.

References CHKPTR, socket_t::m_eFlavor, socket_t::m_sd, and SOCK_SD_CLOSED.

Referenced by SocketAttrIsConnection(), and SocketAttrIsListener().

816 {
817  CHKPTR(pSocket, false);
818 
819  // flavors only apply to open sockets
820  if( pSocket->m_sd == SOCK_SD_CLOSED )
821  {
822  return false;
823  }
824  else
825  {
826  return ((int)(pSocket->m_eFlavor) & nMask) == nFlavor? true: false;
827  }
828 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
SockFlavor m_eFlavor
socket type
Definition: sock.c:140
#define SOCK_SD_CLOSED
closed socket descriptor value
Definition: sock.h:144
int m_sd
(open) socket descriptor
Definition: sock.c:138
INLINE_IN_H bool_t SocketAttrIsConnection ( Socket_T pSocket)

Test if socket is connection flavor.

Parameters
pSocketPointer to socket.
Returns
Returns true or false.

Definition at line 252 of file sock.h.

References SocketAttrHasFlavor(), SockFlavorFuncConnection, and SockFlavorFuncMask.

Referenced by SocketAttrIsConnectionless(), and SocketClose().

253 {
254  return SocketAttrHasFlavor(pSocket,
257 }
connection/connectionless
Definition: sock.h:105
bool_t SocketAttrHasFlavor(Socket_T *pSocket, int nMask, int nFlavor)
Check if Socket has an assigned flavor.
Definition: sock.c:815
INLINE_IN_H bool_t SocketAttrIsConnectionless ( Socket_T pSocket)

Test if socket is connectionless flavor.

Parameters
pSocketPointer to socket.
Returns
Returns true or false.

Definition at line 266 of file sock.h.

References C_DECLS_END, SocketAccept(), SocketAttrIsConnection(), SocketAttrSetNonBlocking(), SocketClose(), SocketOpenTcpConnection(), SocketOpenTcpListener(), SocketOpenUdp(), SocketOpenUnix(), SocketOpenUnixPair(), SocketRead(), SocketStateIsErrored(), SocketStateIsOpen(), SocketWrite(), and SocketWriteAll().

267 {
268  return SocketAttrIsConnection(pSocket)? false: true;
269 }
INLINE_IN_H bool_t SocketAttrIsConnection(Socket_T *pSocket)
Test if socket is connection flavor.
Definition: sock.h:252
INLINE_IN_H bool_t SocketAttrIsListener ( Socket_T pSocket)

Test if socket is a listener flavor.

Parameters
pSocketPointer to socket.
Returns
Returns true or false.

Definition at line 238 of file sock.h.

References SocketAttrHasFlavor(), SockFlavorFuncListener, and SockFlavorFuncMask.

239 {
240  return SocketAttrHasFlavor(pSocket,
243 }
listener socket
Definition: sock.h:104
bool_t SocketAttrHasFlavor(Socket_T *pSocket, int nMask, int nFlavor)
Check if Socket has an assigned flavor.
Definition: sock.c:815
void SocketAttrSetClientData ( Socket_T pSocket,
void *  pClientData 
)

Get Socket client user data.

Parameters
pSocketPointer to Socket.
pClientDataUser defined data.

Definition at line 619 of file sock.c.

References CHKPTR, and socket_t::m_pClientData.

620 {
621  CHKPTR(pSocket);
622  pSocket->m_pClientData = pClientData;
623 }
void * m_pClientData
client data
Definition: sock.c:144
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
void SocketAttrSetFlavor ( Socket_T pSocket,
SockFlavor  eFlavor 
)

Set Socket flavor.

Parameters
pSocketPointer to Socket.
eFlavorFlavor enumeration.

Definition at line 800 of file sock.c.

References CHKPTR, and socket_t::m_eFlavor.

Referenced by SocketAccept(), SocketClose(), SocketOpenTcpConnection(), SocketOpenTcpListener(), and SocketOpenUdp().

801 {
802  CHKPTR(pSocket);
803  pSocket->m_eFlavor = eFlavor;
804 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
SockFlavor m_eFlavor
socket type
Definition: sock.c:140
void SocketAttrSetLocalName ( Socket_T pSocket,
const char *  sHostName 
)

Set Socket local name.

Parameters
pSocketPointer to Socket.
sHostNameLocal host name string.

Definition at line 644 of file sock.c.

References CHKPTR, LOGSYSERROR, socket_t::m_sd, socket_t::m_sLocalName, new_strdup(), SocketAddrName(), SocketStateIsOpen(), and SockStrClosed.

Referenced by SocketAccept(), SocketClose(), SocketNew(), SocketOpenTcpConnection(), SocketOpenTcpListener(), and SocketOpenUdp().

645 {
646  struct sockaddr_un addr; // largest address size (is there a better way?)
647  socklen_t sizAddr;
648 
649  CHKPTR(pSocket);
650 
651  delete(pSocket->m_sLocalName);
652 
653  sizAddr = (socklen_t)sizeof(addr);
654 
655  // closed
656  if( !SocketStateIsOpen(pSocket) )
657  {
659  }
660 
661  // error leg - simply use host name
662  else if( getsockname(pSocket->m_sd, (struct sockaddr *)&addr, &sizAddr) < 0 )
663  {
664  LOGSYSERROR("getsockname(%d, ...)", pSocket->m_sd);
665  pSocket->m_sLocalName = new_strdup(sHostName);
666  }
667 
668  // get local address
669  else
670  {
671  pSocket->m_sLocalName = SocketAddrName((struct sockaddr *)&addr, sHostName);
672  }
673 }
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
const char * SockStrClosed
closed socket
Definition: sock.c:127
char * m_sLocalName
local name
Definition: sock.c:141
bool_t SocketStateIsOpen(Socket_T *pSocket)
Check if Socket is open.
Definition: sock.c:888
static char * SocketAddrName(struct sockaddr *pAddr, const char *sHostName)
Allocate formated address name.
Definition: sock.c:347
int m_sd
(open) socket descriptor
Definition: sock.c:138
int SocketAttrSetNonBlocking ( Socket_T pSocket)

Set Socket for non-blockiong I/O.

Parameters
pSocketPointer to Socket.
Returns
Returns 0 on success, < 0 on failure.

Definition at line 837 of file sock.c.

References CHKPTR, LOGDIAG3, LOGSYSERROR, socket_t::m_sd, and OK.

Referenced by SocketAccept(), and SocketAttrIsConnectionless().

838 {
839  CHKPTR(pSocket, -1);
840 
841  if( fcntl(pSocket->m_sd, F_SETFL, O_NONBLOCK) < 0 )
842  {
843  LOGSYSERROR("fcntl(%d,F_SETFL,O_NONBLOCK)", pSocket->m_sd);
844  return -1;
845  }
846  return OK;
847 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define OK
Okay.
Definition: rnrconfig.h:301
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
int m_sd
(open) socket descriptor
Definition: sock.c:138
void SocketAttrSetPort ( Socket_T pSocket,
int  ipPort 
)

Set Socket port number.

Parameters
pSocketPointer to Socket.
ipPortPort number.

Definition at line 775 of file sock.c.

References CHKPTR, and socket_t::m_ipPort.

Referenced by SocketAccept().

776 {
777  CHKPTR(pSocket);
778  pSocket->m_ipPort = ipPort;
779 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
int m_ipPort
socket port
Definition: sock.c:139
void SocketAttrSetRemoteName ( Socket_T pSocket,
struct sockaddr *  pAddr,
const char *  sHostName 
)

Set Socket remote name.

Parameters
pSocketPointer to Socket.
pAddrSocket address.
sHostNameRemote host name string.

Definition at line 695 of file sock.c.

References CHKPTR, socket_t::m_sRemoteName, new_strdup(), NULL, SocketAddrName(), SocketStateIsOpen(), and SockStrDisconnected.

Referenced by SocketAccept(), SocketClose(), SocketNew(), SocketOpenTcpConnection(), SocketOpenTcpListener(), and SocketOpenUdp().

698 {
699  CHKPTR(pSocket);
700 
701  delete(pSocket->m_sRemoteName);
702 
703  // closed
704  if( !SocketStateIsOpen(pSocket) )
705  {
707  }
708 
709  // set preferred address named
710  else if( pAddr != NULL )
711  {
712  pSocket->m_sRemoteName = SocketAddrName(pAddr, sHostName);
713  }
714 
715  // simply use host name
716  else if( sHostName != NULL )
717  {
718  pSocket->m_sRemoteName = new_strdup(sHostName);
719  }
720 
721  // error
722  else
723  {
725  }
726 }
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
bool_t SocketStateIsOpen(Socket_T *pSocket)
Check if Socket is open.
Definition: sock.c:888
static char * SocketAddrName(struct sockaddr *pAddr, const char *sHostName)
Allocate formated address name.
Definition: sock.c:347
const char * SockStrDisconnected
disconnected
Definition: sock.c:128
char * m_sRemoteName
remote name
Definition: sock.c:142
void SocketAttrSetSd ( Socket_T pSocket,
int  sd 
)

Set Socket socket (file) descriptor.

Parameters
pSocketPointer to Socket.
sdSocket descriptor.

Definition at line 747 of file sock.c.

References CHKPTR, socket_t::m_sd, and SocketClose().

Referenced by SocketAccept(), SocketClose(), SocketOpenTcpConnection(), SocketOpenTcpListener(), and SocketOpenUdp().

748 {
749  CHKPTR(pSocket);
750  if( (pSocket->m_sd = sd) < 0 )
751  {
752  SocketClose(pSocket);
753  }
754 }
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
int SocketClose(Socket_T *pSocket)
Close a Socket.
Definition: sock.c:1292
int m_sd
(open) socket descriptor
Definition: sock.c:138
void SocketBufClear ( Socket_T pSocket,
int  eIOBuf 
)

Clear a Socket buffer.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.

Definition at line 587 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, SimpleBufClear(), and SOCK_CHK_IO.

588 {
589  CHKPTR(pSocket);
590  SOCK_CHK_IO(eIOBuf);
591 
592  SimpleBufClear(pSocket->m_pIOBuf[eIOBuf]);
593 }
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
static void SimpleBufClear(SimpleBuf_T *pBuf)
Clear contents of buffer.
Definition: simplebuf.h:89
SimpleBuf_T* SocketBufGetBuf ( Socket_T pSocket,
int  eIOBuf 
)

Get Socket buffer.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.
Returns
Returns pointer to simplebuffer.

Definition at line 525 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, NULL, and SOCK_CHK_IO.

526 {
527  CHKPTR(pSocket, NULL);
528  SOCK_CHK_IO(eIOBuf, NULL);
529 
530  return pSocket->m_pIOBuf[eIOBuf];
531 }
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
size_t SocketBufGetLen ( Socket_T pSocket,
int  eIOBuf 
)

Get number of bytes in Socket buffer.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.
Returns
Returns number of bytes currently in buffer.

Definition at line 573 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, SimpleBufHasLen(), and SOCK_CHK_IO.

574 {
575  CHKPTR(pSocket, (size_t)0);
576  SOCK_CHK_IO(eIOBuf, (size_t)0);
577 
578  return SimpleBufHasLen(pSocket->m_pIOBuf[eIOBuf]);
579 }
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
static size_t SimpleBufHasLen(SimpleBuf_T *pBuf)
Returns number of bytes currently in buffer.
Definition: simplebuf.h:178
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
byte_t* SocketBufGetRawBuf ( Socket_T pSocket,
int  eIOBuf 
)

Get Socket raw buffer.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.
Returns
Returns pointer to byte buffer.

Definition at line 541 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, SimpleBuf_T::m_pRWBuf, NULL, and SOCK_CHK_IO.

542 {
543  CHKPTR(pSocket, NULL);
544  SOCK_CHK_IO(eIOBuf, NULL);
545 
546  return pSocket->m_pIOBuf[eIOBuf]->m_pRWBuf;
547 }
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
size_t SocketBufGetSize ( Socket_T pSocket,
int  eIOBuf 
)

Get Socket buffer size.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.
Returns
Returns size of buffer in bytes.

Definition at line 557 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, SimpleBufHasSize(), and SOCK_CHK_IO.

558 {
559  CHKPTR(pSocket, (size_t)0);
560  SOCK_CHK_IO(eIOBuf, (size_t)0);
561 
562  return SimpleBufHasSize(pSocket->m_pIOBuf[eIOBuf]);
563 }
static size_t SimpleBufHasSize(SimpleBuf_T *pBuf)
Returns size of buffer.
Definition: simplebuf.h:142
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
SimpleBuf_T* SocketBufNewBuf ( Socket_T pSocket,
int  eIOBuf,
size_t  nBufSize 
)

Allocate a socket buffer and attach to Socket.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.
nBufSizeBuffer size (bytes).
Returns
Returns pointer to newly allocated buffer.

Definition at line 485 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, NULL, SimpleBufDelete(), SimpleBufNewWithBuf(), and SOCK_CHK_IO.

486 {
487  CHKPTR(pSocket, NULL);
488  SOCK_CHK_IO(eIOBuf, NULL);
489 
490  SimpleBufDelete(pSocket->m_pIOBuf[eIOBuf]);
491  pSocket->m_pIOBuf[eIOBuf] = SimpleBufNewWithBuf(nBufSize);
492  return pSocket->m_pIOBuf[eIOBuf];
493 }
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
SimpleBuf_T * SimpleBufNewWithBuf(size_t nBufSize)
Allocate a new simple buffer with internal buffer of nBufSize.
Definition: simplebuf.c:91
void SimpleBufDelete(SimpleBuf_T *pBuf)
Delete a simple buffer along with the internal buffer.
Definition: simplebuf.c:111
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
SimpleBuf_T* SocketBufSetBuf ( Socket_T pSocket,
int  eIOBuf,
byte_t pBuf,
size_t  nBufSize 
)

Attach a buffer to Socket.

Parameters
pSocketPointer to Socket.
eIOBufOne of: SOCK_IO_READ SOCK_IO_WRITE.
pBufPointer to buffer.
nBufSizeBuffer size (bytes).
Returns
Returns pointer to buffer.

Definition at line 505 of file sock.c.

References CHKPTR, socket_t::m_pIOBuf, NULL, SimpleBufSetBuf(), and SOCK_CHK_IO.

509 {
510  CHKPTR(pSocket, NULL);
511  SOCK_CHK_IO(eIOBuf, NULL);
512 
513  SimpleBufSetBuf(pSocket->m_pIOBuf[eIOBuf], pBuf, nBufSize, (size_t)0);
514  return pSocket->m_pIOBuf[eIOBuf];
515 }
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
void SimpleBufSetBuf(SimpleBuf_T *pBuf, byte_t *pRWBuf, size_t nBufSize, size_t nLen)
Set simple buffer&#39;s internal read/write buffer.
Definition: simplebuf.c:134
#define SOCK_CHK_IO(io,...)
Check I/O index.
Definition: sock.h:138
int SocketClose ( Socket_T pSocket)

Close a Socket.

Parameters
pSocketPointer to Socket.
Returns
Returns 0 on success, < 0 on failure.

Definition at line 1292 of file sock.c.

References CHKPTR, LOGDIAG4, LOGSYSERROR, socket_t::m_sd, NULL, SOCK_SD_CLOSED, SocketAttrGetLocalName(), SocketAttrGetRemoteName(), SocketAttrIsConnection(), SocketAttrSetFlavor(), SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SocketAttrSetSd(), SockFlavorUndef, SockStrClosed, and SockStrDisconnected.

Referenced by SocketAccept(), SocketAttrIsConnectionless(), SocketAttrSetSd(), and SocketWriteAll().

1293 {
1294  CHKPTR(pSocket, -1);
1295 
1296  if( pSocket->m_sd != SOCK_SD_CLOSED )
1297  {
1298  shutdown(pSocket->m_sd, SHUT_RDWR);
1299 
1300  if( close(pSocket->m_sd) < 0 )
1301  {
1302  LOGSYSERROR("close(%d)", pSocket->m_sd);
1303  }
1304 
1305  if( SocketAttrIsConnection(pSocket) )
1306  {
1307  LOGDIAG4("Closed Connection %s", SocketAttrGetRemoteName(pSocket));
1308  }
1309  else
1310  {
1311  LOGDIAG4("Closed Socket %s", SocketAttrGetLocalName(pSocket));
1312  }
1313 
1314  SocketAttrSetSd(pSocket, SOCK_SD_CLOSED);
1318  }
1319 
1320  return 0;
1321 }
const char * SocketAttrGetRemoteName(Socket_T *pSocket)
Get Socket remote name.
Definition: sock.c:682
INLINE_IN_H bool_t SocketAttrIsConnection(Socket_T *pSocket)
Test if socket is connection flavor.
Definition: sock.h:252
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
#define NULL
null pointer
Definition: rnrconfig.h:199
void SocketAttrSetSd(Socket_T *pSocket, int sd)
Set Socket socket (file) descriptor.
Definition: sock.c:747
void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor)
Set Socket flavor.
Definition: sock.c:800
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
const char * SocketAttrGetLocalName(Socket_T *pSocket)
Get Socket local name.
Definition: sock.c:632
const char * SockStrClosed
closed socket
Definition: sock.c:127
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
#define SOCK_SD_CLOSED
closed socket descriptor value
Definition: sock.h:144
const char * SockStrDisconnected
disconnected
Definition: sock.c:128
int m_sd
(open) socket descriptor
Definition: sock.c:138
undefined
Definition: sock.h:90
void SocketDelete ( Socket_T pSocket)

Delate an allocated socket.

Warning
Socket should be closed.

Definition at line 457 of file sock.c.

References socket_t::m_pIOBuf, socket_t::m_sLocalName, socket_t::m_sRemoteName, NULL, SimpleBufDelete(), SOCK_IO_READ, and SOCK_IO_WRITE.

Referenced by SocketAccept(), and SocketWriteAll().

458 {
459  if( pSocket == NULL )
460  {
461  return;
462  }
463 
464  delete(pSocket->m_sLocalName);
465  delete(pSocket->m_sRemoteName);
468  delete(pSocket);
469 }
#define SOCK_IO_READ
read index
Definition: sock.h:130
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define NULL
null pointer
Definition: rnrconfig.h:199
void SimpleBufDelete(SimpleBuf_T *pBuf)
Delete a simple buffer along with the internal buffer.
Definition: simplebuf.c:111
char * m_sLocalName
local name
Definition: sock.c:141
#define SOCK_IO_WRITE
write index
Definition: sock.h:131
char * m_sRemoteName
remote name
Definition: sock.c:142
Socket_T* SocketNew ( )

Allocate a new, unconnected socket.

Returns
Returns pointer to allocated Socket_T *.

Definition at line 433 of file sock.c.

References socket_t::m_eFlavor, socket_t::m_ipPort, socket_t::m_pClientData, socket_t::m_pIOBuf, socket_t::m_sd, NEW, NULL, SimpleBufNew(), SOCK_IO_READ, SOCK_IO_WRITE, SOCK_PORT_NONE, SOCK_SD_CLOSED, SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SockFlavorUndef, SockStrClosed, and SockStrDisconnected.

Referenced by SocketAccept(), SocketOpenTcpConnection(), SocketOpenTcpListener(), and SocketOpenUdp().

434 {
435  Socket_T *pSocket = NEW(Socket_T);
436 
437  pSocket->m_sd = SOCK_SD_CLOSED;
438  pSocket->m_ipPort = SOCK_PORT_NONE;
439  pSocket->m_eFlavor = SockFlavorUndef;
440 
443 
444  pSocket->m_pIOBuf[SOCK_IO_READ] = SimpleBufNew();
445  pSocket->m_pIOBuf[SOCK_IO_WRITE] = SimpleBufNew();
446 
447  pSocket->m_pClientData = NULL;
448 
449  return pSocket;
450 }
void * m_pClientData
client data
Definition: sock.c:144
SimpleBuf_T * SimpleBufNew()
Allocate a new simple buffer with no internal buffer allocation.
Definition: simplebuf.c:70
#define SOCK_IO_READ
read index
Definition: sock.h:130
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
#define NULL
null pointer
Definition: rnrconfig.h:199
#define SOCK_PORT_NONE
no port number specified
Definition: sock.h:145
const char * SockStrClosed
closed socket
Definition: sock.c:127
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
#define NEW(T)
Allocate new type.
Definition: new.h:49
SockFlavor m_eFlavor
socket type
Definition: sock.c:140
Definition: sock.c:136
#define SOCK_SD_CLOSED
closed socket descriptor value
Definition: sock.h:144
const char * SockStrDisconnected
disconnected
Definition: sock.c:128
int m_sd
(open) socket descriptor
Definition: sock.c:138
int m_ipPort
socket port
Definition: sock.c:139
undefined
Definition: sock.h:90
#define SOCK_IO_WRITE
write index
Definition: sock.h:131
Socket_T* SocketOpenTcpConnection ( const char *  sRemoteHostName,
int  ipPortRemote 
)

Create and open a new TCP/IP socket to the given remote host and port.

Parameters
sRemoteHostNameDotted address or network name.
ipPortRemoteRemote port number.
Returns
Returns a new TCP Socket_T pointer on success, NULL on failure.

Definition at line 1059 of file sock.c.

References htons_nowarn(), LOGDIAG4, LOGERROR, LOGSYSERROR, NULL, SocketAttrGetRemoteName(), SocketAttrSetFlavor(), SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SocketAttrSetSd(), SocketNew(), SockFlavorDomainIPv4, SockFlavorFuncConnection, SockFlavorProtocolTcp, and SockResolveHostName().

Referenced by SocketAttrIsConnectionless(), and SocketWriteAll().

1060 {
1061  int sd;
1062  struct sockaddr_in addrInet;
1063  Socket_T *pSocket;
1064 
1065  //
1066  // Create socket in protocal familiy INET (IPv4), of type STREAM (TCP).
1067  //
1068  if( (sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
1069  {
1070  LOGSYSERROR("socket(%d,%d,%d)", PF_INET, SOCK_STREAM, IPPROTO_TCP);
1071  return NULL;
1072  }
1073 
1074  //
1075  // Set IPv4 address of remote
1076  //
1077  addrInet.sin_family = AF_INET; // ip application family
1078  addrInet.sin_port = htons_nowarn((ushort_t)ipPortRemote);
1079  // remote port number
1080 
1081  if( SockResolveHostName(sRemoteHostName, &(addrInet.sin_addr)) < 0 )
1082  {
1083  LOGERROR("SockResolveHostName() failed");
1084  close(sd);
1085  return NULL;
1086  }
1087 
1088  //
1089  // Connect to remote end
1090  //
1091  if(connect(sd, (struct sockaddr *)&addrInet, (socklen_t)sizeof(addrInet)) < 0)
1092  {
1093  LOGSYSERROR("connect(%d, ...)", sd);
1094  close(sd);
1095  return NULL;
1096  }
1097 
1098  //
1099  // Now create a new socket and set the appropriate attributes
1100  //
1101  pSocket = SocketNew();
1102 
1103  SocketAttrSetSd(pSocket, sd);
1104  SocketAttrSetFlavor(pSocket,
1106  SocketAttrSetLocalName(pSocket, NULL);
1107  SocketAttrSetRemoteName(pSocket, (struct sockaddr *)&addrInet,
1108  sRemoteHostName);
1109 
1110  LOGDIAG4("Opened Connection %s", SocketAttrGetRemoteName(pSocket));
1111 
1112  return pSocket;
1113 }
Socket_T * SocketNew()
Allocate a new, unconnected socket.
Definition: sock.c:433
const char * SocketAttrGetRemoteName(Socket_T *pSocket)
Get Socket remote name.
Definition: sock.c:682
connection/connectionless
Definition: sock.h:105
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
#define NULL
null pointer
Definition: rnrconfig.h:199
void SocketAttrSetSd(Socket_T *pSocket, int sd)
Set Socket socket (file) descriptor.
Definition: sock.c:747
void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor)
Set Socket flavor.
Definition: sock.c:800
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
Definition: sock.c:136
u16_t ushort_t
16-bit unsigned integer
Definition: rnrconfig.h:179
static ushort_t htons_nowarn(ushort_t huShort)
htons() wrapper with no annoying warnings.
Definition: sock.c:117
static int SockResolveHostName(const char *sHostName, struct in_addr *pAddrInet)
Resolve host name into IP address (IPV4 only).
Definition: sock.c:155
Socket_T* SocketOpenTcpListener ( int  ipPortListener,
const char *  sLocalHostName 
)

Create and open a new TCP/IP passive listener socket on this host.

Parameters
ipPortListenerPort number to listen on.
sLocalHostNameString name of local host to bind to. If NULL, then this host will automatically choose the IP address.
Returns
: Returns a new listener Socket_T pointer on success, NULL on failure.

Definition at line 942 of file sock.c.

References _TINT, _TSTR, htons_nowarn(), LOGDIAG4, LOGDIAG4CALL, LOGERROR, LOGSYSERROR, NULL, SocketAttrGetLocalName(), SocketAttrSetFlavor(), SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SocketAttrSetSd(), SocketNew(), SockFlavorDomainIPv4, SockFlavorFuncListener, SockFlavorProtocolTcp, SockResolveHostName(), and SockStrAnyHost.

Referenced by SocketAttrIsConnectionless(), and SocketWriteAll().

943 {
944  int sd;
945  struct sockaddr_in addrInet;
946  int backlog = 8;
947  int nVal;
948  Socket_T *pSocket;
949 
950  LOGDIAG4CALL(_TINT(ipPortListener), _TSTR(sLocalHostName));
951 
952  //
953  // Create socket in protocal familiy INET (IPv4), of type STREAM (TCP).
954  //
955  if( (sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
956  {
957  LOGSYSERROR("socket(%d,%d,%d)", PF_INET, SOCK_STREAM, IPPROTO_TCP);
958  return NULL;
959  }
960 
961  //
962  // Disallow binding if another local task already listening on this address.
963  // After socking binding and listen, then this sock option will disallow
964  // binding from any task to this port on any local address.
965  // Note: Non-critacal, so continue on errors.
966  //
967  nVal = 1;
968  if( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
969  &nVal, (socklen_t)sizeof(nVal)) < 0 )
970  {
971  LOGSYSERROR("setsockopt(%d,SOL_SOCKET,SO_REUSEADDR)", sd);
972  }
973 
974  //
975  // Enable sending of keep-alive messages on connection-oriented sockets.
976  // Note: Non-critacal, so continue on errors.
977  //
978  nVal = 1;
979  if( setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE,
980  &nVal, (socklen_t)sizeof(nVal)) < 0 )
981  {
982  LOGSYSERROR("setsockopt(%d,SOL_SOCKET,SO_KEEPALIVE)", sd);
983  }
984 
985  //
986  // Bind this socket to application family INET (IP) at the given port.
987  //
988  addrInet.sin_family = AF_INET; // ip application family
989  addrInet.sin_port = htons_nowarn((uint16_t)ipPortListener);
990  // listen port number
991 
992  //
993  // Let bind() automatically fill in the IP address of the machine where
994  // the calling process is running on.
995  //
996  if( sLocalHostName == NULL )
997  {
998  addrInet.sin_addr.s_addr = htonl(INADDR_ANY);
999  }
1000 
1001  //
1002  // Bind to a specific local address
1003  //
1004  else if( SockResolveHostName(sLocalHostName, &(addrInet.sin_addr)) < 0 )
1005  {
1006  LOGERROR("SockResolveHostName() failed");
1007  close(sd);
1008  return NULL;
1009  }
1010 
1011  //
1012  // Bind socket to the address.
1013  //
1014  if( bind(sd, (struct sockaddr *)&addrInet, (socklen_t)sizeof(addrInet)) < 0 )
1015  {
1016  LOGSYSERROR("bind(%d, ...)", sd);
1017  close(sd);
1018  return NULL;
1019  }
1020 
1021  //
1022  // Mark this bound socket as a listener for new incoming connections that
1023  // can then be accepted. The backlog is the maximum queue length of pending
1024  // connection requests.
1025  //
1026  if( listen(sd, backlog) < 0 )
1027  {
1028  LOGSYSERROR("listen(%d,%d)", sd, backlog);
1029  close(sd);
1030  return NULL;
1031  }
1032 
1033  //
1034  // Now create a new socket and set the appropriate attributes
1035  //
1036  pSocket = SocketNew();
1037 
1038  SocketAttrSetSd(pSocket, sd);
1039  SocketAttrSetFlavor(pSocket,
1041  SocketAttrSetLocalName(pSocket, NULL);
1043 
1044  LOGDIAG4("Opened Socket Listener on %s", SocketAttrGetLocalName(pSocket));
1045 
1046  return pSocket;
1047 }
Socket_T * SocketNew()
Allocate a new, unconnected socket.
Definition: sock.c:433
#define _TINT(var)
int (decimal)
Definition: log.h:582
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
#define NULL
null pointer
Definition: rnrconfig.h:199
void SocketAttrSetSd(Socket_T *pSocket, int sd)
Set Socket socket (file) descriptor.
Definition: sock.c:747
void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor)
Set Socket flavor.
Definition: sock.c:800
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
const char * SocketAttrGetLocalName(Socket_T *pSocket)
Get Socket local name.
Definition: sock.c:632
listener socket
Definition: sock.h:104
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
const char * SockStrAnyHost
any host
Definition: sock.c:129
#define _TSTR(var)
string variable
Definition: log.h:578
Definition: sock.c:136
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
static ushort_t htons_nowarn(ushort_t huShort)
htons() wrapper with no annoying warnings.
Definition: sock.c:117
static int SockResolveHostName(const char *sHostName, struct in_addr *pAddrInet)
Resolve host name into IP address (IPV4 only).
Definition: sock.c:155
Socket_T* SocketOpenUdp ( int  ipPortUdp,
const char *  sLocalHostName 
)

Create and open a new UDP/IP connectionless socket. port.

Parameters
ipPortUdpBinding UDP port number.
sLocalHostNameString name of local host to bind to. If NULL, then this host will automatically choose the IP address.
Returns
Returns a new UDP Socket_T pointer on success, NULL on failure.

Definition at line 1126 of file sock.c.

References _TINT, _TSTR, htons_nowarn(), LOGDIAG4, LOGDIAG4CALL, LOGERROR, LOGSYSERROR, NULL, SocketAttrGetLocalName(), SocketAttrSetFlavor(), SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SocketAttrSetSd(), SocketNew(), SockFlavorDomainIPv4, SockFlavorProtocolUdp, SockResolveHostName(), and SockStrAnyHost.

Referenced by SocketAttrIsConnectionless(), and SocketWriteAll().

1127 {
1128  int sd;
1129  struct sockaddr_in addrInet;
1130  Socket_T *pSocket;
1131 
1132  LOGDIAG4CALL(_TINT(ipPortUdp), _TSTR(sLocalHostName));
1133 
1134  //
1135  // Create socket in protocal familiy INET (IPv4), of type DateGram (UDP).
1136  //
1137  if( (sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0 )
1138  {
1139  LOGSYSERROR("socket(%d,%d,%d)", PF_INET, SOCK_DGRAM, IPPROTO_UDP);
1140  return NULL;
1141  }
1142 
1143  //
1144  // Bind this socket to application family INET (IP) at the given port.
1145  //
1146  memset((void*)&addrInet, 0, sizeof(addrInet));
1147  addrInet.sin_family = AF_INET; // ip application family
1148  addrInet.sin_port = htons_nowarn((ushort_t)ipPortUdp);
1149  // listen port number
1150 
1151 
1152  //
1153  // Let bind() automatically fill in the IP address of the machine where
1154  // the calling process is running on.
1155  //
1156  if( sLocalHostName == NULL )
1157  {
1158  addrInet.sin_addr.s_addr = htonl(INADDR_ANY);
1159  }
1160 
1161  //
1162  // Bind to a specific local address
1163  //
1164  else if( SockResolveHostName(sLocalHostName, &(addrInet.sin_addr)) < 0 )
1165  {
1166  LOGERROR("SockResolveHostName() failed");
1167  close(sd);
1168  return NULL;
1169  }
1170 
1171  //
1172  // Bind socket to the address.
1173  //
1174  if( bind(sd, (struct sockaddr *)&addrInet, (socklen_t)sizeof(addrInet)) < 0 )
1175  {
1176  LOGSYSERROR("bind(%d, ...)", sd);
1177  close(sd);
1178  return NULL;
1179  }
1180 
1181  //
1182  // Now create a new socket and set the appropriate attributes
1183  //
1184  pSocket = SocketNew();
1185 
1186  SocketAttrSetSd(pSocket, sd);
1187  SocketAttrSetFlavor(pSocket,
1189  SocketAttrSetLocalName(pSocket, NULL);
1191 
1192  LOGDIAG4("Opened UDP Socket %s", SocketAttrGetLocalName(pSocket));
1193 
1194  return pSocket;
1195 }
Socket_T * SocketNew()
Allocate a new, unconnected socket.
Definition: sock.c:433
#define _TINT(var)
int (decimal)
Definition: log.h:582
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
#define NULL
null pointer
Definition: rnrconfig.h:199
void SocketAttrSetSd(Socket_T *pSocket, int sd)
Set Socket socket (file) descriptor.
Definition: sock.c:747
void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor)
Set Socket flavor.
Definition: sock.c:800
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
const char * SocketAttrGetLocalName(Socket_T *pSocket)
Get Socket local name.
Definition: sock.c:632
UDP (DGRAM)
Definition: sock.h:100
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
const char * SockStrAnyHost
any host
Definition: sock.c:129
#define _TSTR(var)
string variable
Definition: log.h:578
Definition: sock.c:136
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
u16_t ushort_t
16-bit unsigned integer
Definition: rnrconfig.h:179
static ushort_t htons_nowarn(ushort_t huShort)
htons() wrapper with no annoying warnings.
Definition: sock.c:117
static int SockResolveHostName(const char *sHostName, struct in_addr *pAddrInet)
Resolve host name into IP address (IPV4 only).
Definition: sock.c:155
Socket_T* SocketOpenUnix ( int  ipPortUdp,
int  nType 
)

Create and open a new UNIX connectionless socket.

Todo:
Create and open a new UNIX connectionless socket.
Parameters
ipPortUdpBinding UDP port number.
nTypeType of...
Returns
Returns a new UNIX Socket_T pointer on success, NULL on failure.

Definition at line 1208 of file sock.c.

References NULL.

Referenced by SocketAttrIsConnectionless().

1209 {
1210  return NULL;
1211 }
#define NULL
null pointer
Definition: rnrconfig.h:199
Socket_T* SocketOpenUnixPair ( int  nType)

Create and open a new UNIX pair socket.

Todo:
Create and open a new UNIX pair socket.
Parameters
nTypeType of...
Returns
Returns a new UNIX Socket_T pointer on success, NULL on failure.

Definition at line 1223 of file sock.c.

References NULL.

Referenced by SocketAttrIsConnectionless().

1224 {
1225  return NULL;
1226 }
#define NULL
null pointer
Definition: rnrconfig.h:199
ssize_t SocketRead ( Socket_T pSocket)

Reads the next available bytes from the Socket into in the Socket's read buffer.

Note
Read are from the connection to the read buffer.
Parameters
pSocketPointer to Socket.
Returns
Number of bytes read on success (may be zero if a timeout or an interupt occurred). Returns < 0 (RC_SOCK_E*) return code on error.

Definition at line 1340 of file sock.c.

References CHKPTR, LOGDIAG4, LOGERROR, LOGSYSERROR, socket_t::m_pIOBuf, SimpleBuf_T::m_pRWBuf, socket_t::m_sd, socket_t::m_sLocalName, SIMPLEBUF_REL, SimpleBufGetWritePtr(), SimpleBufHasAvail(), SimpleBufWriteSeek(), SOCK_IO_READ, SOCK_RC_EBADSD, SOCK_RC_EBADSOCK, SOCK_RC_EBUFFULL, SOCK_RC_ENOBUF, SOCK_RC_ESYSERR, and SocketStateIsOpen().

Referenced by SocketAttrIsConnectionless().

1341 {
1342  SimpleBuf_T *pBuf;
1343  size_t nBytes;
1344  ssize_t nRead;
1345 
1346  CHKPTR(pSocket, SOCK_RC_EBADSOCK);
1347 
1348  // socket isn't open
1349  if( !SocketStateIsOpen(pSocket) )
1350  {
1351  LOGERROR("Socket %s is not open for reading", pSocket->m_sLocalName);
1352  return SOCK_RC_EBADSD;
1353  }
1354 
1355  // socket's simple read buffer
1356  pBuf = pSocket->m_pIOBuf[SOCK_IO_READ];
1357 
1358  // no read buffer present
1359  CHKPTR(pBuf, SOCK_RC_ENOBUF);
1360  CHKPTR(pBuf->m_pRWBuf, SOCK_RC_ENOBUF);
1361 
1362  // bytes remaining available in buffer
1363  nBytes = SimpleBufHasAvail(pBuf);
1364 
1365  // read buffer is full
1366  if( nBytes == 0 )
1367  {
1368  return SOCK_RC_EBUFFULL;
1369  }
1370 
1371  // read
1372  nRead = read(pSocket->m_sd, SimpleBufGetWritePtr(pBuf), nBytes);
1373 
1374  LOGDIAG4("SocketRead: read()=%d", nRead);
1375 
1376  // system error occurred - interpret
1377  if( nRead < 0 )
1378  {
1379  switch(errno)
1380  {
1381  case EAGAIN: // non-blocking, but read would have caused blocking
1382  case EINTR: // read was interrupted
1383  return 0;
1384  default:
1385  LOGSYSERROR("read()");
1386  return SOCK_RC_ESYSERR;
1387  }
1388  }
1389 
1390  SimpleBufWriteSeek(pBuf, (size_t)nRead, SIMPLEBUF_REL);
1391 
1392  return nRead;
1393 }
#define SOCK_IO_READ
read index
Definition: sock.h:130
#define SIMPLEBUF_REL
relative
Definition: simplebuf.h:53
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define SOCK_RC_EBADSD
bad/closed socket descriptor
Definition: sock.h:124
#define SOCK_RC_ENOBUF
no buffer
Definition: sock.h:123
#define SOCK_RC_EBUFFULL
buffer is full
Definition: sock.h:121
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
static size_t SimpleBufHasAvail(SimpleBuf_T *pBuf)
Returns number of bytes available in buffer for writing.
Definition: simplebuf.h:190
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
char * m_sLocalName
local name
Definition: sock.c:141
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
bool_t SocketStateIsOpen(Socket_T *pSocket)
Check if Socket is open.
Definition: sock.c:888
#define SOCK_RC_EBADSOCK
socket is bad
Definition: sock.h:122
#define SOCK_RC_ESYSERR
system error occurred
Definition: sock.h:125
static size_t SimpleBufWriteSeek(SimpleBuf_T *pBuf, size_t wpos, int how)
Seek buffer write head to the new write position.
Definition: simplebuf.h:125
int m_sd
(open) socket descriptor
Definition: sock.c:138
static byte_t * SimpleBufGetWritePtr(SimpleBuf_T *pBuf)
Returns pointer to internal I/O buffer at the current write position.
Definition: simplebuf.h:214
bool_t SocketStateIsErrored ( Socket_T pSocket)

Check if Socket is in some kind of errored state.

The fcntl() non-blocking function is used to test the socket.

Parameters
pSocketPointer to Socket.
Returns
Returns true or false.

Definition at line 909 of file sock.c.

References socket_t::m_sd, NULL, and SOCK_SD_CLOSED.

Referenced by SocketAttrIsConnectionless().

910 {
911  int fopts = 0;
912 
913  if( (pSocket == NULL) || (pSocket->m_sd == SOCK_SD_CLOSED) )
914  {
915  return false;
916  }
917  else if( fcntl(pSocket->m_sd, F_GETFL, &fopts) >= 0 )
918  {
919  return false;
920  }
921  else
922  {
923  return true;
924  }
925 }
#define NULL
null pointer
Definition: rnrconfig.h:199
#define SOCK_SD_CLOSED
closed socket descriptor value
Definition: sock.h:144
int m_sd
(open) socket descriptor
Definition: sock.c:138
bool_t SocketStateIsOpen ( Socket_T pSocket)

Check if Socket is open.

Parameters
pSocketPointer to Socket.
Returns
Returns true or false.

Definition at line 888 of file sock.c.

References socket_t::m_sd, NULL, and SOCK_SD_CLOSED.

Referenced by SocketAttrIsConnectionless(), SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SocketRead(), SocketWrite(), and SockSetChkSd().

889 {
890  if( pSocket == NULL )
891  {
892  return false;
893  }
894  else
895  {
896  return pSocket->m_sd == SOCK_SD_CLOSED? false: true;
897  }
898 }
#define NULL
null pointer
Definition: rnrconfig.h:199
#define SOCK_SD_CLOSED
closed socket descriptor value
Definition: sock.h:144
int m_sd
(open) socket descriptor
Definition: sock.c:138
char* SocketThisHostName ( )

Get the host string name of this network node (computer).

Returns
Returns pointer to allocated name string.

Definition at line 379 of file sock.c.

References HOST_NAME_MAX, LOGERROR, LOGSYSERROR, new_strdup(), NEWSTR, NULL, and SockStrUnknownHost.

380 {
381  char sHostName[HOST_NAME_MAX];
382  char sAddr[INET6_ADDRSTRLEN];
383  struct hostent *pHostEntry;
384  char *p;
385 
386  // get the host name of this processor
387  if( gethostname(sHostName, sizeof(sHostName)) == -1 )
388  {
389  LOGSYSERROR("gethostname(...)");
391  }
392  sHostName[sizeof(sHostName)-1] = 0;
393 
394  // get this host entry info
395  if( (pHostEntry = gethostbyname(sHostName)) == NULL )
396  {
397  LOGERROR("gethostbyname(%s): %s", sHostName, hstrerror(h_errno));
398  return new_strdup(sHostName);
399  }
400 
401  // dotted address to string
402  if( pHostEntry->h_addr_list && pHostEntry->h_addr_list[0] )
403  {
404  if( inet_ntop(pHostEntry->h_addrtype, pHostEntry->h_addr_list[0],
405  sAddr, (socklen_t)sizeof(sAddr)) == NULL )
406  {
407  LOGSYSERROR("inet_ntop(%d, ...)", pHostEntry->h_addrtype);
408  strcpy(sAddr, "ADDR???");
409  }
410  }
411  else
412  {
413  strcpy(sAddr, "ADDR???");
414  }
415  sAddr[sizeof(sAddr)-1] = 0;
416 
417  p = NEWSTR(strlen(sHostName)+strlen(sAddr)+2);
418  sprintf(p, "%s(%s)", sHostName, sAddr);
419 
420  return p;
421 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * SockStrUnknownHost
unknown host
Definition: sock.c:131
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
#define HOST_NAME_MAX
maximum host name length
Definition: sock.c:86
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
ssize_t SocketWrite ( Socket_T pSocket)

Writes the bytes from the Socket's write buffer to the Socket connection.

Only one write attempt is made.

Note
Writes are from the Socket write buffer.
Parameters
pSocketPointer to Socket.
Returns
Number of bytes written on success (may be zero if a timeout or an interupt occurred). Returns < 0 (RC_SOCK_E*) return code on error.

Definition at line 1409 of file sock.c.

References CHKPTR, LOGERROR, LOGSYSERROR, socket_t::m_pIOBuf, SimpleBuf_T::m_pRWBuf, socket_t::m_sd, SIMPLEBUF_REL, SimpleBufGetReadPtr(), SimpleBufHasLen(), SimpleBufReadSeek(), SOCK_IO_WRITE, SOCK_RC_EBADSD, SOCK_RC_EBADSOCK, SOCK_RC_ENOBUF, SOCK_RC_ESYSERR, SOCK_RC_IODONE, and SocketStateIsOpen().

Referenced by SocketAttrIsConnectionless(), and SocketWriteAll().

1410 {
1411  SimpleBuf_T *pBuf;
1412  size_t nBytes;
1413  ssize_t nWritten;
1414 
1415  CHKPTR(pSocket, SOCK_RC_EBADSOCK);
1416 
1417  // socket isn't open
1418  if( !SocketStateIsOpen(pSocket) )
1419  {
1420  LOGERROR("Socket is not open for writing");
1421  return SOCK_RC_EBADSD;
1422  }
1423 
1424  // socket's simple read buffer
1425  pBuf = pSocket->m_pIOBuf[SOCK_IO_WRITE];
1426 
1427  // no write buffer present
1428  CHKPTR(pBuf, SOCK_RC_ENOBUF);
1429  CHKPTR(pBuf->m_pRWBuf, SOCK_RC_ENOBUF);
1430 
1431  // bytes in buffer
1432  nBytes = SimpleBufHasLen(pBuf);
1433 
1434  // read buffer is empty - finished writing this buffer
1435  if( nBytes == 0 )
1436  {
1437  return SOCK_RC_IODONE;
1438  }
1439 
1440  // write
1441  nWritten = write(pSocket->m_sd, SimpleBufGetReadPtr(pBuf), nBytes);
1442 
1443  // system error occurred - interpret
1444  if( nWritten < 0 )
1445  {
1446  switch(errno)
1447  {
1448  case EAGAIN: // non-blocking, but write would have caused blocking
1449  case EINTR: // write was interrupted
1450  return 0;
1451  default:
1452  LOGSYSERROR("write()");
1453  return SOCK_RC_ESYSERR;
1454  }
1455  }
1456 
1457  SimpleBufReadSeek(pBuf, (size_t)nWritten, SIMPLEBUF_REL);
1458 
1459  return nWritten;
1460 }
static byte_t * SimpleBufGetReadPtr(SimpleBuf_T *pBuf)
Returns pointer to internal I/O buffer at the current read position.
Definition: simplebuf.h:202
#define SOCK_RC_IODONE
finished current I/O
Definition: sock.h:120
#define SIMPLEBUF_REL
relative
Definition: simplebuf.h:53
SimpleBuf_T * m_pIOBuf[SOCK_IO_NUMOF]
read/write buffers
Definition: sock.c:143
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define SOCK_RC_EBADSD
bad/closed socket descriptor
Definition: sock.h:124
static size_t SimpleBufHasLen(SimpleBuf_T *pBuf)
Returns number of bytes currently in buffer.
Definition: simplebuf.h:178
#define SOCK_RC_ENOBUF
no buffer
Definition: sock.h:123
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
bool_t SocketStateIsOpen(Socket_T *pSocket)
Check if Socket is open.
Definition: sock.c:888
#define SOCK_RC_EBADSOCK
socket is bad
Definition: sock.h:122
#define SOCK_RC_ESYSERR
system error occurred
Definition: sock.h:125
static size_t SimpleBufReadSeek(SimpleBuf_T *pBuf, size_t rpos, int how)
Seek buffer read head to the new read position.
Definition: simplebuf.h:105
int m_sd
(open) socket descriptor
Definition: sock.c:138
#define SOCK_IO_WRITE
write index
Definition: sock.h:131
ssize_t SocketWriteAll ( Socket_T pSocket)

Writes all of the bytes from the socket's write buffer until no more bytes or error.

Parameters
pSocketPointer to Socket.
Returns
Number of bytes written on success. Returns < 0 (RC_SOCK_E*) return code on error.

Definition at line 1472 of file sock.c.

References LOGDIAG4, LOGERROR, NULL, SOCK_RC_IODONE, SocketAttrGetLocalName(), SocketAttrGetRemoteName(), SocketClose(), SocketDelete(), SocketOpenTcpConnection(), SocketOpenTcpListener(), SocketOpenUdp(), and SocketWrite().

Referenced by SocketAttrIsConnectionless().

1473 {
1474  ssize_t n, nWritten=0;
1475 
1476  // write all of the bytes in the buffer
1477  while( (n = SocketWrite(pSocket)) >= 0 )
1478  {
1479  nWritten += n;
1480  }
1481 
1482  // all bytes have been
1483  if( n == SOCK_RC_IODONE )
1484  {
1485  return nWritten;
1486  }
1487 
1488  // error occurred
1489  else
1490  {
1491  return n;
1492  }
1493 }
#define SOCK_RC_IODONE
finished current I/O
Definition: sock.h:120
ssize_t SocketWrite(Socket_T *pSocket)
Writes the bytes from the Socket&#39;s write buffer to the Socket connection.
Definition: sock.c:1409