From 5af7fdf330ad2d3cf4453eff8588c18cd7d2a64c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 5 Aug 2023 17:08:52 +1000 Subject: [PATCH] AP_Networking: use host byte order addresses internally this should make the lua API easier to handle, as it can manipulate uint32_t easily --- libraries/AP_Networking/AP_Networking.cpp | 30 +++++-------------- libraries/AP_Networking/AP_Networking.h | 5 ++-- .../AP_Networking/AP_Networking_ChibiOS.cpp | 12 ++++---- .../AP_Networking/AP_Networking_address.cpp | 8 ++--- 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/libraries/AP_Networking/AP_Networking.cpp b/libraries/AP_Networking/AP_Networking.cpp index 9ff4d1da83..c5004e0fb4 100644 --- a/libraries/AP_Networking/AP_Networking.cpp +++ b/libraries/AP_Networking/AP_Networking.cpp @@ -142,14 +142,10 @@ void AP_Networking::update() uint32_t AP_Networking::convert_netmask_bitcount_to_ip(const uint32_t netmask_bitcount) { - if (netmask_bitcount > 32) { - return 0; + if (netmask_bitcount >= 32) { + return 0xFFFFFFFFU; } - uint32_t netmask_ip = 0; - for (uint32_t i=0; isnprintf(_str_buffer, sizeof(_str_buffer), "%u.%u.%u.%u", (unsigned)ip[0], (unsigned)ip[1], (unsigned)ip[2], (unsigned)ip[3]) == 0) { - _str_buffer[0] = '\0'; - } + inet_ntop(AF_INET, &ip, _str_buffer, sizeof(_str_buffer)); return _str_buffer; } -const char* AP_Networking::convert_ip_to_str(const uint32_t ip) -{ - uint8_t ip_array[4]; - ip_array[3] = ((ip >> 24) & 0xff); - ip_array[2] = ((ip >> 16) & 0xff); - ip_array[1] = ((ip >> 8) & 0xff); - ip_array[0] = (ip & 0xff); - - return convert_ip_to_str(ip_array); -} - /* convert a string to an ethernet MAC address */ diff --git a/libraries/AP_Networking/AP_Networking.h b/libraries/AP_Networking/AP_Networking.h index f0b4fdb17e..0a5628c97e 100644 --- a/libraries/AP_Networking/AP_Networking.h +++ b/libraries/AP_Networking/AP_Networking.h @@ -11,7 +11,7 @@ #include "AP_Networking_backend.h" /* - Note! all uint32_t IPv4 addresses are in network byte order (big-endian) + Note! all uint32_t IPv4 addresses are in host byte order */ class AP_Networking @@ -145,8 +145,7 @@ public: // helper functions to convert between 32bit IP addresses and null terminated strings and back static uint32_t convert_str_to_ip(const char* ip_str); - static const char* convert_ip_to_str(const uint8_t ip[4]); - static const char* convert_ip_to_str(const uint32_t ip); + static const char* convert_ip_to_str(uint32_t ip); // convert string to ethernet mac address static bool convert_str_to_macaddr(const char *mac_str, uint8_t addr[6]); diff --git a/libraries/AP_Networking/AP_Networking_ChibiOS.cpp b/libraries/AP_Networking/AP_Networking_ChibiOS.cpp index c47bb3d6b2..6e65d13cf8 100644 --- a/libraries/AP_Networking/AP_Networking_ChibiOS.cpp +++ b/libraries/AP_Networking/AP_Networking_ChibiOS.cpp @@ -114,9 +114,9 @@ bool AP_Networking_ChibiOS::init() lwip_options->addrMode = NET_ADDRESS_DHCP; } else { lwip_options->addrMode = NET_ADDRESS_STATIC; - lwip_options->address = frontend.get_ip_param(); - lwip_options->netmask = frontend.get_netmask_param(); - lwip_options->gateway = frontend.get_gateway_param(); + lwip_options->address = htonl(frontend.get_ip_param()); + lwip_options->netmask = htonl(frontend.get_netmask_param()); + lwip_options->gateway = htonl(frontend.get_gateway_param()); } frontend.param.macaddr.get_address(macaddr); lwip_options->macaddress = macaddr; @@ -131,9 +131,9 @@ bool AP_Networking_ChibiOS::init() */ void AP_Networking_ChibiOS::update() { - const uint32_t ip = lwipGetIp(); - const uint32_t nm = lwipGetNetmask(); - const uint32_t gw = lwipGetGateway(); + const uint32_t ip = ntohl(lwipGetIp()); + const uint32_t nm = ntohl(lwipGetNetmask()); + const uint32_t gw = ntohl(lwipGetGateway()); if (ip != activeSettings.ip || nm != activeSettings.nm || diff --git a/libraries/AP_Networking/AP_Networking_address.cpp b/libraries/AP_Networking/AP_Networking_address.cpp index 386dac004c..675b9159f4 100644 --- a/libraries/AP_Networking/AP_Networking_address.cpp +++ b/libraries/AP_Networking/AP_Networking_address.cpp @@ -92,8 +92,6 @@ const AP_Param::GroupInfo AP_Networking_MAC::var_info[] = { AP_GROUPEND }; - - /* IPV4 address parameter class */ @@ -107,8 +105,8 @@ uint32_t AP_Networking_IPV4::get_uint32(void) const { uint32_t v = 0; uint8_t *b = (uint8_t*)&v; - for (uint8_t i=0; i