|
| 1 | +// RFC3261 20.30 Record-Route / 20.43 Route |
| 2 | +// Record-Route: <sip:p1.example.com;lr> |
| 3 | +// Route: <sip:p1.example.com;lr>,<sip:p2.domain.com;lr> |
| 4 | + |
| 5 | +/* |
| 6 | +Record-Route = "Record-Route" HCOLON rec-route *(COMMA rec-route) |
| 7 | +rec-route = name-addr *( SEMI rr-param ) |
| 8 | +rr-param = generic-param |
| 9 | +Route = "Route" HCOLON route-param *(COMMA route-param) |
| 10 | +route-param = name-addr *( SEMI rr-param ) |
| 11 | +name-addr = [ display-name ] LAQUOT addr-spec RAQUOT |
| 12 | +addr-spec = SIP-URI / SIPS-URI / absoluteURI |
| 13 | +*/ |
| 14 | + |
| 15 | +#include "sip-header.h" |
| 16 | +#include <stdlib.h> |
| 17 | +#include <ctype.h> |
| 18 | + |
| 19 | +int sip_header_route(const char* s, const char* end, struct sip_uri_t* uri) |
| 20 | +{ |
| 21 | + const char* p; |
| 22 | + |
| 23 | + p = (s && s < end) ? strpbrk(s, "<\"") : NULL; |
| 24 | + if (!p || p > end) |
| 25 | + { |
| 26 | + // addr-spec only |
| 27 | + return sip_header_uri(s, end, uri); |
| 28 | + } |
| 29 | + |
| 30 | + if ('"' == *p) |
| 31 | + { |
| 32 | + // name-addr: [ display-name ] |
| 33 | + p = strchr(p + 1, '"'); |
| 34 | + if (!p || p > end) |
| 35 | + return -EINVAL; |
| 36 | + |
| 37 | + // LAQUOT addr-spec RAQUOT |
| 38 | + p = strchr(p + 1, '<'); |
| 39 | + if (!p || p > end) |
| 40 | + return -EINVAL; |
| 41 | + } |
| 42 | + |
| 43 | + if ('<' == *p) |
| 44 | + { |
| 45 | + s = p + 1; |
| 46 | + p = s < end ? strchr(s, '>') : NULL; |
| 47 | + if (!p || p > end) |
| 48 | + return -EINVAL; |
| 49 | + } |
| 50 | + |
| 51 | + return sip_header_uri(s, p, uri); |
| 52 | +} |
| 53 | + |
| 54 | +int sip_header_routes(const char* s, const char* end, struct sip_uris_t* routes) |
| 55 | +{ |
| 56 | + int r; |
| 57 | + const char* p; |
| 58 | + struct sip_uri_t c; |
| 59 | + |
| 60 | + for (r = 0; 0 == r && s && s < end; s = p + 1) |
| 61 | + { |
| 62 | + // filter "," |
| 63 | + p = strpbrk(s, ",\""); |
| 64 | + while (p && p + 1 < end && '"' == *p) |
| 65 | + { |
| 66 | + p = strchr(p + 1, '"'); |
| 67 | + if (p && p < end) |
| 68 | + p = strpbrk(p + 1, ",\""); |
| 69 | + } |
| 70 | + if (!p || p >= end) |
| 71 | + p = end; |
| 72 | + |
| 73 | + r = sip_header_route(s, p, &c); |
| 74 | + if (0 == r) |
| 75 | + r = sip_uris_push(routes, &c); |
| 76 | + } |
| 77 | + |
| 78 | + return r; |
| 79 | +} |
| 80 | + |
| 81 | +// Route: <sip:p1.example.com;lr>,<sip:p2.domain.com;lr> |
| 82 | +int sip_route_write(const struct sip_uri_t* route, char* data, const char* end) |
| 83 | +{ |
| 84 | + int n; |
| 85 | + char* p; |
| 86 | + |
| 87 | + p = data; |
| 88 | + if (p < end) *p++ = '<'; |
| 89 | + n = sip_uri_write(route, p, end); |
| 90 | + if (n < 0) return n; |
| 91 | + p += n; |
| 92 | + if (p < end) *p++ = '>'; |
| 93 | + |
| 94 | + if (p < end) *p = '\0'; |
| 95 | + return (int)(p - data); |
| 96 | +} |
| 97 | + |
| 98 | +#if defined(DEBUG) || defined(_DEBUG) |
| 99 | +void sip_header_route_test(void) |
| 100 | +{ |
| 101 | + char p[1024]; |
| 102 | + const char* s; |
| 103 | + const struct sip_uri_t* c; |
| 104 | + struct sip_uri_t route; |
| 105 | + struct sip_uris_t routes; |
| 106 | + |
| 107 | + sip_uris_init(&routes); |
| 108 | + s = "\"Mr.Watson\" <sip:[email protected]>;q=0.7; expires=3600,\"Mr.Watson\" <mailto:[email protected]> ;q=0.1"; |
| 109 | + assert(0 == sip_header_routes(s, s + strlen(s), &routes) && 2 == sip_uris_count(&routes)); |
| 110 | + c = sip_uris_get(&routes, 0); |
| 111 | + assert( 0 == cstrcmp( &c->scheme, "sip") && 0 == cstrcmp( &c->host, "[email protected]") && 0 == sip_params_count( &c->headers) && 0 == sip_params_count( &c->parameters)); |
| 112 | + c = sip_uris_get(&routes, 1); |
| 113 | + assert( 0 == cstrcmp( &c->scheme, "mailto") && 0 == cstrcmp( &c->host, "[email protected]") && 0 == sip_params_count( &c->headers) && 0 == sip_params_count( &c->parameters)); |
| 114 | + sip_uris_free(&routes); |
| 115 | + |
| 116 | + s = "<sips:[email protected]>;expires=60"; |
| 117 | + assert(0 == sip_header_route(s, s + strlen(s), &route)); |
| 118 | + assert( 0 == cstrcmp( &route. scheme, "sips") && 0 == cstrcmp( &route. host, "[email protected]") && 0 == sip_params_count( &route. headers) && 0 == sip_params_count( &route. parameters)); |
| 119 | + assert(sip_route_write(&route, p, p + sizeof(p)) < sizeof(p) && 0 == strncmp(s, p, 20)); |
| 120 | + sip_uri_free(&route); |
| 121 | + |
| 122 | + |
| 123 | + assert(0 == sip_header_route(s, s + strlen(s), &route)); |
| 124 | + assert( 0 == cstrcmp( &route. scheme, "sip") && 0 == cstrcmp( &route. host, "[email protected]") && 0 == sip_params_count( &route. headers) && 0 == sip_params_count( &route. parameters)); |
| 125 | + assert(sip_route_write(&route, p, p + sizeof(p)) < sizeof(p) && 0 == strcmp(s+20, p)); |
| 126 | + sip_uri_free(&route); |
| 127 | + |
| 128 | + s = "<sip:p1.example.com;lr>,<sip:p2.domain.com;lr>"; |
| 129 | + sip_uris_init(&routes); |
| 130 | + assert(0 == sip_header_routes(s, s + strlen(s), &routes) && 2 == sip_uris_count(&routes)); |
| 131 | + c = sip_uris_get(&routes, 0); |
| 132 | + assert(0 == cstrcmp(&c->scheme, "sip") && 0 == cstrcmp(&c->host, "p1.example.com") && 0 == sip_params_count(&c->headers) && 1 == sip_params_count(&c->parameters)); |
| 133 | + assert(0 == cstrcmp(&sip_params_get(&c->parameters, 0)->name, "lr") && c->lr); |
| 134 | + c = sip_uris_get(&routes, 1); |
| 135 | + assert(0 == cstrcmp(&c->scheme, "sip") && 0 == cstrcmp(&c->host, "p2.domain.com") && 0 == sip_params_count(&c->headers) && 1 == sip_params_count(&c->parameters)); |
| 136 | + assert(0 == cstrcmp(&sip_params_get(&c->parameters, 0)->name, "lr") && c->lr); |
| 137 | + //assert(sip_route_write(c, p, p + sizeof(p)) < sizeof(p) && 0 == strcmp(s, p)); |
| 138 | + sip_uris_free(&routes); |
| 139 | +} |
| 140 | +#endif |
0 commit comments