Main Page | Class List | File List | Class Members | File Members | Related Pages

network.h File Reference

Abstracted socket routines. More...

#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

Go to the source code of this file.

Defines

#define RW_BUFFER   2048
#define RW_SOCKBUFFER   65535 /* socket layer read/write buffer */
#define NET_IP_STRBUF

Functions

EXTERN_C_BEGIN LIBGIFT_EXPORT
void 
net_close (int fd)
LIBGIFT_EXPORT int net_connect (const char *ip, in_port_t port, BOOL blocking)
LIBGIFT_EXPORT int net_accept (int sock, BOOL blocking)
LIBGIFT_EXPORT int net_bind (in_port_t port, BOOL blocking)
LIBGIFT_EXPORT int net_set_blocking (int fd, BOOL blocking)
LIBGIFT_EXPORT int net_sock_error (int fd)
LIBGIFT_EXPORT int net_send (int fd, const char *data, size_t len)
LIBGIFT_EXPORT in_addr_t net_ip (const char *ip_str)
LIBGIFT_EXPORT char * net_ip_str (in_addr_t ip)
LIBGIFT_EXPORT char * net_ip_strbuf (in_addr_t ip, char *buf, size_t size)
LIBGIFT_EXPORT in_addr_t net_peer (int fd)
LIBGIFT_EXPORT char * net_peer_ip (int fd)
LIBGIFT_EXPORT in_addr_t net_local_ip (int fd, in_port_t *port)
LIBGIFT_EXPORT in_addr_t net_mask (int bitwidth)
LIBGIFT_EXPORT BOOL net_match_host (in_addr_t ip, char *match)
LIBGIFT_EXPORT uint8_t net_get8 (unsigned char *src)
LIBGIFT_EXPORT uint16_t net_get16 (unsigned char *src, int tohost)
LIBGIFT_EXPORT uint32_t net_get32 (unsigned char *src, int tohost)
LIBGIFT_EXPORT void net_put8 (unsigned char *dst, uint8_t src)
LIBGIFT_EXPORT void net_put16 (unsigned char *dst, uint16_t src)
LIBGIFT_EXPORT void net_put32 (unsigned char *dst, uint32_t src)
LIBGIFT_EXPORT int net_sock_adj_buf (int fd, int buf_name, float factor)


Detailed Description

Abstracted socket routines.

Contains high-level abstraction of the low-level socket functions. Most of these routines are meant for TCP socket manipulation only, but UDP will likely come in the future. UI developers should make sure they see tcp_open before using any of these routines.


Define Documentation

#define NET_IP_STRBUF
 

Defined when net_ip_strbuf is to be provided. This causes a slight slowdown in net_ip_str, and should be disabled if the functionality will never be used.

#define RW_BUFFER   2048
 

Sane network block size.

#define RW_SOCKBUFFER   65535 /* socket layer read/write buffer */
 

Socket layer read/write buffer. Used for throttling of bandwidth usage during large transfers.


Function Documentation

LIBGIFT_EXPORT int net_accept int  sock,
BOOL  blocking
 

Accept an incoming connection.

Note:
This function creates a new socket using the accept call. For more information try looking up the man page.
Parameters:
sock Bind socket to accept from.
blocking Blocking state to use for the new socket.
Returns:
See accept's manual page.

LIBGIFT_EXPORT int net_bind in_port_t  port,
BOOL  blocking
 

Bind (and listen) on the specified port.

Parameters:
port Port to listen on.
blocking Blocking state for the new socket.
Returns:
Newly created socket.

EXTERN_C_BEGIN LIBGIFT_EXPORT void net_close int  fd  ) 
 

Close a socket file descriptor. Use this routine if portability is important to you.

Parameters:
fd File descriptor to close.

LIBGIFT_EXPORT int net_connect const char *  ip,
in_port_t  port,
BOOL  blocking
 

Make an outgoing TCP connection

Parameters:
ip IP address (in dotted notation) that you wish to connect to.
port 
blocking Boolean value used to set the blocking (or non-blocking) state of the socket that will be created. If you're unsure you probably want blocking set to TRUE.
Returns:
Newly created socket.
Return values:
>=0 Successfully created the socket and if blocking, successfully connected to the remote host. You should be aware that if you do not set blocking this call will need to be later checked with net_sock_error.
-1 Error.

LIBGIFT_EXPORT in_addr_t net_ip const char *  ip_str  ) 
 

Wrapper for inet_addr.

LIBGIFT_EXPORT char* net_ip_str in_addr_t  ip  ) 
 

Formats an IPv4 address for display.

Parameters:
ip IPv4 address to format.
Returns:
Pointer to internally used memory that describes the dotted quad address notation of the supplied long address.

LIBGIFT_EXPORT char* net_ip_strbuf in_addr_t  ip,
char *  buf,
size_t  size
 

Similar to net_ip_str except that the supplied buffer will be used to copy the inet_ntoa(3) result into. net_ip_str uses its own internal buffer when this function is provided so that they will never collide.

LIBGIFT_EXPORT in_addr_t net_local_ip int  fd,
in_port_t *  port
 

Determine the locally bound ip address and optionally port.

LIBGIFT_EXPORT in_addr_t net_mask int  bitwidth  ) 
 

Returns a network mask.

Parameters:
bitwidth Width of the mask.
Returns:
Network order IPv4 address.

LIBGIFT_EXPORT BOOL net_match_host in_addr_t  ip,
char *  match
 

Matches an IP address against a worded notation.

Parameters:
ip IPv4 address supplied for comparison.
match Worded notation. Either ALL, LOCAL, or a dotted quad notation with an optionally appended "/" followed by the bitwidth of the mask to apply.
Return values:
TRUE Successful match.
FALSE No match could be identified.

LIBGIFT_EXPORT in_addr_t net_peer int  fd  ) 
 

Determine the peer IP address.

Parameters:
fd File descriptor to examine. This descriptor must be fully connected in order for this function do anything.

LIBGIFT_EXPORT char* net_peer_ip int  fd  ) 
 

Wrapper for net_peer which passes the result to net_ip_str.

LIBGIFT_EXPORT int net_send int  fd,
const char *  data,
size_t  len
 

Send data over the socket

Parameters:
fd File descriptor for writing.
data Linear character array to write.
len Length of non-terminated data stream. Use 0 if you wish to simply call strlen from within this function.
Returns:
See send's manual page.

LIBGIFT_EXPORT int net_set_blocking int  fd,
BOOL  blocking
 

Manually change the blocking status of a socket that has already been created.

Parameters:
fd File descriptor to manipulate.
blocking Blocking state for the original socket.

LIBGIFT_EXPORT int net_sock_error int  fd  ) 
 

Check if the socket has any pending errors resulting from a previous socket operation. If you are using a non-blocking socket scheme you will need to use this function when the selected socket gains the WRITE state after sending a connect request.

Parameters:
fd File descriptor to check.
Returns:
If greater than zero, the socket has errors as reported by getsockopt and the return value reflects this error. If less than zero, getsockopt failed. If exactly 0, no errors present on this socket.


Generated on Sun Aug 22 08:07:11 2004 by doxygen 1.3.7