Ver Fonte

- generic fix_param fixup function, the function produces fparam_t structure,
which can contain parsed integer, str string, AVP, regex, or asciiz character.
The function gets the original string parameter as input

Jan Janak há 19 anos atrás
pai
commit
b91364c6ce
2 ficheiros alterados com 119 adições e 0 exclusões
  1. 87 0
      sr_module.c
  2. 32 0
      sr_module.h

+ 87 - 0
sr_module.c

@@ -47,6 +47,7 @@
 #include "ut.h"
 #include "route_struct.h"
 #include "flags.h"
+#include "trim.h"
 
 #include <regex.h>
 #include <dlfcn.h>
@@ -722,3 +723,89 @@ int fix_flag( modparam_t type, void* val,
 	*flag=num;
 	return 0;
 }
+
+
+/*
+ * Generic parameter fixup function which creates
+ * fparam_t structure. type parameter contains allowed
+ * parameter types
+ */
+int fix_param(int type, void** param)
+{	
+	fparam_t* p;
+	str name;
+	unsigned long num;
+	int err;
+
+	p = (fparam_t*)pkg_malloc(sizeof(fparam_t));
+	if (!p) {
+		ERR("fix_param: No memory left\n");
+		return E_OUT_OF_MEM;
+	}
+	memset(p, 0, sizeof(fparam_t));
+	p->orig = *param;
+
+	switch(type) {
+	case FPARAM_UNSPEC:
+		ERR("fix_param: Invalid type value\n");
+		goto error;
+
+	case FPARAM_ASCIIZ:
+		p->v.asciiz = *param;
+		break;
+
+	case FPARAM_STR:
+		p->v.str.s = (char*)*param;
+		p->v.str.len = strlen(p->v.str.s);
+		break;
+
+	case FPARAM_INT:
+		num = str2s(*param, strlen(*param), &err);
+		if (err == 0) {
+			p->v.i = num;
+		} else {
+			ERR("Bad number <%s>\n",
+			    (char*)(*param));
+			goto error;
+		}
+		break;
+
+	case FPARAM_REGEX:
+		if ((p->v.regex = pkg_malloc(sizeof(regex_t))) == 0) {
+			ERR("No memory left\n");
+			goto error;
+		}
+		if (regcomp(p->v.regex, *param, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
+			pkg_free(p->v.regex);
+			ERR("Bad regular expression '%s'\n", (char*)*param);
+		        goto error;
+		}
+		break;
+
+	case FPARAM_AVP:
+		name.s = (char*)*param;
+		name.len = strlen(name.s);
+		trim(&name);
+		if (!name.len || name.s[0] != '$') {
+			     /* Not an AVP identifier */
+			pkg_free(p);
+			return 1;
+		}
+		name.s++;
+		name.len--;
+		
+		if (parse_avp_ident(&name, &p->v.avp) < 0) {
+			ERR("Error while parsing attribute name\n");
+			goto error;
+		}
+		break;
+	}
+
+	p->type = type;
+	*param = (void*)p;
+	return 0;
+
+ error:
+	pkg_free(p);
+	return E_UNSPEC;
+}

+ 32 - 0
sr_module.h

@@ -110,6 +110,31 @@ struct param_export_ {
 };
 
 
+enum {
+	FPARAM_UNSPEC = 0,
+	FPARAM_ASCIIZ = (1 << 0),
+	FPARAM_STR    = (1 << 1),
+	FPARAM_INT    = (1 << 2),
+	FPARAM_REGEX  = (1 << 3),
+	FPARAM_AVP    = (1 << 5),
+};
+
+/*
+ * Function parameter
+ */
+typedef struct fparam {
+        char* orig;                /* The original value */
+        int type;                  /* Type of parameter */
+        union {
+		char* asciiz;      /* Zero terminated ASCII string */
+		str str;           /* pointer/len string */
+		int i;             /* Integer value */
+		regex_t* regex;    /* Compiled regular expression */
+		avp_ident_t avp;   /* AVP identifier */
+	} v;
+} fparam_t;
+
+
 typedef struct cmd_export_ cmd_export_t;
 typedef struct param_export_ param_export_t;
 
@@ -203,6 +228,13 @@ int fixup_regex_1(void** param, int param_no);
 /* Compile regular expression in second parameter */
 int fixup_regex_2(void** param, int param_no);
 
+/*
+ * Generic parameter fixup function which creates
+ * fparam_t structure. type parameter contains allowed
+ * parameter types
+ */
+int fix_param(int type, void** param);
+
 /* API function to get other parameters from fixup */
 action_u_t *fixup_get_param(void **cur_param, int cur_param_no, int required_param_no);
 int fixup_get_param_count(void **cur_param, int cur_param_no);