Skip to content

Instantly share code, notes, and snippets.

@alfredh
Created July 14, 2018 22:11
Show Gist options
  • Save alfredh/2577a5dadbde35a9ba47d96696ddb31d to your computer and use it in GitHub Desktop.
Save alfredh/2577a5dadbde35a9ba47d96696ddb31d to your computer and use it in GitHub Desktop.
diff --git a/src/ua.c b/src/ua.c
index ef24b9b..bef9479 100644
--- a/src/ua.c
+++ b/src/ua.c
@@ -30,6 +30,8 @@ struct ua {
int af_media; /**< Preferred Address Family for media */
enum presence_status my_status; /**< Presence Status */
bool catchall; /**< Catch all inbound requests */
+ struct list hdr_filter; /**< Filter for incoming headers */
+ struct list *custom_hdrs; /**< List of outgoing headers */
};
struct ua_eh {
@@ -38,6 +40,11 @@ struct ua_eh {
void *arg;
};
+struct ua_xhdr_filter {
+ struct le le;
+ char *hdr_name;
+};
+
static struct {
struct config_sip *cfg; /**< SIP configuration */
struct list ual; /**< List of User-Agents (struct ua) */
@@ -543,6 +550,8 @@ static void ua_destructor(void *arg)
if (list_isempty(&uag.ual)) {
sip_close(uag.sip, false);
}
+
+ list_flush(&ua->hdr_filter);
}
@@ -831,6 +840,9 @@ int ua_connect(struct ua *ua, struct call **callp,
pl.p = (char *)dialbuf->buf;
pl.l = dialbuf->end;
+ if (ua->custom_hdrs)
+ call_set_custom_hdrs(call, ua->custom_hdrs);
+
err = call_connect(call, &pl);
if (err)
@@ -838,6 +850,8 @@ int ua_connect(struct ua *ua, struct call **callp,
else if (callp)
*callp = call;
+ ua_set_custom_hdrs(ua, NULL);
+
out:
mem_deref(dialbuf);
@@ -1309,6 +1323,35 @@ static void sipsess_conn_handler(const struct sip_msg *msg, void *arg)
goto error;
}
+ if (!list_isempty(&ua->hdr_filter)) {
+ struct list *hdrs;
+ struct le *le;
+
+ err = custom_hdrs_alloc(&hdrs);
+ if (err)
+ goto error;
+
+ le = list_head(&ua->hdr_filter);
+ while (le) {
+ const struct sip_hdr *hdr_local;
+ const struct ua_xhdr_filter *filter = le->data;
+
+ le = le->next;
+ hdr_local = sip_msg_xhdr_apply(msg, true, filter->hdr_name,
+ NULL, NULL);
+
+ if (hdr_local) {
+ char name[256];
+ pl_strcpy(&hdr_local->name, name, sizeof(name));
+ if (custom_hdrs_add(hdrs, name, "%r", &hdr_local->val))
+ goto error;
+ }
+ }
+
+ call_set_custom_hdrs(call, hdrs);
+ mem_deref(hdrs);
+ }
+
err = call_accept(call, uag.sock, msg);
if (err)
goto error;
@@ -1321,6 +1364,36 @@ static void sipsess_conn_handler(const struct sip_msg *msg, void *arg)
}
+static void ua_xhdr_filter_destructor(void *arg)
+{
+ struct ua_xhdr_filter *filter = arg;
+ mem_deref(filter->hdr_name);
+}
+
+
+int ua_add_xhdr_filter(struct ua *ua, const char *hdr_name)
+{
+ struct ua_xhdr_filter *filter;
+
+ if (!ua)
+ return EINVAL;
+
+ filter = mem_zalloc(sizeof(*filter), ua_xhdr_filter_destructor);
+ if (!filter)
+ goto error;
+
+ if (str_dup(&filter->hdr_name, hdr_name))
+ goto error;
+
+ list_append(&ua->hdr_filter, &filter->le, filter);
+
+ return 0;
+
+ error:
+ return ENOMEM;
+}
+
+
static void net_change_handler(void *arg)
{
(void)arg;
@@ -1963,3 +2036,9 @@ int uag_set_extra_params(const char *eprm)
return 0;
}
+
+
+void ua_set_custom_hdrs(struct ua *ua, struct list *custom_headers)
+{
+ ua->custom_hdrs = custom_headers;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment