Repository: XKNX/xknx
Pull request #1131 (2023-01-04 14:06:52)

KNX-IP header total_length is 2 bytes long
<!--
  You are awesome! Thanks for contributing to our project!
  Please, DO NOT DELETE ANY TEXT from this template!.
-->
## Description
<!--
Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context.
List any dependencies that are required for this change.
-->
There are no reserved bytes.

Fixes # (issue)

## Type of change
<!--
Please tick the applicable options.
NOTE: Ticking multiple options most likely indicates
that your change is to big and it is suggested to split it into several smaller PRs.
-->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

## Checklist:

- [ ] The documentation has been adjusted accordingly
- [x] Tests have been added that prove the fix is effective or that the feature works
- [x] The changes are documented in the changelog (docs/changelog.md)

codecov[bot]: # [Codecov](https://codecov.io/gh/XKNX/xknx/pull/1131?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) Report
> Merging [#1131](https://codecov.io/gh/XKNX/xknx/pull/1131?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) (a8d7ad6) into [main](https://codecov.io/gh/XKNX/xknx/commit/cd31de80136f7b1f21bf2e7064838eabd7e7f18b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) (cd31de8) will **decrease** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/XKNX/xknx/pull/1131/graphs/tree.svg?width=650&height=150&src=pr&token=irWbIygS84&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX)](https://codecov.io/gh/XKNX/xknx/pull/1131?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX)

```diff
@@            Coverage Diff             @@
##             main    #1131      +/-   ##
==========================================
- Coverage   96.35%   96.35%   -0.01%     
==========================================
  Files         138      138              
  Lines        8661     8659       -2     
==========================================
- Hits         8345     8343       -2     
  Misses        316      316              
```


| [Impacted Files](https://codecov.io/gh/XKNX/xknx/pull/1131?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) | Coverage Δ | |
|---|---|---|
| [xknx/knxip/header.py](https://codecov.io/gh/XKNX/xknx/pull/1131/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX#diff-eGtueC9rbnhpcC9oZWFkZXIucHk=) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/docs/changelog.md b/docs/changelog.md
index c86b736c..d8491b85 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -11,6 +11,7 @@ nav_order: 2
 ### Internals
 
 - Parse T_Data_Broadcast TPCI. Forward these telegrams to the Management class.
+- KNXIPHeader total_length is 2 bytes long. There are no reserved bytes.
 
 # 2.2.0 Expose cooldown 2022-12-27
 
diff --git a/xknx/knxip/header.py b/xknx/knxip/header.py
index 211cbe0c..ae46de0a 100644
--- a/xknx/knxip/header.py
+++ b/xknx/knxip/header.py
@@ -18,7 +18,6 @@ class KNXIPHeader:
     def __init__(self) -> None:
         """Initialize KNXIPHeader class."""
         self.service_type_ident = KNXIPServiceType.ROUTING_INDICATION
-        self.b4_reserve = 0
         self.total_length = 0  # to be set later
 
     def from_knx(self, data: bytes) -> int:
@@ -28,17 +27,14 @@ class KNXIPHeader:
         if data[0] != KNXIPHeader.HEADERLENGTH:
             raise CouldNotParseKNXIP("wrong connection header length")
         # set immediately, as we need it for tcp stream parsing before raising exception
-        self.total_length = data[5]
+        self.total_length = data[4] * 256 + data[5]
         if data[1] != KNXIPHeader.PROTOCOLVERSION:
             raise CouldNotParseKNXIP("wrong protocol version")
 
         try:
             self.service_type_ident = KNXIPServiceType(data[2] * 256 + data[3])
         except ValueError:
-            raise CouldNotParseKNXIP(
-                f"KNXIPServiceType unknown: {hex(data[2] * 256 + data[3])}"
-            )
-        self.b4_reserve = data[4]
+            raise CouldNotParseKNXIP(f"KNXIPServiceType unknown: 0x{data[2:4].hex()}")
         return KNXIPHeader.HEADERLENGTH
 
     def set_length(self, body: KNXIPBody) -> None:
@@ -62,7 +58,6 @@ class KNXIPHeader:
             f'HeaderLength="{KNXIPHeader.HEADERLENGTH}" '
             f'ProtocolVersion="{KNXIPHeader.PROTOCOLVERSION}" '
             f'KNXIPServiceType="{self.service_type_ident.name}" '
-            f'Reserve="{self.b4_reserve}" '
             f'TotalLength="{self.total_length}" />'
         )
 


Test patch:
diff --git a/test/knxip_tests/header_test.py b/test/knxip_tests/header_test.py
index 1ae23949..67a67322 100644
--- a/test/knxip_tests/header_test.py
+++ b/test/knxip_tests/header_test.py
@@ -16,7 +16,6 @@ class TestKNXIPHeader:
         assert header.HEADERLENGTH == 6
         assert header.PROTOCOLVERSION == 16
         assert header.service_type_ident == KNXIPServiceType.TUNNELLING_ACK
-        assert header.b4_reserve == 0
         assert header.total_length == 10
         assert header.to_knx() == raw
 
diff --git a/test/str_test.py b/test/str_test.py
index 19b1c1d4..1baccfcf 100644
--- a/test/str_test.py
+++ b/test/str_test.py
@@ -513,7 +513,7 @@ class TestStringRepresentations:
         header.total_length = 42
         assert (
             str(header)
-            == '<KNXIPHeader HeaderLength="6" ProtocolVersion="16" KNXIPServiceType="ROUTING_INDICATION" Reserve="0" TotalLength="42" '
+            == '<KNXIPHeader HeaderLength="6" ProtocolVersion="16" KNXIPServiceType="ROUTING_INDICATION" TotalLength="42" '
             "/>"
         )
 
@@ -660,7 +660,7 @@ class TestStringRepresentations:
         assert (
             str(knxipframe)
             == '<KNXIPFrame <KNXIPHeader HeaderLength="6" ProtocolVersion="16" KNXIPServiceType="SEARCH_REQUEST" '
-            'Reserve="0" TotalLength="14" /> body="<SearchRequest discovery_endpoint="0.0.0.0:0/udp" />" />'
+            'TotalLength="14" /> body="<SearchRequest discovery_endpoint="0.0.0.0:0/udp" />" />'
         )
 
     #