# 2005-09-03 09:47 +0100 # # This patch adds support for non-numeric callerids being sent on over # SIP (instead of being changed to "asterisk"), and for calling using any # peer as a SIP proxy where the outbound domain is unknown in sip.conf. # # Dial(SIP/peer![extension@]host) will prepare a connection to 'peer' and # then continue as if it was connected to 'host' but still using all of # the settings of 'peer'. # # -- # Simon Arlott # http://simon.arlott.org/ diff -U4 -r asterisk-1.2.0-beta1/channels/chan_sip.c asterisk-1.2.0-beta1+byte/channels/chan_sip.c --- asterisk-1.2.0-beta1/channels/chan_sip.c 2005-08-26 21:04:28.000000000 +0100 +++ asterisk-1.2.0-beta1+byte/channels/chan_sip.c 2005-09-03 08:42:54.776277111 +0100 @@ -103,9 +103,9 @@ #define MAX(a,b) ((a) > (b) ? (a) : (b)) #endif #define CALLERID_UNKNOWN "Unknown" - +#define MAX_CIDNUM_LEN 79 /* It's used in the Contact header too so it must fit. */ #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */ #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */ @@ -4356,10 +4356,12 @@ char to[256]; char tmp[80]; char iabuf[INET_ADDRSTRLEN]; char *l = default_callerid, *n=NULL; + char cidnum[MAX_CIDNUM_LEN+1]; int x; char urioptions[256]=""; + int pos=0; if (ast_test_flag(p, SIP_USEREQPHONE)) { char onlydigits = 1; x=0; @@ -4390,10 +4392,13 @@ if (p->owner) { l = p->owner->cid.cid_num; n = p->owner->cid.cid_name; } +/* if (!l || (!ast_isphonenumber(l) && default_callerid[0])) l = default_callerid; +*/ + if (!l) l = CALLERID_UNKNOWN; /* if user want's his callerid restricted */ if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) { l = CALLERID_UNKNOWN; n = l; @@ -4411,12 +4416,41 @@ n = p->fromname; else /* Save for any further attempts */ ast_copy_string(p->fromname, n, sizeof(p->fromname)); + if (!ast_isphonenumber(l)) { + static char hex[] = "0123456789ABCDEF"; + int i; + for (i=0; i= 65 && l[i] <= 90) //A-Z + || (l[i] >= 97 && l[i] <= 122) //a-z + || (l[i] >= 48 && l[i] <= 57) //0-9 + || (l[i] == 35) //# + || (l[i] == 42) //* + || (l[i] == 43) //+ + || (l[i] == 45) //- + || (l[i] == 95) //_ + ) { // valid char + cidnum[pos]=l[i]; + pos++; + if (pos == MAX_CIDNUM_LEN) break; + } else { // invalid char + if (pos+3 > MAX_CIDNUM_LEN) break; + cidnum[pos] = '%'; + cidnum[pos+1] = hex[l[i] >> 4]; + cidnum[pos+2] = hex[l[i] & 0x0f]; + pos+=3; + if (pos == MAX_CIDNUM_LEN) break; + } + } + cidnum[pos]='\0'; + } + if ((ourport != 5060) && ast_strlen_zero(p->fromdomain)) /* Needs to be 5060 */ - snprintf(from, sizeof(from), "\"%s\" ;tag=as%08x", n, l, ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain, ourport, p->tag); + snprintf(from, sizeof(from), "\"%s\" ;tag=as%08x", n, (pos == 0 ? l : cidnum), ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain, ourport, p->tag); else - snprintf(from, sizeof(from), "\"%s\" ;tag=as%08x", n, l, ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain, p->tag); + snprintf(from, sizeof(from), "\"%s\" ;tag=as%08x", n, (pos == 0 ? l : cidnum), ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain, p->tag); /* If we're calling a registred SIP peer, use the fullcontact to dial to the peer */ if (!ast_strlen_zero(p->fullcontact)) { /* If we have full contact, trust it */ @@ -4448,9 +4482,9 @@ add_header(req, "Via", p->via); /* SLD: FIXME?: do Route: here too? I think not cos this is the first request. * OTOH, then we won't have anything in p->route anyway */ add_header(req, "From", from); - ast_copy_string(p->exten, l, sizeof(p->exten)); + ast_copy_string(p->exten, (pos == 0 ? l : cidnum), sizeof(p->exten)); build_contact(p); add_header(req, "To", to); add_header(req, "Contact", p->our_contact); add_header(req, "Call-ID", p->callid); @@ -10437,8 +10471,9 @@ struct ast_channel *tmpc = NULL; char *ext, *host; char tmp[256] = ""; char *dest = data; + char *fakehost = NULL; oldformat = format; format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1); if (!format) { @@ -10451,33 +10486,51 @@ return NULL; } ast_copy_string(tmp, dest, sizeof(tmp)); - host = strchr(tmp, '@'); - if (host) { - *host = '\0'; - host++; - ext = tmp; - } else { - ext = strchr(tmp, '/'); - if (ext) { - *ext++ = '\0'; - host = tmp; + ext = strchr(tmp, '!'); + if (ext) { + *ext = '\0'; + ext++; + fakehost = tmp; + host = strchr(ext, '@'); + if (host) { + *host++ = '\0'; } else { - host = tmp; + host = ext; ext = NULL; } + } else { + host = strchr(tmp, '@'); + if (host) { + *host = '\0'; + host++; + ext = tmp; + } else { + ext = strchr(tmp, '/'); + if (ext) { + *ext++ = '\0'; + host = tmp; + } + else { + host = tmp; + ext = NULL; + } + } } + if (!fakehost && host) fakehost = host; /* Assign a default capability */ p->capability = global_capability; - if (create_addr(p, host)) { + if (create_addr(p, fakehost)) { *cause = AST_CAUSE_UNREGISTERED; sip_destroy(p); return NULL; } + ast_copy_string(p->tohost, host, sizeof(p->tohost)); + if (ast_strlen_zero(p->peername) && ext) ast_copy_string(p->peername, ext, sizeof(p->peername)); /* Recalculate our side, and recalculate Call ID */ if (ast_sip_ouraddrfor(&p->sa.sin_addr,&p->ourip))