particle* read_particles(const char* file) { char postfix[10]; srand((unsigned)time(NULL)); rand_string(postfix, 10); sprintf(dir_name, "out_%s_%s", basename((char*)file), postfix); mkdir(dir_name, 0777); ps = NULL; char* line = NULL; FILE* fp = fopen(file, "r"); size_t len = 0; if (!fp) { printf("Invalid input: %s\nCould not open file.\n", file); exit(EXIT_FAILURE); } if (getline(&line, &len, fp) != -1) { char* end; particle_count = strtol(line, &end, 10); if (*end == 0) { printf("Invalid input: %s\nExpected number of particles.\n", line); exit(EXIT_FAILURE); } ps = _mm_malloc(particle_count * sizeof(particle), 64); } else { printf("Invalid input: Empty file.\n"); exit(EXIT_FAILURE); } // m x y z vx vy vz for (long i = 0; i < particle_count; i++) { getline(&line, &len, fp); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf", &ps[i].mass, &ps[i].position[X], &ps[i].position[Y], &ps[i].position[Z], &ps[i].velocity[X], &ps[i].velocity[Y], &ps[i].velocity[Z]); if (ps[i].mass == 0) { printf("Invalid input: Invald particle at line %ld.\n", i + 1); exit(EXIT_FAILURE); } ps[i].force[X] = 0.0; ps[i].force[Y] = 0.0; ps[i].force[Z] = 0.0; update_minmax(ps[i].position); } calculate_cube(); fclose(fp); if (line) { free(line); } write_plot_limits(); write_particles(-1); // Initial state of particles. return ps; }
/* * This will handle connection for each client * */ void *connection_handler(void *socket_desc) { //Get the socket descriptor int sock = *(int*)socket_desc; int read_size; char message[65] , client_message[15],randmess[52]; /* //Send some messages to the client message = "Greetings! I am your connection handler\n"; write(sock , message , strlen(message)); message = "Now type something and i shall repeat what you type \n"; write(sock , message , strlen(message)); */ //Receive a message from client while( (read_size = recv(sock , client_message , 12 , 0)) > 0 ) { //end of string marker client_message[read_size] = '\0'; //Send the message back to client if(strcmp(client_message,"GETMESSAGE\n")==0){ //If the request is "GETMESSAGE\n" strcpy(message,"The message is "); rand_string(randmess,50); strcat(message,randmess); strcat(message,"\n"); write(sock ,message, strlen(message)); }else if(strcmp(client_message,"BYE\n")==0){ //If the request is "BYE\n" write(sock ,client_message, strlen(client_message)); close(sock); return 0; }else{ //If the request is undefied strcpy(message,"Bad Requset\n"); write(sock ,message, strlen(message)); } //clear the message buffer memset(client_message, 0, 15); } /* if(read_size == 0) { puts("Client disconnected"); fflush(stdout); } else if(read_size == -1) { perror("recv failed"); }*/ return 0; }
int main ( void ) { static const char filename[] = "inputcharfile.txt"; FILE *file = fopen ( filename, "w" ); srand(time(NULL)); char s[ATTRSIZE]; int i;//35447312 for(i=0; i<1865648; i++) { rand_string(s); fprintf(file, "%s", s); } fclose(file); }
EtagStore::EtagStore(EtagStoreResource* resource, WContainerWidget* parent): AbstractStore(parent), resource_(resource) { cookie_value_ = rand_string(); int day = 3600 * 24; wApp->setCookie(resource_->cookie_name(), cookie_value_, day); { boost::mutex::scoped_lock lock(resource_->cookie_to_etag_mutex_); typedef EtagStoreResource::Etag Etag; Etag& etag = resource_->cookie_to_etag_[cookie_value_]; etag.handler = one_bound_post(boost::bind(&EtagStore::emit_value, this, _1)); } resize(0, 0); wApp->enableUpdates(); }
int main(){ printf("En este ejemplo un proceso va a poner en memoria una hilera de caracteres aleatorios. Se usan tres semaforos, uno para el area de memoria compartida y dos para que ambos procesos se avisen cuando se terminan de interactuar con la operacion de produccion/consumo.\n\n"); // inicializacíon de los semaforos int sem_id = semget(IPC_PRIVATE, 3, PERM|IPC_CREAT|IPC_EXCL); if(sem_id < 0){ printf("No pude obtener el semaforo.\n"); perror("RAZON"); exit(-1); } union semun { int val; struct semid_ds *buf; ushort * array; } argument; argument.val = 0; if(semctl(sem_id, SEMAFORO_PRODUCTOR, SETVAL, argument) < 0){ printf("Error al inicializar el contador del semaforo del productor.\n"); perror("RAZON"); exit(-1); }else{ printf("Inicializado el semaforo del productor. %s", get_time()); } if(semctl(sem_id, SEMAFORO_CONSUMIDOR, SETVAL, argument) < 0){ printf("Error al inicializar el contador del semaforo del consumidor.\n"); perror("RAZON"); exit(-1); }else{ printf("Inicializado el semaforo del consumidor. %s", get_time()); } argument.val = 1; // le doy permiso para que el hijo ponga el primer texto if(semctl(sem_id, SEMAFORO_SHM, SETVAL, argument) < 0){ printf("Error al inicializar el contador del semaforo de memoria.\n"); perror("RAZON"); exit(-1); }else{ printf("Inicializado el semaforo de memoria compartida. %s", get_time()); } // inicializacion de la memoria compartida int shm_id = shmget(IPC_PRIVATE, 512, PERM|IPC_CREAT|IPC_EXCL); if(shm_id < 0){ printf("No pude obtener la memoria compartida.\n"); perror("RAZON"); exit(-1); }else{ printf("Inicializada la memoria compartida %d. %s\n", shm_id, get_time()); } // struct para las operaciones struct sembuf oper_productor[1], oper_shm[1], oper_consumidor[1]; int f = fork(); if(f == -1) exit(1); if(f == 0){ //printf("I'm Luke\n"); int retval; char *shared_memory =(char*)shmat(shm_id,0,0); if(shared_memory == (void*)-1){ printf("Error al conectarse a la memoria compartida.\n"); perror("RAZON"); exit(-1); } while(1){ sleep(1); // 1 segundo entre cada envio para notar los cambios retval = sem_op(sem_id, SEMAFORO_SHM, oper_shm, -1); // wait shm //zona critica sprintf (shared_memory, "%s\n", rand_string(12)); retval = sem_op(sem_id, SEMAFORO_SHM, oper_shm, 1); // signal shm retval = sem_op(sem_id, SEMAFORO_PRODUCTOR, oper_productor, 1); // signal productor if(retval < 0){ printf("Error al realizar el signal de productor.\n"); perror("RAZON"); exit(-1); // hago esto por miedo a inanicion }else{ retval = sem_op(sem_id, SEMAFORO_CONSUMIDOR, oper_consumidor, -1); // wait consumidor if(retval < 0){ printf("Error al realizar el wait de consumidor.\n"); perror("RAZON"); exit(-1); // hago esto por miedo a inanicion } } } }else{ //printf("Luke, I'm your father\n"); int retval; char *shared_memory =(char*)shmat(shm_id,0,0); if(shared_memory == (void*)-1){ printf("Error al conectarse a la memoria compartida.\n"); perror("RAZON"); exit(-1); } while(1){ retval = sem_op(sem_id, SEMAFORO_PRODUCTOR, oper_productor, -1); // wait productor if(retval == 0){ retval = sem_op(sem_id, SEMAFORO_SHM, oper_shm, -1); // wait shm //zona critica printf ("En la memoria compartida hay: %s\n", shared_memory); retval = sem_op(sem_id, SEMAFORO_SHM, oper_shm, 1); // signal shm retval = sem_op(sem_id, SEMAFORO_CONSUMIDOR, oper_consumidor, 1); // signal consumidor if(retval < 0){ printf("Error al realizar el signal de consumidor.\n"); perror("RAZON"); exit(-1); // hago esto por miedo a inanicion } }else{ printf("Error al realizar el wait del productor.\n"); perror("RAZON"); exit(-1); // hago esto por miedo a inanicion } } } return 0; }
int main(int argc, char **argv) { daemonize(); //saveData("admin",100,100,"admin"); config_obj=parseConfigFile(); ipWhitelist = cJSON_GetObjectItem(config_obj,"ipWhitelist"); wiringPiSetup(); pinDirections = cJSON_GetObjectItem(config_obj,"pinDirections"); handlePinsDirection(pinDirections); signal(SIGINT, sighandler); struct lws_context_creation_info info; unsigned int ms, oldms= 0; const char *iface = NULL; char cert_path[1024]; char key_path[1024]; int use_ssl = 0; int opts = 0; int n = 0; /* * take care to zero down the info struct, he contains random garbaage * from the stack otherwise */ memset(&info, 0, sizeof info); info.port = 7681; signal(SIGINT, sighandler); info.iface = iface; info.protocols = protocols; info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; if (use_ssl) { if (strlen(resource_path) > sizeof(cert_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(cert_path, "%s/libwebsockets-test-server.pem", resource_path); if (strlen(resource_path) > sizeof(key_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(key_path, "%s/libwebsockets-test-server.key.pem", resource_path); info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; } info.gid = -1; info.uid = -1; info.max_http_header_pool = 1; info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8; info.extensions = NULL; context = lws_create_context(&info); if (context == NULL) { lwsl_err("libwebsocket init failed\n"); return -1; } n = 0; while (n >= 0 && !force_exit) { struct timeval tv; gettimeofday(&tv, NULL); /* * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every * live websocket connection using the DUMB_INCREMENT protocol, * as soon as it can take more packets (usually immediately) */ extern struct ram_usage ram_l; extern struct cpu_live cpu_l; extern struct cpu_freq cpu_freq; if(old_client_count!=client_count){ if(hash){ free(hash); } hash=rand_string(); lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_GPIO]); lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_DETAILS]); lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_SERVICES]); lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_HOME]); old_client_count=client_count; } ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); if ((ms - oldms) > 200 && get_client_count()>0 && (cpuLive(&cpu_l,5)==0 && ramLive(&ram_l,5)==0 && cpuLiveFreq(&cpu_freq)==0 )) { lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_DETAILS]); oldms = ms; } n = lws_service(context, 50); } lws_context_destroy(context); lwsl_notice("libwebsockets-test-server exited cleanly\n"); return 0; }
main(int argc, char *argv[]) { EVP_MD_CTX *mdctx; EVP_MD_CTX *mdctxdup; const EVP_MD *md; const EVP_MD *mddup; char mess1[] = "Hello World"; unsigned char md_value[EVP_MAX_MD_SIZE]; unsigned char md_valuedup[EVP_MAX_MD_SIZE]; int md_len,md_lendup, i; char originalBinary[24]; char randomBinary[24]; OpenSSL_add_all_digests(); if(!argv[1]) { printf("Usage: mdtest digestname\n"); exit(1); } md = EVP_get_digestbyname(argv[1]) ; if(!md) { printf("Unknown message digest %s\n", argv[1]); exit(1); } mdctx = EVP_MD_CTX_create(); EVP_DigestInit_ex(mdctx, md, NULL); EVP_DigestUpdate(mdctx, mess1, strlen(mess1)); EVP_DigestFinal_ex(mdctx, md_value, &md_len); EVP_MD_CTX_destroy(mdctx); printf("Input Original: %s\n",mess1); printf("Digest Original: "); for(i = 0; i < md_len; i++) printf("%02x", md_value[i]); printf("\n"); int dontExit =1; char *str ; while(dontExit) { str = (char*)malloc(10); str = rand_string(str,9); mdctxdup = EVP_MD_CTX_create(); EVP_DigestInit_ex(mdctxdup, md, NULL); EVP_DigestUpdate(mdctxdup, str, strlen(str)); EVP_DigestFinal_ex(mdctxdup, md_valuedup, &md_lendup); EVP_MD_CTX_destroy(mdctxdup); if((md_value[0] == md_valuedup[0]) && (md_value[1] == md_valuedup[1]) && (md_value[2] == md_valuedup[2]) ) { break; } free(str); } printf("Input Random: %s\n",str); printf("Digest Random: "); for(i = 0; i < md_lendup; i++) printf("%02x", md_valuedup[i]); printf("\n"); /* Call this once before exit. */ EVP_cleanup(); exit(0); }
int main(int argc, char **argv) { _LHASH *lh; struct dummy_lhash dummy_lh = {NULL}; unsigned i; CRYPTO_library_init(); lh = lh_new(NULL, NULL); if (lh == NULL) { return 1; } for (i = 0; i < 100000; i++) { unsigned action; char *s, *s1, *s2; if (dummy_lh_num_items(&dummy_lh) != lh_num_items(lh)) { fprintf(stderr, "Length mismatch\n"); return 1; } action = rand() % 3; switch (action) { case 0: s = rand_string(); s1 = (char *)lh_retrieve(lh, s); s2 = dummy_lh_retrieve(&dummy_lh, s); if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) { fprintf(stderr, "lh_retrieve failure\n"); abort(); } free(s); break; case 1: s = rand_string(); lh_insert(lh, (void **)&s1, s); dummy_lh_insert(&dummy_lh, &s2, strdup(s)); if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) { fprintf(stderr, "lh_insert failure\n"); abort(); } if (s1) { free(s1); } if (s2) { free(s2); } break; case 2: s = rand_string(); s1 = lh_delete(lh, s); s2 = dummy_lh_delete(&dummy_lh, s); if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) { fprintf(stderr, "lh_insert failure\n"); abort(); } if (s1) { free(s1); } if (s2) { free(s2); } free(s); break; default: abort(); } } lh_doall(lh, free); lh_free(lh); dummy_lh_free(&dummy_lh); printf("PASS\n"); return 0; }
/** internal functions implementation starts here **/ void os_sendpass(IRC_User *s, IRC_User *u) { u_int32_t source_snid; u_int32_t snid; char *target; char *email; int lang; /* status validation */ CHECK_IF_IDENTIFIED_NICK /* syntax validation */ target = strtok(NULL, " "); if (!is_soper(u->snid)) { send_lang(u, s, PERMISSION_DENIED); return; } if(!irc_IsUMode(u, UMODE_OPER)) /* extra security */ return; else if(IsNull(target)) send_lang(u, s, SENDPASS_SYNTAX); else if( (snid = nick2snid(target)) == 0 ) send_lang(u, s, NICK_X_NOT_REGISTERED, target); /* sub-command */ else if(is_sadmin(snid) || is_sroot(snid)) { log_log(os_log, mod_info.name, "Nick %s trying SENDPASS on sadmin/soper %s", s->nick, target); irc_SendSanotice(s, "Nick %s trying SENDPASS on sadmin/soper %s", s->nick, target); } else if((sql_singlequery("SELECT email, lang FROM nickserv WHERE snid=%d", snid) < 1) || ((email = sql_field(0)) == NULL)) send_lang(u, s, OS_SENDPASS_NO_EMAIL_X, target); else { char buf[512]; char pbuf[PASSLEN+1]; lang = sql_field_i(1); rand_string(pbuf, PASSLEN, PASSLEN); pbuf[2] = '0'+ (random() % 10); sql_execute("UPDATE nickserv_security SET pass=%s WHERE snid=%d", sql_str(hex_str(encrypted_password(pbuf), 16)), snid); snprintf(buf, sizeof(buf), "From: \"%%from_name%%\" <%%from%%>\r\nTo:\"%s\" <%s>\r\nSubject:%s\r\n\r\n%s", target, email, "Nick Password", lang_str_l(lang, SENDPASS_X_X, target, pbuf) ); email_init_symbols(); email_add_symbol("email", email); email_send(buf); memset(pbuf, 0, PASSLEN); send_lang(u, s, SENDPASS_X_SENT_X, target, email); log_log(os_log, mod_info.name, "SENDPASS for %s requested by %s", target, u->nick); irc_SendSanotice(s, "SENDPASS for %s requested by %s", target, u->nick); } }