/******************************************************************************
* Copyright (c) 2000-2019 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
///////////////////////////////////////////////////////////////////////////////
//
//  File:               TCCInterface_Functions.ttcn
//  Description:        TCC Useful Functions: Interface Functions
//  Rev:                R36B
//  Prodnr:             CNL 113 472
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
//  Module: TCCInterface_Functions
// 
//  Purpose:
//    This module supports network related operations
// 
//  Module Parameters:
//      - 
// 
//  Module depends on:
//    -
//
///////////////////////////////////////////////////////////////////////////////
module TCCInterface_Functions {
import from TCCConversion_Functions all
///////////////////////////////////////////////////////////////////////////////
//  Record: IPAddresses
// 
//  Purpose:
//    To store multiple ipv4 and ipv6 addresses typically describing a host.
//    Warning: An own CharstringList type is definied, that is not compatible
//    with any other record of charstring in TITAN's load run time library, which
//    is the defalt, and thus it is not able to directly copy this lists for example
//    to EPTF_CharstringList. 
//
///////////////////////////////////////////////////////////////////////////////
type record of charstring CharstringList;
type record IPAddresses{
  CharstringList ipv4List,
  CharstringList ipv6List
}

///////////////////////////////////////////////////////////////////////////////
//  Enum: TCCInterface_IPAddressType
// 
//  Purpose:
//   An enum type for setting the type of IP addresses. Possible values:
//	IPv4 - for IPv4 type addresses
//	IPv6 - for IPv6 type addresses
//	etc
///////////////////////////////////////////////////////////////////////////////
type enumerated TCCInterface_IPAddressType 
{
  IPv4,
  IPv6,
  UNKNOWN
};

///////////////////////////////////////////////////////////////////////////////
//  Enum: TCCInterface_ProtocolType
// 
//  Purpose:
//   An enum type for setting the type of protocols. Possible values:
//	PROTO_TYPE_UDP
//	PROTO_TYPE_TCP
//	PROTO_TYPE_SCTP
///////////////////////////////////////////////////////////////////////////////
type enumerated  TCCInterface_ProtocolType
{
	PROTO_TYPE_UDP,
	PROTO_TYPE_TCP,
	PROTO_TYPE_SCTP
};
///////////////////////////////////////////////////////////////////////////////
//  Enum: TCCInterface_PortStatus
// 
//  Purpose:
//   An enum type for represent a status of UDP/TCP/SCTP port. Possible values:
//	PORT_STATUS_WAS_VACANT,
//	PORT_STATUS_WAS_OCCUPIED,
//	PORT_STATUS_PARAMETER_ERROR
//
//  Note: 
//     The value represent  the status of the port/ip/protocol in a distant past.
//     It tells nothing the status at the time you would like to use.
///////////////////////////////////////////////////////////////////////////////
type enumerated  TCCInterface_PortStatus
{
	PORT_STATUS_WAS_VACANT,          // The IP/port/prtocol was available for use at the time of the check.
	PORT_STATUS_WAS_OCCUPIED,        // The IP/port/prtocol was used at the time of the check.
	PORT_STATUS_PARAMETER_ERROR,     // You passed some garbage value as parameters.
        PORT_STATUS_INTERNAL_ERROR       // Error behind the screen. Take a brake and rest in space
};


///////////////////////////////////////////////////////////////////////////////
//  Function: f_getPortAvailabilityStatus
// 
//  Purpose:
//    The function returns what was the status of the IP/port/Protocol some time ago.
//    The returned information was outdated before the function returns, so use it only 
//    as a hint.
//
//  Parameters:
//    ipAddress - *in* *charstring* - the IP address
//    portNumber - *in* *integer* - the port number to check
//    protocolType - *in* *TCCInterface_ProtocolType* - the protocol UDP/TCP/SCTP
// 
//  Return Value:
//    The status in the past (used/ not used) or error report.
//
// 
///////////////////////////////////////////////////////////////////////////////
external function f_getPortAvailabilityStatus(in charstring ipAddress, in integer portNumber,
                       in TCCInterface_ProtocolType protocolType) return TCCInterface_PortStatus;



///////////////////////////////////////////////////////////////////////////////
//  Function: f_setIP
// 
//  Purpose:
//    Set IP address, subnet mask and broadcast address in a network inteface
//    If number is set, a range of virtual interfaces are set up with
//    continuous IP address (no subnet mask, broadcast checking)
//
//  Parameters:
//    interface - *in* *charstring* - network interface
//    ipaddress - *in* *charstring* - starting IP address
//    subnetmask - *in* *charstring* - subnetmask
//    broadcast - *in* *charstring* - broadcast
//    number - *in* *integer* - number of interfaces to set up
// 
//  Return Value:
//    -
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_setIP(in charstring interface, in charstring ipaddress, 
  in charstring subnetmask, in charstring broadcast,
  in integer number := 1);

///////////////////////////////////////////////////////////////////////////////
//  Function: f_setIP_ip
// 
//  Purpose:
//    Set IP address, subnet mask in a network inteface
//    Uses RTLN netlink interface on linux, which is faster than original POSIX.    
//
//  Parameters:
//    interface - *in* *charstring* - network interface
//    ipaddress - *in* *charstring* - starting IP address
//    prefix - *in* *integer* - subnetmask of the ipaddress
//    v_set - *in* *integer* - Determines behavior, 1 sets, other deletes the IP
// 
//  Return Value:
//    True on success, false in other cases.
//
//  Errors:
//    Many possibilities, all generates a TTCN_warning
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_setIP_ip(in charstring interface, in charstring ipaddress, in integer prefix := 32, in integer v_set := 1) return boolean;

///////////////////////////////////////////////////////////////////////////////
//  Function: f_delIP_ip
// 
//  Purpose:
//    Deletes IP address, subnet mask in a network inteface
//    Uses RTLN netlink interface on linux, which is faster than original POSIX.    
//
//  Parameters:
//    interface - *in* *charstring* - network interface
//    ipaddress - *in* *charstring* - starting IP address
//    prefix - *in* *integer* - subnetmask of the ipaddress
// 
//  Return Value:
//    True on success, false in other cases.
//
//  Errors:
//    Many possibilities, all generates a TTCN_warning
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_delIP_ip(in charstring interface, in charstring ipaddress, in integer prefix := 32) return boolean;

///////////////////////////////////////////////////////////////////////////////
//  Function: f_deleteIP
// 
//  Purpose:
//    Delete IP address from a network inteface
//
//  Parameters:
//    interface - *in* *charstring* - network interface
// 
//  Return Value:
//    -
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////  
external function f_deleteIP(in charstring interface);

///////////////////////////////////////////////////////////////////////////////
//  Function: f_getIP
// 
//  Purpose:
//    Get IP address, subnet mask and broadcast address from a network inteface
//
//  Parameters:
//    interface - *in* *charstring* - network interface
//    ip - *out* *charstring* - IP address
//    netmask - *out* *charstring* - netmask
//    broadcast - *out* *charstring* - broadcast
//    addressType - *in* <TCCInterface_IPAddressType> - type of IP addresses (default is IPv4)
// 
//  Return Value:
//    -
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////  
external function f_getIP(in charstring interface, out charstring ip,
  out charstring netmask, out charstring broadcast, in TCCInterface_IPAddressType addressType:=IPv4);

///////////////////////////////////////////////////////////////////////////////
//  Function: f_setInterfaceUp
// 
//  Purpose:
//    Set up a network interface
//
//  Parameters:
//    interface - *in* *charstring* - network interface
//    addressType - *in* <TCCInterface_IPAddressType> - type of IP addresses (default is IPv4)
// 
//  Return Value:
//    -
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_setInterfaceUp(in charstring interface, in TCCInterface_IPAddressType addressType:=IPv4);

///////////////////////////////////////////////////////////////////////////////
//  Function: f_setInterfaceDown
// 
//  Purpose:
//    Set down a network interface
//
//  Parameters:
//    interface - *in* *charstring* - network interface
//    addressType - *in* <TCCInterface_IPAddressType> - type of IP addresses (default is IPv4)
// 
//  Return Value:
//    -
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_setInterfaceDown(in charstring interface, in TCCInterface_IPAddressType addressType:=IPv4);

///////////////////////////////////////////////////////////////////////////////
//  Function: f_getHostName
// 
//  Purpose:
//    Get name of host
//
//  Parameters:
//    -
// 
//  Return Value:
//    charstring - name of the host
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_getHostName() return charstring;

///////////////////////////////////////////////////////////////////////////////
//  Function: f_getIpAddr
// 
//  Purpose:
//    Get IP address of host
//
//  Parameters:
//    hostname - *in* *charstring* - name of the host
//    addressType - *in* <TCCInterface_IPAddressType> - type of IP addresses (default is IPv4)
// 
//  Return Value:
//    charstring - IP address of the host
//
//  Errors:
//    - 
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_getIpAddr(in charstring hostname, in TCCInterface_IPAddressType addressType:=IPv4) return charstring;

///////////////////////////////////////////////////////////////////////////////
//  Function: f_getIpAddresses
// 
//  Purpose:
//    Get both IPv4 and IPv6 addresses of host. 
//
//  Parameters:
//    hostname - *in* *charstring* - name of the host
// 
//  Return Value:
//    Initialized IPAddresses structure
//
//  Errors:
//    Drops TTCN_Warnings on fault
// 
//  Detailed description:
//    -
// 
///////////////////////////////////////////////////////////////////////////////
external function f_getIpAddresses(in charstring hostname) return IPAddresses;

///////////////////////////////////////////////////////////////////////////////
//  Function: f_verifyIpAddr
//
//  Purpose:
//    Returns the given host is valid IPv4/IPv6 or not.
//
//  Parameters:
//    pl_host   - *in* *charstring* - host to investigate
//    pl_ipType - *TCCInterface_IPAddressType* - type of IP
//
//  Return Value:
//    boolean - true if given host is an IPv4 or IPv6 valid address
//
//  Errors:
//    -
//
//  Detailed description:
//    If pl_ipType  is not default value "UNKNOWN",
//       function also check matching real IP type of host with
//       given type in parameter
//
///////////////////////////////////////////////////////////////////////////////
public function f_verifyIpAddr(in charstring pl_host, in TCCInterface_IPAddressType pl_ipType := UNKNOWN) return boolean
{
  var boolean vl_valid_ip := f_convertIPAddrToBinary(pl_host) != ''O;
  if(vl_valid_ip and pl_ipType != UNKNOWN)
  {
    var boolean vl_ipv6_real := f_strstr(pl_host, ":") != -1;
    if(pl_ipType == IPv4){return not vl_ipv6_real}
    else if (pl_ipType == IPv6) {return vl_ipv6_real}
  }
  return vl_valid_ip
}
}
