Skip to content

Commit

Permalink
Added pike mi command for unblocking IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrod Baumann committed Apr 8, 2015
1 parent 8627946 commit d506309
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 2 additions & 0 deletions modules/pike/pike.c
Expand Up @@ -93,6 +93,8 @@ static param_export_t params[]={
static mi_export_t mi_cmds [] = {
{MI_PIKE_LIST, "lists the nodes in the pike tree",
mi_pike_list, MI_NO_INPUT_FLAG, 0, 0 },
{MI_PIKE_RM, "remove a node from the tree",
mi_pike_rm, 0, 0, 0 },
{0,0,0,0,0,0}
};

Expand Down
78 changes: 77 additions & 1 deletion modules/pike/pike_mi.c
Expand Up @@ -24,6 +24,11 @@
* 2006-12-05 created (bogdan)
*/

#include <assert.h>

#include "../../resolve.h"
#include "../../timer.h"

#include "ip_tree.h"
#include "pike_mi.h"

Expand All @@ -32,7 +37,11 @@
#define MAX_IP_LEN IPv6_LEN


static struct ip_node *ip_stack[MAX_IP_LEN];
static struct ip_node *ip_stack[MAX_IP_LEN];
extern int pike_log_level;
extern int timeout;
extern struct list_link* timer;
extern gen_lock_t* timer_lock;


static inline void print_ip_stack( int level, struct mi_node *node)
Expand Down Expand Up @@ -86,6 +95,73 @@ static void print_red_ips( struct ip_node *ip, int level, struct mi_node *node)

}

struct mi_root* mi_pike_rm(struct mi_root *cmd, void *param)
{
struct mi_node *mn;
struct ip_node *node;
struct ip_node *dad;
struct ip_node *kid;
struct ip_addr *ip;
int byte_pos;

mn = cmd->node.kids;
if (mn==NULL)
return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);

ip = str2ip(&mn->value);
if (ip==0)
return init_mi_tree( 500, "Bad IP", 6);

node = 0;
byte_pos = 0;

kid = get_tree_branch((unsigned char)ip->u.addr[byte_pos]);

/* pilfered from ip_tree.c:mark_node(..) */
while (kid && byte_pos < ip->len) {
while (kid && kid->byte!=(unsigned char)ip->u.addr[byte_pos]) {
kid = kid->next;
}
if (kid) {
node = kid;
kid = kid->kids;
byte_pos++;
}
}

/* If all octets weren't matched, 404 */
if (byte_pos!=ip->len) {
return init_mi_tree( 404, "Match not found", 15);
}

/* If the node exists, check to see if it's really blocked */
if (!(node->flags&NODE_ISRED_FLAG)) {
return init_mi_tree( 400, "IP not blocked", 14);
}

/* pilfered from pike_funcs.c:clean_routine(..) */
if (node->prev!=0) {
if (node->prev->kids==node && node->next==0) {
dad = node->prev;
if ( !(dad->flags&NODE_IPLEAF_FLAG) ) {
lock_get(timer_lock);
dad->expires = get_ticks() + timeout;
assert( !has_timer_set(&(dad->timer_ll)) );
append_to_timer( timer, &(dad->timer_ll));
dad->flags |= NODE_INTIMER_FLAG;
lock_release(timer_lock);
} else {
assert( has_timer_set(&(dad->timer_ll)) );
}
}
}
remove_node(node);

LM_GEN1(pike_log_level,
"PIKE - UNBLOCKing ip %s, node=%p\n",ip_addr2a(ip),node);

return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
}


/*
Expand Down
2 changes: 2 additions & 0 deletions modules/pike/pike_mi.h
Expand Up @@ -31,8 +31,10 @@
#include "../../mi/mi.h"

#define MI_PIKE_LIST "pike_list"
#define MI_PIKE_RM "pike_rm"

struct mi_root* mi_pike_list(struct mi_root* cmd_tree, void* param);
struct mi_root* mi_pike_rm(struct mi_root *cmd, void *param);

#endif

Expand Down

0 comments on commit d506309

Please sign in to comment.