Cross reference for modules/sk/sk_socket.c of index.

# File modules/sk/sk_socket.c

      $Revision: 1.9 $

      Example code: A socket module.

      Status: NOT REVUED, NOT TESTED

    Online References:

      Adapted from sample source code.

## Included Files

- `#include <arpa/inet.h>`

- `#include "modules/sk/sk.h"`
  - `#include <sys/types.h>`
  - `#include <sys/socket.h>`
  - `#include <netinet/in.h>`
  - `#include <stdlib.h>`
  - `#include <errno.h>`
  - `#include <netdb.h>`
  - `#include <signal.h>`
  - `#include <stdio.h>`
  - `#include <include/iproutines.h>`

- `#include "include/constants.h"`
  - `#include <glib.h>`

- `#include "include/stubs.h"`

- `#include "include/iproutines.h"`

- `#include "include/memwrap.h"`
  - `#include <erroutines.h>`
  - `#include <glib.h>`

- `#include <pthread.h>`

## Preprocessor definitions

String sizes

`#define STR_S 63`

`#define STR_M 255`

`#define STR_L 1023`

`#define STR_XL 4095`

`#define STR_XXL 16383`

`#define SKBUFLEN 2047`

## External Variables

h_errno
`extern int h_errno`

## Local Variables

sk_watch_tsd
`static pthread_key_t sk_watch_tsd`

Used in: | func_sigusr()
| sk_real_init()
| sk_watchdog()

sk_init_once
`static pthread_once_t sk_init_once`

Used in: | SK_init()

## 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

      More:

      Authors:
    	joao

`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: | ER_dbg_va() | modules/er/er.c
| accept(), close(), perror()

## 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").

      More:

      Authors:
            ottrey

    Online References:

`int SK_atoport ( const char* service, const char* proto )`

Prototyped in: | modules/sk/sk.h
Calls: | getservbyname_r(), strtol()

## Global Function SK_cd_close()

`int SK_cd_close ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Calls: | SK_close() | modules/sk/sk_socket.c
Called by: | PM_interact() | modules/pm/protocol_mirror.c

## Global Function SK_cd_gets()

       Wrapper around SK_gets.

      More:
           if the connection structure has bad status for this connection
           from previous calls, no write will be attempted.

      Authors:
            marek

      Side Effects:
           broken connections get registered
           in the connection structure within the query environment

`int SK_cd_gets ( sk_conn_st* condat, char* str, size_t count )`

`int SK_cd_gets`
  The total_count of bytes read, or errors (represented as negative numbers)
`sk_conn_st* condat`
  connection data
`char* str`
  The buffer to be written from the socket.
`size_t count`
  &nbs;

Prototyped in: | modules/sk/sk.h
Calls: | fprintf(), memset(), read(), select()
Called by: | PC_interact() | modules/pc/protocol_config.c
| PM_interact() | modules/pm/protocol_mirror.c
| authenticate_user() | modules/pc/protocol_config.c

## Global Function SK_cd_printf()

`int SK_cd_printf ( sk_conn_st* condat, char* txt, ... )`

Prototyped in: | modules/sk/sk.h
Calls: | NOERR() | modules/er/er.c
| SK_cd_puts() | modules/sk/sk_socket.c
| __builtin_next_arg(), __builtin_saveregs(), fprintf(), vsnprintf(), wr_real_free(), wr_real_malloc()
Called by: | PC_interact() | modules/pc/protocol_config.c
| QC_fill() | modules/qc/query_command.c
| er_macro_list_hook() | modules/er/er_macro.c
| process_input() | modules/pc/protocol_config.c
| qi_prep_run_refer() | modules/qi/query_instructions.c

## Global Function SK_cd_puts()

       This function writes a character string out to a socket.

       int SK_qe_puts  The total_count of bytes written,
                    or errors (represented as negative numbers)

       char   *str       The buffer to be written from the socket.

      More:
           if the connection structure has bad status for this connection
           from previous calls, no write will be attempted.

      Authors:
            marek

      Side Effects:
           broken connections get registered
           in the connection structure within the query environment

`int SK_cd_puts ( sk_conn_st* condat, const char* str )`

`sk_conn_st* condat`
  connection data
`const char* str`
  &nbs;

Prototyped in: | modules/sk/sk.h
Calls: | SK_puts() | modules/sk/sk_socket.c
| fprintf()
Called by: | AC_rxwalkhook_print() | modules/ac/access_control.c
| AC_rxwalkhook_print_acl() | modules/ac/access_control.c
| PC_interact() | modules/pc/protocol_config.c
| PM_interact() | modules/pm/protocol_mirror.c
| QC_fill() | modules/qc/query_command.c
| SK_cd_printf() | modules/sk/sk_socket.c
| authenticate_user() | modules/pc/protocol_config.c
| process_input() | modules/pc/protocol_config.c
| qi_prep_run_refer() | modules/qi/query_instructions.c
| run_referral() | modules/qi/query_instructions.c
| rx_space_printone() | modules/rx/rx_print.c
| rx_tree_print() | modules/rx/rx_print.c
| rx_walk_hook_printnode() | modules/rx/rx_print.c
| show_access() | modules/pc/pc_commands.c
| show_acl() | modules/pc/pc_commands.c
| write_radix_immediate() | modules/qi/query_instructions.c
| write_results() | modules/qi/query_instructions.c

## Global Function SK_close()

      More:

      Authors:
            ottrey

    Online References:

`int SK_close ( int socket )`

Prototyped in: | modules/sk/sk.h
Calls: | ER_dbg_va() | modules/er/er.c
| close()
Called by: | PC_interact() | modules/pc/protocol_config.c
| SK_cd_close() | modules/sk/sk_socket.c

