Skip to content

Commit f09cd45

Browse files
committed
blacklists code: Improve coding style
* add space between: * statement and paranthesis * operands and operators * proper use of "NULL" vs. "0" * proper way to perform NULL-checking: * for function results * for all pointers
1 parent 2e7588b commit f09cd45

File tree

5 files changed

+83
-94
lines changed

5 files changed

+83
-94
lines changed

Diff for: blacklists.c

+73-84
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static unsigned int used_heads;
4848

4949
static int bl_ctx_idx = -1;
5050

51-
static void delete_expired_routine(unsigned int ticks, void* param);
52-
static struct mi_root* mi_print_blacklists(struct mi_root *cmd, void *param);
51+
static void delete_expired_routine(unsigned int ticks, void *param);
52+
static struct mi_root *mi_print_blacklists(struct mi_root *cmd, void *param);
5353

5454

5555
static mi_export_t mi_bl_cmds[] = {
@@ -65,14 +65,14 @@ int init_black_lists(void)
6565
return -1;
6666

6767
/* register timer routine */
68-
if ( register_timer( "blcore-expire", delete_expired_routine, 0, 1,
69-
TIMER_FLAG_SKIP_ON_DELAY)<0 ) {
68+
if (register_timer("blcore-expire", delete_expired_routine, 0, 1,
69+
TIMER_FLAG_SKIP_ON_DELAY) < 0) {
7070
LM_ERR("failed to register timer\n");
7171
return -1;
7272
}
7373

7474
/* register MI commands */
75-
if (register_mi_mod( "blacklists", mi_bl_cmds)<0) {
75+
if (register_mi_mod("blacklists", mi_bl_cmds) < 0) {
7676
LM_ERR("unable to register MI cmds\n");
7777
return -1;
7878
}
@@ -105,48 +105,48 @@ struct bl_head *create_bl_head(int owner, int flags, struct bl_rule *head,
105105
{
106106
unsigned int i;
107107

108-
if(!blst_heads) {
109-
blst_heads = (struct bl_head*)shm_malloc(max_heads*sizeof(struct bl_head));
110-
if (blst_heads==NULL) {
108+
if (!blst_heads) {
109+
blst_heads = shm_malloc(max_heads * sizeof *blst_heads);
110+
if (!blst_heads) {
111111
LM_ERR("no more shared memory!\n");
112112
return NULL;
113113
}
114-
memset( blst_heads, 0, max_heads*sizeof(struct bl_head));
114+
memset(blst_heads, 0, max_heads * sizeof *blst_heads);
115115
}
116116
i = used_heads;
117-
if (i==max_heads) {
117+
if (i == max_heads) {
118118
LM_ERR("too many lists\n");
119119
return NULL;
120120
}
121121

122-
if (get_bl_head_by_name(name)!=NULL) {
122+
if (get_bl_head_by_name(name)) {
123123
LM_CRIT("duplicated name!\n");
124124
return NULL;
125125
}
126126

127-
if ( flags&BL_READONLY_LIST && flags&BL_DO_EXPIRE){
127+
if (flags & BL_READONLY_LIST && flags & BL_DO_EXPIRE) {
128128
LM_CRIT("RO lists cannot accept EXPIRES!\n");
129129
return NULL;
130130
}
131131

132132
/* copy list name */
133-
blst_heads[i].name.s = (char*)shm_malloc(name->len + 1);
134-
if (blst_heads[i].name.s==NULL) {
133+
blst_heads[i].name.s = shm_malloc(name->len + 1);
134+
if (!blst_heads[i].name.s) {
135135
LM_ERR("no more shm memory!\n");
136136
return NULL;
137137
}
138-
memcpy( blst_heads[i].name.s, name->s, name->len);
138+
memcpy(blst_heads[i].name.s, name->s, name->len);
139139
blst_heads[i].name.s[name->len] = '\0';
140140
blst_heads[i].name.len = name->len;
141141

142142
/* build lock? */
143-
if (!(flags&BL_READONLY_LIST)) {
144-
if ( (blst_heads[i].lock=lock_alloc())==NULL ) {
143+
if (!(flags & BL_READONLY_LIST)) {
144+
if (!(blst_heads[i].lock = lock_alloc())) {
145145
LM_ERR("failed to create lock!\n");
146146
shm_free(blst_heads[i].name.s);
147147
return NULL;
148148
}
149-
if ( lock_init(blst_heads[i].lock)==NULL ) {
149+
if (!lock_init(blst_heads[i].lock)) {
150150
LM_ERR("failed to init lock!\n");
151151
shm_free(blst_heads[i].name.s);
152152
lock_dealloc(blst_heads[i].lock);
@@ -161,8 +161,8 @@ struct bl_head *create_bl_head(int owner, int flags, struct bl_rule *head,
161161
blst_heads[i].first = head;
162162
blst_heads[i].last = tail;
163163

164-
if (flags&BL_BY_DEFAULT)
165-
bl_default_marker |= (1<<i);
164+
if (flags & BL_BY_DEFAULT)
165+
bl_default_marker |= (1 << i);
166166

167167
return blst_heads + i;
168168
}
@@ -174,14 +174,13 @@ void destroy_black_lists(void)
174174
unsigned int i;
175175
struct bl_rule *p, *q;
176176

177-
for(i = 0 ; i < used_heads ; i++){
178-
177+
for (i = 0; i < used_heads; i++) {
179178
if (blst_heads[i].lock) {
180179
lock_destroy(blst_heads[i].lock);
181180
lock_dealloc(blst_heads[i].lock);
182181
}
183182

184-
for( p=blst_heads[i].first ; p ; ) {
183+
for (p = blst_heads[i].first; p; ) {
185184
q = p;
186185
p = p->next;
187186
shm_free(q);
@@ -193,7 +192,7 @@ void destroy_black_lists(void)
193192
blst_heads[i].first = blst_heads[i].last = NULL;
194193
}
195194

196-
if(blst_heads)
195+
if (blst_heads)
197196
shm_free(blst_heads);
198197
}
199198

@@ -207,64 +206,60 @@ static inline void delete_expired(struct bl_head *elem, unsigned int ticks)
207206

208207
/* get list for write */
209208
lock_get(elem->lock);
210-
while(elem->count_write){
209+
while (elem->count_write){
211210
lock_release(elem->lock);
212211
sleep_us(5);
213212
lock_get(elem->lock);
214213
}
215214
elem->count_write = 1;
216-
while(elem->count_read){
215+
216+
while (elem->count_read){
217217
lock_release(elem->lock);
218218
sleep_us(5);
219219
lock_get(elem->lock);
220220
}
221221
lock_release(elem->lock);
222222

223-
if(elem->first==NULL)
223+
if (!elem->first)
224224
goto done;
225225

226-
for( q=0,p = elem->first ; p ; q=p,p=p->next) {
227-
if(p->expire_end > ticks)
226+
for (q = 0, p = elem->first; p; q = p, p = p->next)
227+
if (p->expire_end > ticks)
228228
break;
229-
}
230229

231-
if (q==NULL)
232-
/* nothing to remove */
233-
goto done;
230+
if (!q)
231+
goto done; /* nothing to remove */
234232

235-
if (p==NULL) {
233+
if (!p) {
236234
/* remove everything */
237235
q = elem->first;
238236
elem->first = elem->last = NULL;
239237
} else {
240238
/* remove up to p */
241-
q->next = 0;
239+
q->next = NULL;
242240
q = elem->first;
243241
elem->first = p;
244242
}
245243

246244
done:
247245
elem->count_write = 0;
248246

249-
for( ; q ; ){
247+
for (; q; ) {
250248
p = q;
251249
q = q->next;
252250
shm_free(p);
253251
}
254-
255-
return;
256252
}
257253

258254

259255

260-
static void delete_expired_routine(unsigned int ticks, void* param)
256+
static void delete_expired_routine(unsigned int ticks, void *param)
261257
{
262258
unsigned int i;
263259

264-
for(i = 0 ; i < used_heads ; i++){
265-
if( blst_heads[i].flags&BL_DO_EXPIRE && blst_heads[i].first)
260+
for (i = 0 ; i < used_heads ; i++)
261+
if (blst_heads[i].flags&BL_DO_EXPIRE && blst_heads[i].first)
266262
delete_expired(blst_heads + i, ticks);
267-
}
268263
}
269264

270265

@@ -274,15 +269,14 @@ static inline int ip_class_compare(struct net *net1, struct net *net2)
274269
unsigned int r;
275270

276271
if (net1->ip.af == net2->ip.af){
277-
for(r=0; r<net1->ip.len/4; r++){ /* ipv4 & ipv6 addresses are
278-
all multiples of 4*/
272+
/* ipv4 & ipv6 addresses are all multiples of 4 */
273+
for(r=0; r<net1->ip.len/4; r++)
279274
if ((net1->ip.u.addr32[r]&net1->mask.u.addr32[r])!=
280-
(net2->ip.u.addr32[r]&net2->mask.u.addr32[r]) ){
275+
(net2->ip.u.addr32[r]&net2->mask.u.addr32[r]))
281276
return 0;
282-
}
283-
}
284277
return 1;
285-
};
278+
}
279+
286280
return -1;
287281
}
288282

@@ -304,7 +298,7 @@ int add_rule_to_list(struct bl_rule **first, struct bl_rule **last,
304298
body = 0;
305299

306300
/* is it a duplicate? */
307-
for(q = *first ; q ; q = q->next) {
301+
for (q = *first; q; q = q->next) {
308302
if ( (flags==q->flags) && (port==q->port) &&
309303
(proto==q->proto) &&
310304
(ip_class_compare(ip_net, &q->ip_net)==1) &&
@@ -316,11 +310,9 @@ int add_rule_to_list(struct bl_rule **first, struct bl_rule **last,
316310
}
317311
}
318312

319-
320313
/* alloc memory */
321-
p = (struct bl_rule*)shm_malloc
322-
(sizeof(struct bl_rule) + (body?(body->len + 1):0));
323-
if(!p){
314+
p = shm_malloc(sizeof *p + (body?(body->len + 1):0));
315+
if (!p) {
324316
LM_ERR("no more shm memory!\n");
325317
return -1;
326318
}
@@ -331,9 +323,9 @@ int add_rule_to_list(struct bl_rule **first, struct bl_rule **last,
331323
p->proto = proto;
332324
p->port = port;
333325
if (body) {
334-
p->body.s = (char *)p + sizeof(struct bl_rule);
326+
p->body.s = (char *)(p + 1);
335327
memcpy(p->body.s, body->s, body->len);
336-
(p->body.s)[body->len] = '\0';
328+
p->body.s[body->len] = '\0';
337329
p->body.len = body->len;
338330
} else {
339331
p->body.s = NULL;
@@ -432,34 +424,34 @@ static inline int reload_permanent_list(struct bl_rule *first,
432424

433425

434426
/* should NOT add ANY DUPLICATES */
435-
int add_list_to_head( struct bl_head *head,
427+
int add_list_to_head(struct bl_head *head,
436428
struct bl_rule *first, struct bl_rule *last,
437429
int truncate, int expire_limit)
438430
{
439431
struct bl_rule *p;
440-
unsigned int expire_end=0;
432+
unsigned int expire_end = 0;
441433

442434
if (!head || !first || !last)
443435
return -1;
444436

445437
/* may I add to this list? */
446-
if (head->flags&BL_READONLY_LIST) {
438+
if (head->flags & BL_READONLY_LIST) {
447439
LM_CRIT("list is readonly!!!\n");
448440
return -1;
449441
}
450442

451443
LM_DBG("adding to bl %.*s %p,%p\n",
452-
head->name.len, head->name.s, first,last);
444+
head->name.len, head->name.s, first, last);
453445

454446
/* for expiring lists, sets the timeout */
455-
if (head->flags&BL_DO_EXPIRE) {
447+
if (head->flags & BL_DO_EXPIRE) {
456448
if (expire_limit==0) {
457449
LM_CRIT("expire is zero!!!\n");
458450
return -1;
459451
}
460452
expire_end = get_ticks() + expire_limit;
461453

462-
for(p = first ; p ; p = p->next)
454+
for (p = first; p; p = p->next)
463455
p->expire_end = expire_end;
464456
}
465457

@@ -469,41 +461,39 @@ int add_list_to_head( struct bl_head *head,
469461

470462
/* get list for write */
471463
lock_get(head->lock);
472-
while(head->count_write){
473-
lock_release( head->lock );
464+
while (head->count_write){
465+
lock_release(head->lock);
474466
sleep_us(5);
475-
lock_get( head->lock );
467+
lock_get(head->lock);
476468
}
477469
head->count_write = 1;
478-
while(head->count_read){
479-
lock_release( head->lock );
470+
471+
while (head->count_read){
472+
lock_release(head->lock);
480473
sleep_us(5);
481-
lock_get( head->lock );
474+
lock_get(head->lock);
482475
}
483-
lock_release( head->lock );
476+
lock_release(head->lock);
484477

485-
rm_dups( head, &first, &last);
486-
if (first==NULL)
478+
rm_dups(head, &first, &last);
479+
if (!first)
487480
goto done;
488481

489-
if (head->first==NULL) {
482+
if (!head->first) {
490483
head->last = last;
491484
head->first = first;
492-
} else
493-
if ( !(head->flags&BL_DO_EXPIRE) ) {
485+
} else if (!(head->flags & BL_DO_EXPIRE)) {
494486
head->last->next = first;
495487
head->last = last;
496-
} else
497-
if( head->first->expire_end >= expire_end){
488+
} else if (head->first->expire_end >= expire_end) {
498489
last->next = head->first;
499490
head->first = first;
500-
} else
501-
if(head->last->expire_end <= expire_end){
491+
} else if (head->last->expire_end <= expire_end) {
502492
head->last->next = first;
503493
head->last = last;
504494
} else {
505-
for(p = head->first ; ; p = p->next)
506-
if( p->next->expire_end >= expire_end)
495+
for (p = head->first; ; p = p->next)
496+
if (p->next->expire_end >= expire_end)
507497
break;
508498
last->next = p->next;
509499
p->next = first;
@@ -521,18 +511,17 @@ struct bl_head *get_bl_head_by_name(str *name)
521511
{
522512
unsigned int i;
523513

524-
for(i = 0 ; i < used_heads ; i++){
514+
for (i = 0; i < used_heads; i++)
525515
if ((name->len == blst_heads[i].name.len) &&
526-
!strncmp(name->s, blst_heads[i].name.s, name->len))
527-
return (blst_heads + i);
528-
}
516+
!strncmp(name->s, blst_heads[i].name.s, name->len))
517+
return blst_heads + i;
529518

530519
return NULL;
531520
}
532521

533522

534523

535-
int mark_for_search(struct bl_head *list, int unsigned set)
524+
int mark_for_search(struct bl_head *list, unsigned int set)
536525
{
537526
unsigned int n;
538527
unsigned int bl_marker;

Diff for: blacklists.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ static inline int check_blacklists( unsigned short proto,
9696

9797
body.s = body_s;
9898
body.len = body_len;
99-
su2ip_addr( &ip, to);
100-
port = su_getport( to );
101-
return check_against_blacklist( &ip, &body, port, proto);
99+
su2ip_addr(&ip, to);
100+
port = su_getport(to);
101+
return check_against_blacklist(&ip, &body, port, proto);
102102
}
103103

104104
#endif /* _BLACKLST_H */

0 commit comments

Comments
 (0)