Browse code

modules_k/path: Added outbound support to add_path()

Peter Dunkley authored on 26/12/2012 14:15:48
Showing 3 changed files
... ...
@@ -41,6 +41,10 @@
41 41
 #include "path.h"
42 42
 #include "path_mod.h"
43 43
 
44
+typedef enum {
45
+	PATH_PARAM_NONE, PATH_PARAM_RECEIVED, PATH_PARAM_OB
46
+} path_param_t;
47
+
44 48
 #define PATH_PREFIX		"Path: <sip:"
45 49
 #define PATH_PREFIX_LEN		(sizeof(PATH_PREFIX)-1)
46 50
 
... ...
@@ -50,10 +54,20 @@
50 54
 #define PATH_RC_PARAM		";received="
51 55
 #define PATH_RC_PARAM_LEN	(sizeof(PATH_RC_PARAM)-1)
52 56
 
57
+#define PATH_OB_PARAM		";ob"
58
+#define PATH_OB_PARAM_LEN	(sizeof(PATH_OB_PARAM)-1)
59
+
53 60
 #define	PATH_CRLF		">\r\n"
54 61
 #define PATH_CRLF_LEN		(sizeof(PATH_CRLF)-1)
55 62
 
56
-static int prepend_path(struct sip_msg* _m, str *user, int recv)
63
+#define ALLOC_AND_COPY_PATH_HDR() \
64
+	if ((suffix = pkg_malloc(suffix_len)) == NULL) { \
65
+		LM_ERR("no pkg memory left for suffix\n"); \
66
+		goto out1; \
67
+	} \
68
+	memcpy(suffix, PATH_LR_PARAM, PATH_LR_PARAM_LEN);
69
+
70
+static int prepend_path(struct sip_msg* _m, str *user, path_param_t param)
57 71
 {
58 72
 	struct lump *l;
59 73
 	char *prefix, *suffix, *crlf;
... ...
@@ -76,15 +90,24 @@ static int prepend_path(struct sip_msg* _m, str *user, int recv)
76 90
 		memcpy(prefix + prefix_len - 1, "@", 1);
77 91
 	}
78 92
 
