Internetprotocol-Sockets

ALlows to connect & communicate to other systems via TCP or UDP.

Summary
Internetprotocol-SocketsALlows to connect & communicate to other systems via TCP or UDP.
CopyrightThis program is free software.
Files
C-kern/api/io/ip/ipsocket.hHeader file of Internetprotocol-Sockets.
C-kern/api/io/ip/ipaddr.hUse Internetprotocol-Address to construct ip-addresses.
C-kern/platform/Linux/io/ipsocket.cLinux specific implementation Internetprotocol-Sockets Linux.
Types
ipsocket_tDefines ipsocket_t as alias for sys_iochannel_t.
struct ipsocket_async_tExports ipsocket_async_t to asynchronously establish a connection.
Functions
test
unittest_io_ipsocketUnittest client/server tcp/udp communications.
ipsocket_t
lifetime
ipsocket_FREEStatic initializer for ipsocket_t.
init_ipsocketCreates a new unconnected UDP network communication endpoint.
initconnect_ipsocketCreates a connected network communication endpoint.
initlisten_ipsocketCreates a TCP server socket for accepting connections from peers (clients).
initaccept_ipsocketWaits for an incoming connection request from a peer (client).
free_ipsocketCloses communication channel and frees system resources.
query
io_ipsocketReturns the file descriptor sys_iochannel_t of the socket.
isconnected_ipsocketReturns if the socket is connected to a peer.
islisten_ipsocketReturns true if the socket listens for incoming connections.
protocol_ipsocketReturns protocol ipprotocol_e of the socket.
version_ipsocketReturns protocol version ipversion_e.
localaddr_ipsocketReturns local ip address in localaddr.
remoteaddr_ipsocketReturns remote ip address of peer in remoteaddr.
buffer management
bytestoread_ipsocketReturns the number of bytes in the receive queue.
bytestowrite_ipsocketReturns the number of bytes in the send queue.
queuesize_ipsocketReturns the buffer size in bytes.
setqueuesize_ipsocketChanges the size of the read and write queue.
connected io
read_ipsocketReads bytes from read queue at max maxdata_len bytes.
write_ipsocketTransfers maxdata_len bytes into internal write (send) queue.
readoob_ipsocketSame as read_ipsocket excepts returns additional OOB index.
writeoob_ipsocketWrites a single byte of “out of band” data (urgent data).
unconnected io
readfrom_ipsocketSame as read_ipsocket but for unconnected (UDP) sockets.
writeto_ipsocketSame as write_ipsocket but for unconnected (UDP) sockets.
ipsocket_async_tAllows to make a TCP connection in an async way.
ipsockA socket of type ipsocket_t.
errIndicates if operation has completed.
lifetime
ipsocket_async_FREEStatic initializer.
initconnect_ipsocketasyncSame as <initconnect_ipsocket> except for async operation.
free_ipsocketasyncCloses socket and frees all resources.
convert_ipsocketasyncConverts ipsocket_async_t into ipsocket_t.
query
success_ipsocketasyncReturns 0 if async connect completed.
waitms_ipsocketasyncWaits until operation completes.
inline implementation
Macros
io_ipsocketImplements ipsocket_t.io_ipsocket.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/io/ip/ipsocket.h

Header file of Internetprotocol-Sockets.

C-kern/api/io/ip/ipaddr.h

Use Internetprotocol-Address to construct ip-addresses.

C-kern/platform/Linux/io/ipsocket.c

Linux specific implementation Internetprotocol-Sockets Linux.

Types

ipsocket_t

typedef sys_iochannel_t ipsocket_t

Defines ipsocket_t as alias for sys_iochannel_t.  A socket is the abstraction of a network connection.  It is considered as the communication end point of one side.  Connected sockets can be considered like files except that written data is transported to another communication endpoint.

struct ipsocket_async_t

typedef struct ipsocket_async_t ipsocket_async_t

Exports ipsocket_async_t to asynchronously establish a connection.

Functions

Summary
test
unittest_io_ipsocketUnittest client/server tcp/udp communications.

test

