GENERIC_SERVER  0.0.0.9
A light-weight, cross-platform, pluggable, extensible and secure framework for deploying C++ plug-ins.
 All Classes Files Functions Variables Typedefs Pages
Macros | Functions
control.h File Reference

Master program that initiates GENERIC_SERVER, spawns a thread for each plug-in and runs message loop for that plug-in. More...

#include <map>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <pthread.h>
#include "sslserver.h"

Go to the source code of this file.

Macros

#define TLS_REQ   1
 
#define SOCKET_REQ   2
 
#define PR_SET_NAME   15
 

Functions

void sig_int (int sig_num)
 
void do_nothing (int sig_num)
 
void * thread_fn (void *inp_arg)
 
void * ssl_thread_fn (void *inp_arg)
 
void daemonize ()
 
unsigned int get_data_size (unsigned char *recvbuf)
 
int add_size (unsigned char *recvbuf, unsigned int size)
 
string get_input_plugin_name (char *buffer)
 
static int process_client_req (SOCKET clnt_sock, int plugin_init_state, GENERIC_PLUGIN *plugin_p, int thread_no, generic_server *framework)
 
static int process_ssl_client_req (SOCKET clnt_sock, SSL *ssl, int plugin_init_state, GENERIC_PLUGIN *plugin_p, int thread_no, generic_server *framework)
 
static int terminate_client_session (SOCKET clnt_sock, generic_server *framework, GENERIC_PLUGIN *plugin_p, int thread_no, unsigned char *senduf)
 
static int terminate_ssl_client_session (SSL *ssl, generic_server *framework, GENERIC_PLUGIN *plugin_p, int thread_no, unsigned char *senduf)
 
static int print_source_hostname (int clnt_sock, int req_type, generic_server *framework, int thread_no, GENERIC_PLUGIN *plugin_p)
 
static int process_data (void)
 Main loop to 'select()' sockets from multiple clients.
 
static unsigned int init_sock_fds (fd_set *, SOCKET)
 
static int chk_command_sock (fd_set *, SOCKET)
 
static int launch_thread (GENERIC_PLUGIN *generic_plugin, unsigned int new_slot)
 
static int chk_all_clients (fd_set *socks)
 
static int read_socket (unsigned char *buffer, SOCKET sock, int size)
 

Detailed Description

Master program that initiates GENERIC_SERVER, spawns a thread for each plug-in and runs message loop for that plug-in.

Author
Suraj Vijayan
Date
: 2013/07/01 14:16:20

Contact: suraj.nosp@m.@bro.nosp@m.adcom.nosp@m..com

Definition in file control.h.

Function Documentation

static int launch_thread ( GENERIC_PLUGIN generic_plugin,
unsigned int  new_slot 
)
static

Launches a new thread to run a plug-in. Thread could be for an TLS session or a non-TLS session.

Parameters
[in]plugin_pPointer to generic_plugin object
[in]new_slotslot position into thread_ids map
Returns
1 always

Definition at line 689 of file control.cpp.

References generic_server::pinstance, and generic_server::thread_ids.

690 {
692 #ifdef LINUX
693  pthread_attr_t attr; // thread attribute
694  // set thread detachstate attribute to DETACHED
695  fwork->get_thread_sync_mutex();
696  pthread_attr_init(&attr);
697  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
698 #endif
699  if(generic_plugin->get_tls_flag())
700  {
701 #ifdef WINDOWS
702  fwork->get_thread_sync_mutex();
703  fwork->thread_ids[new_slot].th = (HANDLE)_beginthreadex(NULL, 0, &ssl_thread_fn, (void *)generic_plugin, 0, NULL );
704 #else
705  pthread_create(&fwork->thread_ids[new_slot].th,&attr,ssl_thread_fn, (void *)generic_plugin);
706 #endif
707  }
708  else
709  {
710 #ifdef WINDOWS
711  fwork->get_thread_sync_mutex();
712  fwork->thread_ids[new_slot].th = (HANDLE)_beginthreadex(NULL, 0, &thread_fn, (void *)generic_plugin, 0, NULL );
713 #else
714  pthread_create(&fwork->thread_ids[new_slot].th,&attr,thread_fn, (void *)generic_plugin);
715 #endif
716  }
717 #ifdef LINUX
718  pthread_attr_destroy(&attr);
719 #endif
720  fwork->close_thread_ids();
721  return(1);
722 }
map< int, THREAD_INFO > thread_ids
Map to store all currently running threads.
static generic_server * pinstance
Pointer to singleton framework instance.
This is a singleton class and provides framework functionality.
static int process_client_req ( SOCKET  clnt_sock,
int  plugin_init_state,
GENERIC_PLUGIN plugin_p,
int  thread_no,
generic_server framework 
)
static

