static void free_old_route(struct sip_route *route);
static void list_route(struct sip_route *route);
static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards, int resp);
+static int build_path(struct sip_pvt *p, struct sip_peer *peer, struct sip_request *req, char *pathbuf);
+static int copy_route(struct sip_route **dst, const struct sip_route *src);
static enum check_auth_result register_verify(struct sip_pvt *p, struct ast_sockaddr *addr,
struct sip_request *req, const char *uri);
static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
static int peer_ipcmp_cb_full(void *obj, void *arg, void *data, int flags);
/* Realtime device support */
-static void realtime_update_peer(const char *peername, struct ast_sockaddr *addr, const char *username, const char *fullcontact, const char *useragent, int expirey, unsigned short deprecated_username, int lastms);
+static void realtime_update_peer(const char *peername, struct ast_sockaddr *addr, const char *username, const char *fullcontact, const char *useragent, int expirey, unsigned short deprecated_username, int lastms, const char *path);
static void update_peer(struct sip_peer *p, int expire);
static struct ast_variable *get_insecure_variable_from_config(struct ast_config *config);
static const char *get_name_from_variable(const struct ast_variable *var);
static int add_rpid(struct sip_request *req, struct sip_pvt *p);
static int add_vidupdate(struct sip_request *req);
static void add_route(struct sip_request *req, struct sip_route *route);
+static void make_route_list(struct sip_route *route, char *r, int rem);
static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
that name and store that in the "regserver" field in the sippeers
table to facilitate multi-server setups.
*/
-static void realtime_update_peer(const char *peername, struct ast_sockaddr *addr, const char *defaultuser, const char *fullcontact, const char *useragent, int expirey, unsigned short deprecated_username, int lastms)
+static void realtime_update_peer(const char *peername, struct ast_sockaddr *addr, const char *defaultuser, const char *fullcontact, const char *useragent, int expirey, unsigned short deprecated_username, int lastms, const char *path)
{
char port[10];
char ipaddr[INET6_ADDRSTRLEN];
ast_copy_string(ipaddr, ast_sockaddr_isnull(addr) ? "" : ast_sockaddr_stringify_addr(addr), sizeof(ipaddr));
ast_copy_string(port, ast_sockaddr_port(addr) ? ast_sockaddr_stringify_port(addr) : "", sizeof(port));
- if (ast_strlen_zero(sysname)) /* No system name, disable this */
+ if (ast_strlen_zero(sysname)) { /* No system name, disable this */
sysname = NULL;
- else if (sip_cfg.rtsave_sysname)
+ } else if (sip_cfg.rtsave_sysname) {
syslabel = "regserver";
+ }
/* XXX IMPORTANT: Anytime you add a new parameter to be updated, you
* must also add it to contrib/scripts/asterisk.ldap-schema,
* and to configs/res_ldap.conf.sample as described in
* bugs 15156 and 15895
*/
- if (fc) {
- ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
- "port", port, "regseconds", regseconds,
- deprecated_username ? "username" : "defaultuser", defaultuser,
- "useragent", useragent, "lastms", str_lastms,
- fc, fullcontact, syslabel, sysname, SENTINEL); /* note fc and syslabel _can_ be NULL */
+
+ /* This is ugly, we need something better ;-) */
+ if (sip_cfg.rtsave_path) {
+ if (fc) {
+ ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
+ "port", port, "regseconds", regseconds,
+ deprecated_username ? "username" : "defaultuser", defaultuser,
+ "useragent", useragent, "lastms", str_lastms,
+ "path", path, /* Path data can be NULL */
+ fc, fullcontact, syslabel, sysname, SENTINEL); /* note fc and syslabel _can_ be NULL */
+ } else {
+ ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
+ "port", port, "regseconds", regseconds,
+ "useragent", useragent, "lastms", str_lastms,
+ deprecated_username ? "username" : "defaultuser", defaultuser,
+ "path", path, /* Path data can be NULL */
+ syslabel, sysname, SENTINEL); /* note syslabel _can_ be NULL */
+ }
} else {
- ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
- "port", port, "regseconds", regseconds,
- "useragent", useragent, "lastms", str_lastms,
- deprecated_username ? "username" : "defaultuser", defaultuser,
- syslabel, sysname, SENTINEL); /* note syslabel _can_ be NULL */
+ if (fc) {
+ ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
+ "port", port, "regseconds", regseconds,
+ deprecated_username ? "username" : "defaultuser", defaultuser,
+ "useragent", useragent, "lastms", str_lastms,
+ fc, fullcontact, syslabel, sysname, SENTINEL); /* note fc and syslabel _can_ be NULL */
+ } else {
+ ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
+ "port", port, "regseconds", regseconds,
+ "useragent", useragent, "lastms", str_lastms,
+ deprecated_username ? "username" : "defaultuser", defaultuser,
+ syslabel, sysname, SENTINEL); /* note syslabel _can_ be NULL */
+ }
}
}
ast_variables_destroy(peer->chanvars);
peer->chanvars = NULL;
}
+ if (peer->path) {
+ free_old_route(peer->path);
+ peer->path = NULL;
+ }
register_peer_exten(peer, FALSE);
ast_free_acl_list(peer->acl);
int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
if (sip_cfg.peer_rtupdate &&
(p->is_realtime || rtcachefriends)) {
- realtime_update_peer(p->name, &p->addr, p->username, p->fullcontact, p->useragent, expire, p->deprecated_username, p->lastms);
+ char path[SIPBUFSIZE * 2];
+ make_route_list(p->path, path, sizeof(path));
+ realtime_update_peer(p->name, &p->addr, p->username, p->fullcontact, p->useragent, expire, p->deprecated_username, p->lastms, path);
}
}
return 0;
}
+static int __set_address_from_contact(const char *fullcontact, struct ast_sockaddr *addr, int tcp);
+
/*! \brief Create address structure from peer reference.
* This function copies data from peer to the dialog, so we don't have to look up the peer
* again from memory or database during the life time of the dialog.
dialog->rtptimeout = peer->rtptimeout;
dialog->rtpholdtimeout = peer->rtpholdtimeout;
dialog->rtpkeepalive = peer->rtpkeepalive;
+ copy_route(&dialog->route, peer->path);
+ if (dialog->route) {
+ /* Parse SIP URI of first route-set hop and use it as target address */
+ __set_address_from_contact(dialog->route->hop, &dialog->sa, dialog->socket.type == SIP_TRANSPORT_TLS ? 1 : 0);
+ }
+
if (dialog_initialize_rtp(dialog)) {
return -1;
}
ast_clear_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_T38);
if (p->options->transfer) {
- char buf[SIPBUFSIZE/2];
+ char buf[SIPBUFSIZE / 2];
if (referer) {
if (sipdebug)
* is supported for this dialog. */
static int add_supported(struct sip_pvt *pvt, struct sip_request *req)
{
+ char supported_value[SIPBUFSIZE];
int res;
- if (st_get_mode(pvt, 0) != SESSION_TIMER_MODE_REFUSE) {
- res = add_header(req, "Supported", "replaces, timer");
- } else {
- res = add_header(req, "Supported", "replaces");
- }
+
+ sprintf(supported_value, "replaces%s%s",
+ (st_get_mode(pvt, 0) != SESSION_TIMER_MODE_REFUSE) ? ", timer" : "",
+ ast_test_flag(&pvt->flags[0], SIP_USEPATH) ? ", path" : "");
+ res = add_header(req, "Supported", supported_value);
+
return res;
}
/*! \brief Add route header into request per learned route */
static void add_route(struct sip_request *req, struct sip_route *route)
{
- char r[SIPBUFSIZE*2], *p;
- int n, rem = sizeof(r);
+ char r[SIPBUFSIZE * 2];
if (!route)
return;
+ make_route_list(route, r, sizeof(r));
+ add_header(req, "Route", r);
+}
+
+/*! \brief Make the comma separated list of route headers from the route list */
+static void make_route_list(struct sip_route *route, char *r, int rem)
+{
+ char *p;
+ int n;
+
p = r;
for (;route ; route = route->next) {
n = strlen(route->hop);
rem -= (n+2);
}
*p = '\0';
- add_header(req, "Route", r);
}
/*! \brief Set destination from SIP URI
snprintf(contact, sizeof(contact), "%s%s%s;expires=%d", brackets ? "" : "<", contact_uri, brackets ? "" : ">", p->expiry);
add_header(resp, "Contact", contact); /* Not when we unregister */
}
+ if (p->method == SIP_REGISTER && ast_test_flag(&p->flags[0], SIP_USEPATH)) {
+ copy_header(resp, req, "Path");
+ }
} else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
add_header(resp, "Contact", p->our_contact);
}
add_header(&req, "To", to);
add_header(&req, "Call-ID", p->callid);
add_header(&req, "CSeq", tmp);
+ add_supported(p, &req);
if (!ast_strlen_zero(global_useragent))
add_header(&req, "User-Agent", global_useragent);
ast_update_realtime(tablename, "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "regserver", "", "useragent", "", "lastms", "0", SENTINEL);
} else {
ast_db_del("SIP/Registry", peer->name);
+ ast_db_del("SIP/RegistryPath", peer->name);
ast_db_del("SIP/PeerMethods", peer->name);
}
}
static void reg_source_db(struct sip_peer *peer)
{
char data[256];
+ char path[SIPBUFSIZE * 2];
struct ast_sockaddr sa;
int expire;
char full_addr[128];
sip_unref_peer(peer, "remove registration ref"),
sip_ref_peer(peer, "add registration ref"));
register_peer_exten(peer, TRUE);
+ if (!ast_db_get("SIP/RegistryPath", peer->name, path, sizeof(path))) {
+ build_path(NULL, peer, NULL, path);
+ }
}
/*! \brief Save contact header for 200 OK on INVITE */
}
}
pvt->expiry = expire;
+ if (!build_path(pvt, peer, req, NULL)) {
+ /* Tell the dialog to use the Path header in the response */
+ ast_set2_flag(&pvt->flags[0], 1, SIP_USEPATH);
+ }
snprintf(data, sizeof(data), "%s:%d:%s:%s", ast_sockaddr_stringify(&peer->addr),
expire, peer->username, peer->fullcontact);
/* We might not immediately be able to reconnect via TCP, but try caching it anyhow */
- if (!peer->rt_fromcontact || !sip_cfg.peer_rtupdate)
+ if (!peer->rt_fromcontact || !sip_cfg.peer_rtupdate) {
+ char path[SIPBUFSIZE * 2];
+ if (peer->path) {
+ make_route_list(peer->path, path, sizeof(path));
+ ast_db_put("SIP/RegistryPath", peer->name, path);
+ }
ast_db_put("SIP/Registry", peer->name, data);
+ }
manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\nAddress: %s\r\n", peer->name, ast_sockaddr_stringify(&peer->addr));
/* Is this a new IP address for us? */
static void list_route(struct sip_route *route)
{
if (!route) {
- ast_verbose("list_route: no route\n");
+ ast_verbose("list_route: no route/path\n");
} else {
for (;route; route = route->next)
- ast_verbose("list_route: hop: <%s>\n", route->hop);
+ ast_verbose("list_route: route/path hop: <%s>\n", route->hop);
}
}
}
}
+/*! \internal \brief Create a new route
+ * \retval NULL on error
+ * \retval sip_route on success
+ */
+static struct sip_route *create_route(const char *hop, struct sip_route *prev)
+{
+ struct sip_route *route;
+ int len;
+
+ if (ast_strlen_zero(hop)) {
+ return NULL;
+ }
+ len = strlen(hop) + 1;
+
+ /* ast_calloc is not needed because all fields are initialized in
+ * this block */
+ route = ast_malloc(sizeof(*route) + len);
+ if (!route) {
+ return NULL;
+ }
+ ast_copy_string(route->hop, hop, len);
+
+ route->next = NULL;
+ if (prev) {
+ prev->next = route;
+ }
+ return route;
+}
+
+/*! \internal \brief copy route-set
+ * \retval non-zero on failure
+ * \retval 0 on success
+ */
+static int copy_route(struct sip_route **dst, const struct sip_route *src)
+{
+ struct sip_route *thishop, *head, *tail;
+
+ /* Build a tailq, then assign it to **d when done. */
+ head = NULL;
+ tail = head;
+ for (; src; src = src->next) {
+ thishop = create_route(src->hop, tail);
+ if (!thishop) {
+ return -1;
+ }
+ if (!head) {
+ head = thishop;
+ }
+ tail = thishop;
+
+ ast_debug(2, "copy_route: copied hop: <%s>\n", thishop->hop);
+ }
+ *dst = head;
+
+ return 0;
+}
+
+/*! \brief Build route list from Path header
+ * RFC 3327 requires that the Path header contains SIP URIs with lr paramter.
+ * Thus, we do not care about strict routing SIP routers
+ */
+static int build_path(struct sip_pvt *p, struct sip_peer *peer, struct sip_request *req, char *pathbuf)
+{
+ struct sip_route *thishop, *head, *tail;
+ int start = 0;
+ int len;
+ char *pr;
+
+ if (peer->path) {
+ free_old_route(peer->path);
+ peer->path = NULL;
+ }
+
+ if (!ast_test_flag(&peer->flags[0], SIP_USEPATH)) {
+ ast_debug(2, "build_path: do not use Path headers\n");
+ return -1;
+ }
+ ast_debug(2, "build_path: try to build pre-loaded route-set by parsing Path headers\n");
+
+ /* Build a tailq, then assign it to peer->path when done. */
+ head = NULL;
+ tail = head;
+ /* 1st we pass through all the hops in any Path headers */
+ for (;;) {
+ /* Either loop over the request's Path headers or parse the buffer */
+ if (req) {
+ pr = ast_strdupa(__get_header(req, "Path", &start));
+ if (*pr == '\0') {
+ break;
+ }
+ } else if (pathbuf) {
+ if (start == 0) {
+ pr = ast_strdupa(pathbuf);
+ start++;
+ } else {
+ break;
+ }
+ } else {
+ break;
+ }
+ for (; (pr = strchr(pr, '<')) ; pr += (len + 1)) {
+ /* Parse out each route entry */
+ ++pr;
+ len = strcspn(pr, ">");
+ *(pr + len) = '\0';
+ thishop = create_route(pr, tail);
+ if (!thishop) {
+ return -1;
+ }
+
+ if (!head) {
+ head = thishop;
+ }
+ tail = thishop;
+ ast_debug(2, "build_path: Path hop: <%s>\n", thishop->hop);
+ }
+ }
+
+ /* Store as new route */
+ peer->path = head;
+
+ /* For debugging dump what we ended up with */
+ if (p && sip_debug_test_pvt(p)) {
+ list_route(peer->path);
+ }
+ return 0;
+}
+
/*! \brief builds the sip_pvt's nonce field which is used for the authentication
* challenge. When forceupdate is not set, the nonce is only updated if
* the current one is stale. In this case, a stalenonce is one which
ast_cli(fd, " Ign SDP ver : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
ast_cli(fd, " Trust RPID : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_TRUSTRPID)));
ast_cli(fd, " Send RPID : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_SENDRPID)));
+ ast_cli(fd, " Path support : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_USEPATH)));
+ ast_cli(fd, " Path : ");
+ if (!peer->path) {
+ ast_cli(fd, "N/A\n");
+ } else {
+ struct sip_route *r = peer->path;
+ int first = 1;
+ while (r) {
+ ast_cli(fd, "%s<%s>", first ? "" : ", ", r->hop);
+ first = 0;
+ r = r->next;
+ }
+ ast_cli(fd, "\n");
+ }
ast_cli(fd, " Subscriptions: %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
ast_cli(fd, " Overlap dial : %s\n", allowoverlap2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP)));
if (peer->outboundproxy)
ast_cli(a->fd, " Allow promisc. redir: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_PROMISCREDIR)));
ast_cli(a->fd, " Enable call counters: %s\n", AST_CLI_YESNO(global_callcounter));
ast_cli(a->fd, " SIP domain support: %s\n", AST_CLI_YESNO(!AST_LIST_EMPTY(&domain_list)));
+ ast_cli(a->fd, " Path support : %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_USEPATH)));
ast_cli(a->fd, " Realm. auth: %s\n", AST_CLI_YESNO(credentials != NULL));
if (credentials) {
struct sip_auth *auth;
ast_cli(a->fd, " Update: %s\n", AST_CLI_YESNO(sip_cfg.peer_rtupdate));
ast_cli(a->fd, " Ignore Reg. Expire: %s\n", AST_CLI_YESNO(sip_cfg.ignore_regexpire));
ast_cli(a->fd, " Save sys. name: %s\n", AST_CLI_YESNO(sip_cfg.rtsave_sysname));
+ ast_cli(a->fd, " Save path header: %s\n", AST_CLI_YESNO(sip_cfg.rtsave_path));
ast_cli(a->fd, " Auto Clear: %d (%s)\n", sip_cfg.rtautoclear, ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR) ? "Enabled" : "Disabled");
}
ast_cli(a->fd, "\n----\n");
{
int expires, expires_ms;
struct sip_registry *r;
- r=p->registry;
+ r = p->registry;
switch (resp) {
case 401: /* Unauthorized */
ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
+ copy_route(&p->route, peer->path);
+ if (p->route) {
+ /* Parse SIP URI of first route-set hop and use it as target address */
+ __set_address_from_contact(p->route->hop, &p->sa, p->socket.type == SIP_TRANSPORT_TLS ? 1 : 0);
+ }
/* Send OPTIONs to peer's fullcontact */
if (!ast_strlen_zero(peer->fullcontact)) {
if (!strcasecmp(v->name, "trustrpid")) {
ast_set_flag(&mask[0], SIP_TRUSTRPID);
ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
+ } else if (!strcasecmp(v->name, "supportpath")) {
+ ast_set_flag(&mask[0], SIP_USEPATH);
+ ast_set2_flag(&flags[0], ast_true(v->value), SIP_USEPATH);
} else if (!strcasecmp(v->name, "sendrpid")) {
ast_set_flag(&mask[0], SIP_SENDRPID);
if (!strcasecmp(v->value, "pai")) {
ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
} else if (!strcasecmp(v->name, "rtsavesysname")) {
sip_cfg.rtsave_sysname = ast_true(v->value);
+ } else if (!strcasecmp(v->name, "rtsavepath")) {
+ sip_cfg.rtsave_path = ast_true(v->value);
} else if (!strcasecmp(v->name, "rtupdate")) {
sip_cfg.peer_rtupdate = ast_true(v->value);
} else if (!strcasecmp(v->name, "ignoreregexpire")) {