2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define DEBUG_EC_IMPLEMENTATION
28 #include <common/Format.h> // Needed for CFormat
31 #include "ECTag.h" // Needed for ECTag
32 #include "ECSocket.h" // Needed for CECSocket
33 #include "ECSpecialTags.h" // Needed for CValueMap
35 /**********************************************************
39 **********************************************************/
41 //! Defines the Null tag which may be returned by GetTagByNameSafe.
42 const CECTag
CECTag::s_theNullTag
;
45 * Creates a new null-valued CECTag instance
48 * @see GetTagByNameSafe
52 m_dataType(EC_TAGTYPE_UNKNOWN
),
54 m_tagData(NULL
) // All access functions check m_dataType, so no need to allocate a dummy buffer.
59 * Creates a new CECTag instance from the given data
61 * @param name TAG name
62 * @param length length of data buffer
63 * @param data TAG data
66 CECTag::CECTag(ec_tagname_t name
, unsigned int length
, const void *data
) : m_tagName(name
)
71 memcpy(m_tagData
, data
, m_dataLen
);
75 m_dataType
= EC_TAGTYPE_CUSTOM
;
79 * Creates a new CECTag instance for custom data
81 * @param name TAG name
82 * @param length length of data buffer that will be alloc'ed
83 * @param dataptr pointer to a void pointer which will be assigned the internal TAG data buffer
85 * \note TAG data buffer has to be filled with valid data after the ctor
87 CECTag::CECTag(ec_tagname_t name
, unsigned int length
, void **dataptr
) : m_tagName(name
)
92 m_dataType
= EC_TAGTYPE_CUSTOM
;
96 * Creates a new CECTag instance, which contains an IPv4 address.
98 * This function takes care of the endianness of the port number.
100 * @param name TAG name
101 * @param data The EC_IPv4_t class containing the IPv4 address.
105 CECTag::CECTag(ec_tagname_t name
, const EC_IPv4_t
& data
) : m_tagName(name
)
108 m_dataLen
= sizeof(EC_IPv4_t
);
110 RawPokeUInt32( ((EC_IPv4_t
*)m_tagData
)->m_ip
, RawPeekUInt32( data
.m_ip
) );
111 ((EC_IPv4_t
*)m_tagData
)->m_port
= ENDIAN_HTONS(data
.m_port
);
112 m_dataType
= EC_TAGTYPE_IPV4
;
116 * Creates a new CECTag instance, which contains a MD4 hash.
118 * This function takes care to store hash in network byte order.
120 * @param name TAG name
121 * @param data The CMD4Hash class containing the MD4 hash.
125 CECTag::CECTag(ec_tagname_t name
, const CMD4Hash
& data
) : m_tagName(name
)
129 RawPokeUInt64( m_tagData
, RawPeekUInt64( data
.GetHash() ) );
130 RawPokeUInt64( m_tagData
+ 8, RawPeekUInt64( data
.GetHash() + 8 ) );
131 m_dataType
= EC_TAGTYPE_HASH16
;
135 * Creates a new CECTag instance, which contains a string
137 * @param name TAG name
138 * @param data wxString object, it's contents are converted to UTF-8.
140 * @see GetStringDataSTL()
142 CECTag::CECTag(ec_tagname_t name
, const std::string
& data
) : m_tagName(name
)
144 ConstructStringTag(name
, data
);
148 * Creates a new CECTag instance, which contains a string
150 * @param name TAG name
151 * @param data wxString object, it's contents are converted to UTF-8.
153 * @see GetStringData()
155 CECTag::CECTag(ec_tagname_t name
, const wxString
& data
)
157 ConstructStringTag(name
, (const char*)unicode2UTF8(data
));
159 CECTag::CECTag(ec_tagname_t name
, const wxChar
* data
)
161 ConstructStringTag(name
, (const char*)unicode2UTF8(data
));
167 CECTag::CECTag(const CECTag
& tag
)
174 * Creates a new CECTag instance, which contains an int value.
176 * This takes care of endianness problems with numbers.
178 * @param name TAG name.
179 * @param data number.
183 CECTag::CECTag(ec_tagname_t name
, bool data
) : m_tagName(name
)
187 CECTag::CECTag(ec_tagname_t name
, uint8 data
) : m_tagName(name
)
191 CECTag::CECTag(ec_tagname_t name
, uint16 data
) : m_tagName(name
)
195 CECTag::CECTag(ec_tagname_t name
, uint32 data
) : m_tagName(name
)
199 CECTag::CECTag(ec_tagname_t name
, uint64 data
) : m_tagName(name
)
204 void CECTag::InitInt(uint64 data
)
207 m_dataType
= EC_TAGTYPE_UINT8
;
209 } else if (data
<= 0xFFFF) {
210 m_dataType
= EC_TAGTYPE_UINT16
;
212 } else if (data
<= 0xFFFFFFFF) {
213 m_dataType
= EC_TAGTYPE_UINT32
;
216 m_dataType
= EC_TAGTYPE_UINT64
;
222 switch (m_dataType
) {
223 case EC_TAGTYPE_UINT8
:
224 PokeUInt8( m_tagData
, (uint8
) data
);
226 case EC_TAGTYPE_UINT16
:
227 PokeUInt16( m_tagData
, wxUINT16_SWAP_ALWAYS((uint16
) data
));
229 case EC_TAGTYPE_UINT32
:
230 PokeUInt32( m_tagData
, wxUINT32_SWAP_ALWAYS((uint32
) data
));
232 case EC_TAGTYPE_UINT64
:
233 PokeUInt64( m_tagData
, wxUINT64_SWAP_ALWAYS(data
) );
239 * Creates a new CECTag instance, which contains a double precision floating point number
241 * @param name TAG name
242 * @param data double number
244 * @note The actual data is converted to string representation, because we have not found
245 * yet an effective and safe way to transmit floating point numbers.
247 * @see GetDoubleData()
249 CECTag::CECTag(ec_tagname_t name
, double data
) : m_tagName(name
)
251 std::ostringstream double_str
;
253 std::string double_string
= double_str
.str();
254 const char * double_chr
= double_string
.c_str();
255 m_dataLen
= (ec_taglen_t
)strlen(double_chr
) + 1;
257 memcpy(m_tagData
, double_chr
, m_dataLen
);
258 m_dataType
= EC_TAGTYPE_DOUBLE
;
262 * Destructor - frees allocated data and deletes child TAGs.
264 CECTag::~CECTag(void)
270 * Copy assignment operator.
272 * std::vector uses this, but the compiler-supplied version wouldn't properly
273 * handle m_dynamic and m_tagData. This wouldn't be necessary if m_tagData
274 * was a smart pointer (Hi, Kry!).
276 CECTag
& CECTag::operator=(const CECTag
& tag
)
279 m_tagName
= tag
.m_tagName
;
280 m_dataLen
= tag
.m_dataLen
;
281 m_dataType
= tag
.m_dataType
;
283 if (m_dataLen
!= 0) {
285 memcpy(m_tagData
, tag
.m_tagData
, m_dataLen
);
290 if (!tag
.m_tagList
.empty()) {
291 m_tagList
.reserve(tag
.m_tagList
.size());
292 for (TagList::size_type i
=0; i
<tag
.m_tagList
.size(); i
++) {
293 m_tagList
.push_back(tag
.m_tagList
[i
]);
305 bool CECTag::operator==(const CECTag
& tag
) const
307 return m_dataType
== tag
.m_dataType
308 && m_tagName
== tag
.m_tagName
309 && m_dataLen
== tag
.m_dataLen
311 || !memcmp(m_tagData
, tag
.m_tagData
, m_dataLen
))
312 && m_tagList
== tag
.m_tagList
;
316 * Add a child tag to this one.
318 * Be very careful that this creates a copy of \e tag. Thus, the following code won't work as expected:
321 * CECPacket *p = new CECPacket(whatever);
322 * CECTag *t1 = new CECTag(whatever);
323 * CECTag *t2 = new CECTag(whatever);
325 * t1.AddTag(*t2); // t2 won't be part of p !!!
329 * To get the desired results, the above should be replaced with something like:
333 * CECPacket *p = new CECPacket(whatever);
334 * CECTag *t1 = new CECTag(whatever);
335 * CECTag *t2 = new CECTag(whatever);
337 * delete t2; // we can safely delete t2 here, because t1 holds a copy
339 * delete t1; // now p holds a copy of both t1 and t2
343 * Then why copying? The answer is to enable simplifying the code like this:
347 * CECPacket *p = new CECPacket(whatever);
348 * CECTag t1(whatever);
349 * t1.AddTag(CECTag(whatever)); // t2 is now created on-the-fly
350 * p.AddTag(t1); // now p holds a copy of both t1 and t2
354 * @param tag a CECTag class instance to add.
355 * @return \b true if tag was really added,
356 * \b false when it was omitted through valuemap.
358 bool CECTag::AddTag(const CECTag
& tag
, CValueMap
* valuemap
)
361 return valuemap
->AddTag(tag
, this);
363 // cannot have more than 64k tags
364 wxASSERT(m_tagList
.size() < 0xffff);
366 m_tagList
.push_back(tag
);
370 void CECTag::AddTag(ec_tagname_t name
, uint64_t data
, CValueMap
* valuemap
)
373 valuemap
->CreateTag(name
, data
, this);
375 AddTag(CECTag(name
, data
));
379 void CECTag::AddTag(ec_tagname_t name
, const wxString
& data
, CValueMap
* valuemap
)
382 valuemap
->CreateTag(name
, data
, this);
384 AddTag(CECTag(name
, data
));
388 bool CECTag::ReadFromSocket(CECSocket
& socket
)
390 ec_tagname_t tmp_tagName
;
391 if (!socket
.ReadNumber(&tmp_tagName
, sizeof(ec_tagname_t
))) {
394 m_tagName
= tmp_tagName
>> 1;
395 bool hasChildren
= (tmp_tagName
& 0x01) != 0;
397 if (!socket
.ReadNumber(&m_dataType
, sizeof(ec_tagtype_t
))) {
401 if (!socket
.ReadNumber(&m_dataLen
, sizeof(ec_taglen_t
))) {
405 if (hasChildren
&& !ReadChildren(socket
)) {
409 unsigned int tmp_len
= m_dataLen
;
411 m_dataLen
= tmp_len
- GetTagLen();
414 if (!socket
.ReadBuffer(m_tagData
, m_dataLen
)) {
425 bool CECTag::WriteTag(CECSocket
& socket
) const
427 ec_tagname_t tmp_tagName
= (m_tagName
<< 1) | (m_tagList
.empty() ? 0 : 1);
428 ec_tagtype_t type
= m_dataType
;
429 ec_taglen_t tagLen
= GetTagLen();
430 wxASSERT(type
!= EC_TAGTYPE_UNKNOWN
);
432 if (!socket
.WriteNumber(&tmp_tagName
, sizeof(ec_tagname_t
))) return false;
433 if (!socket
.WriteNumber(&type
, sizeof(ec_tagtype_t
))) return false;
434 if (!socket
.WriteNumber(&tagLen
, sizeof(ec_taglen_t
))) return false;
436 if (!m_tagList
.empty()) {
437 if (!WriteChildren(socket
)) return false;
441 if (m_tagData
!= NULL
) { // This is here only to make sure everything, it should not be NULL at this point
442 if (!socket
.WriteBuffer(m_tagData
, m_dataLen
)) return false;
449 bool CECTag::ReadChildren(CECSocket
& socket
)
452 if (!socket
.ReadNumber(&tmp_tagCount
, sizeof(uint16
))) {
456 if (tmp_tagCount
> 0) {
457 m_tagList
.reserve(tmp_tagCount
);
458 for (int i
=0; i
<tmp_tagCount
; i
++) {
459 m_tagList
.push_back(CECTag());
460 CECTag
& tag
= m_tagList
[i
];
461 if (!tag
.ReadFromSocket(socket
)) {
469 bool CECTag::WriteChildren(CECSocket
& socket
) const
471 wxASSERT(m_tagList
.size() < 0xFFFF);
472 uint16 tmp
= (uint16
)m_tagList
.size();
473 if (!socket
.WriteNumber(&tmp
, sizeof(tmp
))) return false;
474 if (!m_tagList
.empty()) {
475 for (TagList::size_type i
=0; i
<m_tagList
.size(); i
++) {
476 if (!m_tagList
[i
].WriteTag(socket
)) return false;
483 * Finds the (first) child tag with given name.
485 * @param name TAG name to look for.
486 * @return the tag found, or NULL.
488 const CECTag
* CECTag::GetTagByName(ec_tagname_t name
) const
490 for (TagList::size_type i
=0; i
<m_tagList
.size(); i
++)
491 if (m_tagList
[i
].m_tagName
== name
) return &m_tagList
[i
];
496 * Finds the (first) child tag with given name.
498 * @param name TAG name to look for.
499 * @return the tag found, or NULL.
501 CECTag
* CECTag::GetTagByName(ec_tagname_t name
)
503 for (TagList::size_type i
=0; i
<m_tagList
.size(); i
++)
504 if (m_tagList
[i
].m_tagName
== name
) return &m_tagList
[i
];
509 * Finds the (first) child tag with given name.
511 * @param name TAG name to look for.
512 * @return the tag found, or a special null-valued tag otherwise.
516 const CECTag
* CECTag::GetTagByNameSafe(ec_tagname_t name
) const
518 const CECTag
* result
= GetTagByName(name
);
520 result
= &s_theNullTag
;
525 * Query TAG length that is suitable for the TAGLEN field (i.e.\
526 * without it's own header size).
528 * @return Tag length, containing its childs' length.
530 uint32
CECTag::GetTagLen(void) const
532 uint32 length
= m_dataLen
;
533 for (TagList::size_type i
=0; i
<m_tagList
.size(); i
++) {
534 length
+= m_tagList
[i
].GetTagLen();
535 length
+= sizeof(ec_tagname_t
) + sizeof(ec_tagtype_t
) + sizeof(ec_taglen_t
) + ((m_tagList
[i
].GetTagCount() > 0) ? 2 : 0);
541 uint64_t CECTag::GetInt() const
543 if (m_tagData
== NULL
) {
544 // Empty tag - This is NOT an error.
545 EC_ASSERT(m_dataType
== EC_TAGTYPE_UNKNOWN
);
549 switch (m_dataType
) {
550 case EC_TAGTYPE_UINT8
:
551 return PeekUInt8(m_tagData
);
552 case EC_TAGTYPE_UINT16
:
553 return ENDIAN_NTOHS( RawPeekUInt16( m_tagData
) );
554 case EC_TAGTYPE_UINT32
:
555 return ENDIAN_NTOHL( RawPeekUInt32( m_tagData
) );
556 case EC_TAGTYPE_UINT64
:
557 return ENDIAN_NTOHLL( RawPeekUInt64( m_tagData
) );
558 case EC_TAGTYPE_UNKNOWN
:
559 // Empty tag - This is NOT an error.
568 std::string
CECTag::GetStringDataSTL() const
570 if (m_dataType
!= EC_TAGTYPE_STRING
) {
571 EC_ASSERT(m_dataType
== EC_TAGTYPE_UNKNOWN
);
572 return std::string();
573 } else if (m_tagData
== NULL
) {
575 return std::string();
578 return std::string(m_tagData
);
582 #ifdef USE_WX_EXTENSIONS
583 wxString
CECTag::GetStringData() const
585 return UTF82unicode(GetStringDataSTL().c_str());
590 CMD4Hash
CECTag::GetMD4Data() const
592 if (m_dataType
!= EC_TAGTYPE_HASH16
) {
593 EC_ASSERT(m_dataType
== EC_TAGTYPE_UNKNOWN
);
597 EC_ASSERT(m_tagData
!= NULL
);
599 // Doesn't matter if m_tagData is NULL in CMD4Hash(),
600 // that'll just result in an empty hash.
601 return CMD4Hash((const unsigned char *)m_tagData
);
606 * Returns an EC_IPv4_t class.
608 * This function takes care of the enadianness of the port number.
610 * @return EC_IPv4_t class.
612 * @see CECTag(ec_tagname_t, const EC_IPv4_t&)
614 EC_IPv4_t
CECTag::GetIPv4Data() const
618 if (m_dataType
!= EC_TAGTYPE_IPV4
) {
619 EC_ASSERT(m_dataType
== EC_TAGTYPE_UNKNOWN
);
620 } else if (m_tagData
== NULL
) {
623 RawPokeUInt32( p
.m_ip
, RawPeekUInt32( ((EC_IPv4_t
*)m_tagData
)->m_ip
) );
624 p
.m_port
= ENDIAN_NTOHS(((EC_IPv4_t
*)m_tagData
)->m_port
);
631 * Returns a double value.
633 * @note The returned value is what we get by converting the string form
634 * of the number to a double.
636 * @return The double value of the tag.
638 * @see CECTag(ec_tagname_t, double)
640 double CECTag::GetDoubleData(void) const
642 if (m_dataType
!= EC_TAGTYPE_DOUBLE
) {
643 EC_ASSERT(m_dataType
== EC_TAGTYPE_UNKNOWN
);
645 } else if (m_tagData
== NULL
) {
650 std::istringstream
double_str(m_tagData
);
658 void CECTag::ConstructStringTag(ec_tagname_t name
, const std::string
& data
)
661 m_dataLen
= (ec_taglen_t
)strlen(data
.c_str()) + 1;
663 memcpy(m_tagData
, data
.c_str(), m_dataLen
);
664 m_dataType
= EC_TAGTYPE_STRING
;
667 void CECTag::SetStringData(const wxString
& s
)
671 ConstructStringTag(m_tagName
, (const char*)unicode2UTF8(s
));
676 void CECTag::DebugPrint(int level
, bool print_empty
) const
678 if (m_dataLen
|| print_empty
) {
680 for (int i
= level
; i
--;) space
+= wxT(" ");
681 wxString s1
= CFormat(wxT("%s%s %d = ")) % space
% GetDebugNameECTagNames(m_tagName
) % m_dataLen
;
684 case EC_TAG_DETAIL_LEVEL
:
685 s2
= GetDebugNameEC_DETAIL_LEVEL(GetInt()); break;
686 case EC_TAG_SEARCH_TYPE
:
687 s2
= GetDebugNameEC_SEARCH_TYPE(GetInt()); break;
688 case EC_TAG_STAT_VALUE_TYPE
:
689 s2
= GetDebugNameEC_STATTREE_NODE_VALUE_TYPE(GetInt()); break;
691 switch (m_dataType
) {
692 case EC_TAGTYPE_UINT8
:
693 case EC_TAGTYPE_UINT16
:
694 case EC_TAGTYPE_UINT32
:
695 case EC_TAGTYPE_UINT64
:
696 s2
= CFormat(wxT("%d")) % GetInt(); break;
697 case EC_TAGTYPE_STRING
:
698 s2
= GetStringData(); break;
699 case EC_TAGTYPE_DOUBLE
:
700 s2
= CFormat(wxT("%.1f")) % GetDoubleData(); break;
701 case EC_TAGTYPE_HASH16
:
702 s2
= GetMD4Data().Encode(); break;
704 s2
= GetDebugNameECTagTypes(m_dataType
);
707 DoECLogLine(s1
+ s2
);
709 for (TagList::const_iterator it
= m_tagList
.begin(); it
!= m_tagList
.end(); ++it
) {
710 it
->DebugPrint(level
+ 1, true);
716 * \fn CMD4Hash CECTag::GetMD4Data(void) const
718 * \brief Returns a CMD4Hash class.
720 * This function takes care of converting from MSB to LSB as necessary.
722 * \return CMD4Hash class.
724 * \sa CECTag(ec_tagname_t, const CMD4Hash&)
728 * \fn CECTag *CECTag::GetTagByIndex(size_t index) const
730 * \brief Finds the indexth child tag.
732 * \param index 0-based index, 0 <= \e index < GetTagCount()
734 * \return The child tag, or NULL if index out of range.
738 * \fn CECTag *CECTag::GetTagByIndexSafe(size_t index) const
740 * \brief Finds the indexth child tag.
742 * \param index 0-based index, 0 <= \e index < GetTagCount()
744 * \return The child tag, or a special null-valued tag if index out of range.
748 * \fn size_t CECTag::GetTagCount(void) const
750 * \brief Returns the number of child tags.
752 * \return The number of child tags.
756 * \fn const void *CECTag::GetTagData(void) const
758 * \brief Returns a pointer to the TAG DATA.
760 * \return A pointer to the TAG DATA. (As specified with the data field of the constructor.)
764 * \fn uint16 CECTag::GetTagDataLen(void) const
766 * \brief Returns the length of the data buffer.
768 * \return The length of the data buffer.
772 * \fn ec_tagname_t CECTag::GetTagName(void) const
774 * \brief Returns TAGNAME.
776 * \return The name of the tag.
780 * \fn wxString CECTag::GetStringData(void) const
782 * \brief Returns the string data of the tag.
784 * Returns a wxString created from TAGDATA. It is automatically
785 * converted from UTF-8 to the internal application encoding.
786 * Should be used with care (only on tags created with the
787 * CECTag(ec_tagname_t, const wxString&) constructor),
788 * becuse it does not perform any check to see if the tag really contains a
791 * \return The string data of the tag.
793 * \sa CECTag(ec_tagname_t, const wxString&)
797 * \fn uint8 CECTag::GetInt(void) const
799 * \brief Returns the uint8 data of the tag.
801 * This function takes care of the endianness problems with numbers.
803 * \return The uint8 data of the tag.
805 * \sa CECTag(ec_tagname_t, uint8)
809 * \fn uint16 CECTag::GetInt(void) const
811 * \brief Returns the uint16 data of the tag.
813 * This function takes care of the endianness problems with numbers.
815 * \return The uint16 data of the tag.
817 * \sa CECTag(ec_tagname_t, uint16)
821 * \fn uint32 CECTag::GetInt(void) const
823 * \brief Returns the uint32 data of the tag.
825 * This function takes care of the endianness problems with numbers.
827 * \return The uint32 data of the tag.
829 * \sa CECTag(ec_tagname_t, uint32)
833 * \fn uint64 CECTag::GetInt(void) const
835 * \brief Returns the uint64 data of the tag.
837 * This function takes care of the endianness problems with numbers.
839 * \return The uint64 data of the tag.
841 * \sa CECTag(ec_tagname_t, uint64)
843 // File_checked_for_headers