/* * Try to make the needed options complete by looking through the data file, * environment variables and registry entries. */ void complete_options(void) { /* Try to find a descent RelDir */ if( !RelDir ) { DWORD sz = 32; while (1) { DWORD nsz; if (RelDir) free(RelDir); RelDir = malloc(sz); if (!RelDir) { fprintf(stderr, "** Error : failed to allocate memory\n"); exit(1); } SetLastError(0); nsz = GetEnvironmentVariable((LPCTSTR) "RELDIR", (LPTSTR) RelDir, sz); if (nsz == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { free(RelDir); RelDir = NULL; break; } else if (nsz <= sz) break; else sz = nsz; } if (RelDir == NULL) { if(DataFileName) { /* Needs to be absolute for this to work, but we can try... */ read_datafile(); read_registry_keys(); } else { /* Impossible to find all data... */ exit_help("Need either Release directory or an absolute " "datafile name."); } /* Ok, construct our own RelDir from RootDir */ RelDir = (char *) malloc(strlen(RootDir)+strlen(RELEASE_SUBDIR)+1); assert(RelDir); sprintf(RelDir, "%s" RELEASE_SUBDIR, RootDir); } else { read_datafile(); read_registry_keys(); } } else { read_datafile(); read_registry_keys(); } read_bootflags(); #ifdef _DEBUG fprintf(stderr, "RelDir: '%s'\n", RelDir); #endif }
/** verify DS matches DNSKEY from a file */ static void dstest_file(const char* fname) { /* * The file contains a list of ldns-testpkts entries. * The first entry must be a query for DNSKEY. * The answer rrset is the keyset that will be used for verification */ struct regional* region = regional_create(); struct alloc_cache alloc; ldns_buffer* buf = ldns_buffer_new(65535); struct entry* e; struct entry* list = read_datafile(fname); struct module_env env; if(!list) fatal_exit("could not read %s: %s", fname, strerror(errno)); alloc_init(&alloc, NULL, 1); memset(&env, 0, sizeof(env)); env.scratch = region; env.scratch_buffer = buf; unit_assert(region && buf); /* ready to go! */ for(e = list; e; e = e->next) { dstest_entry(e, &alloc, region, buf, &env); } delete_entry(list); regional_destroy(region); alloc_clear(&alloc); ldns_buffer_free(buf); }
/** verify from a file */ static void verifytest_file(const char* fname, const char* at_date) { /* * The file contains a list of ldns-testpkts entries. * The first entry must be a query for DNSKEY. * The answer rrset is the keyset that will be used for verification */ struct ub_packed_rrset_key* dnskey; struct regional* region = regional_create(); struct alloc_cache alloc; ldns_buffer* buf = ldns_buffer_new(65535); struct entry* e; struct entry* list = read_datafile(fname); struct module_env env; struct val_env ve; uint32_t now = time(NULL); if(!list) fatal_exit("could not read %s: %s", fname, strerror(errno)); alloc_init(&alloc, NULL, 1); memset(&env, 0, sizeof(env)); memset(&ve, 0, sizeof(ve)); env.scratch = region; env.scratch_buffer = buf; env.now = &now; ve.date_override = cfg_convert_timeval(at_date); unit_assert(region && buf); dnskey = extract_keys(list, &alloc, region, buf); if(vsig) log_nametypeclass(VERB_QUERY, "test dnskey", dnskey->rk.dname, ntohs(dnskey->rk.type), ntohs(dnskey->rk.rrset_class)); /* ready to go! */ for(e = list->next; e; e = e->next) { verifytest_entry(e, &alloc, region, buf, dnskey, &env, &ve); } ub_packed_rrset_parsedelete(dnskey, &alloc); delete_entry(list); regional_destroy(region); alloc_clear(&alloc); ldns_buffer_free(buf); }
void main(int argc, char *argv[]) { char *specfilename; int numRecords,beam; SpecRecord * dataset; FILE *specfile; if (argc != 3) { printf("Usage: %s <specfilename> <beam>\n", argv[0]); exit(1); } else { specfilename = argv[1]; beam = atoi(argv[2]); } printf("Reading data file %s. \n", specfilename); specfile = fopen(specfilename,"r"); numRecords = read_datafile(specfile, &dataset, beam); fclose(specfile); printf("Read %i records\n", numRecords); //print_data(dataset, numRecords, 0, 128); print_data(dataset, numRecords, 1000, 1001); }
int midi_play_graph (const char *fname, const char *userdir, const char *midiplayer) { char outname[FILENAME_MAX]; midi_spec spec; midi_track track; dataset dset; sprintf(outname, "%sgretl.mid", userdir); spec.fp = gretl_fopen(outname, "wb"); if (spec.fp == NULL) { fprintf(stderr, "Couldn't write to '%s'\n", outname); return 1; } if (read_datafile(fname, &dset)) { audio_graph_error("Error reading data file"); fclose(spec.fp); return 1; } spec.ntracks = 1 + dset.series2; spec.nticks = 96; spec.dset = &dset; spec.nsecs = 8; four_four_header(&spec); print_dataset_comments(&dset); play_dataset(&spec, &track, &dset); dataset_free(&dset); fclose(spec.fp); midi_fork(outname, midiplayer); return 0; }
/** Read file to test NSEC3 hash algo */ static void nsec3_hash_test(const char* fname) { /* * The list contains a list of ldns-testpkts entries. * Every entry is a test. * The qname is hashed. * The answer section AAAA RR name is the required result. * The auth section NSEC3 is used to get hash parameters. * The hash cache is maintained per file. * * The test does not perform canonicalization during the compare. */ rbtree_t ct; struct regional* region = regional_create(); struct alloc_cache alloc; ldns_buffer* buf = ldns_buffer_new(65535); struct entry* e; struct entry* list = read_datafile(fname); if(!list) fatal_exit("could not read %s: %s", fname, strerror(errno)); rbtree_init(&ct, &nsec3_hash_cmp); alloc_init(&alloc, NULL, 1); unit_assert(region && buf); /* ready to go! */ for(e = list; e; e = e->next) { nsec3_hash_test_entry(e, &ct, &alloc, region, buf); } delete_entry(list); regional_destroy(region); alloc_clear(&alloc); ldns_buffer_free(buf); }
/* * Try to make the needed options complete by looking through the data file, * environment variables and registry entries. */ void complete_options(void) { /* Try to find a descent RelDir */ if( !RelDir ) { DWORD sz = 32; while (1) { DWORD nsz; if (RelDir) free(RelDir); RelDir = malloc(sz); if (!RelDir) { fprintf(stderr, "** Error : failed to allocate memory\n"); exit(1); } SetLastError(0); nsz = GetEnvironmentVariable((LPCTSTR) "RELDIR", (LPTSTR) RelDir, sz); if (nsz == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { free(RelDir); RelDir = NULL; break; } else if (nsz <= sz) break; else sz = nsz; } if (RelDir == NULL) { if (!RootDir) { /* Impossible to find all data... */ exit_help("Need either Root directory nor Release directory."); } /* Ok, construct our own RelDir from RootDir */ RelDir = (char *) malloc(strlen(RootDir)+strlen(RELEASE_SUBDIR)+1); assert(RelDir); sprintf(RelDir, "%s" RELEASE_SUBDIR, RootDir); read_datafile(); } else { read_datafile(); } } else { read_datafile(); } if( !RootDir ) { /* Try to construct RootDir from RelDir */ char *p; RootDir = malloc(strlen(RelDir)+1); strcpy(RootDir,RelDir); p = RootDir+strlen(RootDir)-1; if (p >= RootDir && (*p == '/' || *p == '\\')) --p; while (p >= RootDir && *p != '/' && *p != '\\') --p; if (p <= RootDir) { /* Empty RootDir is also an error */ exit_help("Cannot determine Root directory from " "Release directory."); } *p = '\0'; } BinDir = (char *) malloc(strlen(RootDir)+strlen(ERTS_SUBDIR_PREFIX)+ strlen(Version)+strlen(BIN_SUBDIR)+1); assert(BinDir); sprintf(BinDir, "%s" ERTS_SUBDIR_PREFIX "%s" BIN_SUBDIR, RootDir, Version); read_bootflags(); #ifdef _DEBUG fprintf(stderr, "RelDir: '%s'\n", RelDir); fprintf(stderr, "BinDir: '%s'\n", BinDir); #endif }
int main(int argc, char **argv) { /* arguments */ int c; int port = DEFAULT_PORT; const char* datafile; int forknum = 0; /* network */ int fam = AF_INET; bool random_port_success; #ifdef USE_WINSOCK WSADATA wsa_data; #endif /* parse arguments */ srandom(time(NULL) ^ getpid()); logfile = stdout; prog_name = argv[0]; log_msg("%s: start\n", prog_name); while((c = getopt(argc, argv, "6f:p:rv")) != -1) { switch(c) { case '6': #ifdef AF_INET6 fam = AF_INET6; #else log_msg("cannot -6: no IP6 available\n"); exit(1); #endif break; case 'r': port = 0; break; case 'f': forknum = atoi(optarg); if(forknum < 1) error("invalid forkno %s, give number", optarg); break; case 'p': port = atoi(optarg); if (port < 1) { error("Invalid port %s, use a number.", optarg); } break; case 'v': do_verbose++; break; default: usage(); break; } } argc -= optind; argv += optind; if(argc == 0 || argc > 1) usage(); datafile = argv[0]; log_msg("Reading datafile %s\n", datafile); entries = read_datafile(datafile, 0); #ifdef USE_WINSOCK if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) error("WSAStartup failed\n"); #endif if((udp_sock = socket(fam, SOCK_DGRAM, 0)) < 0) { error("udp socket(): %s\n", strerror(errno)); } if((tcp_sock = socket(fam, SOCK_STREAM, 0)) < 0) { error("tcp socket(): %s\n", strerror(errno)); } c = 1; if(setsockopt(tcp_sock, SOL_SOCKET, SO_REUSEADDR, (void*)&c, (socklen_t) sizeof(int)) < 0) { error("setsockopt(SO_REUSEADDR): %s\n", strerror(errno)); } /* bind ip4 */ if (port > 0) { if (bind_port(udp_sock, port, fam)) { error("cannot bind(): %s\n", strerror(errno)); } if (bind_port(tcp_sock, port, fam)) { error("cannot bind(): %s\n", strerror(errno)); } if (listen(tcp_sock, CONN_BACKLOG) < 0) { error("listen(): %s\n", strerror(errno)); } } else { random_port_success = false; while (!random_port_success) { port = (random() % 64510) + 1025; log_msg("trying to bind to port %d\n", port); random_port_success = true; if (bind_port(udp_sock, port, fam)) { #ifdef EADDRINUSE if (errno != EADDRINUSE) { #elif defined(USE_WINSOCK) if (WSAGetLastError() != WSAEADDRINUSE) { #else if (1) { #endif perror("bind()"); return -1; } else { random_port_success = false; } } if (random_port_success) { if (bind_port(tcp_sock, port, fam)) { #ifdef EADDRINUSE if (errno != EADDRINUSE) { #elif defined(USE_WINSOCK) if (WSAGetLastError()!=WSAEADDRINUSE) { #else if (1) { #endif perror("bind()"); return -1; } else { random_port_success = false; } } } if (random_port_success) { if (listen(tcp_sock, CONN_BACKLOG) < 0) { error("listen(): %s\n", strerror(errno)); } } } } log_msg("Listening on port %d\n", port); /* forky! */ if(forknum > 0) forkit(forknum); service(); return 0; }
int main (int argc, char **argv) { int i; /* Loop index. */ des_key_manager km; /* Key manager. */ double delta = 0, new_delta; unsigned long long key, bestkey, finalkey, mask; int sbox; int k; /************************************************************************/ /* Before doing anything else, check the correctness of the DES library */ /************************************************************************/ if (!des_check ()) { ERROR (-1, "DES functional test failed"); } /*************************************/ /* Check arguments and read datafile */ /*************************************/ /* If invalid number of arguments (including program name), exit with error * message. */ if (argc != 3) { ERROR (-1, "usage: ta <datafile> <nexp>\n"); } /* Number of experiments to use is argument #2, convert it to integer and * store the result in variable n. */ n = atoi (argv[2]); if (n < 1) /* If invalid number of experiments. */ { ERROR (-1, "number of experiments to use (<nexp>) shall be greater than 1 (%d)", n); } read_datafile (argv[1], /* Name of data file is argument #1. */ n /* Number of experiments to use. */ ); /***************************************************************************** * Compute the Hamming weight of output of first (leftmost) SBox during last * * round, under the assumption that the last round key is all zeros. * *****************************************************************************/ /* per ogni cipher provo tutte le combinazioni dei primi 6 bit della key // medio i tempi dei cipher reali che dopo la sbox hanno hamming 0 e 4 // delta tra le due medie // cambio key e riprovo, terro la key che ha delta maggiore // ripetero per le box successive */ /* for every SBox */ finalkey = 0ULL; for(k=0; k<1; k++){ for (sbox = 7; sbox >= 0; sbox--){ mask = 63ULL << (42 - 6*sbox); finalkey &= ~mask; /* printf("mask :%012" PRIx64 "\n", ~mask); */ for (i = 0; i < 64; i++){ key = ((unsigned long long) i) << (42 - 6*sbox); new_delta = try_key(finalkey | key, sbox); if (new_delta > delta) { delta = new_delta; bestkey = key; } } delta = 0; finalkey |= bestkey; /*printf("finalkey:%012" PRIx64 "\n", finalkey);*/ } } /******************************************************************************* * Try all the 256 secret keys under the assumption that the last round key is * * all zeros. * *******************************************************************************/ /* If we are lucky, the secret key is one of the 256 possible with a all zeros * last round key. Let's try them all, using the known plain text - cipher text * pair as an oracle. */ km = des_km_init (); /* Initialize the key manager with no knowledge. */ /* Tell the key manager that we 'know' the last round key (#16) is all zeros. */ des_km_set_rk (km, /* Key manager */ 16, /* Round key number */ 1, /* Force (we do not care about conflicts with pre-existing knowledge) */ UINT64_C (0xffffffffffff), /* We 'know' all the 48 bits of the round key */ finalkey ); /* Brute force attack with the knowledge we have and a known * plain text - cipher text pair as an oracle. */ if (!brute_force (km, pt, ct[0])) { /* //printf ("Too bad, we lose: the last round key is not all zeros.\n"); */ } free (ct); /* Deallocate cipher texts */ free (t); /* Deallocate timings */ des_km_free (km); /* Deallocate the key manager */ /* printf("\n****\nReal key was: e59dcd40dc51b56d\n */ return 0; /* Exits with "everything went fine" status. */ }