Dillo v3.1.1-46-g8a360e32
Loading...
Searching...
No Matches
dpid.c File Reference
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "dpid_common.h"
#include "dpid.h"
#include "dpi.h"
#include "dpi_socket_dir.h"
#include "misc_new.h"
#include "../dpip/dpip.h"
Include dependency graph for dpid.c:

Go to the source code of this file.

Macros

#define DPI_EXT   (".dpi" EXEEXT)
 
#define QUEUE   5
 

Functions

void cleanup (void)
 
void free_dpi_attr (struct dp *dpi_attr)
 
void free_plugin_list (struct dp **dpi_attr_list_ptr, int numdpis)
 
void free_services_list (Dlist *s_list)
 
static void terminator (int sig)
 
void est_dpi_terminator (void)
 
static int ends_with (const char *str, const char *suffix)
 
enum file_type get_file_type (char *file_name)
 
char * get_dpi_dir (char *dpidrc)
 
int get_dpi_attr (char *dpi_dir, char *service, struct dp *dpi_attr)
 
int register_service (struct dp *dpi_attr, char *service)
 
int register_all (struct dp **attlist)
 
static int services_alpha_comp (const struct service *s1, const struct service *s2)
 
int fill_services_list (struct dp *attlist, int numdpis, Dlist **services_list)
 
static int make_socket_fd (void)
 
int bind_socket_fd (int base_port, int *p_port)
 
int save_comm_keys (int srs_port)
 
int init_ids_srs_socket (void)
 
int init_dpi_socket (struct dp *dpi_attr)
 
int init_all_dpi_sockets (struct dp *dpi_attr_list)
 
void dpi_sigchld (int sig)
 
void handle_sigchld (void)
 
void est_dpi_sigchld (void)
 
int ckd_connect (int sock_fd, struct sockaddr *addr, socklen_t len)
 
void stop_active_dpis (struct dp *dpi_attr_list, int numdpis)
 
void ignore_dpi_sockets (struct dp *dpi_attr_list, int numdpis)
 
int register_all_cmd (void)
 
char * get_message (int sock_fd, char *dpi_tag)
 
int service_match (const struct service *A, const char *B)
 
void send_sockport (int sock_fd, char *dpi_tag, struct dp *dpi_attr_list)
 

Variables

volatile sig_atomic_t caught_sigchld = 0
 
char * SharedKey = NULL
 

Detailed Description

Main functions to set-up dpi information and to initialise sockets

Definition in file dpid.c.

Macro Definition Documentation

◆ DPI_EXT

#define DPI_EXT   (".dpi" EXEEXT)

Definition at line 41 of file dpid.c.

◆ QUEUE

#define QUEUE   5

Definition at line 43 of file dpid.c.

Function Documentation

◆ bind_socket_fd()

int bind_socket_fd ( int  base_port,
int *  p_port 
)

Bind a socket port on localhost. Try to be close to base_port. \Return

  • listening socket file descriptor on success
  • -1 on failure

Definition at line 554 of file dpid.c.

References ERRMSG, make_socket_fd(), MSG_ERR, and QUEUE.

Referenced by init_dpi_socket(), and init_ids_srs_socket().

◆ ckd_connect()

int ckd_connect ( int  sock_fd,
struct sockaddr *  addr,
socklen_t  len 
)

EINTR aware connect() call

Definition at line 733 of file dpid.c.

References ERRMSG.

Referenced by stop_active_dpis().

◆ cleanup()

void cleanup ( void  )

Remove dpid_comm_keys file. This avoids that dillo instances connect to a stale port after dpid has exited (e.g. after a reboot).

Definition at line 52 of file dpid.c.

References dFree(), dGethomedir(), dotDILLO_DPID_COMM_KEYS, and dStrconcat().

Referenced by est_dpi_terminator(), and terminator().

◆ dpi_sigchld()

void dpi_sigchld ( int  sig)

SIGCHLD handler

Definition at line 687 of file dpid.c.

References caught_sigchld.

Referenced by est_dpi_sigchld().

◆ ends_with()

static int ends_with ( const char *  str,
const char *  suffix 
)
static

Definition at line 147 of file dpid.c.

Referenced by get_file_type().

◆ est_dpi_sigchld()

void est_dpi_sigchld ( void  )

Establish SIGCHLD handler

Definition at line 717 of file dpid.c.

References dpi_sigchld(), and ERRMSG.

Referenced by main().

◆ est_dpi_terminator()

void est_dpi_terminator ( void  )

Establish handler for termination signals and register cleanup with atexit

Definition at line 117 of file dpid.c.

References cleanup(), ERRMSG, MSG_ERR, and terminator().

Referenced by main().

◆ fill_services_list()

int fill_services_list ( struct dp attlist,
int  numdpis,
Dlist **  services_list 
)

