#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
+/* Arguments for find_peer */
+#define FINDALLDEVICES FALSE
+#define FINDONLYUSERS TRUE
+
#define SIPBUFSIZE 512 /*!< Buffer size for many operations */
struct sip_peer *p = NULL;
struct sip_peer tmp_peer;
+ /* Inline function to assist finding peers by name only */
auto int find_by_name(void *obj, void *arg, int flags);
int find_by_name(void *obj, void *arg, int flags)
{
struct sip_peer *search = obj, *match = arg;
- if (strcasecmp(search->name, match->name)) {
+ /* Usernames in SIP uri's are case sensitive. Domains are not */
+ if (strcmp(search->name, match->name)) {
return 0;
}
+ /* If we're only looking for name matches, we should avoid type=peer devices,
+ since these should not match on any name-based search */
if (forcenamematch && search->onlymatchonip) {
return 0;
}
dialog->sa.sin_family = AF_INET;
dialog->timer_t1 = global_t1; /* Default SIP retransmission timer T1 (RFC 3261) */
dialog->timer_b = global_timer_b; /* Default SIP transaction timer B (RFC 3261) */
- peer = find_peer(peername, NULL, TRUE, TRUE, FALSE);
+ peer = find_peer(peername, NULL, TRUE, FINDALLDEVICES, FALSE);
if (peer) {
int res;
ast_copy_string(name, fup->username, sizeof(name));
/* Check the list of devices */
- if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, TRUE, FALSE, FALSE) ) ) { /* Try to find peer */
+ if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, TRUE, FINDALLDEVICES, FALSE) ) ) { /* Try to find peer */
inuse = &p->inUse;
call_limit = &p->call_limit;
inringing = &p->inRinging;
/*! \brief Change onhold state of a peer using a pvt structure */
static void sip_peer_hold(struct sip_pvt *p, int hold)
{
- struct sip_peer *peer = find_peer(p->peername, NULL, 1, TRUE, FALSE);
+ struct sip_peer *peer = find_peer(p->peername, NULL, 1, FINDALLDEVICES, FALSE);
if (!peer)
return;
ast_string_field_set(p, exten, name);
build_contact(p);
- peer = find_peer(name, NULL, TRUE, TRUE, FALSE);
+ peer = find_peer(name, NULL, TRUE, FINDALLDEVICES, FALSE);
if (!(peer && ast_apply_ha(peer->ha, sin))) {
/* Peer fails ACL check */
if (peer) {
}
}
-/*! \brief Validate peer authentication */
+/*! \brief Validate device authentication */
static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
struct sip_request *req, int sipmethod, struct sockaddr_in *sin,
struct sip_peer **authpeer,
int debug=sip_debug_test_addr(sin);
struct sip_peer *peer;
- /* For subscribes, match on device name only; for other methods,
- * match on IP address-port of the incoming request.
- */
- peer = (sipmethod == SIP_SUBSCRIBE) ? find_peer(of, NULL, TRUE, FALSE, FALSE) : find_peer(NULL, &p->recv, TRUE, FALSE, FALSE);
+ if (sipmethod == SIP_SUBSCRIBE) {
+ /* For subscribes, match on device name only; for other methods,
+ * match on IP address-port of the incoming request.
+ */
+ peer = find_peer(of, NULL, TRUE, FINDALLDEVICES, FALSE);
+ } else {
+ /* First find devices based on username (avoid all type=peer's) */
+ peer = find_peer(of, NULL, TRUE, FINDONLYUSERS, FALSE);
+
+ /* Then find devices based on IP */
+ if (!peer) {
+ find_peer(NULL, &p->recv, TRUE, FINDALLDEVICES, FALSE);
+ }
+ }
if (!peer) {
if (debug)
return CLI_SHOWUSAGE;
load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
- if ((peer = find_peer(argv[3], NULL, load_realtime, FALSE, FALSE))) {
+ if ((peer = find_peer(argv[3], NULL, load_realtime, FINDALLDEVICES, FALSE))) {
sip_poke_peer(peer, 1);
unref_peer(peer, "qualify: done with peer");
} else if (type == 0) {
return CLI_SHOWUSAGE;
load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
- peer = find_peer(argv[3], NULL, load_realtime, TRUE, FALSE);
+ peer = find_peer(argv[3], NULL, load_realtime, FINDALLDEVICES, FALSE);
if (s) { /* Manager */
if (peer) {
const char *id = astman_get_header(m, "ActionID");
if (a->argc != 3)
return CLI_SHOWUSAGE;
- if ((peer = find_peer(a->argv[2], NULL, load_realtime, TRUE, TRUE))) {
+ if ((peer = find_peer(a->argv[2], NULL, load_realtime, FINDALLDEVICES, TRUE))) {
if (peer->expire > 0) {
expire_register(peer);
ast_cli(a->fd, "Unregistered peer \'%s\'\n\n", a->argv[2]);
/*! \brief Turn on SIP debugging for a given peer */
static char *sip_do_debug_peer(int fd, char *arg)
{
- struct sip_peer *peer = find_peer(arg, NULL, TRUE, TRUE, FALSE);
+ struct sip_peer *peer = find_peer(arg, NULL, TRUE, FINDALLDEVICES, FALSE);
if (!peer)
ast_cli(fd, "No such peer '%s'\n", arg);
else if (peer->addr.sin_addr.s_addr == 0)
else
colname = "ip";
- if (!(peer = find_peer(data, NULL, TRUE, TRUE, FALSE)))
+ if (!(peer = find_peer(data, NULL, TRUE, FINDALLDEVICES, FALSE)))
return -1;
if (!strcasecmp(colname, "ip")) {
sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
return 0;
} else {
- /* This is a spiral. What we need to do is to just change the outgoing INVITE
+ /*! This is a spiral. What we need to do is to just change the outgoing INVITE
* so that it now routes to the new Request URI. Since we created the INVITE ourselves
* that should be all we need to do.
+ *
+ * \todo XXX This needs to be reviewed. YOu don't change the request URI really, you route the packet
+ * correctly instead...
*/
char *uri = ast_strdupa(req->rlPart2);
char *at = strchr(uri, '@');
if (p->stimer->st_cached_max_se) {
return p->stimer->st_cached_max_se;
} else if (p->peername) {
- struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE, FALSE);
+ struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDALLDEVICES, FALSE);
if (pp) {
p->stimer->st_cached_max_se = pp->stimer.st_max_se;
unref_peer(pp, "unref peer pointer from find_peer call in st_get_se");
if (p->stimer->st_cached_min_se) {
return p->stimer->st_cached_min_se;
} else if (p->peername) {
- struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE, FALSE);
+ struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDALLDEVICES, FALSE);
if (pp) {
p->stimer->st_cached_min_se = pp->stimer.st_min_se;
unref_peer(pp, "unref peer pointer from find_peer call in st_get_se (2)");
return p->stimer->st_cached_ref;
if (p->peername) {
- struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE, FALSE);
+ struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDALLDEVICES, FALSE);
if (pp) {
p->stimer->st_cached_ref = pp->stimer.st_ref;
unref_peer(pp, "unref peer pointer from find_peer call in st_get_refresher");
return p->stimer->st_cached_mode;
if (p->peername) {
- struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE, FALSE);
+ struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDALLDEVICES, FALSE);
if (pp) {
p->stimer->st_cached_mode = pp->stimer.st_mode_oper;
unref_peer(pp, "unref peer pointer from find_peer call in st_get_mode");
* load it BACK into memory, thus defeating the point of trying to clear dead
* hosts out of memory.
*/
- if ((p = find_peer(host, NULL, FALSE, TRUE, TRUE))) {
+ if ((p = find_peer(host, NULL, FALSE, FINDALLDEVICES, TRUE))) {
if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
/* we have an address for the peer */