Browse code

AVPs' types purification usr_avp declares type for each part (avp_flags_t, avp_name_t, avp_value_t, avp_index_t) and one compund type avp_ident_t.

Rest of the source code needs to be updated to new types usage before we
would be able to smoothly change internal structure without breaking
anything else. (e.g. flags unsigned short -> unsigned long)

Michal Matyska authored on 08/01/2006 18:52:16
Showing 2 changed files
... ...
@@ -101,7 +101,7 @@ int init_avps(void)
101 101
 /*
102 102
  * Select active AVP list based on the value of flags
103 103
  */
104
-static avp_list_t* select_list(unsigned short flags)
104
+static avp_list_t* select_list(avp_flags_t flags)
105 105
 {
106 106
 	if (flags & AVP_CLASS_USER) {
107 107
 		if (flags & AVP_TRACK_TO) {
... ...
@@ -120,10 +120,10 @@ static avp_list_t* select_list(unsigned short flags)
120 120
 	return *crt_glist;
121 121
 }
122 122
 
123
-inline static unsigned short compute_ID( str *name )
123
+inline static avp_id_t compute_ID( str *name )
124 124
 {
125 125
 	char *p;
126
-	unsigned short id;
126
+	avp_id_t id;
127 127
 
128 128
 	id=0;
129 129
 	for( p=name->s+name->len-1 ; p>=name->s ; p-- )
... ...
@@ -132,7 +132,7 @@ inline static unsigned short compute_ID( str *name )
132 132
 }
133 133
 
134 134
 
135
-avp_t *create_avp (unsigned short flags, int_str name, int_str val)
135
+avp_t *create_avp (avp_flags_t flags, avp_name_t name, avp_value_t val)
136 136
 {
137 137
 	avp_t *avp;
138 138
 	str *s;
... ...
@@ -215,7 +215,7 @@ error:
215 215
 	return 0;
216 216
 }
217 217
 
218
-int add_avp_list(avp_list_t* list, unsigned short flags, int_str name, int_str val)
218
+int add_avp_list(avp_list_t* list, avp_flags_t flags, avp_name_t name, avp_value_t val)
219 219
 {
220 220
 	avp_t *avp;
221 221
 
... ...
@@ -231,9 +231,9 @@ int add_avp_list(avp_list_t* list, unsigned short flags, int_str name, int_str v
231 231
 }
232 232
 
233 233
 
234
-int add_avp(unsigned short flags, int_str name, int_str val)
234
+int add_avp(avp_flags_t flags, avp_name_t name, avp_value_t val)
235 235
 {
236
-	unsigned short avp_class;
236
+	avp_flags_t avp_class;
237 237
 	avp_list_t* list;
238 238
 
239 239
 	     /* Add avp to user class if no class has been
... ...
@@ -253,7 +253,7 @@ int add_avp(unsigned short flags, int_str name, int_str val)
253 253
 	return add_avp_list(list, flags & (~(AVP_CLASS_ALL) | avp_class), name, val);
254 254
 }
255 255
 
256
-int add_avp_before(avp_t *avp, unsigned short flags, int_str name, int_str val)
256
+int add_avp_before(avp_t *avp, avp_flags_t flags, avp_name_t name, avp_value_t val)
257 257
 {
258 258
 	avp_t *new_avp;
259 259
 	
... ...
@@ -300,7 +300,7 @@ inline str* get_avp_name(avp_t *avp)
300 300
 }
301 301
 
302 302
 
303
-inline void get_avp_val(avp_t *avp, int_str *val)
303
+inline void get_avp_val(avp_t *avp, avp_value_t *val)
304 304
 {
305 305
 	if (avp==0 || val==0)
306 306
 		return;
... ...
@@ -327,7 +327,7 @@ inline void get_avp_val(avp_t *avp, int_str *val)
327 327
 
328 328
 
329 329
 /* Return the current list of user attributes */
330
-avp_list_t get_avp_list(unsigned short flags)
330
+avp_list_t get_avp_list(avp_flags_t flags)
331 331
 {
332 332
 	return *select_list(flags);
333 333
 }
... ...
@@ -336,7 +336,7 @@ avp_list_t get_avp_list(unsigned short flags)
336 336
 /*
337 337
  * Compare given id with id in avp, return true if they match
338 338
  */
339
-static inline int match_by_id(avp_t* avp, unsigned short id)
339
+static inline int match_by_id(avp_t* avp, avp_id_t id)
340 340
 {
341 341
 	if (avp->id == id && (avp->flags&AVP_NAME_STR)==0) {
342 342
 		return 1;
... ...
@@ -348,7 +348,7 @@ static inline int match_by_id(avp_t* avp, unsigned short id)
348 348
 /*
349 349
  * Compare given name with name in avp, return true if they are same
350 350
  */
351
-static inline int match_by_name(avp_t* avp, unsigned short id, str* name)
351
+static inline int match_by_name(avp_t* avp, avp_id_t id, str* name)
352 352
 {
353 353
 	str* avp_name;
354 354
 	if (id==avp->id && avp->flags&AVP_NAME_STR &&
... ...
@@ -381,66 +381,71 @@ static inline int match_by_re(avp_t* avp, regex_t* re)
381 381
 }
382 382
 
383 383
 
384
-avp_t *search_first_avp(unsigned short flags, int_str name, int_str *val, struct search_state* s)
384
+avp_t *search_first_avp(avp_flags_t flags, avp_name_t name, avp_value_t *val, struct search_state* s)
385
+{
386
+	avp_ident_t id;
387
+	id.flags = flags;
388
+	id.name = name;
389
+	id.index = 0;
390
+	return search_avp (id, val, s);
391
+}
392
+
393
+avp_t *search_avp (avp_ident_t ident, avp_value_t* val, struct search_state* state)
385 394
 {
386 395
 	avp_t* ret;
387 396
 	static struct search_state st;
388 397
 	avp_list_t* list;
389 398
 
390
-	if (name.s.s==0 && name.s.len == 0) {
399
+	if (ident.name.s.s==0 && ident.name.s.len == 0) {
391 400
 		LOG(L_ERR,"ERROR:avp:search_first_avp: 0 ID or NULL NAME AVP!");
392 401
 		return 0;
393 402
 	}
394 403
 	
395
-	switch (flags & AVP_INDEX_ALL) {
404
+	switch (ident.flags & AVP_INDEX_ALL) {
396 405
 		case AVP_INDEX_BACKWARD:
397 406
 		case AVP_INDEX_FORWARD:
398 407
 			WARN("AVP specified with index, but not used for search\n");
399 408
 			break;
400 409
 	}
401 410
 
402
-	if (!s) s = &st;
411
+	if (!state) state = &st;
403 412
 
404
-	if ((flags & AVP_CLASS_ALL) == 0) {
413
+	if ((ident.flags & AVP_CLASS_ALL) == 0) {
405 414
 		     /* The caller did not specify any class to search in, so enable
406 415
 		      * all of them by default
407 416
 		      */
408
-		flags |= AVP_CLASS_ALL;
417
+		ident.flags |= AVP_CLASS_ALL;
409 418
 		
410
-		if ((flags & AVP_TRACK_ALL) == 0) {
419
+		if ((ident.flags & AVP_TRACK_ALL) == 0) {
411 420
 		    /* The caller did not specify even the track to search in, so try
412 421
 		     * track_from first, and if not found try track_to
413 422
 		     */
414
-		     	ret = search_first_avp(flags | AVP_TRACK_FROM, name, val, s);
415
-		     	if (ret) {
416
-		     		return ret;
417
-		     	}
418
-		     	flags |= AVP_TRACK_TO;
423
+		     	ident.flags |= AVP_TRACK_FROM;
424
+		     	if ((ret = search_avp(ident, val, state))) return ret;
425
+		     	ident.flags = (ident.flags & ~AVP_TRACK_ALL) | AVP_TRACK_TO;
419 426
 		}
420 427
 	}
421 428
 
422
-	list = select_list(flags);
429
+	list = select_list(ident.flags);
423 430
 
424
-	s->flags = flags;
425
-	s->avp = *list;
426
-	s->name = name;
431
+	state->flags = ident.flags;
432
+	state->avp = *list;
433
+	state->name = ident.name;
427 434
 
428
-	if (flags & AVP_NAME_STR) {
429
-		s->id = compute_ID(&name.s);
435
+	if (ident.flags & AVP_NAME_STR) {
436
+		state->id = compute_ID(&ident.name.s);
430 437
 	}
431 438
 
432
-        ret = search_next_avp(s, val);
439
+        ret = search_next_avp(state, val);
433 440
 
434 441
 	     /* Make sure that search next avp stays in the same class as the first
435 442
 	      * avp found
436 443
 	      */
437
-	if (s && ret) s->flags = (flags & ~AVP_CLASS_ALL) | (ret->flags & AVP_CLASS_ALL);
444
+	if (state && ret) state->flags = (ident.flags & ~AVP_CLASS_ALL) | (ret->flags & AVP_CLASS_ALL);
438 445
 	return ret;
439 446
 }
440 447
 
441
-
442
-
443
-avp_t *search_next_avp(struct search_state* s, int_str *val )
448
+avp_t *search_next_avp(struct search_state* s, avp_value_t *val )
444 449
 {
445 450
 	int matched;
446 451
 	avp_t* avp;
... ...
@@ -490,9 +495,9 @@ avp_t *search_next_avp(struct search_state* s, int_str *val )
490 490
 }
491 491
 
492 492
 int search_reverse( avp_t *cur, struct search_state* st,
493
-                     unsigned short index, avp_list_t *ret)
493
+                     avp_index_t index, avp_list_t *ret)
494 494
 {
495
-	unsigned short lvl;
495
+	avp_index_t lvl;
496 496
 	
497 497
 	if (!cur)
498 498
 		return 0;
... ...
@@ -502,8 +507,8 @@ int search_reverse( avp_t *cur, struct search_state* st,
502 502
 	return lvl;
503 503
 }
504 504
                             
505
-avp_t *search_avp_by_index( unsigned short flags, int_str name,
506
-                            int_str *val, unsigned short index) 	
505
+avp_t *search_avp_by_index( avp_flags_t flags, avp_name_t name,
506
+                            avp_value_t *val, avp_index_t index) 	
507 507
 {
508 508
 	avp_t *ret, *cur;
509 509
 	struct search_state st;
... ...
@@ -615,7 +620,7 @@ void reset_avps(void)
615 615
 }
616 616
 
617 617
 
618
-avp_list_t* set_avp_list( unsigned short flags, avp_list_t* list )
618
+avp_list_t* set_avp_list( avp_flags_t flags, avp_list_t* list )
619 619
 {
620 620
 	avp_list_t* prev;
621 621
 
... ...
@@ -761,6 +766,20 @@ int lookup_avp_galias(str *alias, int *type, int_str *avp_name)
761 761
 
762 762
 int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
763 763
 {
764
+	int ret;
765
+	avp_ident_t attr;
766
+	
767
+	ret=parse_avp_ident(name, &attr);
768
+	if (!ret) {
769
+		if (type) *type = attr.flags;
770
+		if (avp_name) *avp_name = attr.name;
771
+		if (index) *index = attr.index;
772
+	}
773
+	return ret;
774
+}
775
+
776
+int parse_avp_ident( str *name, avp_ident_t* attr)
777
+{
764 778
 	unsigned int id;
765 779
 	char c;
766 780
 	char *p;
... ...
@@ -771,7 +790,7 @@ int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
771 771
 		goto error;
772 772
 	}
773 773
 
774
-	if (index) *index = 0;
774
+	attr->index = 0;
775 775
 	DBG("Parsing '%.*s'\n", name->len, name->s);
776 776
 	if (name->len>=2 && name->s[1]==':') { // old fashion i: or s:
777 777
 		WARN("i: and s: avp name syntax is deprecated!\n");
... ...
@@ -782,17 +801,17 @@ int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
782 782
 			goto error;
783 783
 		switch (c) {
784 784
 			case 's': case 'S':
785
-				*type = AVP_NAME_STR;
786
-				avp_name->s = *name;
785
+				attr->flags = AVP_NAME_STR;
786
+				attr->name.s = *name;
787 787
 				break;
788 788
 			case 'i': case 'I':
789
-				*type = 0;
789
+				attr->flags = 0;
790 790
 				if (str2int( name, &id)!=0) {
791 791
 					ERR("invalid ID "
792 792
 						"<%.*s> - not a number\n", name->len, name->s);
793 793
 					goto error;
794 794
 				}
795
-				avp_name->n = (int)id;
795
+				attr->name.n = (int)id;
796 796
 				break;
797 797
 			default:
798 798
 				ERR("unsupported type "
... ...
@@ -818,19 +837,19 @@ int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
818 818
 		}
819 819
 		switch (id) {
820 820
 			case 'f':
821
-				*type = AVP_TRACK_FROM | AVP_CLASS_USER;
821
+				attr->flags = AVP_TRACK_FROM | AVP_CLASS_USER;
822 822
 				break;
823 823
 			case 't':
824
-				*type = AVP_TRACK_TO | AVP_CLASS_USER;
824
+				attr->flags = AVP_TRACK_TO | AVP_CLASS_USER;
825 825
 				break;
826 826
 			case 0x6664: //'fd'
827
-				*type = AVP_TRACK_FROM | AVP_CLASS_DOMAIN;
827
+				attr->flags = AVP_TRACK_FROM | AVP_CLASS_DOMAIN;
828 828
 				break;
829 829
 			case 0x7464: // 'td'
830
-				*type = AVP_TRACK_TO | AVP_CLASS_DOMAIN;
830
+				attr->flags = AVP_TRACK_TO | AVP_CLASS_DOMAIN;
831 831
 				break;
832 832
 			case 'g':
833
-				*type = AVP_TRACK_ALL | AVP_CLASS_GLOBAL;
833
+				attr->flags = AVP_TRACK_ALL | AVP_CLASS_GLOBAL;
834 834
 				break;
835 835
 			default:
836 836
 				if (id < 1<<8)
... ...
@@ -848,23 +867,19 @@ int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
848 848
 			s.s=p+1;
849 849
 			s.len=name->len-(p-name->s)-2; // [ and ]
850 850
 			if (s.len == 0) {
851
-				*type |= AVP_INDEX_ALL;
851
+				attr->flags |= AVP_INDEX_ALL;
852 852
 			} else {
853 853
 				if (s.s[0]=='-') {
854
-					*type |= AVP_INDEX_BACKWARD;
854
+					attr->flags |= AVP_INDEX_BACKWARD;
855 855
 					s.s++;s.len--;
856 856
 				} else {
857
-					*type |= AVP_INDEX_FORWARD;
857
+					attr->flags |= AVP_INDEX_FORWARD;
858 858
 				}	
859 859
 				if ((str2int(&s, &id) != 0)||(id==0)) {
860 860
 					ERR("Invalid AVP index '%.*s'\n", s.len, s.s);
861 861
 					goto error;
862 862
 				}
863
-				if (index){
864
-					*index = id;
865
-				} else {
866
-					WARN("AVP index correcly specified, but called without placeholed\n");
867
-				}
863
+				attr->index = id;
868 864
 			}
869 865
 			name->len=p-name->s;
870 866
 		}
... ...
@@ -872,28 +887,28 @@ int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
872 872
 		ERR_IF_CONTAINS(name,'[');
873 873
 		ERR_IF_CONTAINS(name,']');
874 874
 		if ((name->len > 2) && (name->s[0]=='/') && (name->s[name->len-1]=='/')) {
875
-			avp_name->re=pkg_malloc(sizeof(regex_t));
876
-			if (!avp_name->re) {
875
+			attr->name.re=pkg_malloc(sizeof(regex_t));
876
+			if (!attr->name.re) {
877 877
 				BUG("No free memory to allocate AVP_NAME_RE regex\n");
878 878
 				goto error;
879 879
 			}
880
-			c=name->s[name->len];
881
-			name->s[name->len]=0;
882
-			if (regcomp(avp_name->re, name->s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
883
-				pkg_free(avp_name->re);
884
-				name->s[name->len] = c;
880
+			name->s[name->len-1]=0;
881
+			if (regcomp(attr->name.re, name->s+1, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
882
+				pkg_free(attr->name.re);
883
+				name->s[name->len-1] = '/';
885 884
 				goto error;
886 885
 			}
887
-			*type |= AVP_NAME_RE;
886
+			name->s[name->len-1] = '/';
887
+			attr->flags |= AVP_NAME_RE;
888 888
 		} else {
889 889
 			ERR_IF_CONTAINS(name,'/');
890
-			*type |= AVP_NAME_STR;
890
+			attr->flags |= AVP_NAME_STR;
891
+			attr->name.s = *name;
891 892
 		}
892
-		avp_name->s = *name;
893 893
 	} else {
894 894
 		/*default is string name*/
895
-		*type = AVP_NAME_STR;
896
-		avp_name->s = *name;
895
+		attr->flags = AVP_NAME_STR;
896
+		attr->name.s = *name;
897 897
 	}
898 898
 
899 899
 	return 0;
... ...
@@ -997,7 +1012,7 @@ error:
997 997
 }
998 998
 
999 999
 
1000
-void delete_avp(unsigned short flags, int_str name)
1000
+void delete_avp(avp_flags_t flags, avp_name_t name)
1001 1001
 {
1002 1002
 	struct search_state st;
1003 1003
 	avp_t* avp;
... ...
@@ -84,34 +84,45 @@ typedef union {
84 84
 	regex_t* re;
85 85
 } int_str;
86 86
 
87
+#define avp_id_t	unsigned short
88
+#define avp_flags_t	unsigned short
89
+#define avp_name_t	int_str
90
+#define avp_value_t	int_str
91
+#define avp_index_t	unsigned short
87 92
 
88 93
 typedef struct usr_avp {
89
-	unsigned short id;
94
+	avp_id_t id;
90 95
 	     /* Flags that are kept for the AVP lifetime */
91
-	unsigned short flags;
96
+	avp_flags_t flags;
92 97
 	struct usr_avp *next;
93 98
 	void *data;
94 99
 } avp_t;
95 100
 
96 101
 typedef avp_t* avp_list_t;
97 102
 
103
+/* AVP identification */
104
+typedef struct avp_ident {
105
+	avp_flags_t flags;
106
+	avp_name_t name;
107
+	avp_index_t index;
108
+} avp_ident_t;
98 109
 
99 110
 /*
100 111
  * AVP search state
101 112
  */
102 113
 struct search_state {
103
-	unsigned short flags;  /* Type of search and additional flags */
104
-	unsigned short id;
105
-	int_str name;
114
+	avp_flags_t flags;  /* Type of search and additional flags */
115
+	avp_id_t id;
116
+	avp_name_t name;
106 117
 	avp_t* avp;            /* Current AVP */
107
-	regex_t* search_re;    /* Compiled regular expression */
118
+//	regex_t* search_re;    /* Compiled regular expression */
108 119
 };
109 120
 
110 121
 /* avp aliases structs*/
111 122
 typedef struct avp_spec {
112
-	int type;
113
-	int_str name;
114
-	int index;
123
+	avp_flags_t type;
124
+	avp_name_t name;
125
+	avp_index_t index;
115 126
 } avp_spec_t;
116 127
 
117 128
 /* AVP types */
... ...
@@ -142,20 +153,23 @@ typedef struct avp_spec {
142 142
 int init_avps(void);
143 143
 
144 144
 /* add avp to the list of avps */
145
-int add_avp(unsigned short flags, int_str name, int_str val);
146
-int add_avp_before(avp_t *avp, unsigned short flags, int_str name, int_str val);
147
-int add_avp_list(avp_list_t* list, unsigned short flags, int_str name, int_str val);
145
+int add_avp(avp_flags_t flags, avp_name_t name, avp_value_t val);
146
+int add_avp_before(avp_t *avp, avp_flags_t flags, avp_name_t name, avp_value_t val);
147
+int add_avp_list(avp_list_t* list, avp_flags_t flags, avp_name_t name, avp_value_t val);
148 148
 
149 149
 /* Delete avps with given type and name */
150
-void delete_avp(unsigned short flags, int_str name);
150
+void delete_avp(avp_flags_t flags, avp_name_t name);
151 151
 
152 152
 /* search functions */
153
-avp_t *search_first_avp( unsigned short flags, int_str name,
154
-			 int_str *val, struct search_state* state);
155
-avp_t *search_next_avp(struct search_state* state, int_str *val);
153
+avp_t *search_first_avp( avp_flags_t flags, avp_name_t name,
154
+			 avp_value_t *val, struct search_state* state);
155
+avp_t *search_avp_by_index( avp_flags_t flags, avp_name_t name,
156
+                            avp_value_t *val, avp_index_t index);
157
+
158
+avp_t *search_avp (avp_ident_t ident, avp_value_t* val, struct search_state* state);
159
+avp_t *search_next_avp(struct search_state* state, avp_value_t *val);
160
+
156 161
 
157
-avp_t *search_avp_by_index( unsigned short flags, int_str name,
158
-                            int_str *val, unsigned short index);
159 162
 /* free functions */
160 163
 void reset_avps(void);
161 164
 
... ...
@@ -164,17 +178,18 @@ void destroy_avp_list(avp_list_t *list );
164 164
 void destroy_avp_list_unsafe(avp_list_t *list );
165 165
 
166 166
 /* get func */
167
-void get_avp_val(avp_t *avp, int_str *val );
167
+void get_avp_val(avp_t *avp, avp_value_t *val );
168 168
 str* get_avp_name(avp_t *avp);
169 169
 
170
-avp_list_t get_avp_list(unsigned short flags);
171
-avp_list_t* set_avp_list(unsigned short flags, avp_list_t* list);
170
+avp_list_t get_avp_list(avp_flags_t flags);
171
+avp_list_t* set_avp_list(avp_flags_t flags, avp_list_t* list);
172 172
 
173 173
 
174 174
 /* global alias functions (manipulation and parsing)*/
175 175
 int add_avp_galias_str(char *alias_definition);
176 176
 int lookup_avp_galias(str *alias, int *type, int_str *avp_name);
177 177
 int add_avp_galias(str *alias, int type, int_str avp_name);
178
+int parse_avp_ident( str *name, avp_ident_t* attr);
178 179
 int parse_avp_name( str *name, int *type, int_str *avp_name, int *index);
179 180
 int parse_avp_spec( str *name, int *type, int_str *avp_name, int *index);
180 181
 void free_avp_name( int *type, int_str *avp_name);