MQTT C Client Libraries Internals
Macros | Functions | Variables
Socket.c File Reference

Socket related functions. More...

#include "Socket.h"
#include "Log.h"
#include "SocketBuffer.h"
#include "Messages.h"
#include "StackTrace.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include "Heap.h"
Include dependency graph for Socket.c:

Macros

#define ADDRLEN   INET6_ADDRSTRLEN+1
 
#define PORTLEN   10
 

Functions

int isReady (int index)
 Don't accept work from a client unless it is accepting work back, i.e. More...
 
int Socket_continueWrites (SOCKET *sock, mutex_type mutex)
 Continue any outstanding socket writes. More...
 
int Socket_setnonblocking (SOCKET sock)
 Set a socket non-blocking, OS independently. More...
 
int Socket_error (char *aString, SOCKET sock)
 Gets the specific error corresponding to SOCKET_ERROR. More...
 
int Socket_addSocket (SOCKET newSd)
 Add a socket to the list of socket to check with select. More...
 
int Socket_writev (SOCKET socket, iobuf *iovecs, int count, unsigned long *bytes)
 Attempts to write a series of iovec buffers to a socket in one system call so that they are sent as one packet. More...
 
int Socket_close_only (SOCKET socket)
 Close a socket without removing it from the select list. More...
 
int Socket_continueWrite (SOCKET socket)
 Continue an outstanding write for a particular socket. More...
 
char * Socket_getaddrname (struct sockaddr *sa, SOCKET sock)
 Convert a numeric address to character string. More...
 
int Socket_abortWrite (SOCKET socket)
 Continue an outstanding write for a particular socket. More...
 
void Socket_outInitialize (void)
 Initialize the socket module.
 
void Socket_outTerminate (void)
 Terminate the socket module.
 
static int cmpfds (const void *p1, const void *p2)
 
static int cmpsockfds (const void *p1, const void *p2)
 
SOCKET Socket_getReadySocket (int more_work, int timeout, mutex_type mutex, int *rc)
 Returns the next socket ready for communications as indicated by select. More...
 
int Socket_getch (SOCKET socket, char *c)
 Reads one byte from a socket. More...
 
char * Socket_getdata (SOCKET socket, size_t bytes, size_t *actual_len, int *rc)
 Attempts to read a number of bytes from a socket, non-blocking. More...
 
int Socket_noPendingWrites (SOCKET socket)
 Indicate whether any data is pending outbound for a socket. More...
 
int Socket_putdatas (SOCKET socket, char *buf0, size_t buf0len, PacketBuffers bufs)
 Attempts to write a series of buffers to a socket in one system call so that they are sent as one packet. More...
 
void Socket_addPendingWrite (SOCKET socket)
 Add a socket to the pending write list, so that it is checked for writing in select. More...
 
void Socket_clearPendingWrite (SOCKET socket)
 Clear a socket from the pending write list - if one was added with Socket_addPendingWrite. More...
 
int Socket_close (SOCKET socket)
 Close a socket and remove it from the select list. More...
 
int Socket_new (const char *addr, size_t addr_len, int port, SOCKET *sock)
 Create a new socket and TCP connect to an address/port. More...
 
void Socket_setWriteContinueCallback (Socket_writeContinue *mywritecontinue)
 
void Socket_setWriteCompleteCallback (Socket_writeComplete *mywritecomplete)
 
void Socket_setWriteAvailableCallback (Socket_writeAvailable *mywriteavailable)
 
char * Socket_getpeer (SOCKET sock)
 Get information about the other end connected to a socket. More...
 

Variables

Sockets mod_s
 Structure to hold all socket data for this module.
 
static Socket_writeContinue * writecontinue = NULL
 
static Socket_writeComplete * writecomplete = NULL
 
static Socket_writeAvailable * writeAvailable = NULL
 

Detailed Description

Socket related functions.

Some other related functions are in the SocketBuffer module

Function Documentation

◆ isReady()

int isReady ( int  index)

Don't accept work from a client unless it is accepting work back, i.e.

its socket is writeable this seems like a reasonable form of flow control, and practically, seems to work.

Parameters
indexthe socket index to check
Returns
boolean - is the socket ready to go?
Here is the call graph for this function:

◆ Socket_abortWrite()

int Socket_abortWrite ( SOCKET  socket)

Continue an outstanding write for a particular socket.

Parameters
socketthat socket
Returns
completion code: 0=incomplete, 1=complete, -1=socket error
Here is the call graph for this function:

◆ Socket_addPendingWrite()

void Socket_addPendingWrite ( SOCKET  socket)

Add a socket to the pending write list, so that it is checked for writing in select.

This is used in connect processing when the TCP connect is incomplete, as we need to check the socket for both ready to read and write states.

Parameters
socketthe socket to add

◆ Socket_addSocket()

int Socket_addSocket ( SOCKET  newSd)

Add a socket to the list of socket to check with select.

Parameters
newSdthe new socket to add

◆ Socket_clearPendingWrite()

