librnr  1.14.5
RoadNarrows Robotics Common Library 1
sock.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Socket services declarations.
5  *
6  * These socket functions were inspired by the camserv socket.c functions
7  * written by Jon Travis
8  * (\ref sock_h_original_src "see below").
9  *
10  * \pkgsynopsis
11  * RoadNarrows Robotics Common Library 1
12  *
13  * \pkgcomponent{Library}
14  * librnr
15  *
16  * \pkgfile{rnr/sock.h}
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
21  *
22  * \license{MIT}
23  *
24  * \EulaBegin
25  * See the README and EULA files for any copyright and licensing information.
26  * \EulaEnd
27  *
28  * <hr>
29  * \anchor sock_h_original_src
30  * \par Original Source Comment Block
31  *
32  * \par Original Author
33  * Jon Travis (jtravis@p00p.org)
34  *
35  * \par Original Copyright
36  * N/A
37  *
38  * \par Original Header
39  * N/A
40  *
41  * <hr>
42  */
43 ////////////////////////////////////////////////////////////////////////////////
44 
45 #ifndef _RNR_SOCK_H
46 #define _RNR_SOCK_H
47 
48 /*! Allow IPV6 */
49 #define USE_IPV6
50 
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 
54 
55 #ifdef ARCH_cygwin
56 #endif // ARCH_cygwin
57 
58 #include "rnr/rnrconfig.h"
59 #include "rnr/log.h"
60 #include "rnr/simplebuf.h"
61 
62 //
63 // Cygwin work-arounds
64 //
65 #ifdef ARCH_cygwin
66 
67 #endif // ARCH_cygwin
68 
69 //
70 // Debug valves
71 //
72 #undef debugSock ///< general debugging
73 #undef debugSockUTest ///< unit tests
74 
75 //
76 // Unit tests
77 //
78 #if defined(debugSockUTest) && !defined(debugSock)
79 #define debugSock ///< debug sockets
80 #endif
81 
83 
84 /*!
85  * Socket Flavors (a distillation of the various POSIX domains, families,
86  * protocals, and types).
87  */
88 typedef enum
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;
109 
110 //
111 // The Socket Structure
112 //
113 typedef struct socket_t Socket_T; ///< the Socket
114 
115 //
116 // Socket Return Codes
117 //
118 #define SOCK_RC_OK 0 ///< no error
119 #define SOCK_RC_EFAIL (-1) ///< general, unspecified error
120 #define SOCK_RC_IODONE (-2) ///< finished current I/O
121 #define SOCK_RC_EBUFFULL (-3) ///< buffer is full
122 #define SOCK_RC_EBADSOCK (-4) ///< socket is bad
123 #define SOCK_RC_ENOBUF (-5) ///< no buffer
124 #define SOCK_RC_EBADSD (-6) ///< bad/closed socket descriptor
125 #define SOCK_RC_ESYSERR (-7) ///< system error occurred
126 
127 //
128 // I/O Buffer Indices
129 //
130 #define SOCK_IO_READ 0 ///< read index
131 #define SOCK_IO_WRITE 1 ///< write index
132 #define SOCK_IO_NUMOF 2 ///< number of indices
133 
134 /*!
135  * \brief Check I/O index
136  * \param io I/O buffer index
137  */
138 #define SOCK_CHK_IO(io, ...) \
139  CHKEXPR_INT(io, ((io) >= 0) && ((io) < SOCK_IO_NUMOF), __VA_ARGS__)
140 
141 //
142 // Special Data Values
143 //
144 #define SOCK_SD_CLOSED (-1) ///< closed socket descriptor value
145 #define SOCK_PORT_NONE (-1) ///< no port number specified
146 
147 
148 //
149 // Prototypes
150 //
151 
152 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
153 // Socket Utilities
154 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
155 
156 extern char *SocketThisHostName();
157 
158 
159 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
160 // Socket New/Delete Functions
161 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
162 
163 extern Socket_T *SocketNew();
164 
165 extern void SocketDelete(Socket_T *pSocket);
166 
167 
168 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
169 // Socket Read/Write Buffer Functions
170 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
171 
172 extern SimpleBuf_T *SocketBufNewBuf(Socket_T *pSocket,
173  int eIOBuf,
174  size_t nBufSize);
175 
176 extern SimpleBuf_T *SocketBufSetBuf(Socket_T *pSocket,
177  int eIOBuf,
178  byte_t *pBuf,
179  size_t nBufSize);
180 
181 extern SimpleBuf_T *SocketBufGetBuf(Socket_T *pSocket, int eIOBuf);
182 
183 extern byte_t *SocketBufGetRawBuf(Socket_T *pSocket, int eIOBuf);
184 
185 extern size_t SocketBufGetSize(Socket_T *pSocket, int eIOBuf);
186 
187 extern size_t SocketBufGetLen(Socket_T *pSocket, int eIOBuf);
188 
189 extern void SocketBufClear(Socket_T *pSocket, int eIOBuf);
190 
191 
192 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
193 // Socket Attribute Access Functions
194 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
195 
196 // Client Data Attributes
197 extern void *SocketAttrGetClientData(Socket_T *pSocket);
198 
199 extern void SocketAttrSetClientData(Socket_T *pSocket, void *pClientData);
200 
201 // Local Name Attributes
202 extern const char *SocketAttrGetLocalName(Socket_T *pSocket);
203 
204 extern void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName);
205 
206 // Remote Name Attributes
207 extern const char *SocketAttrGetRemoteName(Socket_T *pSocket);
208 
209 // Remote Name
210 extern void SocketAttrSetRemoteName(Socket_T *pSocket,
211  struct sockaddr *pAddr,
212  const char *sHostName);
213 
214 // Socket Descriptor Attributes
215 extern int SocketAttrGetSd(Socket_T *pSocket);
216 
217 extern void SocketAttrSetSd(Socket_T *pSocket, int sd);
218 
219 // IP Port Attributes
220 extern int SocketAttrGetPort(Socket_T *pSocket);
221 
222 extern void SocketAttrSetPort(Socket_T *pSocket, int ipPort);
223 
224 // Socket Flavor and State Attributes
225 extern SockFlavor SocketAttrGetFlavor(Socket_T *pSocket);
226 
227 extern void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor);
228 
229 extern bool_t SocketAttrHasFlavor(Socket_T *pSocket, int nMask, int nFlavor);
230 
231 /*!
232  * \brief Test if socket is a listener flavor.
233  *
234  * \param pSocket Pointer to socket.
235  *
236  * \return Returns true or false.
237  */
239 {
240  return SocketAttrHasFlavor(pSocket,
243 }
244 
245 /*!
246  * \brief Test if socket is connection flavor.
247  *
248  * \param pSocket Pointer to socket.
249  *
250  * \return Returns true or false.
251  */
253 {
254  return SocketAttrHasFlavor(pSocket,
257 }
258 
259 /*!
260  * \brief Test if socket is connectionless flavor.
261  *
262  * \param pSocket Pointer to socket.
263  *
264  * \return Returns true or false.
265  */
267 {
268  return SocketAttrIsConnection(pSocket)? false: true;
269 }
270 
271 extern int SocketAttrSetNonBlocking(Socket_T *pSocket);
272 
273 
274 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
275 // Socket State Functions
276 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
277 
278 extern bool_t SocketStateIsOpen(Socket_T *pSocket);
279 
280 extern bool_t SocketStateIsErrored(Socket_T *pSocket);
281 
282 
283 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
284 // Socket Open/Close Functions
285 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
286 
287 extern Socket_T *SocketOpenTcpListener(int ipPortListener,
288  const char *sLocalHostName);
289 
290 extern Socket_T *SocketOpenTcpConnection(const char *sRemoteHostName,
291  int ipPortRemote);
292 
293 extern Socket_T *SocketOpenUdp(int ipPortUdp, const char *sLocalHostName);
294 
295 extern Socket_T *SocketOpenUnix(int ipPortUdp, int nType);
296 
297 extern Socket_T *SocketOpenUnixPair(int nType);
298 
299 extern Socket_T *SocketAccept(Socket_T *pSocketListener, bool_t bNonBlock);
300 
301 extern int SocketClose(Socket_T *pSocket);
302 
303 
304 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
305 // Socket Read/Write Functions
306 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
307 
308 extern ssize_t SocketRead(Socket_T *pSocket);
309 
310 extern ssize_t SocketWrite(Socket_T *pSocket);
311 
312 extern ssize_t SocketWriteAll(Socket_T *pSocket);
313 
314 #ifdef debugSockUTest
315 /*! Unit Test Function. */
316 extern void SocketUTest();
317 #endif
318 
320 
321 
322 #endif // _RNR_SOCK_H
void * SocketAttrGetClientData(Socket_T *pSocket)
Get Socket client user data.
Definition: sock.c:607
size_t SocketBufGetSize(Socket_T *pSocket, int eIOBuf)
Get Socket buffer size.
Definition: sock.c:557
INLINE_IN_H bool_t SocketAttrIsConnection(Socket_T *pSocket)
Test if socket is connection flavor.
Definition: sock.h:252
char * SocketThisHostName()
Get the host string name of this network node (computer).
Definition: sock.c:379
void SocketAttrSetLocalName(Socket_T *pSocket, const char *sHostName)
Set Socket local name.
Definition: sock.c:644
int SocketAttrGetSd(Socket_T *pSocket)
Get Socket socket (file) descriptor.
Definition: sock.c:735
connection/connectionless
Definition: sock.h:105
Socket_T * SocketNew()
Allocate a new, unconnected socket.
Definition: sock.c:433
void SocketDelete(Socket_T *pSocket)
Delate an allocated socket.
Definition: sock.c:457
void SocketAttrSetSd(Socket_T *pSocket, int sd)
Set Socket socket (file) descriptor.
Definition: sock.c:747
byte_t * SocketBufGetRawBuf(Socket_T *pSocket, int eIOBuf)
Get Socket raw buffer.
Definition: sock.c:541
const char * SocketAttrGetLocalName(Socket_T *pSocket)
Get Socket local name.
Definition: sock.c:632
SimpleBuf_T * SocketBufNewBuf(Socket_T *pSocket, int eIOBuf, size_t nBufSize)
Allocate a socket buffer and attach to Socket.
Definition: sock.c:485
int SocketClose(Socket_T *pSocket)
Close a Socket.
Definition: sock.c:1292
ssize_t SocketRead(Socket_T *pSocket)
Reads the next available bytes from the Socket into in the Socket&#39;s read buffer.
Definition: sock.c:1340
Socket_T * SocketOpenTcpConnection(const char *sRemoteHostName, int ipPortRemote)
Create and open a new TCP/IP socket to the given remote host and port.
Definition: sock.c:1059
void SocketAttrSetRemoteName(Socket_T *pSocket, struct sockaddr *pAddr, const char *sHostName)
Set Socket remote name.
Definition: sock.c:695
INLINE_IN_H bool_t SocketAttrIsConnectionless(Socket_T *pSocket)
Test if socket is connectionless flavor.
Definition: sock.h:266
SockFlavor
< unit tests
Definition: sock.h:88
SimpleBuf_T * SocketBufSetBuf(Socket_T *pSocket, int eIOBuf, byte_t *pBuf, size_t nBufSize)
Attach a buffer to Socket.
Definition: sock.c:505
listener socket
Definition: sock.h:104
UDP (DGRAM)
Definition: sock.h:100
int SocketAttrSetNonBlocking(Socket_T *pSocket)
Set Socket for non-blockiong I/O.
Definition: sock.c:837
void SocketAttrSetFlavor(Socket_T *pSocket, SockFlavor eFlavor)
Set Socket flavor.
Definition: sock.c:800
bool_t SocketStateIsOpen(Socket_T *pSocket)
Check if Socket is open.
Definition: sock.c:888
SockFlavor SocketAttrGetFlavor(Socket_T *pSocket)
Get Socket flavor.
Definition: sock.c:788
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
INLINE_IN_H bool_t SocketAttrIsListener(Socket_T *pSocket)
Test if socket is a listener flavor.
Definition: sock.h:238
Socket_T * SocketOpenUdp(int ipPortUdp, const char *sLocalHostName)
Create and open a new UDP/IP connectionless socket. port.
Definition: sock.c:1126
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
RoadNarrows Robotics common configuration file.
#define INLINE_IN_H
inline C funtion in C header
Definition: rnrconfig.h:157
Socket_T * SocketOpenTcpListener(int ipPortListener, const char *sLocalHostName)
Create and open a new TCP/IP passive listener socket on this host.
Definition: sock.c:942
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
void SocketAttrSetPort(Socket_T *pSocket, int ipPort)
Set Socket port number.
Definition: sock.c:775
Socket_T * SocketAccept(Socket_T *pSocketListener, bool_t bNonBlock)
Accept a client connection requst from the listener socket.
Definition: sock.c:1240
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
Definition: sock.c:136
bool_t SocketStateIsErrored(Socket_T *pSocket)
Check if Socket is in some kind of errored state.
Definition: sock.c:909
size_t SocketBufGetLen(Socket_T *pSocket, int eIOBuf)
Get number of bytes in Socket buffer.
Definition: sock.c:573
SimpleBuf_T * SocketBufGetBuf(Socket_T *pSocket, int eIOBuf)
Get Socket buffer.
Definition: sock.c:525
UNIX (local)
Definition: sock.h:95
undefined
Definition: sock.h:90
Socket_T * SocketOpenUnixPair(int nType)
Create and open a new UNIX pair socket.
Definition: sock.c:1223
bool_t SocketAttrHasFlavor(Socket_T *pSocket, int nMask, int nFlavor)
Check if Socket has an assigned flavor.
Definition: sock.c:815
void SocketBufClear(Socket_T *pSocket, int eIOBuf)
Clear a Socket buffer.
Definition: sock.c:587
ssize_t SocketWriteAll(Socket_T *pSocket)
Writes all of the bytes from the socket&#39;s write buffer until no more bytes or error.
Definition: sock.c:1472
int SocketAttrGetPort(Socket_T *pSocket)
Get Socket port number.
Definition: sock.c:763
Logger declarations.
void SocketAttrSetClientData(Socket_T *pSocket, void *pClientData)
Get Socket client user data.
Definition: sock.c:619
const char * SocketAttrGetRemoteName(Socket_T *pSocket)
Get Socket remote name.
Definition: sock.c:682
Simple [io] buffer declarations and operations.
Socket_T * SocketOpenUnix(int ipPortUdp, int nType)
Create and open a new UNIX connectionless socket.
Definition: sock.c:1208
ssize_t SocketWrite(Socket_T *pSocket)
Writes the bytes from the Socket&#39;s write buffer to the Socket connection.
Definition: sock.c:1409