unittest_io_ipsocket

int unittest_io_ipsocket(void)

Unittest client/server tcp/udp communications.

ipsocket_t

Summary
lifetime
ipsocket_FREEStatic initializer for ipsocket_t.
init_ipsocketCreates a new unconnected UDP network communication endpoint.
initconnect_ipsocketCreates a connected network communication endpoint.
initlisten_ipsocketCreates a TCP server socket for accepting connections from peers (clients).
initaccept_ipsocketWaits for an incoming connection request from a peer (client).
free_ipsocketCloses communication channel and frees system resources.
query
io_ipsocketReturns the file descriptor sys_iochannel_t of the socket.
isconnected_ipsocketReturns if the socket is connected to a peer.
islisten_ipsocketReturns true if the socket listens for incoming connections.
protocol_ipsocketReturns protocol ipprotocol_e of the socket.
version_ipsocketReturns protocol version ipversion_e.
localaddr_ipsocketReturns local ip address in localaddr.
remoteaddr_ipsocketReturns remote ip address of peer in remoteaddr.
buffer management
bytestoread_ipsocketReturns the number of bytes in the receive queue.
bytestowrite_ipsocketReturns the number of bytes in the send queue.
queuesize_ipsocketReturns the buffer size in bytes.
setqueuesize_ipsocketChanges the size of the read and write queue.
connected io
read_ipsocketReads bytes from read queue at max maxdata_len bytes.
write_ipsocketTransfers maxdata_len bytes into internal write (send) queue.
readoob_ipsocketSame as read_ipsocket excepts returns additional OOB index.
writeoob_ipsocketWrites a single byte of “out of band” data (urgent data).
unconnected io
readfrom_ipsocketSame as read_ipsocket but for unconnected (UDP) sockets.
writeto_ipsocketSame as write_ipsocket but for unconnected (UDP) sockets.

lifetime

ipsocket_FREE

#define ipsocket_FREE sys_iochannel_FREE

Static initializer for ipsocket_t.  Makes calling of free_ipsocket safe.

init_ipsocket

int init_ipsocket(/*out*/ipsocket_t *ipsock,
const struct ipaddr_t *localaddr)

Creates a new unconnected UDP network communication endpoint.  If localaddr does not describe an ipprotocol_UDP address EPROTONOSUPPORT is returned.  Unconnected sockets can be used to send and receive to/from a many network peers.

initconnect_ipsocket

int initconnect_ipsocket(/*out*/ipsocket_t *ipsock,  
const struct ipaddr_t *remoteaddr,  
const struct ipaddr_t *localaddr/*0 = newany_ipaddr*/)

Creates a connected network communication endpoint.  The socket has a local address described by localaddr and tries to establish a connection to a remote peer with its network address stored in remoteaddr.  The connection is only established if ipprotocol_TCP is used.  In case of ipprotocol_UDP the socket is configured to allow only to send to and receive from from a network peer with address remoteaddr.

EINVAL is returned if localaddr or remoteaddr is invalid or if they have different protocols.  Error code EAFNOSUPPORT indicates that ipversion_e from localaddr differs from remoteaddr.

Special value

In case localaddr is 0 it is set to newany_ipaddr with the same version and protocol as remoteaddr and the port number set to 0 (choose next free number).  After successfull connection the local address of the created ipsocket_t ipsock is assigned a free ip port and has an ip address of the network interface which connects to remoteaddr.

Performance

For TCP it is possible that this call needs some time to complete especially with a slow network connection.  So calling this function in a dedicated system worker thread is a good idea.

initlisten_ipsocket

int initlisten_ipsocket(/*out*/ipsocket_t *ipsock,
const struct ipaddr_t *localaddr,
uint16_t max_outstanding_connections)

Creates a TCP server socket for accepting connections from peers (clients).  The parameter max_outstanding_connections sets the number of connections which are established automatically from the underlying operating system without calling initaccept_ipsocket.  To establish a new connections call initaccept_ipsocket.

Returns EOPNOTSUPP if localaddr uses a protocol other than TCP.