void Socket_clearPendingWrite ( SOCKET  socket)

Clear a socket from the pending write list - if one was added with Socket_addPendingWrite.

Parameters
socketthe socket to remove

◆ Socket_close()

int Socket_close ( SOCKET  socket)

Close a socket and remove it from the select list.

Parameters
socketthe socket to close
Returns
completion code
Here is the call graph for this function:

◆ Socket_close_only()

int Socket_close_only ( SOCKET  socket)

Close a socket without removing it from the select list.

Parameters
socketthe socket to close
Returns
completion code
Here is the call graph for this function:

◆ Socket_continueWrite()

int Socket_continueWrite ( SOCKET  socket)

Continue an outstanding write for a particular socket.

Parameters
socketthat socket
Returns
completion code: 0=incomplete, 1=complete, -1=socket error
Here is the call graph for this function:

◆ Socket_continueWrites()

int Socket_continueWrites ( SOCKET *  sock,
mutex_type  mutex 
)

Continue any outstanding socket writes.

Parameters
sockin case of a socket error contains the affected socket
Returns
completion code, 0 or SOCKET_ERROR
Here is the call graph for this function:

◆ Socket_error()

int Socket_error ( char *  aString,
SOCKET  sock 
)

Gets the specific error corresponding to SOCKET_ERROR.

Parameters
aStringthe function that was being used when the error occurred
sockthe socket on which the error occurred
Returns
the specific TCP error code
Here is the call graph for this function:

◆ Socket_getaddrname()

char * Socket_getaddrname ( struct sockaddr *  sa,
SOCKET  sock 
)

Convert a numeric address to character string.

Parameters
sasocket numerical address
socksocket
Returns
the peer information

maximum length of the address string

maximum length of the port string

◆ Socket_getch()

int Socket_getch ( SOCKET  socket,
char *  c 
)

Reads one byte from a socket.

Parameters
socketthe socket to read from
cthe character read, returned
Returns
completion code
Here is the call graph for this function:

◆ Socket_getdata()

char* Socket_getdata ( SOCKET  socket,
size_t  bytes,
size_t *  actual_len,
int *  rc 
)

Attempts to read a number of bytes from a socket, non-blocking.

If a previous read did not finish, then retrieve that data.

Parameters
socketthe socket to read from
bytesthe number of bytes to read
actual_lenthe actual number of bytes read
Returns
completion code
Here is the call graph for this function:

◆ Socket_getpeer()

char* Socket_getpeer ( SOCKET  sock)

Get information about the other end connected to a socket.

Parameters
sockthe socket to inquire on
Returns
the peer information
Here is the call graph for this function:

◆ Socket_getReadySocket()

SOCKET Socket_getReadySocket ( int  more_work,
int  timeout,
mutex_type  mutex,
int *  rc 
)

Returns the next socket ready for communications as indicated by select.

Parameters
more_workflag to indicate more work is waiting, and thus a timeout value of 0 should be used for the select
timeoutthe timeout to be used in ms
rca value other than 0 indicates an error of the returned socket
Returns
the socket next ready, or 0 if none is ready
Here is the call graph for this function:

◆ Socket_new()

int Socket_new ( const char *  addr,
size_t  addr_len,
int  port,
SOCKET *  sock 
)

Create a new socket and TCP connect to an address/port.

Parameters
addrthe address string
portthe TCP port
sockreturns the new socket
timeoutthe timeout in milliseconds
Returns
completion code 0=good, SOCKET_ERROR=fail
Here is the call graph for this function:

◆ Socket_noPendingWrites()

int Socket_noPendingWrites ( SOCKET  socket)

Indicate whether any data is pending outbound for a socket.

Returns
boolean - true == no pending data.
Here is the call graph for this function:

◆ Socket_putdatas()

int Socket_putdatas ( SOCKET  socket,
char *  buf0,
size_t  buf0len,
PacketBuffers  bufs 
)

Attempts to write a series of buffers to a socket in one system call so that they are sent as one packet.

Parameters
socketthe socket to write to
buf0the first buffer
buf0lenthe length of data in the first buffer
countnumber of buffers
buffersan array of buffers to write
buflensan array of corresponding buffer lengths
Returns
completion code, especially TCPSOCKET_INTERRUPTED
Here is the call graph for this function:

◆ Socket_setnonblocking()

int Socket_setnonblocking ( SOCKET  sock)

Set a socket non-blocking, OS independently.

Parameters
sockthe socket to set non-blocking
Returns
TCP call error code

◆ Socket_writev()

int Socket_writev ( SOCKET  socket,
iobuf *  iovecs,
int  count,
unsigned long *  bytes 
)

Attempts to write a series of iovec buffers to a socket in one system call so that they are sent as one packet.

Parameters
socketthe socket to write to
iovecsan array of buffers to write
countnumber of buffers in iovecs
bytesnumber of bytes actually written returned
Returns
completion code, especially TCPSOCKET_INTERRUPTED
Here is the call graph for this function: