CScpp/CS.h
changeset 0 5c129dd80d4f
equal deleted inserted replaced
-1:000000000000 0:5c129dd80d4f
       
     1 #ifndef _CSH_
       
     2 #define _CSH_ 1
       
     3 
       
     4 using namespace std;
       
     5 #include <string>
       
     6 #include <iostream>
       
     7 #include <thread>
       
     8 #include <string.h>
       
     9 #include <stdlib.h>
       
    10 #include <stdio.h>
       
    11 #include <unistd.h>
       
    12 #include <signal.h>
       
    13 #include <errno.h>
       
    14 #include <time.h>
       
    15 #include <sys/types.h>
       
    16 #include <sys/socket.h>
       
    17 #include <netdb.h>
       
    18 #include <sys/time.h>
       
    19 #include <openssl/ssl.h>
       
    20 #include <openssl/bio.h>
       
    21 #include <openssl/err.h>
       
    22 #include <sys/wait.h>
       
    23 #include <sys/select.h>
       
    24 #include <sys/mman.h>
       
    25 #include <semaphore.h>
       
    26 #include <libgen.h>
       
    27 #include <math.h>
       
    28 #include "Debug.h"
       
    29 
       
    30 typedef struct ShareS {			// shared items between procs or threads
       
    31 	int conns;						// overall connections# both in ring and mash
       
    32 	int msgs;						// overall forwards#	   both in ring and mash
       
    33 	int act;							//	active nodes#		   both in ring and mash
       
    34 	sem_t counterSem;				// semaphore for counters
       
    35 } ShareT, *ShareP;
       
    36 #define ShareA(v) if((v = (ShareP) mmap(NULL, sizeof(ShareT), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0)) < 0) SYSERR("mmap")
       
    37 
       
    38 typedef struct CSS {				// top level attributes
       
    39 	string text;					// text to be sent in messages
       
    40 	int ttl;							// TTL for circulating msgs
       
    41 	int mp0;							// TCP port of first mash node
       
    42 	int mn;							// intended # of nodes in mash
       
    43 	int rp0;							// TCP port of first ring node
       
    44 	int rn;							// intended # of nodes in ring
       
    45    struct timespec pace;		// pacing time quantum
       
    46    int pacing;						// pacing indicator
       
    47 	int issl;						// ssl switch: 0=no_ssl, 1=ssl, 2=both
       
    48 	int connThreshold;			// connection retry threshhold
       
    49 	int connTO;						// connection timeout in usecs
       
    50 	int selTO;						// selection timeout in secs
       
    51 	string cePath;					// SSL certs path
       
    52 	string caPath;					// CA  certs path
       
    53 	ShareP shP;
       
    54 	CSS(DebugP, char*);
       
    55 } *CSP;
       
    56 
       
    57 typedef struct HeaderS {		// container header
       
    58 	int ttl;
       
    59 	int ts;
       
    60 	int remPort;
       
    61 
       
    62 	HeaderS();
       
    63 	HeaderS(int);
       
    64 	int len();
       
    65 } *HeaderP;
       
    66 
       
    67 typedef struct PayloadS {		// payload structure
       
    68 	time_t ts;
       
    69 	char text;
       
    70 
       
    71 	PayloadS();
       
    72 	PayloadS(const char *);
       
    73 	int check(PayloadS*);
       
    74 	char *deliver();
       
    75 	string digest();
       
    76 	void sabotage();
       
    77 	int len();
       
    78 } *PayloadP;
       
    79 
       
    80 typedef struct ContainerS {	// data container sent throug mash or ring
       
    81 	HeaderS hdr;
       
    82 	PayloadS payl;
       
    83 	int len();
       
    84 } ContainerT, *ContainerP;
       
    85 
       
    86 typedef class DataC {			// data sent and received
       
    87 public:
       
    88 	DebugP deP;
       
    89 	ContainerP contP;
       
    90 	int dataLen;
       
    91 //	int transferLen;
       
    92 
       
    93 	DataC();
       
    94 	DataC(DebugP);
       
    95 	int dttl();
       
    96 	int ttl();
       
    97 	void load(int, const char*);
       
    98 	string unld();
       
    99 	string digest();
       
   100 	bool dataOk();
       
   101 	int ts();
       
   102 	int remPort();
       
   103 	int remPort(int);
       
   104 } *DataP;
       
   105 
       
   106 typedef struct SocketS {		// info about sockets allocated in node
       
   107 	int remPort;
       
   108 	int sc;
       
   109 	SSL *sslP;
       
   110 } *SocketP;
       
   111 
       
   112 typedef enum{ring, mash} topology;
       
   113 typedef enum{client, server} nodeside;
       
   114 class ConstellationC {			// constellation of communication nodes (mash or ring)
       
   115 	DebugP deP;
       
   116 public:
       
   117 	topology topo;
       
   118 	int ssl, first, nodes, *forwP;
       
   119 
       
   120 	ConstellationC();
       
   121 	ConstellationC(topology, int);
       
   122 	int run();
       
   123 };
       
   124 typedef ConstellationC *ConstellationP;
       
   125 
       
   126 class NodeC : public ConstellationC {	// attributes and operations of one node of constellation (mash or ring)
       
   127 public:
       
   128 	DebugP deP;
       
   129 	int locPort;
       
   130 	int last;
       
   131 	int kicker;
       
   132 	int closing;
       
   133 	thread closingThread;		// thread to close client side sockets
       
   134 	DataC data;
       
   135 	int len;
       
   136 	int ssc;
       
   137 	SocketP cliSides;
       
   138 	SocketP srvSides;
       
   139 	SSL_CTX *ctxP;
       
   140 
       
   141 	NodeC(ConstellationP, int);
       
   142 	int run();
       
   143 	void mainLoop();
       
   144 	void bindN();
       
   145 	void conn(int, int);
       
   146 	void acc(int);
       
   147 	void closeSocket(int, nodeside);
       
   148 	int getN(int);
       
   149 	int readN(int);
       
   150 	int putN(int);
       
   151 	int writeN(int);
       
   152 	int next_node();
       
   153 	void forward(int);
       
   154 	void closeClients();
       
   155 };
       
   156 typedef NodeC *NodeP;
       
   157 
       
   158 extern char *gpa(struct sockaddr *);
       
   159 extern void gai(int, struct addrinfo *ai, DebugP);
       
   160 #define GAI(level, ai) gai(level, ai, deP)
       
   161 extern void ssl_err(char*);
       
   162 extern void abend();
       
   163 
       
   164 #define SYSERR(e) LOG(0, "%s: %s (%d)", (char*)e, strerror(errno), errno), abend(deP)
       
   165 #define SSLERR(e) ssl_err((char*)e, deP), abend(deP)
       
   166 #define SOFTERR(e) LOG(0, e), fflush(stderr)
       
   167 #define HARDERR(e) SOFTERR(e), abend(deP);
       
   168 
       
   169 extern CSP csP;
       
   170 
       
   171 #endif