[sr-dev] git:master:62a08366: core: pv api - relocated implementation of $env(name)

Daniel-Constantin Mierla miconda at gmail.com
Tue Dec 22 20:02:11 CET 2020


Module: kamailio
Branch: master
Commit: 62a08366b4424578ffd9a9f42855ce5f4c710668
URL: https://github.com/kamailio/kamailio/commit/62a08366b4424578ffd9a9f42855ce5f4c710668

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2020-12-22T19:44:16+01:00

core: pv api - relocated implementation of $env(name)

---

Modified: src/core/pv_core.c

---

Diff:  https://github.com/kamailio/kamailio/commit/62a08366b4424578ffd9a9f42855ce5f4c710668.diff
Patch: https://github.com/kamailio/kamailio/commit/62a08366b4424578ffd9a9f42855ce5f4c710668.patch

---

diff --git a/src/core/pv_core.c b/src/core/pv_core.c
index 3279a0a22a..0998aa18a8 100644
--- a/src/core/pv_core.c
+++ b/src/core/pv_core.c
@@ -24,17 +24,25 @@
  * Module: \ref core
  */
 
+#include <stdlib.h>
+
 #include "pv_core.h"
 #include "pvar.h"
 #include "str.h"
+#include "mem/pkg.h"
 
 static int pv_get_retcode(struct sip_msg*, pv_param_t*, pv_value_t*);
+static int pv_parse_env_name(pv_spec_p sp, str *in);
+static int pv_get_env(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
+
 
 static pv_export_t core_pvs[] = {
 	/* return code, various synonims */
 	{ STR_STATIC_INIT("?"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
 	{ STR_STATIC_INIT("rc"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
 	{ STR_STATIC_INIT("retcode"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
+	{ STR_STATIC_INIT("env"), PVT_OTHER, pv_get_env, 0,
+		pv_parse_env_name, 0, 0, 0},
 
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
 };
@@ -52,6 +60,43 @@ static int pv_get_retcode(struct sip_msg* msg, pv_param_t* p, pv_value_t* res)
 }
 
 
+static int pv_parse_env_name(pv_spec_p sp, str *in)
+{
+	char *csname;
+
+	if(in->s==NULL || in->len<=0)
+		return -1;
+
+	csname = pkg_malloc(in->len + 1);
+
+	if (csname == NULL) {
+		LM_ERR("no more pkg memory");
+		return -1;
+	}
+
+	memcpy(csname, in->s, in->len);
+	csname[in->len] = '\0';
+
+	sp->pvp.pvn.u.dname = (void*)csname;
+	sp->pvp.pvn.type = PV_NAME_OTHER;
+	return 0;
+}
+
+static int pv_get_env(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
+{
+	char *val;
+	char *csname = (char *) param->pvn.u.dname;
+
+	if (csname) {
+		val = getenv(csname);
+
+		if (val) {
+			return pv_get_strzval(msg, param, res, val);
+		}
+	}
+	return pv_get_null(msg, param, res);
+}
+
 
 /**
  * register built-in core pvars.




More information about the sr-dev mailing list