LCOV - code coverage report
Current view: top level - DCPS/RTPS - MessageUtils.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 14 18 77.8 %
Date: 2023-04-30 01:32:43 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *
       4             :  * Distributed under the OpenDDS License.
       5             :  * See: http://www.opendds.org/license.html
       6             :  */
       7             : 
       8             : #ifndef OPENDDS_DCPS_RTPS_MESSAGEUTILS_H
       9             : #define OPENDDS_DCPS_RTPS_MESSAGEUTILS_H
      10             : 
      11             : #include "RtpsCoreTypeSupportImpl.h"
      12             : #include "rtps_export.h"
      13             : 
      14             : #include <dds/DCPS/Hash.h>
      15             : #include <dds/DCPS/Util.h>
      16             : #include <dds/DCPS/Message_Block_Ptr.h>
      17             : #include <dds/DCPS/Serializer.h>
      18             : #include <dds/DCPS/TypeSupportImpl.h>
      19             : #include <dds/DCPS/SequenceNumber.h>
      20             : #include <dds/DCPS/TimeTypes.h>
      21             : #include <dds/DCPS/GuidConverter.h>
      22             : 
      23             : #include <dds/DdsDcpsInfoUtilsC.h>
      24             : #include <dds/DdsDcpsInfoUtilsTypeSupportImpl.h>
      25             : 
      26             : #include <ace/INET_Addr.h>
      27             : #include <ace/Message_Block.h>
      28             : 
      29             : #include <cstring>
      30             : 
      31             : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
      32             : 
      33             : namespace OpenDDS {
      34             : namespace RTPS {
      35             : 
      36             : using DCPS::GuidPrefix_t;
      37             : using DCPS::GUID_t;
      38             : using DCPS::assign;
      39             : using DCPS::EntityId_t;
      40             : using DCPS::GUID_tKeyLessThan;
      41             : using DCPS::LogGuid;
      42             : using DCPS::String;
      43             : using DCPS::SequenceNumber;
      44             : using DCPS::TimeDuration;
      45             : using DCPS::MonotonicTimePoint;
      46             : using DCPS::SystemTimePoint;
      47             : using DCPS::DCPS_debug_level;
      48             : using DCPS::RcHandle;
      49             : 
      50             : template <typename T>
      51             : void marshal_key_hash(const T& msg, KeyHash_t& hash)
      52             : {
      53             :   using DCPS::Serializer;
      54             :   using DCPS::Encoding;
      55             :   typedef DCPS::MarshalTraits<T> Traits;
      56             : 
      57             :   DCPS::KeyOnly<const T> ko(msg);
      58             : 
      59             :   static const size_t hash_limit = 16;
      60             :   std::memset(hash.value, 0, hash_limit);
      61             : 
      62             :   const Encoding encoding(Encoding::KIND_XCDR1, DCPS::ENDIAN_BIG);
      63             :   const OpenDDS::DCPS::SerializedSizeBound bound =
      64             :     Traits::key_only_serialized_size_bound(encoding);
      65             : 
      66             :   if (bound && bound.get() <= hash_limit) {
      67             :     // If it is bounded and can always fit in 16 bytes, we will use the
      68             :     // marshaled key
      69             :     ACE_Message_Block mb(hash_limit);
      70             :     Serializer out_serializer(&mb, encoding);
      71             :     out_serializer << ko;
      72             :     std::memcpy(hash.value, mb.rd_ptr(), mb.length());
      73             : 
      74             :   } else {
      75             :     // We will use the hash of the marshaled key
      76             :     ACE_Message_Block mb(serialized_size(encoding, ko));
      77             :     Serializer out_serializer(&mb, encoding);
      78             :     out_serializer << ko;
      79             : 
      80             :     DCPS::MD5Hash(hash.value, mb.rd_ptr(), mb.length());
      81             :   }
      82             : }
      83             : 
      84             : OpenDDS_Rtps_Export
      85             : const DCPS::Encoding& get_locators_encoding();
      86             : 
      87             : OpenDDS_Rtps_Export
      88             : DDS::ReturnCode_t blob_to_locators(const DCPS::TransportBLOB& blob,
      89             :                                    DCPS::LocatorSeq& locators,
      90             :                                    bool* requires_inline_qos = 0,
      91             :                                    unsigned int* pBytesRead = 0);
      92             : 
      93             : OpenDDS_Rtps_Export
      94             : void locators_to_blob(const DCPS::LocatorSeq& locators,
      95             :                       DCPS::TransportBLOB& blob);
      96             : 
      97             : OpenDDS_Rtps_Export
      98             : DCPS::LocatorSeq transport_locator_to_locator_seq(const DCPS::TransportLocator& info);
      99             : 
     100             : template <typename T>
     101          13 : void message_block_to_sequence(const ACE_Message_Block& mb_locator, T& out)
     102             : {
     103          13 :   out.length (CORBA::ULong(mb_locator.length()));
     104          13 :   std::memcpy (out.get_buffer(), mb_locator.rd_ptr(), mb_locator.length());
     105          13 : }
     106             : 
     107             : #ifndef OPENDDS_SAFETY_PROFILE
     108             : 
     109           4 : inline bool operator==(const Duration_t& x, const Duration_t& y)
     110             : {
     111           4 :   return x.seconds == y.seconds && x.fraction == y.fraction;
     112             : }
     113             : 
     114           2 : inline bool operator==(const VendorId_t& v1, const VendorId_t& v2)
     115             : {
     116           2 :   return (v1.vendorId[0] == v2.vendorId[0] && v1.vendorId[1] == v2.vendorId[1]);
     117             : }
     118             : 
     119             : #endif
     120             : 
     121           4 : inline bool operator<(const ProtocolVersion_t& v1, const ProtocolVersion_t& v2)
     122             : {
     123           4 :   return (v1.major < v2.major || (v1.major == v2.major && v1.minor < v2.minor));
     124             : }
     125             : 
     126             : OpenDDS_Rtps_Export
     127             : DCPS::TimeDuration rtps_duration_to_time_duration(const Duration_t& rtps_duration, const ProtocolVersion_t& version, const VendorId_t& vendor);
     128             : 
     129             : OpenDDS_Rtps_Export
     130             : bool bitmapNonEmpty(const SequenceNumberSet& snSet);
     131             : 
     132           0 : inline DCPS::SequenceNumber to_opendds_seqnum(const RTPS::SequenceNumber_t& rtps_seqnum)
     133             : {
     134           0 :   DCPS::SequenceNumber opendds_seqnum;
     135           0 :   opendds_seqnum.setValue(rtps_seqnum.high, rtps_seqnum.low);
     136           0 :   return opendds_seqnum;
     137             : }
     138             : 
     139         108 : inline RTPS::SequenceNumber_t to_rtps_seqnum(const DCPS::SequenceNumber& opendds_seqnum)
     140             : {
     141             :   RTPS::SequenceNumber_t rtps_seqnum;
     142         108 :   rtps_seqnum.high = opendds_seqnum.getHigh();
     143         108 :   rtps_seqnum.low = opendds_seqnum.getLow();
     144         108 :   return rtps_seqnum;
     145             : }
     146             : 
     147             : inline void append_submessage(RTPS::Message& message, const RTPS::InfoDestinationSubmessage& submessage)
     148             : {
     149             :   RTPS::Submessage sm;
     150             :   sm.info_dst_sm(submessage);
     151             :   DCPS::push_back(message.submessages, sm);
     152             : }
     153             : 
     154             : inline void append_submessage(RTPS::Message& message, const RTPS::InfoTimestampSubmessage& submessage)
     155             : {
     156             :   RTPS::Submessage sm;
     157             :   sm.info_ts_sm(submessage);
     158             :   DCPS::push_back(message.submessages, sm);
     159             : }
     160             : 
     161             : inline void append_submessage(RTPS::Message& message, const RTPS::DataSubmessage& submessage)
     162             : {
     163             :   RTPS::Submessage sm;
     164             :   sm.data_sm(submessage);
     165             :   DCPS::push_back(message.submessages, sm);
     166             : }
     167             : 
     168             : inline void append_submessage(RTPS::Message& message, const RTPS::DataFragSubmessage& submessage)
     169             : {
     170             :   RTPS::Submessage sm;
     171             :   sm.data_frag_sm(submessage);
     172             :   DCPS::push_back(message.submessages, sm);
     173             : }
     174             : 
     175             : #ifdef OPENDDS_SECURITY
     176             : inline DDS::Security::ParticipantSecurityAttributesMask
     177             : security_attributes_to_bitmask(const DDS::Security::ParticipantSecurityAttributes& sec_attr)
     178             : {
     179             :   using namespace DDS::Security;
     180             : 
     181             :   ParticipantSecurityAttributesMask result = PARTICIPANT_SECURITY_ATTRIBUTES_FLAG_IS_VALID;
     182             :   if (sec_attr.is_rtps_protected) {
     183             :     result |= PARTICIPANT_SECURITY_ATTRIBUTES_FLAG_IS_RTPS_PROTECTED;
     184             :   }
     185             :   if (sec_attr.is_discovery_protected) {
     186             :     result |= PARTICIPANT_SECURITY_ATTRIBUTES_FLAG_IS_DISCOVERY_PROTECTED;
     187             :   }
     188             :   if (sec_attr.is_liveliness_protected) {
     189             :     result |= PARTICIPANT_SECURITY_ATTRIBUTES_FLAG_IS_LIVELINESS_PROTECTED;
     190             :   }
     191             :   return result;
     192             : }
     193             : 
     194             : inline DDS::Security::EndpointSecurityAttributesMask
     195             : security_attributes_to_bitmask(const DDS::Security::EndpointSecurityAttributes& sec_attr)
     196             : {
     197             :   using namespace DDS::Security;
     198             : 
     199             :   EndpointSecurityAttributesMask result = ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_VALID;
     200             : 
     201             :   if (sec_attr.base.is_read_protected)
     202             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_READ_PROTECTED;
     203             : 
     204             :   if (sec_attr.base.is_write_protected)
     205             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_WRITE_PROTECTED;
     206             : 
     207             :   if (sec_attr.base.is_discovery_protected)
     208             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_DISCOVERY_PROTECTED;
     209             : 
     210             :   if (sec_attr.base.is_liveliness_protected)
     211             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_LIVELINESS_PROTECTED;
     212             : 
     213             :   if (sec_attr.is_submessage_protected)
     214             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_SUBMESSAGE_PROTECTED;
     215             : 
     216             :   if (sec_attr.is_payload_protected)
     217             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_PAYLOAD_PROTECTED;
     218             : 
     219             :   if (sec_attr.is_key_protected)
     220             :     result |= ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_KEY_PROTECTED;
     221             : 
     222             :   return result;
     223             : }
     224             : 
     225             : inline DDS::OctetSeq
     226             : handle_to_octets(DDS::Security::NativeCryptoHandle handle)
     227             : {
     228             :   DDS::OctetSeq handleOctets(sizeof handle);
     229             :   handleOctets.length(handleOctets.maximum());
     230             :   unsigned char* rawHandleOctets = handleOctets.get_buffer();
     231             :   unsigned int handleTmp = handle;
     232             :   for (unsigned int j = sizeof handle; j > 0; --j) {
     233             :     rawHandleOctets[j - 1] = handleTmp & 0xff;
     234             :     handleTmp >>= 8;
     235             :   }
     236             :   return handleOctets;
     237             : }
     238             : #endif
     239             : 
     240             : } // namespace RTPS
     241             : } // namespace OpenDDS
     242             : 
     243             : OPENDDS_END_VERSIONED_NAMESPACE_DECL
     244             : 
     245             : #endif /* OPENDDS_DCPS_RTPS_MESSAGEUTILS_H */

Generated by: LCOV version 1.16