[border-agent] use ephemeral port (#6450)

This commit changes border agent service to use ephemeral port by
default. This change makes it easier to support multiple Thread
interfaces on a single host.
This commit is contained in:
Yakun Xu
2021-04-20 22:32:07 -07:00
committed by GitHub
parent e3b971e497
commit 8467a3d522
19 changed files with 112 additions and 11 deletions
+11 -1
View File
@@ -65,13 +65,23 @@ typedef enum otBorderAgentState
/**
* This function gets the state of Thread Border Agent role.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aInstance A pointer to an OpenThread instance.
*
* @returns State of the Border Agent.
*
*/
otBorderAgentState otBorderAgentGetState(otInstance *aInstance);
/**
* This function gets the UDP port of Thread Border Agent service.
*
* @param[in] aInstance A pointer to an OpenThread instance.
*
* @returns UDP port of the Border Agent.
*
*/
uint16_t otBorderAgentGetUdpPort(otInstance *aInstance);
/**
* @}
*
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (102)
#define OPENTHREAD_API_VERSION (103)
/**
* @addtogroup api-instance
+11
View File
@@ -21,6 +21,7 @@ Done
## OpenThread Command List
- [ba](#ba-port)
- [bbr](#bbr)
- [br](#br)
- [bufferinfo](#bufferinfo)
@@ -316,6 +317,16 @@ Set jitter (in seconds) for Backbone Router registration for Thread 1.2 FTD.
Done
```
### ba port
Print border agent service port.
```bash
> ba port
49152
Done
```
### br
Enbale/disable the Border Routing functionality.
+14
View File
@@ -285,6 +285,20 @@ otError Interpreter::ProcessHelp(uint8_t aArgsLength, char *aArgs[])
return OT_ERROR_NONE;
}
#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
otError Interpreter::ProcessBorderAgent(uint8_t aArgsLength, char *aArgs[])
{
otError error = OT_ERROR_NONE;
VerifyOrExit(aArgsLength == 1 && strcmp(aArgs[0], "port") == 0, error = OT_ERROR_INVALID_COMMAND);
OutputLine("%hu", otBorderAgentGetUdpPort(mInstance));
exit:
return error;
}
#endif
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
otError Interpreter::ProcessBorderRouting(uint8_t aArgsLength, char *aArgs[])
{
+6
View File
@@ -332,6 +332,9 @@ private:
otError ProcessCcaThreshold(uint8_t aArgsLength, char *aArgs[]);
otError ProcessBufferInfo(uint8_t aArgsLength, char *aArgs[]);
otError ProcessChannel(uint8_t aArgsLength, char *aArgs[]);
#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
otError ProcessBorderAgent(uint8_t aArgsLength, char *aArgs[]);
#endif
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
otError ProcessBorderRouting(uint8_t aArgsLength, char *aArgs[]);
#endif
@@ -638,6 +641,9 @@ private:
void HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo &aInfo);
static constexpr Command sCommands[] = {
#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
{"ba", &Interpreter::ProcessBorderAgent},
#endif
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
{"bbr", &Interpreter::ProcessBackboneRouter},
#endif
+7
View File
@@ -49,4 +49,11 @@ otBorderAgentState otBorderAgentGetState(otInstance *aInstance)
return static_cast<otBorderAgentState>(instance.Get<MeshCoP::BorderAgent>().GetState());
}
uint16_t otBorderAgentGetUdpPort(otInstance *aInstance)
{
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.Get<MeshCoP::BorderAgent>().GetUdpPort();
}
#endif // OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
+8
View File
@@ -155,6 +155,14 @@ public:
*/
MeshCoP::Dtls &GetDtls(void) { return mDtls; }
/**
* This method gets the UDP port of this agent.
*
* @returns UDP port number.
*
*/
uint16_t GetUdpPort(void) const { return mDtls.GetUdpPort(); }
/**
* This method sets the PSK.
*
+10
View File
@@ -65,4 +65,14 @@
#define OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE 0
#endif
/**
* @def OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT
*
* Specifies the Border Agent UDP port, and use 0 for ephemeral port.
*
*/
#ifndef OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT
#define OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT 0
#endif
#endif // CONFIG_BORDER_ROUTER_H_
+1 -1
View File
@@ -48,7 +48,7 @@
/**
* @def OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT
*
* Specifies the SRP Server UDP port.
* Specifies the SRP Server UDP port, and use 0 for ephemeral port.
*
*/
#ifndef OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT
+9
View File
@@ -48,6 +48,10 @@
namespace ot {
namespace MeshCoP {
namespace {
constexpr uint16_t kBorderAgentUdpPort = OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT; ///< UDP port of border agent service.
}
void BorderAgent::ForwardContext::Init(Instance & aInstance,
const Coap::Message &aMessage,
bool aPetition,
@@ -570,6 +574,11 @@ void BorderAgent::HandleConnected(bool aConnected)
}
}
uint16_t BorderAgent::GetUdpPort(void) const
{
return Get<Coap::CoapSecure>().GetUdpPort();
}
Error BorderAgent::Start(void)
{
Error error;
+8
View File
@@ -74,6 +74,14 @@ public:
*/
explicit BorderAgent(Instance &aInstance);
/**
* This method gets the UDP port of this service.
*
* @returns UDP port number.
*
*/
uint16_t GetUdpPort(void) const;
/**
* This method starts the Border Agent service.
*
+5
View File
@@ -208,6 +208,11 @@ exit:
return;
}
uint16_t Dtls::GetUdpPort(void) const
{
return mSocket.GetSockName().GetPort();
}
Error Dtls::Bind(uint16_t aPort)
{
Error error;
+8
View File
@@ -135,6 +135,14 @@ public:
*/
Error Bind(uint16_t aPort);
/**
* This method gets the UDP port of this session.
*
* @returns UDP port number.
*
*/
uint16_t GetUdpPort(void) const;
/**
* This method binds this DTLS with a transport callback.
*
+1 -1
View File
@@ -59,7 +59,7 @@ namespace MeshCoP {
enum
{
kBorderAgentUdpPort = 49191, ///< UDP port of border agent service.
kNativeCommissionerUdpPort = 49191, ///< UDP port of native commissioner service.
};
/**
+1 -1
View File
@@ -88,7 +88,7 @@ bool Filter::Accept(Message &aMessage) const
}
// Allow native commissioner traffic
if (mAllowNativeCommissioner && dstport == MeshCoP::kBorderAgentUdpPort)
if (mAllowNativeCommissioner && dstport == MeshCoP::kNativeCommissionerUdpPort)
{
ExitNow(rval = true);
}
+2 -1
View File
@@ -2909,7 +2909,8 @@ Error MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, const M
if (Get<KeyManager>().GetSecurityPolicy().mNativeCommissioningEnabled)
{
SuccessOrExit(error = Tlv::Append<MeshCoP::CommissionerUdpPortTlv>(*message, MeshCoP::kBorderAgentUdpPort));
SuccessOrExit(error =
Tlv::Append<MeshCoP::CommissionerUdpPortTlv>(*message, MeshCoP::kNativeCommissionerUdpPort));
discoveryResponse.SetNativeCommissioner(true);
}
+3
View File
@@ -167,4 +167,7 @@ expect_line "Done"
send "diag start\n"
expect ": InvalidState"
send "ba port\n"
expect "Done"
dispose_all
@@ -232,13 +232,16 @@ class TestDnssdServerOnBr(thread_cert.TestCase):
dp_instance_name = f'{network_name}._meshcop._udp.default.service.arpa.'
dp_hostname = lambda x: x.endswith('.default.service.arpa.')
def check_border_agent_port(port):
return 0 < port <= 65535
dig_result = self.nodes[DIGGER].dns_dig(server_addr, dp_service_name, 'PTR')
self._assert_dig_result_matches(
dig_result, {
'QUESTION': [(dp_service_name, 'IN', 'PTR'),],
'ANSWER': [(dp_service_name, 'IN', 'PTR', dp_instance_name),],
'ADDITIONAL': [
(dp_instance_name, 'IN', 'SRV', 0, 0, config.BORDER_AGENT_UDP_PORT, dp_hostname),
(dp_instance_name, 'IN', 'SRV', 0, 0, check_border_agent_port, dp_hostname),
(dp_instance_name, 'IN', 'TXT', lambda txt: (isinstance(txt, dict) and txt.get(
'nn') == network_name and 'xp' in txt and 'tv' in txt and 'dd' in txt)),
],
@@ -258,7 +261,7 @@ class TestDnssdServerOnBr(thread_cert.TestCase):
self._assert_dig_result_matches(
dig_result, {
'QUESTION': [(dp_instance_name, 'IN', 'SRV'),],
'ANSWER': [(dp_instance_name, 'IN', 'SRV', 0, 0, config.BORDER_AGENT_UDP_PORT, dp_hostname),],
'ANSWER': [(dp_instance_name, 'IN', 'SRV', 0, 0, check_border_agent_port, dp_hostname),],
'ADDITIONAL': [(dp_instance_name, 'IN', 'TXT', lambda txt: (isinstance(txt, dict) and txt.get(
'nn') == network_name and 'xp' in txt and 'tv' in txt and 'dd' in txt)),],
})
@@ -269,7 +272,7 @@ class TestDnssdServerOnBr(thread_cert.TestCase):
'QUESTION': [(dp_instance_name, 'IN', 'TXT'),],
'ANSWER': [(dp_instance_name, 'IN', 'TXT', lambda txt: (isinstance(txt, dict) and txt.get(
'nn') == network_name and 'xp' in txt and 'tv' in txt and 'dd' in txt)),],
'ADDITIONAL': [(dp_instance_name, 'IN', 'SRV', 0, 0, config.BORDER_AGENT_UDP_PORT, dp_hostname),],
'ADDITIONAL': [(dp_instance_name, 'IN', 'SRV', 0, 0, check_border_agent_port, dp_hostname),],
})
if dp_ip6_address is not None:
-2
View File
@@ -144,8 +144,6 @@ LEADER_NOTIFY_SED_BY_CHILD_UPDATE_REQUEST = True
THREAD_VERSION_1_1 = 2
THREAD_VERSION_1_2 = 3
BORDER_AGENT_UDP_PORT = 49191
def create_default_network_data_prefix_sub_tlvs_factories():
return {