File modules/sk/sk_socket.c

  $Revision: 1.15 $

Socket module - routines facilitating calls to socket library.
Status: NOT REVUED, TESTED
Basic code adapted by Chris Ottrey from http://www.ibrado.com/sock-faq/sfaq.html#faq65 - sample source code.

Included Files


Preprocessor definitions

String sizes

#define STR_S 63

#define STR_XXL 16383

Wait for an incoming connection on the specified socket
int SK_accept_connection The socket for communicating to the client
int listening_socket The socket that the server is bound to
Authors: joao, marek.

#define MAX_ACCEPT_ERRORS 3


Global Function SK_accept_connection()

   Wait for an incoming connection on the specified socket

int SK_accept_connection The socket for communicating to the client
Authors: joao, marek.
int SK_accept_connection ( int listening_socket )
int listening_socket
The socket that the server is bound to
Prototyped in: modules/sk/sk.h
Calls: CO_get_do_server()modules/co/constants.c
  ER_dbg_va()modules/er/er.c
  ER_perror()modules/er/er.c
  accept(), fprintf(), strerror()

Global Function SK_atoport()

   Take a service name, and a service type, and return a port number.  If the
   service name is not found, it tries it as a decimal number.  The number
   returned is byte ordered for the network.

char *service Service name (or port number).
char *proto Protocol (eg "tcp").
Author: ottrey.
int SK_atoport ( const char* service, const char* proto )
Prototyped in: modules/sk/sk.h
Calls: getservbyname_r(), strtol()

Global Function SK_close()



Author: ottrey
int SK_close ( int socket )
int SK_close
wrapper around closing the socket. Returns the value returned by close(2)
int socket
socket to be closed
Prototyped in: modules/sk/sk.h
Calls: ER_dbg_va()modules/er/er.c
  close()
Called by: SK_cd_close()modules/sk/cd_socket.c

Global Function SK_connect()



int port port to connect to
int timeout in seconds
Author: marek
er_ret_t SK_connect ( int* sock, char* hostname, unsigned int port, unsigned int timeout )
er_ret_t SK_connect
wrapper around connect(), doing non-blocking connection with timeout
int* sock
pointer to the storage for socket descriptor
char* hostname
host to connect to
unsigned int port
&nbs;
unsigned int timeout
&nbs;
Prototyped in: modules/sk/sk.h
Calls: bcopy(), bind(), bzero(), close(), connect(), fcntl(), fprintf(), gethostbyname_r(), getsockopt(), memset(), select(), socket()

Global Function SK_getc()

   This function reads a single character from a socket.

returns EOF when no character can be read.
int SK_getc ( int sockfd )
Prototyped in: modules/sk/sk.h
Calls: read()

Global Function SK_getpeerip()

  This function will check the ip of the connected peer and store it in the
  ip_addr_t structure defined in the IP module.

  Author:
	marek
int SK_getpeerip ( int sockfd, ip_addr_t* ip )
int SK_getpeerip
returns 0 on success, -1 on failure.
int sockfd
The socket descriptor (file will result in -1)
ip_addr_t* ip
Pointer to where the address should be stored.
Prototyped in: modules/sk/sk.h
Calls: IP_addr_s2b()modules/ip/ip.c
  getpeername(), memset()
Called by: PC_interact()modules/pc/protocol_config.c
  PM_interact()modules/pm/protocol_mirror.c
  SK_cd_make()modules/sk/cd_socket.c

Global Function SK_getpeername()

  This function will tell you who is at the other end of a connected stream socket.

  Authors:
        ottrey,
	marek (modified error handling, made MT-Safe).
char* SK_getpeername ( int sockfd )
char* SK_getpeername
Returns allocated string with the IP in it, or "--" if the descriptor is not a socket, or NULL on error.
int sockfd
The socket or file descriptor.
Prototyped in: modules/sk/sk.h
Calls: inet_ntop()modules/ip/inet_ntop.c
  fprintf(), getpeername(), wr_real_malloc(), wr_real_string()
Called by: PC_interact()modules/pc/protocol_config.c
  PM_interact()modules/pm/protocol_mirror.c
  PW_interact()modules/pw/protocol_whois.c
  SK_cd_make()modules/sk/cd_socket.c
  er_path_safeguard()modules/er/er_paths.c

Global Function SK_gets()

   This function reads from a socket, until it recieves a linefeed
   character.  It fills the buffer "str" up to the maximum size "count".


Authors: ottrey, marek (modified for meaningful error codes).
Side Effects: This function will return -1 if the socket is closed during the read operation.
Note that if a single line exceeds the length of count, the extra data will be read and discarded! You have been warned.
int SK_gets ( int sockfd, char* str, size_t count )
int SK_gets
Returns the total_count of bytes read.
int sockfd
The socket file descriptor.
char* str
The buffer to be written from the socket.
size_t count
The number of bytes in the buffer.
Prototyped in: modules/sk/sk.h
Calls: ER_dbg_va()modules/er/er.c
  read()

Global Function SK_getsock()



unsigned port The port to listen on. Ports < 1024 are reserved for the root user. Host byte order.
Authors: ottrey, joao, marek (added htons conversion for port).
int SK_getsock ( int socket_type, unsigned h_port, int backlog, uint32_t bind_address )
int SK_getsock
This function creates a socket and binds to it. Returns the number of the created descriptor/listening socket.
int socket_type
SOCK_STREAM or SOCK_DGRAM (TCP or UDP sockets)
unsigned h_port
&nbs;
int backlog
Size of the backlog queue to be set on that socket.
uint32_t bind_address
Address to bind to, in network order.
Prototyped in: modules/sk/sk.h
Calls: bind(), close(), exit(), listen(), memset(), perror(), setsockopt(), socket()

Global Function SK_putc()

  This function writes a single character out to a socket.
int SK_putc ( int sockfd, char ch )
int SK_putc
Returns the number of characters written.
int sockfd
socket
char ch
character
Prototyped in: modules/sk/sk.h
Calls: SK_write()modules/sk/sk_socket.c

Global Function SK_puts()

   This function writes a character string out to a socket.

char *str The buffer to be written from the socket.
More:
  Authors:
        ottrey

Side Effects: This function will return -1 if the socket is closed during the write operation.
int SK_puts ( int sockfd, const char* str )
int SK_puts
The total_count of bytes written, or errors (represented as negative numbers)
int sockfd
The socket file descriptor.
const char* str
&nbs;
Prototyped in: modules/sk/sk.h
Calls: SK_write()modules/sk/sk_socket.c
  strlen()
Called by: SK_cd_puts()modules/sk/cd_socket.c

Global Function SK_read()

   This is just like the read() system call, except that it will make
   sure that all your data goes through the socket.

Author: ottrey
int SK_read ( int sockfd, char* buf, size_t count )
int SK_read
Returns the number of bytes read.
int sockfd
The socket file descriptor.
char* buf
The buffer to be read from the socket.
size_t count
The number of bytes in the buffer.
Prototyped in: modules/sk/sk.h
Calls: read()

Global Function SK_write()

   This is just like the write() system call, except that it will
   make sure that all data is transmitted.

char *buf The buffer to be written to the socket.
Author: ottrey
int SK_write ( int sockfd, const char* buf, size_t count )
int SK_write
Returns the number of bytes written.
int sockfd
The socket file descriptor.
const char* buf
&nbs;
size_t count
The number of bytes in the buffer.
Prototyped in: modules/sk/sk.h
Calls: ER_dbg_va()modules/er/er.c
  write()
Called by: SK_putc()modules/sk/sk_socket.c
  SK_puts()modules/sk/sk_socket.c