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

Socket services definitions. More...

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/sock.h"
#include "rnr/simplebuf.h"
#include "rnr/log.h"
#include "rnr/new.h"

Go to the source code of this file.

Classes

struct  socket_t
 

Macros

#define USE_IPV6
 enable IPV6 (Cygwin)
 
#define HOST_NAME_MAX   64
 maximum host name length
 

Functions

static void fdset_nowarn (int fd, fd_set *pset)
 FD_SET() wrapper with no annoying warnings. More...
 
static ushort_t ntohs_nowarn (ushort_t huShort)
 ntohs() wrapper with no annoying warnings. More...
 
static ushort_t htons_nowarn (ushort_t huShort)
 htons() wrapper with no annoying warnings. More...
 
static int SockResolveHostName (const char *sHostName, struct in_addr *pAddrInet)
 Resolve host name into IP address (IPV4 only). More...
 
static char * SocketAddrNameInet (int af, struct sockaddr *pAddr, const char *sHostName)
 Allocate formated internet name (IPV4, IPV6). More...
 
static char * SocketAddrNameUnix (struct sockaddr_un *pAddrUnix, const char *sHostName)
 Allocate formated UNIX name. More...
 
static char * SocketAddrName (struct sockaddr *pAddr, const char *sHostName)
 Allocate formated address name. More...
 
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...
 
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...
 

Variables

const char * SockStrClosed = "** Closed **"
 closed socket More...
 
const char * SockStrDisconnected = "** Disconnected **"
 disconnected
 
const char * SockStrAnyHost = "ANYHOST"
 any host
 
const char * SockStrLocalHost = "LOCALHOST"
 local host
 
const char * SockStrUnknownHost = "UNKNOWNHOST"
 unknown host
 

Detailed Description

Socket services definitions.

LastChangedDate
2012-07-10 12:37:42 -0600 (Tue, 10 Jul 2012)
Rev
2078

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

Todo:

Socket_T *SocketOpenUnix(int ipPortUdp, int nType)

Socket_T *SocketOpenUnixPair(int nType)

fix cygwin shortcommings.

Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Original Source and Copyright:
Original Author:
Jon Travis (jtrav.nosp@m.is@p.nosp@m.00p.o.nosp@m.rg)

Definition in file sock.c.

Function Documentation

static void fdset_nowarn ( int  fd,
fd_set *  pset 
)
inlinestatic

FD_SET() wrapper with no annoying warnings.

Parameters
fdFile descriptor to add to set.
psetPointer to fd set.

Definition at line 95 of file sock.c.

References PRAGMA_IGNORED, and PRAGMA_WARNING.

96 {
97  FD_SET(fd, pset);
98 }
static ushort_t htons_nowarn ( ushort_t  huShort)
inlinestatic

htons() wrapper with no annoying warnings.

Parameters
huShortUnsigned short integer.
Returns
Network byte order.

Definition at line 117 of file sock.c.

References PRAGMA_WARNING.

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

118 {
119  return htons(huShort);
120 }
static ushort_t ntohs_nowarn ( ushort_t  huShort)
inlinestatic

ntohs() wrapper with no annoying warnings.

Parameters
huShortUnsigned short integer.
Returns
Host byte order.

Definition at line 107 of file sock.c.

Referenced by SocketAddrNameInet().

108 {
109  return ntohs(huShort);
110 }
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
static char* SocketAddrName ( struct sockaddr *  pAddr,
const char *  sHostName 
)
static

Allocate formated address name.

Generated Name Format:
* addrname:
*    addrname_inet
*    addrname_unix
* 
Parameters
pAddrSocket address.
sHostNameHost name. NULLL to force lookup.
Returns
Returns pointer to allocated name string.

Definition at line 347 of file sock.c.

References new_strdup(), SocketAddrNameInet(), and SocketAddrNameUnix().

Referenced by SocketAttrSetLocalName(), and SocketAttrSetRemoteName().

