LibSip_Steps.ttcn 110 KB
Newer Older
 *	@author 	STF 346, STF366, STF368, STF369
 *  @version    $Id$
 *	@desc		This module provides the types used by the test component 
 *              for SIP-IMS tests.
 */

module LibSip_Steps language "TTCN-3:2005"//MRO
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
{	
	//LibCommon
	import from LibCommon_Sync all ;
	import from LibCommon_VerdictControl all ;
	//LibSip
	import from LibSip_SIPTypesAndValues all;
	import from LibSip_SDPTypes all;
	import from LibSip_Templates all;
	import from LibSip_Interface all;
	import from LibSip_PIXITS all;
	import from LibSip_XMLTypes all;

group externalfunctions {
	
	//Return random charstring
	external function fx_rndStr() return charstring;
	// Return the equivalent string in lower case
	external function fx_putInLowercase(charstring par_string) return charstring;
	
	external function fx_getIpAddr(charstring host_name) return charstring;

	// External function to generate a digest response.
	// References:
	//   * RFC 2617 HTTP Authentication: Basic and Digest Access
	//     Authentication, and
	//   * RFC 1321 The MD5 Message-Digest Algorithm
	// See RFC 2617, chapter 5 Sample implementation, for example usage, as
	// the signature of calculateDigestResponse is according to the example
	// given in the RFC.
	//
	external function fx_calculateDigestResponse(
	  charstring nonce,
	  charstring cnonce,
	  charstring user,
	  charstring realm,
	  charstring passwd,
	  charstring alg,
	  charstring nonceCount,
	  charstring method,
	  charstring qop,
	  charstring URI,
	  charstring HEntity) return charstring;

}

group ParameterOperations {

	/**
	 * 
	 * @desc function to generate a 32 bits random number as a charstring for tag field
	 * (used as e.g.: tag in from-header field, or branch parameter in via header)
	 * @return random value with at least 32 bits of randomness
	 * 
	 */
	 function f_getRndTag() return charstring
	{
	  var charstring tag_value;
	  //tag_value is initialized with a random value with at least 32 bits of randomness
	  // 4294967296 is a 32 bits integer
	  tag_value := fx_rndStr()&fx_rndStr();
	  return(tag_value);
	}

	/**
	 * 
	 * @desc  Function to prepare credentials for request that has an empty
	 * entity body such as a REGISTER message.
	 * @param p_userprofile to get important parameters
	 * @return Credentials field
	 */
	function f_calculatecCredentials_empty(in SipUserProfile p_userprofile) return Credentials
	{
	  var Credentials v_result;
	      
	  var charstring v_nonce := "";
	      
	  // RFC 2617 3.2.2 username:
	  // The name of user in the specified realm.
	  var charstring v_username := p_userprofile.privUsername;
	      
	  var charstring v_realm := p_userprofile.registrarDomain;
	      
	  var charstring v_uri := c_sipScheme & ":" & p_userprofile.registrarDomain;
	           
	  var charstring v_response := "";
	 
	  // Construct credentials for an Authorization field of a request.
	  v_result :=
	  {
		digestResponse :=
		{
		  { id := "Digest username", paramValue := v_username },
		  { id := "realm", paramValue := v_realm },
		  { id := "uri", paramValue := v_uri },
		  { id := "nonce=""""", paramValue := omit }, // already enclosed to " characters
		  { id := "response=""""", paramValue := omit } // already enclosed to " characters
		}
	  };
	      
	  return v_result;
	}


	/**
	 * 
	 * @desc  Function to calculate credentials for request that has an empty
	 * entity body such as a REGISTER message.
	 * @param p_userprofile to get important parameters
	 * @param p_method (can be "REGISTER", "INVITE",....)
	 * @param p_challenge parameter from 4xx response
	 * @return Credentials field
	 * @verdict 
	 */
	function f_calculatecCredentials(in SipUserProfile p_userprofile, in charstring p_method, 
		in CommaParam_List p_challenge) return Credentials
	{
	  var Credentials v_result;
      
	  var charstring v_nonce := "";
      
	  // Use a fixed client nonce.
	  var charstring v_cnonce := "1317265";
      
	  // RFC 2617 3.2.2 username:
	  // The name of user in the specified realm.
	  var charstring v_username := p_userprofile.privUsername;
      
	  var charstring v_realm;
      
	  // RFC 2617 3.2.2.2 passwd:
	  // A known shared secret, the password of user of the specified
	  // username.
	  var charstring v_passwd := p_userprofile.passwd;
      
	  var charstring v_algorithm;
      
	  // Use a fixed nonce count.
	  const charstring c_nonceCount := "00000002";

	  var charstring v_qop := p_userprofile.qop;
      
	  var charstring v_uri := c_sipScheme & ":" & p_userprofile.registrarDomain;
      
	  // MD5 hash of empty entity body.
	  const charstring c_hEntity := "d41d8cd98f00b204e9800998ecf8427e";
      
	  var charstring v_response;
	  var charstring v_opaque;
      
      
	  // extract nonce, realm, algorithm, and opaque from challenge
      
	  v_nonce := f_extractParamValueFromChallenge(p_challenge, "nonce"); 
	  v_realm := f_extractParamValueFromChallenge(p_challenge, "realm"); 
	  v_algorithm := f_extractParamValueFromChallenge(p_challenge, "algorithm");
	  v_opaque := f_extractParamValueFromChallenge(p_challenge, "opaque");
      
	  // calculate a digest response for the Authorize header
	  v_response := fx_calculateDigestResponse(
		v_nonce,
		v_cnonce,
		v_username,
		v_realm,
		v_passwd,
		v_algorithm,
		c_nonceCount,
		p_method,
		v_qop,
		v_uri,
		c_hEntity);
      
	  // Construct credentials for an Authorization field of a request.
	  v_result :=
	  {
		digestResponse :=
		{
		  { id := "Digest username", paramValue := v_username },
		  { id := "realm", paramValue := v_realm },
		  { id := "nonce", paramValue := v_nonce },
		  { id := "uri", paramValue := v_uri },
		  { id := "response", paramValue := v_response },
		  { id := "algorithm=md5", paramValue := omit }, // algorithm is not enclosed to " characters
		  { id := "cnonce", paramValue := v_cnonce },
		  { id := "opaque=""""", paramValue := omit }, // already enclosed to " characters
		  { id := "qop="&v_qop, paramValue := omit },//qop
		  { id := "nc="&c_nonceCount, paramValue := omit }//nonceCount
		}
	  };
      
	  return v_result;
	}

	/**
	 * 
	 * @desc  Function to calculate credentials for request that has an empty
	 * entity body such as a REGISTER message. NO RESPONSE value to cause an error!
	 * @param p_userprofile to get important parameters
	 * @param p_method (can be "REGISTER", "INVITE",....)
	 * @param p_challenge parameter from 4xx response
	 * @return Credentials field
	 * @verdict 
	 */
	function f_calculatecCredentials_wo_response(in SipUserProfile p_userprofile, in charstring p_method, 
		in CommaParam_List p_challenge) return Credentials
	{
	  var Credentials v_result;
      
	  var charstring v_nonce := "";
      
	  // Use a fixed client nonce.
	  var charstring v_cnonce := "1317265";
      
	  // RFC 2617 3.2.2 username:
	  // The name of user in the specified realm.
	  var charstring v_username := p_userprofile.privUsername;
      
	  var charstring v_realm;
      
	  // RFC 2617 3.2.2.2 passwd:
	  // A known shared secret, the password of user of the specified
	  // username.
	  var charstring v_passwd := p_userprofile.passwd;
      
	  var charstring v_algorithm;
      
	  // Use a fixed nonce count.
	  const charstring c_nonceCount := "00000002";

	  var charstring v_qop := p_userprofile.qop;
      
	  var charstring v_uri := c_sipScheme & ":" & p_userprofile.registrarDomain;
      
	  // MD5 hash of empty entity body.
	  const charstring c_hEntity := "d41d8cd98f00b204e9800998ecf8427e";
      
	  var charstring v_response;
	  var charstring v_opaque;
      
      
	  // extract nonce, realm, algorithm, and opaque from challenge
      
	  v_nonce := f_extractParamValueFromChallenge(p_challenge, "nonce"); 
	  v_realm := f_extractParamValueFromChallenge(p_challenge, "realm"); 
	  v_algorithm := f_extractParamValueFromChallenge(p_challenge, "algorithm");
	  v_opaque := f_extractParamValueFromChallenge(p_challenge, "opaque");
      
	  // calculate a digest response for the Authorize header
	  v_response := fx_calculateDigestResponse(
		v_nonce,
		v_cnonce,
		v_username,
		v_realm,
		v_passwd,
		v_algorithm,
		c_nonceCount,
		p_method,
		v_qop,
		v_uri,
		c_hEntity);
      
	  // Construct credentials for an Authorization field of a request.
	  v_result :=
	  {
		digestResponse :=
		{
		  { id := "Digest username", paramValue := v_username },
		  { id := "realm", paramValue := v_realm },
		  { id := "nonce", paramValue := v_nonce },
		  { id := "uri", paramValue := v_uri },
//		  { id := "response", paramValue := v_response }, // not included to cause an error
		  { id := "algorithm=md5", paramValue := omit }, // algorithm is not enclosed to " characters
		  { id := "cnonce", paramValue := v_cnonce },
		  { id := "opaque=""""", paramValue := omit }, // already enclosed to " characters
		  { id := "qop="&v_qop, paramValue := omit },//qop
		  { id := "nc="&c_nonceCount, paramValue := omit }//nonceCount
		}
	  };
      
	  return v_result;
	}

	/**
	 * 
	 * @desc  Function to calculate credentials for response 401 - WWW-Authorization
	 * @param p_qop of the peer UE (alternatively )
	 * @param p_challenge parameter from 1st REGISTER request
	 * @return Credentials field
	 * @verdict 
	 */
	function f_calculatecChallenge_forWWWAuthorization(in charstring p_qop, in CommaParam_List p_challenge) return Challenge
	{
	  var Challenge v_result;

	  var charstring v_realm;
      
	  var charstring v_qop := p_qop;

	  v_realm := f_extractParamValueFromChallenge(p_challenge, "realm");
      
	  // Construct credentials for an Authorization field of a request.
	  v_result :=
	  {
		digestCln :=
		{
		  { id := "Digest realm", paramValue := v_realm },
		  { id := "nonce=", paramValue := c_nonce },
		  { id := "algorithm=", paramValue := c_algorithm }, // algorithm is not enclosed to " characters
		  { id := "ck=", paramValue := c_ck },
		  { id := "ik=", paramValue := c_ik }, // already enclosed to " characters
		  { id := "qop="&v_qop, paramValue := omit }//qop

		}
	  };
      
	  return v_result;
	}
	
	/**
	* 
	* @desc  Function to calculate credentials for request that has an empty
	* entity body such as a REGISTER message and at the end put different private name
	* @param p_userprofile to get important parameters
	* @param p_method (can be "REGISTER", "INVITE",....)
	* @param p_challenge parameter from 4xx response
	* @return Credentials field
	* @verdict 
	*/
   function f_calculatecCredentialsAndChangeUserName(in SipUserProfile p_userprofile, in charstring p_method, 
	   in CommaParam_List p_challenge) return Credentials
   {
	 var Credentials v_result;
      
	 var charstring v_nonce := "";
      
	 // Use a fixed client nonce.
	 var charstring v_cnonce := "1317265";
      
	 // RFC 2617 3.2.2 username:
	 // The name of user in the specified realm.
	 var charstring v_username := p_userprofile.privUsername;
      
	 var charstring v_realm;
      
	 // RFC 2617 3.2.2.2 passwd:
	 // A known shared secret, the password of user of the specified
	 // username.
	 var charstring v_passwd := p_userprofile.passwd;
      
	 var charstring v_algorithm;
      
	 // Use a fixed nonce count.
	 const charstring c_nonceCount := "00000002";

	 var charstring v_qop := p_userprofile.qop;
      
	 var charstring v_uri := c_sipScheme & ":" & p_userprofile.registrarDomain;
      
	 // MD5 hash of empty entity body.
	 const charstring c_hEntity := "d41d8cd98f00b204e9800998ecf8427e";
      
	 var charstring v_response;
	 var charstring v_opaque;
      
      
	 // extract nonce, realm, algorithm, and opaque from challenge
      
	 v_nonce := f_extractParamValueFromChallenge(p_challenge, "nonce"); 
	 v_realm := f_extractParamValueFromChallenge(p_challenge, "realm"); 
	 v_algorithm := f_extractParamValueFromChallenge(p_challenge, "algorithm");
	 v_opaque := f_extractParamValueFromChallenge(p_challenge, "opaque");
      
	 // calculate a digest response for the Authorize header
	 v_response := fx_calculateDigestResponse(
	   v_nonce,
	   v_cnonce,
	   v_username,
	   v_realm,
	   v_passwd,
	   v_algorithm,
	   c_nonceCount,
	   p_method,
	   v_qop,
	   v_uri,
	   c_hEntity);
      
	 // Construct credentials for an Authorization field of a request.
	 v_result :=
	 {
	   digestResponse :=
	   {
		 { id := "Digest username", paramValue := "DifferentToPrivateUser"},
		 { id := "realm", paramValue := v_realm },
		 { id := "nonce", paramValue := v_nonce },
		 { id := "uri", paramValue := v_uri },
		 { id := "response", paramValue := v_response },
		 { id := "algorithm=md5", paramValue := omit }, // algorithm is not enclosed to " characters
		 { id := "cnonce", paramValue := v_cnonce },
		 { id := "opaque=""""", paramValue := omit }, // already enclosed to " characters
		 { id := "qop="&v_qop, paramValue := omit },//qop
		 { id := "nc="&c_nonceCount, paramValue := omit }//nonceCount
	   }
	 };
      
	 return v_result;
   }
	

   /**
	* 
	* @desc Function to check if param related to id from CommanParam_List exist
	* containing challenge.
	* @param p_challenge parameter from 4xx response
	* @param p_id name of parameter("nonce", "realm", "ck", "ik"...)
	* @return  parameter p_id value 
	*/
   function f_checkParamValueFromChallengeIfPresent(in CommaParam_List p_challenge, in charstring p_id) return boolean
   {
	 var boolean v_result := false;
	 var integer v_len := sizeof(p_challenge);
	 var charstring v_id := fx_putInLowercase(p_id);
      
	 for (var integer i := 0; i < v_len; i := i + 1)
	 {
	   if (fx_putInLowercase(p_challenge[i].id) == v_id)
	   {
		 v_result := true;
	   }
	 }
      
	 return v_result;
   }	
    
    /**
	* 
	* @desc Function to check if tag is present in SemicolonParam_List
	* @param p_param_l SemicolonParam_List
	* @return  boolean  true if tag is present 
	*/
    function f_checkTagPresent(SemicolonParam_List p_param_l) runs on SipComponent return boolean {
	    var integer v_numberOfParams;
	    var integer i := 0;

	    v_numberOfParams := sizeof (p_param_l);
	    while (i < v_numberOfParams) {
		    if (fx_putInLowercase(p_param_l[i].id) == c_tagId) {
			    return (true);
		    }
		    i := i + 1;
	    }
	    return (false);
    }

	/**
	 * 
	 * @desc Function to extract paramValue related to id from CommanParam_List
	 * containing challenge.
	 * @param p_challenge parameter from 4xx response
	 * @param p_id name of parameter("nonce", "realm",...)
	 * @return  parameter p_id value 
	 */
	function f_extractParamValueFromChallenge(in CommaParam_List p_challenge, in charstring p_id) return charstring
	{
	  var charstring v_result := "";
	  var integer v_len := sizeof(p_challenge);
	  var charstring v_id := fx_putInLowercase(p_id);
      
	  for (var integer i := 0; i < v_len; i := i + 1)
	  {
		if (fx_putInLowercase(p_challenge[i].id) == v_id)
		{
		  v_result := p_challenge[i].paramValue;
		}
	  }

	  if (v_result == "")
	  {
		if(match(p_id,"algorithm")) 
		{
			v_result := "MD5"
		}
		else if(match(p_id,"opaque"))
		{
			v_result := ""
		}
		else
		{
		var charstring v_tmpchar := "Cannot acquire value from credentials.";
		log ("Cannot acquire value from credentials.");
		setverdict(inconc);
		stop;
		}
	  }
      
	  return v_result;
	}
	
}//end group ParameterOperations

group FieldOperations {

	/**
	 * 
	 * @desc function adds "Tag"-parameter in "To"-headerfield
	 * @param p_to To header field that should get a Tag parameter
	 * 
	 */
	function f_addTagInTo(inout To p_to) runs on SipComponent
	{      
	  f_addParameterTagIfNotPresent(c_tagId, f_getRndTag(), p_to); 
	}

	/**
	 * 
	 * @desc addition of a single parameter in the via header field
	 * @param p_parameter_name name of parameter to be added
	 * @param p_parameter_value value of parameter to be added
	 * @param p_viaBody the via parameter to be extended
	 * @verdict 
	 */
	function f_addParameterIfNotPresent(
		in charstring p_parameter_name,
		in charstring p_parameter_value,
		inout ViaBody p_viaBody) 
		
		{
		if (ispresent (p_viaBody.viaParams)) {
			return;
		}
		p_viaBody.viaParams := {
			{
				p_parameter_name,
				p_parameter_value
			}
		};
	}    
    
	/**
	 * 
	 * @desc function to addd a parameter to the "To" header field (if there is not any parameter)
	 * @param p_parameter_name name of the parameter to be added
	 * @param p_parameter_value value of the paramter to be added
	 * @param p_to "To" header field to be extended
	 * @verdict 
	 */
	function f_addParameterTagIfNotPresent(
		in charstring p_parameter_name,
		in charstring p_parameter_value,
		inout To p_to) 
		
		{
		if (ispresent (p_to.toParams)) {
			return;
		}
		p_to.toParams := {
			{
				p_parameter_name,
				p_parameter_value
			}
		};
	}

	/**
	 * 
	 * @desc function compares the IP address of two hosts
	 * @param p_host1 hostname
	 * @param p_host2 hostname
	 * @return boolean value that is true if the IP addresses are identical
	 * @verdict 
	 */	
 	function f_equivalentHostAddr(in charstring p_host1,  in charstring p_host2) return boolean
	{
	  //A DNS server may be used
	  return(fx_getIpAddr(p_host1) == fx_getIpAddr(p_host2));
	}


	/**
		* 
		* @desc function checks if Require contains Precondition
		* @param p_message (request or response) SIP message to be analysed
		* @return true if p_id parameter exist
		*/
	function f_checkRequirePrecondition(in Request p_message)
	{
		if (ispresent(p_message.msgHeader.require))
		{
			var boolean v_precondition_found:=false;
			for (var integer v_i:=0; v_i<sizeof(p_message.msgHeader.require.optionsTags); v_i:=v_i+1){			
				if (match(p_message.msgHeader.require.optionsTags[v_i],c_tagPrecond)) {
					v_precondition_found:=true;
				}
			}
			if (not(v_precondition_found)){
				setverdict(fail);
				log("FAIL: precondition not found in Require options list!");
			}
		} 
		else
		{
			setverdict(fail);
			log("FAIL: Require options is not present!");
		}
	}

	/**
	 * 
	 * @desc function checks if P-Charging-Vector contains a particular parameter
	 * @param p_message (request or response) SIP message to be analysed
	 * @param p_id name of parameter
	 * @return true if p_id parameter exist
	 */
	function f_checkPChargingVectorHeaderParamId(in Request p_message, charstring p_id) return boolean
	{
	  var integer v_chargeParamsLen;
	  
	  if (ispresent(p_message.msgHeader.pChargingVector)) {		
		for (var integer i:=0; i<sizeof(p_message.msgHeader.pChargingVector.chargeParams); i:=i+1)
				{if(p_message.msgHeader.pChargingVector.chargeParams[i].id == p_id)
					{return (true)}
				};
	  }
	  return (false)
	}

	/**
	 * 
	 * @desc function checks if P-Charging-Vector contains a particular parameter
	 * @param p_message (request or response) SIP message to be analysed
	 * @param p_id name of parameter
	 * @return true if p_id parameter exist
	 */
	function f_checkPChargingVectorHeaderParamIdResponse(in Response p_message, charstring p_id) return boolean
	{
	  var integer v_chargeParamsLen;
	  
	  if (ispresent(p_message.msgHeader.pChargingVector)) {		
		for (var integer i:=0; i<sizeof(p_message.msgHeader.pChargingVector.chargeParams); i:=i+1)
				{if(p_message.msgHeader.pChargingVector.chargeParams[i].id == p_id)
					{return (true)}
				};
	  }
	  return (false)
	}

	/**
	 * 
	 * @desc function returns the Host/Port of a given Contact header field
	 * @param p_contact contact header field to be analysed
	 * @return Host/Port record  from the contact header field
	 */
	function f_getContactAddr(in ContactAddress p_contact) runs on SipComponent return HostPort
	  {
		var HostPort v_locAddr;
		var SipUrl    v_SipUrl;
      
		if (ischosen(p_contact.addressField.nameAddr))
		{
		  v_SipUrl := p_contact.addressField.nameAddr.addrSpec;
		}
		else
		{
		  v_SipUrl := p_contact.addressField.addrSpecUnion;
		}
      
		v_locAddr.host := v_SipUrl.hostPort.host;
      
		if (ispresent(v_SipUrl.hostPort.portField))
		{
			v_locAddr.portField := v_SipUrl.hostPort.portField;
		}
		else
		{
			v_locAddr.portField := c_defaultSipPort;
		}
      
		return(v_locAddr);
	  } // end f_getContactAddr

	/**
		* 
		* @desc function checks if History-Info-Header of the p_message contains a particular URI
		* @param p_message (request or response) SIP message to be analysed
		* @param p_URI name of parameter
		* @return true if p_URI parameter exist
		*/
	function f_checkHeaderInfoURI(in Response p_message, SipUrl p_URI) return boolean
	{
			var integer v_chargeParamsLen;
	  
			if (ispresent(p_message.msgHeader.historyInfo)) {		
		for (var integer i:=0; i<sizeof(p_message.msgHeader.historyInfo.historyInfoList); i:=i+1)
				{if(p_message.msgHeader.historyInfo.historyInfoList[i].nameAddr.addrSpec == p_URI)
					{return (true)}
				};
			}
			return (false)
	}
	
	/**
	* 
	* @desc function returns the Userinfo from  a given To header field
	* @param p_to To header field to be analysed
	* @return Userinfo from the To header field as a charstring
	*/
	function f_getUserfromTo(in To p_to) runs on SipComponent return charstring
	{
		var SipUrl    v_SipUrl;
  
  		if (ischosen(p_to.addressField.nameAddr))
		{
			v_SipUrl := p_to.addressField.nameAddr.addrSpec;
		}
		else
		{
			v_SipUrl := p_to.addressField.addrSpecUnion;
		}
  
		return(v_SipUrl.userInfo.userOrTelephoneSubscriber);
	} // end f_getUserfromTo

	/**
	 * 
	 * @desc function to generate a 32 bits random number as a charstring for tag field
	 * @param p_cSeq_s CSeq parameter used to modify the tag field value
	 * @return tag value
	 */
	function f_getRndCallId(inout CSeq p_cSeq_s) return charstring
	{
	  var charstring v_tag_value;
	  v_tag_value := fx_rndStr()&fx_rndStr();
	  //v_tag_value is initialized with a random value with at least 32 bits of randomness
	  // 4294967296 is a 32 bits integer
	  //v_tag_value := int2str(float2int(4294967296.0*rnd()) + loc_CSeq_s.seqNumber );
	  return(v_tag_value);
	}

	/**
	 * 
	 * @desc function give access to the top element of the Path header field.
	 * @param p_Request SIP message to be analysed
	 * @return NameAddr (e.g. <sip:p.home.com>) or omit
	 */
rennoch's avatar
rennoch committed
	function f_getPathHeaderTop(inout Request p_Request) return template NameAddr
	{
	  if (ispresent(p_Request.msgHeader.path)) {
	  	if (sizeof(p_Request.msgHeader.path.pathValues)>0) {
			return(p_Request.msgHeader.path.pathValues[0].nameAddr)}
	  	};
	  return(omit)
	}

	/**
	 * 
	 * @desc function updates first element of a Via headerfield list 
	 * @param p_viaBody_List address list of a Via header field
	 * @param p_source_address address to be inserted in the top element
	 */
	function f_getViaReplyAddr(inout ViaBody_List p_viaBody_List, inout address4SIP p_source_address)
	 runs on SipComponent 
	 {	
	   var ViaBody  v_viaBody;
	   var HostPort v_locAddr;
	   // The address to send message shall be updated after getting information
	   // in the Via header fied and according to 18.2.2
	   v_viaBody := p_viaBody_List[0];
      
	   // received parameter has to be addded to the via hader field
	   // Be careful it could be an Host name and not an IP Address

	   if (not f_equivalentHostAddr(valueof (v_viaBody.sentBy.host),
		   valueof (p_source_address.host))) {
		   f_addParameterIfNotPresent(
			   c_receivedId,
			   valueof (p_source_address.host),
			   v_viaBody);
	   }
	   if (ispresent(v_viaBody.sentBy.portField))
	   {
		 p_source_address.portField := valueof(v_viaBody.sentBy.portField);
	   }
	   else
	   {
		 p_source_address.portField := c_defaultSipPort;
	   }
	 }
 
	/**
	 * 
	 * @desc functions give access to an element of the Route header field (record).
	 * @param p_message (request) SIP message to be analysed
	 * @param p_index index of Route record element to be retrieved
	 * @return HostPort value of the Route element or omit
	 */
	function f_getRouteHeaderElementAddressFromRequest(in Request p_message, in integer p_index) return HostPort
	{
	  if (ispresent(p_message.msgHeader.route)) {
		if (sizeof(p_message.msgHeader.route.routeBody)>p_index) {
			return(p_message.msgHeader.route.routeBody[p_index].nameAddr.addrSpec.hostPort)}
		};
	setverdict(fail);
	return(c_hostport_dummy)
	}

	/**
	 * 
	 * @desc functions give access to an element of the Record-Route header field (record).
	 * @param p_message (request) SIP message to be analysed
	 * @param p_index index of recordRoute record element to be retrieved
	 * @return HostPort value of the Record-Route element or omit
	 */
	function f_getRecordRouteHeaderElementAddressFromRequest(in Request p_message, in integer p_index) return HostPort
	{
	  if (ispresent(p_message.msgHeader.recordRoute)) {
		if (sizeof(p_message.msgHeader.recordRoute.routeBody)>p_index) {
			return(p_message.msgHeader.recordRoute.routeBody[p_index].nameAddr.addrSpec.hostPort)}
		};
	setverdict(fail);
	return(c_hostport_dummy)
	}
    
	/**
	 * 
	 * @desc functions give access to an element of the Record-Route header field (record).
	 * @param p_message (response) SIP message to be analysed
	 * @param p_index index of recordRoute record element to be retrieved
	 * @return HostPort value of the Record-Route element or omit
	 */
	function f_getRecordRouteHeaderElementAddressFromResponse(in Response p_message, in integer p_index) return HostPort
	{
	  if (ispresent(p_message.msgHeader.recordRoute)) {
		if (sizeof(p_message.msgHeader.recordRoute.routeBody)>p_index) {
			return(p_message.msgHeader.recordRoute.routeBody[p_index].nameAddr.addrSpec.hostPort)}
		};
	setverdict(fail);
	return(c_hostport_dummy)
	}
    
	/**
	 * 
	 * @desc functions give access to an element of the Via header field (record).
	 * @param p_message (request) SIP message to be analysed
 	 * @param p_index index of via record element to be retrieved
 	 * @return HostPort value of the Via element or omit
	 */
	function f_getViaHeaderElementHostPort(in Request p_message, in integer p_index) return HostPort
	{
	
		if (sizeof(p_message.msgHeader.via.viaBody)>p_index) {
			return(p_message.msgHeader.via.viaBody[p_index].sentBy)}
	setverdict(fail);
	return(c_hostport_dummy)
	}
	
	/**
	 * 
	 * @desc functions give access to an element of the Via header field (record).
	 * @param p_message (response) SIP message to be analysed
	 * @param p_index index of via record element to be retrieved
	 * @return HostPort value of the Via element or omit
	 */	function f_getViaHeaderElementHostPortResponse(in Response p_message, in integer p_index) return HostPort
	{
	
		if (sizeof(p_message.msgHeader.via.viaBody)>p_index) {
			return(p_message.msgHeader.via.viaBody[p_index].sentBy)}
	setverdict(fail);
	return(c_hostport_dummy)
	}	
	
	/**
	 * 
	 * @desc function checks indicators if topology hiding (TH) has been applied:
	 *	- second element in via-header record has tokenized-by parameter
	 * @param p_Request SIP message to be analysed
	 * @return boolean value (true indicate TH, false otherwise)
	 */
	function f_topologyHiding(inout Request p_request) runs on SipComponent return boolean
	{ 
	  var GenericParam v_viaParameter := p_request.msgHeader.via.viaBody[1].viaParams[0]; // second element
	  if (not v_viaParameter.id == "tokenized-by") 
		 {return(false)};
	  return(true)
	}

	/**
	 * 
	 * @desc function checks indicators if topology hiding (TH) has been applied:
	 *	- any element in via-header record has tokenized-by parameter
	 * @param Response SIP message to be analysed
	 * @return boolean value (true indicate TH, false otherwise)
	 */
	function f_topologyHidingResponse(inout Response p_response) runs on SipComponent return boolean
	{ 
	 var GenericParam v_viaParameter;
	
	 for (var integer v_i := 0; v_i < sizeof(p_response.msgHeader.via.viaBody); v_i := v_i + 1) {
		
	  v_viaParameter := p_response.msgHeader.via.viaBody[v_i].viaParams[0]; // first parameter

	  if (not v_viaParameter.id == "tokenized-by") 
		 {return(false)}
	 }
	 return(true);
	
	}
	


group SetHeaders {


	/**
	 * 
	 * @desc function for setting of component variables related to message header fields 
	 *		(message type independent: CSeq, contact, via), function uses information from userprofile
	 *		
	 * @param p_cSeq_s CSeq parameter
	 * @param p_method method name for cSeq header field
	 */	
 	function f_setHeadersGeneral(inout CSeq p_cSeq_s, in charstring p_method) runs on SipComponent
	{
	  var SemicolonParam_List v_params;

	  p_cSeq_s.fieldName := CSEQ_E;
	  p_cSeq_s.seqNumber := p_cSeq_s.seqNumber + 1;
	  p_cSeq_s.method    := p_method ;
	  vc_cSeq := p_cSeq_s;
      
	  vc_contact := valueof(m_Contact(m_SipUrl_contactIpaddr(vc_userprofile)));
	  vc_branch := c_branchCookie & f_getRndTag();
	  vc_via:={
		fieldName := VIA_E,
		viaBody 	 := {valueof(m_ViaBody_currIpaddr(vc_branch, vc_userprofile))}
	  };
	}// end function f_setHeadersGeneral
	
	/**
	 * 
	 * @desc setting of general and basic Bye header fields
	 * 		in additon to the addresses (To, From, ReqUri)
	 * @param p_cSeq_s
	 */
	function f_setHeadersBYE(inout CSeq p_cSeq_s) runs on SipComponent
	{      
	  f_setHeadersGeneral(p_cSeq_s, "BYE"); // cseq, contact, branch, via

	  vc_callId := { fieldName:=CALL_ID_E, callid:=f_getRndCallId(p_cSeq_s) & c_AT & vc_userprofile.currIpaddr };

	  f_addTagInTo(vc_to);

	  vc_cancel_To := vc_to;
	  vc_caller_To := vc_to;
      
	  vc_caller_From := vc_from;
            
	  vc_reqHostPort := vc_requestUri.hostPort;
      
	}// end function f_setHeadersBYE

	/**
	 * 
	 * @desc function sets header field for the next outgoing REGISTER message
	 * @param p_cSeq_s CSeq parameter to be applied
	 */
 	function f_setHeaders_REGISTER(inout CSeq p_cSeq_s) runs on SipComponent
	{
	  var SemicolonParam_List v_params;

	  f_setHeadersGeneral(p_cSeq_s, "REGISTER"); // cseq, contact, branch, via

	  vc_requestUri:=
	  {
		scheme := c_sipScheme,
		userInfo := omit,
		hostPort := {host:=vc_userprofile.registrarDomain, portField:=omit},
		urlParameters := omit,
		headers := omit
	  };
      
	  vc_reqHostPort := vc_requestUri.hostPort;
      
	  vc_callId := { fieldName:=CALL_ID_E, callid:=f_getRndCallId(p_cSeq_s) & c_AT & vc_userprofile.currIpaddr };
	  vc_callIdReg := vc_callId;
      
	  vc_to := valueof(m_To(m_SipUrl_currDomain(vc_userprofile)));
	  v_params := {{id:=c_tagId, paramValue:=f_getRndTag()}}
	  vc_from := {fieldName := FROM_E,
		addressField :=vc_to.addressField,
		fromParams := v_params
	  };
	  
	  if(not vc_firstREGISTER_sent)
	  {