1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21 
22 #ifndef _SER_USR_AVP_H_
23 #define _SER_USR_AVP_H_
24 
25 #include <sys/types.h>
26 #include <regex.h>
27 
28 
29 /*
30  *   LIST with the allocated flags, their meaning and owner
31  *   flag no.    owner            description
32  *   -------------------------------------------------------
33  *     0        avp_core          avp has a string name
34  *     1        avp_core          avp has a string value
35  *     2        avp_core          regex search in progress
36  *     3        avpops module     avp was loaded from DB
37  *     4        lcr module        contact avp qvalue change
38  *     5        core              avp is in user list
39  *     6        core              avp is in domain list
40  *     7        core              avp is in global list
41  *     8        core              avp is in the from avp list
42  *     9        core              avp is in the to avp list
43  *    10	core		  avp name with positive index
44  *    11	core		  avp name with negative index
45  */
46 
47 #include "str.h"
48 
49 
50 #define AVP_UID          "uid"           /* Unique user identifier */
51 #define AVP_DID          "did"           /* Unique domain identifier */
52 #define AVP_REALM        "digest_realm"  /* Digest realm */
53 #define AVP_FR_TIMER     "fr_timer"      /* Value of final response timer */
54 #define AVP_FR_INV_TIMER "fr_inv_timer"  /* Value of final response invite timer */
55 #define AVP_RPID         "rpid"          /* Remote-Party-ID */
56 #define AVP_GFLAGS       "gflags"        /* global flags */
57 
58 struct str_int_data {
59 	str name;
60 	int val;
61 };
62 
63 struct str_str_data {
64 	str name;
65 	str val;
66 };
67 
68 typedef union {
69 	int  n;
70 	str  s;
71 	regex_t* re;
72 } int_str;
73 
74 #define avp_id_t	unsigned short
75 #define avp_flags_t	unsigned int
76 #define avp_name_t	int_str
77 #define avp_value_t	int_str
78 #define avp_index_t	unsigned short
79 
80 union usr_avp_data{
81 	void *p; /* forces alignment */
82 	long l;
83 	char data[sizeof(void*)]; /* used to access other types, var length */
84 };
85 
86 typedef struct usr_avp {
87 	avp_id_t id;
88 	/* Flags that are kept for the AVP lifetime */
89 	avp_flags_t flags;
90 	struct usr_avp *next;
91 	union usr_avp_data d; /* var length */
92 } avp_t;
93 
94 typedef avp_t* avp_list_t;
95 
96 /* AVP identification */
97 typedef struct avp_ident {
98 	avp_flags_t flags;
99 	avp_name_t name;
100 	avp_index_t index;
101 } avp_ident_t;
102 
103 /*
104  * AVP search state
105  */
106 typedef struct search_state {
107 	avp_flags_t flags;  /* Type of search and additional flags */
108 	avp_id_t id;
109 	avp_name_t name;
110 	avp_t* avp;            /* Current AVP */
111 //	regex_t* search_re;    /* Compiled regular expression */
112 } avp_search_state_t;
113 
114 /* avp aliases structs*/
115 typedef struct avp_spec {
116 	avp_flags_t type;
117 	avp_name_t name;
118 	avp_index_t index;
119 } avp_spec_t;
120 
121 /* AVP types */
122 #define AVP_NAME_STR     (1<<0)
123 #define AVP_VAL_STR      (1<<1)
124 #define AVP_NAME_RE      (1<<2)
125 
126 /* AVP classes */
127 #define AVP_CLASS_URI    (1<<4)
128 #define AVP_CLASS_USER   (1<<5)
129 #define AVP_CLASS_DOMAIN (1<<6)
130 #define AVP_CLASS_GLOBAL (1<<7)
131 
132 /* AVP track (either from or to) */
133 #define AVP_TRACK_FROM   (1<<8)
134 #define AVP_TRACK_TO     (1<<9)
135 #define AVP_TRACK_ALL    (AVP_TRACK_FROM|AVP_TRACK_TO)
136 
137 #define AVP_CLASS_ALL (AVP_CLASS_URI|AVP_CLASS_USER|AVP_CLASS_DOMAIN|AVP_CLASS_GLOBAL)
138 
139 /* AVP name index */
140 #define AVP_INDEX_FORWARD	(1<<10)
141 #define AVP_INDEX_BACKWARD	(1<<11)
142 #define AVP_INDEX_ALL		(AVP_INDEX_FORWARD | AVP_INDEX_BACKWARD)
143 
144 /* AVP DB flag used by avpops module - defined in avpops
145  * - kept here for reference */
146 // #define AVP_IS_IN_DB    (1<<12)
147 
148 #define AVP_CUSTOM_FLAGS	13
149 
150 #define GALIAS_CHAR_MARKER  '$'
151 
152 #define AVP_NAME_VALUE_MASK     0x0007
153 #define AVP_CORE_MASK           0x00ff
154 #define AVP_SCRIPT_MASK         0xff00
155 #define avp_core_flags(f)       ((f)&0x00ff)
156 #define avp_script_flags(f)     (((f)<<8)&0xff00)
157 #define avp_get_script_flags(f) (((f)&0xff00)>>8)
158 
159 #define is_avp_str_name(a)      ((a)->flags&AVP_NAME_STR)
160 #define is_avp_str_val(a)       ((a)->flags&AVP_VAL_STR)
161 
162 
163 #define AVP_IS_ASSIGNABLE(ident) ( ((ident).flags & AVP_NAME_RE) == 0 && (((ident).flags & AVP_NAME) == 0 || (((ident)->flags & AVP_NAME) && (ident).name.s.len)) )
164 /* Initialize memory structures */
165 int init_avps(void);
166 
167 /* add avp to the list of avps */
168 int add_avp(avp_flags_t flags, avp_name_t name, avp_value_t val);
169 int add_avp_before(avp_t *avp, avp_flags_t flags, avp_name_t name, avp_value_t val);
170 int add_avp_list(avp_list_t* list, avp_flags_t flags, avp_name_t name, avp_value_t val);
171 
172 /* Delete avps with given type and name */
173 void delete_avp(avp_flags_t flags, avp_name_t name);
174 
175 int destroy_avps(avp_flags_t flags, avp_name_t name, int all);
176 
177 /* search functions */
178 avp_t *search_first_avp( avp_flags_t flags, avp_name_t name,
179 			 avp_value_t *val, struct search_state* state);
180 avp_t *search_avp_by_index( avp_flags_t flags, avp_name_t name,
181                             avp_value_t *val, avp_index_t index);
182 
183 avp_t *search_avp (avp_ident_t ident, avp_value_t* val, struct search_state* state);
184 avp_t *search_next_avp(struct search_state* state, avp_value_t *val);
185 
186 /* Reset one avp list */
187 int reset_avp_list(int flags);
188 
189 /* free functions */
190 void reset_avps(void);
191 
192 void destroy_avp(avp_t *avp);
193 void destroy_avp_list(avp_list_t *list );
194 void destroy_avp_list_unsafe(avp_list_t *list );
195 
196 /* get func */
197 void get_avp_val(avp_t *avp, avp_value_t *val );
198 str* get_avp_name(avp_t *avp);
199 
200 avp_list_t get_avp_list(avp_flags_t flags);
201 avp_list_t* set_avp_list(avp_flags_t flags, avp_list_t* list);
202 
203 
204 /* global alias functions (manipulation and parsing)*/
205 int add_avp_galias_str(char *alias_definition);
206 int lookup_avp_galias(str *alias, int *type, int_str *avp_name);
207 int add_avp_galias(str *alias, int type, int_str avp_name);
208 int parse_avp_ident( str *name, avp_ident_t* attr);
209 int parse_avp_name( str *name, int *type, int_str *avp_name, int *index);
210 int parse_avp_spec( str *name, int *type, int_str *avp_name, int *index);
211 int km_parse_avp_spec( str *name, int *type, int_str *avp_name);
212 void free_avp_name( avp_flags_t *type, int_str *avp_name);
213 /* Free an ident obtained with parse_avp_ident() */
214 void free_avp_ident(avp_ident_t* attr);
215 
216 /* AVP flags functions */
217 #define MAX_AVPFLAG  ((unsigned int)( sizeof(avp_flags_t) * CHAR_BIT - 1 - AVP_CUSTOM_FLAGS))
218 
219 avp_flags_t register_avpflag(char* name);
220 avp_flags_t get_avpflag_no(char* name);
221 
222 #endif
223