Browse code

pv: suport also Route headers in $hfl(...)

Daniel-Constantin Mierla authored on 02/06/2021 14:57:05
Showing 1 changed files
... ...
@@ -2227,6 +2227,10 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2227 2227
 	}
2228 2228
 
2229 2229
 	if((tv.flags == 0) && (tv.ri==HDR_VIA_T)) {
2230
+		if(msg->h_via1==NULL) {
2231
+			LM_WARN("no Via header\n");
2232
+			return pv_get_null(msg, param, res);
2233
+		}
2230 2234
 		if(idx<0) {
2231 2235
 			n = 1;
2232 2236
 			/* count Via header bodies */
... ...
@@ -2271,17 +2275,28 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2271 2275
 		LM_DBG("unexpected via index out of range\n");
2272 2276
 		return pv_get_null(msg, param, res);
2273 2277
 	}
2274
-	if((tv.flags == 0) && (tv.ri==HDR_RECORDROUTE_T)) {
2278
+	if((tv.flags == 0) && (tv.ri==HDR_RECORDROUTE_T || tv.ri==HDR_ROUTE_T)) {
2279
+		if(tv.ri==HDR_RECORDROUTE_T) {
2280
+			hf=msg->record_route;
2281
+		} else {
2282
+			hf=msg->route;
2283
+		}
2284
+		if(hf==NULL) {
2285
+			LM_DBG("no %s header\n", (tv.ri==HDR_ROUTE_T)?"route":"record-route");
2286
+			return pv_get_null(msg, param, res);
2287
+		}
2288
+
2275 2289
 		if(idx<0) {
2276 2290
 			n = 1;
2277
-			/* count Record-Route header bodies */
2278
-			for(hf=msg->record_route; hf!=NULL; hf=hf->next) {
2279
-				if(hf->type==HDR_RECORDROUTE_T) {
2291
+			/* count Record-Route/Route header bodies */
2292
+			for(; hf!=NULL; hf=hf->next) {
2293
+				if(hf->type==tv.ri) {
2280 2294
 					if(parse_rr(hf) == -1) {
2281
-						LM_ERR("failed parsing rr header\n");
2295
+						LM_ERR("failed parsing %s header\n",
2296
+								(tv.ri==HDR_ROUTE_T)?"route":"record-route");
2282 2297
 						return pv_get_null(msg, param, res);
2283 2298
 					}
2284
-					for(rrb=(rr_t*)hf->parsed; vb!=NULL; rrb=rrb->next) {
2299
+					for(rrb=(rr_t*)hf->parsed; rrb!=NULL; rrb=rrb->next) {
2285 2300
 						n++;
2286 2301
 					}
2287 2302
 				}
... ...
@@ -2289,17 +2304,24 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2289 2304
 
2290 2305
 			idx = -idx;
2291 2306
 			if(idx>n) {
2292
-				LM_DBG("index out of rr headers range\n");
2307
+				LM_DBG("index out of %s headers range\n",
2308
+						(tv.ri==HDR_ROUTE_T)?"route":"record-route");
2293 2309
 				return pv_get_null(msg, param, res);
2294 2310
 			}
2295 2311
 			idx = n - idx;
2296 2312
 		}
2297 2313
 		if(idx==0) {
2298
-			if(parse_rr(msg->record_route) == -1) {
2299
-				LM_ERR("failed parsing rr header\n");
2314
+			if(tv.ri==HDR_RECORDROUTE_T) {
2315
+				hf=msg->record_route;
2316
+			} else {
2317
+				hf=msg->route;
2318
+			}
2319
+			if(parse_rr(hf) == -1) {
2320
+				LM_ERR("failed parsing %s header\n",
2321
+						(tv.ri==HDR_ROUTE_T)?"route":"record-route");
2300 2322
 				return pv_get_null(msg, param, res);
2301 2323
 			}
2302
-			rrb = (rr_t*)(msg->record_route->parsed);
2324
+			rrb = (rr_t*)(hf->parsed);
2303 2325
 			sval.s = rrb->nameaddr.name.s;
2304 2326
 			sval.len = rrb->len;
2305 2327
 			trim(&sval);
... ...
@@ -2307,13 +2329,19 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2307 2329
 			return 0;
2308 2330
 		}
2309 2331
 		n=0;
2310
-		for(hf=msg->record_route; hf!=NULL; hf=hf->next) {
2311
-			if(hf->type==HDR_RECORDROUTE_T) {
2332
+		if(tv.ri==HDR_RECORDROUTE_T) {
2333
+			hf=msg->record_route;
2334
+		} else {
2335
+			hf=msg->route;
2336
+		}
2337
+		for(; hf!=NULL; hf=hf->next) {
2338
+			if(hf->type==tv.ri) {
2312 2339
 				if(parse_rr(hf) == -1) {
2313
-					LM_ERR("failed parsing rr header\n");
2340
+					LM_ERR("failed parsing %s header\n",
2341
+							(tv.ri==HDR_ROUTE_T)?"route":"record-route");
2314 2342
 					return pv_get_null(msg, param, res);
2315 2343
 				}
2316
-				for(rrb=(rr_t*)hf->parsed; vb!=NULL; rrb=rrb->next) {
2344
+				for(rrb=(rr_t*)hf->parsed; rrb!=NULL; rrb=rrb->next) {
2317 2345
 					if(n==idx) {
2318 2346
 						sval.s = rrb->nameaddr.name.s;
2319 2347
 						sval.len = rrb->len;
... ...
@@ -2325,7 +2353,8 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2325 2353
 				}
2326 2354
 			}
2327 2355
 		}
2328
-		LM_DBG("unexpected record-route index out of range\n");
2356
+		LM_DBG("unexpected %s index out of range\n",
2357
+				(tv.ri==HDR_ROUTE_T)?"route":"record-route");
2329 2358
 		return pv_get_null(msg, param, res);
2330 2359
 	}
2331 2360