Browse code

Functions to set/reset/test per-branch flags.

This patch adds support for setbflag,resetbflag, and isbflagset
functions, these functions can be used to manipulate branch flags and
they are inspired by similar functions in kamailio.

There is a small difference compared to their counterparts in kamailio
though. The second parameter of all these functions is of type flag_t
and it is the index (counting from 0) of the flag to be manipulated.
This is better aligned with how normal flags (flags.[ch]) in sip-router
work. Similar functions in kamailio took the mask as the second
parameter with values of flags to be manipulated.

This also prevents users from modifying multiple flags at the same time
accidental (note that it is not obvious from the function name that
the function could do it).

Jan Janak authored on 14/03/2009 18:23:07
Showing 2 changed files
... ...
@@ -66,7 +66,7 @@ struct branch
66 66
 	struct socket_info* force_send_socket;
67 67
 
68 68
 	/* Branch flags */
69
-	unsigned int flags;
69
+	flag_t flags;
70 70
 };
71 71
 
72 72
 
... ...
@@ -83,7 +83,47 @@ unsigned int nr_branches = 0;
83 83
 static int branch_iterator = 0;
84 84
 
85 85
 /* The q parameter of the Request-URI */
86
-static qvalue_t ruri_q = Q_UNSPECIFIED; 
86
+static qvalue_t ruri_q = Q_UNSPECIFIED;
87
+
88
+/* Branch flags of the Request-URI */
89
+static flag_t ruri_bflags;
90
+
91
+
92
+static inline flag_t* get_bflags_ptr(unsigned int branch)
93
+{
94
+	if (branch == 0) return &ruri_bflags;
95
+	if (branch - 1 < nr_branches) return &branches[branch - 1].flags;
96
+	return NULL;
97
+}
98
+
99
+
100
+int setbflag(unsigned int branch, flag_t flag)
101
+{
102
+	flag_t* flags;
103
+
104
+	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
105
+	(*flags) |= 1 << flag;
106
+	return 1;
107
+}
108
+
109
+
110
+int isbflagset(unsigned int branch, flag_t flag)
111
+{
112
+	flag_t* flags;
113
+
114
+	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
115
+	return ((*flags) & (1 << flag)) ? 1 : -1;
116
+}
117
+
118
+
119
+int resetbflag(unsigned int branch, flag_t flag)
120
+{
121
+	flag_t* flags;
122
+
123
+	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
124
+	(*flags) &= ~ (1 << flag);
125
+	return 1;
126
+}
87 127
 
88 128
 
89 129
 /*
... ...
@@ -30,6 +30,7 @@
30 30
 
31 31
 #include "ip_addr.h"
32 32
 #include "qvalue.h"
33
+#include "flags.h"
33 34
 
34 35
 struct sip_msg;
35 36
 
... ...
@@ -82,5 +83,34 @@ qvalue_t get_ruri_q(void);
82 83
 int get_request_uri(struct sip_msg* _m, str* _u);
83 84
 int rewrite_uri(struct sip_msg* _m, str* _s);
84 85
 
86
+/**
87
+ * Set a per-branch flag to 1.
88
+ *
89
+ * This function sets the value of one particular branch flag to 1.
90
+ * @param branch Number of the branch (0 for the main Request-URI branch)
91
+ * @param flag Number of the flag to be set (starting with 0)
92
+ * @return 1 on success, -1 on failure.
93
+ */
94
+int setbflag(unsigned int branch, flag_t flag);
95
+
96
+/**
97
+ * Reset a per-branch flag value to 0.
98
+ *
99
+ * This function resets the value of one particular branch flag to 0.
100
+ * @param branch Number of the branch (0 for the main Request-URI branch)
101
+ * @param flag Number of the flag to be reset (starting with 0)
102
+ * @return 1 on success, -1 on failure.
103
+ */
104
+int resetbflag(unsigned int branch, flag_t flag);
105
+
106
+/**
107
+ * Determine if a branch flag is set.
108
+ *
109
+ * This function tests the value of one particular per-branch flag.
110
+ * @param branch Number of the branch (0 for the main Request-URI branch)
111
+ * @param flag Number of the flag to be tested (starting with 0)
112
+ * @return 1 if the branch flag is set, -1 if not or on failure.
113
+ */
114
+int isbflagset(unsigned int branch, flag_t flag);
85 115
 
86 116
 #endif /* _DSET_H */