Add services reading a dpidrc file each non empty or commented line has the form service = path_relative_to_dpidir \Return:

  • Returns number of available services on success
  • -1 on failure

Definition at line 437 of file dpid.c.

References _MSG, dFree(), dGethomedir(), dGetline(), dList_append(), dList_length(), dList_new(), dList_sort(), dNew, dotDILLO_DPI, dotDILLO_DPIDRC, service::dp_index, dParser_parse_rc_line(), dStrconcat(), dStrdup(), ERRMSG, get_dpi_dir(), MSG_ERR, service::name, numdpis, path(), services_alpha_comp(), and services_list.

Referenced by main(), and register_all_cmd().

◆ free_dpi_attr()

void free_dpi_attr ( struct dp dpi_attr)

Free memory used to describe a set of dpi attributes

Definition at line 63 of file dpid.c.

References dFree(), dp::id, and dp::path.

Referenced by free_plugin_list().

◆ free_plugin_list()

void free_plugin_list ( struct dp **  dpi_attr_list_ptr,
int  numdpis 
)

Free memory used by the plugin list

Definition at line 77 of file dpid.c.

References dFree(), dpi_attr_list, free_dpi_attr(), and numdpis.

Referenced by register_all_cmd().

◆ free_services_list()

void free_services_list ( Dlist s_list)

Free memory used by the services list

Definition at line 94 of file dpid.c.

References dFree(), dList_free(), dList_length(), dList_nth_data(), and service::name.

Referenced by register_all_cmd().

◆ get_dpi_attr()

int get_dpi_attr ( char *  dpi_dir,
char *  service,
struct dp dpi_attr 
)

Scans a service directory in dpi_dir and fills dpi_attr \Note Caller must allocate memory for dpi_attr. \Return

  • 0 on success
  • -1 on failure
    Todo:
    Add other file types, but first we need to add files associated with a dpi to the design.

Definition at line 233 of file dpid.c.

References dFree(), DPI_FILE, dStrconcat(), dStrdup(), ERRMSG, dp::filter, get_file_type(), dp::id, MSG_ERR, dp::path, dp::pid, and dp::port.

Referenced by register_all(), and register_service().

◆ get_dpi_dir()

char * get_dpi_dir ( char *  dpidrc)

Get dpi directory path from dpidrc \Return dpi directory on success, NULL on failure \Important The dpi_dir definition in dpidrc must have no leading white space.

Definition at line 179 of file dpid.c.

References dFree(), dGetline(), dStrdup(), ERRMSG, and MSG_ERR.

Referenced by fill_services_list(), register_all(), and register_service().

◆ get_file_type()

enum file_type get_file_type ( char *  file_name)

Identify a given file Currently there is only one file type associated with dpis. More file types will be added as needed

Definition at line 162 of file dpid.c.

References DPI_EXT, DPI_FILE, ends_with(), MSG_ERR, and UNKNOWN_FILE.

Referenced by get_dpi_attr().

◆ get_message()

char * get_message ( int  sock_fd,
char *  dpi_tag 
)

Get value of msg field from dpi_tag \Return message on success, NULL on failure

Definition at line 832 of file dpid.c.

References a_Dpip_build_cmd(), a_Dpip_get_attr(), CKD_WRITE, dFree(), and ERRMSG.

Referenced by send_sockport().

◆ handle_sigchld()

void handle_sigchld ( void  )

Called by main loop when caught_sigchld == 1

Definition at line 694 of file dpid.c.

References dpi_attr_list, numdpis, numsocks, dp::pid, and sock_set.

Referenced by main().

◆ ignore_dpi_sockets()

void ignore_dpi_sockets ( struct dp dpi_attr_list,
int  numdpis 
)

Removes dpis in dpi_attr_list from the set of sockets watched by select and closes their sockets.

Definition at line 794 of file dpid.c.

References dClose(), dpi_attr_list, numdpis, and sock_set.

◆ init_all_dpi_sockets()

int init_all_dpi_sockets ( struct dp dpi_attr_list)

Setup sockets for the plugins and add them to the set of sockets (sock_set) watched by select. \Return

  • Number of sockets on success
  • -1 on failure \Modifies dpi_attr_list.sa, dpi_attr_list.socket, numsocks, sock_set, srs \Uses numdpis, srs, srs_name

Definition at line 671 of file dpid.c.

References dpi_attr_list, init_dpi_socket(), numdpis, and numsocks.

Referenced by main(), and register_all_cmd().

◆ init_dpi_socket()

int init_dpi_socket ( struct dp dpi_attr)

Initialize a single dpi socket \Return

  • 1 on success
  • -1 on failure

Definition at line 647 of file dpid.c.

References bind_socket_fd(), DPID_BASE_PORT, dp::port, dp::sock_fd, and sock_set.

