(libT) copyediting: reformat blocklist.c to the new indentation style

This commit is contained in:
Jordan Lee
2013-01-21 17:39:20 +00:00
parent 29d6f5d934
commit 1ece2b3f98
+203 -202
View File
@@ -49,98 +49,96 @@
struct tr_ipv4_range struct tr_ipv4_range
{ {
uint32_t begin; uint32_t begin;
uint32_t end; uint32_t end;
}; };
struct tr_blocklist struct tr_blocklist
{ {
bool isEnabled; bool isEnabled;
int fd; int fd;
size_t ruleCount; size_t ruleCount;
size_t byteCount; size_t byteCount;
char * filename; char * filename;
struct tr_ipv4_range * rules; struct tr_ipv4_range * rules;
}; };
static void static void
blocklistClose (tr_blocklist * b) blocklistClose (tr_blocklist * b)
{ {
if (b->rules) if (b->rules != NULL)
{ {
munmap (b->rules, b->byteCount); munmap (b->rules, b->byteCount);
close (b->fd); close (b->fd);
b->rules = NULL; b->rules = NULL;
b->ruleCount = 0; b->ruleCount = 0;
b->byteCount = 0; b->byteCount = 0;
b->fd = -1; b->fd = -1;
} }
} }
static void static void
blocklistLoad (tr_blocklist * b) blocklistLoad (tr_blocklist * b)
{ {
int fd; int fd;
size_t byteCount; size_t byteCount;
struct stat st; struct stat st;
const char * err_fmt = _("Couldn't read \"%1$s\": %2$s"); char * base;
const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
blocklistClose (b); blocklistClose (b);
if (stat (b->filename, &st) == -1) if (stat (b->filename, &st) == -1)
return; return;
fd = open (b->filename, O_RDONLY | O_BINARY); fd = open (b->filename, O_RDONLY | O_BINARY);
if (fd == -1) if (fd == -1)
{ {
tr_err (err_fmt, b->filename, tr_strerror (errno)); tr_err (err_fmt, b->filename, tr_strerror (errno));
return; return;
} }
byteCount = (size_t) st.st_size; byteCount = (size_t) st.st_size;
b->rules = mmap (NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0); b->rules = mmap (NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0);
if (!b->rules) if (!b->rules)
{ {
tr_err (err_fmt, b->filename, tr_strerror (errno)); tr_err (err_fmt, b->filename, tr_strerror (errno));
close (fd); close (fd);
return; return;
} }
b->fd = fd; b->fd = fd;
b->byteCount = byteCount; b->byteCount = byteCount;
b->ruleCount = byteCount / sizeof (struct tr_ipv4_range); b->ruleCount = byteCount / sizeof (struct tr_ipv4_range);
{ base = tr_basename (b->filename);
char * base = tr_basename (b->filename); tr_inf (_("Blocklist \"%s\" contains %zu entries"), base, b->ruleCount);
tr_inf (_("Blocklist \"%s\" contains %zu entries"), base, b->ruleCount); tr_free (base);
tr_free (base);
}
} }
static void static void
blocklistEnsureLoaded (tr_blocklist * b) blocklistEnsureLoaded (tr_blocklist * b)
{ {
if (!b->rules) if (b->rules == NULL)
blocklistLoad (b); blocklistLoad (b);
} }
static int static int
compareAddressToRange (const void * va, compareAddressToRange (const void * va, const void * vb)
const void * vb)
{ {
const uint32_t * a = va; const uint32_t * a = va;
const struct tr_ipv4_range * b = vb; const struct tr_ipv4_range * b = vb;
if (*a < b->begin) return -1; if (*a < b->begin) return -1;
if (*a > b->end) return 1; if (*a > b->end) return 1;
return 0; return 0;
} }
static void static void
blocklistDelete (tr_blocklist * b) blocklistDelete (tr_blocklist * b)
{ {
blocklistClose (b); blocklistClose (b);
unlink (b->filename); unlink (b->filename);
} }
/*** /***
@@ -150,83 +148,83 @@ blocklistDelete (tr_blocklist * b)
tr_blocklist * tr_blocklist *
_tr_blocklistNew (const char * filename, bool isEnabled) _tr_blocklistNew (const char * filename, bool isEnabled)
{ {
tr_blocklist * b; tr_blocklist * b;
b = tr_new0 (tr_blocklist, 1); b = tr_new0 (tr_blocklist, 1);
b->fd = -1; b->fd = -1;
b->filename = tr_strdup (filename); b->filename = tr_strdup (filename);
b->isEnabled = isEnabled; b->isEnabled = isEnabled;
return b; return b;
} }
const char* const char*
_tr_blocklistGetFilename (const tr_blocklist * b) _tr_blocklistGetFilename (const tr_blocklist * b)
{ {
return b->filename; return b->filename;
} }
void void
_tr_blocklistFree (tr_blocklist * b) _tr_blocklistFree (tr_blocklist * b)
{ {
blocklistClose (b); blocklistClose (b);
tr_free (b->filename); tr_free (b->filename);
tr_free (b); tr_free (b);
} }
int int
_tr_blocklistExists (const tr_blocklist * b) _tr_blocklistExists (const tr_blocklist * b)
{ {
struct stat st; struct stat st;
return !stat (b->filename, &st); return !stat (b->filename, &st);
} }
int int
_tr_blocklistGetRuleCount (const tr_blocklist * b) _tr_blocklistGetRuleCount (const tr_blocklist * b)
{ {
blocklistEnsureLoaded ((tr_blocklist*)b); blocklistEnsureLoaded ((tr_blocklist*)b);
return b->ruleCount; return b->ruleCount;
} }
int int
_tr_blocklistIsEnabled (tr_blocklist * b) _tr_blocklistIsEnabled (tr_blocklist * b)
{ {
return b->isEnabled; return b->isEnabled;
} }
void void
_tr_blocklistSetEnabled (tr_blocklist * b, bool isEnabled) _tr_blocklistSetEnabled (tr_blocklist * b, bool isEnabled)
{ {
b->isEnabled = isEnabled ? 1 : 0; b->isEnabled = isEnabled ? 1 : 0;
} }
int int
_tr_blocklistHasAddress (tr_blocklist * b, const tr_address * addr) _tr_blocklistHasAddress (tr_blocklist * b, const tr_address * addr)
{ {
uint32_t needle; uint32_t needle;
const struct tr_ipv4_range * range; const struct tr_ipv4_range * range;
assert (tr_address_is_valid (addr)); assert (tr_address_is_valid (addr));
if (!b->isEnabled || addr->type == TR_AF_INET6) if (!b->isEnabled || addr->type == TR_AF_INET6)
return 0; return 0;
blocklistEnsureLoaded (b); blocklistEnsureLoaded (b);
if (!b->rules || !b->ruleCount) if (!b->rules || !b->ruleCount)
return 0; return 0;
needle = ntohl (addr->addr.addr4.s_addr); needle = ntohl (addr->addr.addr4.s_addr);
range = bsearch (&needle, range = bsearch (&needle,
b->rules, b->rules,
b->ruleCount, b->ruleCount,
sizeof (struct tr_ipv4_range), sizeof (struct tr_ipv4_range),
compareAddressToRange); compareAddressToRange);
return range != NULL; return range != NULL;
} }
/* /*
@@ -237,33 +235,33 @@ _tr_blocklistHasAddress (tr_blocklist * b, const tr_address * addr)
static bool static bool
parseLine1 (const char * line, struct tr_ipv4_range * range) parseLine1 (const char * line, struct tr_ipv4_range * range)
{ {
char * walk; char * walk;
int b[4]; int b[4];
int e[4]; int e[4];
char str[64]; char str[64];
tr_address addr; tr_address addr;
walk = strrchr (line, ':'); walk = strrchr (line, ':');
if (!walk) if (!walk)
return false; return false;
++walk; /* walk past the colon */ ++walk; /* walk past the colon */
if (sscanf (walk, "%d.%d.%d.%d-%d.%d.%d.%d", if (sscanf (walk, "%d.%d.%d.%d-%d.%d.%d.%d",
&b[0], &b[1], &b[2], &b[3], &b[0], &b[1], &b[2], &b[3],
&e[0], &e[1], &e[2], &e[3]) != 8) &e[0], &e[1], &e[2], &e[3]) != 8)
return false; return false;
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]); tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
if (!tr_address_from_string (&addr, str)) if (!tr_address_from_string (&addr, str))
return false; return false;
range->begin = ntohl (addr.addr.addr4.s_addr); range->begin = ntohl (addr.addr.addr4.s_addr);
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", e[0], e[1], e[2], e[3]); tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", e[0], e[1], e[2], e[3]);
if (!tr_address_from_string (&addr, str)) if (!tr_address_from_string (&addr, str))
return false; return false;
range->end = ntohl (addr.addr.addr4.s_addr); range->end = ntohl (addr.addr.addr4.s_addr);
return true; return true;
} }
/* /*
@@ -273,159 +271,162 @@ parseLine1 (const char * line, struct tr_ipv4_range * range)
static bool static bool
parseLine2 (const char * line, struct tr_ipv4_range * range) parseLine2 (const char * line, struct tr_ipv4_range * range)
{ {
int unk; int unk;
int a[4]; int a[4];
int b[4]; int b[4];
char str[32]; char str[32];
tr_address addr; tr_address addr;
if (sscanf (line, "%3d.%3d.%3d.%3d - %3d.%3d.%3d.%3d , %3d , ", if (sscanf (line, "%3d.%3d.%3d.%3d - %3d.%3d.%3d.%3d , %3d , ",
&a[0], &a[1], &a[2], &a[3], &a[0], &a[1], &a[2], &a[3],
&b[0], &b[1], &b[2], &b[3], &b[0], &b[1], &b[2], &b[3],
&unk) != 9) &unk) != 9)
return false; return false;
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
if (!tr_address_from_string (&addr, str)) if (!tr_address_from_string (&addr, str))
return false; return false;
range->begin = ntohl (addr.addr.addr4.s_addr); range->begin = ntohl (addr.addr.addr4.s_addr);
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]); tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
if (!tr_address_from_string (&addr, str)) if (!tr_address_from_string (&addr, str))
return false; return false;
range->end = ntohl (addr.addr.addr4.s_addr); range->end = ntohl (addr.addr.addr4.s_addr);
return true; return true;
} }
static int static int
parseLine (const char * line, struct tr_ipv4_range * range) parseLine (const char * line, struct tr_ipv4_range * range)
{ {
return parseLine1 (line, range) return parseLine1 (line, range)
|| parseLine2 (line, range); || parseLine2 (line, range);
} }
static int static int
compareAddressRangesByFirstAddress (const void * va, const void * vb) compareAddressRangesByFirstAddress (const void * va, const void * vb)
{ {
const struct tr_ipv4_range * a = va; const struct tr_ipv4_range * a = va;
const struct tr_ipv4_range * b = vb; const struct tr_ipv4_range * b = vb;
if (a->begin != b->begin) if (a->begin != b->begin)
return a->begin < b->begin ? -1 : 1; return a->begin < b->begin ? -1 : 1;
return 0; return 0;
} }
int int
_tr_blocklistSetContent (tr_blocklist * b, const char * filename) _tr_blocklistSetContent (tr_blocklist * b, const char * filename)
{ {
FILE * in; FILE * in;
FILE * out; FILE * out;
int inCount = 0; int inCount = 0;
char line[2048]; char line[2048];
const char * err_fmt = _("Couldn't read \"%1$s\": %2$s"); const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
struct tr_ipv4_range * ranges = NULL; struct tr_ipv4_range * ranges = NULL;
size_t ranges_alloc = 0; size_t ranges_alloc = 0;
size_t ranges_count = 0; size_t ranges_count = 0;
if (!filename) if (!filename)
{ {
blocklistDelete (b); blocklistDelete (b);
return 0; return 0;
} }
in = fopen (filename, "rb"); in = fopen (filename, "rb");
if (!in) if (in == NULL)
{ {
tr_err (err_fmt, filename, tr_strerror (errno)); tr_err (err_fmt, filename, tr_strerror (errno));
return 0; return 0;
} }
blocklistClose (b); blocklistClose (b);
out = fopen (b->filename, "wb+"); out = fopen (b->filename, "wb+");
if (!out) if (out == NULL)
{ {
tr_err (err_fmt, b->filename, tr_strerror (errno)); tr_err (err_fmt, b->filename, tr_strerror (errno));
fclose (in); fclose (in);
return 0; return 0;
} }
/* load the rules into memory */ /* load the rules into memory */
while (fgets (line, sizeof (line), in) != NULL) while (fgets (line, sizeof (line), in) != NULL)
{ {
char * walk; char * walk;
struct tr_ipv4_range range; struct tr_ipv4_range range;
++inCount; ++inCount;
/* zap the linefeed */ /* zap the linefeed */
if ((walk = strchr (line, '\r'))) *walk = '\0'; if ((walk = strchr (line, '\r'))) *walk = '\0';
if ((walk = strchr (line, '\n'))) *walk = '\0'; if ((walk = strchr (line, '\n'))) *walk = '\0';
if (!parseLine (line, &range)) if (!parseLine (line, &range))
{ {
/* don't try to display the actual lines - it causes issues */ /* don't try to display the actual lines - it causes issues */
tr_err (_("blocklist skipped invalid address at line %d"), inCount); tr_err (_("blocklist skipped invalid address at line %d"), inCount);
continue; continue;
} }
if (ranges_alloc == ranges_count) if (ranges_alloc == ranges_count)
{ {
ranges_alloc += 4096; /* arbitrary */ ranges_alloc += 4096; /* arbitrary */
ranges = tr_renew (struct tr_ipv4_range, ranges, ranges_alloc); ranges = tr_renew (struct tr_ipv4_range, ranges, ranges_alloc);
} }
ranges[ranges_count++] = range; ranges[ranges_count++] = range;
} }
if (ranges_count > 0) /* sort and merge */ if (ranges_count > 0) /* sort and merge */
{ {
struct tr_ipv4_range * r; struct tr_ipv4_range * r;
struct tr_ipv4_range * keep = ranges; struct tr_ipv4_range * keep = ranges;
const struct tr_ipv4_range * end; const struct tr_ipv4_range * end;
/* sort */ /* sort */
qsort (ranges, ranges_count, sizeof (struct tr_ipv4_range), qsort (ranges, ranges_count, sizeof (struct tr_ipv4_range),
compareAddressRangesByFirstAddress); compareAddressRangesByFirstAddress);
/* merge */ /* merge */
for (r=ranges+1, end=ranges+ranges_count; r!=end; ++r) { for (r=ranges+1, end=ranges+ranges_count; r!=end; ++r) {
if (keep->end < r->begin) if (keep->end < r->begin)
*++keep = *r; *++keep = *r;
else if (keep->end < r->end) else if (keep->end < r->end)
keep->end = r->end; keep->end = r->end;
} }
ranges_count = keep + 1 - ranges; ranges_count = keep + 1 - ranges;
#ifndef NDEBUG #ifndef NDEBUG
/* sanity checks: make sure the rules are sorted /* sanity checks: make sure the rules are sorted
* in ascending order and don't overlap */ * in ascending order and don't overlap */
{ {
size_t i; size_t i;
for (i=0; i<ranges_count; ++i) for (i=0; i<ranges_count; ++i)
assert (ranges[i].begin <= ranges[i].end); assert (ranges[i].begin <= ranges[i].end);
for (i=1; i<ranges_count; ++i) for (i=1; i<ranges_count; ++i)
assert (ranges[i-1].end < ranges[i].begin); assert (ranges[i-1].end < ranges[i].begin);
} }
#endif #endif
} }
if (fwrite (ranges, sizeof (struct tr_ipv4_range), ranges_count, out) != ranges_count) if (fwrite (ranges, sizeof (struct tr_ipv4_range), ranges_count, out) != ranges_count)
tr_err (_("Couldn't save file \"%1$s\": %2$s"), b->filename, tr_strerror (errno)); {
else { tr_err (_("Couldn't save file \"%1$s\": %2$s"), b->filename, tr_strerror (errno));
char * base = tr_basename (b->filename); }
tr_inf (_("Blocklist \"%s\" updated with %zu entries"), base, ranges_count); else
tr_free (base); {
char * base = tr_basename (b->filename);
tr_inf (_("Blocklist \"%s\" updated with %zu entries"), base, ranges_count);
tr_free (base);
} }
tr_free (ranges); tr_free (ranges);
fclose (out); fclose (out);
fclose (in); fclose (in);
blocklistLoad (b); blocklistLoad (b);
return ranges_count; return ranges_count;
} }