Skip to content

Commit

Permalink
Bugfix in socketGetLocalAddr, possible memory lifetime problem
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerZ committed Mar 13, 2024
1 parent 81d63e1 commit 4d8c414
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 73 deletions.
16 changes: 7 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CPP_Demo/build/
build_XCPlite/
build_C_Demo/
build_CPP_Demo/
build/
bin/
obj/
*.[oa]
Expand Down Expand Up @@ -35,6 +36,12 @@ obj/
*.log
*.vcxproj.FileListAbsolute.txt

# Xcode
*.DS_Store
Xcode_build/
/XCPlite.xcodeproj/xcuserdata/
/XCPlite.xcodeproj/project.xcworkspace/


# CANape specific
*.pcap
Expand All @@ -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
8 changes: 4 additions & 4 deletions XCPlite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
D52705B52B76D3F900875CEC /* main_cfg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = main_cfg.h; sourceTree = "<group>"; };
Expand All @@ -38,7 +39,6 @@
D5712CA82B719648008E08CC /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
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 = "<group>"; };
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; };
Expand All @@ -64,6 +64,7 @@
D5712CA22B719429008E08CC /* XCPlite */ = {
isa = PBXGroup;
children = (
8017513D2B8E829200D4AD60 /* xcpAppl.c */,
D595B51F2B77FB8C008DC3F2 /* A2L.h */,
D595B51C2B77FB8C008DC3F2 /* platform.h */,
D595B51E2B77FB8C008DC3F2 /* xcp.h */,
Expand All @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions XCPlite/main_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions XCPlite/xcp_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
108 changes: 58 additions & 50 deletions src/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,69 +260,77 @@ int socketClose(SOCKET *sp) {
#include <net/if_dl.h>
#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;
}


Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4d8c414

Please sign in to comment.