Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement relay echo-request option handler
- Loading branch information
Showing
4 changed files
with
230 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
Implementation of Echo Request option handling as specified in :rfc:`4994`. | ||
""" | ||
from typing import List | ||
|
||
from dhcpkit.ipv6.extensions.relay_echo_request import EchoRequestOption | ||
from dhcpkit.ipv6.messages import RelayForwardMessage, RelayReplyMessage | ||
from dhcpkit.ipv6.server.handlers import Handler, RelayHandler | ||
from dhcpkit.ipv6.server.transaction_bundle import TransactionBundle | ||
|
||
|
||
def create_cleanup_handlers() -> List[Handler]: | ||
""" | ||
Create handlers to clean up stuff in the transaction bundle | ||
:return: Handlers to add to the handler chain | ||
""" | ||
return [RelayEchoRequestOptionHandler()] | ||
|
||
|
||
class RelayEchoRequestOptionHandler(RelayHandler): | ||
""" | ||
When a server creates a Relay-Reply, it SHOULD perform ERO processing | ||
after processing the ORO and other options processing. For each | ||
option in the ERO: | ||
a. If the option is already in the Relay-Reply, the server MUST | ||
ignore that option and continue to process any remaining options | ||
in the ERO. | ||
b. If the option was not in the received Relay-Forward, the server | ||
MUST ignore that option and continue to process any remaining | ||
options in the ERO. | ||
c. Otherwise, the server MUST copy the option, verbatim, from the | ||
received Relay-Forward to the Relay-Reply, even if the server | ||
does not otherwise recognize that option. | ||
""" | ||
|
||
def handle_relay(self, bundle: TransactionBundle, | ||
relay_message_in: RelayForwardMessage, relay_message_out: RelayReplyMessage): | ||
""" | ||
Handle the options for each relay message pair. | ||
:param bundle: The transaction bundle | ||
:param relay_message_in: The incoming relay message | ||
:param relay_message_out: Thr outgoing relay message | ||
""" | ||
# See if the relay message contains an ERO | ||
ero = relay_message_in.get_option_of_type(EchoRequestOption) | ||
if not ero: | ||
# Nothing to do | ||
return | ||
|
||
for option_type in ero.requested_options: | ||
# Don't do anything if the outgoing relay message already has this one | ||
if any(option.option_type == option_type for option in relay_message_out.options): | ||
continue | ||
|
||
# Get the incoming options of the requested type | ||
incoming_options = [option for option in relay_message_in.options if option.option_type == option_type] | ||
|
||
for option in incoming_options: | ||
# Make sure this option can go into this type of response | ||
if not relay_message_out.may_contain(option): | ||
return | ||
|
||
# And append them to the outgoing message if possible | ||
relay_message_out.options.append(option) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
dhcpkit/tests/ipv6/server/handlers/test_echo_request_option_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
""" | ||
Tests for a relay message handler | ||
""" | ||
import unittest | ||
from ipaddress import IPv6Address | ||
|
||
from dhcpkit.ipv6.duids import LinkLayerDUID, LinkLayerTimeDUID | ||
from dhcpkit.ipv6.extensions.relay_echo_request import EchoRequestOption | ||
from dhcpkit.ipv6.extensions.remote_id import OPTION_REMOTE_ID, RemoteIdOption | ||
from dhcpkit.ipv6.extensions.subscriber_id import OPTION_SUBSCRIBER_ID | ||
from dhcpkit.ipv6.messages import RelayForwardMessage, RelayReplyMessage, SolicitMessage | ||
from dhcpkit.ipv6.options import ClientIdOption, ElapsedTimeOption, IANAOption, InterfaceIdOption, OPTION_INTERFACE_ID, \ | ||
RelayMessageOption, UnknownOption | ||
from dhcpkit.ipv6.server.message_handler import MessageHandler | ||
from dhcpkit.ipv6.server.statistics import StatisticsSet | ||
from dhcpkit.ipv6.server.transaction_bundle import TransactionBundle | ||
|
||
|
||
class RelayHandlerTestCase(unittest.TestCase): | ||
def setUp(self): | ||
# This is the DUID that is used in the message fixtures | ||
self.duid = LinkLayerTimeDUID(hardware_type=1, time=488458703, link_layer_address=bytes.fromhex('00137265ca42')) | ||
|
||
# Create a message handler | ||
self.message_handler = MessageHandler(server_id=self.duid) | ||
|
||
def test_empty_echo_request(self): | ||
relayed_solicit_message = RelayForwardMessage( | ||
hop_count=1, | ||
link_address=IPv6Address('2001:db8:ffff:1::1'), | ||
peer_address=IPv6Address('fe80::3631:c4ff:fe3c:b2f1'), | ||
options=[ | ||
RelayMessageOption(relayed_message=SolicitMessage( | ||
transaction_id=bytes.fromhex('f350d6'), | ||
options=[ | ||
ElapsedTimeOption(elapsed_time=0), | ||
ClientIdOption(duid=LinkLayerDUID(hardware_type=1, | ||
link_layer_address=bytes.fromhex('3431c43cb2f1'))), | ||
IANAOption(iaid=bytes.fromhex('c43cb2f1')), | ||
], | ||
)), | ||
EchoRequestOption(requested_options=[]), | ||
UnknownOption(option_type=65535), | ||
InterfaceIdOption(interface_id=b'Fa2/3'), | ||
RemoteIdOption(enterprise_number=9, | ||
remote_id=bytes.fromhex('020023000001000a0003000100211c7d486e')), | ||
] | ||
) | ||
|
||
bundle = TransactionBundle(incoming_message=relayed_solicit_message, received_over_multicast=True) | ||
self.message_handler.handle(bundle, StatisticsSet()) | ||
|
||
self.assertIsInstance(bundle.outgoing_message, RelayReplyMessage) | ||
self.assertEqual(len(bundle.outgoing_message.options), 2) | ||
self.assertIsInstance(bundle.outgoing_message.options[0], InterfaceIdOption) | ||
self.assertIsInstance(bundle.outgoing_message.options[1], RelayMessageOption) | ||
|
||
def test_unnecessary_echo_request(self): | ||
relayed_solicit_message = RelayForwardMessage( | ||
hop_count=1, | ||
link_address=IPv6Address('2001:db8:ffff:1::1'), | ||
peer_address=IPv6Address('fe80::3631:c4ff:fe3c:b2f1'), | ||
options=[ | ||
RelayMessageOption(relayed_message=SolicitMessage( | ||
transaction_id=bytes.fromhex('f350d6'), | ||
options=[ | ||
ElapsedTimeOption(elapsed_time=0), | ||
ClientIdOption(duid=LinkLayerDUID(hardware_type=1, | ||
link_layer_address=bytes.fromhex('3431c43cb2f1'))), | ||
IANAOption(iaid=bytes.fromhex('c43cb2f1')), | ||
], | ||
)), | ||
EchoRequestOption(requested_options=[OPTION_INTERFACE_ID]), | ||
UnknownOption(option_type=65535), | ||
InterfaceIdOption(interface_id=b'Fa2/3'), | ||
RemoteIdOption(enterprise_number=9, | ||
remote_id=bytes.fromhex('020023000001000a0003000100211c7d486e')), | ||
] | ||
) | ||
|
||
bundle = TransactionBundle(incoming_message=relayed_solicit_message, received_over_multicast=True) | ||
self.message_handler.handle(bundle, StatisticsSet()) | ||
|
||
self.assertIsInstance(bundle.outgoing_message, RelayReplyMessage) | ||
self.assertEqual(len(bundle.outgoing_message.options), 2) | ||
self.assertIsInstance(bundle.outgoing_message.options[0], InterfaceIdOption) | ||
self.assertIsInstance(bundle.outgoing_message.options[1], RelayMessageOption) | ||
|
||
def test_absent_option_echo_request(self): | ||
relayed_solicit_message = RelayForwardMessage( | ||
hop_count=1, | ||
link_address=IPv6Address('2001:db8:ffff:1::1'), | ||
peer_address=IPv6Address('fe80::3631:c4ff:fe3c:b2f1'), | ||
options=[ | ||
RelayMessageOption(relayed_message=SolicitMessage( | ||
transaction_id=bytes.fromhex('f350d6'), | ||
options=[ | ||
ElapsedTimeOption(elapsed_time=0), | ||
ClientIdOption(duid=LinkLayerDUID(hardware_type=1, | ||
link_layer_address=bytes.fromhex('3431c43cb2f1'))), | ||
IANAOption(iaid=bytes.fromhex('c43cb2f1')), | ||
], | ||
)), | ||
EchoRequestOption(requested_options=[OPTION_SUBSCRIBER_ID]), | ||
UnknownOption(option_type=65535), | ||
InterfaceIdOption(interface_id=b'Fa2/3'), | ||
RemoteIdOption(enterprise_number=9, | ||
remote_id=bytes.fromhex('020023000001000a0003000100211c7d486e')), | ||
] | ||
) | ||
|
||
bundle = TransactionBundle(incoming_message=relayed_solicit_message, received_over_multicast=True) | ||
self.message_handler.handle(bundle, StatisticsSet()) | ||
|
||
self.assertIsInstance(bundle.outgoing_message, RelayReplyMessage) | ||
self.assertEqual(len(bundle.outgoing_message.options), 2) | ||
self.assertIsInstance(bundle.outgoing_message.options[0], InterfaceIdOption) | ||
self.assertIsInstance(bundle.outgoing_message.options[1], RelayMessageOption) | ||
|
||
def test_remote_id_echo_request(self): | ||
relayed_solicit_message = RelayForwardMessage( | ||
hop_count=1, | ||
link_address=IPv6Address('2001:db8:ffff:1::1'), | ||
peer_address=IPv6Address('fe80::3631:c4ff:fe3c:b2f1'), | ||
options=[ | ||
RelayMessageOption(relayed_message=SolicitMessage( | ||
transaction_id=bytes.fromhex('f350d6'), | ||
options=[ | ||
ElapsedTimeOption(elapsed_time=0), | ||
ClientIdOption(duid=LinkLayerDUID(hardware_type=1, | ||
link_layer_address=bytes.fromhex('3431c43cb2f1'))), | ||
IANAOption(iaid=bytes.fromhex('c43cb2f1')), | ||
], | ||
)), | ||
EchoRequestOption(requested_options=[OPTION_REMOTE_ID]), | ||
UnknownOption(option_type=65535), | ||
InterfaceIdOption(interface_id=b'Fa2/3'), | ||
RemoteIdOption(enterprise_number=9, | ||
remote_id=bytes.fromhex('020023000001000a0003000100211c7d486e')), | ||
] | ||
) | ||
|
||
bundle = TransactionBundle(incoming_message=relayed_solicit_message, received_over_multicast=True) | ||
self.message_handler.handle(bundle, StatisticsSet()) | ||
|
||
self.assertIsInstance(bundle.outgoing_message, RelayReplyMessage) | ||
self.assertEqual(len(bundle.outgoing_message.options), 3) | ||
self.assertIsInstance(bundle.outgoing_message.options[0], InterfaceIdOption) | ||
self.assertIsInstance(bundle.outgoing_message.options[1], RelayMessageOption) | ||
self.assertIsInstance(bundle.outgoing_message.options[2], RemoteIdOption) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters