File modules/sv/server.c

  $Revision: 1.37 $

Example code: A server for a client to connect to.
Status: NOT REVUED, NOT TESTED
Authors: Chris Ottrey, Joao Damas
Online References:

Included Files


Preprocessor definitions

#define RIPE_REG 17

String sizes

#define STR_S 63

#define STR_M 255

#define STR_L 1023

#define STR_XL 4095

#define STR_XXL 16383

#define MAX_SOURCES 100


Global Variable Lock

pthread_mutex_t Lock
Included from include/ca_defs.h
Visible in:  modules/ac/access_control.c
   modules/ca/ca_configFns.c
   modules/ca/ca_values.c
   modules/pm/pm_serials.c
   modules/pm/protocol_mirror.c
   modules/qc/query_command.c
   modules/qi/query_instructions.c
   modules/rp/rp_convert.c
   modules/rp/rp_load.c
   modules/rp/rp_search.c
   modules/rp/rp_tree.c
   modules/rp/rp_update.c
   modules/sv/server.c
Used in: ca_get_adminIntElement()modules/ca/ca_configFns.c
  ca_get_adminStrElement()modules/ca/ca_configFns.c
  ca_get_boolean()modules/ca/ca_configFns.c
  ca_get_dirlist()modules/ca/ca_configFns.c
  ca_get_int()modules/ca/ca_configFns.c
  ca_get_string()modules/ca/ca_configFns.c
  ca_set_boolean()modules/ca/ca_configFns.c
  ca_set_string()modules/ca/ca_configFns.c

Global Variable newPort

char newPort[16]
Included from include/ca_defs.h
Visible in:  modules/ac/access_control.c
   modules/ca/ca_configFns.c
   modules/ca/ca_values.c
   modules/pm/pm_serials.c
   modules/pm/protocol_mirror.c
   modules/qc/query_command.c
   modules/qi/query_instructions.c
   modules/rp/rp_convert.c
   modules/rp/rp_load.c
   modules/rp/rp_search.c
   modules/rp/rp_tree.c
   modules/rp/rp_update.c
   modules/sv/server.c

Global Variable sv_lockfd

int sv_lockfd[4]
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_sleep()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_whois_sock

int SV_whois_sock
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_config_sock

int SV_config_sock
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_mirror_sock

int SV_mirror_sock
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_update_sock

int SV_update_sock[100]
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable Whois_thread_count_lock

Mutex lock. Used for synchronizing changes.

pthread_mutex_t Whois_thread_count_lock
Visible in:  modules/sv/server.c
Used in: SV_do_whois()modules/sv/server.c


Global Variable Config_thread_count_lock

pthread_mutex_t Config_thread_count_lock
Visible in:  modules/sv/server.c

Global Variable Mirror_thread_count_lock

pthread_mutex_t Mirror_thread_count_lock
Visible in:  modules/sv/server.c
Used in: SV_do_mirror()modules/sv/server.c

Global Variable Whois_thread_count

The number of threads.

int Whois_thread_count
Visible in:  modules/sv/server.c
Used in: SV_do_whois()modules/sv/server.c


Global Variable Config_thread_count

int Config_thread_count
Visible in:  modules/sv/server.c

Global Variable Mirror_thread_count

int Mirror_thread_count
Visible in:  modules/sv/server.c
Used in: SV_do_mirror()modules/sv/server.c

Global Variable SV_starttime

Server starting time

time_t SV_starttime
Visible in:  modules/sv/server.c
  PC_interact()modules/pc/protocol_config.c
Used in: PC_interact()modules/pc/protocol_config.c
  SV_start()modules/sv/server.c


Global Function SV_concurrent_server()

  This is the routine that creates the main threads. 

More:
  Author:
        ottrey
	joao
void SV_concurrent_server ( int sock, void* do_function(void*) )
int sock
The socket to connect to. void * do_function The function to call for each type of service
void* do_function(void*)
&nbs;
Prototyped in: modules/sv/server.h
Calls: TH_create(), fprintf(), wr_real_calloc()
Called by: SV_start()modules/sv/server.c
References Functions: main_loop()modules/sv/server.c

