int main(int argc, char **argv) { int ret = EXIT_FAILURE; char *payload = NULL; if (!parse_options(argc, argv)) { goto fail; } if (version_p) { g_print(PACKAGE_VERSION "\n"); goto success; } if (!validate_opts()) { goto fail; } if (!get_payload(&payload)) { goto fail; } if (!send_record(payload)) { goto fail; } success: ret = EXIT_SUCCESS; fail: free_glib_strings(); return ret; }
int report_faces(int start, int n, Ptr<FaceRecognizer> model, Ptr<FaceRecognizer> LBPH_model) { const char* folder_name ="/Users/xueqianjiang/Desktop/Images"; const char* file_name = "/Users/xueqianjiang/Desktop/Images/Images.txt"; std::stringstream filename; int gender; int person_new; int sizeofeig=3; float eigenface[sizeofeig]; float eig[sizeofeig]; ofstream outputFile; outputFile.open(file_name, ios::app); //outputFile.open(file_name); // for (int i = start; i<=start+n; i++) { int i=start; filename<< folder_name << "/img" << i << ".jpg"; gender = detect(model, filename.str()); // person_new returns: the Id of the person if the person is in the database, and -1 if the person is not in the database person_new = 0; person_new = detect_Id(LBPH_model, filename.str()); //gender = 0; // replace with above //eigenface = eigenface(filename.str()); // try to write to file filename<<";"<<start<<'\n'; outputFile<< filename.str(); for (int j=0; j<sizeofeig; j++) { eig[j]=eigenface[j]; // } send_record(gender, person_new, eig, sizeofeig); } outputFile.close(); // query database on the eigenface of the images // save the face visit in the database return gender; }
bool TLSClient_Impl::send_application_data() { if (send_in_data.get_size() == send_in_data_read_pos || !can_send_record()) return false; const char *data = send_in_data.get_data() + send_in_data_read_pos; int size = send_in_data.get_size() - send_in_data_read_pos; unsigned int max_record_length_gcc_fix = max_record_length; unsigned int data_in_record = clan::min((unsigned int)size, max_record_length_gcc_fix); int offset = 0; int offset_tls_record = offset; offset += sizeof(TLS_Record); int offset_tls_appdata = offset; offset += data_in_record; Secret message(offset); unsigned char *message_ptr = message.get_data(); set_tls_record(message_ptr + offset_tls_record, cl_tls_content_application_data, offset - offset_tls_record); memcpy(message_ptr + offset_tls_appdata, data, data_in_record); send_record(message_ptr, offset); send_in_data_read_pos += data_in_record; if (send_in_data_read_pos > desired_buffer_size / 2 || send_in_data_read_pos == 0) { int available = send_in_data.get_size() - send_in_data_read_pos; memmove(send_in_data.get_data(), send_in_data.get_data() + send_in_data_read_pos, available); send_in_data.set_size(available); send_in_data_read_pos = 0; } return true; }
int main (int argc, char ** argv) { char * device = DEFAULT_FLOCK_DEVICE; int number_of_birds = atoi (DEFAULT_NUMBER_OF_BIRDS); double noise_level = atof (DEFAULT_NOISE_LEVEL); flock_t flock = NULL; bird_data_t data_of_birds = NULL; int flockfd = -1; int port = 0; int sockfd = -1; char * host = NULL; struct sockaddr_in host_addr; fd_set input_fd_set; int maxfd; OSC_space_t space = NULL; int c; int count; int result = EXIT_SUCCESS; /* Parse arguments. */ opterr = 0; while ((c = getopt (argc, argv, "d:b:n:")) != -1) switch (c) { case 'd': device = optarg; break; case 'b': number_of_birds = atoi (optarg); break; case 'n': noise_level = atof (optarg); break; default: break; } if (argc - optind != 2) { usage (argv[0]); exit (EXIT_FAILURE); } host = argv[optind++]; port = atoi (argv[optind++]); get_more_priority (); signal (SIGINT, handle_signal); fprintf (stderr, "Preparing flock device: %s, number of birds: %d.\n", device, number_of_birds); if ((flock = flock_hl_open (device, number_of_birds, flock_bird_record_mode_position_angles, 1, 1)) == NULL) { result = EXIT_FAILURE; goto terminate; } flockfd = flock_get_file_descriptor (flock); /* TODO: use a command-line option for the local port. */ fprintf (stderr, "Opening configuration port: %d.\n", port + 1); if ((sockfd = open_socket (port + 1)) == -1) { result = EXIT_FAILURE; goto terminate; } fprintf (stderr, "Preparing communication with host: %s, port: %d.\n", host, port); if ((fill_host_addr (host, port, &host_addr)) == -1) { result = EXIT_FAILURE; goto terminate; } FD_ZERO (&input_fd_set); FD_SET (sockfd, &input_fd_set); FD_SET (flockfd, &input_fd_set); maxfd = (sockfd < flockfd) ? flockfd : sockfd; data_of_birds = (bird_data_t) malloc (number_of_birds * sizeof (struct bird_data_s)); OSC_init (); space = OSC_space_make (); OSC_space_register_method (space, OSC_method_make ("zthreshold", ",f", set_zthreshold, NULL)); count = 0; /* First values. */ { bird_data_t data; int bird; if (flock_next_record (flock, 1) == 0) { fprintf (stderr, "Can't get response from flock.\n"); result = EXIT_FAILURE; goto terminate; } count++; for (bird = 0, data = data_of_birds; bird < number_of_birds; bird++, data++) { memcpy (&data->rec, flock_get_record (flock, bird + 1), sizeof (data->rec)); data->zset = 0; data->zcount = 0; data->maxdz = 0; data->lastbump = 0; data->bumpcount = 0; } } fprintf (stderr, "Running... (Hit Ctrl-C to stop.)\n"); while (!terminate) { fd_set read_fd_set; read_fd_set = input_fd_set; /* Block until new data is available, either from the flock or from peer. */ if (select (maxfd + 1, &read_fd_set, NULL, NULL, NULL) == -1) { perror ("select"); result = EXIT_FAILURE; goto terminate; } if (FD_ISSET (sockfd, &read_fd_set)) /* Get data from peer. */ receive (space, sockfd); if (FD_ISSET (flockfd, &read_fd_set)) { bird_data_t data; int bird; /* Get data from flock. */ if (flock_next_record (flock, 1) == 0) { result = EXIT_FAILURE; goto terminate; } count++; for (bird = 0, data = data_of_birds; bird < number_of_birds; bird++, data++) { double dx, dy, dz; /* Shift previous record. */ memcpy (&data->prev_rec, &data->rec, sizeof (data->rec)); /* Copy bird's record. */ memcpy (&data->rec, flock_get_record (flock, bird + 1), sizeof (data->rec)); dx = data->rec.values.pa.x - data->prev_rec.values.pa.x; dy = data->rec.values.pa.y - data->prev_rec.values.pa.y; dz = data->rec.values.pa.z - data->prev_rec.values.pa.z; { /* Coordinates. */ double distance = sqrt ((dx * dx) + (dy * dy) + (dz * dz)); if (distance > noise_level) send_record (sockfd, &host_addr, bird + 1, &data->rec); } { /* Bumps. */ if ((count - data->lastbump) < after_bump_delay) continue; if (dz > zthreshold) { data->zset = 1; if (data->maxdz < dz) data->maxdz = dz; } if (!data->zset) { data->maxdz = 0; continue; } /* Proposition: delay could depend on maxdz. */ if (dz < 0.5 * zthreshold) { send_bump (sockfd, &host_addr, bird + 1, data->maxdz); /* fprintf (stderr, "bird %d bumps (%g).\n", */ /* bird + 1, data->maxdz); */ data->zset = 0; data->maxdz = 0; data->lastbump = count; data->bumpcount++; } } } } } terminate: fprintf (stderr, "Closing device and connection.\n"); if (sockfd != -1) close_socket (sockfd); if (flock) flock_hl_close (flock); if (data_of_birds) free (data_of_birds); if (space) OSC_space_free (space); return result; }
/* * auditd_plugin() sends a record via a tcp connection. * * Operation: * - 1 tcp connection opened at a time, referenced by current_host->sockfd * - tries to (open and) send a record to the current_host where its address * is taken from the first hostent h_addr_list entry * - if connection times out, tries second host * - if all hosts where tried tries again for retries number of times * - if everything fails, it bails out with AUDITD_RETRY * * Note, that space on stack allocated for any error message returned along * with AUDITD_RETRY is subsequently freed by auditd. * */ auditd_rc_t auditd_plugin(const char *input, size_t in_len, uint64_t sequence, char **error) { int rc = AUDITD_FAIL; int send_record_rc = SEND_RECORD_FAIL; hostlist_t *start_host; int attempts = 0; char *ext_error; /* extended error string */ close_rsn_t err_rsn = RSN_UNDEFINED; char *rsn_msg; #if DEBUG char *rc_msg; static uint64_t last_sequence = 0; if ((last_sequence > 0) && (sequence != last_sequence + 1)) { DPRINT((dfile, "audit_remote: buffer sequence=%llu " "but prev=%llu\n", sequence, last_sequence)); } last_sequence = sequence; DPRINT((dfile, "audit_remote: input seq=%llu, len=%d\n", sequence, in_len)); #endif (void) pthread_mutex_lock(&transq_lock); if (transq_hdr.count == transq_count_max) { DPRINT((dfile, "Transmission queue is full (%ld)\n", transq_hdr.count)); (void) pthread_mutex_unlock(&transq_lock); *error = strdup(gettext("retransmission queue is full")); return (AUDITD_RETRY); } (void) pthread_mutex_unlock(&transq_lock); (void) pthread_mutex_lock(&plugin_mutex); /* cycle over the hosts and possibly deliver the record */ start_host = current_host; while (rc != AUDITD_SUCCESS) { DPRINT((dfile, "Trying to send record to %s [attempt:%d/%d]\n", current_host->host->h_name, attempts + 1, retries)); send_record_rc = send_record(current_host, input, in_len, sequence, &err_rsn); DPRINT((dfile, "send_record() returned %d - ", send_record_rc)); switch (send_record_rc) { case SEND_RECORD_SUCCESS: DPRINT((dfile, "success\n")); nosuccess_cnt = 0; rc = AUDITD_SUCCESS; if (hosts_prev != NULL) { freehostlist(&hosts_prev); DPRINT((dfile, "stale host list freed\n")); } break; case SEND_RECORD_NEXT: DPRINT((dfile, "retry the same host: %s (penalty) " "rsn:%d\n", current_host->host->h_name, err_rsn)); attempts++; break; case SEND_RECORD_RETRY: DPRINT((dfile, "retry the same host: %s (no penalty) " "rsn:%d\n", current_host->host->h_name, err_rsn)); break; } if (send_record_rc == SEND_RECORD_NEXT) { /* warn about unsuccessful auditd record delivery */ rsn_msg = rsn_to_msg(err_rsn); (void) asprintf(&ext_error, "retry %d connection %s:%d %s", attempts + 1, current_host->host->h_name, ntohs(current_host->port), rsn_msg); if (ext_error == NULL) { free(rsn_msg); *error = strdup(gettext("no memory")); rc = AUDITD_NO_MEMORY; break; } __audit_dowarn2("plugin", "audit_remote.so", "retry", ext_error, attempts + 1); free(rsn_msg); free(ext_error); if (attempts < retries) { /* semi-exponential timeout back off */ timeout = BOFF_TIMEOUT(attempts, timeout); DPRINT((dfile, "New timeout=%d\n", timeout)); } else { /* get next host */ current_host = current_host->next_host; if (current_host == NULL) { current_host = hosts; } timeout = timeout_p_timeout; DPRINT((dfile, "New timeout=%d\n", timeout)); attempts = 0; } /* one cycle finished */ if (current_host == start_host && attempts == 0) { nosuccess_cnt++; (void) asprintf(&ext_error, "all hosts defined " "as p_hosts were tried to deliver " "the audit record to with no success " "- sleeping for %d seconds", NOSUCCESS_DELAY); if (ext_error == NULL) { *error = strdup(gettext("no memory")); rc = AUDITD_NO_MEMORY; break; } __audit_dowarn2("plugin", "audit_remote.so", "retry", ext_error, nosuccess_cnt); free(ext_error); (void) sleep(NOSUCCESS_DELAY); } } /* if (send_record_rc == SEND_RECORD_NEXT) */ err_rsn = RSN_UNDEFINED; } /* while (rc != AUDITD_SUCCESS) */ (void) pthread_mutex_unlock(&plugin_mutex); #if DEBUG rc_msg = auditd_message(rc); DPRINT((dfile, "audit_remote: returning: %s\n", rc_msg)); free(rc_msg); #endif return (rc); }