int address_table_write_update(address_table_t* const table, gzFile handle) { if (!gzprintf(handle, "%d %d\n", NORM(table->last - table->added_since_last_update + 1), MAC_TABLE_ENTRIES)) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } int idx; for (idx = table->added_since_last_update; idx > 0; --idx) { int mac_id = NORM(table->last - idx + 1); #ifndef DISABLE_ANONYMIZATION uint64_t digest_ip; uint8_t digest_mac[ETH_ALEN]; if (anonymize_ip(table->entries[mac_id].ip_address, &digest_ip) || anonymize_mac(table->entries[mac_id].mac_address, digest_mac)) { #ifndef NDEBUG fprintf(stderr, "Error anonymizing MAC mapping\n"); #endif return -1; } if (!gzprintf(handle, "%s %" PRIx64 "\n", buffer_to_hex(digest_mac, ETH_ALEN), digest_ip)) { #else if (!gzprintf(handle, "%s %" PRIx32 "\n", buffer_to_hex(table->entries[mac_id].mac_address, ETH_ALEN), table->entries[mac_id].ip_address)) { #endif #ifndef NDEBUG perror("Error writing update"); #endif return -1; } } if (!gzprintf(handle, "\n")) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } table->added_since_last_update = 0; return 0; }
static int init_hex_seed_digest() { unsigned char seed_digest[SHA_DIGEST_LENGTH]; anonymization_process(seed, ANONYMIZATION_SEED_LEN, seed_digest); const char* hex_digest = buffer_to_hex(seed_digest, SHA_DIGEST_LENGTH); if (!hex_digest) { return -1; } memcpy(seed_hex_digest, hex_digest, sizeof(seed_hex_digest)); seed_hex_digest[sizeof(seed_hex_digest) - 1] = '\0'; return 0; }
static void write_frequent_update() { printf("Writing frequent log to %s\n", PENDING_FREQUENT_UPDATE_FILENAME); FILE* handle = fopen(PENDING_FREQUENT_UPDATE_FILENAME, "w"); if (!handle) { perror("Could not open update file for writing"); exit(1); } if (fprintf(handle, "%d\n", FREQUENT_FILE_FORMAT_VERSION) < 0) { perror("Error writing update"); exit(1); } time_t current_timestamp = time(NULL); if (fprintf(handle, "%" PRId64 "\n", (int64_t)current_timestamp) < 0) { perror("Error writing update"); exit(1); } if (fprintf(handle, "%s\n\n", buffer_to_hex(bismark_mac, ETH_ALEN)) < 0) { perror("Error writing update"); exit(1); } if (device_throughput_table_write_update(&device_throughput_table, handle)) { exit(1); } fclose(handle); char update_filename[FILENAME_MAX]; snprintf(update_filename, FILENAME_MAX, FREQUENT_UPDATE_FILENAME, bismark_id, start_timestamp_microseconds, frequent_sequence_number); if (rename(PENDING_FREQUENT_UPDATE_FILENAME, update_filename)) { perror("Could not stage update"); exit(1); } ++frequent_sequence_number; device_throughput_table_init(&device_throughput_table); }
static int init_hex_seed_digest() { unsigned char seed_digest[SHA_DIGEST_LENGTH]; if (anonymization_process(seed, ANONYMIZATION_SEED_LEN, seed_digest)) { return -1; } const char* hex_digest = buffer_to_hex(seed_digest, SHA_DIGEST_LENGTH); if (!hex_digest) { return -1; } int idx; for (idx = 0; idx < SHA_DIGEST_LENGTH; ++idx) { if (sprintf(seed_hex_digest + 2 * idx, "%02x", seed_digest[idx]) < 2) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } } seed_hex_digest[SHA_DIGEST_LENGTH * 2] = '\0'; return 0; }
int dns_table_write_update(dns_table_t* const table, gzFile handle) { if (!gzprintf(handle, "%d %d\n", table->num_dropped_a_entries, table->num_dropped_cname_entries)) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } int idx; for (idx = 0; idx < table->a_length; ++idx) { #ifndef DISABLE_ANONYMIZATION if (table->a_entries[idx].unanonymized) { #endif if (!gzprintf(handle, "%" PRIu16 " %" PRIu8 " 0 %s %" PRIx32 " %" PRId32 "\n", table->a_entries[idx].packet_id, table->a_entries[idx].mac_id, table->a_entries[idx].domain_name, table->a_entries[idx].ip_address, table->a_entries[idx].ttl)) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } #ifndef DISABLE_ANONYMIZATION } else { unsigned char domain_digest[ANONYMIZATION_DIGEST_LENGTH]; uint64_t address_digest; if (anonymize_domain(table->a_entries[idx].domain_name, domain_digest) || anonymize_ip(table->a_entries[idx].ip_address, &address_digest)) { fprintf(stderr, "Error anonymizing DNS data\n"); return -1; } if (!gzprintf(handle, "%" PRIu16 " %" PRIu8 " 1 %s %" PRIx64 " %" PRId32 "\n", table->a_entries[idx].packet_id, table->a_entries[idx].mac_id, buffer_to_hex(domain_digest, ANONYMIZATION_DIGEST_LENGTH), address_digest, table->a_entries[idx].ttl)) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } } #endif } if (!gzprintf(handle, "\n")) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } for (idx = 0; idx < table->cname_length; ++idx) { #ifndef DISABLE_ANONYMIZATION if (table->cname_entries[idx].unanonymized) { #endif if (!gzprintf(handle, "%" PRIu16 " %" PRIu8 " 0 %s %s %" PRId32 "\n", table->cname_entries[idx].packet_id, table->cname_entries[idx].mac_id, table->cname_entries[idx].domain_name, table->cname_entries[idx].cname, table->cname_entries[idx].ttl)) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } #ifndef DISABLE_ANONYMIZATION } else { unsigned char domain_digest[ANONYMIZATION_DIGEST_LENGTH]; unsigned char cname_digest[ANONYMIZATION_DIGEST_LENGTH]; if (anonymize_domain(table->cname_entries[idx].domain_name, domain_digest) || anonymize_domain(table->cname_entries[idx].cname, cname_digest)) { fprintf(stderr, "Error anonymizing DNS data\n"); return -1; } char hex_domain_digest[ANONYMIZATION_DIGEST_LENGTH * 2 + 1]; strcpy(hex_domain_digest, buffer_to_hex(domain_digest, ANONYMIZATION_DIGEST_LENGTH)); char hex_cname_digest[ANONYMIZATION_DIGEST_LENGTH * 2 + 1]; strcpy(hex_cname_digest, buffer_to_hex(cname_digest, ANONYMIZATION_DIGEST_LENGTH)); if (!gzprintf(handle, "%" PRIu16 " %" PRIu8 " 1 %s %s %" PRId32 "\n", table->cname_entries[idx].packet_id, table->cname_entries[idx].mac_id, hex_domain_digest, hex_cname_digest, table->cname_entries[idx].ttl)) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } } #endif } if (!gzprintf(handle, "\n")) { #ifndef NDEBUG perror("Error writing update"); #endif return -1; } return 0; }
int dns_table_write_update(dns_table_t* const table, gzFile handle) { if (!gzprintf(handle, "%d %d\n", table->num_dropped_a_entries, table->num_dropped_cname_entries)) { perror("Error writing update"); return -1; } int idx; for (idx = 0; idx < table->a_length; ++idx) { uint64_t address_digest; #ifndef DISABLE_ANONYMIZATION if (anonymize_ip(table->a_entries[idx].ip_address, &address_digest)) { fprintf(stderr, "Error anonymizing DNS data\n"); return -1; } #else address_digest = table->a_entries[idx].ip_address; #endif unsigned int domain_anonymized; const char* domain_string; if (table->whitelist && !domain_whitelist_lookup(table->whitelist, table->a_entries[idx].domain_name)) { domain_anonymized = 0; domain_string = table->a_entries[idx].domain_name; } else { unsigned char domain_digest[ANONYMIZATION_DIGEST_LENGTH]; if (anonymize_domain(table->a_entries[idx].domain_name, domain_digest)) { fprintf(stderr, "Error anonymizing DNS data\n"); return -1; } char hex_domain_digest[ANONYMIZATION_DIGEST_LENGTH * 2 + 1]; strcpy(hex_domain_digest, buffer_to_hex(domain_digest, ANONYMIZATION_DIGEST_LENGTH)); domain_anonymized = 1; domain_string = hex_domain_digest; } if (!gzprintf(handle, "%" PRIu16 " %" PRIu8 " %u %s %" PRIx64 " %" PRId32 "\n", table->a_entries[idx].packet_id, table->a_entries[idx].mac_id, domain_anonymized, domain_string, address_digest, table->a_entries[idx].ttl)) { perror("Error writing update"); return -1; } } if (!gzprintf(handle, "\n")) { perror("Error writing update"); return -1; } for (idx = 0; idx < table->cname_length; ++idx) { unsigned int domain_anonymized, cname_anonymized; const char* domain_string; const char* cname_string; #ifndef DISABLE_ANONYMIZATION if (table->whitelist && !domain_whitelist_lookup(table->whitelist, table->cname_entries[idx].domain_name)) { #endif domain_anonymized = 0; domain_string = table->cname_entries[idx].domain_name; #ifndef DISABLE_ANONYMIZATION } else { domain_anonymized = 1; unsigned char domain_digest[ANONYMIZATION_DIGEST_LENGTH]; if (anonymize_domain(table->cname_entries[idx].domain_name, domain_digest)) { fprintf(stderr, "Error anonymizing DNS data\n"); return -1; } char hex_domain_digest[ANONYMIZATION_DIGEST_LENGTH * 2 + 1]; strcpy(hex_domain_digest, buffer_to_hex(domain_digest, ANONYMIZATION_DIGEST_LENGTH)); domain_string = hex_domain_digest; } #endif #ifndef DISABLE_ANONYMIZATION if (table->whitelist && !domain_whitelist_lookup(table->whitelist, table->cname_entries[idx].cname)) { #endif cname_anonymized = 0; cname_string = table->cname_entries[idx].cname; #ifndef DISABLE_ANONYMIZATION } else { cname_anonymized = 1; unsigned char cname_digest[ANONYMIZATION_DIGEST_LENGTH]; if (anonymize_domain(table->cname_entries[idx].cname, cname_digest)) { fprintf(stderr, "Error anonymizing DNS data\n"); return -1; } char hex_cname_digest[ANONYMIZATION_DIGEST_LENGTH * 2 + 1]; strcpy(hex_cname_digest, buffer_to_hex(cname_digest, ANONYMIZATION_DIGEST_LENGTH)); cname_string = hex_cname_digest; } #endif if (!gzprintf(handle, "%" PRIu16 " %" PRIu8 " %u %s %u %s %" PRId32 "\n", table->cname_entries[idx].packet_id, table->cname_entries[idx].mac_id, domain_anonymized, domain_string, cname_anonymized, cname_string, table->cname_entries[idx].ttl)) { perror("Error writing update"); return -1; } } if (!gzprintf(handle, "\n")) { perror("Error writing update"); return -1; } return 0; }