/* ** Authors: Krishna Puttaswamy ** this file represents an instance of a node for the cashmere path life-time test */ #include #include #include #include #include #include #include #include #include #include "chimera.h" #include "cashmere.h" #include "dtime.h" // joinwaittime is the time the node waits before sending the message #define USAGE "node [ -j bootstrap:port ] port key timetolive" //0=notmalicious, 1=msg hijack, 2= 1+ver denial, 3 = 2+certhijack #define OPTSTR "j:" extern char *optarg; extern int optind; static char mykey[195]; ChimeraState *state; static int bootstrap = 0; static int receiver = 0; static double lastAck = 0; pthread_mutex_t lock; #define RAND() (lrand48()) #define SEED(s) (srand48(s)) #define HELLO_MESSAGE 51 #define SEND_MESSAGE 52 #define REPLY_TIMEOUT 10 #define CONSECUTIVE_TIMEOUTS 3 #define PATH_CLEAR_THRESHOLD 40 Key randomKeys[100]; int randomKeysCount = 0; static int ingraceperiod = 0; static int rebuildpath = 0; static int pathJustCleared = 0; static double lastPathClear = 0; void* ack_timeout(void *state) { int setLastTime = 0; while(1) { if (lastAck != 0) { double now = getTime(); if (pathJustCleared == 1) { int slp = (int)(now - lastPathClear); slp = PATH_CLEAR_THRESHOLD - slp; fprintf(stderr, "Sleeping for %d secs as the path was cleard just now \n", slp); sleep(slp); pthread_mutex_lock(&lock); pathJustCleared = 0; rebuildpath = 0; pthread_mutex_unlock(&lock); } now = getTime(); fprintf(stderr, " diff between lastack and now is %f \n", now - lastAck); if (now - lastAck > REPLY_TIMEOUT) { pthread_mutex_lock(&lock); rebuildpath = 1; pthread_mutex_unlock(&lock); fprintf(stderr, "Path timedout..setting the flag \n"); } } sleep(3); } } void die_handler(int sig) { exit(0); } void signal_handler(int sig) { char temp[200]; strncpy(temp, mykey, 10); temp[10] = '\0'; if (sig == SIGALRM) { if (bootstrap == 1) fprintf(stderr, "SENDER %s is dying \n", mykey); else if (receiver == 1) fprintf(stderr, "Final/Intermediate receiver %s is dying \n", mykey); else fprintf(stderr, "%s is dying \n", temp); /* ingraceperiod = 1; signal(SIGALRM, die_handler); alarm(EVAL_MESSAGE_DELAY); */ exit(0); } } void test_fwd(Key **kp, Message **mp, ChimeraHost **hp) { } void read_hosts_entries(char *filename) { FILE *fp = fopen ("./hosts", "r"); if (fp == NULL) { perror ("Error reading the file hosts\n"); exit (1); } char hn[200], keyinput[200]; int port; char s[1000]; randomKeysCount = 0; while (fgets (s, 256, fp) != NULL) { if (randomKeysCount == TEST_NETWORK_SIZE) break; sscanf (s, "%s %d %s", hn, &port, keyinput); str_to_key (keyinput, &randomKeys[randomKeysCount]); randomKeysCount++; } fclose (fp); } void send_handler (Key * key, Message * msg) { static double currentTime, lastTime; static double messageCounter = 0; // some randome key and message for now if (msg->type == CASHMERE_REPLY) { lastAck = getTime(); pthread_mutex_lock(&lock); rebuildpath = 0; pthread_mutex_unlock(&lock); fprintf(stderr, "Received ack %s \n", msg->payload); } else if (msg->type == CASHMERE_MESSAGE) { // if in graceperiod ... return if (ingraceperiod == 1) return; if (messageCounter == 0) { currentTime = lastTime = getTime(); receiver = 1; } else currentTime = getTime(); messageCounter++; log_message (state->log, LOG_CASHMERE, "calling process_cashmere_message in test file\n"); log_message (state->log, LOG_DATA, "Received %dth in the last %f secs \n", messageCounter, currentTime - lastTime); lastTime = currentTime; process_cashmere_message (state, msg, msg->type); } else if (msg->type == CASHMERE_GROUP_BROADCAST_MESSAGE || msg->type == CASHMERE_REPLY_GROUP_BROADCAST_MESSAGE) { log_message (state->log, LOG_CASHMERE, "calling group_broadcast in test file\n"); process_group_broadcast_message (state, msg, msg->type); } } int main(int argc, char **argv) { int opt; char *hn = NULL; int port, joinport; ChimeraHost *host = NULL; char tmp[256]; Key key, keyinput; int i, j; char *digest; char message[] = " Hello "; char *configfile = "./hosts"; mykey[0] = '\0'; signal(SIGALRM, signal_handler); /*fprintf(stderr, "%d arguments are ... ", argc); for ( i = 0; i < argc; i++) { fprintf(stderr, " %s ", argv[i]); } fprintf(stderr, "\n"); fflush(stderr); */ while((opt = getopt(argc, argv, OPTSTR)) != EOF) { switch((char) opt) { case 'j': for(i=0; optarg[i] != ':' && ilog, LOG_DATA, datalogfp); //log_direct(state->log, LOG_CASHMERE, cashlogfp); //log_direct(state->log, LOG_ROUTING, routingfp); //log_direct(state->log, LOG_NETWORKDEBUG, networklogfp); //log_direct(state->log, LOG_WARN, stderr); //log_direct(state->log, LOG_ERROR, stderr); cashmere_init (state); chimera_deliver (state, send_handler); if(host != NULL) { chimera_join(state,host); } bootstrap = 0; //fprintf(stderr,"** send messages to key with command **\n"); if(hn == NULL) { // The amount by which the driver script sleeps for the bootstrap node bootstrap = 1; sleep(5); // sleep for the same amount as the driver } int ii = 0; /* if (bootstrap == 1) { read_hosts_entries(configfile); pthread_t tid; if (pthread_create (&tid, NULL, ack_timeout, NULL) != 0) { fprintf(stderr, "Error creating a new thread \n"); } if (pthread_mutex_init(&lock, NULL) != 0) { fprintf(stderr, "Error initializing the lock \n"); } int i = 0; char payload[EVAL_MESSAGE_SIZE]; for( i = 0; i < EVAL_MESSAGE_SIZE - 2; i++) payload[i] = 'x'; payload[i] = '\0'; int numberOfFlows = 1; int j = 0; fprintf(stderr, "The receivers are the following ..\n"); for (j = 0; j < numberOfFlows; j++) fprintf(stderr, "flow %d receiver %s \n", j+1, randomKeys[j+1].keystr); sleep(20); lastPathClear = getTime(); fprintf(stderr, "Bootstrap node %s starts sending messages ... \n", mykey); for(i = 0; i < EVAL_MESSAGE_NUMBER; i++) { for (j = 0; j < numberOfFlows; j++) { sprintf(payload, "%d %d %s", j+1, i+1, mykey); double tttt = getTime(); if (rebuildpath == 1) { fprintf(stderr, "The flag to clear is set.. \n"); if (tttt - lastPathClear > PATH_CLEAR_THRESHOLD) { fprintf(stderr, "Clearing path rebuildpath = %d and Clearing after %f secs\n", rebuildpath, tttt - lastPathClear); cashmere_clear_sourcepathcache(state, randomKeys[j+1].keystr); pthread_mutex_lock(&lock); rebuildpath = 0; pathJustCleared = 1; pthread_mutex_unlock(&lock); lastPathClear = tttt; } } // just pick the 10th node away from the bootstrap node; no logic in choosing 10 cashmere_send(state, randomKeys[j+10], strlen(payload) + 1, payload); fprintf(stderr, "Sent seqnum %d flownum %d message at %f \n", i+1, j+1, tttt); if (lastAck == 0) lastAck = getTime(); // this is to start off the thread that does checks between acks } sleep(EVAL_MESSAGE_DELAY); } } */ while(1) // keep sending messages infinitely { sleep(1000); } //fclose(datalogfp); //fclose(securelogfp); fprintf(stderr, "exiting... \n"); exit(0); }