Browse code

pv: support for Record-Route in $hfl(...)

Daniel-Constantin Mierla authored on 02/06/2021 10:23:40
Showing 1 changed files
... ...
@@ -2190,6 +2190,7 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2190 2190
 	int idxf = 0;
2191 2191
 	pv_value_t tv = {0};
2192 2192
 	via_body_t *vb = NULL;
2193
+	rr_t *rrb = NULL;
2193 2194
 	hdr_field_t *hf = NULL;
2194 2195
 	int n = 0;
2195 2196
 	str sval = STR_NULL;
... ...
@@ -2228,7 +2229,7 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2228 2229
 	if((tv.flags == 0) && (tv.ri==HDR_VIA_T)) {
2229 2230
 		if(idx<0) {
2230 2231
 			n = 1;
2231
-			/* count Vua header bodies */
2232
+			/* count Via header bodies */
2232 2233
 			for(hf=msg->h_via1; hf!=NULL; hf=hf->next) {
2233 2234
 				if(hf->type==HDR_VIA_T) {
2234 2235
 					for(vb=(via_body_t*)hf->parsed; vb!=NULL; vb=vb->next) {
... ...
@@ -2270,6 +2271,64 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2270 2271
 		LM_DBG("unexpected via index out of range\n");
2271 2272
 		return pv_get_null(msg, param, res);
2272 2273
 	}
2274
+	if((tv.flags == 0) && (tv.ri==HDR_RECORDROUTE_T)) {
2275
+		if(idx<0) {
2276
+			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) {
2280
+					if(parse_rr(hf) == -1) {
2281
+						LM_ERR("failed parsing rr header\n");
2282
+						return pv_get_null(msg, param, res);
2283
+					}
2284
+					for(rrb=(rr_t*)hf->parsed; vb!=NULL; rrb=rrb->next) {
2285
+						n++;
2286
+					}
2287
+				}
2288
+			}
2289
+
2290
+			idx = -idx;
2291
+			if(idx>n) {
2292
+				LM_DBG("index out of rr headers range\n");
2293
+				return pv_get_null(msg, param, res);
2294
+			}
2295
+			idx = n - idx;
2296
+		}
2297
+		if(idx==0) {
2298
+			if(parse_rr(msg->record_route) == -1) {
2299
+				LM_ERR("failed parsing rr header\n");
2300
+				return pv_get_null(msg, param, res);
2301
+			}
2302
+			rrb = (rr_t*)(msg->record_route->parsed);
2303
+			sval.s = rrb->nameaddr.name.s;
2304
+			sval.len = rrb->len;
2305
+			trim(&sval);
2306
+			res->rs = sval;
2307
+			return 0;
2308
+		}
2309
+		n=0;
2310
+		for(hf=msg->record_route; hf!=NULL; hf=hf->next) {
2311
+			if(hf->type==HDR_RECORDROUTE_T) {
2312
+				if(parse_rr(hf) == -1) {
2313
+					LM_ERR("failed parsing rr header\n");
2314
+					return pv_get_null(msg, param, res);
2315
+				}
2316
+				for(rrb=(rr_t*)hf->parsed; vb!=NULL; rrb=rrb->next) {
2317
+					if(n==idx) {
2318
+						sval.s = rrb->nameaddr.name.s;
2319
+						sval.len = rrb->len;
2320
+						trim(&sval);
2321
+						res->rs = sval;
2322
+						return 0;
2323
+					}
2324
+					n++;
2325
+				}
2326
+			}
2327
+		}
2328
+		LM_DBG("unexpected record-route index out of range\n");
2329
+		return pv_get_null(msg, param, res);
2330
+	}
2331
+
2273 2332
 	return pv_get_hdr_helper(msg, param, res, &tv, idx, idxf);
2274 2333
 }
2275 2334