From bf24f3c39ebf307e775f398669df4b0f7a7a97be Mon Sep 17 00:00:00 2001 From: dhmhd Date: Mon, 4 Dec 2023 18:24:54 +0300 Subject: [PATCH 1/6] Include missing files to the project. --- src/Windows/hsflowd/hsflowd.vcxproj | 9 +++++---- src/Windows/hsflowd/hsflowd.vcxproj.filters | 3 +++ src/Windows/installHelper/installHelper.vcxproj | 12 ++++++------ src/Windows/installer/installer.vcxproj | 8 ++++---- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Windows/hsflowd/hsflowd.vcxproj b/src/Windows/hsflowd/hsflowd.vcxproj index 66f997d3..2dea4f43 100644 --- a/src/Windows/hsflowd/hsflowd.vcxproj +++ b/src/Windows/hsflowd/hsflowd.vcxproj @@ -29,20 +29,20 @@ true MultiByte - v110 + v143 true MultiByte - v110 + v143 MultiByte - v110 + v143 MultiByte - v110 + v143 @@ -206,6 +206,7 @@ + diff --git a/src/Windows/hsflowd/hsflowd.vcxproj.filters b/src/Windows/hsflowd/hsflowd.vcxproj.filters index a61ba154..0b5923d2 100644 --- a/src/Windows/hsflowd/hsflowd.vcxproj.filters +++ b/src/Windows/hsflowd/hsflowd.vcxproj.filters @@ -81,6 +81,9 @@ Source Files + + Source Files + diff --git a/src/Windows/installHelper/installHelper.vcxproj b/src/Windows/installHelper/installHelper.vcxproj index 575300cb..92cecfe4 100644 --- a/src/Windows/installHelper/installHelper.vcxproj +++ b/src/Windows/installHelper/installHelper.vcxproj @@ -1,7 +1,7 @@  - - + + Debug Win32 @@ -31,26 +31,26 @@ DynamicLibrary true - v110 + v143 Unicode DynamicLibrary true - v110 + v143 Unicode DynamicLibrary false - v110 + v143 true Unicode DynamicLibrary false - v110 + v143 true Unicode diff --git a/src/Windows/installer/installer.vcxproj b/src/Windows/installer/installer.vcxproj index 5553e9fe..ec2913f1 100644 --- a/src/Windows/installer/installer.vcxproj +++ b/src/Windows/installer/installer.vcxproj @@ -31,26 +31,26 @@ Utility true - v110 + v143 Unicode Utility true - v110 + v143 Unicode Utility false - v110 + v143 true Unicode Utility false - v110 + v143 true Unicode From da25b349bee7bd4cb40b71219d28fe57e96638df Mon Sep 17 00:00:00 2001 From: dhmhd Date: Mon, 4 Dec 2023 18:24:54 +0300 Subject: [PATCH 2/6] Replase deprecated GetVersionEx() --- src/Windows/hsflowd/readHidCounters.c | 44 +++++++++++++++++++++------ 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/Windows/hsflowd/readHidCounters.c b/src/Windows/hsflowd/readHidCounters.c index 75a191c1..65500bd9 100644 --- a/src/Windows/hsflowd/readHidCounters.c +++ b/src/Windows/hsflowd/readHidCounters.c @@ -8,6 +8,7 @@ extern "C" { #endif #include "hsflowd.h" +#include /** * Populates the host_descr structure with, computer name for hostname, @@ -30,17 +31,42 @@ void readHidCounters(HSP *sp, SFLHost_hid_counters *hid){ hid->os_name = SFLOS_windows; + if (GetComputerNameExA(ComputerNameDnsHostname, dnsBuf, &dnsLen)) { + uint32_t copyLen = dnsLen < SFL_MAX_HOSTNAME_CHARS ? dnsLen : SFL_MAX_HOSTNAME_CHARS; + memcpy(hid->hostname.str, dnsBuf, copyLen); + hid->hostname.str[copyLen] = '\0'; + hid->hostname.len = copyLen; + } + + hid->os_name = SFLOS_windows; + ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - dwRes = GetVersionEx(&osvi); - if (dwRes){ - sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS,"%d.%d.%d %s", - osvi.dwMajorVersion, - osvi.dwMinorVersion, - osvi.dwBuildNumber, - osvi.szCSDVersion); - hid->os_release.len = (uint32_t)strnlen(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS); + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + + if (IsWindows10OrGreater()) { + sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS, "%s", "Windows 10 or later"); + } + else if (IsWindows8Point1OrGreater()) { + sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS, "%s", "Windows 8.1 or later"); + } + else if (IsWindows8OrGreater()) { + sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS, "%s", "Windows 8 or later"); + } + else if (IsWindows7SP1OrGreater()) { + sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS, "%s", "Windows 7 SP1 or later"); } + else if (IsWindowsVistaSP2OrGreater()) { + sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS, "%s", "Windows Vista SP2 or later"); + } + else { + sprintf_s(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS, "%d.%d.%d %s", + osvi.dwMajorVersion, + osvi.dwMinorVersion, + osvi.dwBuildNumber, + osvi.szCSDVersion); + } + + hid->os_release.len = (uint32_t)strnlen(hid->os_release.str, SFL_MAX_OSRELEASE_CHARS); GetNativeSystemInfo(&si); hid->machine_type = SFLMT_unknown; From 3a096a85aeecad6690ceb67e51ae3c940a3daed5 Mon Sep 17 00:00:00 2001 From: dhmhd Date: Mon, 4 Dec 2023 18:24:54 +0300 Subject: [PATCH 3/6] Fix ternary operators. I've never seen such syntax, consider this a mistake. --- src/sflow/sflow_notifier.c | 2 +- src/sflow/sflow_poller.c | 2 +- src/sflow/sflow_sampler.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sflow/sflow_notifier.c b/src/sflow/sflow_notifier.c index 2e7ec7a0..430ac5c5 100644 --- a/src/sflow/sflow_notifier.c +++ b/src/sflow/sflow_notifier.c @@ -113,7 +113,7 @@ void sfl_notifier_writeEventSample(SFLNotifier *notifier, SFLEvent_discarded_pac es->sequence_number = ++notifier->seqNo; /* copy the other header fields in - event samples always use expanded form */ es->ds_class = SFL_DS_CLASS(notifier->dsi); - es->ds_index = notifier->ds_alias ?: SFL_DS_INDEX(notifier->dsi); + es->ds_index = notifier->ds_alias ? notifier->ds_alias : SFL_DS_INDEX(notifier->dsi); /* send to my receiver */ if(notifier->myReceiver) sfl_receiver_writeEventSample(notifier->myReceiver, es); diff --git a/src/sflow/sflow_poller.c b/src/sflow/sflow_poller.c index 13697434..cbbd57f9 100644 --- a/src/sflow/sflow_poller.c +++ b/src/sflow/sflow_poller.c @@ -136,7 +136,7 @@ void sfl_poller_writeCountersSample(SFLPoller *poller, SFL_COUNTERS_SAMPLE_TYPE /* fill in the rest of the header fields, and send to the receiver */ cs->sequence_number = ++poller->countersSampleSeqNo; uint32_t ds_class = SFL_DS_CLASS(poller->dsi); - uint32_t ds_index = poller->ds_alias ?: SFL_DS_INDEX(poller->dsi); + uint32_t ds_index = poller->ds_alias ? poller->ds_alias : SFL_DS_INDEX(poller->dsi); #ifdef SFL_USE_32BIT_INDEX cs->ds_class = ds_class; cs->ds_index = ds_index; diff --git a/src/sflow/sflow_sampler.c b/src/sflow/sflow_sampler.c index d89d8e9e..40ead069 100644 --- a/src/sflow/sflow_sampler.c +++ b/src/sflow/sflow_sampler.c @@ -148,7 +148,7 @@ void sfl_sampler_writeFlowSample(SFLSampler *sampler, SFL_FLOW_SAMPLE_TYPE *fs) fs->sequence_number = ++sampler->flowSampleSeqNo; /* copy the other header fields in */ uint32_t ds_class = SFL_DS_CLASS(sampler->dsi); - uint32_t ds_index = sampler->ds_alias ?: SFL_DS_INDEX(sampler->dsi); + uint32_t ds_index = sampler->ds_alias ? sampler->ds_alias : SFL_DS_INDEX(sampler->dsi); #ifdef SFL_USE_32BIT_INDEX fs->ds_class = ds_class; fs->ds_index = ds_index; From e269645bd52f62c9f9af8f6d0d312b39bd497dd9 Mon Sep 17 00:00:00 2001 From: dhmhd Date: Mon, 4 Dec 2023 18:24:54 +0300 Subject: [PATCH 4/6] Fix various types issues --- src/Windows/hsflowd/hsflowd.c | 4 ++-- src/Windows/hsflowd/util.c | 6 +++--- src/sflow/sflow_agent.c | 2 +- src/sflow/sflow_receiver.c | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Windows/hsflowd/hsflowd.c b/src/Windows/hsflowd/hsflowd.c index 8b95ae2c..c04590fb 100644 --- a/src/Windows/hsflowd/hsflowd.c +++ b/src/Windows/hsflowd/hsflowd.c @@ -442,7 +442,7 @@ static bool initialiseProgramDataFiles(HSP *sp, wchar_t *programDataDir) myLog(LOG_ERR, "initialiseProgramDataFiles: cannot open VM store file %S\n", vmStoreFile); return false; } else { - int cHandle = _open_osfhandle((long)fileHandle, _O_RDWR | _O_TEXT); + int cHandle = _open_osfhandle((intptr_t)fileHandle, _O_RDWR | _O_TEXT); sp->f_vmStore = _fdopen(cHandle, "r+t"); } fnLen = dirLen+wcslen(HSP_DEFAULT_PORTSTORE)+1; @@ -459,7 +459,7 @@ static bool initialiseProgramDataFiles(HSP *sp, wchar_t *programDataDir) myLog(LOG_ERR, "initialiseProgramDataFiles: cannot open VM store file %S\n", portStoreFile); return false; } else { - int cHandle = _open_osfhandle((long)fileHandle, _O_RDWR | _O_TEXT); + int cHandle = _open_osfhandle((intptr_t)fileHandle, _O_RDWR | _O_TEXT); sp->f_portStore = _fdopen(cHandle, "r+t"); } return true; diff --git a/src/Windows/hsflowd/util.c b/src/Windows/hsflowd/util.c index 468d3e00..28803560 100644 --- a/src/Windows/hsflowd/util.c +++ b/src/Windows/hsflowd/util.c @@ -192,7 +192,7 @@ void *UTHeapQNew(size_t len) utRealm.bufferLists[queueIdx] = utBuf->nxt; } else { // allocate a new one - utBuf = (UTHeapHeader *)my_os_calloc(1<(1) << queueIdx); utRealm.totalAllocatedBytes += (1<h.queueIdx; - memset(utBuf, 0, 1 << queueIdx); + memset(utBuf, 0, static_cast(1) << queueIdx); // put it back on the queue utBuf->nxt = (UTHeapHeader *)(utRealm.bufferLists[queueIdx]); utRealm.bufferLists[queueIdx] = utBuf; @@ -325,7 +325,7 @@ char *my_wcstombs(wchar_t *wcstr) { size_t wcslen = 1+wcsnlen_s(wcstr, UT_DEFAULT_MAX_STRLEN); char *str = (char *)my_calloc(wcslen * sizeof(char)); - size_t numConverted; + size_t numConverted = 0; wcstombs_s(&numConverted, str, wcslen, wcstr, wcslen); return str; } diff --git a/src/sflow/sflow_agent.c b/src/sflow/sflow_agent.c index aad06ac7..098c963a 100644 --- a/src/sflow/sflow_agent.c +++ b/src/sflow/sflow_agent.c @@ -160,7 +160,7 @@ void sfl_agent_set_address(SFLAgent *agent, SFLAddress *ip) uint32_t sfl_agent_uptime_mS(SFLAgent *agent) { - return ((agent->now - agent->bootTime) * 1000) + (agent->now_nS / 1000000); + return static_cast((static_cast(agent->now - agent->bootTime) * 1000) + (agent->now_nS / 1000000)); } /*_________________---------------------------__________________ diff --git a/src/sflow/sflow_receiver.c b/src/sflow/sflow_receiver.c index 4512f377..d520db32 100644 --- a/src/sflow/sflow_receiver.c +++ b/src/sflow/sflow_receiver.c @@ -642,7 +642,7 @@ static int computeFlowSampleElementsSize(SFLReceiver *receiver, SFLFlow_sample_e { SFLFlow_sample_element *elem; uint32_t elemSiz; - uint siz = 4; /* num_elements */ + int siz = 4; /* num_elements */ uint32_t num_elements = 0; for(elem = elements; elem != NULL; elem = elem->nxt) { num_elements++; @@ -883,7 +883,7 @@ int sfl_receiver_writeFlowSample(SFLReceiver *receiver, SFL_FLOW_SAMPLE_TYPE *fs } // sanity check - int dgramSize = ((u_char *)receiver->sampleCollector.datap - (u_char *)receiver->sampleCollector.data); + int64_t dgramSize = ((u_char *)receiver->sampleCollector.datap - (u_char *)receiver->sampleCollector.data); assert(dgramSize - receiver->sampleCollector.pktlen == packedSize); // update the pktlen @@ -959,7 +959,7 @@ int sfl_receiver_writeEventSample(SFLReceiver *receiver, SFLEvent_discarded_pack } // sanity check - int dgramSize = ((u_char *)receiver->sampleCollector.datap - (u_char *)receiver->sampleCollector.data); + int64_t dgramSize = ((u_char *)receiver->sampleCollector.datap - (u_char *)receiver->sampleCollector.data); assert(dgramSize - receiver->sampleCollector.pktlen == packedSize); // update the pktlen From a7412ed5f0c66e03172adfe02f0dd95d758885c4 Mon Sep 17 00:00:00 2001 From: dhmhd Date: Mon, 4 Dec 2023 18:24:54 +0300 Subject: [PATCH 5/6] Set SFL_MAX_HOSTNAME_CHARS to 255 according to RFC1035 # Conflicts: # src/sflow/sflow.h --- src/sflow/sflow.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sflow/sflow.h b/src/sflow/sflow.h index 020921a3..6218d10b 100644 --- a/src/sflow/sflow.h +++ b/src/sflow/sflow.h @@ -839,7 +839,16 @@ typedef struct _SFLHost_par_counters { uint32_t dsIndex; /* sFlowDataSource index */ } SFLHost_par_counters; -#define SFL_MAX_HOSTNAME_CHARS 64 +/* + * From RFC1035 + * + * 2.3.4. Size limits + * names 255 octets or less + * + * 3.1. Name space definitions + * To simplify implementations, the total length of a domain name (i.e., label octets and label length octets) is restricted to 255 octets or less. + */ +#define SFL_MAX_HOSTNAME_CHARS 255 #define SFL_MAX_OSRELEASE_CHARS 32 typedef struct _SFLHost_hid_counters { From 3b2dfdd477122b62e09fc9e767b338ac401c8cf9 Mon Sep 17 00:00:00 2001 From: dhmhd Date: Mon, 4 Dec 2023 18:24:54 +0300 Subject: [PATCH 6/6] Use _TRUNCATE as count parameter of wcstombs_s() to avoid exception Exception occurs in two cases: 1. Widechar representation of domain name is too long and not fit multibyte buffer after conversion; 2. Domain name contains resolve error message. I think it makes sense to use the _TRUNCATE constant https://learn.microsoft.com/en-us/cpp/c-runtime-library/truncate?view=msvc-170 https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/wcstombs-s-wcstombs-s-l?view=msvc-170 --- src/Windows/hsflowd/hsflowd.c | 3 ++- src/Windows/hsflowd/hypervVm.c | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Windows/hsflowd/hsflowd.c b/src/Windows/hsflowd/hsflowd.c index c04590fb..2603ea6e 100644 --- a/src/Windows/hsflowd/hsflowd.c +++ b/src/Windows/hsflowd/hsflowd.c @@ -510,6 +510,7 @@ void main(int argc, char *argv[]) usage(argv[0]); } } + SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = HSP_SERVICE_NAME; ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; @@ -574,7 +575,7 @@ void ServiceMain(int argc, char** argv) if (isService && *programDataDir != NULL) { //set the log file name to the default. size_t dirLen = 0; - if (0 == wcstombs_s(&dirLen, mbcLogFilename, MAX_PATH, programDataDir, wcslen(programDataDir))) { + if (0 == wcstombs_s(&dirLen, mbcLogFilename, MAX_PATH, programDataDir, _TRUNCATE)) { PathAppend(mbcLogFilename, HSP_DEFAULT_LOGFILE); logFilename = mbcLogFilename; } else { diff --git a/src/Windows/hsflowd/hypervVm.c b/src/Windows/hsflowd/hypervVm.c index c28283bd..6d8ca79a 100644 --- a/src/Windows/hsflowd/hypervVm.c +++ b/src/Windows/hsflowd/hypervVm.c @@ -141,19 +141,19 @@ BOOL readXmlInstance(IXmlReader *xmlReader, SFLHost_hid_counters *hid, while (readXmlProperty(xmlReader, &nameVal, &dataVal)) { if (nameVal != NULL) { if (wcscmp(nameVal, XML_FQDN) == 0) { - wcstombs_s(&hnLen, hnamebuf, hnamebufLen, dataVal, wcslen(dataVal)); + wcstombs_s(&hnLen, hnamebuf, hnamebufLen, dataVal, _TRUNCATE); //don't count the NULL if (hnLen > 0) { hnLen--; } - } else if (wcscmp(nameVal, XML_OSNAME) == 0) { + } else if (wcscmp(nameVal, XML_OSNAME) == 0) { if (StrStrIW(dataVal, L"Windows") != NULL) { osName = SFLOS_windows; } else if (StrStrIW(dataVal, L"Linux") != NULL) { osName = SFLOS_linux; } } else if (wcscmp(nameVal, XML_OSVERSION) == 0) { - wcstombs_s(&osrLen, osrelbuf, osrelbufLen, dataVal, wcslen(dataVal)); + wcstombs_s(&osrLen, osrelbuf, osrelbufLen, dataVal, _TRUNCATE); //don't count the NULL if (osrLen > 0) { osrLen--; @@ -326,8 +326,8 @@ static void readVmHidCounters(HVSVmState *state, SFLHost_hid_counters *hid, hid->hostname.str = ""; hid->hostname.len = 0; } else { - size_t hnLen; - wcstombs_s(&hnLen, hnamebuf, hnamebufLen, punycode, wcslen(punycode)); + size_t hnLen = 0; + wcstombs_s(&hnLen, hnamebuf, hnamebufLen, punycode, _TRUNCATE); if (hnLen > 0) { hnLen--; }