This function gets called in a plug-in specific thread to process request sent by client on raw (non TLS) session. Once a valid request is received, 'process_request()' method on plug-in is invoked. Message structure:

  • SIGNATURE - 4 bytes SIGNATURE for all messages
  • PAYLOAD_SIZE - 4 bytes network byte order
  • DATA - PAYLOAD_SIZE data
  • Data payload could be:
  • PLUGIN_NAME_LEN - 4 bytes network byte order
  • PLUGIN_NAME - Size as defined by <PLUGIN_NAME_LEN>
  • APPLICATION_DATA - PAYLOAD_SIZE-(4+PLUGIN_NAME_LEN)
Parameters
[in]clnt_sockClient socket to read/write
[in]plugin_init_stateFlag to indicate return status of plugin_init() callback
[in]plugin_pPointer to plug-in object
[in]thread_noThread number assigned by framework
[in]frameworkPointer to framework object
Returns
1 on success, 0 on failure
static int process_ssl_client_req ( SOCKET  clnt_sock,
SSL *  ssl,
int  plugin_init_state,
GENERIC_PLUGIN plugin_p,
int  thread_no,
generic_server framework 
)
static

This function gets called in a plug-in specific thread to process request sent by client on TLS enabled session. Once a valid request is received, 'process_request()' method on plug-in is invoked. Message structure:

  • SIGNATURE - 4 bytes SIGNATURE for all messages
  • PAYLOAD_SIZE - 4 bytes network byte order
  • DATA - PAYLOAD_SIZE data
  • Data payload could be:
  • PLUGIN_NAME_LEN - 4 bytes network byte order
  • PLUGIN_NAME - Size as defined by <PLUGIN_NAME_LEN>
  • APPLICATION_DATA - PAYLOAD_SIZE-(4+PLUGIN_NAME_LEN)
Parameters
[in]clnt_sockClient socket attached to SSL
[in]sslPointer to SSL structure
[in]plugin_init_stateFlag to indicate return status of plugin_init() callback
[in]thread_noThread number assigned by framework
[in]frameworkPointer to framework object
Returns
1 on success, 0 on failure
static int terminate_client_session ( SOCKET  clnt_sock,
generic_server framework,
GENERIC_PLUGIN plugin_p,
int  thread_no,
unsigned char *  senduf 
)
static

This function gets called in a plug-in specific thread whenever a non-TLS client session is to be terminated.

Parameters
[in]clnt_socketClient socket to read/write
[in]frameworkPointer to framework object
[in]plugin_pPointer to plugin object
[in]thread_noThread number assigned by framework
[in]sendbufNULL terminated ASCII data to send to client
Returns
1 always

Definition at line 416 of file control.cpp.

References generic_server::log(), and generic_plugin::shutdown_plugin().

417 {
418  int len;
419  ostringstream os;
420 
421  len = strlen((char *)sendbuf);
422  send(clnt_sock, (char *)sendbuf, (int)len, 0 );
423  plugin_p->shutdown_plugin(); // Aborting SESSION..
424  os.str("");
425  os.clear();
426  os << " Thr.ID:" << thread_no << " Plugin:" << plugin_p->get_plugin_name() << " Quitting thread." << sendbuf;
427  fwork->log(LOG_LOW,LOG_LOW,os.str());
428  fwork->socket_close(clnt_sock);
429  delete plugin_p;
430  fwork->rel_thread_id(thread_no);
431  return(1);
432 }
virtual int shutdown_plugin(void)
static int terminate_ssl_client_session ( SSL *  ssl,
generic_server framework,
GENERIC_PLUGIN plugin_p,
int  thread_no,
unsigned char *  senduf 
)
static

This function gets called in a plug-in specific thread whenever a TLS client session is to be terminated.

Parameters
[in]sslPointer to SSL structure
[in]frameworkPointer to framework object
[in]plugin_pPointer to plugin object
[in]thread_noThread number assigned by framework
[in]sendbufNULL terminated ASCII data to send to client
Returns
1 always

Definition at line 435 of file control.cpp.

References generic_server::log(), and generic_plugin::shutdown_plugin().

436 {
437  int len,ssl_response,status;
438  ostringstream os;
439  char buf[100];
440 
441  len = strlen((char *)sendbuf);
442  status = recv(SSL_get_fd(ssl), (char*) buf, 10, MSG_PEEK);
443  if(status > 0)
444  {
445  ssl_response = fwork->ssl_async_write((SSL *)ssl, (char *)sendbuf, (int)len);
446  if(ssl_response > 0)
447  {
448  while((SSL_shutdown(ssl)) == 0)
449  ;
450  }
451  }
452  plugin_p->shutdown_plugin();
453  os << " Thr.ID:" << thread_no << " Plugin:" << plugin_p->get_plugin_name() << " Quitting thread." << sendbuf;
454  fwork->log(LOG_LOW,LOG_LOW,os.str());
455  fwork->socket_close(SSL_get_fd(ssl));
456  SSL_free((SSL *)ssl);
457  ERR_free_strings();
458  ERR_remove_state(0);
459  delete plugin_p;
460  fwork->rel_thread_id(thread_no);
461  return(1);
462 }
virtual int shutdown_plugin(void)