$Revision: 1.2 $
Socket module - cd_socket.c - basic read/write socket routines defined
in terms of connection data structures
with timeouts and storing information about
broken connections.
Status: NOT REVUED, TESTED
Design and implementation by Marek Bukowy.
Included Files
- #include "modules/sk/sk.h"
- #include "include/stubs.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <stdio.h>
- #include "include/iproutines.h"
- #include <sys/types.h>
- #include <glib.h>
- #include <include/inet6def.h>
- #include <include/erroutines.h>
Preprocessor definitions
#define SKBUFLEN 2047
Wrapper around the close(2) system call,
Author:
marek
int SK_cd_close ( sk_conn_st* condat )
- int SK_cd_close
- returns the error codes of close(2).
- sk_conn_st* condat
- Pointer to the connection data structure.
Destroys the data allocated and anchored by the connection data structure.
void SK_cd_free ( sk_conn_st* condat )
- sk_conn_st* condat
- Pointer to the connection data structure.
Read from a socket, until a linefeed character is received or the buffer
fills up to the maximum size "count". If the connection data has non-zero
timeout value for reading, it is used here between calls to read
the next 1 character.
More:
if the connection structure has bad status for this connection
from previous calls, no read will be attempted.
Author:
marek
Side Effects:
broken connections get registered in the connection structure.
int SK_cd_gets ( sk_conn_st* condat, char* str, size_t count )
- int SK_cd_gets
- Returns the total_count of bytes read,
or inverted error codes (negative numbers):
(- SK_DISCONNECT) on broken connection,
(- SK_TIMEOUT) on timeout.
- sk_conn_st* condat
- connection data
- char* str
- The buffer to store the data received from
the socket.
- size_t count
- size of the buffer.
Construct a connection data given the socket or file descriptor.
Also performs the getpeername check and stores the IP in an allocated
string.
void SK_cd_make ( sk_conn_st* condat, int sock, unsigned timeout )
- sk_conn_st* condat
- pointer to where the data is to be stored.
- int sock
- The socket or file descriptor.
- unsigned timeout
- Read timeout (used in SK_cd_gets) in seconds.
Value of 0 disables the timeout.
Printf-like function to print to socket/file specified by connection
data structure. First writes the text to a temporary buffer, then
uses SK_cd_puts to print it. Maintains a 2K static buffer, and allocates
more memory if this is not enough.
Author:
marek
int SK_cd_printf ( sk_conn_st* condat, char* txt, ... )
- int SK_cd_printf
- Returns the SK_cd_puts error code/return value.
- sk_conn_st* condat
- Pointer to the connection data structure.
- char* txt
- Format text to be written
- ...
- more arguments (like printf)
This function writes a character string out to a socket, unless
the connection is broken.
char *str The buffer to be written to the socket.
More:
if the connection structure has bad status for this connection
from previous calls, no write will be attempted.
Author:
marek
Side Effects:
broken connections get registered in the connection structure
int SK_cd_puts ( sk_conn_st* condat, const char* str )
- int SK_cd_puts
- Returns the total_count of bytes written,
or inverted error codes (negative numbers):
(- SK_DISCONNECT) on broken connection,
(- SK_INTERRUPT) on control-c received,
(- SK_TIMEOUT) on timeout.
- sk_conn_st* condat
- Pointer to the connection data structure.
- const char* str
- &nbs;