Browse Source

secfilter: simplify RPC code

Victor Seva 6 years ago
parent
commit
40468c0fb4
1 changed files with 44 additions and 44 deletions
  1. 44 44
      src/modules/secfilter/secfilter_rpc.c

+ 44 - 44
src/modules/secfilter/secfilter_rpc.c

@@ -65,81 +65,81 @@ void rpc_add_dst(rpc_t *rpc, void *ctx)
 				"Invalid Parameters. Usage: secfilter.add_dst "
 				"number\n     Example: secfilter.add_dst "
 				"555123123");
+		return;
+	}
+	text = int2str(number, &data.len);
+	data.s = pkg_malloc(data.len * sizeof(char));
+	if(!data.s) {
+		PKG_MEM_ERROR;
+		rpc->rpl_printf(ctx, "Error insert values in the blacklist");
+		return;
+	}
+	memcpy(data.s, text, data.len);
+	lock_get(&secf_data->lock);
+	if(append_rule(2, 0, &data) == 0) {
+		rpc->rpl_printf(ctx,
+				"Values (%s) inserted into blacklist destinations", data);
 	} else {
-		text = int2str(number, &data.len);
-		data.s = pkg_malloc(data.len * sizeof(char));
-		if(!data.s) {
-			PKG_MEM_ERROR;
-			rpc->rpl_printf(ctx, "Error insert values in the blacklist");
-			return;
-		}
-		memcpy(data.s, text, data.len);
-		lock_get(&secf_data->lock);
-		if(append_rule(2, 0, &data) == 0) {
-			rpc->rpl_printf(ctx,
-					"Values (%s) inserted into blacklist destinations", data);
-		} else {
-			rpc->rpl_printf(ctx, "Error insert values in the blacklist");
-		}
-		lock_release(&secf_data->lock);
-		if(data.s)
-			pkg_free(data.s);
+		rpc->rpl_printf(ctx, "Error insert values in the blacklist");
 	}
+	lock_release(&secf_data->lock);
+	if(data.s)
+		pkg_free(data.s);
 }
 
 /* Add blacklist value */
 void rpc_add_bl(rpc_t *rpc, void *ctx)
 {
-	char *ctype;
+	char *ctype = NULL;
 	str data = STR_NULL;
 	int type;
 
-	if(rpc->scan(ctx, "ss", (char *)(&ctype), (char *)(&data.s)) < 2) {
+	if(rpc->scan(ctx, "ss", ctype, &data.s) < 2) {
 		rpc->fault(ctx, 0,
 				"Invalid Parameters. Usage: secfilter.add_bl type "
 				"value\n     Example: secfilter.add_bl user "
 				"sipvicious");
+		return;
+	}
+	data.len = strlen(data.s);
+	type = get_type(ctype);
+
+	lock_get(&secf_data->lock);
+	if(append_rule(0, type, &data) == 0) {
+		rpc->rpl_printf(ctx, "Values (%s, %s) inserted into blacklist",
+				ctype, data);
 	} else {
-		data.len = strlen(data.s);
-		type = get_type(ctype);
-
-		lock_get(&secf_data->lock);
-		if(append_rule(0, type, &data) == 0) {
-			rpc->rpl_printf(ctx, "Values (%s, %s) inserted into blacklist",
-					ctype, data);
-		} else {
-			rpc->rpl_printf(ctx, "Error insert values in the blacklist");
-		}
-		lock_release(&secf_data->lock);
+		rpc->rpl_printf(ctx, "Error insert values in the blacklist");
 	}
+	lock_release(&secf_data->lock);
 }
 
 
 /* Add whitelist value */
 void rpc_add_wl(rpc_t *rpc, void *ctx)
 {
-	char *ctype;
+	char *ctype = NULL;
 	str data = STR_NULL;
 	int type;
 
-	if(rpc->scan(ctx, "ss", (char *)(&ctype), (char *)(&data.s)) < 2) {
+	if(rpc->scan(ctx, "ss", ctype, &data.s) < 2) {
 		rpc->fault(ctx, 0,
 				"Invalid Parameters. Usage: secfilter.add_wl type "
 				"value\n     Example: secfilter.add_wl user "
 				"trusted_user");
+		return;
+	}
+	data.len = strlen(data.s);
+	type = get_type(ctype);
+
+	lock_get(&secf_data->lock);
+	if(append_rule(1, type, &data) == 0) {
+		rpc->rpl_printf(
+				ctx, "Values (%s, %s) inserted into whitelist", type, data);
 	} else {
-		data.len = strlen(data.s);
-		type = get_type(ctype);
-
-		lock_get(&secf_data->lock);
-		if(append_rule(1, type, &data) == 0) {
-			rpc->rpl_printf(
-					ctx, "Values (%s, %s) inserted into whitelist", type, data);
-		} else {
-			rpc->rpl_printf(ctx, "Error insert values in the whitelist");
-		}
-		lock_release(&secf_data->lock);
+		rpc->rpl_printf(ctx, "Error insert values in the whitelist");
 	}
+	lock_release(&secf_data->lock);
 }