1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 #undef HAVE_CONFIG_H
17
18 #include <sipwitch-config.h>
20 #include <ucommon/secure.h>
21
22 #ifdef HAVE_SYSTEMD
23 #include <systemd/sd-daemon.h>
24 #endif
25
26 #ifdef WIN32
27 #undef alloca
28 #endif
29
30 #include <cctype>
31 #include <new>
32
33 #define SECOND_TIMEOUT (1000l)
34 #define MINUTE_TIMEOUT (SECOND_TIMEOUT * 60l)
35 #define HOUR_TIMEOUT (MINUTE_TIMEOUT * 60l)
36 #define DAY_TIMEOUT (HOUR_TIMEOUT * 24l)
37
38 namespace sipwitch {
39
40 #define PAGING_SIZE (2048l * sizeof(void *))
41
42 #define ALLOWS_INVITE 0x0001
43 #define ALLOWS_MESSAGE 0x0002
44 #define ALLOWS_DEFAULT 0x0003
45
46 class thread;
47
49
51 {
52 public:
53 static void reload(void);
54
55 static const char *get(const char *id);
56
57 static bool set(const char *id, const char *hash);
58
59 static void release(const char *hash);
60
61 static void load(void);
62 };
63
65 {
66 public:
68 {
69 public:
70 void update(Socket::address& addr, int changed);
71 void update(void);
72 bool expire(Socket::address& addr);
73 bool refresh(Socket::address& addr, time_t expires, const char *target_contact);
75 unsigned addTarget(Socket::address& via, time_t expires,
const char *contact,
const char *policy,
struct sockaddr *peer,
voip::context_t context);
76 unsigned setTarget(Socket::address& via, time_t expires,
const char *contact,
const char *policy,
struct sockaddr *peer,
voip::context_t context);
77 void addContact(const char *id);
78 void addPublished(const char *id);
79 void addRoute(const char *pat, unsigned pri, const char *prefix, const char *suffix);
80 };
81
83 {
84 private:
86
87 public:
92
93 void operator=(
mapped *ptr);
94
95 inline operator bool()
const
96 {return entry != NULL;}
97
99 {return entry == NULL;}
100
102 {return entry;}
103
105 {return entry;}
106 };
107
109 {
110 public:
111 typedef enum {READY, BUSY, AWAY, DND, OFFLINE, UNKNOWN} status_t;
112 // internal hidden address indexing object
114 {
115 public:
119 } index;
120 struct sockaddr_internet address;
121 struct sockaddr_storage peering;
123
130
131 static void *operator new(size_t size);
132 static void operator delete(void *ptr);
133 };
134
136 {
137 public:
143 };
144
145 private:
147 {
148 public:
150
151 static void *operator new(size_t size);
152 static void operator delete(void *ptr);
153 };
154
155 bool check(void);
159 void snapshot(FILE *fp);
160
161 static void clear(
mapped *rr);
162 static void expire(
mapped *rr);
163 static mapped *find(
const char *
id);
164
166
168
174
175 public:
177
179 {return (const char *)reg.realm;}
180
182 {return (const char *)reg.digest;}
183
185 {return reg.expires;}
186
188 {return reg.prefix;}
189
191 {return reg.range;}
192
194 {return reg.routes;}
195
196 static const char *getDomain(void);
199 static unsigned getEntries(void);
200 static unsigned getIndex(mapped *rr);
201 static bool isExtension(const char *id);
202 static bool isUserid(const char *id);
203 static mapped *address(const struct sockaddr *addr);
204 static mapped *contact(
const char *
uri);
205 static mapped *contact(const struct sockaddr *addr, const char *uid);
206 static mapped *getExtension(const char *id);
207 static mapped *allocate(const char *id);
208 static mapped *access(const char *id);
210 static mapped *dialing(const char *id);
211 static bool exists(const char *id);
212 static pattern *getRouting(unsigned trs, const char *id);
213 static void detach(mapped *m);
214 static bool remove(const char *id);
215 static unsigned cleanup(time_t period);
216 };
217
219 {
220 private:
225
227
229 {
230 public:
231 static void create(timeout_t interval);
232 static void cancel(void);
233
235
237
238 private:
243
245 void run(void);
246 };
247
249 {
250 public:
262
263 enum {OPEN, CLOSED,
RING, BUSY, REORDER, REFER, REINVITE} state;
264
272
274 struct sockaddr_storage peering;
275
279
281 {return (this == parent->source);}
282
284 {return (this == parent->target);}
285 };
286
288 {
289 public:
291
292 static void *operator new(size_t size);
293 static void operator delete(void *obj);
294
296
298 {return &sid;}
299 };
300
302 {
303 public:
304 typedef enum {INITIAL,
TRYING, RINGING, RINGBACK, REORDER, HOLDING, ANSWERED, JOINED, TRANSFER, REDIRECT, BUSY, TERMINATE, FAILED, FINAL}
state_t;
305
307
309
311
319
320 void disarm(void);
321 void arm(timeout_t timeout);
322 void reply_source(int error);
332 timeout_t getTimeout(void);
333 void closingLocked(
session *s);
334 void terminateLocked(void);
335 void disconnectLocked(void);
337 void cancelLocked(void);
340 void set(
state_t state,
char id,
const char *text);
341
352 unsigned count;
// total open segments
353 unsigned invited;
// pending segments with invites
354 unsigned ringing;
// number of ringing segments
360
361 static void *operator new(size_t size);
362 static void operator delete(void *obj);
363
364 private:
365 void expired(void);
366 };
367
371 void snapshot(FILE *fp);
372 bool check(void);
373
375
378
380
394
395 void release(void);
396
397 public:
399
401 {
402 private:
405
406 public:
407 subnet(cidr::policy **acl,
const char *rule,
const char *
name);
408
409 struct sockaddr_storage iface;
410
412 {return netname;}
413
415 {return (struct sockaddr *)&iface;}
416
417 inline operator bool()
418 {return active;}
419
421 {return !active;}
422
424 {active = true;}
425
427 {active = false;}
428
430 {return active == false;}
431 };
432
434
435 static const char *getScheme(void);
436 static void getInterface(struct sockaddr *iface, const struct sockaddr *dest);
439 static void destroy(session *s);
440 static void destroy(call *cr);
441 static void disjoin(call *cr);
442 static void detach(session *s);
443 static void clear(session *s);
444 static void close(session *s);
446 static char *sipAddress(
struct sockaddr_internet *addr,
char *buf,
const char *user = NULL,
size_t size =
MAX_URI_SIZE);
447 static char *sipPublish(
struct sockaddr_internet *addr,
char *buf,
const char *user = NULL,
size_t size =
MAX_URI_SIZE);
448 static char *sipContact(
struct sockaddr_internet *addr,
char *buf,
const char *user = NULL,
const char *display = NULL,
size_t size =
MAX_URI_SIZE);
449 static Socket::address *getAddress(
const char *
uri, Socket::address *addr = NULL);
451 static void enableDumping(void);
452 static void clearDumping(void);
453 static void disableDumping(void);
456 static void setDialog(session *session,
voip::did_t did);
457 static int getDialog(session *session);
461 static int inviteRemote(
stack::session *session,
const char *
uri,
const char *digest = NULL);
463
466
469
472
475 };
476
478 {
479 private:
480 typedef linked_value<profile_t, LinkedObject>
profile;
481
486
487 bool create(const char *id, keynode *node);
488 keynode *find(const char *id);
489
490 void confirm(void);
491 void dump(FILE *fp);
492
493 public:
499
501
502 static bool check(void);
503 static profile_t *getProfile(
const char *
id);
504 static keynode *getRouting(const char *id);
505 static void getProvision(
const char *
id,
usernode& user);
506 static void getDialing(
const char *
id,
usernode& user);
507 static keynode *getConfig(void);
508 static void listPolicy(FILE *fp);
509 static stack::subnet *getPolicy(
const struct sockaddr *addr);
511 static bool isLocal(const struct sockaddr *addr);
513 static void release(keynode *node);
514 static void release(
usernode& user);
515 static void reload(void);
516 static Socket::address *getContact(const char *id);
517 static void plugins(const char *argv0, const char *names);
518 static void run(void);
519 static void stop(void);
520 static void *allocate(
size_t size,
LinkedObject **list,
volatile unsigned *
count = NULL);
521 static unsigned allocate(void);
522
523 static bool announce(
MappedRegistry *rr,
const char *msgtype,
const char *event,
const char *expires,
const char *body);
528 static bool authenticate(
voip::reg_t id,
const char *realm);
529 static const char *referLocal(
MappedRegistry *rr,
const char *target,
char *buffer,
size_t size);
530 static const char *referRemote(
MappedRegistry *rr,
const char *target,
char *buffer,
size_t size);
531 static bool checkId(const char *id);
532 static void printlog(const char *fmt, ...) __PRINTF(1, 2);
533 };
534
536 {
537 private:
539 {
540 public:
548 void create();
549 };
550
552
553 bool check(void);
555 void cleanup(void);
556 void snapshot(FILE *fp);
557
558 static int deliver(
message *msg);
559 static int remote(
const char *to,
message *msg,
const char *digest = NULL);
560
561 public:
563
564 static void automatic(void);
565 static void update(const char *userid);
566 static int deliver(const char *to, const char *reply, const char *from, caddr_t body, size_t size, const char *msgtype, const char *digest = NULL);
567 static int system(
const char *to,
const char *
message);
568 };
569
570
572 {
573 private:
576
581 struct sockaddr_storage peering;
610
614
616
618
619 static void wait(
unsigned count);
621
622 void send_reply(int error);
623 void expiration(void);
624 void invite(void);
625 void identify(void);
626 bool getsource(void);
627 bool unauthenticated(void);
628 bool authenticate(void);
630 bool authorize(void);
631 void registration(void);
632 void validate(void);
633 void message(void);
634 void publish(void);
635 void reregister(const char *contact, time_t interval);
636 void deregister(void);
637 void challenge(void);
638 void options(void);
639 void run(void);
641 const char *getIdent(void);
642
643 public:
644 static void shutdown(void);
645 };
646
647 // media proxy support for NAT transversal is being moved to here...
649 {
650 public:
651 // support thread
653 {
654 public:
655 static void startup(void);
656
658
659 static void shutdown(void);
660
661 private:
663
664 void run(void);
665 };
666
667 // a support class to help in sdp parsing
669 {
670 public:
675 struct sockaddr_storage local, top;
679
682
683 inline struct sockaddr *
get(void)
684 {return (struct sockaddr *)&local;}
685
686 void set(
const char *source,
char *target,
size_t len =
MAX_SDP_BUFFER);
687 char *get(char *buffer, size_t len);
688 size_t put(char *buffer);
689
690 // check connect in sdp output
691 void check_connect(char *buffer, size_t len);
692 void check_media(char *buffer, size_t len);
693
694 // can do backfill of NAT if connect in media record
695 void reconnect(void);
696 };
697
698 // proxy socket class
700 {
701 private:
702 void release(void);
703
704 public:
708 struct sockaddr_storage local, remote, peering;
709 bool fw;
// to be used when we add ipfw rules support
710
713
715 void release(time_t expire = 0l);
716 void reconnect(struct sockaddr *address);
717 void copy(void);
718 };
719
721
725
726 // get and activate nat instance if any are free...
728
729 // set ipv6 flag, removes need to proxy any external addresses...
730 static void enableIPV6(void);
731
732 // release any existing media proxy for the call session, proxy can be kept active for re-invite transition
733 static void release(
LinkedObject **nat,
unsigned expires = 0);
734
735 // rewrite an invite for a call target if different, otherwise uses original source sdp...
737
738 // rewrite or copy sdp of session on answer for connection
740
741 // re-assign or copy sdp on re-invite; clears and rebuilds media proxy if needed...
743
744 private:
745 // low level rewrite & proxy assignment
747
748 // see if connected directly or if requires proxy
749 static bool isProxied(const char *source, const char *target, struct sockaddr_storage *peering);
750
751 void release(void);
752 };
753
755 {
756 public:
758
759 history(shell::loglevel_t lid,
const char *msg);
760
761 void set(shell::loglevel_t lid, const char *msg);
762
763 static void add(shell::loglevel_t lid, const char *msg);
764 static void set(unsigned size);
765 static void out(void);
766 };
767
768 #ifdef HAVE_SIGWAIT
769
771 {
772 private:
773 bool shutdown;
774 bool started;
775
776 sigset_t sigs;
777
778 void run(void);
779 void cancel(void);
780
783
785
786 public:
788 static void setup(void);
789 static void start(void);
790 static void stop(void);
791 };
792
793 #else
795 {
796 public:
798 static void setup(void);
799 static void start(void);
800 static void stop(void);
801 };
802 #endif
803
804 #ifdef HAVE_SYS_INOTIFY_H
805
807 {
808 private:
810
812
813 void run(void);
814
816
817 public:
818 static void start(void);
819 static void stop(void);
820 };
821
822 #else
823
825 {
826 public:
827 static void start(void);
828 static void stop(void);
829 };
830
831 #endif
832
833 } // namespace sipwitch
834
linked_value< profile_t, LinkedObject > profile
Structure for SIP Message (REQUEST and RESPONSE).
static const char * sipusers
Some convenience methods for manipulating SIP uri's.
voip::proxyauth_t proxy_auth
static unsigned getRoutes(void)
static timeout_t ringTimeout(void)
registry::mapped * reginfo
const char *volatile trusted
Pointer to a provisioned user xml subtree.
User profiles are used to map features and toll restriction level together under a common identifier...
mapped * operator->() const
const char *volatile proxy
Structure for event description.
const char *volatile published
Representation of a mapped active user record.
service::keynode * routed
enum _state_t state_t
Enumeration for transaction state.
static unsigned inviteExpires(void)
static unsigned getRange(void)
service::usernode authorized
Server control interfaces and functions.
MappedRegistry * accepted
const char *volatile restricted
Top level include directory for GNU Telephony SIP Witch Server.
static background * thread
struct sockaddr * getInterface(void)
static const char * getRealm(void)
Representation of an active call record.
Callback methods for objects managed under the service thread.
Definition of the From header.
static timeout_t resetTimeout(void)
bool isSource(void) const
static const char * sipadmin
static shell::logmode_t logmode
destination_t destination
bool isTarget(void) const
static time_t getExpires(void)
Socket::address via_address
static const char * getDigest(void)
Definition of the Via header.
eXosip_event_type
Structure for event type description.
const char *volatile localnames
static timeout_t cfnaTimeout(void)
System configuration instance and service functions.
mapped * operator*() const
static unsigned getPrefix(void)
Interface class for call detail records.
Structure for referencing SIP urls.
voip::proxyauth_t www_auth
Definition of the WWW-Authenticate header.
struct sockaddr * address
treemap< char * > keynode
Definition of a xml node.