net.h File Reference

Go to the source code of this file.

Defines

#define SELECT_TIMEOUT   5
#define LINGER_TIMEOUT   10
#define TRUE   1
#define FALSE   0

Functions

int check_connect (char *, int, int)
 Test INET Socket connection to host on port with the specified socket protocol type.

int check_host (char *)
 Check if the hostname resolves.

int check_connection_io (Port_T)
 Verify that the socket connection is ready for i|o.

int check_socket (int)
 Verify that the socket is ready for i|o.

int check_udp_socket (int)
 Verify that the udp socket is ready for i|o.

int create_socket (char *, int, int)
 Open a socket against hostname:port with the given protocol.

int create_generic_socket (Port_T)
 Open a socket with given Port_T structure The protocol, destination and type are selected appropriately.

int create_unix_socket (char *, int)
 Open a socket against pathname with the given protocol.

int create_server_socket (int, int, char *bindAddr)
 Creates a server socket (SOCK_STREAM type) and binds it to the specified local port number, with the specified backlog.

int close_socket (int)
 Shutdown a socket and close the descriptor.

int set_sotimeout (int, int)
 Set a so_timeout (read/write) for the given socket with the specified timeout in seconds.

int set_noblock (int)
 Enable nonblocking i|o on the given socket.

int set_block (int)
 Disable nonblocking i|o on the given socket.

int sock_send (int, const char *, int, int)
 Send the message 'msg' down the socket in non-blocking mode and with failover.

int sock_recv (int, char *, int, int)
 Read a message from the socket in non-blocking mode and with failover.

int port_send (Port_T p, const char *, int, int)
 Send the message 'msg' down a port in non-blocking mode and with failover.

int port_recv (Port_T p, char *, int, int)
 Read a message from the socket in non-blocking mode and with failover.

char * get_localhostname ()
 Get the local host name.

char * get_remote_host (int)
 Get the remote host name from the originating host.


Define Documentation

#define FALSE   0
 

Definition at line 31 of file net.h.

#define LINGER_TIMEOUT   10
 

Definition at line 29 of file net.h.

#define SELECT_TIMEOUT   5
 

Definition at line 28 of file net.h.

Referenced by check_socket(), sock_recv(), and sock_send().

#define TRUE   1
 

Definition at line 30 of file net.h.


Function Documentation

int check_connect char *    hostname,
int    port,
int    protocol
 

Test INET Socket connection to host on port with the specified socket protocol type.

The protocol is normaly either SOCK_STREAM or SOCK_DGRAM.

Parameters:
hostname  The host to connect to
port  The portnumber to connect to
protocol  The socket protocol to use
Returns:
TRUE if the connect succeeded otherwise FALSE.

Definition at line 112 of file net.c.

References ASSERT, check_socket(), close_socket(), and create_socket().

Referenced by check_httpd().

int check_connection_io Port_T    p
 

Verify that the socket connection is ready for i|o.

This test use select for a stream based socket. For a dgram sockets the testing technique is to send a 0 byte udp packet and "check" for an ICMP error.

Parameters:
p  A Port object
Returns:
TRUE if the socket is ready, otherwise FALSE.

Definition at line 166 of file net.c.

References ASSERT, check_socket(), check_udp_socket(), myport::socket, and myport::type.

int check_host char *    hostname
 

Check if the hostname resolves.

Parameters:
hostname  The host to check
Returns:
TRUE if hostname resolves, otherwise FALSE

Definition at line 141 of file net.c.

References ASSERT.

int check_socket int    socket
 

Verify that the socket is ready for i|o.

Parameters:
socket  A socket
Returns:
TRUE if the socket is ready, otherwise FALSE.

Definition at line 186 of file net.c.

References SELECT_TIMEOUT.

Referenced by check_connect(), check_connection_io(), and start_httpd().

int check_udp_socket int    socket
 

Verify that the udp socket is ready for i|o.

The given socket must be a connected udp socket. The test is conducted by sending one byte to the server and check for any returned ICMP error when reading from the socket. A better test would be to send an empty udp package to avoid possibly raising an error from the server we are testing.

Parameters:
socket  A socket
Returns:
TRUE if the socket is ready, otherwise FALSE.

Definition at line 212 of file net.c.

References set_block(), and set_noblock().

Referenced by check_connection_io().

int close_socket int    socket
 

Shutdown a socket and close the descriptor.

Parameters:
socket  The socket to shutdown and close
Returns:
TRUE if the close succeed otherwise FALSE

Definition at line 411 of file net.c.

Referenced by check_connect(), create_socket(), create_unix_socket(), d_check_process(), destroy_wrapper(), and stop_httpd().

int create_generic_socket Port_T    pp
 

Open a socket with given Port_T structure The protocol, destination and type are selected appropriately.

Parameters:
pp  connection description
Returns:
The socket or -1 if an error occured.

Definition at line 288 of file net.c.

References ASSERT, create_socket(), create_unix_socket(), myport::family, myport::hostname, myport::pathname, myport::port, and myport::type.

int create_server_socket int    port,
int    backlog,
char *    bindAddr
 

Creates a server socket (SOCK_STREAM type) and binds it to the specified local port number, with the specified backlog.

