Main Page   File List   File Members  

snl.h File Reference

An user friendly multi threaded network library. More...

Go to the source code of this file.

Defines

#define SNL_LISTEN_CALLBACK(CB)   void (CB)(unsigned short port, int error, int fd, void *data)
 Macro to shorten the listen callback function prototype. More...

#define SNL_READ_CALLBACK(CB)   void (CB)(int fd, int error, void *buf, int len, void *data)
 Read callback function prototype macro. More...

#define SNL_ERR_OK   0
 no error.

#define SNL_ERR_OPEN   -1
 could'nt open socket.

#define SNL_ERR_LISTEN   -2
 error while listening to socket.

#define SNL_ERR_CONNECT   -3
 connecting to socket failed.

#define SNL_ERR_BIND   -4
 couldn't bind to socket.

#define SNL_ERR_ACCEPT   -5
 error while accepting connection.

#define SNL_ERR_READ   -6
 error while reading from socket.

#define SNL_ERR_WRITE   -7
 failed to write to socket.

#define SNL_ERR_CLOSED   -8
 peer closed connection.

#define SNL_ERR_BUFFER   -9
 out of memory (buffer allocation error).

#define SNL_ERR_ADDRESS   -10
 could not get address from hostname.

#define NULL   ((void *)0)
 NULL is defined in a somehow save manner.

#define FALSE   (0)
 FALSE is equal to 0.

#define TRUE   (!FALSE)
 TRUE is not equal to FALSE.


Functions

int snl_write (int fd, void *buf, unsigned long int len)
 Send a datagram over the socket connection. More...

int snl_read (int fd, SNL_READ_CALLBACK(*cb), void *data)
 Start a seperate thread to handle exact one socket connection. More...

int snl_listen (const char *name, unsigned short port, SNL_LISTEN_CALLBACK(*cb), void *data)
 Start a thread to listen for incoming connections. More...

int snl_connect (const char *name, unsigned short port)
 Connect to a listening socket. More...


Detailed Description

An user friendly multi threaded network library.

Author:
Clemens Kirchgatterer <clemens@thf.ath.cx>
Version:
V1.1.1
Date:
2002-03-30

This is a very user friendly library to hide all complicating things about network programming behind a neat api. It starts the threads on incoming connections, sends and receives data over a socket and takes care of large enough buffers. This way, a buffer overflow is definitly impossible to happen, as long as only library functions are used to read from and write to the socket filedescriptors.


Define Documentation

#define SNL_LISTEN_CALLBACK CB       void (CB)(unsigned short port, int error, int fd, void *data)
 

Macro to shorten the listen callback function prototype.

The SNL_LISTEN_CALLBACK() macro expands the callback function to its full prototype to make the sourcecode more readable.

#define SNL_READ_CALLBACK CB       void (CB)(int fd, int error, void *buf, int len, void *data)
 

Read callback function prototype macro.

The SNL_READ_CALLBACK() macro expands the callback function to its full prototype to make the sourcecode more readable.


Function Documentation

int snl_connect const char *    name,
unsigned short    port
 

Connect to a listening socket.

Parameters:
name  <const char *> hostname, ip address or unix socket to connect
port  <unsigned short> the port the server is listening on
Returns:
a filedescriptor on success or a negative error code
Note:
if port is 0, then a UNIX DOMAIN SOCKET is used
For connecting to a snl server, one has to call this fuction. The first paramter can be a hostname, an ipaddress or a unix socket name, depending on the second parameter. If port == 0, then name is interpreted as an unix socket file name (i.g. "/tmp/socket").

int snl_listen const char *    name,
unsigned short    port,
SNL_LISTEN_CALLBACK *    cb,
void *    data
 

Start a thread to listen for incoming connections.

Parameters:
name  <const char *> unix socket name for local connections
port  <unsigned short> port number the server should listen on
cb  <void (*)()> a pointer to the callback function
data  <void *> user specific data
Returns:
0 on success or a negative error code
Note:
if port is 0, then a UNIX DOMAIN SOCKET is used
To start the listening thread, this function must be used. Its interface is similar to the one of the read function.

int snl_read int    fd,
SNL_READ_CALLBACK *    cb,
void *    data
 

Start a seperate thread to handle exact one socket connection.

Parameters:
fd  <int> filedescriptor for this connection
cb  <void (*)()> a pointer to the callback function
data  <void *> user specific data
Returns:
0 on success or a negative error code
The read function registers a callback function, that will get called, when a complete datagram has been received. It also takes an additional parameter, a void pointer, that one can use for its own purpose.

int snl_write int    fd,
void *    buf,
unsigned long int    len
 

Send a datagram over the socket connection.

Parameters:
fd  <int> filedescriptor for this connection
buf  <void *> pointer to the data to send
len  <int> length of that data
Returns:
0 on success or a negative error code
This function is used to send a datagram over the network connection. Thereby the library ensures, that the hole datagram will get sent at once and that the other side receives all data correctly.


Generated on Sat Mar 30 10:17:18 2002 for SNL (Simple Network Layer) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002