1 /* mate_util.h
2  *
3  * Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 
13 #ifndef __AVP_H_
14 #define __AVP_H_
15 #include "epan/proto.h"
16 #include <sys/types.h>
17 
18 /* #define _AVP_DEBUGGING */
19 
20 
21 /******* dbg_print *********/
22 #define DEBUG_BUFFER_SIZE 4096
23 extern void dbg_print(const gint* which, gint how, FILE* where,
24 	const gchar* fmt, ... ) G_GNUC_PRINTF(4, 5);
25 
26 
27 /******* single copy strings *********/
28 typedef struct _scs_collection SCS_collection;
29 
30 #define SCS_SMALL_SIZE 16
31 #define SCS_MEDIUM_SIZE 256
32 #define SCS_LARGE_SIZE 4096
33 #define SCS_HUGE_SIZE 65536
34 
35 extern gchar* scs_subscribe(SCS_collection* collection, const gchar* s);
36 extern void scs_unsubscribe(SCS_collection* collection, gchar* s);
37 extern gchar* scs_subscribe_printf(SCS_collection* collection, gchar* fmt, ...)
38 	G_GNUC_PRINTF(2, 3);
39 
40 /******* AVPs & Co. *********/
41 
42 /* these are the defined oreators of avps */
43 #define AVP_OP_EQUAL		'='
44 #define AVP_OP_NOTEQUAL		'!'
45 #define AVP_OP_STARTS		'^'
46 #define AVP_OP_ENDS		'$'
47 #define AVP_OP_CONTAINS		'~'
48 #define AVP_OP_LOWER		'<'
49 #define AVP_OP_HIGHER		'>'
50 #define AVP_OP_EXISTS		'?'
51 #define AVP_OP_ONEOFF		'|'
52 #define AVP_OP_TRANSF		'&'
53 
54 
55 /* an avp is an object made of a name a value and an operator */
56 typedef struct _avp {
57 	gchar* n;
58 	gchar* v;
59 	gchar o;
60 } AVP;
61 
62 /* avp nodes are used in avp lists */
63 typedef struct _avp_node {
64 	AVP* avp;
65 	struct _avp_node* next;
66 	struct _avp_node* prev;
67 } AVPN;
68 
69 /* an avp list is a sorted set of avps */
70 typedef struct _avp_list {
71 	gchar* name;
72 	guint32 len;
73 	AVPN null;
74 } AVPL;
75 
76 
77 
78 /* an avpl transformation operation */
79 typedef enum _avpl_match_mode {
80 	AVPL_NO_MATCH,
81 	AVPL_STRICT,
82 	AVPL_LOOSE,
83 	AVPL_EVERY
84 } avpl_match_mode;
85 
86 typedef enum _avpl_replace_mode {
87 	AVPL_NO_REPLACE,
88 	AVPL_INSERT,
89 	AVPL_REPLACE
90 } avpl_replace_mode;
91 
92 typedef struct _avpl_transf AVPL_Transf;
93 
94 struct _avpl_transf {
95 	gchar* name;
96 
97 	AVPL* match;
98 	AVPL* replace;
99 
100 	avpl_match_mode match_mode;
101 	avpl_replace_mode replace_mode;
102 
103 	GHashTable* map;
104 	AVPL_Transf* next;
105 };
106 
107 /* loalnodes are used in LoALs */
108 typedef struct _loal_node {
109 	AVPL* avpl;
110 	struct _loal_node *next;
111 	struct _loal_node *prev;
112 } LoALnode;
113 
114 
115 /* a loal is a list of avp lists */
116 typedef struct _loal {
117 	gchar* name;
118 	guint len;
119 	LoALnode null;
120 } LoAL;
121 
122 
123 /* avp library (re)initialization */
124 extern void avp_init(void);
125 
126 /* If enabled set's up the debug facilities for the avp library */
127 #ifdef _AVP_DEBUGGING
128 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op);
129 #endif /* _AVP_DEBUGGING */
130 
131 /*
132  * avp constructors
133  */
134 
135 /* creates a new avp */
136 extern AVP* new_avp(const gchar* name, const gchar* value, gchar op);
137 
138 /* creates a copy od an avp */
139 extern AVP* avp_copy(AVP* from);
140 
141 /* creates an avp from a field_info record */
142 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo);
143 
144 /*
145  * avp destructor
146  */
147 extern void delete_avp(AVP* avp);
148 
149 /*
150  * avp methods
151  */
152 /* returns a newly allocated string containing a representation of the avp */
153 #define avp_to_str(avp) (g_strdup_printf("%s%c%s",avp->n,avp->o,avp->v))
154 
155 /* returns the src avp if the src avp matches(*) the op avp or NULL if it doesn't */
156 extern AVP* match_avp(AVP* src, AVP* op);
157 
158 
159 /*
160  * avplist constructors
161  */
162 
163 /* creates an empty avp list */
164 extern AVPL* new_avpl(const gchar* name);
165 
166 
167 /* creates a copy of an avp list */
168 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps);
169 
170 extern AVPL* new_avpl_loose_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps);
171 
172 extern AVPL* new_avpl_pairs_match(const gchar* name, AVPL* src, AVPL* op, gboolean strict, gboolean copy_avps);
173 
174 /* uses mode to call one of the former matches. NO_MATCH = merge(merge(copy(src),op)) */
175 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
176 
177 
178 /*
179  * functions on avpls
180  */
181 
182 /* it will insert an avp to an avpl */
183 extern gboolean insert_avp(AVPL* avpl, AVP* avp);
184 
185 /* renames an avpl */
186 extern void rename_avpl(AVPL* avpl, gchar* name);
187 
188 /* it will add all the avps in src which don't match(*) any attribute in dest */
189 extern void merge_avpl(AVPL* dest, AVPL* src, gboolean copy);
190 
191 /* it will return the first avp in an avpl whose name matches the given name.
192   will return NULL if there is not anyone matching */
193 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie);
194 
195 /* it will get the next avp from an avpl, using cookie to keep state */
196 extern AVP* get_next_avp(AVPL* avpl, void** cookie);
197 
198 /* it will extract the first avp from an avp list */
199 extern AVP* extract_first_avp(AVPL* avpl);
200 
201 /* it will extract the last avp from an avp list */
202 extern AVP* extract_last_avp(AVPL* avpl);
203 
204 /* it will extract the first avp in an avpl whose name matches the given name.
205    it will not extract any and  return NULL if there is not anyone matching */
206 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name);
207 
208 /* returns a newly allocated string containing a representation of the avp list */
209 extern gchar* avpl_to_str(AVPL* avpl);
210 extern gchar* avpl_to_dotstr(AVPL*);
211 
212 /* deletes an avp list  and eventually its contents */
213 extern void delete_avpl(AVPL* avpl, gboolean avps_too);
214 
215 /*
216  *  AVPL transformations
217  */
218 extern void delete_avpl_transform(AVPL_Transf* it);
219 extern void avpl_transform(AVPL* src, AVPL_Transf* op);
220 
221 
222 /*
223  * Lists of AVP lists
224  */
225 
226 /* creates an empty list of avp lists */
227 extern LoAL* new_loal(const gchar* name);
228 
229 /* given a file loads all the avpls contained in it
230    every line is formatted as it is the output of avplist_to_string */
231 extern LoAL* loal_from_file(gchar* filename);
232 
233 /* inserts an avplist into a LoAL */
234 extern void loal_append(LoAL* loal, AVPL* avpl);
235 
236 /* extracts the first avp list from the loal */
237 extern AVPL* extract_first_avpl(LoAL* loal);
238 
239 /* extracts the last avp list from the loal */
240 extern AVPL* extract_last_avpl(LoAL* loal);
241 
242 /* it will get the next avp list from a LoAL, using cookie to keep state */
243 extern AVPL* get_next_avpl(LoAL* loal,void** cookie);
244 
245 /* deletes a loal and eventually its contents */
246 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too);
247 
248 
249 #endif
250