SIP Witch 1.9.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uri.cpp
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include <sipwitch-config.h>
18 #include <ucommon/ucommon.h>
19 #include <ucommon/export.h>
20 #include <sipwitch/uri.h>
21 #include <sipwitch/service.h>
22 
23 namespace sipwitch {
24 
25 void uri::serviceid(const char *addr, char *buf, size_t size)
26 {
27  const char *cp = strchr(addr, '@');
28  if(addr) {
29  String::set(buf, size, ++cp);
30  return;
31  }
32 
33  if(String::equal(addr, "sip:", 4))
34  addr += 4;
35  else if(String::equal(addr, "sips:", 5))
36  addr += 5;
37 
38  String::set(buf, size, addr);
39 }
40 
41 bool uri::hostid(const char *addr, char *buf, size_t size)
42 {
43  assert(buf != NULL);
44  assert(size > 0);
45 
46  char *ep;
47  const char *cp;
48  buf[0] = 0;
49 
50  if(!addr)
51  return false;
52 
53  if(!strnicmp(addr, "sip:", 4))
54  addr += 4;
55  else if(!strnicmp(addr, "sips:", 5))
56  addr += 5;
57 
58  cp = strchr(addr, '@');
59  if(cp)
60  addr = ++cp;
61 
62  String::set(buf, size, addr);
63  if(buf[0] == '[')
64  ep = strchr(buf, ']');
65  else
66  ep = strrchr(buf, ':');
67 
68  if(ep)
69  *ep = 0;
70  return true;
71 }
72 
73 bool uri::userid(const char *addr, char *buf, size_t size)
74 {
75  assert(buf != NULL);
76  assert(size > 0);
77 
78  buf[0] = 0;
79  char *ep;
80 
81  if(!addr)
82  return false;
83 
84  if(!strnicmp(addr, "sip:", 4))
85  addr += 4;
86  else if(!strnicmp(addr, "sips:", 5))
87  addr += 5;
88 
89  if(!strchr(addr, '@'))
90  return false;
91 
92  String::set(buf, size, addr);
93  ep = strchr(buf, '@');
94  if(ep)
95  *ep = 0;
96  return true;
97 }
98 
99 unsigned short uri::portid(const char *uri)
100 {
101  const char *pp = NULL;
102  const char *fp = NULL;
103 
104  if(eq(uri, "sips:", 5))
105  uri += 5;
106  else if(eq(uri, "sip:", 4)) {
107  uri += 4;
108  }
109 
110  if(*uri == '[') {
111  pp = strchr(uri, ']');
112  if(pp)
113  pp = strchr(pp, ':');
114  }
115  else {
116  pp = strrchr(uri, ':');
117  fp = strchr(uri, ':');
118  if(fp != pp)
119  pp = NULL;
120  }
121  if(pp)
122  return atoi(++pp);
123  else
124  return 0;
125 }
126 
127 void uri::publish(const char *uri, char *buf, const char *user, size_t size)
128 {
129  assert(uri != NULL);
130  assert(buf != NULL);
131  assert(user == NULL || *user != 0);
132  assert(size > 0);
133 
134  const char *schema = "sip";
135  const char *cp;
136 
137  if(String::equal(uri, "sip:", 4)) {
138  uri += 4;
139  schema = "sip";
140  }
141  else if(String::equal(uri, "sips:", 5)) {
142  uri += 5;
143  schema = "sips";
144  }
145 
146  cp = strchr(uri, '@');
147  if(cp && user != NULL)
148  uri = ++cp;
149 
150  if(user)
151  snprintf(buf, size, "%s:%s@%s", schema, user, uri);
152  else
153  snprintf(buf, size, "%s:%s", schema, uri);
154 }
155 
156 voip::context_t uri::route(const char *uri, char *buf, size_t size)
157 {
158  buf[0] = 0;
159  const char *schema="sip:";
161 
162  if(eq(uri, "sips:", 5)) {
164  schema="sips:";
165  uri += 5;
166  }
167  else if(!strncmp(uri, "sip:", 4))
168  uri += 4;
169  else if(!strncmp(uri, "tcp:", 4)) {
171  uri += 4;
172  }
173  else if(!strncmp(uri, "udp:", 4)) {
175  uri += 4;
176  }
177 
178  const char *sp = strchr(uri, '@');
179  if(sp)
180  uri = ++sp;
181 
182  snprintf(buf, size, "%s%s", schema, uri);
183  return ctx;
184 }
185 
186 bool uri::server(struct sockaddr *addr, char *buf, size_t size)
187 {
188  assert(addr != NULL);
189  assert(buf != NULL);
190  assert(size > 0);
191 
192  char host[256];
193 
194  buf[0] = 0;
195 
196  if(!Socket::query(addr, host, sizeof(host)))
197  return false;
198 
199 #ifdef AF_INET6
200  if(addr->sa_family == AF_INET6)
201  snprintf(buf, size, "sip:[%s]:%u", host, (unsigned)ntohs(((struct sockaddr_in6 *)(addr))->sin6_port) & 0xffff);
202  else
203 #endif
204  snprintf(buf, size, "sip:%s:%u", host, (unsigned)ntohs(((struct sockaddr_in *)(addr))->sin_port) & 0xffff);
205  return true;
206 }
207 
208 void uri::identity(const struct sockaddr *addr, char *buf, const char *user, size_t size)
209 {
210  assert(addr != NULL);
211  assert(buf != NULL);
212  assert(user == NULL || *user != 0);
213  assert(size > 0);
214 
215  *buf = 0;
216  size_t len;
217 
218  if(user) {
219  String::add(buf, size, user);
220  String::add(buf, size, "@");
221  }
222 
223  len = strlen(buf);
224  Socket::query(addr, buf + len, size - len);
225 }
226 
227 } // end namespace
Some convenience methods for manipulating SIP uri's.
Definition: uri.h:55
static void identity(const struct sockaddr *address, char *buffer, const char *user, size_t size)
Definition: uri.cpp:208
static bool userid(const char *sipuri, char *buffer, size_t size)
Definition: uri.cpp:73
static voip::context_t udp_context
Definition: service.h:235
static voip::context_t tls_context
Definition: service.h:236
Manipulate address strings.
static void serviceid(const char *sipuri, char *buffer, size_t size)
Definition: uri.cpp:25
static voip::context_t route(const char *uri, char *buf, size_t size)
Definition: uri.cpp:156
static voip::context_t tcp_context
Definition: service.h:234
static bool server(struct sockaddr *address, char *buffer, size_t size)
Definition: uri.cpp:186
void * context_t
Definition: voip.h:53
Service configuration and component callbacks.
static voip::context_t out_context
Definition: service.h:233
static bool hostid(const char *sipuri, char *buffer, size_t size)
Definition: uri.cpp:41
static void publish(const char *uri, char *buffer, const char *user, size_t size)
Definition: uri.cpp:127
static unsigned short portid(const char *sipuri)
Definition: uri.cpp:99