Referenced by init_all_dpi_sockets().

◆ init_ids_srs_socket()

int init_ids_srs_socket ( void  )

Initialise the service request socket (IDS) \Return:

  • Number of sockets (1 == success)
  • -1 on failure

Definition at line 623 of file dpid.c.

References a_Misc_mksecret(), bind_socket_fd(), DPID_BASE_PORT, save_comm_keys(), SharedKey, sock_set, and srs_fd.

Referenced by main().

◆ make_socket_fd()

static int make_socket_fd ( void  )
static

Definition at line 531 of file dpid.c.

References ERRMSG.

Referenced by bind_socket_fd(), and stop_active_dpis().

◆ register_all()

int register_all ( struct dp **  attlist)

Create dpi directory for available plugins and create plugin list. \Return

  • Returns number of available plugins on success
  • -1 on failure

Definition at line 347 of file dpid.c.

References dFree(), dGethomedir(), dotDILLO_DPI, dotDILLO_DPIDRC, dRealloc(), dStrconcat(), dStrdup(), ERRMSG, get_dpi_attr(), get_dpi_dir(), and MSG_ERR.

Referenced by main(), and register_all_cmd().

◆ register_all_cmd()

int register_all_cmd ( void  )

Registers available dpis and stops active non-filter dpis. Called when dpid receives cmd='register' service='all' command \Return Number of available dpis

Definition at line 811 of file dpid.c.

References dpi_attr_list, fill_services_list(), free_plugin_list(), free_services_list(), init_all_dpi_sockets(), numdpis, numsocks, register_all(), services_list, sock_set, srs_fd, and stop_active_dpis().

Referenced by main().

◆ register_service()

int register_service ( struct dp dpi_attr,
char *  service 
)

Register a service Retrieves attributes for "service" and stores them in dpi_attr. It looks for "service" in ~/.dillo/dpi first, and then in the system wide dpi directory. Caller must allocate memory for dpi_attr. \Return

  • 0 on success
  • -1 on failure

Definition at line 293 of file dpid.c.

References dFree(), dGethomedir(), dotDILLO_DPI, dotDILLO_DPIDRC, dStrconcat(), dStrdup(), ERRMSG, get_dpi_attr(), get_dpi_dir(), and MSG_ERR.

◆ save_comm_keys()

int save_comm_keys ( int  srs_port)

Save the current port and a shared secret in a file so dillo can find it. \Return:

  • -1 on failure

Definition at line 598 of file dpid.c.

References CKD_CLOSE, CKD_WRITE, dFree(), dGethomedir(), dotDILLO_DPID_COMM_KEYS, dStrconcat(), dStrerror, MSG, and SharedKey.

Referenced by init_ids_srs_socket().

◆ send_sockport()

void send_sockport ( int  sock_fd,
char *  dpi_tag,
struct dp dpi_attr_list 
)

Send socket port that matches dpi_id to client

Definition at line 868 of file dpid.c.

References a_Dpip_build_cmd(), CKD_WRITE, dFree(), dList_find_custom(), service::dp_index, dpi_attr_list, dReturn_if_fail, get_message(), numdpis, service_match(), and services_list.

Referenced by main().

◆ service_match()

int service_match ( const struct service A,
const char *  B 
)

Definition at line 851 of file dpid.c.

References dStrnAsciiCasecmp(), MAX, and service::name.

Referenced by send_sockport().

◆ services_alpha_comp()

static int services_alpha_comp ( const struct service s1,
const struct service s2 
)
static

Definition at line 424 of file dpid.c.

References service::name.

Referenced by fill_services_list().

◆ stop_active_dpis()

void stop_active_dpis ( struct dp dpi_attr_list,
int  numdpis 
)

Send DpiBye command to all active non-filter dpis

Definition at line 748 of file dpid.c.

References a_Dpip_build_cmd(), ckd_connect(), CKD_WRITE, dClose(), dFree(), dpi_attr_list, ERRMSG, make_socket_fd(), MSG_ERR, numdpis, path(), and SharedKey.

Referenced by main(), and register_all_cmd().

◆ terminator()

static void terminator ( int  sig)
static

Signal handler for SIGINT, SIGQUIT, and SIGTERM. Calls cleanup

Definition at line 108 of file dpid.c.

References cleanup().

Referenced by est_dpi_terminator().

Variable Documentation

◆ caught_sigchld

volatile sig_atomic_t caught_sigchld = 0

Set to 1 by the SIGCHLD handler dpi_sigchld

Definition at line 45 of file dpid.c.

Referenced by dpi_sigchld().

◆ SharedKey

char* SharedKey = NULL

Definition at line 46 of file dpid.c.

Referenced by init_ids_srs_socket(), save_comm_keys(), and stop_active_dpis().