- fixed bug in sv_subscriber: missing initialization of allocated memory

This commit is contained in:
Michael Zillgith 2015-11-17 14:56:44 +01:00
parent bae3de6bc3
commit daf876e4df
7 changed files with 88 additions and 84 deletions

View file

@ -23,6 +23,8 @@
#define DEBUG_MMS_SERVER 0 #define DEBUG_MMS_SERVER 0
#define DEBUG_GOOSE_SUBSCRIBER 0 #define DEBUG_GOOSE_SUBSCRIBER 0
#define DEBUG_GOOSE_PUBLISHER 0 #define DEBUG_GOOSE_PUBLISHER 0
#define DEBUG_SV_SUBSCRIBER 0
#define DEBUG_HAL_ETHERNET 0
/* Maximum MMS PDU SIZE - default is 65000 */ /* Maximum MMS PDU SIZE - default is 65000 */
#define CONFIG_MMS_MAXIMUM_PDU_SIZE 65000 #define CONFIG_MMS_MAXIMUM_PDU_SIZE 65000

View file

@ -5,8 +5,6 @@ set(goose_subscriber_example_SRCS
IF(WIN32) IF(WIN32)
IF(WITH_WPCAP)
set_source_files_properties(${goose_subscriber_example_SRCS} set_source_files_properties(${goose_subscriber_example_SRCS}
PROPERTIES LANGUAGE CXX) PROPERTIES LANGUAGE CXX)
add_executable(goose_subscriber_example add_executable(goose_subscriber_example
@ -17,8 +15,6 @@ target_link_libraries(goose_subscriber_example
iec61850 iec61850
) )
ENDIF(WITH_WPCAP)
ELSE(WIN32) ELSE(WIN32)
add_executable(goose_subscriber_example add_executable(goose_subscriber_example

View file

@ -5,20 +5,16 @@ set(sv_subscriber_example_SRCS
IF(WIN32) IF(WIN32)
IF(WITH_WPCAP)
set_source_files_properties(${sv_subscriber_example_SRCS} set_source_files_properties(${sv_subscriber_example_SRCS}
PROPERTIES LANGUAGE CXX) PROPERTIES LANGUAGE CXX)
add_executable(sv_subscriber_example add_executable(sv_subscriber_example
${sv_subscriber_example_SRCS} ${sv_subscriber_example_SRCS}
) )
target_link_libraries(svsubscriber_example target_link_libraries(sv_subscriber_example
iec61850 iec61850
) )
ENDIF(WITH_WPCAP)
ELSE(WIN32) ELSE(WIN32)
add_executable(sv_subscriber_example add_executable(sv_subscriber_example

View file

@ -1,4 +1,4 @@
LIBIEC_HOME=../../../libiec61850 LIBIEC_HOME=../..
PROJECT_BINARY_NAME = sv_subscriber PROJECT_BINARY_NAME = sv_subscriber
PROJECT_SOURCES += sv_subscriber_example.c PROJECT_SOURCES += sv_subscriber_example.c

View file

@ -55,8 +55,8 @@ main(int argc, char** argv)
SVReceiver receiver = SVReceiver_create(); SVReceiver receiver = SVReceiver_create();
if (argc > 1) { if (argc > 1) {
printf("Set interface id: %s\n", argv[1]);
SVReceiver_setInterfaceId(receiver, argv[1]); SVReceiver_setInterfaceId(receiver, argv[1]);
printf("Set interface id: %s\n", argv[1]);
} }
else { else {
printf("Using interface eth0\n"); printf("Using interface eth0\n");

View file

@ -29,6 +29,10 @@
#include "libiec61850_platform_includes.h" #include "libiec61850_platform_includes.h"
#ifndef DEBUG_HAL_ETHERNET
#define DEBUG_HAL_ETHERNET 1
#endif
#if (CONFIG_INCLUDE_ETHERNET_WINDOWS == 1) #if (CONFIG_INCLUDE_ETHERNET_WINDOWS == 1)
@ -92,26 +96,25 @@ static bool dllLoaded = false;
static void static void
loadDLLs(void) loadDLLs(void)
{ {
HINSTANCE hDll = LoadLibrary("iphlpapi.dll"); HINSTANCE hDll = LoadLibrary("iphlpapi.dll");
if (hDll == NULL) { if (hDll == NULL) {
printf("Error loading iphlpapi.dll!\n"); if (DEBUG_HAL_ETHERNET)
return; printf("Error loading iphlpapi.dll!\n");
} return;
}
GetAdaptersAddresses = (pgetadaptersaddresses) GetProcAddress(hDll,
"GetAdaptersAddresses");
GetAdaptersAddresses = (pgetadaptersaddresses) GetProcAddress(hDll, if (GetAdaptersAddresses == NULL)
"GetAdaptersAddresses"); printf("Error loading GetAdaptersAddresses from iphlpapi.dll (%d)\n", (int) GetLastError());
if (GetAdaptersAddresses == NULL)
printf("Error loading GetAdaptersAddresses from iphlpapi.dll (%d)\n", (int) GetLastError());
} }
#endif /* __MINGW64_VERSION_MAJOR */ #endif /* __MINGW64_VERSION_MAJOR */
#endif /* __GNUC__ */ #endif /* __GNUC__ */
static char* static char*
getInterfaceName(int interfaceIndex) getInterfaceName(int interfaceIndex)
{ {
@ -137,6 +140,7 @@ getInterfaceName(int interfaceIndex)
if (i == interfaceIndex) { if (i == interfaceIndex) {
interfaceName = (char*) malloc(strlen(device->name) + 1); interfaceName = (char*) malloc(strlen(device->name) + 1);
strcpy(interfaceName, device->name); strcpy(interfaceName, device->name);
if (DEBUG_HAL_ETHERNET)
printf("Use interface (%s)\n", interfaceName); printf("Use interface (%s)\n", interfaceName);
ifaceFound = true; ifaceFound = true;
break; break;
@ -147,6 +151,7 @@ getInterfaceName(int interfaceIndex)
if (!ifaceFound) if (!ifaceFound)
{ {
if (DEBUG_HAL_ETHERNET)
printf("No ethernet interfaces found! Make sure WinPcap is installed.\n"); printf("No ethernet interfaces found! Make sure WinPcap is installed.\n");
return NULL; return NULL;
} }
@ -159,52 +164,55 @@ getInterfaceName(int interfaceIndex)
static void static void
getAdapterMacAddress(char* pcapAdapterName, uint8_t* macAddress) getAdapterMacAddress(char* pcapAdapterName, uint8_t* macAddress)
{ {
PIP_ADAPTER_ADDRESSES pAddresses = NULL; PIP_ADAPTER_ADDRESSES pAddresses = NULL;
ULONG outBufLen = 0; ULONG outBufLen = 0;
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(65000); pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(65000);
if (GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) { if (GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAddresses); free(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen); pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
} }
if (GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAddresses, &outBufLen) == NO_ERROR) { if (GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAddresses, &outBufLen) == NO_ERROR) {
PIP_ADAPTER_ADDRESSES pAddress = pAddresses; PIP_ADAPTER_ADDRESSES pAddress = pAddresses;
while (pAddress != NULL) { while (pAddress != NULL) {
DWORD addressLength = pAddress->PhysicalAddressLength; DWORD addressLength = pAddress->PhysicalAddressLength;
if (addressLength == 6) { if (addressLength == 6) {
int i; int i;
printf("Adapter %s: ", pAddress->AdapterName); if (DEBUG_HAL_ETHERNET) {
printf("Adapter %s: ", pAddress->AdapterName);
for (i = 0; i < (int) addressLength; i++) { for (i = 0; i < (int) addressLength; i++) {
printf("%02x ", pAddress->PhysicalAddress[i]); printf("%02x ", pAddress->PhysicalAddress[i]);
} }
}
if (strstr(pcapAdapterName, pAddress->AdapterName) != 0) { if (strstr(pcapAdapterName, pAddress->AdapterName) != 0) {
printf(" requested found!"); if (DEBUG_HAL_ETHERNET)
printf(" requested found!");
for (i = 0; i < (int) addressLength; i++) { for (i = 0; i < (int) addressLength; i++) {
macAddress[i] = pAddress->PhysicalAddress[i]; macAddress[i] = pAddress->PhysicalAddress[i];
} }
} }
printf("\n"); printf("\n");
} }
pAddress = pAddress->Next; pAddress = pAddress->Next;
} }
free(pAddresses); free(pAddresses);
} }
else { else {
printf("Error getting device addresses!\n"); printf("Error getting device addresses!\n");
} }
} }
@ -215,8 +223,8 @@ Ethernet_getInterfaceMACAddress(const char* interfaceId, uint8_t* addr)
#ifdef __GNUC__ #ifdef __GNUC__
#ifndef __MINGW64_VERSION_MAJOR #ifndef __MINGW64_VERSION_MAJOR
if (!dllLoaded) { if (!dllLoaded) {
loadDLLs(); loadDLLs();
dllLoaded = true; dllLoaded = true;
} }
#endif #endif
#endif #endif
@ -226,13 +234,13 @@ Ethernet_getInterfaceMACAddress(const char* interfaceId, uint8_t* addr)
long interfaceIndex = strtol(interfaceId, &endPtr, 10); long interfaceIndex = strtol(interfaceId, &endPtr, 10);
if (endPtr != NULL) { if (endPtr != NULL) {
printf("Ethernet_getInterfaceMACAddress: invalid interface number %s\n", interfaceId); printf("Ethernet_getInterfaceMACAddress: invalid interface number %s\n", interfaceId);
return; return;
} }
char* interfaceName = getInterfaceName((int) interfaceIndex); char* interfaceName = getInterfaceName((int) interfaceIndex);
getAdapterMacAddress(interfaceName, addr); getAdapterMacAddress(interfaceName, addr);
} }
@ -276,44 +284,44 @@ Ethernet_sendPacket(EthernetSocket ethSocket, uint8_t* buffer, int packetSize)
void void
Ethernet_setProtocolFilter(EthernetSocket ethSocket, uint16_t etherType) Ethernet_setProtocolFilter(EthernetSocket ethSocket, uint16_t etherType)
{ {
char filterString[100]; char filterString[100];
sprintf(filterString, "(ether proto 0x%04x) or (vlan and ether proto 0x%04x)", etherType, etherType); sprintf(filterString, "(ether proto 0x%04x) or (vlan and ether proto 0x%04x)", etherType, etherType);
if (pcap_compile(ethSocket->rawSocket, &(ethSocket->etherTypeFilter), filterString, 1, 0) < 0) { if (pcap_compile(ethSocket->rawSocket, &(ethSocket->etherTypeFilter), filterString, 1, 0) < 0) {
printf("Compiling packet filter failed!\n"); printf("Compiling packet filter failed!\n");
return; return;
} }
if (pcap_setfilter(ethSocket->rawSocket, &(ethSocket->etherTypeFilter)) < 0) { if (pcap_setfilter(ethSocket->rawSocket, &(ethSocket->etherTypeFilter)) < 0) {
printf("Setting packet filter failed!\n"); printf("Setting packet filter failed!\n");
} }
} }
int int
Ethernet_receivePacket(EthernetSocket self, uint8_t* buffer, int bufferSize) Ethernet_receivePacket(EthernetSocket self, uint8_t* buffer, int bufferSize)
{ {
struct pcap_pkthdr* header; struct pcap_pkthdr* header;
uint8_t* packetData; uint8_t* packetData;
int pcapCode = pcap_next_ex(self->rawSocket, &header, (const unsigned char**) &packetData); int pcapCode = pcap_next_ex(self->rawSocket, &header, (const unsigned char**) &packetData);
if (pcapCode > 0) { if (pcapCode > 0) {
int packetSize = header->caplen; int packetSize = header->caplen;
if (packetSize > bufferSize) if (packetSize > bufferSize)
packetSize = bufferSize; packetSize = bufferSize;
memcpy(buffer, packetData, packetSize); memcpy(buffer, packetData, packetSize);
return packetSize; return packetSize;
} }
else { else {
if (pcapCode < 0) if (pcapCode < 0)
printf("winpcap error\n"); printf("winpcap error\n");
return 0; return 0;
} }
} }
bool bool

View file

@ -80,7 +80,7 @@ struct sSVClientASDU {
SVReceiver SVReceiver
SVReceiver_create(void) SVReceiver_create(void)
{ {
SVReceiver self = (SVReceiver) GLOBAL_MALLOC(sizeof(struct sSVReceiver)); SVReceiver self = (SVReceiver) GLOBAL_CALLOC(1, sizeof(struct sSVReceiver));
if (self != NULL) { if (self != NULL) {
self->subscriberList = LinkedList_create(); self->subscriberList = LinkedList_create();
@ -180,6 +180,8 @@ SVReceiver_destroy(SVReceiver self)
void void
SVReceiver_startThreadless(SVReceiver self) SVReceiver_startThreadless(SVReceiver self)
{ {
printf("SVReceiver_startThreadless\n");
if (self->interfaceId == NULL) if (self->interfaceId == NULL)
self->ethSocket = Ethernet_createSocket(CONFIG_ETHERNET_INTERFACE_ID, NULL); self->ethSocket = Ethernet_createSocket(CONFIG_ETHERNET_INTERFACE_ID, NULL);
else else