int main(){ char buf[SIZE]; int students_number; student_t *table; float avg_value; printf("Dwse ton arithmo twn mathitwn : "); fgets(buf, SIZE, stdin); students_number = atoi(buf); if(students_number <= 0){ printf("ERROR : lathos arithmos mathhtwn"); return 0; } table = create(students_number); printf("\n"); printf("\n"); printf("\n"); printf("Details \n"); print_details(table, students_number); avg_value = average(table, students_number); printf("AVERAGE : %f\n ", avg_value); printf("\n"); printf("Students who failed : \n"); show_failed(table, students_number); printf("\n"); printf("New names : \n"); changed_failed_name(table, students_number); show_failed(table, students_number); //free malloc free(table); return 0; }
void Measurements::print() { if (!Measure) return; fprintf(stdout, "\n\nMeasurements (baseline = %ld):\n", baseline); print_details(); print_summaries(); }
void main() { process_details(); //ask_process_details(); print_details(); calculate_totlatime(); sort_arrivaltime(); round_robin(); }
Superblock::Superblock(char* dump, unsigned long dump_size, int offset) { cout << "Searching for Superblock..." << endl; findSB(dump, dump_size, offset); s_blockID = s_offset / (1024 << get_struct()->s_log_block_size); print_details(); }
char *nextprint(char *str, t_opt opt) { if (opt.get['.'] == ON) str = print_details(str, opt); if (opt.get['+'] == ON && *str != '-') str = ft_strjoin("+", str); else if (opt.width == 0 && opt.get[' '] == ON && *str != '-') str = ft_strjoin(" ", str); return (final_print(str, opt)); }
static void lk_fini(Int exitcode) { char percentify_buf[5]; /* Two digits, '%' and 0. */ const int percentify_size = sizeof(percentify_buf) - 1; const int percentify_decs = 0; tl_assert(clo_fnname); tl_assert(clo_fnname[0]); if (clo_basic_counts) { ULong total_Jccs = n_Jccs + n_IJccs; ULong taken_Jccs = (n_Jccs - n_Jccs_untaken) + n_IJccs_untaken; VG_(umsg)("Counted %'llu call%s to %s()\n", n_func_calls, ( n_func_calls==1 ? "" : "s" ), clo_fnname); VG_(umsg)("\n"); VG_(umsg)("Jccs:\n"); VG_(umsg)(" total: %'llu\n", total_Jccs); VG_(percentify)(taken_Jccs, (total_Jccs ? total_Jccs : 1), percentify_decs, percentify_size, percentify_buf); VG_(umsg)(" taken: %'llu (%s)\n", taken_Jccs, percentify_buf); VG_(umsg)("\n"); VG_(umsg)("Executed:\n"); VG_(umsg)(" SBs entered: %'llu\n", n_SBs_entered); VG_(umsg)(" SBs completed: %'llu\n", n_SBs_completed); VG_(umsg)(" guest instrs: %'llu\n", n_guest_instrs); VG_(umsg)(" IRStmts: %'llu\n", n_IRStmts); VG_(umsg)("\n"); VG_(umsg)("Ratios:\n"); tl_assert(n_SBs_entered); // Paranoia time. VG_(umsg)(" guest instrs : SB entered = %'llu : 10\n", 10 * n_guest_instrs / n_SBs_entered); VG_(umsg)(" IRStmts : SB entered = %'llu : 10\n", 10 * n_IRStmts / n_SBs_entered); tl_assert(n_guest_instrs); // Paranoia time. VG_(umsg)(" IRStmts : guest instr = %'llu : 10\n", 10 * n_IRStmts / n_guest_instrs); } if (clo_detailed_counts) { VG_(umsg)("\n"); VG_(umsg)("IR-level counts by type:\n"); print_details(); } if (clo_basic_counts) { VG_(umsg)("\n"); VG_(umsg)("Exit code: %d\n", exitcode); } }
static void lk_fini(Int exitcode) { tl_assert(clo_fnname); tl_assert(clo_fnname[0]); if (clo_basic_counts) { ULong total_Jccs = n_Jccs + n_IJccs; ULong taken_Jccs = (n_Jccs - n_Jccs_untaken) + n_IJccs_untaken; VG_(umsg)("Counted %'llu call%s to %s()\n", n_func_calls, ( n_func_calls==1 ? "" : "s" ), clo_fnname); VG_(umsg)("\n"); VG_(umsg)("Jccs:\n"); VG_(umsg)(" total: %'llu\n", total_Jccs); VG_(umsg)(" taken: %'llu (%.0f%%)\n", taken_Jccs, taken_Jccs * 100.0 / (total_Jccs ? total_Jccs : 1)); VG_(umsg)("\n"); VG_(umsg)("Executed:\n"); VG_(umsg)(" SBs entered: %'llu\n", n_SBs_entered); VG_(umsg)(" SBs completed: %'llu\n", n_SBs_completed); VG_(umsg)(" guest instrs: %'llu\n", n_guest_instrs); VG_(umsg)(" IRStmts: %'llu\n", n_IRStmts); VG_(umsg)("\n"); VG_(umsg)("Ratios:\n"); tl_assert(n_SBs_entered); // Paranoia time. VG_(umsg)(" guest instrs : SB entered = %'llu : 10\n", 10 * n_guest_instrs / n_SBs_entered); VG_(umsg)(" IRStmts : SB entered = %'llu : 10\n", 10 * n_IRStmts / n_SBs_entered); tl_assert(n_guest_instrs); // Paranoia time. VG_(umsg)(" IRStmts : guest instr = %'llu : 10\n", 10 * n_IRStmts / n_guest_instrs); } if (clo_detailed_counts) { VG_(umsg)("\n"); VG_(umsg)("IR-level counts by type:\n"); print_details(); } if (clo_basic_counts) { VG_(umsg)("\n"); VG_(umsg)("Exit code: %d\n", exitcode); } }
static void ascii_draw_item(item_t *i, void *arg) { node_t *node = arg; switch (c_diagram_type) { case D_LIST: print_list(i, node); break; case D_DETAILS: print_details(i); break; case D_GRAPH: print_graph(i); break; } }
/*}}}*/ static void list_chain(struct links *x, int indent, int depth, const struct list_options *options, char *index_buffer, enum Priority prio, time_t now, unsigned char *hits)/*{{{*/ { struct node *y; int idx, is_done, is_deferred, is_postponed; int show_node; char component_buffer[8]; char new_index_buffer[64]; for (y = x->next, idx = 1; y != (struct node *) x; y = y->chain.next, ++idx) { is_done = (y->done > 0); is_postponed = (y->arrived == POSTPONED_TIME); is_deferred = (y->arrived > now); show_node = options->show_all || (options->show_postponed && !is_done) || (!is_deferred && !is_postponed); if (!show_node) continue; sprintf(component_buffer, "%d", idx); strcpy(new_index_buffer, index_buffer); if (strlen(new_index_buffer) > 0) { strcat(new_index_buffer, "."); } strcat(new_index_buffer, component_buffer); if (y->priority >= prio) { int summarise_kids = (options->set_depth && (options->depth == depth)); if (hits[y->iscratch]) { print_details(y, indent, summarise_kids, options, new_index_buffer, now); } } /* Maybe list children regardless of priority assigned to parent. */ if (!options->set_depth || (depth < options->depth)) { list_chain(&y->kids, indent + INDENT_TAB, depth + 1, options, new_index_buffer, prio, now, hits); } } return; }
static void lk_fini(Int exitcode) { HChar percentify_buf[5]; /* Two digits, '%' and 0. */ const int percentify_size = sizeof(percentify_buf) - 1; const int percentify_decs = 0; tl_assert(clo_fnname); tl_assert(clo_fnname[0]); if (clo_basic_counts) { ULong total_Jccs = n_Jccs + n_IJccs; ULong taken_Jccs = (n_Jccs - n_Jccs_untaken) + n_IJccs_untaken; VG_(umsg)("Counted %'llu call%s to %s()\n", n_func_calls, ( n_func_calls==1 ? "" : "s" ), clo_fnname); VG_(umsg)("\n"); VG_(umsg)("Jccs:\n"); VG_(umsg)(" total: %'llu\n", total_Jccs); VG_(percentify)(taken_Jccs, (total_Jccs ? total_Jccs : 1), percentify_decs, percentify_size, percentify_buf); VG_(umsg)(" taken: %'llu (%s)\n", taken_Jccs, percentify_buf); VG_(umsg)("\n"); VG_(umsg)("Executed:\n"); VG_(umsg)(" SBs entered: %'llu\n", n_SBs_entered); VG_(umsg)(" SBs completed: %'llu\n", n_SBs_completed); VG_(umsg)(" guest instrs: %'llu\n", n_guest_instrs); VG_(umsg)(" IRStmts: %'llu\n", n_IRStmts); VG_(umsg)("\n"); VG_(umsg)("Ratios:\n"); tl_assert(n_SBs_entered); // Paranoia time. VG_(umsg)(" guest instrs : SB entered = %'llu : 10\n", 10 * n_guest_instrs / n_SBs_entered); VG_(umsg)(" IRStmts : SB entered = %'llu : 10\n", 10 * n_IRStmts / n_SBs_entered); tl_assert(n_guest_instrs); // Paranoia time. VG_(umsg)(" IRStmts : guest instr = %'llu : 10\n", 10 * n_IRStmts / n_guest_instrs); } if (clo_detailed_counts) { VG_(umsg)("\n"); VG_(umsg)("IR-level counts by type:\n"); print_details(); } if (clo_basic_counts) { VG_(umsg)("\n"); VG_(umsg)("Exit code: %d\n", exitcode); } //Write results to the file char fileName [1000]; VG_(snprintf)(fileName,1000,"%s%s_load.csv",clo_output_dir,clo_program_name); lk_write_results(tableLoad,fileName); VG_(snprintf)(fileName,1000,"%s%s_store.csv",clo_output_dir,clo_program_name); lk_write_results(tableStore,fileName); VG_(snprintf)(fileName,1000,"%s%s_modify.csv",clo_output_dir,clo_program_name); lk_write_results(tableModify,fileName); VG_(snprintf)(fileName,1000,"%s%s_global.csv",clo_output_dir,clo_program_name); lk_write_global(fileName); //CLEANUP allocated memory by the hashtable //Report Size VG_(printf)("Table Load Size:%d\n",VG_(HT_count_nodes)(tableLoad)); VG_(printf)("Table Store Size:%d\n",VG_(HT_count_nodes)(tableStore)); VG_(printf)("Table Modify Size:%d\n",VG_(HT_count_nodes)(tableModify)); VG_(HT_destruct) ( tableLoad, freenode_function ); VG_(HT_destruct) ( tableModify, freenode_function ); VG_(HT_destruct) ( tableStore, freenode_function ); VG_(printf)("\nTOTAL INSTRUCTIONS:%lu\n",global_counter); }
/*}}}*/ int process_list(char **x)/*{{{*/ { struct list_options options; int options_done = 0; int any_paths = 0; char index_buffer[256]; char *y; enum Priority prio = PRI_NORMAL, prio_to_use, node_prio; int prio_set = 0; time_t now = time(NULL); unsigned char *hits; int node_index, n_nodes; options.monochrome = 0; options.show_all = 0; options.show_postponed = 0; options.verbose = 0; options.set_depth = 0; if ( (getenv("TDL_LIST_MONOCHROME") != NULL) || (isatty(STDOUT_FILENO) == 0) ) { options.monochrome = 1; } /* Initialisation to support searching */ node_index = 0; allocate_indices(&top, &node_index); n_nodes = node_index; hits = n_nodes ? new_array(unsigned char, n_nodes) : NULL; /* all nodes match until proven otherwise */ memset(hits, 1, n_nodes); while ((y = *x) != 0) { /* An argument starting '1' or '+1' or '+-1' (or '-1' after '--') is * treated as the path of the top node to show */ if (isdigit(y[0]) || (y[0] == '.') || (options_done && (y[0] == '-') && isdigit(y[1])) || ((y[0] == '+') && (isdigit(y[1]) || ((y[1] == '-' && isdigit(y[2])))))) { struct node *n = lookup_node(y, 0, NULL); int summarise_kids; if (!n) return -1; any_paths = 1; index_buffer[0] = '\0'; strcat(index_buffer, y); summarise_kids = (options.set_depth && (options.depth==0)); if (hits[n->iscratch]) { print_details(n, 0, summarise_kids, &options, index_buffer, now); } if (!options.set_depth || (options.depth > 0)) { node_prio = n->priority; /* If the priority has been set on the cmd line, always use that. * Otherwise, use the priority from the specified node, _except_ when * that is higher than normal, in which case use normal. */ prio_to_use = (prio_set) ? prio : ((node_prio > prio) ? prio : node_prio); list_chain(&n->kids, INDENT_TAB, 0, &options, index_buffer, prio_to_use, now, hits); } } else if ((y[0] == '-') && (y[1] == '-')) { options_done = 1; } else if (y[0] == '-') { while (*++y) { switch (*y) { case 'v': options.verbose = 1; break; case 'a': options.show_all = 1; break; case 'm': options.monochrome = 1; break; case 'p': options.show_postponed = 1; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': options.set_depth = 1; options.depth = (*y) - '1'; break; default: fprintf(stderr, "Unrecognized option : -%c\n", *y); break; } } } else if (y[0] == '/') { /* search expression */ merge_search_condition(hits, n_nodes, y+1); } else { int error; prio = parse_priority(y, &error); if (error < 0) return error; prio_set = 1; } x++; } colours_init(&options); if (!any_paths) { struct node *narrow_top = get_narrow_top(); if (narrow_top) { index_buffer[0] = 0; if (hits[narrow_top->iscratch]) { int summarise_kids = (options.set_depth && (options.depth==0)); print_details(narrow_top, 0, summarise_kids, &options, index_buffer, now); } if (!options.set_depth || (options.depth > 0)) { list_chain(&narrow_top->kids, 0, 1, &options, index_buffer, prio, now, hits); } } else { index_buffer[0] = 0; list_chain(&top, 0, 0, &options, index_buffer, prio, now, hits); } } if (hits) free(hits); return 0; }
static bool arcls(const Arc::URL& dir_url, Arc::UserConfig& usercfg, bool show_details, // longlist bool show_urls, // locations bool show_meta, // metadata bool no_list, // don't list dirs bool force_list, // force dir list bool check_access, // checkaccess int recursion, // recursion int timeout) { // timeout if (!dir_url) { logger.msg(Arc::ERROR, "Invalid URL: %s", dir_url.fullstr()); return false; } if (dir_url.Protocol() == "urllist") { std::list<Arc::URL> dirs = Arc::ReadURLList(dir_url); if (dirs.empty()) { logger.msg(Arc::ERROR, "Can't read list of locations from file %s", dir_url.Path()); return false; } bool r = true; for (std::list<Arc::URL>::iterator dir = dirs.begin(); dir != dirs.end(); dir++) { if(!arcls(*dir, usercfg, show_details, show_urls, show_meta, no_list, force_list, check_access, recursion, timeout)) r = false; } return r; } Arc::DataHandle url(dir_url, usercfg); if (!url) { logger.msg(Arc::ERROR, "Unsupported URL given"); return false; } if (url->RequiresCredentials()) { if (usercfg.ProxyPath().empty() ) { logger.msg(Arc::ERROR, "Unable to list content of %s: No valid credentials found", dir_url.str()); return false; } Arc::Credential holder(usercfg.ProxyPath(), "", "", ""); if (holder.GetEndTime() < Arc::Time()){ logger.msg(Arc::ERROR, "Proxy expired"); logger.msg(Arc::ERROR, "Unable to list content of %s: No valid credentials found", dir_url.str()); return false; } } url->SetSecure(false); if(check_access) { std::cout << dir_url << " - "; if(url->Check(false)) { std::cout << "passed" << std::endl; return true; } else { std::cout << "failed" << std::endl; return false; } } Arc::DataPoint::DataPointInfoType verb = (Arc::DataPoint::DataPointInfoType) (Arc::DataPoint::INFO_TYPE_MINIMAL | Arc::DataPoint::INFO_TYPE_NAME); if(show_urls) verb = (Arc::DataPoint::DataPointInfoType) (verb | Arc::DataPoint::INFO_TYPE_STRUCT); if(show_meta) verb = (Arc::DataPoint::DataPointInfoType) (verb | Arc::DataPoint::INFO_TYPE_ALL); if(show_details) verb = (Arc::DataPoint::DataPointInfoType) (verb | Arc::DataPoint::INFO_TYPE_TYPE | Arc::DataPoint::INFO_TYPE_TIMES | Arc::DataPoint::INFO_TYPE_CONTENT | Arc::DataPoint::INFO_TYPE_ACCESS); if(recursion > 0) verb = (Arc::DataPoint::DataPointInfoType) (verb | Arc::DataPoint::INFO_TYPE_TYPE); Arc::DataStatus res; Arc::FileInfo file; std::list<Arc::FileInfo> files; if(no_list) { // only requested object is queried res = url->Stat(file, verb); if(res) files.push_back(file); } else if(force_list) { // assume it is directory, fail otherwise res = url->List(files, verb); } else { // try to guess what to do res = url->Stat(file, (Arc::DataPoint::DataPointInfoType)(verb | Arc::DataPoint::INFO_TYPE_TYPE)); if(res && (file.GetType() == Arc::FileInfo::file_type_file)) { // If it is file and we are sure, then just report it. files.push_back(file); } else { // If it is dir then we must list it. But if stat failed or // if type is undefined there is still chance it is directory. Arc::DataStatus res_ = url->List(files, verb); if(!res_) { // If listing failed maybe simply report previous result if any. if(res) { files.push_back(file); } } else { res = res_; } } } if (!res) { if (files.empty()) { logger.msg(Arc::ERROR, std::string(res)); if (res.Retryable()) logger.msg(Arc::ERROR, "This seems like a temporary error, please try again later"); return false; } logger.msg(Arc::INFO, "Warning: " "Failed listing files but some information is obtained"); } files.sort(); // Sort alphabetically by name if (show_details) { print_details(files, show_urls, show_meta); } else { for (std::list<Arc::FileInfo>::iterator i = files.begin(); i != files.end(); i++) { std::cout << i->GetName() << std::endl; if (show_urls) print_urls(*i); if (show_meta) print_meta(*i); } } // Do recursion. Recursion has no sense if listing is forbidden. if ((recursion > 0) && (!no_list)) { for (std::list<Arc::FileInfo>::iterator i = files.begin(); i != files.end(); i++) { if (i->GetType() == Arc::FileInfo::file_type_dir) { Arc::URL suburl = dir_url; if(suburl.Protocol() != "file") { if (suburl.Path()[suburl.Path().length() - 1] != '/') suburl.ChangePath(suburl.Path() + "/" + i->GetName()); else suburl.ChangePath(suburl.Path() + i->GetName()); } else { if (suburl.Path()[suburl.Path().length() - 1] != G_DIR_SEPARATOR) suburl.ChangePath(suburl.Path() + G_DIR_SEPARATOR_S + i->GetName()); else suburl.ChangePath(suburl.Path() + i->GetName()); } std::cout << std::endl; std::cout << suburl.str() << ":" << std::endl; arcls(suburl, usercfg, show_details, show_urls, show_meta, no_list, force_list, check_access, recursion - 1, timeout); std::cout << std::endl; } } } return true; }
int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count, clock_t *s_time, clock_t *c_time) { long cw_num = count, cr_num = count, sw_num = count, sr_num = count; BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL; BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL; int ret = 1; size_t bufsiz = 256; /* small buffer for testing */ if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz)) goto err; if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz)) goto err; s_ssl_bio = BIO_new(BIO_f_ssl()); if (!s_ssl_bio) goto err; c_ssl_bio = BIO_new(BIO_f_ssl()); if (!c_ssl_bio) goto err; SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, client, client); (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE); SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, server, server); (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE); do { /* c_ssl_bio: SSL filter BIO * * client: pseudo-I/O for SSL library * * client_io: client's SSL communication; usually to be * relayed over some I/O facility, but in this * test program, we're the server, too: * * server_io: server's SSL communication * * server: pseudo-I/O for SSL library * * s_ssl_bio: SSL filter BIO * * The client and the server each employ a "BIO pair": * client + client_io, server + server_io. * BIO pairs are symmetric. A BIO pair behaves similar * to a non-blocking socketpair (but both endpoints must * be handled by the same thread). * [Here we could connect client and server to the ends * of a single BIO pair, but then this code would be less * suitable as an example for BIO pairs in general.] * * Useful functions for querying the state of BIO pair endpoints: * * BIO_ctrl_pending(bio) number of bytes we can read now * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil * other side's read attempt * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now * * ..._read_request is never more than ..._write_guarantee; * it depends on the application which one you should use. */ /* We have non-blocking behaviour throughout this test program, but * can be sure that there is *some* progress in each iteration; so * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE * -- we just try everything in each iteration */ { /* CLIENT */ MS_STATIC char cbuf[1024*8]; int i, r; clock_t c_clock = clock(); memset(cbuf, 0, sizeof(cbuf)); if (debug) if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); if (cw_num > 0) { /* Write to server. */ if (cw_num > (long)sizeof cbuf) i = sizeof cbuf; else i = (int)cw_num; r = BIO_write(c_ssl_bio, cbuf, i); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr,"ERROR in CLIENT\n"); goto err; } /* BIO_should_retry(...) can just be ignored here. * The library expects us to call BIO_write with * the same arguments again, and that's what we will * do in the next iteration. */ } else if (r == 0) { fprintf(stderr,"SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", r); cw_num -= r; } } if (cr_num > 0) { /* Read from server. */ r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf)); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr,"ERROR in CLIENT\n"); goto err; } /* Again, "BIO_should_retry" can be ignored. */ } else if (r == 0) { fprintf(stderr,"SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", r); cr_num -= r; } } /* c_time and s_time increments will typically be very small * (depending on machine speed and clock tick intervals), * but sampling over a large number of connections should * result in fairly accurate figures. We cannot guarantee * a lot, however -- if each connection lasts for exactly * one clock tick, it will be counted only for the client * or only for the server or even not at all. */ *c_time += (clock() - c_clock); } { /* SERVER */ MS_STATIC char sbuf[1024*8]; int i, r; clock_t s_clock = clock(); memset(sbuf, 0, sizeof(sbuf)); if (debug) if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); if (sw_num > 0) { /* Write to client. */ if (sw_num > (long)sizeof sbuf) i = sizeof sbuf; else i = (int)sw_num; r = BIO_write(s_ssl_bio, sbuf, i); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr,"ERROR in SERVER\n"); goto err; } /* Ignore "BIO_should_retry". */ } else if (r == 0) { fprintf(stderr,"SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server wrote %d\n", r); sw_num -= r; } } if (sr_num > 0) { /* Read from client. */ r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf)); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr,"ERROR in SERVER\n"); goto err; } /* blah, blah */ } else if (r == 0) { fprintf(stderr,"SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server read %d\n", r); sr_num -= r; } } *s_time += (clock() - s_clock); } { /* "I/O" BETWEEN CLIENT AND SERVER. */ size_t r1, r2; BIO *io1 = server_io, *io2 = client_io; /* we use the non-copying interface for io1 * and the standard BIO_write/BIO_read interface for io2 */ static int prev_progress = 1; int progress = 0; /* io1 to io2 */ do { size_t num; int r; r1 = BIO_ctrl_pending(io1); r2 = BIO_ctrl_get_write_guarantee(io2); num = r1; if (r2 < num) num = r2; if (num) { char *dataptr; if (INT_MAX < num) /* yeah, right */ num = INT_MAX; r = BIO_nread(io1, &dataptr, (int)num); assert(r > 0); assert(r <= (int)num); /* possibly r < num (non-contiguous data) */ num = r; r = BIO_write(io2, dataptr, (int)num); if (r != (int)num) /* can't happen */ { fprintf(stderr, "ERROR: BIO_write could not write " "BIO_ctrl_get_write_guarantee() bytes"); goto err; } progress = 1; if (debug) printf((io1 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num); } } while (r1 && r2); /* io2 to io1 */ { size_t num; int r; r1 = BIO_ctrl_pending(io2); r2 = BIO_ctrl_get_read_request(io1); /* here we could use ..._get_write_guarantee instead of * ..._get_read_request, but by using the latter * we test restartability of the SSL implementation * more thoroughly */ num = r1; if (r2 < num) num = r2; if (num) { char *dataptr; if (INT_MAX < num) num = INT_MAX; if (num > 1) --num; /* test restartability even more thoroughly */ r = BIO_nwrite0(io1, &dataptr); assert(r > 0); if (r < (int)num) num = r; r = BIO_read(io2, dataptr, (int)num); if (r != (int)num) /* can't happen */ { fprintf(stderr, "ERROR: BIO_read could not read " "BIO_ctrl_pending() bytes"); goto err; } progress = 1; r = BIO_nwrite(io1, &dataptr, (int)num); if (r != (int)num) /* can't happen */ { fprintf(stderr, "ERROR: BIO_nwrite() did not accept " "BIO_nwrite0() bytes"); goto err; } if (debug) printf((io2 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num); } } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */ if (!progress && !prev_progress) if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) { fprintf(stderr, "ERROR: got stuck\n"); if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0) { fprintf(stderr, "This can happen for SSL2 because " "CLIENT-FINISHED and SERVER-VERIFY are written \n" "concurrently ..."); if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0 && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0) { fprintf(stderr, " ok.\n"); goto end; } } fprintf(stderr, " ERROR.\n"); goto err; } prev_progress = progress; } } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0); if (verbose) print_details(c_ssl, "DONE via BIO pair: "); end: ret = 0; err: ERR_print_errors(bio_err); if (server) BIO_free(server); if (server_io) BIO_free(server_io); if (client) BIO_free(client); if (client_io) BIO_free(client_io); if (s_ssl_bio) BIO_free(s_ssl_bio); if (c_ssl_bio) BIO_free(c_ssl_bio); return ret; }
int main(int argc, char *argv[]) { char *CApath=NULL,*CAfile=NULL; int badop=0; int bio_pair=0; int force=0; int tls1=0,ssl2=0,ssl3=0,ret=1; int client_auth=0; int server_auth=0,i; int app_verify=0; char *server_cert=TEST_SERVER_CERT; char *server_key=NULL; char *client_cert=TEST_CLIENT_CERT; char *client_key=NULL; SSL_CTX *s_ctx=NULL; SSL_CTX *c_ctx=NULL; SSL_METHOD *meth=NULL; SSL *c_ssl,*s_ssl; int number=1,reuse=0; long bytes=1L; #ifndef OPENSSL_NO_DH DH *dh; int dhe1024 = 0, dhe1024dsa = 0; #endif int no_dhe = 0; int print_time = 0; clock_t s_time = 0, c_time = 0; int comp = 0; COMP_METHOD *cm = NULL; verbose = 0; debug = 0; cipher = 0; bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); CRYPTO_set_locking_callback(lock_dbg_cb); /* enable memory leak checking unless explicitly disabled */ if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) { CRYPTO_malloc_debug_init(); CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); } else { /* OPENSSL_DEBUG_MEMORY=off */ CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); } CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); RAND_seed(rnd_seed, sizeof rnd_seed); bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE); argc--; argv++; while (argc >= 1) { if (strcmp(*argv,"-server_auth") == 0) server_auth=1; else if (strcmp(*argv,"-client_auth") == 0) client_auth=1; else if (strcmp(*argv,"-v") == 0) verbose=1; else if (strcmp(*argv,"-d") == 0) debug=1; else if (strcmp(*argv,"-reuse") == 0) reuse=1; else if (strcmp(*argv,"-dhe1024") == 0) { #ifndef OPENSSL_NO_DH dhe1024=1; #else fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n"); #endif } else if (strcmp(*argv,"-dhe1024dsa") == 0) { #ifndef OPENSSL_NO_DH dhe1024dsa=1; #else fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n"); #endif } else if (strcmp(*argv,"-no_dhe") == 0) no_dhe=1; else if (strcmp(*argv,"-ssl2") == 0) ssl2=1; else if (strcmp(*argv,"-tls1") == 0) tls1=1; else if (strcmp(*argv,"-ssl3") == 0) ssl3=1; else if (strncmp(*argv,"-num",4) == 0) { if (--argc < 1) goto bad; number= atoi(*(++argv)); if (number == 0) number=1; } else if (strcmp(*argv,"-bytes") == 0) { if (--argc < 1) goto bad; bytes= atol(*(++argv)); if (bytes == 0L) bytes=1L; i=strlen(argv[0]); if (argv[0][i-1] == 'k') bytes*=1024L; if (argv[0][i-1] == 'm') bytes*=1024L*1024L; } else if (strcmp(*argv,"-cert") == 0) { if (--argc < 1) goto bad; server_cert= *(++argv); } else if (strcmp(*argv,"-s_cert") == 0) { if (--argc < 1) goto bad; server_cert= *(++argv); } else if (strcmp(*argv,"-key") == 0) { if (--argc < 1) goto bad; server_key= *(++argv); } else if (strcmp(*argv,"-s_key") == 0) { if (--argc < 1) goto bad; server_key= *(++argv); } else if (strcmp(*argv,"-c_cert") == 0) { if (--argc < 1) goto bad; client_cert= *(++argv); } else if (strcmp(*argv,"-c_key") == 0) { if (--argc < 1) goto bad; client_key= *(++argv); } else if (strcmp(*argv,"-cipher") == 0) { if (--argc < 1) goto bad; cipher= *(++argv); } else if (strcmp(*argv,"-CApath") == 0) { if (--argc < 1) goto bad; CApath= *(++argv); } else if (strcmp(*argv,"-CAfile") == 0) { if (--argc < 1) goto bad; CAfile= *(++argv); } else if (strcmp(*argv,"-bio_pair") == 0) { bio_pair = 1; } else if (strcmp(*argv,"-f") == 0) { force = 1; } else if (strcmp(*argv,"-time") == 0) { print_time = 1; } else if (strcmp(*argv,"-zlib") == 0) { comp = COMP_ZLIB; } else if (strcmp(*argv,"-rle") == 0) { comp = COMP_RLE; } else if (strcmp(*argv,"-app_verify") == 0) { app_verify = 1; } else { fprintf(stderr,"unknown option %s\n",*argv); badop=1; break; } argc--; argv++; } if (badop) { bad: sv_usage(); goto end; } if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force) { fprintf(stderr, "This case cannot work. Use -f to perform " "the test anyway (and\n-d to see what happens), " "or add one of -ssl2, -ssl3, -tls1, -reuse\n" "to avoid protocol mismatch.\n"); EXIT(1); } if (print_time) { if (!bio_pair) { fprintf(stderr, "Using BIO pair (-bio_pair)\n"); bio_pair = 1; } if (number < 50 && !force) fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n"); } /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */ SSL_library_init(); SSL_load_error_strings(); if (comp == COMP_ZLIB) cm = COMP_zlib(); if (comp == COMP_RLE) cm = COMP_rle(); if (cm != NULL) { if (cm->type != NID_undef) SSL_COMP_add_compression_method(comp, cm); else { fprintf(stderr, "Warning: %s compression not supported\n", (comp == COMP_RLE ? "rle" : (comp == COMP_ZLIB ? "zlib" : "unknown"))); ERR_print_errors_fp(stderr); } } #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3) if (ssl2) meth=SSLv2_method(); else if (tls1) meth=TLSv1_method(); else if (ssl3) meth=SSLv3_method(); else meth=SSLv23_method(); #else #ifdef OPENSSL_NO_SSL2 meth=SSLv3_method(); #else meth=SSLv2_method(); #endif #endif c_ctx=SSL_CTX_new(meth); s_ctx=SSL_CTX_new(meth); if ((c_ctx == NULL) || (s_ctx == NULL)) { ERR_print_errors(bio_err); goto end; } if (cipher != NULL) { SSL_CTX_set_cipher_list(c_ctx,cipher); SSL_CTX_set_cipher_list(s_ctx,cipher); } #ifndef OPENSSL_NO_DH if (!no_dhe) { if (dhe1024dsa) { /* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */ SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE); dh=get_dh1024dsa(); } else if (dhe1024) dh=get_dh1024(); else dh=get_dh512(); SSL_CTX_set_tmp_dh(s_ctx,dh); DH_free(dh); } #else (void)no_dhe; #endif #ifndef OPENSSL_NO_RSA SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb); #endif if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM)) { ERR_print_errors(bio_err); } else if (!SSL_CTX_use_PrivateKey_file(s_ctx, (server_key?server_key:server_cert), SSL_FILETYPE_PEM)) { ERR_print_errors(bio_err); goto end; } if (client_auth) { SSL_CTX_use_certificate_file(c_ctx,client_cert, SSL_FILETYPE_PEM); SSL_CTX_use_PrivateKey_file(c_ctx, (client_key?client_key:client_cert), SSL_FILETYPE_PEM); } if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) || (!SSL_CTX_set_default_verify_paths(s_ctx)) || (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) || (!SSL_CTX_set_default_verify_paths(c_ctx))) { /* fprintf(stderr,"SSL_load_verify_locations\n"); */ ERR_print_errors(bio_err); /* goto end; */ } if (client_auth) { BIO_printf(bio_err,"client authentication\n"); SSL_CTX_set_verify(s_ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback); if (app_verify) { SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg); } } if (server_auth) { BIO_printf(bio_err,"server authentication\n"); SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER, verify_callback); if (app_verify) { SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg); } } { int session_id_context = 0; SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context); } c_ssl=SSL_new(c_ctx); s_ssl=SSL_new(s_ctx); #ifndef OPENSSL_NO_KRB5 if (c_ssl && c_ssl->kssl_ctx) { char localhost[MAXHOSTNAMELEN+2]; if (gethostname(localhost, sizeof localhost-1) == 0) { localhost[sizeof localhost-1]='\0'; if(strlen(localhost) == sizeof localhost-1) { BIO_printf(bio_err,"localhost name too long\n"); goto end; } kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER, localhost); } } #endif /* OPENSSL_NO_KRB5 */ for (i=0; i<number; i++) { if (!reuse) SSL_set_session(c_ssl,NULL); if (bio_pair) ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time); else ret=doit(s_ssl,c_ssl,bytes); } if (!verbose) { print_details(c_ssl, ""); } if ((number > 1) || (bytes > 1L)) BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes); if (print_time) { #ifdef CLOCKS_PER_SEC /* "To determine the time in seconds, the value returned * by the clock function should be divided by the value * of the macro CLOCKS_PER_SEC." * -- ISO/IEC 9899 */ BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n" "Approximate total client time: %6.2f s\n", (double)s_time/CLOCKS_PER_SEC, (double)c_time/CLOCKS_PER_SEC); #else /* "`CLOCKS_PER_SEC' undeclared (first use this function)" * -- cc on NeXTstep/OpenStep */ BIO_printf(bio_stdout, "Approximate total server time: %6.2f units\n" "Approximate total client time: %6.2f units\n", (double)s_time, (double)c_time); #endif } SSL_free(s_ssl); SSL_free(c_ssl); end: if (s_ctx != NULL) SSL_CTX_free(s_ctx); if (c_ctx != NULL) SSL_CTX_free(c_ctx); if (bio_stdout != NULL) BIO_free(bio_stdout); #ifndef OPENSSL_NO_RSA free_tmp_rsa(); #endif #ifndef OPENSSL_NO_ENGINE ENGINE_cleanup(); #endif CRYPTO_cleanup_all_ex_data(); ERR_free_strings(); ERR_remove_state(0); EVP_cleanup(); CRYPTO_mem_leaks(bio_err); if (bio_err != NULL) BIO_free(bio_err); EXIT(ret); }
int doit(SSL *s_ssl, SSL *c_ssl, long count) { MS_STATIC char cbuf[1024*8],sbuf[1024*8]; long cw_num=count,cr_num=count; long sw_num=count,sr_num=count; int ret=1; BIO *c_to_s=NULL; BIO *s_to_c=NULL; BIO *c_bio=NULL; BIO *s_bio=NULL; int c_r,c_w,s_r,s_w; int c_want,s_want; int i,j; int done=0; int c_write,s_write; int do_server=0,do_client=0; memset(cbuf,0,sizeof(cbuf)); memset(sbuf,0,sizeof(sbuf)); c_to_s=BIO_new(BIO_s_mem()); s_to_c=BIO_new(BIO_s_mem()); if ((s_to_c == NULL) || (c_to_s == NULL)) { ERR_print_errors(bio_err); goto err; } c_bio=BIO_new(BIO_f_ssl()); s_bio=BIO_new(BIO_f_ssl()); if ((c_bio == NULL) || (s_bio == NULL)) { ERR_print_errors(bio_err); goto err; } SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl,s_to_c,c_to_s); BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE); SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl,c_to_s,s_to_c); BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE); c_r=0; s_r=1; c_w=1; s_w=0; c_want=W_WRITE; s_want=0; c_write=1,s_write=0; /* We can always do writes */ for (;;) { do_server=0; do_client=0; i=(int)BIO_pending(s_bio); if ((i && s_r) || s_w) do_server=1; i=(int)BIO_pending(c_bio); if ((i && c_r) || c_w) do_client=1; if (do_server && debug) { if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); /* else if (s_write) printf("server:SSL_write()\n"); else printf("server:SSL_read()\n"); */ } if (do_client && debug) { if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); /* else if (c_write) printf("client:SSL_write()\n"); else printf("client:SSL_read()\n"); */ } if (!do_client && !do_server) { fprintf(stdout,"ERROR IN STARTUP\n"); ERR_print_errors(bio_err); break; } if (do_client && !(done & C_DONE)) { if (c_write) { j=(cw_num > (long)sizeof(cbuf)) ?sizeof(cbuf):(int)cw_num; i=BIO_write(c_bio,cbuf,j); if (i < 0) { c_r=0; c_w=0; if (BIO_should_retry(c_bio)) { if (BIO_should_read(c_bio)) c_r=1; if (BIO_should_write(c_bio)) c_w=1; } else { fprintf(stderr,"ERROR in CLIENT\n"); ERR_print_errors(bio_err); goto err; } } else if (i == 0) { fprintf(stderr,"SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n",i); /* ok */ s_r=1; c_write=0; cw_num-=i; } } else { i=BIO_read(c_bio,cbuf,sizeof(cbuf)); if (i < 0) { c_r=0; c_w=0; if (BIO_should_retry(c_bio)) { if (BIO_should_read(c_bio)) c_r=1; if (BIO_should_write(c_bio)) c_w=1; } else { fprintf(stderr,"ERROR in CLIENT\n"); ERR_print_errors(bio_err); goto err; } } else if (i == 0) { fprintf(stderr,"SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n",i); cr_num-=i; if (sw_num > 0) { s_write=1; s_w=1; } if (cr_num <= 0) { s_write=1; s_w=1; done=S_DONE|C_DONE; } } } } if (do_server && !(done & S_DONE)) { if (!s_write) { i=BIO_read(s_bio,sbuf,sizeof(cbuf)); if (i < 0) { s_r=0; s_w=0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r=1; if (BIO_should_write(s_bio)) s_w=1; } else { fprintf(stderr,"ERROR in SERVER\n"); ERR_print_errors(bio_err); goto err; } } else if (i == 0) { ERR_print_errors(bio_err); fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n"); goto err; } else { if (debug) printf("server read %d\n",i); sr_num-=i; if (cw_num > 0) { c_write=1; c_w=1; } if (sr_num <= 0) { s_write=1; s_w=1; c_write=0; } } } else { j=(sw_num > (long)sizeof(sbuf))? sizeof(sbuf):(int)sw_num; i=BIO_write(s_bio,sbuf,j); if (i < 0) { s_r=0; s_w=0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r=1; if (BIO_should_write(s_bio)) s_w=1; } else { fprintf(stderr,"ERROR in SERVER\n"); ERR_print_errors(bio_err); goto err; } } else if (i == 0) { ERR_print_errors(bio_err); fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n"); goto err; } else { if (debug) printf("server wrote %d\n",i); sw_num-=i; s_write=0; c_r=1; if (sw_num <= 0) done|=S_DONE; } } } if ((done & S_DONE) && (done & C_DONE)) break; } if (verbose) print_details(c_ssl, "DONE: "); ret=0; err: /* We have to set the BIO's to NULL otherwise they will be * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and * again when c_ssl is SSL_free()ed. * This is a hack required because s_ssl and c_ssl are sharing the same * BIO structure and SSL_set_bio() and SSL_free() automatically * BIO_free non NULL entries. * You should not normally do this or be required to do this */ if (s_ssl != NULL) { s_ssl->rbio=NULL; s_ssl->wbio=NULL; } if (c_ssl != NULL) { c_ssl->rbio=NULL; c_ssl->wbio=NULL; } if (c_to_s != NULL) BIO_free(c_to_s); if (s_to_c != NULL) BIO_free(s_to_c); if (c_bio != NULL) BIO_free_all(c_bio); if (s_bio != NULL) BIO_free_all(s_bio); return(ret); }