79
-	suffix_len = PATH_LR_PARAM_LEN + (recv ? PATH_RC_PARAM_LEN : 0);
80
-	suffix = pkg_malloc(suffix_len);
81
-	if (!suffix) {
82
-		LM_ERR("no pkg memory left for suffix\n");
83
-		goto out1;
93
+	switch(param) {
94
+	default:
95
+		suffix_len = PATH_LR_PARAM_LEN;
96
+		ALLOC_AND_COPY_PATH_HDR();
97
+		break;
98
+	case PATH_PARAM_RECEIVED:
99
+		suffix_len = PATH_LR_PARAM_LEN + PATH_RC_PARAM_LEN;
100
+		ALLOC_AND_COPY_PATH_HDR();
101
+		memcpy(suffix + PATH_LR_PARAM_LEN, PATH_RC_PARAM,	
102
+			PATH_RC_PARAM_LEN);
103
+		break;
104
+	case PATH_PARAM_OB:
105
+		suffix_len = PATH_LR_PARAM_LEN + PATH_OB_PARAM_LEN;
106
+		ALLOC_AND_COPY_PATH_HDR();
107
+		memcpy(suffix + PATH_LR_PARAM_LEN, PATH_OB_PARAM,
108
+			PATH_OB_PARAM_LEN);
109
+		break;
84 110
 	}
85
-	memcpy(suffix, PATH_LR_PARAM, PATH_LR_PARAM_LEN);
86
-	if(recv)
87
-		memcpy(suffix+PATH_LR_PARAM_LEN, PATH_RC_PARAM, PATH_RC_PARAM_LEN);
88 111
 
89 112
 	crlf = pkg_malloc(PATH_CRLF_LEN);
90 113
 	if (!crlf) {
... ...
@@ -119,7 +142,7 @@ static int prepend_path(struct sip_msg* _m, str *user, int recv)
119 142
 	if (!l) goto out2;
120 143
 	l = insert_new_lump_before(l, suffix, suffix_len, 0);
121 144
 	if (!l) goto out2;
122
-	if (recv) {
145
+	if (param == PATH_PARAM_RECEIVED) {
123 146
 		/* TODO: agranig: optimize this one! */
124 147
 		src_ip = ip_addr2a(&_m->rcv.src_ip);
125 148
 		rcv_addr.s = pkg_malloc(6 + IP_ADDR_MAX_STR_SIZE + 22); /* 'sip:<ip>:<port>;transport=sctp'\0 */
... ...
@@ -174,7 +197,24 @@ out4:
174 197
 int add_path(struct sip_msg* _msg, char* _a, char* _b)
175 198
 {
176 199
 	str user = {0,0};
177
-	return prepend_path(_msg, &user, 0);
200
+	int ret;
201
+	path_param_t param = PATH_PARAM_NONE;
202
+
203
+	if (path_obb.use_outbound != NULL
204
+		&& path_obb.use_outbound(_msg)) {
205
+		if (path_obb.encode_flow_token(&user, _msg->rcv) != 0) {
206
+			LM_ERR("encoding outbound flow token\n");
207
+			return -1;	
208
+		}
209
+		param = PATH_PARAM_OB;
210
+	}
211
+
212
+	ret = prepend_path(_msg, &user, param);
213
+
214
+	if (user.s != NULL)
215
+		pkg_free(user.s);
216
+
217
+	return ret;
178 218
 }
179 219
 
180 220
 /*! \brief
... ...
@@ -183,7 +223,7 @@ int add_path(struct sip_msg* _msg, char* _a, char* _b)
183 223
  */
184 224
 int add_path_usr(struct sip_msg* _msg, char* _usr, char* _b)
185 225
 {
186
-	return prepend_path(_msg, (str*)_usr, 0);
226
+	return prepend_path(_msg, (str*)_usr, PATH_PARAM_NONE);
187 227
 }
188 228
 
189 229
 /*! \brief
... ...
@@ -193,7 +233,7 @@ int add_path_usr(struct sip_msg* _msg, char* _usr, char* _b)
193 233
 int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
194 234
 {
195 235
 	str user = {0,0};
196
-	return prepend_path(_msg, &user, 1);
236
+	return prepend_path(_msg, &user, PATH_PARAM_RECEIVED);
197 237
 }
198 238
 
199 239
 /*! \brief
... ...
@@ -202,7 +242,7 @@ int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
202 242
  */
203 243
 int add_path_received_usr(struct sip_msg* _msg, char* _usr, char* _b)
204 244
 {
205
-	return prepend_path(_msg, (str*)_usr, 1);
245
+	return prepend_path(_msg, (str*)_usr, PATH_PARAM_RECEIVED);
206 246
 }
207 247
 
208 248
 /*! \brief
... ...
@@ -52,6 +52,7 @@
52 52
 #include "../../sr_module.h"
53 53
 #include "../../mem/mem.h"
54 54
 #include "../../mod_fix.h"
55
+#include "../outbound/api.h"
55 56
 #include "../rr/api.h"
56 57
 
57 58
 #include "path.h"
... ...
@@ -74,6 +75,10 @@ static int mod_init(void);
74 75
  */
75 76
 struct rr_binds path_rrb;
76 77
 
78
+/*! \brief
79
+ * outbound API
80
+ */
81
+ob_api_t path_obb;
77 82
 
78 83
 /*! \brief
79 84
  * Exported functions
... ...
@@ -131,6 +136,13 @@ static int mod_init(void)
131 136
 			return -1;
132 137
 		}
133 138
 	}
139
+
140
+	if (ob_load_api(&path_obb) == 0)
141
+		LM_INFO("Bound path module to outbound module\n");
142
+	else {
143
+		LM_INFO("outbound module not available\n");
144
+		memset(&path_obb, 0, sizeof(ob_api_t));
145
+	}
134 146
 	
135 147
 	return 0;
136 148
 }
... ...
@@ -34,7 +34,9 @@
34 34
 #ifndef PATH_MOD_H
35 35
 #define PATH_MOD_H
36 36
 
37
-extern int use_received;
37
+#include "../outbound/api.h"
38 38
 
39
+extern int use_received;
40
+extern ob_api_t path_obb;
39 41
 
40 42
 #endif /* PATH_MOD_H */