#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include "xssocket.h"
Go to the source code of this file.
Classes | |
struct | XsSocketPrivate |
Macros | |
#define | INVALID_SOCKET ((int32_t)-1) |
#define | MSG_NOSIGNAL 0 |
#define | PEEKBUFSIZE 32768 |
#define | SOCKET int32_t |
#define | SOCKET_ERROR -1 |
Typedefs | |
typedef int(* | lookupTestFunction) (XsSocket *thisPtr, SOCKET currentSocket, struct addrinfo const *info) |
Functions | |
static int | binder (XsSocket *thisPtr, SOCKET currentSocket, struct addrinfo const *info) |
static int | closesocket (SOCKET s) |
static int | connector (XsSocket *thisPtr, SOCKET currentSocket, struct addrinfo const *info) |
static int | defaultLookupTest (XsSocket *thisPtr, SOCKET currentSocket, struct addrinfo const *info) |
static void | getRemoteHostAddress (const struct sockaddr_storage *remote, XsString *address) |
Get the IP address of remote. More... | |
int | isIPv4Address (XsString const *hostname) |
static int | peekPendingDataSize (XsSocket *thisPtr) |
static XsResultValue | setLastResult (XsSocket *thisPtr, XsResultValue retval, int systemError) |
static int | socketIsUsable (SOCKET s) |
static XsResultValue | translateAndReturnSocketError (XsSocket *thisPtr, int functionResult) |
static void | translateSocketError (XsSocket *thisPtr, int functionResult) |
XsSocket * | XsSocket_accept (XsSocket *thisPtr, int mstimeout) |
Accept an incoming connection. More... | |
XsSocket * | XsSocket_allocate () |
Dynamically allocate an XsSocket. More... | |
XsResultValue | XsSocket_bind (XsSocket *thisPtr, const XsString *hostname, uint16_t port) |
Bind to the hostname and port combination. More... | |
int | XsSocket_broadcast (XsSocket *thisPtr, const void *data, XsSize size, uint16_t port) |
Broadcast data over the socket to the port indicated by port. More... | |
XsResultValue | XsSocket_connect (XsSocket *thisPtr, const XsString *host, uint16_t port) |
Connect to hostame on port. More... | |
void | XsSocket_create (XsSocket *thisPtr, enum NetworkLayerProtocol ip, enum IpProtocol protocol) |
Create socket thisPtr for IP version ip with protocol protocol. More... | |
void | XsSocket_createFromNativeSocket (XsSocket *thisPtr, SOCKET nativeSocket, struct sockaddr const *theirInfo, socklen_t infolen, XsDataFlags flags) |
void | XsSocket_destroy (XsSocket *thisPtr) |
Destroy the given socket. More... | |
void | XsSocket_fixupHostname (XsSocket const *thisPtr, XsString *hostname) |
void | XsSocket_flush (XsSocket *thisPtr) |
Flush the incoming data. More... | |
void | XsSocket_free (XsSocket *thisPtr) |
Free a socket returned from XsSocket_accept() or XsSocket_allocate() More... | |
int | XsSocket_getLastSystemError (const XsSocket *thisPtr) |
Return the system error code of the last socket operation. More... | |
void | XsSocket_getRemoteAddress (const XsSocket *thisPtr, XsString *address) |
Get the numeric IP address of remote host of this socket. More... | |
static void | XsSocket_initialize (XsSocket *thisPtr, XsDataFlags flags) |
static XsResultValue | XsSocket_internalLookup (XsSocket *thisPtr, const XsString *hostname, uint16_t port, int hints_flags, lookupTestFunction tester, struct sockaddr *info, socklen_t *addrlen) |
int | XsSocket_isUsable (const XsSocket *thisPtr) |
Return non-zero if the socket is usable for reading and writing data. More... | |
XsResultValue | XsSocket_listen (XsSocket *thisPtr, int maxPending) |
Start listening for incoming connections on this socket. More... | |
static XsResultValue | XsSocket_lookup (XsSocket *thisPtr, const XsString *hostname, uint16_t port, struct sockaddr *info, socklen_t *addrlen) |
Perform a lookup. More... | |
XSOCKET | XsSocket_nativeDescriptor (XsSocket const *thisPtr) |
Return the native file descriptor. More... | |
int | XsSocket_read (XsSocket *thisPtr, void *dest, XsSize size, int timeout) |
Read size data into dest. More... | |
int | XsSocket_read2ByteArray (XsSocket *thisPtr, XsByteArray *dest, int timeout) |
Read size data into dest. More... | |
int | XsSocket_readFrom (XsSocket *thisPtr, void *dest, XsSize size, XsString *hostname, uint16_t *port, int timeout) |
Read size data into dest. More... | |
int | XsSocket_readFrom2ByteArray (XsSocket *thisPtr, XsByteArray *dest, XsString *hostname, uint16_t *port, int timeout) |
Read size data into dest. More... | |
int | XsSocket_write (XsSocket *thisPtr, const void *data, XsSize size) |
Write data to the socket. More... | |
int | XsSocket_writeTo (XsSocket *thisPtr, const void *data, XsSize size, const XsString *hostname, uint16_t port) |
Write data to the socket to the host indicated by hostname, port. More... | |
#define INVALID_SOCKET ((int32_t)-1) |
Definition at line 85 of file xssocket.c.
#define MSG_NOSIGNAL 0 |
Definition at line 102 of file xssocket.c.
#define PEEKBUFSIZE 32768 |
Definition at line 108 of file xssocket.c.
#define SOCKET int32_t |
Definition at line 83 of file xssocket.c.
#define SOCKET_ERROR -1 |
Definition at line 84 of file xssocket.c.
typedef int(* lookupTestFunction) (XsSocket *thisPtr, SOCKET currentSocket, struct addrinfo const *info) |
Definition at line 728 of file xssocket.c.
Definition at line 1188 of file xssocket.c.
|
static |
Definition at line 88 of file xssocket.c.
|
static |
Definition at line 1269 of file xssocket.c.
|
static |
Definition at line 839 of file xssocket.c.
|
static |
Get the IP address of remote.
[in] | remote | the socket address info of the remote peer |
[in,out] | address | XsString to return the host IP address in. |
Definition at line 145 of file xssocket.c.
int isIPv4Address | ( | XsString const * | hostname | ) |
Definition at line 677 of file xssocket.c.
|
static |
Definition at line 551 of file xssocket.c.
|
static |
Definition at line 171 of file xssocket.c.
|
static |
Definition at line 134 of file xssocket.c.
|
static |
Definition at line 185 of file xssocket.c.
|
static |
Definition at line 324 of file xssocket.c.
Accept an incoming connection.
This function requires that XsSocket_listen() has already been called. The returned pointer should be freed using XsSocket_freeAcceptedSocket(). \param[in] mstimeout the timeout in milliseconds \returns a pointer to a newly created socket for the new connection. NULL on error. \relates XsSocket
Definition at line 1084 of file xssocket.c.
XsSocket* XsSocket_allocate | ( | ) |
Dynamically allocate an XsSocket.
After calling this function, it is still required to call XsSocket_create or the internal function XsSocket_createFromNativeSocket Use XsSocket_free to safely free the returned XsSocket. \returns a pointer to a newly allocated XsSocket, or NULL on error
Definition at line 1126 of file xssocket.c.
XsResultValue XsSocket_bind | ( | XsSocket * | thisPtr, |
const XsString * | hostname, | ||
uint16_t | port | ||
) |
Bind to the hostname and port combination.
\param[in] hostname the hostname to bind to, may be NULL, in which we will bind to any address \param[in] port the port to bind to. Must be a value above 1023, unless we have elevated privileges \return an XsResultValue indicating the result of the operation, possibly pointing towards a cause \relates XsSocket
Definition at line 1220 of file xssocket.c.
Broadcast data over the socket to the port indicated by port.
\param[in] data the data to write \param[in] size the size of the data \param[in] port the port to send data to \returns the number of bytes written, -1 on error, depending on whether the majority of sub broadcasts succeeded or failed. \relates XsSocket
Definition at line 924 of file xssocket.c.
XsResultValue XsSocket_connect | ( | XsSocket * | thisPtr, |
const XsString * | host, | ||
uint16_t | port | ||
) |
Connect to hostame on port.
\param[in] host the hostname to connect to \param[in] port the port to connect to \returns an XsResultValue indicating the result of the operation \relates XsSocket
Definition at line 1296 of file xssocket.c.
void XsSocket_create | ( | XsSocket * | thisPtr, |
enum NetworkLayerProtocol | ip, | ||
enum IpProtocol | protocol | ||
) |
Create socket thisPtr for IP version ip with protocol protocol.
\param[in] ip the IP version \param[in] protocol the protocol. Currently supported are TCP and UDP \relates XsSocket
Definition at line 350 of file xssocket.c.
void XsSocket_createFromNativeSocket | ( | XsSocket * | thisPtr, |
SOCKET | nativeSocket, | ||
struct sockaddr const * | theirInfo, | ||
socklen_t | infolen, | ||
XsDataFlags | flags | ||
) |
Definition at line 369 of file xssocket.c.
void XsSocket_destroy | ( | XsSocket * | thisPtr | ) |
Destroy the given socket.
After calling this function, the XsSocket will no longer be usable for socket communications. It is safe to call this function twice on the same XsSocket. If the socket was created from a file descriptor using XsSocket_createFromFileDescriptor and had the XSDF_Managed flag set then the underlying socket will be closed. \relates XsSocket
Definition at line 457 of file xssocket.c.
Definition at line 713 of file xssocket.c.
void XsSocket_flush | ( | XsSocket * | thisPtr | ) |
Flush the incoming data.
Flush all data from the incoming buffer. \relates XsSocket
Definition at line 1063 of file xssocket.c.
void XsSocket_free | ( | XsSocket * | thisPtr | ) |
Free a socket returned from XsSocket_accept() or XsSocket_allocate()
Use this function only on sockets returned from XsSocket_accept(). This function calls XsSocket_destroy() before freeing the memory. \see XsSocket_destroy \relates XsSocket
Definition at line 1140 of file xssocket.c.
int XsSocket_getLastSystemError | ( | const XsSocket * | thisPtr | ) |
Return the system error code of the last socket operation.
Definition at line 1324 of file xssocket.c.
Get the numeric IP address of remote host of this socket.
[in,out] | address | XsString to return the remote host IP address in |
Definition at line 1315 of file xssocket.c.
|
static |
Definition at line 333 of file xssocket.c.
|
static |
Definition at line 744 of file xssocket.c.
int XsSocket_isUsable | ( | const XsSocket * | thisPtr | ) |
Return non-zero if the socket is usable for reading and writing data.
\returns non-zero if the socket is usable for reading and writing data, zero otherwise \relates XsSocket
Definition at line 1306 of file xssocket.c.
XsResultValue XsSocket_listen | ( | XsSocket * | thisPtr, |
int | maxPending | ||
) |
Start listening for incoming connections on this socket.
\param[in] maxPending the maximum amount of pending connections \return an XsResultValue indicating the result of the operation \relates XsSocket
Definition at line 1259 of file xssocket.c.
|
static |
Perform a lookup.
Definition at line 846 of file xssocket.c.
Return the native file descriptor.
The native socket descriptor returned from this function should only be passed to third party libraries. However, it is possible to select, read and write on it, if you know what you're doing. Do remember that this object still manages the lifetime of the file descriptor.
Definition at line 422 of file xssocket.c.
Read size data into dest.
\param[in,out] dest a pointer to a data buffer \param[in] size the size of the buffer \a dest points to \param[in] timeout the amount of time in milliseconds to wait for data \returns the size of the data read from the socket, -1 on error. \relates XsSocket
Definition at line 545 of file xssocket.c.
int XsSocket_read2ByteArray | ( | XsSocket * | thisPtr, |
XsByteArray * | dest, | ||
int | timeout | ||
) |
Read size data into dest.
\param[in,out] dest a pointer to a data buffer XsByteArray, may be NULL, in which case only the size of the pending data is reported. \param[in] timeout the amount of time in milliseconds to wait for data \returns the size of the data read from the socket, -1 on error. \relates XsSocket
Definition at line 609 of file xssocket.c.
int XsSocket_readFrom | ( | XsSocket * | thisPtr, |
void * | dest, | ||
XsSize | size, | ||
XsString * | hostname, | ||
uint16_t * | port, | ||
int | timeout | ||
) |
Read size data into dest.
\param[in,out] dest a pointer to a data buffer, may be NULL, in which case only the size of the pending data is reported. \param[in] size the size of the buffer \a dest points to \param[in,out] hostname a pointer to a string that contains the hostname of the sender after returning from this function. May be NULL. \param[in,out] port a pointer that will contain the port of the sender after returning from this function. May be NULL. \param[in] timeout the amount of time in milliseconds to wait for data \returns the size of the data read from the socket, -1 on error. \relates XsSocket
Definition at line 574 of file xssocket.c.
int XsSocket_readFrom2ByteArray | ( | XsSocket * | thisPtr, |
XsByteArray * | dest, | ||
XsString * | hostname, | ||
uint16_t * | port, | ||
int | timeout | ||
) |
Read size data into dest.
\param[in,out] dest a pointer to a data buffer XsByteArray, may be NULL, in which case only the size of the pending data is reported. \param[in,out] hostname a pointer to a string that contains the hostname of the sender after returning from this function. May be NULL. \param[in,out] port a pointer that will contain the port of the sender after returning from this function. May be NULL. \param[in] timeout the amount of time in milliseconds to wait for data \returns the size of the data read from the socket, -1 on error. \relates XsSocket
Definition at line 624 of file xssocket.c.
Write data to the socket.
\param[in] data a pointer to the data to write \param[in] size the size of the data to write \returns the number of bytes written, -1 or 0 on error \relates XsSocket
Definition at line 665 of file xssocket.c.
int XsSocket_writeTo | ( | XsSocket * | thisPtr, |
const void * | data, | ||
XsSize | size, | ||
const XsString * | hostname, | ||
uint16_t | port | ||
) |
Write data to the socket to the host indicated by hostname, port.
\param[in] data the data to write \param[in] size the size of the data \param[in] hostname the name of the host to send data to \param[in] port the port to send data to \returns the number of bytes written, -1 on error. \relates XsSocket
Definition at line 861 of file xssocket.c.