Exemplo n.º 1
0
// Returns the smallest 'cats[cat].counter+counter' on all ancestors of 'cat'.
int find_minimum_relationship(CatID cat, int counter) {
  if (cat == NIL) return COUNTER_SENTINEL;
  else {
    return
      min_counter(
        add_counter(counter, cats[cat].counter),
        min_counter(
          find_minimum_relationship(cats[cat].sire, counter + 1),
          find_minimum_relationship(cats[cat].dam, counter + 1)));
  }
}
Exemplo n.º 2
0
/* We do not fail on add_counter here, as some counters may not exist on all
 * hosts. The rest will work, and the missing/failed counters will die nicely
 * elsewhere */
static int add_all_monitors()
{
	int i;
	char tmp[512];

	add_counter(PDH_USER, &(current_han[SG_WIN32_PROC_USER]));
	add_counter(PDH_PRIV, &(current_han[SG_WIN32_PROC_PRIV]));
	add_counter(PDH_IDLE, &(current_han[SG_WIN32_PROC_IDLE]));
	add_counter(PDH_INTER, &(current_han[SG_WIN32_PROC_INT]));
	add_counter(PDH_MEM_CACHE, &(current_han[SG_WIN32_MEM_CACHE]));
	add_counter(PDH_UPTIME, &(current_han[SG_WIN32_UPTIME]));
	add_counter(PDH_PAGEIN, &(current_han[SG_WIN32_PAGEIN]));
	add_counter(PDH_PAGEOUT, &(current_han[SG_WIN32_PAGEOUT]));

	diskio_names = get_instance_list("PhysicalDisk", &diskio_no);
	if (diskio_names != NULL) {
		diskio_rhan = (HCOUNTER *)malloc(sizeof(HCOUNTER) * diskio_no);
		if (diskio_rhan == NULL) {
			PdhCloseQuery(h_query);
			return -1;
		}
		diskio_whan = (HCOUNTER *)malloc(sizeof(HCOUNTER) * diskio_no);
		if (diskio_whan == NULL) {
			PdhCloseQuery(h_query);
			free (diskio_rhan);
			return -1;
		}
		for (i = 0; i < diskio_no; i++) {
			snprintf(tmp, sizeof(tmp), PDH_DISKIOREAD, diskio_names[i]);
			add_counter(tmp, &diskio_rhan[i]);

			snprintf(tmp, sizeof(tmp), PDH_DISKIOWRITE, diskio_names[i]);
			add_counter(tmp, &diskio_whan[i]);
		}
	}
	return 0;
}
Exemplo n.º 3
0
// Prints all relationships between 'cat1' and 'cat2' of the given 'distance'.
void print_relationship_at_distance(
  CatID cat1, CatID cat2, CatID ancestor, int distance, int counter) {
  if (ancestor == NIL) return;
  else {
    // print self iff at 'distance'
    if (distance == add_counter(counter, cats[ancestor].counter)) {
      print_relationship(cat1, cats[ancestor].counter, cat2, counter);
    }

    // recur on sire
    print_relationship_at_distance(
      cat1, cat2, cats[ancestor].sire, distance, counter + 1);

    // recur on dam
    print_relationship_at_distance(
      cat1, cat2, cats[ancestor].dam, distance, counter + 1);
  }
}
Exemplo n.º 4
0
static void
handle_packet_in( uint64_t datapath_id, packet_in message ) {
  UNUSED( datapath_id );
  packet_info packet_info = get_packet_info( message.data );
  traffic *db = message.user_data;

  uint8_t *macsa = packet_info.eth_macsa;
  uint8_t *macda = packet_info.eth_macda;

  learn_fdb( db->fdb, macsa, message.in_port );
  add_counter( db->counter, macsa, 1, message.data->length );
  uint16_t out_port = lookup_fdb( db->fdb, macda );
  if ( out_port != ENTRY_NOT_FOUND_IN_FDB ) {
     send_packet_out( datapath_id, &message, out_port );
     send_flow_mod( datapath_id, macsa, macda, out_port );
  }
  else {
     do_flooding( datapath_id, &message );
  }
}
/*******************************************************************************
* Function Name  : decrypt_pdu
* Description    : Decrypts the incoming stream
* Input          : - input_pointer: expended key address
*                  - input_pointer: input cipher data address
*                  - int: length
* Output         : output_pointer: output plain data address
* Return         : None
*******************************************************************************/
u32 decrypt_pdu(u32 *exp_key, unsigned char *cipher,int len, unsigned char *plain )
{
  int out_len = 0;
  
  /*Getting the value of Nonce by capturing first word from incoming data*/
  n_blocks = len / 16;
  n_remain = len % 16;
  
  
  /*Decryption starts here..................*/
  for ( index=0; index< n_blocks; index++ ) {
    AES_encrypt(AES_InitnalizationVector_Rx,(u32*)aes_out,(u32*)exp_key); 
    bitwise_xor((unsigned char*)(aes_out), &cipher[index*16], &plain[index*16]);
    add_counter((u32*)AES_InitnalizationVector_Rx);
    out_len += 16;
  }
  
  
  for ( index=0; index<16; index++ ) {
    remain[index] = 0;
  }
  
  /*Storing remaining words in remain buffer*/
  for ( index=0; index<n_remain; index++ ) {
    remain[index] = cipher[n_blocks*16+index];
  }
  
  /*Decryption of the last block starts here*/
  AES_encrypt(AES_InitnalizationVector_Rx,(u32*)aes_out,(u32*)exp_key);  
  
  /*Notice that AES_encrypt is called for decryption instead of AES_decrypt*/
  bitwise_xor((unsigned char*)(aes_out),&remain[0], &temp[0]);
  
  /*putting remaining result into plain buffer*/
  for ( index=0; index<n_remain; index++ ) {
    plain[n_blocks*16+index] = temp[index];
  }
  out_len += n_remain;
  return nonce;
}
/*******************************************************************************
* Function Name  : encrypt_pdu
* Description    : Encrypts the incoming stream
* Input          : - input_pointer: expended key address
*                  - input_pointer: input plain data address
*                  - int: length
* Output         : output_pointer: output cipher data address
* Return         : None
*******************************************************************************/
u32 encrypt_pdu(u32 *exp_key, unsigned char *plain, int len, unsigned char *cipher)
{
  int out_len = 0;
  out_len += 4;
  n_blocks = len / 16;
  n_remain = len % 16;
  
  
  
  /*Encryption starts here..................*/
  for ( index=0; index< n_blocks; index++ ) {
    AES_encrypt(AES_InitnalizationVector_Tx,(u32*)aes_out,exp_key); 
    bitwise_xor((unsigned char*)(aes_out), &plain[index*16], &cipher[index*16]);
    add_counter((uint32_t*)AES_InitnalizationVector_Tx);
    out_len += 16;
  }/*n_blocks have been encrypted, each with different counter value*/
  
  
  for ( index=0; index<16; index++ ) {
    remain[index] = 0;
  }
  
  /*Storing remaining words in remain buffer*/
  for ( index=0; index<n_remain; index++ ) {
    remain[index] = plain[n_blocks*16+index];
  }
  
  /*Encryption of the last block starts here*/
  AES_encrypt(/*(u32*)*/AES_InitnalizationVector_Tx,(u32*)aes_out,(u32*)exp_key); 
  bitwise_xor((unsigned char*)(aes_out),&remain[0], &temp[0]);
  
  /*putting remaining result into cipher buffer*/
  for ( index=0; index<n_remain; index++ ) {
    cipher[n_blocks*16+index] = temp[index];
  }
  
  out_len += n_remain;
  return nonce;
}
/*******************************************************************************
* Function Name  : encrypt_pdu
* Description    : Encrypts the incoming stream
* Input          : - input_pointer: expended key address
*                  - input_pointer: input plain data address
*                  - int: length
* Output         : output_pointer: output pCipher data address
* Return         : None
*******************************************************************************/
u32 encrypt_pdu(u32 *aExp_key, unsigned char *pPlain, int len, unsigned char *pCipher)
{
    int out_len = 0;
    out_len += 4;
    n_blocks = len / 16;
    n_remain = len % 16;



    /*Encryption starts here..................*/
    for ( g_index=0; g_index< n_blocks; g_index++ ) {
        AES_encrypt(aAES_InitnalizationVector_Tx,(u32*)aes_out,aExp_key);
        bitwise_xor((unsigned char*)(aes_out), &pPlain[g_index*16], &pCipher[g_index*16]);
        add_counter((uint32_t*)aAES_InitnalizationVector_Tx);
        out_len += 16;
    }/*n_blocks have been encrypted, each with different counter value*/


    for ( g_index=0; g_index<16; g_index++ ) {
        aRemain[g_index] = 0;
    }

    /*Storing remaining words in aRemain buffer*/
    for ( g_index=0; g_index<n_remain; g_index++ ) {
        aRemain[g_index] = pPlain[n_blocks*16+g_index];
    }

    /*Encryption of the last block starts here*/
    AES_encrypt(/*(u32*)*/aAES_InitnalizationVector_Tx,(u32*)aes_out,(u32*)aExp_key);
    bitwise_xor((unsigned char*)(aes_out),&aRemain[0], &aTemp[0]);

    /*putting remaining result into pCipher buffer*/
    for ( g_index=0; g_index<n_remain; g_index++ ) {
        pCipher[n_blocks*16+g_index] = aTemp[g_index];
    }

    out_len += n_remain;
    return nonce;
}
Exemplo n.º 8
0
static struct nftnl_rule *setup_rule(uint8_t family, const char *table,
				   const char *chain, const char *handle)
{
	struct nftnl_rule *r = NULL;
	uint8_t proto;
	uint16_t dport;
	uint64_t handle_num;

	r = nftnl_rule_alloc();
	if (r == NULL) {
		perror("OOM");
		exit(EXIT_FAILURE);
	}

	nftnl_rule_set(r, NFTNL_RULE_TABLE, table);
	nftnl_rule_set(r, NFTNL_RULE_CHAIN, chain);
	nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);

	if (handle != NULL) {
		handle_num = atoll(handle);
		nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
	}

	proto = IPPROTO_TCP;
	add_payload(r, NFT_PAYLOAD_NETWORK_HEADER, NFT_REG_1,
		    offsetof(struct iphdr, protocol), sizeof(uint8_t));
	add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &proto, sizeof(uint8_t));

	dport = htons(22);
	add_payload(r, NFT_PAYLOAD_TRANSPORT_HEADER, NFT_REG_1,
		    offsetof(struct tcphdr, dest), sizeof(uint16_t));
	add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &dport, sizeof(uint16_t));

	add_counter(r);

	return r;
}
Exemplo n.º 9
0
static void
handle_flow_removed( uint64_t datapath_id, flow_removed message ) {
  UNUSED( datapath_id );
  traffic *db = message.user_data;
  add_counter( db->counter, message.match.dl_src, message.packet_count, message.byte_count );
}
Exemplo n.º 10
0
int main(int argc, char** argv) {
	if (argc < 2) {
		fprintf(stderr, "Usage: ./stat outputfile\n");
		exit(1);
	}
	int fd_out;
	if (!(fd_out = open(argv[1], O_WRONLY | O_CREAT, 0644))) {
		fprintf(stderr, "Cannot open output file!\n");
		exit(1);
	}

	MYSQL mysql;
	MYSQL *sock = mysql_connect_db(&mysql, DBHOST, DBUSER, DBPASS, DBNAME, DBSOCK);
	if (sock == NULL)
		exit(1);

	int rows_num = mysql_result_int(sock, "SELECT COUNT(*) FROM files");
	if (rows_num <= 0) {
		fprintf(stderr, "Abnormal row num of files: %d\n", rows_num);
		exit(1);
	}
#ifdef DEBUG
	fprintf(stderr, "\nTable file1 Rows: %d\n", rows_num);
#endif
#ifdef DEBUGTIME
	int start_time = time(NULL);
	fprintf(stderr, "Start loading files: epoch %d\n", start_time);
#endif
	// we hope the hashlist will have not too much collision
	int slot_num = rows_num * 3;
	list l = init_list(slot_num);
	if (l == NULL) {
		fprintf(stderr, "Error creating hashlist with %d slots\n", slot_num);
		exit(1);
	}

	init_get_next_url(sock, "files");
	char url[512];
	int length;
	while (0 <= (length = get_next_url(url))) {
		if (length > 0)
			add_str(l, slot_num, url, length);
	}

#ifdef DEBUG
	fprintf(stderr, "\n%d slots occupied after loading files\n", occupied_slots_num(l, slot_num));
	fprintf(stderr, "Hash collision %d times\n", hash_collision_count());
	fprintf(stderr, "Repeat string count: %d\n", repeat_str_count());
#endif
#ifdef DEBUGTIME
	fprintf(stderr, "Start loading log @%ds\n", time(NULL) - start_time);
#endif

	init_get_next_url(sock, "log");
	while (0 <= (length = get_next_url(url))) {
		if (length > 0)
			add_counter(l, slot_num, url, length);
	}

#ifdef DEBUG
	fprintf(stderr, "\nNot found strings count: %d\n", notfound_str_count());
	fprintf(stderr, "\nFound strings count: %d\n", found_str_count());
#endif
#ifdef DEBUGTIME
	fprintf(stderr, "Start loading files @%ds\n", time(NULL) - start_time);
#endif

	init_buffered_get_next_row(sock, "SELECT * FROM files");
	MYSQL_ROW file_fields;
	// Do not free() me! It contains many rows and is managed by buffer.
	MYSQL_RES* res;
	while (res = buffered_get_next_row(&file_fields)) {
		// the first field is id, second is url
		unsigned long *lengths = mysql_fetch_lengths(res);
		unsigned long count = get_counter(l, slot_num, file_fields[1], lengths[1]);
		if (unlikely(count == 0)) {
			fprintf(stderr, "\nError: file hash not found: %s\n", file_fields[1]);
			continue;
		}
		char* line;
		// note that the counter includes one in table `files'
		unsigned len = array_implode(&line, mysql_num_fields(res), file_fields, lengths, count-1);
		write(fd_out, line, len);
		free(line);
	}

#ifdef DEBUGTIME
	fprintf(stderr, "\nEND @%ds\n", time(NULL) - start_time);
#endif
	mysql_close(&mysql);
	close(fd_out);
	return 0;
}