/****************************************************************************** * 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 * * Contributors: * Krisztian Pandi ******************************************************************************/ // // File: UD_PT.cc // Description: UD test port source // Rev: R2A // Prodnr: CNL 113 702 // #include "UD_PT.hh" #include "UD_PortType.hh" #include "UD_Types.hh" #include #include #include #include #include #define DEFAULT_LOCAL_PORT (50000) #define DEFAULT_NUM_CONN (10) using namespace UD__Types; namespace UD__PortType { INTEGER simpleGetMsgLen(const OCTETSTRING& stream, ro__integer& /*args*/) { return stream.lengthof(); } UD__PT_PROVIDER::UD__PT_PROVIDER(const char *par_port_name) : PORT(par_port_name) , debugging(false) , sock_type(SOCK_STREAM) , target_fd(-1) { conn_list = NULL; num_of_conn = 0; conn_list_length = 0; operation_mode=false; conn_list_server = NULL; num_of_conn_server = 0; conn_list_length_server = 0; defaultGetMsgLen = simpleGetMsgLen; defaultMsgLenArgs = new ro__integer(NULL_VALUE); } UD__PT_PROVIDER::~UD__PT_PROVIDER() { Free(conn_list); Free(conn_list_server); } void UD__PT_PROVIDER::setUpSocket() { log("entering UD__PT::setUpSocket()"); log("leaving UD__PT::setUpSocket()"); } void UD__PT_PROVIDER::closeDownSocket() { log("entering UD__PT::closeDownSocket()"); for(int a=0;aput_s(size_read, msg); bool msgFound = false; do { /* determine message length by callback function */ if (conn_list[a].getMsgLen != simpleGetMsgLen) { if (conn_list[a].msgLen == -1) { OCTETSTRING oct; (*conn_list[a].buf)->get_string(oct); conn_list[a].msgLen = conn_list[a].getMsgLen.invoke(oct, *conn_list[a].msgLenArgs); } } else { conn_list[a].msgLen = (*conn_list[a].buf)->get_len(); } /* did we find a complete message inside the buffer? */ msgFound = (conn_list[a].msgLen != -1) && (conn_list[a].msgLen <= (int)conn_list[a].buf[0]->get_len()); if (msgFound) { parameters.data() = OCTETSTRING(conn_list[a].msgLen, conn_list[a].buf[0]->get_data()); conn_list[a].buf[0]->set_pos((size_t)conn_list[a].msgLen); conn_list[a].buf[0]->cut(); parameters.id() = a; incoming_message(parameters); conn_list[a].msgLen = -1; } /* continue to iterate as long as data is left and callback continues to find full messages inside */ } while (msgFound && conn_list[a].buf[0]->get_len() != 0); } else { /* SOCK_DGRAM, SOCK_SEQPACKET: forward directly to * testsuite, bypassing buffering + getMsgLen() */ parameters.data() = OCTETSTRING(size_read, msg); parameters.id() = a; incoming_message(parameters); } } } } } for(int a=0;a= 0 ){ conn_list[cn].status = 1; num_of_conn++; strcpy(conn_list_server[a].remote_Addr.sun_path, store_addr.sun_path); strcpy(conn_list[cn].remote_Addr.sun_path, store_addr.sun_path); result.result()().result__code() = UD__Types::UD__Result__code::SUCCESS; result.result()().err() = OMIT_VALUE; } else{ result.result()().result__code() = UD__Types::UD__Result__code::ERROR_; result.result()().err() = "Accept rejected"; } result.id()=cn; result.path() = conn_list[cn].remote_Addr.sun_path; incoming_message(result); Handler_Add_Fd_Read(conn_list[cn].fd); } } } //} log("leaving UD__PT::Handle_Fd_Event_Readable()"); } void UD__PT_PROVIDER::user_map(const char *system_port) { log("entering UD__PT::user_map()"); if (conn_list != NULL) TTCN_error("UD Test Port (%s): Internal error: " "conn_list is not NULL when mapping.", port_name); conn_list = (conn_data*)Malloc(DEFAULT_NUM_CONN * sizeof(*conn_list)); num_of_conn=0; conn_list_length=DEFAULT_NUM_CONN; for(int a=0;a 0){ if(conn_list[send_par.id()].status!=0){ int close_fd=conn_list[send_par.id()].fd; conn_list[send_par.id()].status=0; num_of_conn--; Handler_Remove_Fd_Read(close_fd); close(close_fd); unlink(conn_list[send_par.id()].remote_Addr.sun_path); } else{ //it has been already removed } }*/ if(conn_list_server[send_par.id()].status!=0){ int close_fd=conn_list_server[send_par.id()].fd; conn_list_server[send_par.id()].status=0; num_of_conn_server--; Handler_Remove_Fd_Read(close_fd); close(close_fd); unlink(conn_list_server[send_par.id()].remote_Addr.sun_path); } log("leaving UD__PT::outgoing_send(UD__Shutdown)"); } void UD__PT_PROVIDER::outgoing_send(const UD__Types::UD__send__data& send_par) { log("entering UD__PT::outgoing_send(UD__send__data)"); logHex("Sending data: ", send_par.data()); int sock; struct sockaddr_un targetAddr; targetAddr.sun_family = AF_UNIX; int cn=send_par.id(); //targetAddr.sun_path = conn_list[cn].remote_Addr.sun_path; strcpy(targetAddr.sun_path,conn_list[cn].remote_Addr.sun_path); sock=conn_list[cn].fd; int nbytes = send_par.data().lengthof(); int nrOfBytesSent; //if ((nrOfBytesSent = write(sock, send_par.data(), nbytes) ) != nbytes) if ((nrOfBytesSent = send(sock, send_par.data(), nbytes, 0) ) != nbytes) { TTCN_error("Write error"); } log("Nr of bytes sent = %d", nrOfBytesSent); if (nrOfBytesSent != send_par.data().lengthof()) { TTCN_error("Sendto system call failed: %d bytes was sent instead of %d", nrOfBytesSent, send_par.data().lengthof()); } log("leaving UDPasp__PT::outgoing_send(ASP__UDP__message)"); } void f__UD__PT_PROVIDER__setGetMsgLen(UD__PT_PROVIDER& portRef, const INTEGER& connId, f__UD__getMsgLen& f, const ro__integer& msgLenArgs) { if ((int)connId == -1) { portRef.defaultGetMsgLen = f; delete portRef.defaultMsgLenArgs; portRef.defaultMsgLenArgs = new Socket__API__Definitions::ro__integer(msgLenArgs); } else { if (!portRef.isConnIdValid(connId)) { TTCN_error("UD: cannot setGetMsgLen: connId %d not valid", (int)connId); return; } portRef.conn_list[(int)connId].getMsgLen = f; delete portRef.conn_list[connId].msgLenArgs; portRef.conn_list[connId].msgLenArgs = new Socket__API__Definitions::ro__integer(msgLenArgs); } } void f__UD__setGetMsgLen(UD__PT& portRef, const INTEGER& connId, f__UD__getMsgLen& f, const ro__integer& msgLenArgs) { f__UD__PT_PROVIDER__setGetMsgLen(portRef, connId, f, msgLenArgs); } }