- 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_GOOSE_SUBSCRIBER 0
#define DEBUG_GOOSE_PUBLISHER 0
#define DEBUG_SV_SUBSCRIBER 0
#define DEBUG_HAL_ETHERNET 0
/* Maximum MMS PDU SIZE - default is 65000 */
#define CONFIG_MMS_MAXIMUM_PDU_SIZE 65000

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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