File: pbm.c

package info (click to toggle)
zmap 4.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,628 kB
  • sloc: ansic: 15,033; python: 1,085; yacc: 125; sh: 120; lex: 28; makefile: 4
file content (111 lines) | stat: -rw-r--r-- 2,578 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2021 Regents of the University of Michigan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "logger.h"
#include "xalloc.h"

#define NUM_VALUES 0xFFFFFFFF
#define PAGE_SIZE_IN_BITS 0x10000
#define PAGE_SIZE_IN_BYTES (PAGE_SIZE_IN_BITS / 8)
#define NUM_PAGES 0x10000
#define PAGE_MASK 0xFFFF

uint8_t **pbm_init(void)
{
	uint8_t **retv = xcalloc(NUM_PAGES, sizeof(void *));
	return retv;
}

uint8_t *bm_init(void)
{
	uint8_t *bm = xmalloc(PAGE_SIZE_IN_BYTES);
	memset(bm, 0, PAGE_SIZE_IN_BYTES);
	return bm;
}

int bm_check(uint8_t *bm, uint16_t v)
{
	uint16_t page_idx = (v >> 3);
	uint8_t bit_idx = (uint8_t)(v & 0x07);
	return bm[page_idx] & (1 << bit_idx);
}

void bm_set(uint8_t *bm, uint16_t v)
{
	uint16_t page_idx = (v >> 3);
	uint8_t bit_idx = (uint8_t)(v & 0x07);
	bm[page_idx] |= (1 << bit_idx);
}

int pbm_check(uint8_t **b, uint32_t v)
{
	uint32_t top = v >> 16;
	uint32_t bottom = v & PAGE_MASK;
	return b[top] && bm_check(b[top], bottom);
}

void pbm_set(uint8_t **b, uint32_t v)
{
	uint16_t top = (uint16_t)(v >> 16);
	uint16_t bottom = (uint16_t)(v & PAGE_MASK);
	if (!b[top]) {
		b[top] = bm_init();
	}
	bm_set(b[top], bottom);
}

uint32_t pbm_load_from_file(uint8_t **b, char *file)
{
	if (!b) {
		log_fatal("pbm", "load_from_file called with NULL PBM");
	}
	if (!file) {
		log_fatal("pbm", "load_from_file called with NULL filename");
	}
	FILE *fp = fopen(file, "r");
	if (fp == NULL) {
		log_fatal("pbm", "unable to open file: %s: %s", file,
			  strerror(errno));
	}
	char line[1000];
	uint32_t count = 0;
	while (fgets(line, sizeof(line), fp)) {
		char *comment = strchr(line, '#');
		if (comment) {
			*comment = '\0';
		}
		struct in_addr addr;
		if (inet_aton(line, &addr) != 1) {
			log_fatal("pbm", "unable to parse IP address: %s",
				  line);
		}
		pbm_set(b, addr.s_addr);
		++count;
	}
	fclose(fp);
	return count;
}