Browse code

path: insert flow token in path header with received

- added the possiblity to have the received parameter and flow toke
in the add_path_received function

Rick Barenthin authored on 04/10/2017 15:41:39
Showing 2 changed files
... ...
@@ -323,6 +323,14 @@ if (!add_path("loadbalancer", "ob")) {
323 323
 		received-parameter.
324 324
 		</para>
325 325
 		<para>
326
+		If the <quote>outbound</quote> module was loaded before this module, and outbound is
327
+		required for this request, the header will be in the form
328
+		<quote>Path: &lt;sip:flowtoken@1.2.3.4;lr;received=sip:2.3.4.5:1234;ob&gt;</quote>, where <quote>flowtoken</quote>
329
+		is the RFC 5626 flow-token that can be used to identify the source and local address and
330
+		transport the request was received on, and where <quote>1.2.3.4</quote> is the address of
331
+		the outgoing interface.
332
+		</para>
333
+		<para>
326 334
 		This function can be used from REQUEST_ROUTE.
327 335
 		</para>
328 336
 		<example>
... ...
@@ -397,4 +405,3 @@ if (!add_path_received("inbound", "ob")) {
397 405
 
398 406
 
399 407
 </chapter>
400
-
... ...
@@ -42,7 +42,7 @@
42 42
 #include "path_mod.h"
43 43
 
44 44
 typedef enum {
45
-	PATH_PARAM_NONE, PATH_PARAM_RECEIVED, PATH_PARAM_OB
45
+	PATH_PARAM_NONE = 0, PATH_PARAM_RECEIVED = 1, PATH_PARAM_OB = 2
46 46
 } path_param_t;
47 47
 
48 48
 #define PATH_PREFIX		"Path: <sip:"
... ...
@@ -73,6 +73,26 @@ static char *path_strzdup(char *src, int len)
73 73
 	return res;
74 74
 }
75 75
 
76
+static int handleOutbound(sip_msg_t* _m, str *user, path_param_t *param)
77
+{
78
+	if (path_obb.use_outbound != NULL && path_obb.use_outbound(_m)) {
79
+		struct via_body *via;
80
+
81
+		if (path_obb.encode_flow_token(user, _m->rcv) != 0) {
82
+			LM_ERR("encoding outbound flow-token\n");
83
+			return -1;
84
+		}
85
+
86
+		/* Only include ;ob parameter if this is the first-hop
87
+		 * (that means only one Via:) */
88
+		if (parse_via_header(_m, 2, &via) < 0) {
89
+			*param |= PATH_PARAM_OB;
90
+		}
91
+	}
92
+
93
+	return 1;
94
+}
95
+
76 96
 static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
77 97
 		str *add_params)
78 98
 {
... ...
@@ -94,10 +114,7 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
94 114
 
95 115
 	cp += sprintf(cp, ";lr");
96 116
 
97
-	switch(param) {
98
-	default:
99
-		break;
100
-	case PATH_PARAM_RECEIVED:
117
+	if (param & PATH_PARAM_RECEIVED) {
101 118
 		if(path_received_format==0) {
102 119
 			if (_m->rcv.proto
103 120
 						< (sizeof(proto_strings) / sizeof(*proto_strings))) {
... ...
@@ -125,14 +142,15 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
125 142
 						_m->rcv.src_port, (int)_m->rcv.proto);
126 143
 			}
127 144
 		}
128
-		break;
129
-	case PATH_PARAM_OB:
145
+	}
146
+
147
+	if (param & PATH_PARAM_OB) {
130 148
 		cp += sprintf(cp, ";ob");
131
-		break;
132 149
 	}
133 150
 
134
-	if (add_params && add_params->len)
151
+	if (add_params && add_params->len) {
135 152
 		cp += sprintf(cp, ";%.*s", add_params->len, add_params->s);
153
+	}
136 154
 
137 155
 	if(path_enable_r2==0) {
138 156
 		cp += sprintf(cp, ">\r\n");
... ...
@@ -207,25 +225,16 @@ int ki_add_path(struct sip_msg* _msg)
207 225
 	str user = {0,0};
208 226
 	int ret;
209 227
 	path_param_t param = PATH_PARAM_NONE;
210
-	struct via_body *via;
211 228
 
212
-	if (path_obb.use_outbound != NULL
213
-		&& path_obb.use_outbound(_msg)) {
214
-		if (path_obb.encode_flow_token(&user, _msg->rcv) != 0) {
215
-			LM_ERR("encoding outbound flow-token\n");
216
-			return -1;
217
-		}
229
+	ret = handleOutbound(_msg, &user, &param);
218 230
 
219
-		/* Only include ;ob parameter if this is the first-hop
220
-		 * (that means only one Via:) */
221
-		if (parse_via_header(_msg, 2, &via) < 0)
222
-			param = PATH_PARAM_OB;
231
+	if (ret > 0) {
232
+		ret = prepend_path(_msg, &user, param, NULL);
223 233
 	}
224 234
 
225
-	ret = prepend_path(_msg, &user, param, NULL);
226
-
227
-	if (user.s != NULL)
235
+	if (user.s != NULL) {
228 236
 		pkg_free(user.s);
237
+	}
229 238
 
230 239
 	return ret;
231 240
 }
... ...
@@ -283,18 +292,32 @@ int ki_add_path_user_params(sip_msg_t* _msg, str* _user, str* _params)
283 292
  * Prepend own uri to Path header and append received address as
284 293
  * "received"-param to that uri.
285 294
  */
286
-int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
295
+int ki_add_path_received(sip_msg_t* _msg)
287 296
 {
288
-	return prepend_path(_msg, NULL, PATH_PARAM_RECEIVED, NULL);
297
+	str user = {0,0};
298
+	int ret;
299
+	path_param_t param = PATH_PARAM_RECEIVED;
300
+
301
+	ret = handleOutbound(_msg, &user, &param);
302
+
303
+	if (ret > 0) {
304
+		ret = prepend_path(_msg, &user, param, NULL);
305
+	}
306
+
307
+	if (user.s != NULL) {
308
+		pkg_free(user.s);
309
+	}
310
+
311
+	return ret;
289 312
 }
290 313
 
291 314
 /*! \brief
292 315
  * Prepend own uri to Path header and append received address as
293 316
  * "received"-param to that uri.
294 317
  */
295
-int ki_add_path_received(sip_msg_t* _msg)
318
+int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
296 319
 {
297
-	return prepend_path(_msg, NULL, PATH_PARAM_RECEIVED, NULL);
320
+	return ki_add_path_received(_msg);
298 321
 }
299 322
 
300 323
 /*! \brief
... ...
@@ -338,7 +361,6 @@ int ki_add_path_received_user(sip_msg_t* _msg, str* _user)
338 361
  */
339 362
 int ki_add_path_received_user_params(sip_msg_t* _msg, str* _user, str* _params)
340 363
 {
341
-
342 364
 	return prepend_path(_msg, _user, PATH_PARAM_RECEIVED, _params);
343 365
 }
344 366