Global Function SV_do_config()

  Handle config connections.

More:
  Author:
        joao
void* SV_do_config ( void* arg )
void* arg
The socket to connect to. (It has to be passed in this way for this thread routine.)
Prototyped in: modules/sv/server.h
Calls: PC_interact()modules/pc/protocol_config.c
  log_print()modules/sv/server.c
  TA_add(), TA_delete(), pthread_exit(), pthread_self(), sprintf(), strcpy()
Used in: SV_start()modules/sv/server.c

Global Function SV_do_mirror()

  Handle NRTM connections.

More:
  Author:
        joao
void* SV_do_mirror ( void* arg )
void* arg
The socket to connect to. (It has to be passed in this way for this thread routine.)
Prototyped in: modules/sv/server.h
Calls: PM_interact()modules/pm/protocol_mirror.c
  log_print()modules/sv/server.c
  TA_add(), TA_delete(), pthread_exit(), pthread_mutex_lock(), pthread_mutex_unlock(), pthread_self(), sprintf(), strcpy()
Used in: SV_start()modules/sv/server.c
References Variables: Mirror_thread_countmodules/sv/server.c
  Mirror_thread_count_lockmodules/sv/server.c

Global Function SV_do_whois()

  Handle whois connections.

More:
  Author:
        joao
void* SV_do_whois ( void* arg )
void* arg
The socket to connect to. (It has to be passed in this way for this thread routine.)
Prototyped in: modules/sv/server.h
Calls: ER_dbg_va()modules/er/er.c
  PW_interact(), TA_add(), TA_delete(), pthread_exit(), pthread_mutex_lock(), pthread_mutex_unlock(), pthread_self()
Used in: SV_start()modules/sv/server.c
References Variables: Whois_thread_countmodules/sv/server.c
  Whois_thread_count_lockmodules/sv/server.c

Global Function SV_shutdown()

  Shutdown the server.

More:
  Authors:
        andrei
Stops the server.
  1. Close listening sockets (whois, config, mirror and updates)
  2. Stop all threads by triggering do_server variable.
.properties
void SV_shutdown ( void )
Prototyped in: modules/sv/server.h
Calls: CO_set_const()modules/co/constants.c
  close(), fprintf(), sprintf(), strcpy(), write()
Called by: SV_signal_thread()modules/sv/server.c
References Variables: SV_config_sockmodules/sv/server.c
  SV_mirror_sockmodules/sv/server.c
  SV_update_sockmodules/sv/server.c
  SV_whois_sockmodules/sv/server.c
  sv_lockfdmodules/sv/server.c

Global Function SV_signal_thread()

  Handle signals.

Changes the flags: do_nrtm do_update do_whoisd
More:
  Author:
        andrei
void* SV_signal_thread ( void )
Prototyped in: modules/sv/server.h
Calls: CO_get_do_update()modules/co/constants.c
  CO_set_const()modules/co/constants.c
  SV_shutdown()modules/sv/server.c
  log_print()modules/sv/server.c
  pthread_exit(), pthread_sigmask(), sigaddset(), sigemptyset(), sigwait(), sprintf(), strcpy()

Global Function SV_sleep()

  Sleep and wake up on special events.

More:
  Authors:
        andrei
Sleeps timeout but wakes up when an envent occures.
int SV_sleep ( int lock, int sleeptime )
Prototyped in: modules/sv/server.h
Calls: fprintf(), fstat(), memset(), select()
Called by: AC_decay()modules/ac/access_control.c
References Variables: sv_lockfdmodules/sv/server.c

Global Function SV_start()

  Start the server.

More:
  Authors:
        ottrey
        joao
Starts up the server.
  1. Create sockets on the necessary ports (whois, config and mirror)
  2. Start new threads for each service.