initaccept_ipsocket

int initaccept_ipsocket(/*out*/ipsocket_t *ipsock,  
ipsocket_t *listensock,  
struct ipaddr_t *remoteaddr/*0 = > ignored*/)

Waits for an incoming connection request from a peer (client).  The parameter listensock must point to an ipsocket_t created with initlisten_ipsocket.  On successfull return the newly established connection is stored in ipsock.  The remote peer network address is returned in remoteaddr (if set != 0).

Returns

0Success
EINVALParameter listensock is of type ipprotocol_TCP but not a listen socket.
EOPNOTSUPPParameter listensock is not of type ipprotocol_TCP and does not support this operation.
EAFNOSUPPORTParameter remoteaddr does not have same IP version than listensock.

free_ipsocket

int free_ipsocket(/*out*/ipsocket_t *ipsock)

Closes communication channel and frees system resources.

query

io_ipsocket

sys_iochannel_t io_ipsocket(const ipsocket_t *ipsock)

Returns the file descriptor sys_iochannel_t of the socket.

isconnected_ipsocket

bool isconnected_ipsocket(const ipsocket_t *ipsock)

Returns if the socket is connected to a peer.  In case of an error false is returned.

islisten_ipsocket

bool islisten_ipsocket(const ipsocket_t *ipsock)

Returns true if the socket listens for incoming connections.  With this socket type you can wait for incoming connection requests from peers.  Use initaccept_ipsocket to query for neyl established connections.  In case of an error false is returned.

protocol_ipsocket

uint16_t protocol_ipsocket(const ipsocket_t *ipsock)

Returns protocol ipprotocol_e of the socket.  In case of error ipprotocol_ANY is returned.

version_ipsocket

uint16_t version_ipsocket(const ipsocket_t *ipsock)

Returns protocol version ipversion_e.  In case of an error ipversion_ANY is returned.

localaddr_ipsocket

int localaddr_ipsocket(const ipsocket_t *ipsock,
struct ipaddr_t *localaddr)

Returns local ip address in localaddr.  The parameter localaddr must have been allocated with <new_ipaddr> before this function is called.  Returns EAFNOSUPPORT in case ipversion of localaddr is not the same as of ipsock.

remoteaddr_ipsocket

int remoteaddr_ipsocket(const ipsocket_t *ipsock,
struct ipaddr_t *remoteaddr)

Returns remote ip address of peer in remoteaddr.  The parameter remoteaddr must have been allocated with <new_ipaddr> before this function is called.  Returns EAFNOSUPPORT in case ipversion of remoteaddr is not the same as of ipsock.

buffer management

bytestoread_ipsocket

int bytestoread_ipsocket(const ipsocket_t *ipsock,
/*out*/size_t *unread_bytes)

Returns the number of bytes in the receive queue.  They can be read without waiting.  If you try to read more byte than the returned value in unread_bytes the read operation will not block.  But it could be return less than unread_bytes bytes in case socket is of type TCP and a out-of-band (urgent) data byte is stored in the receive queue.

bytestowrite_ipsocket

int bytestowrite_ipsocket(const ipsocket_t *ipsock,
/*out*/size_t *unsend_bytes)

Returns the number of bytes in the send queue.  The number of bytes which can be written is the size of the send queue minus the returned value in unsend_bytes.  But the write operation is never blocking.  It writes only that much bytes as will fit into send queue.

queuesize_ipsocket

int queuesize_ipsocket(const ipsocket_t *ipsock,
/*out*/size_t *queuesize_read,
/*out*/size_t *queuesize_write)

Returns the buffer size in bytes.  queuesize_read gives the number of bytes which can be received without reading.  queuesize_write gives the number of bytes which can be written without sending.

setqueuesize_ipsocket

int setqueuesize_ipsocket(ipsocket_t *ipsock,  
size_t queuesize_read/*0 = > no change*/,
size_t queuesize_write/*0 = > no change*/)

Changes the size of the read and write queue.  To change only the read or write queue size set the other value to 0.

connected io

read_ipsocket

