Commit e2379456 authored by Jan Včelák's avatar Jan Včelák 🚀 Committed by Daniel Salzman

rrl: coding style, braces around one-line statements

parent 46a5881d
......@@ -152,8 +152,9 @@ static int rrl_clsname(char *dst, size_t maxlen, uint8_t cls,
break;
default:
/* Use QNAME */
if (req->query)
if (req->query) {
dn = knot_pkt_qname(req->query);
}
break;
}
......@@ -182,7 +183,9 @@ static int rrl_classify(char *dst, size_t maxlen, const struct sockaddr_storage
struct sockaddr_in *ipv4 = (struct sockaddr_in *)a;
nb = ((uint32_t)ipv4->sin_addr.s_addr) & RRL_V4_PREFIX;
}
if (blklen + sizeof(nb) > maxlen) return KNOT_ESPACE;
if (blklen + sizeof(nb) > maxlen) {
return KNOT_ESPACE;
}
memcpy(dst + blklen, (void*)&nb, sizeof(nb));
blklen += sizeof(nb);
......@@ -190,13 +193,16 @@ static int rrl_classify(char *dst, size_t maxlen, const struct sockaddr_storage
uint16_t *nlen = (uint16_t*)(dst + blklen);
blklen += sizeof(uint16_t);
int len = rrl_clsname(dst + blklen, maxlen - blklen, cls, p, z);
if (len < 0)
if (len < 0) {
return len;
}
*nlen = len;
blklen += len;
/* Seed. */
if (blklen + sizeof(seed) > maxlen) return KNOT_ESPACE;
if (blklen + sizeof(seed) > maxlen) {
return KNOT_ESPACE;
}
if (memcpy(dst + blklen, (void*)&seed, sizeof(seed)) == 0) {
blklen += sizeof(seed);
}
......@@ -303,7 +309,9 @@ rrl_table_t *rrl_create(size_t size)
const size_t tbl_len = sizeof(rrl_table_t) + size * sizeof(rrl_item_t);
rrl_table_t *t = malloc(tbl_len);
if (!t) return NULL;
if (!t) {
return NULL;
}
memset(t, 0, sizeof(rrl_table_t));
t->size = size;
rrl_reseed(t);
......@@ -313,7 +321,9 @@ rrl_table_t *rrl_create(size_t size)
uint32_t rrl_setrate(rrl_table_t *rrl, uint32_t rate)
{
if (!rrl) return 0;
if (!rrl) {
return 0;
}
uint32_t old = rrl->rate;
rrl->rate = rate;
return old;
......@@ -321,13 +331,15 @@ uint32_t rrl_setrate(rrl_table_t *rrl, uint32_t rate)
uint32_t rrl_rate(rrl_table_t *rrl)
{
if (!rrl) return 0;
return rrl->rate;
return rrl ? rrl->rate : 0;
}
int rrl_setlocks(rrl_table_t *rrl, unsigned granularity)
{
if (!rrl) return KNOT_EINVAL;
if (!rrl) {
return KNOT_EINVAL;
}
assert(!rrl->lk); /* Cannot change while locks are used. */
assert(granularity <= rrl->size / 10); /* Due to int. division err. */
......@@ -337,12 +349,16 @@ int rrl_setlocks(rrl_table_t *rrl, unsigned granularity)
/* Alloc new locks. */
rrl->lk = malloc(granularity * sizeof(pthread_mutex_t));
if (!rrl->lk) return KNOT_ENOMEM;
if (!rrl->lk) {
return KNOT_ENOMEM;
}
memset(rrl->lk, 0, granularity * sizeof(pthread_mutex_t));
/* Initialize. */
for (size_t i = 0; i < granularity; ++i) {
if (pthread_mutex_init(rrl->lk + i, NULL) < 0) break;
if (pthread_mutex_init(rrl->lk + i, NULL) < 0) {
break;
}
++rrl->lk_count;
}
......@@ -424,7 +440,9 @@ rrl_item_t* rrl_hash(rrl_table_t *t, const struct sockaddr_storage *a, rrl_req_t
int rrl_query(rrl_table_t *rrl, const struct sockaddr_storage *a, rrl_req_t *req,
const zone_t *zone)
{
if (!rrl || !req || !a) return KNOT_EINVAL;
if (!rrl || !req || !a) {
return KNOT_EINVAL;
}
/* Calculate hash and fetch */
int ret = KNOT_EOK;
......@@ -432,7 +450,9 @@ int rrl_query(rrl_table_t *rrl, const struct sockaddr_storage *a, rrl_req_t *req
uint32_t now = time(NULL);
rrl_item_t *b = rrl_hash(rrl, a, req, zone, now, &lock);
if (!b) {
if (lock > -1) rrl_unlock(rrl, lock);
if (lock > -1) {
rrl_unlock(rrl, lock);
}
return KNOT_ERROR;
}
......@@ -475,7 +495,9 @@ int rrl_query(rrl_table_t *rrl, const struct sockaddr_storage *a, rrl_req_t *req
ret = KNOT_ELIMIT;
}
if (lock > -1) rrl_unlock(rrl, lock);
if (lock > -1) {
rrl_unlock(rrl, lock);
}
return ret;
}
......@@ -492,7 +514,9 @@ bool rrl_slip_roll(int n_slip)
int rrl_destroy(rrl_table_t *rrl)
{
if (rrl) {
if (rrl->lk_count > 0) pthread_mutex_destroy(&rrl->ll);
if (rrl->lk_count > 0) {
pthread_mutex_destroy(&rrl->ll);
}
for (size_t i = 0; i < rrl->lk_count; ++i) {
pthread_mutex_destroy(rrl->lk + i);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment