/[resiprocate]/main/resip/stack/Helper.hxx
ViewVC logotype

Contents of /main/resip/stack/Helper.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5415 - (show annotations) (download)
Thu Sep 15 12:57:21 2005 UTC (14 years, 4 months ago) by sgodin
File size: 23395 byte(s)
fix for setting correct reason text for NIT responses
1 #if !defined(RESIP_HELPER_HXX)
2 #define RESIP_HELPER_HXX
3
4 #include <time.h>
5
6 #include "resip/stack/Symbols.hxx"
7 #include "resip/stack/Uri.hxx"
8 #include "resip/stack/MethodTypes.hxx"
9 #include "rutil/BaseException.hxx"
10 #include "rutil/Data.hxx"
11 #include "resip/stack/Contents.hxx"
12 #include "resip/stack/SecurityAttributes.hxx"
13
14 namespace resip
15 {
16
17 class SipMessage;
18 class NameAddr;
19 class SecurityAttributes;
20 class Security;
21
22 class UnsupportedAuthenticationScheme : public BaseException
23 {
24 public:
25 UnsupportedAuthenticationScheme(const Data& msg, const Data& file, const int line)
26 : BaseException(msg, file, line) {}
27
28 const char* name() const { return "UnsupportedAuthenticationScheme"; }
29 };
30
31 class Helper
32 {
33 public:
34
35 /// bytes in to-tag& from-tag, should prob. live somewhere else
36 const static int tagSize;
37
38 /**
39 Used by Registration, Publication and Subscription refreshes, to
40 calculate the time at which a refresh should be performed (which
41 is some time, that is a bit smaller than the Expiration interval).
42 The recommended caluclation from the RFC's is the minimnum of the
43 Exipiration interval less 5 seconds and nine tenths of the exipiration
44 interval.
45 */
46 template<typename T>
47 static T aBitSmallerThan(T secs)
48 {
49 return resipMax(T(0), resipMin(T(secs-5), T(9*secs/10)));
50 }
51
52 /**
53 Used to jitter the expires in a SUBSCRIBE or REGISTER expires header
54
55 @param input Value to jitter
56
57 @param lowerPercentage The lower range of the random percentage by which
58 to jitter the value by.
59
60 @param upperPercentage The upper range of the random percentage by which
61 to jitter the value by. Must be greater than or equal
62 to lowerPercentage
63
64 @param minimum Only jitter the input if greater than minimum
65 */
66 static int jitterValue(int input, int lowerPercentage, int upperPercentage, int minimum=0);
67
68 /**
69 Make an invite request - Empty Contact and Via is added and will be populated
70 by the stack when sent.
71
72 @param target Ends up in the RequestURI and To header
73
74 @param from Ends up in the From header
75 */
76 static SipMessage* makeInvite(const NameAddr& target, const NameAddr& from);
77
78 /**
79 Make an invite request using a overridden contact header - Empty Via is added
80 and will be populated by the stack when sent.
81
82 @param target Ends up in the RequestURI and To header
83
84 @param from Ends up in the From header
85
86 @param contact Ends up in the Contact header. Stack will not change this
87 when sent.
88 */
89 static SipMessage* makeInvite(const NameAddr& target, const NameAddr& from, const NameAddr& contact);
90
91 /**
92 Make a response to a provided request. Adds a To tag, Contact and Record-Route
93 headers appropriately.
94
95 @param response SipMessage populated with the appropriate response
96
97 @param request SipMessage request from which to generate the response
98
99 @param responseCode Response code to use on status line.
100
101 @param reason Optional reason string to use on status line. If not provided
102 then a default reason string will be added for well defined
103 response codes.
104
105 @param hostname Optional hostname to use in Warning header. Only used if
106 warning is also provided.
107
108 @param warning Optional warning text. If present a Warning header is added
109 and hostname is used in warning header.
110 */
111 static void makeResponse(SipMessage& response,
112 const SipMessage& request,
113 int responseCode,
114 const Data& reason = Data::Empty,
115 const Data& hostname = Data::Empty,
116 const Data& warning=Data::Empty);
117
118 /**
119 Make a response to a provided request with an overridden Contact.
120 Adds a To tag, Contact and Record-Route headers appropriately.
121
122 @param response SipMessage populated with the appropriate response
123
124 @param request SipMessage request from which to generate the response
125
126 @param responseCode Response code to use on status line.
127
128 @param myContact Contact header to add to response.
129
130 @param reason Optional reason string to use on status line. If not provided
131 then a default reason string will be added for well defined
132 response codes.
133
134 @param hostname Optional hostname to use in Warning header. Only used if
135 warning is also provided.
136
137 @param warning Optional warning text. If present a Warning header is added
138 and hostname is used in warning header.
139 */
140 static void makeResponse(SipMessage& response,
141 const SipMessage& request,
142 int responseCode,
143 const NameAddr& myContact,
144 const Data& reason = Data::Empty,
145 const Data& hostname = Data::Empty,
146 const Data& warning=Data::Empty);
147
148 /**
149 Make a new response to a provided request. Adds a To tag, Contact and
150 Record-Route headers appropriately. Caller owns the returned pointer and
151 is responsible for deleting it.
152
153 @param request SipMessage request from which to generate the response
154
155 @param responseCode Response code to use on status line.
156
157 @param reason Optional reason string to use on status line. If not provided
158 then a default reason string will be added for well defined
159 response codes.
160
161 @param hostname Optional hostname to use in Warning header. Only used if
162 warning is also provided.
163
164 @param warning Optional warning text. If present a Warning header is added
165 and hostname is used in warning header
166
167 @returns SipMessage populated with the appropriate response.
168 Caller must deallocate.
169 */
170 static SipMessage* makeResponse(const SipMessage& request,
171 int responseCode,
172 const Data& reason = Data::Empty,
173 const Data& hostname = Data::Empty,
174 const Data& warning=Data::Empty);
175
176 /**
177 Make a new response to a provided request with an overridden Contact.
178 Adds a To tag, Contact and Record-Route headers appropriately.
179 Caller owns the returned pointer and is responsible for deleting it.
180
181 @param request SipMessage request from which to generate the response
182
183 @param responseCode Response code to use on status line.
184
185 @param myContact Contact header to add to response.
186
187 @param reason Optional reason string to use on status line. If not provided
188 then a default reason string will be added for well defined
189 response codes.
190
191 @param hostname Optional hostname to use in Warning header. Only used if
192 warning is also provided.
193
194 @param warning Optional warning text. If present a Warning header is added
195 and hostname is used in warning header.
196
197 @returns SipMessage populated with the appropriate response.
198 Caller must deallocate.
199 */
200 static SipMessage* makeResponse(const SipMessage& request,
201 int responseCode,
202 const NameAddr& myContact,
203 const Data& reason = Data::Empty,
204 const Data& hostname = Data::Empty,
205 const Data& warning=Data::Empty);
206
207
208 /**
209 Make a 405 response to a provided request. Allows header is added
210 with specified methods, or with all methods the stack knows about.
211 Caller owns the returned pointer and is responsible for deleting it.
212
213 @param request SipMessage request from which to generate the response
214
215 @param allowedMethods Array of integers representing a list of Method
216 Types to add to the generated Allows header.
217 See MethodTypes.hxx.
218
219 @param nMethods Number of methods specified in the allowedMethods
220 integer array. Specify -1 to have this method fill
221 in the Allows header automatically with each method
222 supported by the stack.
223
224 @returns SipMessage populated with the appropriate response.
225 Caller must deallocate.
226 */
227 static SipMessage* make405(const SipMessage& request,
228 const int* allowedMethods = 0,
229 int nMethods = -1);
230
231 /**
232 Returns the default reason string for a particular response code.
233
234 @param responseCode Response code to get reason for
235
236 @param reason Data where the reason string associated with the
237 responseCode will be set.
238 */
239 static void getResponseCodeReason(int responseCode, Data& reason);
240
241 /**
242 Make a new request with a overridden Contact. To, maxforward=70, requestline
243 created, cseq method set, cseq sequence is 1, from and from tag set, contact
244 set, CallId created. Caller owns the returned pointer and is responsible for
245 deleting it.
246
247 @note While contact is only necessary for requests that establish a dialog,
248 those are the requests most likely created by this method, others will
249 be generated by the dialog.
250
251 @param target Ends up in the RequestURI and To header
252
253 @param from Ends up in the From header
254
255 @param contact Ends up in the Contact header. Stack will not change this
256 when sent.
257
258 @param method Type of request to create. Methos is used in the Request Line
259 and the CSeq.
260
261 @returns SipMessage request created. Caller must deallocate.
262 */
263 static SipMessage* makeRequest(const NameAddr& target, const NameAddr& from, const NameAddr& contact, MethodTypes method);
264
265 /**
266 Make a new request. To, maxforward=70, requestline created, cseq method set,
267 cseq sequence is 1, from and from tag set, CallId created. Caller owns the
268 returned pointer and is responsible for deleting it.
269
270 @note An empty contact header is added. This signals to the stack that it
271 should be populated by the transports when sent.
272
273 @param target Ends up in the RequestURI and To header
274
275 @param from Ends up in the From header
276
277 @param method Type of request to create. Methos is used in the Request Line
278 and the CSeq.
279
280 @returns SipMessage request created. Caller must deallocate.
281 */
282 static SipMessage* makeRequest(const NameAddr& target, const NameAddr& from, MethodTypes method);
283
284 /**
285 Make a new Cancel request for the specified request. Caller owns the
286 returned pointer and is responsible for deleting it.
287
288 @param request Request for which the Cancel will apply. ie. Invite request.
289
290 @returns Created Cancel request. Caller must deallocate.
291 */
292 static SipMessage* makeCancel(const SipMessage& request);
293
294 /// Create a Register request with an overriden Contact. See makeRequest.
295 static SipMessage* makeRegister(const NameAddr& to, const NameAddr& from, const NameAddr& contact);
296
297 /// Create a Register request with an empty Contact. See makeRequest.
298 static SipMessage* makeRegister(const NameAddr& to, const NameAddr& from);
299
300 /// Create a Register request with an overriden Contact, transport is added to Request URI. See makeRequest.
301 static SipMessage* makeRegister(const NameAddr& to, const Data& transport, const NameAddr& contact);
302
303 /// Create a Register request with an empty Contact, transport is added to Request URI. See makeRequest.
304 static SipMessage* makeRegister(const NameAddr& to, const Data& transport);
305
306 /// Create a Subscribe request with an overriden Contact. See makeRequest.
307 static SipMessage* makeSubscribe(const NameAddr& target, const NameAddr& from, const NameAddr& contact);
308
309 /// Create a Subscribe request with an empty Contact. See makeRequest.
310 static SipMessage* makeSubscribe(const NameAddr& target, const NameAddr& from);
311
312 /// Create a Message request with an overriden Contact. See makeRequest.
313 static SipMessage* makeMessage(const NameAddr& target, const NameAddr& from, const NameAddr& contact);
314
315 /// Create a Message request with an empty Contact. See makeRequest.
316 static SipMessage* makeMessage(const NameAddr& target, const NameAddr& from);
317
318 /// Create a Publish request with an overriden Contact. See makeRequest.
319 static SipMessage* makePublish(const NameAddr& target, const NameAddr& from, const NameAddr& contact);
320
321 /// Create a Publish request with an empty Contact. See makeRequest.
322 static SipMessage* makePublish(const NameAddr& target, const NameAddr& from);
323
324 /**
325 This interface should be used by the stack (TransactionState) to create an
326 AckMsg to a failure response. See RFC3261 section 17.1.1.3. Caller owns the
327 returned pointer and is responsible for deleting it.
328
329 @note The branch in this ACK needs to be the one from the request.
330 For TU generated ACK, see Dialog::makeAck(...)
331
332 @param request Request that this ACK applies to.
333
334 @param response Response that we are ACKing - required so that we can get the To tag
335 into the generated ACK.
336
337 @returns Created Ack request. Caller must deallocate.
338 */
339 static SipMessage* makeFailureAck(const SipMessage& request, const SipMessage& response);
340
341 /**
342 Creates and returns a unique branch parameter. Generated branch will contain
343 the RFC3261 magic cookie + 4 randome hex characters + "C1" + 2 random hex characters.
344
345 @deprecated Not used by stack.
346 */
347 static Data computeUniqueBranch();
348 static Data computeProxyBranch(const SipMessage& request);
349
350 static Data computeCallId();
351 static Data computeTag(int numBytes);
352
353 enum AuthResult {Failed = 1, Authenticated, Expired, BadlyFormed};
354
355 static AuthResult authenticateRequest(const SipMessage& request,
356 const Data& realm,
357 const Data& password,
358 int expiresDelta = 0);
359
360 static AuthResult authenticateRequestWithA1(const SipMessage& request,
361 const Data& realm,
362 const Data& hA1,
363 int expiresDelta = 0);
364
365 static std::pair<AuthResult,Data>
366 advancedAuthenticateRequest(const SipMessage& request,
367 const Data& realm,
368 const Data& a1,
369 int expiresDelta = 0);
370
371 // create a 407 response with Proxy-Authenticate header filled in
372 static SipMessage* makeProxyChallenge(const SipMessage& request,
373 const Data& realm,
374 bool useAuth = true,
375 bool stale = false);
376
377 // adds authorization headers in reponse to the 401 or 407--currently
378 // only supports md5.
379 static SipMessage& addAuthorization(SipMessage& request,
380 const SipMessage& challenge,
381 const Data& username,
382 const Data& password,
383 const Data& cnonce,
384 unsigned int& nonceCount);
385
386 static Auth makeChallengeResponseAuth(SipMessage& request,
387 const Data& username,
388 const Data& password,
389 const Auth& challenge,
390 const Data& cnonce,
391 unsigned int& nonceCount,
392 Data& nonceCountString);
393 static Auth makeChallengeResponseAuthWithA1(const SipMessage& request,
394 const Data& username,
395 const Data& passwordHashA1,
396 const Auth& challenge,
397 const Data& cnonce,
398 unsigned int& nonceCount,
399 Data& nonceCountString);
400
401 static Data makeResponseMD5WithA1(const Data& a1,
402 const Data& method, const Data& digestUri, const Data& nonce,
403 const Data& qop = Data::Empty, const Data& cnonce = Data::Empty,
404 const Data& cnonceCount = Data::Empty, const Contents *entityBody = 0);
405
406 static Data makeResponseMD5(const Data& username, const Data& password, const Data& realm,
407 const Data& method, const Data& digestUri, const Data& nonce,
408 const Data& qop = Data::Empty, const Data& cnonce = Data::Empty,
409 const Data& cnonceCount = Data::Empty, const Contents *entityBody = 0);
410
411
412 static Data makeNonce(const SipMessage& request, const Data& timestamp);
413
414 static Uri makeUri(const Data& aor, const Data& scheme=Symbols::DefaultSipScheme);
415
416 static void processStrictRoute(SipMessage& request);
417
418 // renamed to make more explicit that this is the port that we should reply too
419 // given that we are following SIP rules WRT rport etc.
420 static int getPortForReply(SipMessage& request);
421
422 static Uri fromAor(const Data& aor, const Data& scheme=Symbols::DefaultSipScheme);
423
424 // Do basic checks to validate a received message off the wire
425 static bool validateMessage(const SipMessage& message);
426
427 // GRUU support -- reversibly and opaquely combine instance id and aor
428 static Data gruuUserPart(const Data& instanceId,
429 const Data& aor,
430 const Data& key);
431
432 // GRUU support -- extract instance id and aor from user portion
433 static std::pair<Data,Data> fromGruuUserPart(const Data& gruuUserPart,
434 const Data& key);
435
436 struct ContentsSecAttrs
437 {
438 ContentsSecAttrs();
439 ContentsSecAttrs(std::auto_ptr<Contents> contents,
440 std::auto_ptr<SecurityAttributes> attributes);
441 ContentsSecAttrs(const ContentsSecAttrs& rhs);
442 ContentsSecAttrs& operator=(const ContentsSecAttrs& rhs);
443 mutable std::auto_ptr<Contents> mContents;
444 mutable std::auto_ptr<SecurityAttributes> mAttributes;
445 };
446
447 static ContentsSecAttrs extractFromPkcs7(const SipMessage& message, Security& security);
448
449
450 enum FailureMessageEffect{ DialogTermination, TransactionTermination, UsageTermination,
451 RetryAfter, OptionalRetryAfter, ApplicationDependant };
452
453 static FailureMessageEffect determineFailureMessageEffect(const SipMessage& response);
454
455 private:
456 static Data qopOption(const Auth& challenge);
457 };
458
459 }
460
461 #endif
462
463 /* ====================================================================
464 * The Vovida Software License, Version 1.0
465 *
466 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
467 *
468 * Redistribution and use in source and binary forms, with or without
469 * modification, are permitted provided that the following conditions
470 * are met:
471 *
472 * 1. Redistributions of source code must retain the above copyright
473 * notice, this list of conditions and the following disclaimer.
474 *
475 * 2. Redistributions in binary form must reproduce the above copyright
476 * notice, this list of conditions and the following disclaimer in
477 * the documentation and/or other materials provided with the
478 * distribution.
479 *
480 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
481 * and "Vovida Open Communication Application Library (VOCAL)" must
482 * not be used to endorse or promote products derived from this
483 * software without prior written permission. For written
484 * permission, please contact vocal@vovida.org.
485 *
486 * 4. Products derived from this software may not be called "VOCAL", nor
487 * may "VOCAL" appear in their name, without prior written
488 * permission of Vovida Networks, Inc.
489 *
490 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
491 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
492 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
493 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
494 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
495 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
496 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
497 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
498 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
499 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
500 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
501 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
502 * DAMAGE.
503 *
504 * ====================================================================
505 *
506 * This software consists of voluntary contributions made by Vovida
507 * Networks, Inc. and many individuals on behalf of Vovida Networks,
508 * Inc. For more information on Vovida Networks, Inc., please see
509 * <http://www.vovida.org/>.
510 *
511 */

Properties

Name Value
svn:eol-style LF

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27