int read_ipsocket(ipsocket_t *ipsock,
size_t maxdata_len,
/*out*/uint8_t data[maxdata_len],
/*out*/size_t *bytes_read)

Reads bytes from read queue at max maxdata_len bytes.  This call returns EAGAIN if the read queue is empty.  It returns less than maxdata_len bytes if the queue contains less bytes.

write_ipsocket

int write_ipsocket(ipsocket_t *ipsock,
size_t maxdata_len,
const uint8_t data[maxdata_len],
/*out*/size_t *bytes_written)

Transfers maxdata_len bytes into internal write (send) queue.  This call returns EAGAIN if the write queue is full.  It writes less than maxdata_len bytes if the queue contains less free space.

readoob_ipsocket

int readoob_ipsocket(ipsocket_t *ipsock,
size_t maxdata_len,
/*out*/uint8_t data[maxdata_len],
/*out*/size_t *bytes_read,
/*out*/size_t *oob_offset)

Same as read_ipsocket excepts returns additional OOB index.  This index indicates if there is “out of band” data in the stream.  oob_offset is set to maxdata_len if there is no oob data byte in the stream.  If there is a oob data byte stored in the read queue oob_offset points to it (data[oob_offset]) and has a value smaller than maxdata_len.

OODB_INLINE

This option is set during init_ipsocket and all oob-data could also be read with a normal read_ipsocket.  The difference is that read_ipsocket returns less data than possible (see bytestoread_ipsocket) if it encounters an oob byte.  And only the next call to read_ipsocket will return the oob byte and any following data.  readoob_ipsocket will handle this for you and returns an index which indicates if an oob was read.

writeoob_ipsocket

int writeoob_ipsocket(ipsocket_t *ipsock,
uint8_t data)

Writes a single byte of “out of band” data (urgent data).  This operation is only supported by TCP and on an UDP socket EOPNOTSUPP is returned.  If you call it more than once and if the previous oob-data byte was not read by the receiver the previous written oob byte will loose its special oob state and is returned as normal data.

unconnected io

readfrom_ipsocket

int readfrom_ipsocket(ipsocket_t *ipsock,
struct ipaddr_t *remoteaddr,
size_t maxdata_len,
/*out*/uint8_t data[maxdata_len],
/*out*/size_t *bytes_read)

Same as read_ipsocket but for unconnected (UDP) sockets.  In addition to the read bytes the address of the sender is also returned in remoteaddr.  Error code EAFNOSUPPORT is returned in case remoteaddr is not the same ipversion_e as ipsocket_t.

writeto_ipsocket

int writeto_ipsocket(ipsocket_t *ipsock,
const struct ipaddr_t *remoteaddr,
size_t maxdata_len,
const uint8_t data[maxdata_len],
/*out*/size_t *bytes_written)

Same as write_ipsocket but for unconnected (UDP) sockets.  In addition to bytes to write the caller has to supply the network address of the receiver.

Returns

0Success.
EAFNOSUPPORTremoteaddr is not the same ipversion_e as ipsocket_t.
EPROTONOSUPPORTremoteaddr describes no ipprotocol_UDP address.

ipsocket_async_t

struct ipsocket_async_t

Allows to make a TCP connection in an async way.  Call to initconnect_ipsocketasync to start operation.  To check if operation has completed call success_ipsocketasync.  In case of success call convert_ipsocketasync to complete the cycle.  If you want to abort the operation call free_ipsocketasync.  If you want to wait for completion call waitms_ipsocketasync.

Summary
ipsockA socket of type ipsocket_t.
errIndicates if operation has completed.
lifetime
ipsocket_async_FREEStatic initializer.
initconnect_ipsocketasyncSame as <initconnect_ipsocket> except for async operation.
free_ipsocketasyncCloses socket and frees all resources.
convert_ipsocketasyncConverts ipsocket_async_t into ipsocket_t.
query
success_ipsocketasyncReturns 0 if async connect completed.
waitms_ipsocketasyncWaits until operation completes.

ipsock

ipsocket_t ipsock

A socket of type ipsocket_t.  It describes the network connection.

