/** * Prepare accounting request data. * @param[in] session Session. * @param[in,out] req Accounting data request. */ static void zrad_acct_prepare(zscope_t *scope, zsession_t *session, zrad_acct_req_t *req) { zclient_t *client = zsession_get_client(session); pthread_spin_lock(&client->lock); req->username = client->login; snprintf(req->session_id, sizeof(req->session_id), "%s-%" PRIu32, client->login, session->ip); pthread_spin_unlock(&client->lock); zclient_release(client); req->calling_station_id = session->ip_str; req->framed_ip_addr = session->ip; req->nas_id = scope->cfg->radius.nas_id; req->traff_down = atomic_load_acquire(&session->traff_down); req->traff_up = atomic_load_acquire(&session->traff_up); req->octets_down = (uint32_t) (req->traff_down % UINT32_MAX); req->octets_up = (uint32_t) (req->traff_up % UINT32_MAX); req->packets_down = atomic_load_acquire(&session->packets_down) % UINT32_MAX; req->packets_up = atomic_load_acquire(&session->packets_up) % UINT32_MAX; req->gigawords_down = (uint32_t) (req->traff_down / UINT32_MAX); req->gigawords_up = (uint32_t) (req->traff_up / UINT32_MAX); req->authentic = PW_RADIUS; if (PW_STATUS_STOP == req->status) { req->session_time = (uint32_t) USEC2SEC(ztime() - session->create_time); } }
/** * Calculate speed. * @param[in] speed * @return Calculated speed. */ uint64_t spdm_calc(const struct speed_meter *speed) { uint64_t aux = 0; uint64_t curr_time = zclock(false); for (size_t i = 0; i < SPEED_METER_BACKLOG; i++) { uint64_t diff = USEC2SEC(curr_time - atomic_load_explicit(&speed->backlog[i].timestamp, memory_order_acquire)); if (diff <= SPEED_METER_BACKLOG) { aux += atomic_load_explicit(&speed->backlog[i].speed, memory_order_acquire); } } return aux / SPEED_METER_BACKLOG; }
void *runAudience(void* idPtr) { long id = (long) idPtr; int sleepDuration = 1000; int roundsCompleted = 0; long dancer = NO_DANCER; for (roundsCompleted = 0; roundsCompleted != nRounds || nRounds == 0; roundsCompleted++) { // Vegetate printf("Audience %ld: Beginning vegetation \n", id); sleepDuration = rand() % SEC2USEC(10); printf("Audience %ld: Sleeping for %.2lf seconds\n", id, USEC2SEC(sleepDuration)); usleep(sleepDuration); //usleep(SEC2USEC(20)); // Select Dancer // Lock prevents audience members adding themselves while others are being signalled by runDancer() dancer = randomDancer(); pthread_mutex_lock(&(watchMutexes[dancer])); toWatch[dancer]++; validRequest[dancer] = true; printf("Audience %ld: Selected to watch dancer: %ld\n", id, dancer); pthread_mutex_unlock(&(watchMutexes[dancer])); // Watch // Wait on semaphore until dancer starts dancing sem_wait(&toWatchSemaphores[dancer]); printf("Audience %ld: Now Watching dancer: %ld\n", id, dancer); // Observe leave // Wait on semaphore until dancer finishes dancing sem_wait(&nowWatchingSemaphore); //Indicate that the audience member has finished watching pthread_mutex_lock(&nAudienceWatchingMutex); nAudienceWatching--; pthread_mutex_unlock(&nAudienceWatchingMutex); } printf("Audience %ld: Finished %d rounds, dying now.\n", id, nRounds); return NULL; }
void zmonitor_mirror_packet(struct zmonitor *mon, unsigned const char *packet, size_t len) { pthread_rwlock_rdlock(&mon->lock); if (likely(!utarray_len(&mon->monitors))) { goto end; } uint64_t ts = ztime(false); struct pcap_pkthdr pkthdr = { .ts = {0}, .caplen = (uint32_t) len, .len = (uint32_t) len }; struct pcap_sf_pkthdr sf_pkthdr = { .ts = {.tv_sec = (uint32_t) USEC2SEC(ts), .tv_usec = (uint32_t) (ts % 1000000)}, .caplen = (uint32_t) len, .len = (uint32_t) len }; struct zmonitor_conn **pconn = (struct zmonitor_conn **) utarray_front(&mon->monitors); while (pconn) { size_t bufsize = evbuffer_get_length(bufferevent_get_output((*pconn)->bev)); if (bufsize > MONITOR_BUFSIZE) { break; } if ((0 == (*pconn)->bpf.bf_len) || (0 != pcap_offline_filter(&(*pconn)->bpf, &pkthdr, packet))) { if (0 != token_bucket_update(&(*pconn)->band, len)) { break; }