From 4d8c414db7db4c9006524ca71d679d99da061449 Mon Sep 17 00:00:00 2001 From: Rainer Zaiser Date: Wed, 13 Mar 2024 15:54:55 +0100 Subject: [PATCH] Bugfix in socketGetLocalAddr, possible memory lifetime problem --- .gitignore | 16 ++--- XCPlite.xcodeproj/project.pbxproj | 8 +-- XCPlite/main_cfg.h | 4 +- XCPlite/xcp_cfg.h | 8 +-- src/platform.c | 108 ++++++++++++++++-------------- 5 files changed, 71 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index 4e282e0..e808787 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ CPP_Demo/build/ build_XCPlite/ build_C_Demo/ build_CPP_Demo/ +build/ bin/ obj/ *.[oa] @@ -35,6 +36,12 @@ obj/ *.log *.vcxproj.FileListAbsolute.txt +# Xcode +*.DS_Store +Xcode_build/ +/XCPlite.xcodeproj/xcuserdata/ +/XCPlite.xcodeproj/project.xcworkspace/ + # CANape specific *.pcap @@ -46,12 +53,3 @@ obj/ Environment CustomProperties.json -# MAC -*.DS_Store -Xcode_build/ -build/ -/XCPlite.xcodeproj/xcuserdata/rainer.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist -/XCPlite.xcodeproj/project.xcworkspace/xcuserdata/rainer.xcuserdatad/UserInterfaceState.xcuserstate -/XCPlite.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings -/XCPlite.xcodeproj/project.xcworkspace/contents.xcworkspacedata -/XCPlite.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/XCPlite.xcodeproj/project.pbxproj b/XCPlite.xcodeproj/project.pbxproj index 8ba7e7f..beb80d3 100644 --- a/XCPlite.xcodeproj/project.pbxproj +++ b/XCPlite.xcodeproj/project.pbxproj @@ -7,11 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + 8017513E2B8E829200D4AD60 /* xcpAppl.c in Sources */ = {isa = PBXBuildFile; fileRef = 8017513D2B8E829200D4AD60 /* xcpAppl.c */; }; D52705B32B76D3BB00875CEC /* xcpEthServer.c in Sources */ = {isa = PBXBuildFile; fileRef = D52705B22B76D3BB00875CEC /* xcpEthServer.c */; }; D5712CA92B719648008E08CC /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = D5712CA82B719648008E08CC /* main.c */; }; D5712CAD2B719673008E08CC /* xcpLite.c in Sources */ = {isa = PBXBuildFile; fileRef = D5712CAB2B719673008E08CC /* xcpLite.c */; }; D5712CAE2B719673008E08CC /* xcpEthTl.c in Sources */ = {isa = PBXBuildFile; fileRef = D5712CAC2B719673008E08CC /* xcpEthTl.c */; }; - D5712CB02B71967F008E08CC /* xcpAppl.c in Sources */ = {isa = PBXBuildFile; fileRef = D5712CAF2B71967F008E08CC /* xcpAppl.c */; }; D5712CB62B71977D008E08CC /* A2L.c in Sources */ = {isa = PBXBuildFile; fileRef = D5712CB52B71977C008E08CC /* A2L.c */; }; D5712CB82B7197AA008E08CC /* platform.c in Sources */ = {isa = PBXBuildFile; fileRef = D5712CB72B7197AA008E08CC /* platform.c */; }; /* End PBXBuildFile section */ @@ -29,6 +29,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 8017513D2B8E829200D4AD60 /* xcpAppl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = xcpAppl.c; path = src/xcpAppl.c; sourceTree = SOURCE_ROOT; }; D52705B22B76D3BB00875CEC /* xcpEthServer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = xcpEthServer.c; path = src/xcpEthServer.c; sourceTree = SOURCE_ROOT; }; D52705B42B76D3F900875CEC /* xcptl_cfg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xcptl_cfg.h; sourceTree = ""; }; D52705B52B76D3F900875CEC /* main_cfg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = main_cfg.h; sourceTree = ""; }; @@ -38,7 +39,6 @@ D5712CA82B719648008E08CC /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = ""; }; D5712CAB2B719673008E08CC /* xcpLite.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = xcpLite.c; path = src/xcpLite.c; sourceTree = SOURCE_ROOT; }; D5712CAC2B719673008E08CC /* xcpEthTl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = xcpEthTl.c; path = src/xcpEthTl.c; sourceTree = SOURCE_ROOT; }; - D5712CAF2B71967F008E08CC /* xcpAppl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xcpAppl.c; sourceTree = ""; }; D5712CB52B71977C008E08CC /* A2L.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = A2L.c; path = src/A2L.c; sourceTree = SOURCE_ROOT; }; D5712CB72B7197AA008E08CC /* platform.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = platform.c; path = src/platform.c; sourceTree = SOURCE_ROOT; }; D595B51B2B77FB8C008DC3F2 /* xcpEthTl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = xcpEthTl.h; path = src/xcpEthTl.h; sourceTree = SOURCE_ROOT; }; @@ -64,6 +64,7 @@ D5712CA22B719429008E08CC /* XCPlite */ = { isa = PBXGroup; children = ( + 8017513D2B8E829200D4AD60 /* xcpAppl.c */, D595B51F2B77FB8C008DC3F2 /* A2L.h */, D595B51C2B77FB8C008DC3F2 /* platform.h */, D595B51E2B77FB8C008DC3F2 /* xcp.h */, @@ -77,7 +78,6 @@ D52705B42B76D3F900875CEC /* xcptl_cfg.h */, D52705B22B76D3BB00875CEC /* xcpEthServer.c */, D5712CB72B7197AA008E08CC /* platform.c */, - D5712CAF2B71967F008E08CC /* xcpAppl.c */, D5712CB52B71977C008E08CC /* A2L.c */, D5712CAC2B719673008E08CC /* xcpEthTl.c */, D5712CAB2B719673008E08CC /* xcpLite.c */, @@ -160,11 +160,11 @@ buildActionMask = 2147483647; files = ( D52705B32B76D3BB00875CEC /* xcpEthServer.c in Sources */, + 8017513E2B8E829200D4AD60 /* xcpAppl.c in Sources */, D5712CB62B71977D008E08CC /* A2L.c in Sources */, D5712CA92B719648008E08CC /* main.c in Sources */, D5712CB82B7197AA008E08CC /* platform.c in Sources */, D5712CAD2B719673008E08CC /* xcpLite.c in Sources */, - D5712CB02B71967F008E08CC /* xcpAppl.c in Sources */, D5712CAE2B719673008E08CC /* xcpEthTl.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/XCPlite/main_cfg.h b/XCPlite/main_cfg.h index 4e23979..33dc0d9 100644 --- a/XCPlite/main_cfg.h +++ b/XCPlite/main_cfg.h @@ -41,9 +41,7 @@ // Debug prints #define OPTION_ENABLE_DBG_PRINTS ON -#define OPTION_DEBUG_LEVEL 2 -//#define OPTION_UDP_DEBUG_LEVEL 0 - +#define OPTION_DEBUG_LEVEL 2 // A2L generation #define OPTION_ENABLE_A2L_GEN ON // Enable A2L generation diff --git a/XCPlite/xcp_cfg.h b/XCPlite/xcp_cfg.h index d908e1b..d556aa2 100644 --- a/XCPlite/xcp_cfg.h +++ b/XCPlite/xcp_cfg.h @@ -70,13 +70,7 @@ //------------------------------------------------------------------------------- -// Debug - -// Debug console prints -#if OPTION_ENABLE_DBG_PRINTS - #define XCP_ENABLE_DEBUG_PRINTS - #define XCP_DBG_LEVEL XCP_DEBUG_LEVEL -#endif +// Debug // Enable extended error checks, performance penalty !!! #define XCP_ENABLE_TEST_CHECKS diff --git a/src/platform.c b/src/platform.c index 03aa752..d563afe 100644 --- a/src/platform.c +++ b/src/platform.c @@ -260,69 +260,77 @@ int socketClose(SOCKET *sp) { #include #endif -static int GetMAC(char* ifname, uint8_t* mac) { - struct ifaddrs* ifap, * ifaptr; - - if (getifaddrs(&ifap) == 0) { - for (ifaptr = ifap; ifaptr != NULL; ifaptr = ifaptr->ifa_next) { +static BOOL GetMAC(char* ifname, uint8_t* mac) { + struct ifaddrs *ifaddrs, *ifa; + if (getifaddrs(&ifaddrs) == 0) { + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (!strcmp(ifa->ifa_name, ifname)) { #ifdef __APPLE__ - if (((ifaptr)->ifa_addr)->sa_family == AF_LINK) { - memcpy(mac, (unsigned char *)LLADDR((struct sockaddr_dl *)(ifaptr)->ifa_addr), 6); - } -#else - if (!strcmp(ifaptr->ifa_name, ifname) && ifaptr->ifa_addr->sa_family == AF_PACKET) { - struct sockaddr_ll* s = (struct sockaddr_ll*)ifaptr->ifa_addr; - memcpy(mac, s->sll_addr, 6); - break; - } + if (ifa->ifa_addr->sa_family == AF_LINK) { + memcpy(mac, (unsigned char *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 6); + DBG_PRINTF4(" %s: MAC = %02X-%02X-%02X-%02X-%02X-%02X\n", ifa->ifa_name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + } +#else + if (ifa->ifa_addr->sa_family == AF_PACKET) { + struct sockaddr_ll* s = (struct sockaddr_ll*)ifa->ifa_addr; + memcpy(mac, s->sll_addr, 6); + DBG_PRINTF4(" %s: MAC = %02X-%02X-%02X-%02X-%02X-%02X\n", ifa->ifa_name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + break; + } #endif + } } - freeifaddrs(ifap); - return ifaptr != NULL; + freeifaddrs(ifaddrs); + return (ifa != NULL); } - return 0; + return FALSE; } -int socketGetLocalAddr(uint8_t* mac, uint8_t* addr) { - +BOOL socketGetLocalAddr(uint8_t* mac, uint8_t* addr) { static uint32_t addr1 = 0; static uint8_t mac1[6] = { 0,0,0,0,0,0 }; - - if (addr1 != 0) { - if (addr) memcpy(addr, &addr1, 4); - if (mac) memcpy(mac, mac1, 6); - return 1; - } - - struct ifaddrs* ifaddr; - char strbuf[100]; - struct ifaddrs* ifa1 = NULL; - - if (-1 != getifaddrs(&ifaddr)) { - for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if ((NULL != ifa->ifa_addr) && (AF_INET == ifa->ifa_addr->sa_family)) { - struct sockaddr_in* sa = (struct sockaddr_in*)(ifa->ifa_addr); - if (0x100007f != sa->sin_addr.s_addr) { /* not loop back adapter (127.0.0.1) */ - inet_ntop(AF_INET, &sa->sin_addr.s_addr, strbuf, sizeof(strbuf)); - DBG_PRINTF3(" Network interface %s: ip=%s\n", ifa->ifa_name, strbuf); - if (addr1 == 0) { - addr1 = sa->sin_addr.s_addr; - ifa1 = ifa; + if (addr1 == 0) { + struct ifaddrs *ifaddrs, *ifa; + struct ifaddrs *ifa1 = NULL; + char strbuf[100]; + if (-1 != getifaddrs(&ifaddrs)) { + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if ((NULL != ifa->ifa_addr) && (AF_INET == ifa->ifa_addr->sa_family)) { // IPV4 + struct sockaddr_in* sa = (struct sockaddr_in*)(ifa->ifa_addr); + if (0x100007f != sa->sin_addr.s_addr) { /* not 127.0.0.1 */ +#ifdef DBG_LEVEL + if (DBG_LEVEL >= 5) { + inet_ntop(AF_INET, &sa->sin_addr.s_addr, strbuf, sizeof(strbuf)); + printf(" %s: IPV4 = %s\n", ifa->ifa_name, strbuf); + } +#endif + if (addr1 == 0) { + addr1 = sa->sin_addr.s_addr; + ifa1 = ifa; + } } } } + if (addr1 != 0) { + GetMAC(ifa1->ifa_name, mac1); +#ifdef DBG_LEVEL + if (DBG_LEVEL >= 3) { + inet_ntop(AF_INET, &addr1, strbuf, sizeof(strbuf)); + printf(" Use IPV4 adapter %s with IP=%s, MAC=%02X-%02X-%02X-%02X-%02X-%02X for A2L info and clock UUID\n", ifa1->ifa_name, strbuf, mac1[0], mac1[1], mac1[2], mac1[3], mac1[4], mac1[5]); + } +#endif + } + freeifaddrs(ifaddrs); } - freeifaddrs(ifaddr); } if (addr1 != 0) { - GetMAC(ifa1->ifa_name, mac1); if (mac) memcpy(mac, mac1, 6); if (addr) memcpy(addr, &addr1, 4); - inet_ntop(AF_INET, &addr1, strbuf, sizeof(strbuf)); - DBG_PRINTF3(" Use adapter %s with ip=%s, mac=%02X-%02X-%02X-%02X-%02X-%02X for A2L info and clock UUID\n", ifa1->ifa_name, strbuf, mac1[0], mac1[1], mac1[2], mac1[3], mac1[4], mac1[5]); - return 1; + return TRUE; + } + else { + return FALSE; } - return 0; } @@ -498,7 +506,7 @@ BOOL socketGetLocalAddr(uint8_t* mac, uint8_t* addr) { if (pAdapter->Type == MIB_IF_TYPE_ETHERNET) { inet_pton(AF_INET, pAdapter->IpAddressList.IpAddress.String, &a); if (a!=0) { - +#ifdef DBG_LEVEL DBG_PRINTF3(" Ethernet adapter %" PRIu32 ":", (uint32_t) pAdapter->Index); //DBG_PRINTF3(" %s", pAdapter->AdapterName); DBG_PRINTF3(" %s", pAdapter->Description); @@ -508,7 +516,7 @@ BOOL socketGetLocalAddr(uint8_t* mac, uint8_t* addr) { //DBG_PRINTF3(" Gateway: %s", pAdapter->GatewayList.IpAddress.String); //if (pAdapter->DhcpEnabled) DBG_PRINTF3(" DHCP"); DBG_PRINT3("\n"); - +#endif if (addr1[0] == 0 ) { memcpy(addr1, (uint8_t*)&a, 4); memcpy(mac1, pAdapter->Address, 6); @@ -658,7 +666,7 @@ Linux clock type Not available on WSL */ #define CLOCK_TYPE CLOCK_REALTIME -/// #define CLOCK_TYPE CLOCK_TAI +// #define CLOCK_TYPE CLOCK_TAI static struct timespec gtr; #ifndef CLOCK_USE_UTC_TIME_NS @@ -711,7 +719,7 @@ BOOL clockInit() clockGet(); #ifdef DBG_LEVEL - if (DBG_LEVEL >= 2) { + if (DBG_LEVEL >= 4) { uint64_t t1, t2; char s[128]; struct timespec gts;