/** * prepare system on boot */ static void InitializeSystem(void) { #if defined(__18CXX) SetupTimer(); INTCONbits.GIEH = 1; #endif InitLED(); LED_Off(); InitReceiver(); ReceiverOff(); USBDeviceInit(); }
/* ========================================================================= */ int run_as_sender(struct optbase *ob) { /* This used to be char junkbuf[0]; but the compiler complained. junkbuf is a pointer to the top of the auto variables. The point is to have a pointer to some data - but nothing of particular value. */ int junk; char *junkbuf = (char *)&junk; struct roundtrip *rt; struct pc_comm *pcomm = NULL; int go; /* Used multiple times for "event loops" */ char child_message[80]; char mirror_address[MAX_VALUE_SIZE]; char mirror_protocol[MAX_VALUE_SIZE]; char mirror_port[MAX_VALUE_SIZE]; unsigned long multiplier; unsigned short optimal_block; unsigned long run_seconds; char sink_address[MAX_VALUE_SIZE]; char sink_protocol[MAX_VALUE_SIZE]; char sink_port[MAX_VALUE_SIZE]; time_t time_started; /* Really about reporting. */ time_t time_now; /* The most recent time. */ time_t time_stop; /* Do some basic validation of the expected values */ if ( IsInvalidOption(ob, "MIRROR_ADDRESS", IVO_EXISTS) ) { ErrorMessage("ERROR: MIRROR_ADDRESS option is missing or invalid.\n"); return(1); } if ( IsInvalidOption(ob, "MIRROR_PROTOCOL", IVO_EXISTS) ) { ErrorMessage("ERROR: MIRROR_PROTOCOL option is missing or invalid.\n"); return(1); } if ( IsInvalidOption(ob, "MIRROR_PORT", IVO_EXISTS) ) { ErrorMessage("ERROR: MIRROR_PORT option is missing or invalid.\n"); return(1); } if ( IsInvalidOption(ob, "RETURN_MULTIPLIER", IVO_NNNUMERIC) ) { ErrorMessage("ERROR: RETURN_MULTIPLIER option is missing or invalid.\n"); return(1); } if ( IsInvalidOption(ob, "OPTIMAL_BLOCK", IVO_EXISTS) ) { optimal_block = DEFAULT_OPTIMAL_BLOCK; } else { if ( GetUSValue(ob, &optimal_block, "OPTIMAL_BLOCK", 0, 65535) ) { ErrorMessage("ERROR: Problems parsing OPTIMAL_BLOCK value.\n"); return(1); } } if ( IsInvalidOption(ob, "RUN_FOR", IVO_EXISTS) ) { run_seconds = 0; } else { if ( GetTimeValue(ob, &run_seconds, "RUN_FOR", 0, ULONG_MAX) ) { ErrorMessage("ERROR: Problems parsing RUN_FOR value.\n"); return(1); } } if ( GetULValue(ob, &multiplier, "RETURN_MULTIPLIER", 0, ULONG_MAX) ) { ErrorMessage("ERROR: Problems parsing RETURN_MULTIPLIER value.\n"); return(1); } if ( multiplier > 0 ) { /* This *could* be derived - it should not be *required* */ if ( IsInvalidOption(ob, "SINK_ADDRESS", IVO_EXISTS) ) { ErrorMessage("ERROR: SINK_ADDRESS option is missing or invalid.\n"); return(1); } if ( IsInvalidOption(ob, "SINK_PORT", IVO_EXISTS) ) { ErrorMessage("ERROR: SINK_PORT option is missing or invalid.\n"); return(1); } if ( IsInvalidOption(ob, "SINK_PROTOCOL", IVO_EXISTS) ) { ErrorMessage("ERROR: SINK_PROTOCOL option is missing or invalid.\n"); return(1); } } if ( GetStrValue(ob, mirror_address, "MIRROR_ADDRESS", 4, MAX_VALUE_SIZE) ) { ErrorMessage("ERROR: Problems parsing MIRROR_ADDRESS value.\n"); return(1); } if ( GetSTRValue(ob, mirror_protocol, "MIRROR_PROTOCOL", 2, 4) ) { ErrorMessage("ERROR: Problems parsing MIRROR_PROTOCOL value.\n"); return(1); } if ( GetStrValue(ob, mirror_port, "MIRROR_PORT", 2, 5) ) { ErrorMessage("ERROR: Problems parsing MIRROR_PORT value.\n"); return(1); } if ( multiplier > 0 ) { if ( GetStrValue(ob, sink_address, "SINK_ADDRESS", 4, MAX_VALUE_SIZE) ) { ErrorMessage("ERROR: Problems parsing SINK_ADDRESS value.\n"); return(1); } if ( GetSTRValue(ob, sink_protocol, "SINK_PROTOCOL", 2, 4) ) { ErrorMessage("ERROR: Problems parsing SINK_PROTOCOL value.\n"); return(1); } if ( GetStrValue(ob, sink_port, "SINK_PORT", 2, 5) ) { ErrorMessage("ERROR: Problems parsing SINK_PORT value.\n"); return(1); } } ReportStart("netmirror", NULL, "A module to bounce packets across the network."); /* Register signal handlers */ /* signal(SIGTERM, capture_signal); signal(SIGQUIT, capture_signal); signal(SIGINT, capture_signal); signal(SIGHUP, NULL); */ if ( NULL == ( rt = InitRoundTrip() ) ) return(1); if ( InitSender(rt, mirror_address, mirror_protocol, mirror_port) ) return(1); if ( InitReceiver(rt, sink_address, sink_protocol, sink_port, multiplier) ) return(1); DebugMessage("Round trip multiplier is %lu.\n", multiplier); DebugMessage("Run time (in seconds) is: %lu.\n", run_seconds); if ( multiplier > 0 ) { if (NULL == (pcomm = LaunchAsSink("ADDR", "PROTO", "PORT"))) return(1); } /* Check for message from the sink (if we started it). */ if (pcomm) { if(CheckForChildMessages(pcomm->outof, 250, child_message) > 0) VerboseMessage("Sink: %s\n", child_message); /* A sink is present */ } rt->opt_block = optimal_block; rt->run_seconds = run_seconds; /* Start up the sender and send the opening packets */ if(StartSender(rt)) return(1); /* Take time here - use it or not */ time(&time_started); time_stop = time_started + run_seconds; /* Send packets - handle sink messages */ go = 1; while(go) { //DebugMessage("Sending data..."); send(rt->sd, junkbuf, optimal_block, 0); //DebugMessage("Done.\n"); /* NOTE: This is an oppurtunity to put a delay in the sender. It can be done here with the CheckForChildMessages() API. In cases where there is no sink, then that code will not run, so some other blocking mechanisim might be required to insert the (optional) delay. */ if (pcomm) { /* A sink is present */ if(CheckForChildMessages(pcomm->outof, 0, child_message) > 0) VerboseMessage("Sink: %s\n", child_message); } /* Now time becomes conditional */ if ( run_seconds > 0 ) { time(&time_now); if ( time_now >= time_stop ) go = 0; } } DebugMessage("Shutting down the connection..."); close(rt->sd); /* An actual shutdown() is not relevant. */ DebugMessage("Done.\n"); /* There should be a several second pause between shutting down outbound traffic and sending the stop to the sink. */ DebugMessage("Sending stop to sink process"); go = 3; while(go) { DebugMessage("."); sleep(1); go--; } SendStopSink(pcomm->into); DebugMessage("Done.\n"); go = 10; while ( go ) { if(CheckForChildMessages(pcomm->outof, 500, child_message) > 0) { if ( child_message[0] == '.' ) { VerboseMessage("Sink exited. Sender exiting now.\n"); return(0); } } go--; } ErrorMessage("ERROR: Sink process never exited. Sender exiting.\n"); return(1); }
int main(int argc, char **argv) { const char *addr = "127.0.0.1"; int port = 7766; if (argc != 2) { cout << "Please call with 0 if acting as server or 1 if acting as client" << endl; return 0; } // Determines whether the program is executed in the sender or receiver role m_nPID = atoi(argv[1]); cout << "Playing as role: " << m_nPID << endl; assert(m_nPID >= 0 && m_nPID <= 1); // The symmetric security parameter (80, 112, 128) uint32_t m_nSecParam = 128; crypto *crypt = new crypto(m_nSecParam, (uint8_t *)m_cConstSeed[m_nPID]); uint32_t m_nBaseOTs = 190; uint32_t m_nChecks = 380; ot_ext_prot lastprot = PROT_LAST; field_type lastfield = FIELD_LAST; if (m_nPID == SERVER_ID) // Play as OT sender { InitSender(addr, port); OTExtSnd *sender = NULL; for (uint32_t i = 0; i < m_nTests; i++) { if (lastprot != tests[i].prot || lastfield != tests[i].ftype) { // if(sender) delete sender; sender = InitOTExtSnd(tests[i].prot, m_nBaseOTs, m_nChecks, tests[i].usemecr, tests[i].ftype, crypt); lastprot = tests[i].prot; lastfield = tests[i].ftype; } cout << "Test " << i << ": " << getProt(tests[i].prot) << " Sender " << tests[i].numots << " " << getSndFlavor(tests[i].sflavor) << " / " << getRecFlavor(tests[i].rflavor) << " on " << tests[i].bitlen << " bits with " << tests[i].nthreads << " threads, " << getFieldType(tests[i].ftype) << " and" << (tests[i].usemecr ? "" : " no") << " MECR" << endl; run_test_sender(tests[i].numots, tests[i].bitlen, tests[i].sflavor, tests[i].rflavor, tests[i].nthreads, crypt, sender); } delete sender; } else // Play as OT receiver { InitReceiver(addr, port); OTExtRec *receiver = NULL; for (uint32_t i = 0; i < m_nTests; i++) { if (lastprot != tests[i].prot || lastfield != tests[i].ftype) { // if(receiver) delete receiver; receiver = InitOTExtRec(tests[i].prot, m_nBaseOTs, m_nChecks, tests[i].usemecr, tests[i].ftype, crypt); lastprot = tests[i].prot; lastfield = tests[i].ftype; } cout << "Test " << i << ": " << getProt(tests[i].prot) << " Receiver " << tests[i].numots << " " << getSndFlavor(tests[i].sflavor) << " / " << getRecFlavor(tests[i].rflavor) << " on " << tests[i].bitlen << " bits with " << tests[i].nthreads << " threads, " << getFieldType(tests[i].ftype) << " and" << (tests[i].usemecr ? "" : " no") << " MECR" << endl; run_test_receiver(tests[i].numots, tests[i].bitlen, tests[i].sflavor, tests[i].rflavor, tests[i].nthreads, crypt, receiver); } delete receiver; } Cleanup(); delete crypt; return 1; }
// // MAIN // // Main program loop, I/O polling and timing // void main () { // init ADCON1 = 0x7; // disable analog inputs TRISA = MASKPA; // set i/o config. TRISB = MASKPB; TRISC = MASKPC; PORTA = 0; // init all outputs PORTB = 0; PORTC = 0; OPTION = 0x8f; // prescaler assigned to WDT, // TMR0 clock/4, no pull ups CTLearn = 0; // Learn debounce CLearn = 0; // Learn timer COut = 0; // output timer CFlash = 0; // flash counter CTFlash = 0; // flash timer FLearn = FALSE; // start in normal mode F2Chance = FALSE; // no resynchronization required InitReceiver(); // enable and init the receiver state machine // main loop while ( TRUE) { if ( RFFull) // buffer contains a message Remote(); // loop waiting 512* period = 72ms if ( XTMR < 512) continue; // main loop // once every 72ms XTMR=0; // re-init fundamental registers ADCON1 = 0x7; // disable analog inputs TRISA = MASKPA; // set i/o config. TRISB = MASKPB; TRISC = MASKPC; OPTION = 0x0f; // prescaler assigned to WDT, TMR0 clock/4, pull up T0IE = 1; GIE = 1; // poll learn if ( !Learn) // low -> button pressed { CLearn++; // pressing Learn button for more than 10s -> ERASE ALL if (CLearn == 128) // 128 * 72 ms = 10s { Led = OFF; // switch off Learn Led while( !Learn); // wait for button release Led = ON; // signal Led on ClearMem(); // erase all comand! COut = TOUT; // single lomg flash pulse time // timer will switch off Led CLearn = 0; // reset learn debounce FLearn = FALSE; // exit learn mode } // normal Learn button debounce if (CLearn == 4) // 250ms debounce { FLearn = TRUE; // enter learn mode comand! CTLearn = TLEARN; // load timout value Led = ON; // turn Led on } } else CLearn=0; // reset counter // outputs timing if ( COut > 0) // if timer running { COut--; if ( COut == 0) // when it reach 0 { Led = OFF; // all outputs off Out0 = OFF; Out1 = OFF; Out2 = OFF; Out3 = OFF; Vlow = OFF; } } // Learn Mode timout after 18s (TLEARN * 72ms) if ( CTLearn > 0) { CTLearn--; // count down if ( CTLearn == 0) // if timed out { Led = OFF; // exit Learn mode FLearn = FALSE; } } // Led Flashing if ( CFlash > 0) { CTFlash--; // count down if ( CTFlash == 0) // if timed out { CTFlash = TFLASH; // reload timer CFlash--; // count one flash Led = OFF; // toggle Led if ( CFlash & 1) Led = ON; } } } // main loop } // main