1 /*
2  * $Id: sip_trans.h 1001 2008-06-02 10:19:47Z rco $
3  *
4  * Copyright (C) 2007 Raphael Coeffic
5  *
6  * This file is part of SEMS, a free SIP media server.
7  *
8  * SEMS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version. This program is released under
12  * the GPL with the additional exemption that compiling, linking,
13  * and/or using OpenSSL is allowed.
14  *
15  * For a license to use the SEMS software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * SEMS is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29 
30 #ifndef _sip_trans_h
31 #define _sip_trans_h
32 
33 #include "cstring.h"
34 #include "wheeltimer.h"
35 
36 #include <sys/socket.h>
37 
38 #include <list>
39 using std::list;
40 
41 struct sip_msg;
42 struct sip_target_set;
43 
44 class trsp_socket;
45 class msg_logger;
46 
47 /**
48  * Transaction types
49  */
50 enum {
51 
52     TT_UAS=1,
Hyperplane()53     TT_UAC
54 };
55 
Hyperplane(const Hyperplane<Scalar,AmbientDimAtCompileTime,OtherOptions> & other)56 /**
57  * Transaction states
58  */
59 enum {
60 
61     TS_TRYING=1,   // UAC:!INV;     UAS:!INV
62     TS_CALLING,    // UAC:INV
63     TS_PROCEEDING, // UAC:INV,!INV; UAS:INV,!INV
64     TS_PROCEEDING_REL, // UAS:INV
65     TS_COMPLETED,  // UAC:INV,!INV; UAS:INV,!INV
66     TS_CONFIRMED,  //               UAS:INV
67     TS_TERMINATED_200,
68     TS_TERMINATED, // UAC:INV,!INV; UAS:INV,!INV
69 
70     TS_ABANDONED,
71     TS_REMOVED
72 };
73 
74 
75 /**
76  * We support at most 3 timer per transaction,
77  * which is okay according to the standard
Hyperplane(const VectorType & n,const Scalar & d)78  */
79 #define SIP_TRANS_TIMERS 3
80 
81 class sip_trans;
82 
83 class trans_timer
84     : protected timer
85 {
86     trans_timer(const trans_timer& ti) : timer() {}
87 
Through(const VectorType & p0,const VectorType & p1)88 public:
89     unsigned int type;
90     unsigned int bucket_id;
91     sip_trans*   t;
92 
93     trans_timer(unsigned int timer_type, unsigned int expires,
94 		int bucket_id, sip_trans* t)
95         : timer(expires), type(timer_type),
96 	  bucket_id(bucket_id), t(t)
97     {}
98 
Through(const VectorType & p0,const VectorType & p1,const VectorType & p2)99     trans_timer(const trans_timer& ti, int bucket_id, sip_trans* t)
100         : timer(ti.expires), type(ti.type),
101 	  bucket_id(bucket_id), t(t)
102     {}
103 
104     void fire();
105 };
106 
107 class sip_trans
108 {
109     trans_timer* timers[SIP_TRANS_TIMERS];
110 
111  public:
112     /** Transaction type */
113     unsigned int type;
114 
115     /** Request that initiated
116 	the transaction */
117     sip_msg* msg;
118 
119     /** To-tag included in reply.
120 	(useful for ACK matching) */
121     cstring to_tag;
122 
Hyperplane(const ParametrizedLine<Scalar,AmbientDimAtCompileTime> & parametrized)123     /** reply code of last
124         sent/received reply */
125     int reply_status;
126 
127     /** Transaction state */
128     int state;
~Hyperplane()129 
130     /** used by UAS only; keeps RSeq of last sent reliable 1xx */
131     unsigned int last_rseq;
dim()132 
133     /** Dialog-ID used for UAC transactions */
134     cstring dialog_id;
normalize(void)135 
136     /** Destination list for requests */
137     sip_target_set* targets;
138 
139     /**
140      * Retransmission buffer
141      *  - UAC transaction: ACK
142      *  - UAS transaction: last reply
signedDistance(const VectorType & p)143      */
144     char* retr_buf;
145 
146     /** Length of the retransmission buffer */
147     int   retr_len;
absDistance(const VectorType & p)148 
149     /** Destination for retransmissions */
150     sockaddr_storage retr_addr;
151     trsp_socket*     retr_socket;
projection(const VectorType & p)152 
153     /** flags used by send_request() */
154     unsigned int flags;
155 
156     /** message logging */
normal()157     msg_logger* logger;
158 
159     /** request canceled? */
160     bool canceled;
161 
normal()162     /**
163      * Tells if a specific timer is set
164      *
165      * @param timer_type @see sip_timer_type
166      */
167     bool is_timer_set(unsigned int timer_type);
168 
169     /**
170      * Fetches a specific timer
offset()171      *
172      * @param timer_type @see sip_timer_type
173      */
174     trans_timer* get_timer(unsigned int timer_type);
175 
coeffs()176     /**
177      * Resets a specfic timer with a delay value
178      *
179      * @param timer_type @see sip_timer_type
180      * @param expires_delay delay before expiration in millisecond
181      * @param bucket_id id of the transaction's bucket
182      */
183     void reset_timer(unsigned int timer_type,
184 		     unsigned int expire_delay /* ms */,
185 		     unsigned int bucket_id);
186 
187     /**
188      * Resets a specific timer
intersection(const Hyperplane & other)189      *
190      * @param t the new timer
191      * @param timer_type @see sip_timer_type
192     */
193     void reset_timer(trans_timer* t, unsigned int timer_type);
194 
195     /**
196      * Clears a specfic timer
197      *
198      * @param timer_type @see sip_timer_type
199      */
200     void clear_timer(unsigned int timer_type);
201 
202     /**
203      * Resets every timer
204     */
205     void reset_all_timers();
206 
207     /**
208      * Retransmits the content of the retry buffer (replies or non-200 ACK).
209      */
210     void retransmit();
211 
212     sip_trans();
213     ~sip_trans();
214 
215     const char* type_str() const;
216     const char* state_str() const;
217 
218     void dump() const;
219 };
220 
221 #endif
222 
223 /** EMACS **
224  * Local variables:
225  * mode: c++
226  * c-basic-offset: 4
227  * End:
228  */
229