348 {
349  int af;
350 
351  af = pAddr->sa_family;
352 
353  switch(af)
354  {
355  case AF_INET:
356  case AF_INET6:
357  return SocketAddrNameInet(af, pAddr, sHostName);
358  case AF_UNIX:
359  return SocketAddrNameUnix((struct sockaddr_un *)pAddr, sHostName);
360  default:
361  return new_strdup("AF???");
362  }
363 }
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
static char * SocketAddrNameUnix(struct sockaddr_un *pAddrUnix, const char *sHostName)
Allocate formated UNIX name.
Definition: sock.c:303
static char * SocketAddrNameInet(int af, struct sockaddr *pAddr, const char *sHostName)
Allocate formated internet name (IPV4, IPV6).
Definition: sock.c:204
static char* SocketAddrNameInet ( int  af,
struct sockaddr *  pAddr,
const char *  sHostName 
)
static

Allocate formated internet name (IPV4, IPV6).

Generated Name Format:
* addrname_inet:
*    hostname '('dottedaddr')'
*    hostname '('dottedaddr port')'
* hostname:
*    given
*    lookup
*    dottedaddr
* dottedaddr:
*    ipv4_format
*    ipv6_format
* port: 
*    ':' number
* 
Parameters
afInternet socket protocol family.
pAddrSocket address.
sHostNameHost name. NULLL to force lookup.
Returns
Returns pointer to allocated name string.

Definition at line 204 of file sock.c.

References LOGSYSERROR, NEWSTR, ntohs_nowarn(), NULL, and SockStrAnyHost.

Referenced by SocketAddrName().