Set a socket option to make the port reusable again. This is useful if the server has been shut down, and then restarted right away while sockets are still active on its port. If a bind address is given the socket will only accept connect requests to this addresses. If the bind address is NULL it will default accepting connections on any/all local addresses

Parameters:
port  The localhost port number to open
backlog  The maximum queue length for incomming connections
bindAddr  the local address the server will bind to
Returns:
The socket ready for accept, or -1 if an error occured.

Definition at line 358 of file net.c.

Referenced by create_ssl_server_socket(), and start_httpd().

int create_socket char *    hostname,
int    port,
int    protocol
 

Open a socket against hostname:port with the given protocol.

The protocol is normaly either SOCK_STREAM or SOCK_DGRAM.

Parameters:
hostname  The host to open a socket at
port  The port number to connect to
protocol  Socket protocol to use (SOCK_STREAM|SOCK_DGRAM)
Returns:
The socket or -1 if an error occured.

Definition at line 246 of file net.c.

References ASSERT, and close_socket().

Referenced by check_connect(), create_generic_socket(), create_ssl_socket(), and d_check_process().

int create_unix_socket char *    pathname,
int    protocol
 

Open a socket against pathname with the given protocol.

The protocol is normaly either SOCK_STREAM or SOCK_DGRAM.

Parameters:
pathname  The unix socket to connect to
protocol  Socket protocol to use (SOCK_STREAM|SOCK_DGRAM)
Returns:
The socket or -1 if an error occured.

Definition at line 316 of file net.c.

References ASSERT, and close_socket().

Referenced by create_generic_socket().

char* get_localhostname  
 

Get the local host name.

Returns:
the name of the localhost. NULL if lookup failed

Definition at line 658 of file net.c.

Referenced by sendmail().

char* get_remote_host int    socket
 

Get the remote host name from the originating host.

Parameters:
socket  A socket
Returns:
the (official) name (A-Record) of the peer or NULL if lookup failed.

Definition at line 679 of file net.c.

int port_recv Port_T    p,
char *    buf,
int    len,
int    flags
 

Read a message from the socket in non-blocking mode and with failover.

Uses SSL if necessary.

Parameters:
sock  A port handler
buf  The buffer to read a message into
len  The length of the buffer
flags  Optional recv flags
Returns:
Returns the bytes read or -1 if an error occured

Definition at line 511 of file net.c.

References ASSERT, len, recv_ssl_socket(), sock_recv(), myport::socket, and myport::ssl.

Referenced by check_ftp(), check_http(), check_imap(), check_nntp(), check_pop(), check_smtp(), and check_ssh().

int port_send Port_T    p,
const char *    msg,
int    len,
int    flags
 

Send the message 'msg' down a port in non-blocking mode and with failover.

Uses SSL if necessary.

Parameters:
p  port handler
msg  The message to send
len  The length of the message in bytes
flags  Optional send flags
Returns:
The bytes sent or -1 if an error occured

Definition at line 485 of file net.c.

References ASSERT, len, send_ssl_socket(), sock_send(), myport::socket, and myport::ssl.

Referenced by check_ftp(), check_http(), check_imap(), check_nntp(), check_pop(), check_smtp(), and check_ssh().

int set_block int    socket
 

Disable nonblocking i|o on the given socket.

Parameters:
socket  A socket
Returns:
TRUE if success, otherwise FALSE

Definition at line 468 of file net.c.

Referenced by check_udp_socket(), sock_recv(), and sock_send().

int set_noblock int    socket
 

Enable nonblocking i|o on the given socket.

Parameters:
socket  A socket
Returns:
TRUE if success, otherwise FALSE

Definition at line 454 of file net.c.

Referenced by check_udp_socket(), embed_accepted_ssl_socket(), embed_ssl_socket(), sock_recv(), and sock_send().

int set_sotimeout int    socket,
int    timeout
 

Set a so_timeout (read/write) for the given socket with the specified timeout in seconds.

Parameters:
socket  A socket
timeout  Read/Write timeout in seconds
Returns:
TRUE if success otherwise FALSE

Definition at line 439 of file net.c.

int sock_recv int    sock,
char *    buf,
int    len,
int    flags
 

Read a message from the socket in non-blocking mode and with failover.

Inspired by code from the Apache web server.

Parameters:
sock  A socket
buf  The buffer to read a message into
len  The length of the buffer
flags  Optional recv flags
Returns:
Returns the bytes read or -1 if an error occured

Definition at line 607 of file net.c.

References ASSERT, len, SELECT_TIMEOUT, set_block(), and set_noblock().

Referenced by port_recv().

int sock_send int    sock,
const char *    msg,
int    len,
int    flags
 

Send the message 'msg' down the socket in non-blocking mode and with failover.

Inspired by code from the Apache web server.

Parameters:
sock  A socket
msg  The message to send
len  The length of the message in bytes
flags  Optional send flags
Returns:
The bytes sent or -1 if an error occured

Definition at line 539 of file net.c.

References ASSERT, len, SELECT_TIMEOUT, set_block(), and set_noblock().

Referenced by d_check_process(), and port_send().