1#!/usr/bin/env python3 2# 3# Copyright (c) 2016, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29import os 30from enum import Enum 31 32import coap 33import dtls 34import ipv6 35import lowpan 36import mesh_cop 37import message 38import mle 39import net_crypto 40import network_data 41import network_diag 42import network_layer 43import simulator 44import sniffer 45from tlvs_parsing import SubTlvsFactory 46 47MESH_LOCAL_PREFIX = 'fd00:db8::/64' 48MESH_LOCAL_PREFIX_REGEX_PATTERN = '^fd00:0?db8:0{0,4}:0{0,4}' 49ROUTING_LOCATOR = '64/:0:ff:fe00:/16' 50ROUTING_LOCATOR_REGEX_PATTERN = r'.*:(0)?:0{0,2}ff:fe00:\w{1,4}$' 51LINK_LOCAL = 'fe80:/112' 52LINK_LOCAL_REGEX_PATTERN = '^fe80:.*' 53ALOC_FLAG_REGEX_PATTERN = '.*:fc..$' 54LINK_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = 'ff32:40:fd00:db8:0:0:0:1' 55REALM_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = 'ff33:40:fd00:db8:0:0:0:1' 56REALM_LOCAL_ALL_NODES_ADDRESS = 'ff03::1' 57REALM_LOCAL_ALL_ROUTERS_ADDRESS = 'ff03::2' 58LINK_LOCAL_ALL_NODES_ADDRESS = 'ff02::1' 59LINK_LOCAL_ALL_ROUTERS_ADDRESS = 'ff02::2' 60 61DOMAIN_PREFIX = 'fd00:7d03:7d03:7d03::/64' 62DOMAIN_PREFIX_REGEX_PATTERN = '^fd00:7d03:7d03:7d03:' 63DOMAIN_PREFIX_ALTER = 'fd00:7d04:7d04:7d04::/64' 64 65PORT_OFFSET = int(os.getenv('PORT_OFFSET', '0')) 66BACKBONE_PREFIX = f'{0x9100 + PORT_OFFSET:04x}::/64' 67BACKBONE_PREFIX_REGEX_PATTERN = f'^{0x9100 + PORT_OFFSET:04x}:' 68BACKBONE_DOCKER_NETWORK_NAME = f'backbone{PORT_OFFSET}' 69BACKBONE_IFNAME = 'eth0' 70THREAD_IFNAME = 'wpan0' 71 72OTBR_DOCKER_IMAGE = os.getenv('OTBR_DOCKER_IMAGE', 'otbr-ot12-backbone-ci') 73OTBR_DOCKER_NAME_PREFIX = f'otbr_{PORT_OFFSET}_' 74 75ALL_NETWORK_BBRS_ADDRESS = 'ff32:40:fd00:db8:0:0:0:3' 76 77ALL_DOMAIN_BBRS_ADDRESS = 'ff32:40:fd00:7d03:7d03:7d03:0:3' 78ALL_DOMAIN_BBRS_ADDRESS_ALTER = 'ff32:40:fd00:7d04:7d04:7d04:0:3' 79 80ONLINK_GUA_PREFIX = '2021::/64' 81 82# Any address starts with 'fd' are considered on-link address. 83ONLINK_PREFIX_REGEX_PATTERN = '^fd' 84 85DEFAULT_NETWORK_KEY = bytearray([ 86 0x00, 87 0x11, 88 0x22, 89 0x33, 90 0x44, 91 0x55, 92 0x66, 93 0x77, 94 0x88, 95 0x99, 96 0xaa, 97 0xbb, 98 0xcc, 99 0xdd, 100 0xee, 101 0xff, 102]) 103 104 105class ADDRESS_TYPE(Enum): 106 LINK_LOCAL = 'LINK_LOCAL' 107 GLOBAL = 'GLOBAL' 108 RLOC = 'RLOC' 109 ALOC = 'ALOC' 110 ML_EID = 'ML_EID' 111 DUA = 'DUA' 112 BACKBONE_GUA = 'BACKBONE_GUA' 113 OMR = 'OMR' 114 ONLINK_ULA = 'ONLINK_ULA' 115 ONLINK_GUA = 'ONLINK_GUA' 116 117 118RSSI = { 119 'LINK_QULITY_0': -100, 120 'LINK_QULITY_1': -95, 121 'LINK_QULITY_2': -85, 122 'LINK_QULITY_3': -65, 123} 124 125SNIFFER_ID = int(os.getenv('SNIFFER_ID', 34)) 126PANID = 0xface 127 128LEADER_STARTUP_DELAY = 12 129ROUTER_STARTUP_DELAY = 10 130BORDER_ROUTER_STARTUP_DELAY = 20 131MAX_NEIGHBOR_AGE = 100 132INFINITE_COST_TIMEOUT = 90 133 134MAX_ADVERTISEMENT_INTERVAL = 32 135MLE_END_DEVICE_TIMEOUT = 100 136 137AQ_TIMEOUT = 3 138ADDRESS_QUERY_INITIAL_RETRY_DELAY = 15 139DEFAULT_CHILD_TIMEOUT = 6 140VIRTUAL_TIME = int(os.getenv('VIRTUAL_TIME', 0)) 141PARENT_AGGREGATIOIN_DELAY = 5 142DUA_DAD_DELAY = 5 143DEFAULT_BBR_REGISTRATION_JITTER = 2 144DEFAULT_ROUTER_SELECTION_JITTER = 1 145 146LEADER_NOTIFY_SED_BY_CHILD_UPDATE_REQUEST = True 147 148THREAD_VERSION_1_1 = 2 149THREAD_VERSION_1_2 = 3 150THREAD_VERSION_1_3 = 4 151 152PACKET_VERIFICATION_NONE = 0 153PACKET_VERIFICATION_DEFAULT = 1 154PACKET_VERIFICATION_TREL = 2 155 156 157def create_default_network_data_prefix_sub_tlvs_factories(): 158 return { 159 network_data.TlvType.HAS_ROUTE: 160 network_data.HasRouteFactory(routes_factory=network_data.RoutesFactory( 161 route_factory=network_data.RouteFactory())), 162 network_data.TlvType.BORDER_ROUTER: 163 network_data.BorderRouterFactory(), 164 network_data.TlvType.LOWPAN_ID: 165 network_data.LowpanIdFactory(), 166 } 167 168 169def create_default_network_data_prefix_sub_tlvs_factory(): 170 return network_data.PrefixSubTlvsFactory( 171 sub_tlvs_factories=create_default_network_data_prefix_sub_tlvs_factories()) 172 173 174def create_default_network_data_service_sub_tlvs_factories(): 175 return {network_data.TlvType.SERVER: network_data.ServerFactory()} 176 177 178def create_default_network_data_service_sub_tlvs_factory(): 179 return network_data.ServiceSubTlvsFactory( 180 sub_tlvs_factories=create_default_network_data_service_sub_tlvs_factories()) 181 182 183def create_default_network_data_commissioning_data_sub_tlvs_factories(): 184 return { 185 mesh_cop.TlvType.CHANNEL: mesh_cop.ChannelFactory(), 186 mesh_cop.TlvType.STEERING_DATA: mesh_cop.SteeringDataFactory(), 187 mesh_cop.TlvType.BORDER_AGENT_LOCATOR: mesh_cop.BorderAgentLocatorFactory(), 188 mesh_cop.TlvType.COMMISSIONER_SESSION_ID: mesh_cop.CommissionerSessionIdFactory(), 189 mesh_cop.TlvType.COMMISSIONER_UDP_PORT: mesh_cop.CommissionerUdpPortFactory(), 190 } 191 192 193def create_default_network_data_commissioning_data_sub_tlvs_factory(): 194 return network_data.CommissioningDataSubTlvsFactory( 195 sub_tlvs_factories=create_default_network_data_commissioning_data_sub_tlvs_factories()) 196 197 198def create_default_network_data_tlvs_factories(): 199 return { 200 network_data.TlvType.PREFIX: 201 network_data.PrefixFactory(sub_tlvs_factory=create_default_network_data_prefix_sub_tlvs_factory()), 202 network_data.TlvType.SERVICE: 203 network_data.ServiceFactory(sub_tlvs_factory=create_default_network_data_service_sub_tlvs_factory()), 204 network_data.TlvType.COMMISSIONING: 205 network_data.CommissioningDataFactory( 206 sub_tlvs_factory=create_default_network_data_commissioning_data_sub_tlvs_factory()), 207 } 208 209 210def create_default_network_data_tlvs_factory(): 211 return network_data.NetworkDataTlvsFactory(sub_tlvs_factories=create_default_network_data_tlvs_factories()) 212 213 214def create_default_mle_tlv_route64_factory(): 215 return mle.Route64Factory(link_quality_and_route_data_factory=mle.LinkQualityAndRouteDataFactory()) 216 217 218def create_default_mle_tlv_network_data_factory(): 219 return mle.NetworkDataFactory(network_data_tlvs_factory=create_default_network_data_tlvs_factory()) 220 221 222def create_default_mle_tlv_address_registration_factory(): 223 return mle.AddressRegistrationFactory( 224 addr_compressed_factory=mle.AddressCompressedFactory(), 225 addr_full_factory=mle.AddressFullFactory(), 226 ) 227 228 229def create_default_mle_tlv_thread_discovery_factory(): 230 return mle.ThreadDiscoveryFactory(thread_discovery_tlvs_factory=create_default_thread_discovery_tlvs_factory()) 231 232 233def create_default_thread_discovery_tlvs_factory(): 234 return mesh_cop.ThreadDiscoveryTlvsFactory(sub_tlvs_factories=create_default_thread_discovery_sub_tlvs_factories()) 235 236 237def create_default_thread_discovery_sub_tlvs_factories(): 238 return { 239 mesh_cop.TlvType.DISCOVERY_REQUEST: mesh_cop.DiscoveryRequestFactory(), 240 mesh_cop.TlvType.DISCOVERY_RESPONSE: mesh_cop.DiscoveryResponseFactory(), 241 mesh_cop.TlvType.EXTENDED_PANID: mesh_cop.ExtendedPanidFactory(), 242 mesh_cop.TlvType.NETWORK_NAME: mesh_cop.NetworkNameFactory(), 243 mesh_cop.TlvType.STEERING_DATA: mesh_cop.SteeringDataFactory(), 244 mesh_cop.TlvType.JOINER_UDP_PORT: mesh_cop.JoinerUdpPortFactory(), 245 mesh_cop.TlvType.COMMISSIONER_UDP_PORT: mesh_cop.CommissionerUdpPortFactory(), 246 } 247 248 249def create_default_mle_tlvs_factories(): 250 return { 251 mle.TlvType.SOURCE_ADDRESS: mle.SourceAddressFactory(), 252 mle.TlvType.MODE: mle.ModeFactory(), 253 mle.TlvType.TIMEOUT: mle.TimeoutFactory(), 254 mle.TlvType.CHALLENGE: mle.ChallengeFactory(), 255 mle.TlvType.RESPONSE: mle.ResponseFactory(), 256 mle.TlvType.LINK_LAYER_FRAME_COUNTER: mle.LinkLayerFrameCounterFactory(), 257 mle.TlvType.MLE_FRAME_COUNTER: mle.MleFrameCounterFactory(), 258 mle.TlvType.ROUTE64: create_default_mle_tlv_route64_factory(), 259 mle.TlvType.ADDRESS16: mle.Address16Factory(), 260 mle.TlvType.LEADER_DATA: mle.LeaderDataFactory(), 261 mle.TlvType.NETWORK_DATA: create_default_mle_tlv_network_data_factory(), 262 mle.TlvType.TLV_REQUEST: mle.TlvRequestFactory(), 263 mle.TlvType.SCAN_MASK: mle.ScanMaskFactory(), 264 mle.TlvType.CONNECTIVITY: mle.ConnectivityFactory(), 265 mle.TlvType.LINK_MARGIN: mle.LinkMarginFactory(), 266 mle.TlvType.STATUS: mle.StatusFactory(), 267 mle.TlvType.VERSION: mle.VersionFactory(), 268 mle.TlvType.ADDRESS_REGISTRATION: create_default_mle_tlv_address_registration_factory(), 269 mle.TlvType.CHANNEL: mle.ChannelFactory(), 270 mle.TlvType.PANID: mle.PanIdFactory(), 271 mle.TlvType.ACTIVE_TIMESTAMP: mle.ActiveTimestampFactory(), 272 mle.TlvType.PENDING_TIMESTAMP: mle.PendingTimestampFactory(), 273 mle.TlvType.CSL_CHANNEL: mle.CslChannelFactory(), 274 mle.TlvType.CSL_SYNCHRONIZED_TIMEOUT: mle.CslSynchronizedTimeoutFactory(), 275 mle.TlvType.CSL_CLOCK_ACCURACY: mle.CslClockAccuracyFactory(), 276 mle.TlvType.ACTIVE_OPERATIONAL_DATASET: mle.ActiveOperationalDatasetFactory(), 277 mle.TlvType.PENDING_OPERATIONAL_DATASET: mle.PendingOperationalDatasetFactory(), 278 mle.TlvType.TIME_REQUEST: mle.TimeRequestFactory(), 279 mle.TlvType.TIME_PARAMETER: mle.TimeParameterFactory(), 280 mle.TlvType.THREAD_DISCOVERY: create_default_mle_tlv_thread_discovery_factory(), 281 mle.TlvType.LINK_METRICS_QUERY: mle.LinkMetricsQueryFactory(), 282 mle.TlvType.LINK_METRICS_MANAGEMENT: mle.LinkMetricsManagementFactory(), 283 mle.TlvType.LINK_METRICS_REPORT: mle.LinkMetricsReportFactory(), 284 mle.TlvType.LINK_PROBE: mle.LinkProbeFactory(), 285 } 286 287 288def create_default_mle_crypto_engine(network_key): 289 return net_crypto.CryptoEngine(crypto_material_creator=net_crypto.MleCryptoMaterialCreator(network_key)) 290 291 292def create_default_mle_message_factory(network_key): 293 return mle.MleMessageFactory( 294 aux_sec_hdr_factory=net_crypto.AuxiliarySecurityHeaderFactory(), 295 mle_command_factory=mle.MleCommandFactory(tlvs_factories=create_default_mle_tlvs_factories()), 296 crypto_engine=create_default_mle_crypto_engine(network_key), 297 ) 298 299 300def create_deafult_network_tlvs_factories(): 301 return { 302 network_layer.TlvType.TARGET_EID: 303 network_layer.TargetEidFactory(), 304 network_layer.TlvType.MAC_EXTENDED_ADDRESS: 305 network_layer.MacExtendedAddressFactory(), 306 network_layer.TlvType.RLOC16: 307 network_layer.Rloc16Factory(), 308 network_layer.TlvType.ML_EID: 309 network_layer.MlEidFactory(), 310 network_layer.TlvType.STATUS: 311 network_layer.StatusFactory(), 312 network_layer.TlvType.TIME_SINCE_LAST_TRANSACTION: 313 network_layer.TimeSinceLastTransactionFactory(), 314 network_layer.TlvType.ROUTER_MASK: 315 network_layer.RouterMaskFactory(), 316 network_layer.TlvType.ND_OPTION: 317 network_layer.NdOptionFactory(), 318 network_layer.TlvType.ND_DATA: 319 network_layer.NdDataFactory(), 320 network_layer.TlvType.THREAD_NETWORK_DATA: 321 network_layer.ThreadNetworkDataFactory(create_default_network_data_tlvs_factory()), 322 network_layer.TlvType.XTAL_ACCURACY: 323 network_layer.XtalAccuracyFactory(), 324 # Routing information are distributed in a Thread network by MLE Routing TLV 325 # which is in fact MLE Route64 TLV. Thread specificaton v1.1. - Chapter 5.20 326 network_layer.TlvType.MLE_ROUTING: 327 create_default_mle_tlv_route64_factory(), 328 network_layer.TlvType.IPv6_ADDRESSES: 329 network_layer.IPv6AddressesFactory(), 330 } 331 332 333def create_default_network_tlvs_factory(): 334 return SubTlvsFactory(sub_tlvs_factories=create_deafult_network_tlvs_factories()) 335 336 337def create_default_mesh_cop_tlvs_factories(): 338 return { 339 mesh_cop.TlvType.CHANNEL: mesh_cop.ChannelFactory(), 340 mesh_cop.TlvType.PAN_ID: mesh_cop.PanidFactory(), 341 mesh_cop.TlvType.EXTENDED_PANID: mesh_cop.ExtendedPanidFactory(), 342 mesh_cop.TlvType.NETWORK_NAME: mesh_cop.NetworkNameFactory(), 343 mesh_cop.TlvType.PSKC: mesh_cop.PSKcFactory(), 344 mesh_cop.TlvType.NETWORK_KEY: mesh_cop.NetworkKeyFactory(), 345 mesh_cop.TlvType.NETWORK_KEY_SEQUENCE_COUNTER: mesh_cop.NetworkKeySequenceCounterFactory(), 346 mesh_cop.TlvType.NETWORK_MESH_LOCAL_PREFIX: mesh_cop.NetworkMeshLocalPrefixFactory(), 347 mesh_cop.TlvType.STEERING_DATA: mesh_cop.SteeringDataFactory(), 348 mesh_cop.TlvType.BORDER_AGENT_LOCATOR: mesh_cop.BorderAgentLocatorFactory(), 349 mesh_cop.TlvType.COMMISSIONER_ID: mesh_cop.CommissionerIdFactory(), 350 mesh_cop.TlvType.COMMISSIONER_SESSION_ID: mesh_cop.CommissionerSessionIdFactory(), 351 mesh_cop.TlvType.SECURITY_POLICY: mesh_cop.SecurityPolicyFactory(), 352 mesh_cop.TlvType.GET: mesh_cop.GetFactory(), 353 mesh_cop.TlvType.ACTIVE_TIMESTAMP: mesh_cop.ActiveTimestampFactory(), 354 mesh_cop.TlvType.COMMISSIONER_UDP_PORT: mesh_cop.CommissionerUdpPortFactory(), 355 mesh_cop.TlvType.STATE: mesh_cop.StateFactory(), 356 mesh_cop.TlvType.JOINER_DTLS_ENCAPSULATION: mesh_cop.JoinerDtlsEncapsulationFactory(), 357 mesh_cop.TlvType.JOINER_UDP_PORT: mesh_cop.JoinerUdpPortFactory(), 358 mesh_cop.TlvType.JOINER_IID: mesh_cop.JoinerIIDFactory(), 359 mesh_cop.TlvType.JOINER_ROUTER_LOCATOR: mesh_cop.JoinerRouterLocatorFactory(), 360 mesh_cop.TlvType.JOINER_ROUTER_KEK: mesh_cop.JoinerRouterKEKFactory(), 361 mesh_cop.TlvType.PROVISIONING_URL: mesh_cop.ProvisioningUrlFactory(), 362 mesh_cop.TlvType.VENDOR_NAME: mesh_cop.VendorNameFactory(), 363 mesh_cop.TlvType.VENDOR_MODEL: mesh_cop.VendorModelFactory(), 364 mesh_cop.TlvType.VENDOR_SW_VERSION: mesh_cop.VendorSWVersionFactory(), 365 mesh_cop.TlvType.VENDOR_DATA: mesh_cop.VendorDataFactory(), 366 mesh_cop.TlvType.VENDOR_STACK_VERSION: mesh_cop.VendorStackVersionFactory(), 367 mesh_cop.TlvType.UDP_ENCAPSULATION: mesh_cop.UdpEncapsulationFactory(), 368 mesh_cop.TlvType.IPV6_ADDRESS: mesh_cop.Ipv6AddressFactory(), 369 mesh_cop.TlvType.PENDING_TIMESTAMP: mesh_cop.PendingTimestampFactory(), 370 mesh_cop.TlvType.DELAY_TIMER: mesh_cop.DelayTimerFactory(), 371 mesh_cop.TlvType.CHANNEL_MASK: mesh_cop.ChannelMaskFactory(), 372 mesh_cop.TlvType.COUNT: mesh_cop.CountFactory(), 373 mesh_cop.TlvType.PERIOD: mesh_cop.PeriodFactory(), 374 mesh_cop.TlvType.SCAN_DURATION: mesh_cop.ScanDurationFactory(), 375 mesh_cop.TlvType.ENERGY_LIST: mesh_cop.EnergyListFactory(), 376 } 377 378 379def create_default_mesh_cop_tlvs_factory(): 380 return SubTlvsFactory(sub_tlvs_factories=create_default_mesh_cop_tlvs_factories()) 381 382 383def create_default_network_diag_tlv_factories(): 384 return { 385 network_diag.TlvType.EXT_ADDRESS: network_layer.MacExtendedAddressFactory(), 386 network_diag.TlvType.ADDRESS16: mle.Address16Factory(), 387 network_diag.TlvType.MODE: mle.ModeFactory(), 388 network_diag.TlvType.POLLING_PERIOD: mle.TimeoutFactory(), 389 network_diag.TlvType.CONNECTIVITY: mle.ConnectivityFactory(), 390 network_diag.TlvType.ROUTE64: create_default_mle_tlv_route64_factory(), 391 network_diag.TlvType.LEADER_DATA: mle.LeaderDataFactory(), 392 network_diag.TlvType.NETWORK_DATA: create_default_mle_tlv_network_data_factory(), 393 network_diag.TlvType.IPV6_ADDRESS_LIST: network_diag.Ipv6AddressListFactory(), 394 network_diag.TlvType.MAC_COUNTERS: network_diag.MacCountersFactory(), 395 network_diag.TlvType.BATTERY_LEVEL: network_diag.BatteryLevelFactory(), 396 network_diag.TlvType.SUPPLY_VOLTAGE: network_diag.SupplyVoltageFactory(), 397 network_diag.TlvType.CHILD_TABLE: network_diag.ChildTableFactory(), 398 network_diag.TlvType.CHANNEL_PAGES: network_diag.ChannelPagesFactory(), 399 network_diag.TlvType.TYPE_LIST: network_diag.TypeListFactory(), 400 network_diag.TlvType.MAX_CHILD_TIMEOUT: network_diag.MaxChildTimeoutFactory() 401 } 402 403 404def create_default_network_diag_tlvs_factory(): 405 return SubTlvsFactory(sub_tlvs_factories=create_default_network_diag_tlv_factories()) 406 407 408def create_default_uri_path_based_payload_factories(): 409 network_layer_tlvs_factory = create_default_network_tlvs_factory() 410 mesh_cop_tlvs_factory = create_default_mesh_cop_tlvs_factory() 411 network_diag_tlvs_factory = create_default_network_diag_tlvs_factory() 412 413 return { 414 '/a/as': network_layer_tlvs_factory, 415 '/a/aq': network_layer_tlvs_factory, 416 '/a/ar': network_layer_tlvs_factory, 417 '/a/ae': network_layer_tlvs_factory, 418 '/a/an': network_layer_tlvs_factory, 419 '/a/sd': network_layer_tlvs_factory, 420 '/c/lp': mesh_cop_tlvs_factory, 421 '/c/cs': mesh_cop_tlvs_factory, 422 '/d/da': network_diag_tlvs_factory, 423 '/d/dg': network_diag_tlvs_factory, 424 '/d/dq': network_diag_tlvs_factory, 425 '/d/dr': network_diag_tlvs_factory, 426 '/n/mr': network_layer_tlvs_factory, 427 '/n/dr': network_layer_tlvs_factory, 428 '/n/dn': network_layer_tlvs_factory, 429 } 430 431 432def create_default_coap_message_factory(): 433 return coap.CoapMessageFactory( 434 options_factory=coap.CoapOptionsFactory(), 435 uri_path_based_payload_factories=create_default_uri_path_based_payload_factories(), 436 message_id_to_uri_path_binder=coap.CoapMessageIdToUriPathBinder(), 437 ) 438 439 440def create_default_ipv6_hop_by_hop_options_factories(): 441 return {109: ipv6.MPLOptionFactory()} 442 443 444def create_default_ipv6_hop_by_hop_options_factory(): 445 return ipv6.HopByHopOptionsFactory(options_factories=create_default_ipv6_hop_by_hop_options_factories()) 446 447 448def create_default_based_on_src_dst_ports_udp_payload_factory(network_key): 449 mle_message_factory = create_default_mle_message_factory(network_key) 450 coap_message_factory = create_default_coap_message_factory() 451 dtls_message_factory = create_default_dtls_message_factory() 452 453 return ipv6.UdpBasedOnSrcDstPortsPayloadFactory( 454 src_dst_port_based_payload_factories={ 455 19788: mle_message_factory, 456 61631: coap_message_factory, 457 1000: dtls_message_factory, 458 5683: coap_message_factory, 459 5684: dtls_message_factory, 460 }) 461 462 463def create_default_dtls_message_factory(): 464 return dtls.MessageFactory() 465 466 467def create_default_ipv6_icmp_body_factories(): 468 return { 469 ipv6.ICMP_DESTINATION_UNREACHABLE: ipv6.ICMPv6DestinationUnreachableFactory(), 470 ipv6.ICMP_ECHO_REQUEST: ipv6.ICMPv6EchoBodyFactory(), 471 ipv6.ICMP_ECHO_RESPONSE: ipv6.ICMPv6EchoBodyFactory(), 472 'default': ipv6.BytesPayloadFactory(), 473 } 474 475 476def create_default_ipv6_upper_layer_factories(network_key): 477 return { 478 ipv6.IPV6_NEXT_HEADER_UDP: 479 ipv6.UDPDatagramFactory( 480 udp_header_factory=ipv6.UDPHeaderFactory(), 481 udp_payload_factory=create_default_based_on_src_dst_ports_udp_payload_factory(network_key), 482 ), 483 ipv6.IPV6_NEXT_HEADER_ICMP: 484 ipv6.ICMPv6Factory(body_factories=create_default_ipv6_icmp_body_factories()), 485 } 486 487 488def create_default_lowpan_extension_headers_factories(): 489 return { 490 ipv6.IPV6_NEXT_HEADER_HOP_BY_HOP: 491 lowpan.LowpanHopByHopFactory(hop_by_hop_options_factory=create_default_ipv6_hop_by_hop_options_factory()) 492 } 493 494 495def create_default_ipv6_extension_headers_factories(): 496 return { 497 ipv6.IPV6_NEXT_HEADER_HOP_BY_HOP: 498 ipv6.HopByHopFactory(hop_by_hop_options_factory=create_default_ipv6_hop_by_hop_options_factory()) 499 } 500 501 502def create_default_ipv6_packet_factory(network_key): 503 return ipv6.IPv6PacketFactory( 504 ehf=create_default_ipv6_extension_headers_factories(), 505 ulpf=create_default_ipv6_upper_layer_factories(network_key), 506 ) 507 508 509def create_default_lowpan_decompressor(context_manager): 510 return lowpan.LowpanDecompressor( 511 lowpan_ip_header_factory=lowpan.LowpanIpv6HeaderFactory(context_manager=context_manager), 512 lowpan_extension_headers_factory=lowpan.LowpanExtensionHeadersFactory( 513 ext_headers_factories=create_default_lowpan_extension_headers_factories()), 514 lowpan_udp_header_factory=lowpan.LowpanUdpHeaderFactory(), 515 ) 516 517 518def create_default_thread_context_manager(): 519 context_manager = lowpan.ContextManager() 520 context_manager[0] = lowpan.Context(MESH_LOCAL_PREFIX) 521 522 return context_manager 523 524 525def create_default_lowpan_parser(context_manager, network_key=DEFAULT_NETWORK_KEY): 526 return lowpan.LowpanParser( 527 lowpan_mesh_header_factory=lowpan.LowpanMeshHeaderFactory(), 528 lowpan_decompressor=create_default_lowpan_decompressor(context_manager), 529 lowpan_fragements_buffers_manager=lowpan.LowpanFragmentsBuffersManager(), 530 ipv6_packet_factory=create_default_ipv6_packet_factory(network_key), 531 ) 532 533 534def create_default_thread_message_factory(network_key=DEFAULT_NETWORK_KEY): 535 context_manager = create_default_thread_context_manager() 536 lowpan_parser = create_default_lowpan_parser(context_manager, network_key) 537 538 return message.MessageFactory(lowpan_parser=lowpan_parser) 539 540 541def create_default_thread_sniffer(use_message_factory=True): 542 return sniffer.Sniffer(create_default_thread_message_factory() if use_message_factory else None) 543 544 545def create_default_simulator(use_message_factory=True): 546 if VIRTUAL_TIME: 547 return simulator.VirtualTime(use_message_factory=use_message_factory) 548 return simulator.RealTime(use_message_factory=use_message_factory) 549