207 {
208  void *pAddrIn;
209  size_t nAddrSize;
210  char bufAddr[INET6_ADDRSTRLEN];
211  uint_t uPort;
212  char bufPort[16];
213  char bufHost[NI_MAXHOST];
214  char *sHost;
215  char *sAddrName;
216 
217  bufAddr[0] = 0;
218  bufPort[0] = 0;
219  bufHost[0] = 0;
220 
221  // ipv4
222  if( af == AF_INET )
223  {
224  nAddrSize = sizeof(struct sockaddr_in);
225  pAddrIn = &(((struct sockaddr_in *)pAddr)->sin_addr);
226  uPort = (uint_t)(((struct sockaddr_in *)pAddr)->sin_port);
227  }
228  // ipv6
229  else if( af == AF_INET6 )
230  {
231  nAddrSize = sizeof(struct sockaddr_in6);
232  pAddrIn = &(((struct sockaddr_in6 *)pAddr)->sin6_addr);
233  uPort = (uint_t)(((struct sockaddr_in6 *)pAddr)->sin6_port);
234  }
235  // bug
236  else
237  {
238  return NULL;
239  }
240 
241  // dotted address
242  if( inet_ntop(af, pAddrIn, bufAddr, (socklen_t)sizeof(bufAddr)) != NULL )
243  {
244  if( uPort != 0 )
245  {
246  snprintf(bufPort, sizeof(bufPort), ":%hu", ntohs_nowarn((ushort_t)uPort));
247  bufPort[sizeof(bufPort)-1] = 0;
248  }
249  }
250  else
251  {
252  LOGSYSERROR("inet_ntop(%d, ...)", af);
253  strcpy(bufAddr, "ADDR???");
254  }
255 
256  // use given hostname
257  if( sHostName != NULL )
258  {
259  sHost = (char *)sHostName;
260  }
261 
262  // any host address
263  else if( (af == AF_INET)
264  && (((struct in_addr *)pAddrIn)->s_addr == INADDR_ANY) )
265  {
266  sHost = (char *)SockStrAnyHost;
267  }
268 
269  // lookup hostname (makes dotted if cannot find)
270  else
271  {
272  sHost = bufHost;
273  getnameinfo(pAddr, (socklen_t)nAddrSize,
274  bufHost, (socklen_t)sizeof(bufHost),
275  NULL, (socklen_t)0, NI_NOFQDN);
276  }
277 
278  sAddrName = NEWSTR(strlen(sHost)+strlen(bufAddr)+strlen(bufPort)+3);
279 
280  sprintf(sAddrName, "%s(%s%s)", sHost, bufAddr, bufPort);
281 
282  return sAddrName;
283 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
#define NULL
null pointer
Definition: rnrconfig.h:199
u32_t uint_t
32-bit unsigned integer
Definition: rnrconfig.h:181
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
const char * SockStrAnyHost
any host
Definition: sock.c:129
u16_t ushort_t
16-bit unsigned integer
Definition: rnrconfig.h:179
static ushort_t ntohs_nowarn(ushort_t huShort)
ntohs() wrapper with no annoying warnings.
Definition: sock.c:107
static char* SocketAddrNameUnix ( struct sockaddr_un *  pAddrUnix,
const char *  sHostName 
)
static

Allocate formated UNIX name.

Generated Name Format:
* addrname_unix:
*    hostname '('unixpath')'
* hostname:
*    given
*    localhost
*    "LOCALHOST"
* 
Parameters
pAddrUnixUNIX socket address.
sHostNameHost name. NULLL to force lookup.
Returns
Returns pointer to allocated name string.

Definition at line 303 of file sock.c.

References NEWSTR, NULL, and SockStrLocalHost.

Referenced by SocketAddrName().

305 {
306  char bufHost[NI_MAXHOST];
307  char *sHost;
308  char *sAddrName;
309 
310  // use given hostname
311  if( sHostName != NULL )
312  {
313  sHost = (char *)sHostName;
314  }
315  // lookup local hostname
316  else if( gethostname(bufHost, sizeof(bufHost)) < 0 )
317  {
318  sHost = bufHost;
319  }
320  else
321  {
322  sHost = (char *)SockStrLocalHost;
323  }
324 
325  sAddrName = NEWSTR(strlen(sHost)+strlen(pAddrUnix->sun_path)+3);
326 
327  sprintf(sAddrName, "%s(%s)", sHost, pAddrUnix->sun_path);
328 
329  return sAddrName;
330 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * SockStrLocalHost
local host
Definition: sock.c:130
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
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
static int SockResolveHostName ( const char *  sHostName,
struct in_addr *  pAddrInet 
)
static

Resolve host name into IP address (IPV4 only).

Parameters
sHostNameHost name in dotted or DNS network format.
pAddrInetIP address
Returns
Returns 0 on success, -1 on failure.

Definition at line 155 of file sock.c.

References LOGERROR, NULL, and OK.

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

156 {
157  struct hostent *pHostEntry;
158  struct in_addr addrInet;
159 
160  // hostname: "www.xxx.yyy.zzz" (IPv4)
161  if( inet_aton(sHostName, &addrInet) != 0 )
162  {
163  memcpy(pAddrInet, &addrInet, sizeof(addrInet));
164  }
165  // hostname: "www.xxx.yyy.zzz" (IPv4) or "networkname" (may use DNS)
166  else if( (pHostEntry = gethostbyname(sHostName)) != NULL )
167  {
168  memcpy(pAddrInet, pHostEntry->h_addr, (size_t)pHostEntry->h_length);
169  }
170  else
171  {
172  LOGERROR("gethostbyname(%s): %s", sHostName, hstrerror(h_errno));
173  return -1;
174  }
175 
176  return OK;
177 }
#define OK
Okay.
Definition: rnrconfig.h:301
#define NULL
null pointer
Definition: rnrconfig.h:199
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488

Variable Documentation

const char* SockStrClosed = "** Closed **"

closed socket

Special Host Names

Definition at line 127 of file sock.c.

Referenced by SocketAttrSetLocalName(), SocketClose(), and SocketNew().