From efbe586d509f441d928229d88d40e4f6434e2d87 Mon Sep 17 00:00:00 2001 From: ebensza Date: Thu, 5 May 2016 10:20:05 +0200 Subject: [PATCH] added hostid function (artf724006) Signed-off-by: ebensza --- compiler2/Value.cc | 61 ++++++++++++++++++++++++++ compiler2/Value.hh | 5 ++- compiler2/ttcn3/compiler.l | 1 + compiler2/ttcn3/compiler.y | 12 ++++++ core/Communication.hh | 2 + core/Runtime.cc | 30 +++++++++++++ core/Runtime.hh | 3 ++ regression_test/Makefile | 2 +- regression_test/hostid/Hostid.ttcn | 69 ++++++++++++++++++++++++++++++ regression_test/hostid/Makefile | 56 ++++++++++++++++++++++++ 10 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 regression_test/hostid/Hostid.ttcn create mode 100644 regression_test/hostid/Makefile diff --git a/compiler2/Value.cc b/compiler2/Value.cc index 2fe448e..af36e4e 100644 --- a/compiler2/Value.cc +++ b/compiler2/Value.cc @@ -211,6 +211,9 @@ namespace Common { case OPTYPE_REMOVE_BOM: u.expr.v1=p.u.expr.v1->clone(); break; + case OPTYPE_HOSTID: // [v1] + u.expr.v1=p.u.expr.v1?p.u.expr.v1->clone():0; + break; case OPTYPE_ADD: // v1 v2 case OPTYPE_SUBTRACT: case OPTYPE_MULTIPLY: @@ -528,6 +531,7 @@ namespace Common { case OPTYPE_REMOVE_BOM: case OPTYPE_GET_STRINGENCODING: case OPTYPE_DECODE_BASE64: + case OPTYPE_HOSTID: delete u.expr.v1; break; case OPTYPE_ADD: // v1 v2 @@ -922,6 +926,9 @@ namespace Common { if(!p_v1) FATAL_ERROR("Value::Value()"); u.expr.v1=p_v1; break; + case OPTYPE_HOSTID: + u.expr.v1=p_v1; + break; default: FATAL_ERROR("Value::Value()"); } // switch @@ -1534,6 +1541,9 @@ namespace Common { case OPTYPE_DECODE_BASE64: u.expr.v1->set_fullname(p_fullname+"."); break; + case OPTYPE_HOSTID: // [v1] + if(u.expr.v1) u.expr.v1->set_fullname(p_fullname+"."); + break; case OPTYPE_ADD: // v1 v2 case OPTYPE_SUBTRACT: case OPTYPE_MULTIPLY: @@ -1729,6 +1739,9 @@ namespace Common { case OPTYPE_DECODE_BASE64: u.expr.v1->set_my_scope(p_scope); break; + case OPTYPE_HOSTID: // [v1] + if(u.expr.v1) u.expr.v1->set_my_scope(p_scope); + break; case OPTYPE_ADD: // v1 v2 case OPTYPE_SUBTRACT: case OPTYPE_MULTIPLY: @@ -2054,6 +2067,9 @@ namespace Common { case OPTYPE_REMOVE_BOM: u.expr.v1->set_code_section(p_code_section); break; + case OPTYPE_HOSTID: // [v1] + if(u.expr.v1) u.expr.v1->set_code_section(p_code_section); + break; case OPTYPE_ADD: // v1 v2 case OPTYPE_SUBTRACT: case OPTYPE_MULTIPLY: @@ -3167,6 +3183,7 @@ namespace Common { case OPTYPE_TTCN2STRING: case OPTYPE_GET_STRINGENCODING: case OPTYPE_ENCODE_BASE64: + case OPTYPE_HOSTID: return Type::T_CSTR; case OPTYPE_INT2UNICHAR: case OPTYPE_OCT2UNICHAR: @@ -3530,6 +3547,8 @@ namespace Common { return "encode_base64()"; case OPTYPE_DECODE_BASE64: return "decode_base64()"; + case OPTYPE_HOSTID: // [v1] + return "hostid()"; case OPTYPE_ADD: // v1 v2 return "+"; case OPTYPE_SUBTRACT: @@ -6592,6 +6611,17 @@ error: chk_expr_val_ustr_7bitchars(v1, the, opname); } break; + case OPTYPE_HOSTID: + v1=u.expr.v1 ? u.expr.v1 : 0; + if (v1) + { + Error_Context cntxt(this, "In the first operand of operation `%s'", opname); + v1->set_lowerid_to_ref(); + tt1=v1->get_expr_returntype(exp_val); + chk_expr_operandtype_cstr(tt1, second, opname, v1); + chk_expr_eval_value(v1, t_chk, refch, exp_val); + } + break; case OPTYPE_UNICHAR2OCT: // v1 [v2] v1=u.expr.v1; { @@ -7268,6 +7298,7 @@ error: case OPTYPE_DECVALUE_UNICHAR: case OPTYPE_CHECKSTATE_ANY: case OPTYPE_CHECKSTATE_ALL: + case OPTYPE_HOSTID: break; case OPTYPE_TESTCASENAME: { // - if (!my_scope) FATAL_ERROR("Value::evaluate_value()"); @@ -8568,6 +8599,7 @@ error: case OPTYPE_DECVALUE_UNICHAR: case OPTYPE_CHECKSTATE_ANY: case OPTYPE_CHECKSTATE_ALL: + case OPTYPE_HOSTID: return true; case OPTYPE_COMP_NULL: // - return false; @@ -9782,6 +9814,13 @@ error: u.expr.t1->chk_recursions(refch); refch.prev_state(); break; + case OPTYPE_HOSTID: // [v1] + if (u.expr.v1) { + refch.mark_state(); + u.expr.v1->chk_recursions(refch); + refch.prev_state(); + } + break; case OPTYPE_ADD: // v1 v2 case OPTYPE_SUBTRACT: case OPTYPE_MULTIPLY: @@ -10140,6 +10179,9 @@ error: case OPTYPE_REMOVE_BOM: self_ref |= chk_expr_self_ref_val(u.expr.v1, lhs); break; + case OPTYPE_HOSTID: // [v1] + if (u.expr.v1) self_ref |= chk_expr_self_ref_val(u.expr.v1, lhs); + break; case OPTYPE_ADD: // v1 v2 case OPTYPE_SUBTRACT: // v1 v2 case OPTYPE_MULTIPLY: // v1 v2 @@ -10520,6 +10562,10 @@ error: if (u.expr.v2) return create_stringRepr_predef2("encvalue_unichar"); else return create_stringRepr_predef1("encvalue_unichar"); } + case OPTYPE_HOSTID: { + if (u.expr.v1) return create_stringRepr_predef1("hostid"); + else return string("hostid()"); + } case OPTYPE_DECVALUE_UNICHAR: { if (u.expr.v3) { string ret_val("decvalue_unichar"); @@ -11482,6 +11528,9 @@ error: ass = u.expr.r2->get_refd_assignment(); if (parlist) str = parlist->rearrange_init_code(str, usage_mod); break; } + case OPTYPE_HOSTID: + if (u.expr.v1) str = u.expr.v1->rearrange_init_code(str, usage_mod); + break; case OPTYPE_ADD: case OPTYPE_SUBTRACT: case OPTYPE_MULTIPLY: @@ -11791,6 +11840,9 @@ error: case OPTYPE_DECVALUE_UNICHAR: generate_code_expr_decvalue_unichar(expr); break; + case OPTYPE_HOSTID: + generate_code_expr_hostid(expr); + break; case OPTYPE_OCT2HEX: generate_code_expr_predef1(expr, "oct2hex", u.expr.v1); break; @@ -12921,6 +12973,14 @@ void Value::generate_code_expr_encvalue_unichar(expression_struct *expr) expr->expr = mputstr(expr->expr, ")"); } + void Value::generate_code_expr_hostid(expression_struct *expr) + { + expr->expr = mputstr(expr->expr, "TTCN_Runtime::get_host_address("); + if (u.expr.v1) u.expr.v1->generate_code_expr_mandatory(expr); + else expr->expr = mputstr(expr->expr, "CHARSTRING(\"Ipv4orIpv6\")"); + expr->expr = mputstr(expr->expr, ")"); + } + char* Value::generate_code_char_coding_check(expression_struct *expr, Value *v, const char *name) { expression_struct expr2; @@ -13599,6 +13659,7 @@ void Value::generate_code_expr_encvalue_unichar(expression_struct *expr) case OPTYPE_PROF_RUNNING: case OPTYPE_CHECKSTATE_ANY: case OPTYPE_CHECKSTATE_ALL: + case OPTYPE_HOSTID: return true; case OPTYPE_ENCODE: case OPTYPE_DECODE: diff --git a/compiler2/Value.hh b/compiler2/Value.hh index d3c744f..4e0897e 100644 --- a/compiler2/Value.hh +++ b/compiler2/Value.hh @@ -267,6 +267,7 @@ namespace Common { OPTYPE_ANY2UNISTR, // logarg, length = 1 OPTYPE_CHECKSTATE_ANY, // [r1] v2, port or any OPTYPE_CHECKSTATE_ALL, // [r1] v2, port or all + OPTYPE_HOSTID, // [v1] NUMBER_OF_OPTYPES // must be last }; @@ -400,7 +401,7 @@ namespace Common { * COMP_ALIVE_ALL, COMP_ALIVE_ANY, TMR_RUNNING_ANY, GETVERDICT, * PROF_RUNNING */ Value(operationtype_t p_optype); - /** Constructor used by V_EXPR "v1" */ + /** Constructor used by V_EXPR "v1" or [v1] */ Value(operationtype_t p_optype, Value *p_v1); /** Constructor used by V_EXPR "ti1": LENGTHOF, SIZEOF, VALUEOF, TTCN2STRING */ Value(operationtype_t p_optype, TemplateInstance *p_ti1); @@ -927,6 +928,8 @@ namespace Common { void generate_code_expr_checkstate(expression_struct *expr); + void generate_code_expr_hostid(expression_struct *expr); + char* generate_code_char_coding_check(expression_struct *expr, Value *v, const char *name); /** Helper function for \a generate_code_init(). It handles the diff --git a/compiler2/ttcn3/compiler.l b/compiler2/ttcn3/compiler.l index 730ab70..7442e68 100644 --- a/compiler2/ttcn3/compiler.l +++ b/compiler2/ttcn3/compiler.l @@ -589,6 +589,7 @@ decode_base64 RETURN(decode_base64KeyWord); encvalue_unichar RETURN(encvalue_unicharKeyWord); decvalue_unichar RETURN(decvalue_unicharKeyWord); any2unistr RETURN(any2unistrKeyWord); +hostid RETURN(hostidKeyWord); /* Values */ diff --git a/compiler2/ttcn3/compiler.y b/compiler2/ttcn3/compiler.y index 42c3cb2..66ef651 100644 --- a/compiler2/ttcn3/compiler.y +++ b/compiler2/ttcn3/compiler.y @@ -837,6 +837,7 @@ static const string anyname("anytype"); %token encvalue_unicharKeyWord %token decvalue_unicharKeyWord %token any2unistrKeyWord +%token hostidKeyWord /* Multi-character operators */ @@ -8807,6 +8808,17 @@ PredefinedOps: $$ = new Value(Value::V_ERROR); $$->set_location(infile, @$); } +| hostidKeyWord '(' ')' + { + Value *null_value = NULL; + $$ = new Value(Value::OPTYPE_HOSTID, null_value); + $$->set_location(infile, @$); + } +| hostidKeyWord '(' optError Expression optError ')' + { + $$ = new Value(Value::OPTYPE_HOSTID, $4); + $$->set_location(infile, @$); + } ; DecValueArg: diff --git a/core/Communication.hh b/core/Communication.hh index 2d51295..1147128 100644 --- a/core/Communication.hh +++ b/core/Communication.hh @@ -13,6 +13,7 @@ * Forstner, Matyas * Kovacs, Ferenc * Raduly, Csaba + * Szabo, Bence Janos * Szabo, Janos Zoltan – initial implementation * Zalanyi, Balazs Andor * @@ -43,6 +44,7 @@ class TTCN_Communication { public: static const NetworkFamily& get_network_family() { return hcnh.get_family(); } + static bool has_local_address() { return local_addr_set; } static void set_local_address(const char *host_name); static const IPAddress *get_local_address(); static void set_mc_address(const char *host_name, diff --git a/core/Runtime.cc b/core/Runtime.cc index 625004e..8536d41 100644 --- a/core/Runtime.cc +++ b/core/Runtime.cc @@ -16,6 +16,7 @@ * Lovassy, Arpad * Raduly, Csaba * Szabados, Kristof + * Szabo, Bence Janos * Szabo, Janos Zoltan – initial implementation * Zalanyi, Balazs Andor * Pandi, Krisztian @@ -281,6 +282,35 @@ const char *TTCN_Runtime::get_host_name() return host_name; } +CHARSTRING TTCN_Runtime::get_host_address(const CHARSTRING& type) +{ + if (type != "Ipv4orIpv6" && type != "Ipv4" && type != "Ipv6") { + TTCN_error("The argument of hostid function must be Ipv4orIpv6 or Ipv4" + "or Ipv6. %s is not a valid argument.", (const char*)type); + } + + // Return empty if no host address could be retrieved + if (!TTCN_Communication::has_local_address()) { + return CHARSTRING(""); + } + const IPAddress *address = TTCN_Communication::get_local_address(); + + // Return empty if the type is not matching the address type + if (type == "Ipv4") { + const IPv4Address * ipv4 = dynamic_cast(address); + if (ipv4 == NULL) { + return CHARSTRING(""); + } + } else if (type == "Ipv6") { + const IPv6Address * ipv6 = dynamic_cast(address); + if (ipv6 == NULL) { + return CHARSTRING(""); + } + } + // Return the string representation of the address + return CHARSTRING(address->get_addr_str()); +} + CHARSTRING TTCN_Runtime::get_testcase_id_macro() { if (in_controlpart()) TTCN_error("Macro %%testcaseId cannot be used from " diff --git a/core/Runtime.hh b/core/Runtime.hh index 97e6d30..282a531 100644 --- a/core/Runtime.hh +++ b/core/Runtime.hh @@ -14,6 +14,7 @@ * Kovacs, Ferenc * Raduly, Csaba * Szabados, Kristof + * Szabo, Bence Janos * Szabo, Janos Zoltan – initial implementation * Zalanyi, Balazs Andor * @@ -153,6 +154,8 @@ public: /// Returns a string which must not be freed. static const char *get_host_name(); + + static CHARSTRING get_host_address(const CHARSTRING& type); static CHARSTRING get_testcase_id_macro(); static CHARSTRING get_testcasename(); diff --git a/regression_test/Makefile b/regression_test/Makefile index 97e3cca..e359cf6 100644 --- a/regression_test/Makefile +++ b/regression_test/Makefile @@ -46,7 +46,7 @@ assignmentNotation omitdef anytype RAW implicitMsgEncoding pattern_quadruples \ macros visibility hexstrOper ucharstrOper objidOper CRTR00015758 slider \ XML ipv6 implicitOmit testcase_defparam transparent HQ16404 cfgFile \ all_from lazyEval tryCatch text2ttcn json junitlogger ttcn2json profiler templateOmit \ -customEncoding makefilegen uidChars checkstate +customEncoding makefilegen uidChars checkstate hostid ifdef DYN DIRS += loggerplugin diff --git a/regression_test/hostid/Hostid.ttcn b/regression_test/hostid/Hostid.ttcn new file mode 100644 index 0000000..be2a1bc --- /dev/null +++ b/regression_test/hostid/Hostid.ttcn @@ -0,0 +1,69 @@ +/****************************************************************************** + * Copyright (c) 2000-2016 Ericsson Telecom AB + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Szabo, Bence Janos + * + ******************************************************************************/ + +module Hostid +{ + type port MyPort message + { + inout charstring + } with { extension "internal" } + + type component MyMTCType + { + port MyPort MyPCO1; + } + + type component MTCType + { + port MyPort MyPCO2; + } + + testcase tc_hostid() runs on MyMTCType system MTCType + { + // ip should be "127.0.0.1" aka localhost, I think... + var charstring ip := hostid("Ipv4orIpv6"); + log(ip); + if(ip != "") { + setverdict(pass); + }else { + setverdict(fail); + } + + // The same as hostid("Ipv4orIpv6") + ip := hostid(); + log(ip); + if(ip != "") { + setverdict(pass); + }else { + setverdict(fail); + } + + ip := hostid("Ipv4"); + if(ip != "") { + setverdict(pass); + }else { + setverdict(fail); + } + + ip := hostid("Ipv6"); + if(ip == "") { + setverdict(pass); + }else { + setverdict(fail); + } + } + + control + { + execute(tc_hostid()); + } +} diff --git a/regression_test/hostid/Makefile b/regression_test/hostid/Makefile new file mode 100644 index 0000000..614df8f --- /dev/null +++ b/regression_test/hostid/Makefile @@ -0,0 +1,56 @@ +############################################################################## +# Copyright (c) 2000-2016 Ericsson Telecom AB +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Szabo, Bence Janos +# +############################################################################## +TOPDIR := .. +include $(TOPDIR)/Makefile.regression + +.SUFFIXES: .ttcn .hh +.PHONY: all clean dep run + +TTCN3_LIB = ttcn3$(RT2_SUFFIX)-parallel$(DYNAMIC_SUFFIX) + +TTCN3_MODULES = Hostid.ttcn + +GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.cc) +GENERATED_HEADERS = $(GENERATED_SOURCES:.cc=.hh) +ifdef CODE_SPLIT +GENERATED_SOURCES := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), .cc _seq.cc _set.cc _seqof.cc _setof.cc _union.cc)) +endif + +USER_SOURCES = + +OBJECTS = $(GENERATED_SOURCES:.cc=.o) $(USER_SOURCES:.cc=.o) + +TARGET = Hostid$(EXESUFFIX) + +all: $(TARGET) + +$(TARGET): $(GENERATED_SOURCES) $(USER_SOURCES) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ -L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) -L$(OPENSSL_DIR)/lib -lcrypto $($(PLATFORM)_LIBS) + +$(GENERATED_SOURCES) $(GENERATED_HEADERS): compile + @if [ ! -f $@ ]; then $(RM) compile; $(MAKE) compile; fi + +compile: $(TTCN3_MODULES) $(ASN1_MODULES) + $(filter-out -Nold -E, $(TTCN3_COMPILER)) $(COMPILER_FLAGS) $^ + touch compile + +clean distclean: + -rm -f $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \ + $(GENERATED_SOURCES) *.log Makefile.bak + +dep: $(GENERATED_SOURCES) + makedepend $(CPPFLAGS) $(GENERATED_SOURCES) + +run: $(TARGET) + $(TTCN3_DIR)/bin/ttcn3_start $^ + + -- 2.34.1