.properties
void SV_start ( void )
Prototyped in: modules/sv/server.h
Calls: AC_acc_load()modules/ac/access_control.c
  AC_build()modules/ac/access_control.c
  CO_get_config_port()modules/co/constants.c
  CO_get_mirror_port()modules/co/constants.c
  CO_get_whois_port()modules/co/constants.c
  SK_atoport()modules/sk/sk_socket.c
  SK_getsock()modules/sk/sk_socket.c
  SV_concurrent_server()modules/sv/server.c
  ca_get_SourceHandleByPosition()modules/ca/ca_configFns.c
  ca_srchandle2Intelement()modules/ca/ca_configFns.c
  ca_srchandle2Strelement()modules/ca/ca_configFns.c
  TH_create(), exit(), fprintf(), free(), gettimeofday(), pipe(), printf(), pthread_exit()
References Functions: AC_decay()modules/ac/access_control.c
  SV_do_config()modules/sv/server.c
  SV_do_mirror()modules/sv/server.c
  SV_do_whois()modules/sv/server.c
  radix_init()modules/sv/server.c
  UD_do_nrtm(), UD_do_updates()
References Variables: SV_config_sockmodules/sv/server.c
  SV_mirror_sockmodules/sv/server.c
  SV_starttimemodules/sv/server.c
  SV_update_sockmodules/sv/server.c
  SV_whois_sockmodules/sv/server.c
  sv_lockfdmodules/sv/server.c

Global Function SV_watchdog()

  This is the routine that creates a watchdog thread. 

The watchdog will cancel (pthread_cancel()) the calling thread in case the socket is closed by the client (its read-half is closed). The calling thread should make necessaruy preparations when calling the watchdog:
- the socket should be connected - cancellation points and cleanup routines should be defined
In case the connection is closed by the calling thread itself, the watchdog just exits and no action against the calling thread is performed.
wd_args - a pointer to wd_args_t structure containing data about socket and thread ID
More:
  Author:
        ottrey
	joao
	andrei
void SV_watchdog ( wd_args_t* wd_args )
Prototyped in: modules/sv/server.h
Calls: TH_create()
References Functions: do_watchdog()modules/sv/server.c

Global Function radix_init()

void radix_init ( void )
Calls: RP_init_trees()modules/rp/rp_tree.c
  RP_sql_load_reg()modules/rp/rp_load.c
  ca_get_SourceHandleByPosition()modules/ca/ca_configFns.c
  fprintf(), pthread_exit(), wr_log_set()
Used in: SV_start()modules/sv/server.c

Local Function do_watchdog()

The watchdog thread itself

The watchdog thread makes select() on the connected socket waiting until it becomes readable. If this happens as a result of some input, it'll simply dump it. Otherwise, this indicates that the client has closed the connection. In this case watchdog will cancel (pthread_cancel()) the whois thread (which in its turn will kill (mysql_kill()) mysql thread as part of its cleanup routine).
More:
Author:
      andrei
static void do_watchdog ( void* arg )
Prototyped in: modules/sv/server.c
Calls: memset(), pthread_cancel(), pthread_exit(), read(), select()
Used in: SV_watchdog()modules/sv/server.c

Local Function log_print()

static void log_print ( const char* arg )
Calls: printf()
Called by: SV_do_config()modules/sv/server.c
  SV_do_mirror()modules/sv/server.c
  SV_signal_thread()modules/sv/server.c

Local Function main_loop()

  Waits for an incoming connection on the and spawns a new thread to handle it.

More:
  Author:
        ottrey
	joao
	andrei (do_server)
static void* main_loop ( void* arg )
void* arg
Pointer to a struct containing the socket to talk to the client and the function to call depending on the incoming connection.
Calls: CO_get_do_server()modules/co/constants.c
  ER_dbg_va()modules/er/er.c
  SK_accept_connection()modules/sk/sk_socket.c
  TH_create()
Used in: SV_concurrent_server()modules/sv/server.c