## 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()

`int SK_getpeerip ( int sockfd, ip_addr_t* ip )`

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

## Global Function SK_getpeername()

       This function will tell you who is at the other end of a connected stream socket.
       XXX It's not working.
       XXX ? MB it is...

      More:

      Authors:
            ottrey

`char* SK_getpeername ( int sockfd )`

`int sockfd`
  The socket file descriptor.

Prototyped in: | modules/sk/sk.h
Calls: | fprintf(), getpeername(), inet_ntoa(), strcpy(), wr_real_malloc()
Called by: | PC_interact() | modules/pc/protocol_config.c
| PM_interact() | modules/pm/protocol_mirror.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".

      More:

      Authors:
            ottrey

      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.

      To Do:
            Capture the control-c properly!

`int SK_gets ( int sockfd, char* str, size_t count )`

`int SK_gets`
  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()

       This function creates a socket and binds to it

Online References:

`int SK_getsock ( int socket_type, u_short port, uint32_t bind_address )`

`int SK_getsock`
  The new socket
`int socket_type`
  SOCK_STREAM or SOCK_DGRAM (TCP or UDP sockets)
`u_short port`
  The port to listen on. Remember that ports < 1024 are reserved for the root user. Must be passed in network byte order (see "man htons").
`uint32_t bind_address`
  Address to bind to, in network order. More:

        Authors:
              ottrey
      	joao

Prototyped in: | modules/sk/sk.h
Calls: | bind(), close(), exit(), listen(), memset(), perror(), setsockopt(), socket()

## Global Function SK_init()

`void SK_init ( void )`

Prototyped in: | modules/sk/sk.h
Calls: | pthread_once()
References Functions: | sk_real_init() | modules/sk/sk_socket.c
References Variables: | sk_init_once | modules/sk/sk_socket.c

## Global Function SK_putc()

       return number of chars written

`int SK_putc ( int sockfd, char ch )`

`int SK_putc`
  This function writes a single character out to a socket.
`int sockfd`
  socket char ch character
`char ch`
  &nbs;

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.

            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_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/sk_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.

      More:

      Authors:
            ottrey

`int SK_read ( int sockfd, char* buf, size_t count )`

`int SK_read`
  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_watch_setclear()

`void SK_watch_setclear ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Called by: | QI_execute() | modules/qi/query_instructions.c
| sql_execute_watched() | modules/qi/query_instructions.c

## Global Function SK_watch_setexec()

`void SK_watch_setexec ( sk_conn_st* condat, void* (*function)(void*), void* args )`

Prototyped in: | modules/sk/sk.h
Called by: | sql_execute_watched() | modules/qi/query_instructions.c

## Global Function SK_watch_setkill()

`void SK_watch_setkill ( sk_conn_st* condat, pthread_t killthis )`

Prototyped in: | modules/sk/sk.h

## Global Function SK_watchexec()

`void SK_watchexec ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Called by: | SK_watchtrigger() | modules/sk/sk_socket.c

## Global Function SK_watchkill()

`void SK_watchkill ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Calls: | pthread_cancel()
Called by: | SK_watchtrigger() | modules/sk/sk_socket.c

## Global Function SK_watchstart()

`er_ret_t SK_watchstart ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Calls: | fprintf(), pthread_create(), pthread_mutex_init(), pthread_mutex_lock()
Called by: | QI_execute() | modules/qi/query_instructions.c
| sql_execute_watched() | modules/qi/query_instructions.c
References Functions: | sk_watchdog() | modules/sk/sk_socket.c

## Global Function SK_watchstop()

`er_ret_t SK_watchstop ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Calls: | pthread_join(), pthread_kill(), pthread_mutex_destroy(), pthread_mutex_lock()
Called by: | QI_execute() | modules/qi/query_instructions.c
| sql_execute_watched() | modules/qi/query_instructions.c

## Global Function SK_watchtrigger()

`void SK_watchtrigger ( sk_conn_st* condat )`

Prototyped in: | modules/sk/sk.h
Calls: | SK_watchexec() | modules/sk/sk_socket.c
| SK_watchkill() | modules/sk/sk_socket.c
Called by: | sk_watchdog() | modules/sk/sk_socket.c

## Global Function SK_write()

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

       char   *buf    The buffer to be written to the socket.

      More:

      Authors:
            ottrey

    Online References:

`int SK_write ( int sockfd, const char* buf, size_t count )`

`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

## Local Function func_sigusr()

`static void func_sigusr ( int n )`

Calls: | ER_dbg_va() | modules/er/er.c
| pthread_setspecific()
Used in: | sk_watchdog() | modules/sk/sk_socket.c
References Variables: | sk_watch_tsd | modules/sk/sk_socket.c

## Local Function sk_real_init()

`static void sk_real_init ( void )`

Calls: | fprintf(), pthread_key_create()
Used in: | SK_init() | modules/sk/sk_socket.c
References Variables: | sk_watch_tsd | modules/sk/sk_socket.c

## Local Function sk_watchdog()

`static void* sk_watchdog ( void* arg )`

Calls: | ER_dbg_va() | modules/er/er.c
| SK_watchtrigger() | modules/sk/sk_socket.c
| fprintf(), memset(), pthread_exit(), pthread_getspecific(), pthread_mutex_unlock(), pthread_setspecific(), pthread_sigmask(), read(), select(), sigaction(), sigaddset(), sigemptyset()
Used in: | SK_watchstart() | modules/sk/sk_socket.c
References Functions: | func_sigusr() | modules/sk/sk_socket.c
References Variables: | sk_watch_tsd | modules/sk/sk_socket.c