summaryrefslogtreecommitdiff
path: root/ot_accesslist.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-24 04:56:27 +0200
committerDirk Engling <erdgeist@erdgeist.org>2026-10-24 04:56:27 +0200
commit6ab9188d0cec3d887f537894ed669fe803049746 (patch)
treef70827d8423ed4d496cb7d20ca12d82a9dc37ef9 /ot_accesslist.c
parent1aa3411546ec370d600699906b7336eff665c077 (diff)
Implement a tiny mmap wrapper ourselves, replacing libowfat's wrapper
Diffstat (limited to 'ot_accesslist.c')
-rw-r--r--ot_accesslist.c41
1 files changed, 35 insertions, 6 deletions
diff --git a/ot_accesslist.c b/ot_accesslist.c
index e20f42c..6315bc2 100644
--- a/ot_accesslist.c
+++ b/ot_accesslist.c
@@ -10,15 +10,19 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12#include <unistd.h> 12#include <unistd.h>
13#ifdef WANT_DYNAMIC_ACCESSLIST 13#if defined(WANT_ACCESSLIST_BLACK) || defined(WANT_ACCESSLIST_WHITE)
14#include <errno.h> 14#include <fcntl.h>
15#include <stdint.h>
16#include <sys/mman.h>
15#include <sys/stat.h> 17#include <sys/stat.h>
16#include <sys/types.h> 18#include <sys/types.h>
17#endif 19#endif
20#ifdef WANT_DYNAMIC_ACCESSLIST
21#include <errno.h>
22#endif
18 23
19/* Libowfat */ 24/* Libowfat */
20#include "ip6.h" 25#include "ip6.h"
21#include "mmap.h"
22 26
23/* Opentracker */ 27/* Opentracker */
24#include "ot_accesslist.h" 28#include "ot_accesslist.h"
@@ -120,12 +124,37 @@ size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf,
120 return count; 124 return count;
121} 125}
122 126
127/* Map a file read only and report its size. Returns NULL if the file cannot
128 be opened, is empty, or is too large to map, leaving maplen untouched. */
129static const char *accesslist_map(const char *filename, size_t *maplen) {
130 struct stat st;
131 const char *map;
132 int fd = open(filename, O_RDONLY);
133
134 if (fd < 0)
135 return NULL;
136
137 if (fstat(fd, &st) < 0 || st.st_size <= 0 || (uintmax_t)st.st_size > (uintmax_t)SIZE_MAX) {
138 close(fd);
139 return NULL;
140 }
141
142 map = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0);
143 close(fd);
144
145 if (map == MAP_FAILED)
146 return NULL;
147
148 *maplen = (size_t)st.st_size;
149 return map;
150}
151
123static void accesslist_readfile(void) { 152static void accesslist_readfile(void) {
124 ot_accesslist *accesslist_new; 153 ot_accesslist *accesslist_new;
125 const char *map; 154 const char *map;
126 size_t maplen, capacity, found; 155 size_t maplen, capacity, found;
127 156
128 if ((map = mmap_read(g_accesslist_filename, &maplen)) == NULL) { 157 if ((map = accesslist_map(g_accesslist_filename, &maplen)) == NULL) {
129 char *wd = getcwd(NULL, 0); 158 char *wd = getcwd(NULL, 0);
130 fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd); 159 fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd);
131 free(wd); 160 free(wd);
@@ -137,7 +166,7 @@ static void accesslist_readfile(void) {
137 accesslist_new = accesslist_make(g_accesslist, capacity); 166 accesslist_new = accesslist_make(g_accesslist, capacity);
138 if (!accesslist_new) { 167 if (!accesslist_new) {
139 fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", capacity * sizeof(ot_hash)); 168 fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", capacity * sizeof(ot_hash));
140 mmap_unmap(map, maplen); 169 munmap((void *)map, maplen);
141 return; 170 return;
142 } 171 }
143 172
@@ -146,7 +175,7 @@ static void accesslist_readfile(void) {
146 fprintf(stderr, "Added %zd info_hashes to accesslist\n", found); 175 fprintf(stderr, "Added %zd info_hashes to accesslist\n", found);
147#endif 176#endif
148 177
149 mmap_unmap(map, maplen); 178 munmap((void *)map, maplen);
150 179
151 qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash); 180 qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash);
152 accesslist_new->size = found; 181 accesslist_new->size = found;