int main (int argc, char *argv[]) { INIT_SECURITY_INTERFACE pInitSecurityInterface; // set buffer width of console setw (300); puts ("\n [ cms v0.1 - Copyleft 2015 (x) @Odzhan\n"); // set up default values args.address = NULL; args.ai_family = AF_INET; args.port = DEFAULT_PORT; args.port_nbr = atoi(args.port); pInitSecurityInterface = (INIT_SECURITY_INTERFACE)GetProcAddress(LoadLibrary("Secur32"), "InitSecurityInterfaceA" ); if (pInitSecurityInterface==NULL) printf ("didn't resolve"); sspi = pInitSecurityInterface(); // process command line parse_args(argc, argv); // resolve address and open socket if (open_tcp ()) { start_handler (); // create credentials if (create_creds()==SEC_E_OK) { // connect to server if (connect (s, ai_addr, ai_addrlen) != SOCKET_ERROR) { // perform the handshake if (chs () == SEC_E_OK) { printf (" [ connected\n\n"); secure_info(); ss=sspi->QueryContextAttributes (&hContext, SECPKG_ATTR_STREAM_SIZES, &Sizes ); cbBufferLen = Sizes.cbHeader + Sizes.cbMaximumMessage + Sizes.cbTrailer; pbBufferIn = LocalAlloc(LMEM_FIXED, cbBufferLen); pbBufferOut = LocalAlloc(LMEM_FIXED, cbBufferLen); pbDataIn=pbBufferIn + Sizes.cbHeader; pbDataOut=pbBufferOut + Sizes.cbHeader; cbBufferLen = Sizes.cbMaximumMessage; printf (" [ running cmd\n"); cmd(); } else { printf (" [ handshake failed\n"); } } else { printf (" [ unable to connect\n"); } } else { printf (" [ error creating credentials\n"); } stop_handler (); close_tcp(); } return 0; }
PROCESS_THREAD(query_process, ev, data) { static db_handle_t handle; db_result_t result; static tuple_id_t matching; static tuple_id_t processed; #if !PREPARE_DB static struct etimer sampling_timer; #endif static unsigned i, errors; PROCESS_BEGIN(); printf("NetDB host\n"); db_init(); db_set_output_function(buffer_db_data); db_query(NULL, "REMOVE RELATION samples;"); db_query(NULL, "CREATE RELATION samples;"); db_query(NULL, "CREATE ATTRIBUTE time DOMAIN INT IN samples;"); db_query(NULL, "CREATE ATTRIBUTE hum DOMAIN INT IN samples;"); db_query(NULL, "CREATE INDEX samples.time TYPE INLINE;"); #if PREPARE_DB printf("Preparing the DB with %d tuples...\n", CARDINALITY); errors = 0; for(i = 1; i <= CARDINALITY; i++) { PROCESS_PAUSE(); result = db_query(NULL, "INSERT (%u, %u) INTO samples;", i, (unsigned)random_rand()); if(DB_ERROR(result)) { errors++; } } printf("Done. Insertion errors: %d\n", errors); printf("Ready to process queries\n"); #else etimer_set(&sampling_timer, SAMPLING_INTERVAL * CLOCK_SECOND); #endif for(;;) { PROCESS_WAIT_EVENT(); if(ev == serial_line_event_message && data != NULL) { printf("START %s\n", (char *)data); result = db_query(&handle, data); if(DB_ERROR(result)) { buffer_db_data("Query error: %s\n", db_get_result_message(result)); stop_handler(NULL); db_free(&handle); continue; } if(!db_processing(&handle)) { buffer_db_data("OK\n"); send_buffered_data(); stop_handler(NULL); continue; } packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_STREAM); db_print_header(&handle); matching = 0; processed = 0; while(db_processing(&handle)) { PROCESS_PAUSE(); if(matching == RESPONSE_LIMIT) { buffer_db_data("Response suppressed at %u tuples: limit reached\n", RESPONSE_LIMIT); stop_handler(NULL); db_free(&handle); break; } result = db_process(&handle); if(result == DB_GOT_ROW) { /* The processed tuple matched the condition in the query. */ matching++; processed++; db_print_tuple(&handle); } else if(result == DB_OK) { /* A tuple was processed, but did not match the condition. */ processed++; continue; } else { if(result == DB_FINISHED) { /* The processing has finished. Wait for a new command. */ buffer_db_data("[%ld tuples returned; %ld tuples processed]\n", (long)matching, (long)processed); buffer_db_data("OK\n"); } else if(DB_ERROR(result)) { buffer_db_data("Processing error: %s\n", db_get_result_message(result)); } stop_handler(NULL); db_free(&handle); } } send_buffered_data(); } #if !PREPARE_DB if(etimer_expired(&sampling_timer)) { take_sample(); etimer_reset(&sampling_timer); } #endif } PROCESS_END(); }