forked from cjdoucette/net-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrarray.c
More file actions
221 lines (188 loc) · 4.74 KB
/
strarray.c
File metadata and controls
221 lines (188 loc) · 4.74 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strarray.h>
/* Replace '\0' in @buf to @ch, and return the numer of lines in @buf.
*
* This function properly deals with the case that the last line in @buf
* doesn't end with a '\n'.
*/
static uint64_t process_content(char *buf, uint64_t size, char ch)
{
int empty = 1, first = 1;
uint64_t lines = 0;
uint64_t i;
for (i = 0; i < size; i++) {
switch (buf[i]) {
case '\n':
lines++;
empty = 1; /* So far, the next line is empty. */
break;
case '\0':
if (first) {
warnx("File has '\\0' in its content");
first = 0;
}
buf[i] = ch;
/* Fall through. */
default:
empty = 0;
}
}
return empty ? lines : (lines + 1);
}
/* This function properly deals with the case that the last line in @buf
* doesn't end with a '\n'.
*/
static void map_content_to_array(char *content, char **array, uint64_t size)
{
uint64_t index = 0;
char *p = content;
while (1) {
switch (*p) {
case '\n':
array[index++] = content;
*p = '\0';
content = p + 1;
break;
case '\0':
if (*content)
array[index++] = content;
assert(size == index);
return;
}
p++;
}
assert(0); /* Execution should never reach here. */
}
char **load_file_as_array(const char *filename, uint64_t *parray_size)
{
int fd;
char **array;
uint64_t array_size;
off_t file_size;
char *content;
fd = open(filename, O_RDONLY);
if (fd < 0)
err(1, "Can't open file `%s'", filename);
/* Obtain file's size. */
file_size = lseek(fd, 0, SEEK_END);
if (file_size < 0)
err(1, "Can't find size of file `%s'", filename);
assert(!lseek(fd, 0, SEEK_SET));
/* Bring whole file into memory. */
content = malloc(file_size + 1);
assert(content);
assert(read(fd, content, file_size) == file_size);
assert(!close(fd));
content[file_size] = '\0';
array_size = process_content(content, file_size, '?');
/* Allocate array. */
if (!array_size) {
*parray_size = 0;
return NULL;
}
array = malloc((array_size + 1) * sizeof(array[0]));
assert(array);
array[0] = content; /* Save all content on the first entry. */
array++; /* Ignore the first entry. */
map_content_to_array(content, array, array_size);
*parray_size = array_size;
return array;
}
void free_array(char **array)
{
if (!array)
return;
array--;
free(array[0]); /* Release _all_ content. */
free(array); /* Release pointers. */
}
void print_array(char **array, uint64_t size)
{
uint64_t i;
for (i = 0; i < size; i++)
printf("%" PRIu64 ":%s\n", i, array[i]);
}
static void shuffle_array(char **array, uint64_t size,
uint32_t *seeds, int seeds_len)
{
struct unif_state shuffle_dist;
if (size <= 1)
return;
init_unif(&shuffle_dist, seeds, seeds_len);
do {
uint64_t b = sample_unif_0_n1(&shuffle_dist, size);
if (b) {
/* b is not index 0, so swap array[0] and array[b]. */
char *tmp = array[0];
array[0] = array[b];
array[b] = tmp;
}
array++;
size--;
} while (size > 1);
end_unif(&shuffle_dist);
}
struct net_prefix *load_file_as_shuffled_addrs(const char *filename,
uint64_t *parray_size, uint32_t *seeds, int seeds_len, int force_addr)
{
char **prefix_array =
load_file_as_array(filename, parray_size);
struct net_prefix *prefix, *pp;
uint64_t i, bytes;
if (!(*parray_size))
return NULL;
shuffle_array(prefix_array, *parray_size, seeds, seeds_len);
/*
print_array(prefix_array, *parray_size);
*/
bytes = sizeof(*prefix) * (*parray_size);
prefix = malloc(bytes);
assert(prefix);
/* Convert prefixes into binary addresses. */
pp = prefix;
for (i = 0; i < *parray_size; i++) {
int a, b, c, d, m;
assert(sscanf(prefix_array[i], "%i.%i.%i.%i/%i",
&a, &b, &c, &d, &m) == 5);
assert(0 <= a && a <= 255);
assert(0 <= b && b <= 255);
assert(0 <= c && c <= 255);
assert(0 <= d && d <= 255);
assert(8 <= m && m <= 32);
pp->mask = m;
if (!force_addr)
m = 32;
/* In order to make it an address (it's originally a prefix),
* and avoid multiple prefixes maching the address (IP uses
* longest prefix matching), one has to set the bit just
* after the mask.
*/
pp->addr.id[0] = a;
pp->addr.id[1] = 8 <= m && m < 16 ? b | (0x80 >> (m - 8)) : b;
pp->addr.id[2] = 16 <= m && m < 24 ? c | (0x80 >> (m - 16)) : c;
pp->addr.id[3] = 24 <= m && m < 32 ? d | (0x80 >> (m - 24)) : d;
memset(&pp->addr.id[4], 0, sizeof(pp->addr) - 4);
pp++;
}
free_array(prefix_array);
return prefix;
}
void free_net_prefix(struct net_prefix *prefix)
{
free(prefix);
}
void assign_port(struct net_prefix *prefix, uint64_t array_size, int ports,
struct unif_state *unif)
{
uint64_t i;
for (i = 0; i < array_size; i++)
prefix[i].port = sample_unif_0_n1(unif, ports);
}