err

int err

Indicates if operation has completed.

Values

0Operation has completed with no error.
EINPROGRESSOperation has not completed.
other valueOperation has completed with this error.

lifetime

ipsocket_async_FREE

#define ipsocket_async_FREE { ipsocket_FREE, 0 }

Static initializer.

initconnect_ipsocketasync

int initconnect_ipsocketasync(/*out*/ipsocket_async_t *ipsockasync,
const struct ipaddr_t *remoteaddr,
const struct ipaddr_t *localaddr)

Same as <initconnect_ipsocket> except for async operation.  Call success_ipsocketasync to check if connect request operation has completed.

free_ipsocketasync

int free_ipsocketasync(ipsocket_async_t *ipsockasync)

Closes socket and frees all resources.  Any pending connection operations are aborted.

convert_ipsocketasync

int convert_ipsocketasync(ipsocket_async_t *ipsockasync,
/*out*/ipsocket_t *ipsock)

Converts ipsocket_async_t into ipsocket_t.

In case of success (returns 0) ipsockasync is reset to ipsocket_async_FREE and ipsock contains a connected socket ready for read/write operations.

In case success_ipsocketasync does not return 0 this same error code is returned and nothing is done.

query

success_ipsocketasync

int success_ipsocketasync(ipsocket_async_t *ipsockasync)

Returns 0 if async connect completed.  Returns EINPROGRESS if the operation is not completed yet.  Another value != 0 indicates that the operation completed with an error.

waitms_ipsocketasync

int waitms_ipsocketasync(ipsocket_async_t *ipsockasync,  
uint32_t millisec/*0 = > no timeout*/)

Waits until operation completes.  Returns EINPROGRESS in case of timeout and 0 in case operation completed.  Call success_ipsocketasync to check if an error occurred.

inline implementation

Macros

io_ipsocket

#define io_ipsocket(ipsock) (*(ipsock))

Implements ipsocket_t.io_ipsocket.

