@@ -48,8 +48,8 @@ static unsigned int used_heads;
48
48
49
49
static int bl_ctx_idx = -1 ;
50
50
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 );
53
53
54
54
55
55
static mi_export_t mi_bl_cmds [] = {
@@ -65,14 +65,14 @@ int init_black_lists(void)
65
65
return -1 ;
66
66
67
67
/* 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 ) {
70
70
LM_ERR ("failed to register timer\n" );
71
71
return -1 ;
72
72
}
73
73
74
74
/* register MI commands */
75
- if (register_mi_mod ( "blacklists" , mi_bl_cmds )< 0 ) {
75
+ if (register_mi_mod ("blacklists" , mi_bl_cmds ) < 0 ) {
76
76
LM_ERR ("unable to register MI cmds\n" );
77
77
return -1 ;
78
78
}
@@ -105,48 +105,48 @@ struct bl_head *create_bl_head(int owner, int flags, struct bl_rule *head,
105
105
{
106
106
unsigned int i ;
107
107
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 ) {
111
111
LM_ERR ("no more shared memory!\n" );
112
112
return NULL ;
113
113
}
114
- memset ( blst_heads , 0 , max_heads * sizeof ( struct bl_head ) );
114
+ memset (blst_heads , 0 , max_heads * sizeof * blst_heads );
115
115
}
116
116
i = used_heads ;
117
- if (i == max_heads ) {
117
+ if (i == max_heads ) {
118
118
LM_ERR ("too many lists\n" );
119
119
return NULL ;
120
120
}
121
121
122
- if (get_bl_head_by_name (name )!= NULL ) {
122
+ if (get_bl_head_by_name (name )) {
123
123
LM_CRIT ("duplicated name!\n" );
124
124
return NULL ;
125
125
}
126
126
127
- if ( flags & BL_READONLY_LIST && flags & BL_DO_EXPIRE ){
127
+ if (flags & BL_READONLY_LIST && flags & BL_DO_EXPIRE ) {
128
128
LM_CRIT ("RO lists cannot accept EXPIRES!\n" );
129
129
return NULL ;
130
130
}
131
131
132
132
/* 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 ) {
135
135
LM_ERR ("no more shm memory!\n" );
136
136
return NULL ;
137
137
}
138
- memcpy ( blst_heads [i ].name .s , name -> s , name -> len );
138
+ memcpy (blst_heads [i ].name .s , name -> s , name -> len );
139
139
blst_heads [i ].name .s [name -> len ] = '\0' ;
140
140
blst_heads [i ].name .len = name -> len ;
141
141
142
142
/* 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 ())) {
145
145
LM_ERR ("failed to create lock!\n" );
146
146
shm_free (blst_heads [i ].name .s );
147
147
return NULL ;
148
148
}
149
- if ( lock_init (blst_heads [i ].lock )== NULL ) {
149
+ if (! lock_init (blst_heads [i ].lock )) {
150
150
LM_ERR ("failed to init lock!\n" );
151
151
shm_free (blst_heads [i ].name .s );
152
152
lock_dealloc (blst_heads [i ].lock );
@@ -161,8 +161,8 @@ struct bl_head *create_bl_head(int owner, int flags, struct bl_rule *head,
161
161
blst_heads [i ].first = head ;
162
162
blst_heads [i ].last = tail ;
163
163
164
- if (flags & BL_BY_DEFAULT )
165
- bl_default_marker |= (1 << i );
164
+ if (flags & BL_BY_DEFAULT )
165
+ bl_default_marker |= (1 << i );
166
166
167
167
return blst_heads + i ;
168
168
}
@@ -174,14 +174,13 @@ void destroy_black_lists(void)
174
174
unsigned int i ;
175
175
struct bl_rule * p , * q ;
176
176
177
- for (i = 0 ; i < used_heads ; i ++ ){
178
-
177
+ for (i = 0 ; i < used_heads ; i ++ ) {
179
178
if (blst_heads [i ].lock ) {
180
179
lock_destroy (blst_heads [i ].lock );
181
180
lock_dealloc (blst_heads [i ].lock );
182
181
}
183
182
184
- for ( p = blst_heads [i ].first ; p ; ) {
183
+ for ( p = blst_heads [i ].first ; p ; ) {
185
184
q = p ;
186
185
p = p -> next ;
187
186
shm_free (q );
@@ -193,7 +192,7 @@ void destroy_black_lists(void)
193
192
blst_heads [i ].first = blst_heads [i ].last = NULL ;
194
193
}
195
194
196
- if (blst_heads )
195
+ if (blst_heads )
197
196
shm_free (blst_heads );
198
197
}
199
198
@@ -207,64 +206,60 @@ static inline void delete_expired(struct bl_head *elem, unsigned int ticks)
207
206
208
207
/* get list for write */
209
208
lock_get (elem -> lock );
210
- while (elem -> count_write ){
209
+ while (elem -> count_write ){
211
210
lock_release (elem -> lock );
212
211
sleep_us (5 );
213
212
lock_get (elem -> lock );
214
213
}
215
214
elem -> count_write = 1 ;
216
- while (elem -> count_read ){
215
+
216
+ while (elem -> count_read ){
217
217
lock_release (elem -> lock );
218
218
sleep_us (5 );
219
219
lock_get (elem -> lock );
220
220
}
221
221
lock_release (elem -> lock );
222
222
223
- if ( elem -> first == NULL )
223
+ if (! elem -> first )
224
224
goto done ;
225
225
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 )
228
228
break ;
229
- }
230
229
231
- if (q == NULL )
232
- /* nothing to remove */
233
- goto done ;
230
+ if (!q )
231
+ goto done ; /* nothing to remove */
234
232
235
- if (p == NULL ) {
233
+ if (! p ) {
236
234
/* remove everything */
237
235
q = elem -> first ;
238
236
elem -> first = elem -> last = NULL ;
239
237
} else {
240
238
/* remove up to p */
241
- q -> next = 0 ;
239
+ q -> next = NULL ;
242
240
q = elem -> first ;
243
241
elem -> first = p ;
244
242
}
245
243
246
244
done :
247
245
elem -> count_write = 0 ;
248
246
249
- for ( ; q ; ){
247
+ for ( ; q ; ) {
250
248
p = q ;
251
249
q = q -> next ;
252
250
shm_free (p );
253
251
}
254
-
255
- return ;
256
252
}
257
253
258
254
259
255
260
- static void delete_expired_routine (unsigned int ticks , void * param )
256
+ static void delete_expired_routine (unsigned int ticks , void * param )
261
257
{
262
258
unsigned int i ;
263
259
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 )
266
262
delete_expired (blst_heads + i , ticks );
267
- }
268
263
}
269
264
270
265
@@ -274,15 +269,14 @@ static inline int ip_class_compare(struct net *net1, struct net *net2)
274
269
unsigned int r ;
275
270
276
271
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 ++ )
279
274
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 ]))
281
276
return 0 ;
282
- }
283
- }
284
277
return 1 ;
285
- };
278
+ }
279
+
286
280
return -1 ;
287
281
}
288
282
@@ -304,7 +298,7 @@ int add_rule_to_list(struct bl_rule **first, struct bl_rule **last,
304
298
body = 0 ;
305
299
306
300
/* is it a duplicate? */
307
- for (q = * first ; q ; q = q -> next ) {
301
+ for (q = * first ; q ; q = q -> next ) {
308
302
if ( (flags == q -> flags ) && (port == q -> port ) &&
309
303
(proto == q -> proto ) &&
310
304
(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,
316
310
}
317
311
}
318
312
319
-
320
313
/* 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 ) {
324
316
LM_ERR ("no more shm memory!\n" );
325
317
return -1 ;
326
318
}
@@ -331,9 +323,9 @@ int add_rule_to_list(struct bl_rule **first, struct bl_rule **last,
331
323
p -> proto = proto ;
332
324
p -> port = port ;
333
325
if (body ) {
334
- p -> body .s = (char * )p + sizeof ( struct bl_rule );
326
+ p -> body .s = (char * )( p + 1 );
335
327
memcpy (p -> body .s , body -> s , body -> len );
336
- ( p -> body .s ) [body -> len ] = '\0' ;
328
+ p -> body .s [body -> len ] = '\0' ;
337
329
p -> body .len = body -> len ;
338
330
} else {
339
331
p -> body .s = NULL ;
@@ -432,34 +424,34 @@ static inline int reload_permanent_list(struct bl_rule *first,
432
424
433
425
434
426
/* 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 ,
436
428
struct bl_rule * first , struct bl_rule * last ,
437
429
int truncate , int expire_limit )
438
430
{
439
431
struct bl_rule * p ;
440
- unsigned int expire_end = 0 ;
432
+ unsigned int expire_end = 0 ;
441
433
442
434
if (!head || !first || !last )
443
435
return -1 ;
444
436
445
437
/* may I add to this list? */
446
- if (head -> flags & BL_READONLY_LIST ) {
438
+ if (head -> flags & BL_READONLY_LIST ) {
447
439
LM_CRIT ("list is readonly!!!\n" );
448
440
return -1 ;
449
441
}
450
442
451
443
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 );
453
445
454
446
/* for expiring lists, sets the timeout */
455
- if (head -> flags & BL_DO_EXPIRE ) {
447
+ if (head -> flags & BL_DO_EXPIRE ) {
456
448
if (expire_limit == 0 ) {
457
449
LM_CRIT ("expire is zero!!!\n" );
458
450
return -1 ;
459
451
}
460
452
expire_end = get_ticks () + expire_limit ;
461
453
462
- for (p = first ; p ; p = p -> next )
454
+ for (p = first ; p ; p = p -> next )
463
455
p -> expire_end = expire_end ;
464
456
}
465
457
@@ -469,41 +461,39 @@ int add_list_to_head( struct bl_head *head,
469
461
470
462
/* get list for write */
471
463
lock_get (head -> lock );
472
- while (head -> count_write ){
473
- lock_release ( head -> lock );
464
+ while (head -> count_write ){
465
+ lock_release (head -> lock );
474
466
sleep_us (5 );
475
- lock_get ( head -> lock );
467
+ lock_get (head -> lock );
476
468
}
477
469
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 );
480
473
sleep_us (5 );
481
- lock_get ( head -> lock );
474
+ lock_get (head -> lock );
482
475
}
483
- lock_release ( head -> lock );
476
+ lock_release (head -> lock );
484
477
485
- rm_dups ( head , & first , & last );
486
- if (first == NULL )
478
+ rm_dups (head , & first , & last );
479
+ if (! first )
487
480
goto done ;
488
481
489
- if (head -> first == NULL ) {
482
+ if (! head -> first ) {
490
483
head -> last = last ;
491
484
head -> first = first ;
492
- } else
493
- if ( !(head -> flags & BL_DO_EXPIRE ) ) {
485
+ } else if (!(head -> flags & BL_DO_EXPIRE )) {
494
486
head -> last -> next = first ;
495
487
head -> last = last ;
496
- } else
497
- if ( head -> first -> expire_end >= expire_end ){
488
+ } else if (head -> first -> expire_end >= expire_end ) {
498
489
last -> next = head -> first ;
499
490
head -> first = first ;
500
- } else
501
- if (head -> last -> expire_end <= expire_end ){
491
+ } else if (head -> last -> expire_end <= expire_end ) {
502
492
head -> last -> next = first ;
503
493
head -> last = last ;
504
494
} 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 )
507
497
break ;
508
498
last -> next = p -> next ;
509
499
p -> next = first ;
@@ -521,18 +511,17 @@ struct bl_head *get_bl_head_by_name(str *name)
521
511
{
522
512
unsigned int i ;
523
513
524
- for (i = 0 ; i < used_heads ; i ++ ){
514
+ for (i = 0 ; i < used_heads ; i ++ )
525
515
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 ;
529
518
530
519
return NULL ;
531
520
}
532
521
533
522
534
523
535
- int mark_for_search (struct bl_head * list , int unsigned set )
524
+ int mark_for_search (struct bl_head * list , unsigned int set )
536
525
{
537
526
unsigned int n ;
538
527
unsigned int bl_marker ;
0 commit comments