1 /*
2  * Copyright (C) 2006 iptego GmbH
3  * Copyright (C) 2011 Stefan Sayer
4  *
5  * This file is part of SEMS, a free SIP media server.
6  *
7  * SEMS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version. This program is released under
11  * the GPL with the additional exemption that compiling, linking,
12  * and/or using OpenSSL is allowed.
13  *
14  * For a license to use the SEMS software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * SEMS is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28 
29 #ifndef _AmSipRegistration_h_
30 #define _AmSipRegistration_h_
31 #include <string>
32 using std::string;
33 
34 #include "ampi/SIPRegistrarClientAPI.h"
35 #include "ampi/UACAuthAPI.h"
36 #include "AmUriParser.h"
37 #include "AmSessionEventHandler.h"
38 
39 #define REGISTER_SEND_TIMEOUT 60
40 
41 struct SIPRegistrationInfo {
42   string domain;
43   string user;
44   string name;
45   string auth_user;
46   string pwd;
47   string proxy;
48   string contact;
49 
SIPRegistrationInfoSIPRegistrationInfo50   SIPRegistrationInfo(const string& domain,
51 		      const string& user,
52 		      const string& name,
53 		      const string& auth_user,
54 		      const string& pwd,
55 		      const string& proxy,
56 		      const string& contact)
57     : domain(domain),user(user),name(name),
58     auth_user(auth_user),pwd(pwd),proxy(proxy),contact(contact)
59   { }
60 };
61 
62 class AmSIPRegistration
63 : public AmBasicSipEventHandler,
64   public DialogControl,
65   public CredentialHolder
66 
67 {
68 
69   AmBasicSipDialog dlg;
70   UACAuthCred cred;
71 
72   SIPRegistrationInfo info;
73 
74   // session to post events to
75   string sess_link;
76 
77   AmSessionEventHandler* seh;
78 
79   AmSipRequest req;
80 
81   AmUriParser server_contact;
82   AmUriParser local_contact;
83 
84   time_t reg_begin;
85   unsigned int reg_expires;
86   time_t reg_send_begin;
87 
88   unsigned int expires_interval;
89 
90  public:
91   AmSIPRegistration(const string& handle,
92 		    const SIPRegistrationInfo& info,
93 		    const string& sess_link);
94   ~AmSIPRegistration();
95 
96   void setRegistrationInfo(const SIPRegistrationInfo& _info);
97 
98   void setSessionEventHandler(AmSessionEventHandler* new_seh);
99 
100   void setExpiresInterval(unsigned int desired_expires);
101 
102   bool doRegistration();
103   bool doUnregister();
104 
105   bool timeToReregister(time_t now_sec);
106   bool registerExpired(time_t now_sec);
107   void onRegisterExpired();
108   void onRegisterSendTimeout();
109 
110   bool registerSendTimeout(time_t now_sec);
111 
112   void onSendRequest(AmSipRequest& req, int& flags);
113   void onSendReply(const AmSipRequest& req, AmSipReply& reply, int& flags);
114 
115   // DialogControl if
getDlg()116   AmBasicSipDialog* getDlg() { return &dlg; }
117   // CredentialHolder
getCredentials()118   UACAuthCred* getCredentials() { return &cred; }
119 
120   void onSipReply(const AmSipRequest& req,
121 		  const AmSipReply& reply,
122 		  AmBasicSipDialog::Status old_dlg_status);
123 
124   /** is this registration registered? */
125   bool active;
126   /** should this registration be removed from container? */
127   bool remove;
128   /** are we waiting for the response to a register? */
129   bool waiting_result;
130   /** are we unregistering? */
131   bool unregistering;
132 
133   enum RegistrationState {
134     RegisterPending = 0,
135     RegisterActive,
136     RegisterExpired
137   };
138   /** return the state of the registration */
139   RegistrationState getState();
140   /** return the expires left for the registration */
141   unsigned int getExpiresLeft();
142   /** return the expires TS for the registration */
143   time_t getExpiresTS();
144 
145   bool getUnregistering();
146 
getInfo()147   SIPRegistrationInfo& getInfo() { return info; }
getEventSink()148   const string& getEventSink() { return sess_link; }
getHandle()149   const string& getHandle() { return req.from_tag; }
150 };
151 
152 
153 
154 #endif
155