ALlows to connect & communicate to other systems via TCP or UDP.
Impplements Internetprotocol-Sockets on Linux.
typedef sys_iochannel_t ipsocket_t
Defines ipsocket_t as alias for sys_iochannel_t.
#define sys_iochannel_t int
Choose Posix file descriptor type.
typedef struct ipsocket_async_t ipsocket_async_t
Exports ipsocket_async_t to asynchronously establish a connection.
struct ipsocket_async_t
Allows to make a TCP connection in an async way.
int unittest_io_ipsocket(void)
Unittest client/server tcp/udp communications.
#define ipsocket_FREE sys_iochannel_FREE
Static initializer for ipsocket_t.
int init_ipsocket(/*out*/ipsocket_t *ipsock,
const struct ipaddr_t *localaddr)
Creates a new unconnected UDP network communication endpoint.
int initconnect_ipsocket(/*out*/ipsocket_t *ipsock,  
const struct ipaddr_t *remoteaddr,  
const struct ipaddr_t *localaddr/*0 = newany_ipaddr*/)
Creates a connected network communication endpoint.
int initlisten_ipsocket(/*out*/ipsocket_t *ipsock,
const struct ipaddr_t *localaddr,
uint16_t max_outstanding_connections)
Creates a TCP server socket for accepting connections from peers (clients).
int initaccept_ipsocket(/*out*/ipsocket_t *ipsock,  
ipsocket_t *listensock,  
struct ipaddr_t *remoteaddr/*0 = > ignored*/)
Waits for an incoming connection request from a peer (client).
int free_ipsocket(/*out*/ipsocket_t *ipsock)
Closes communication channel and frees system resources.
sys_iochannel_t io_ipsocket(const ipsocket_t *ipsock)
Returns the file descriptor sys_iochannel_t of the socket.
bool isconnected_ipsocket(const ipsocket_t *ipsock)
Returns if the socket is connected to a peer.
bool islisten_ipsocket(const ipsocket_t *ipsock)
Returns true if the socket listens for incoming connections.
uint16_t protocol_ipsocket(const ipsocket_t *ipsock)
Returns protocol ipprotocol_e of the socket.
Currently supported internet protocols.
uint16_t version_ipsocket(const ipsocket_t *ipsock)
Returns protocol version ipversion_e.
Selects the version of the ip address.
int localaddr_ipsocket(const ipsocket_t *ipsock,
struct ipaddr_t *localaddr)
Returns local ip address in localaddr.
int remoteaddr_ipsocket(const ipsocket_t *ipsock,
struct ipaddr_t *remoteaddr)
Returns remote ip address of peer in remoteaddr.
int bytestoread_ipsocket(const ipsocket_t *ipsock,
/*out*/size_t *unread_bytes)
Returns the number of bytes in the receive queue.
int bytestowrite_ipsocket(const ipsocket_t *ipsock,
/*out*/size_t *unsend_bytes)
Returns the number of bytes in the send queue.
int queuesize_ipsocket(const ipsocket_t *ipsock,
/*out*/size_t *queuesize_read,
/*out*/size_t *queuesize_write)
Returns the buffer size in bytes.
int setqueuesize_ipsocket(ipsocket_t *ipsock,  
size_t queuesize_read/*0 = > no change*/,
size_t queuesize_write/*0 = > no change*/)
Changes the size of the read and write queue.
int read_ipsocket(ipsocket_t *ipsock,
size_t maxdata_len,
/*out*/uint8_t data[maxdata_len],
/*out*/size_t *bytes_read)
Reads bytes from read queue at max maxdata_len bytes.
int write_ipsocket(ipsocket_t *ipsock,
size_t maxdata_len,
const uint8_t data[maxdata_len],
/*out*/size_t *bytes_written)
Transfers maxdata_len bytes into internal write (send) queue.
int readoob_ipsocket(ipsocket_t *ipsock,
size_t maxdata_len,
/*out*/uint8_t data[maxdata_len],
/*out*/size_t *bytes_read,
/*out*/size_t *oob_offset)
Same as read_ipsocket excepts returns additional OOB index.
int writeoob_ipsocket(ipsocket_t *ipsock,
uint8_t data)
Writes a single byte of “out of band” data (urgent data).
int readfrom_ipsocket(ipsocket_t *ipsock,
struct ipaddr_t *remoteaddr,
size_t maxdata_len,
/*out*/uint8_t data[maxdata_len],
/*out*/size_t *bytes_read)
Same as read_ipsocket but for unconnected (UDP) sockets.
int writeto_ipsocket(ipsocket_t *ipsock,
const struct ipaddr_t *remoteaddr,
size_t maxdata_len,
const uint8_t data[maxdata_len],
/*out*/size_t *bytes_written)
Same as write_ipsocket but for unconnected (UDP) sockets.
ipsocket_t ipsock
A socket of type ipsocket_t.
int err
Indicates if operation has completed.
#define ipsocket_async_FREE { ipsocket_FREE, 0 }
Static initializer.
int initconnect_ipsocketasync(/*out*/ipsocket_async_t *ipsockasync,
const struct ipaddr_t *remoteaddr,
const struct ipaddr_t *localaddr)
Same as initconnect_ipsocket except for async operation.
int free_ipsocketasync(ipsocket_async_t *ipsockasync)
Closes socket and frees all resources.
int convert_ipsocketasync(ipsocket_async_t *ipsockasync,
/*out*/ipsocket_t *ipsock)
Converts ipsocket_async_t into ipsocket_t.
int success_ipsocketasync(ipsocket_async_t *ipsockasync)
Returns 0 if async connect completed.
int waitms_ipsocketasync(ipsocket_async_t *ipsockasync,  
uint32_t millisec/*0 = > no timeout*/)
Waits until operation completes.
#define io_ipsocket(ipsock) (*(ipsock))
Implements ipsocket_t.io_ipsocket.
Use unreliable datagram protocol to send/receive messages.
Use reliable transmission control protocol to send/receive messages.
Only useful as a filter argument to newdnsquery_ipaddrlist.
Only useful as a filter argument to newdnsquery_ipaddrlist.
Close