|
|
@@ -49,21 +49,20 @@
|
|
|
* string if possible. On conversion success @p errno is set to zero.
|
|
|
* On failure @p errno is set to ERANGE or EINVAL.
|
|
|
*/
|
|
|
-static ULONG StrToIfaceIdx(const char *iface)
|
|
|
-{
|
|
|
- ULONG iface_idx;
|
|
|
- char *end;
|
|
|
- /* The input string is a decimal interface index number or an
|
|
|
- * interface name. */
|
|
|
- errno = 0; /* To distinguish success / failure later */
|
|
|
- iface_idx = strtoul(iface, &end, 10);
|
|
|
- /* overflow is signaled by (errno == ERANGE) */
|
|
|
-
|
|
|
- if ((iface == end) || /* No digits were found */
|
|
|
- ('\0' != *end)) { /* More characters after number */
|
|
|
- errno = EINVAL; /* Signal conversion failure */
|
|
|
- }
|
|
|
- return iface_idx;
|
|
|
+static ULONG StrToIfaceIdx(const char *iface) {
|
|
|
+ ULONG iface_idx;
|
|
|
+ char *end;
|
|
|
+ /* The input string is a decimal interface index number or an
|
|
|
+ * interface name. */
|
|
|
+ errno = 0; /* To distinguish success / failure later */
|
|
|
+ iface_idx = strtoul(iface, &end, 10);
|
|
|
+ /* overflow is signaled by (errno == ERANGE) */
|
|
|
+
|
|
|
+ if ( (iface == end) || /* No digits were found */
|
|
|
+ ('\0' != *end) ) { /* More characters after number */
|
|
|
+ errno = EINVAL; /* Signal conversion failure */
|
|
|
+ }
|
|
|
+ return iface_idx;
|
|
|
}
|
|
|
|
|
|
/** @brief Derive the interface index from a given interface alias name string
|
|
|
@@ -81,29 +80,29 @@ static ULONG StrToIfaceIdx(const char *iface)
|
|
|
* The LUID can then be used to retrieve the interface index needed for the
|
|
|
* other functions by calling ConvertInterfaceLuidToIndex().
|
|
|
*/
|
|
|
-static DWORD ConvertToIndexFromFakeAlias(const char *iface, PNET_IFINDEX iface_idx)
|
|
|
-{
|
|
|
- WCHAR *p_if_alias;
|
|
|
- NET_LUID if_luid;
|
|
|
-
|
|
|
- size_t mbtowc_rc = mbstowcs(NULL, iface, 0);
|
|
|
- if ((size_t)-1 == mbtowc_rc) { /* Invalid byte sequence encountered */
|
|
|
- return ERROR_NO_UNICODE_TRANSLATION;
|
|
|
- }
|
|
|
+static DWORD ConvertToIndexFromFakeAlias(const char *iface,
|
|
|
+ PNET_IFINDEX iface_idx) {
|
|
|
+ WCHAR *p_if_alias;
|
|
|
+ NET_LUID if_luid;
|
|
|
|
|
|
- size_t wc_cnt = mbtowc_rc+1U; /* +1U for nul character */
|
|
|
- p_if_alias = MALLOC(sizeof(WCHAR) * wc_cnt);
|
|
|
- if (NULL == p_if_alias) {
|
|
|
- return ERROR_OUTOFMEMORY;
|
|
|
- }
|
|
|
+ size_t mbtowc_rc = mbstowcs(NULL, iface, 0);
|
|
|
+ if ( (size_t)-1 == mbtowc_rc ) { /* Invalid byte sequence encountered */
|
|
|
+ return ERROR_NO_UNICODE_TRANSLATION;
|
|
|
+ }
|
|
|
|
|
|
- (void) mbstowcs(p_if_alias, iface, wc_cnt);
|
|
|
- DWORD cnv_status = ConvertInterfaceAliasToLuid(p_if_alias, &if_luid);
|
|
|
- if (NETIO_SUCCESS(cnv_status)) {
|
|
|
- cnv_status = ConvertInterfaceLuidToIndex(&if_luid, iface_idx);
|
|
|
- }
|
|
|
- FREE(p_if_alias);
|
|
|
- return cnv_status;
|
|
|
+ size_t wc_cnt = mbtowc_rc + 1U; /* +1U for nul character */
|
|
|
+ p_if_alias = MALLOC(sizeof(WCHAR) * wc_cnt);
|
|
|
+ if (NULL == p_if_alias) {
|
|
|
+ return ERROR_OUTOFMEMORY;
|
|
|
+ }
|
|
|
+
|
|
|
+ (void) mbstowcs(p_if_alias, iface, wc_cnt);
|
|
|
+ DWORD cnv_status = ConvertInterfaceAliasToLuid(p_if_alias, &if_luid);
|
|
|
+ if (NETIO_SUCCESS(cnv_status) ) {
|
|
|
+ cnv_status = ConvertInterfaceLuidToIndex(&if_luid, iface_idx);
|
|
|
+ }
|
|
|
+ FREE(p_if_alias);
|
|
|
+ return cnv_status;
|
|
|
}
|
|
|
|
|
|
/** @brief Determines the interface index number from string input
|
|
|
@@ -122,8 +121,8 @@ static DWORD ConvertToIndexFromFakeAlias(const char *iface, PNET_IFINDEX iface_i
|
|
|
* alias name. This function then in turn calls ConvertToIndexFromFakeAlias()
|
|
|
* to find an interface matching that alias.
|
|
|
*/
|
|
|
-static EipStatus DetermineIfaceIndexByString(const char *iface, PNET_IFINDEX iface_idx)
|
|
|
-{
|
|
|
+static EipStatus DetermineIfaceIndexByString(const char *iface,
|
|
|
+ PNET_IFINDEX iface_idx) {
|
|
|
*iface_idx = StrToIfaceIdx(iface);
|
|
|
|
|
|
BOOL arg_is_numerical = (0 == errno);
|
|
|
@@ -131,8 +130,10 @@ static EipStatus DetermineIfaceIndexByString(const char *iface, PNET_IFINDEX ifa
|
|
|
DWORD cnv_status = ConvertToIndexFromFakeAlias(iface, iface_idx);
|
|
|
if (NO_ERROR != cnv_status) {
|
|
|
char *error_message = GetErrorMessage(cnv_status);
|
|
|
- OPENER_TRACE_ERR("ConvertToIndexFromFakeAlias() failed: %" PRIDW " - %s\n",
|
|
|
- cnv_status, error_message);
|
|
|
+ OPENER_TRACE_ERR(
|
|
|
+ "ConvertToIndexFromFakeAlias() failed: %" PRIDW " - %s\n",
|
|
|
+ cnv_status,
|
|
|
+ error_message);
|
|
|
FreeErrorMessage(error_message);
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
@@ -154,7 +155,8 @@ static EipStatus DetermineIfaceIndexByString(const char *iface, PNET_IFINDEX ifa
|
|
|
* which information to include and what information to exclude from the
|
|
|
* result.
|
|
|
*/
|
|
|
-EipStatus RetrieveAdapterAddressesTable(ULONG flags, PIP_ADAPTER_ADDRESSES *pp_addr_table) {
|
|
|
+EipStatus RetrieveAdapterAddressesTable(ULONG flags,
|
|
|
+ PIP_ADAPTER_ADDRESSES *pp_addr_table) {
|
|
|
PIP_ADAPTER_ADDRESSES p_addr_table;
|
|
|
ULONG ret_val;
|
|
|
/* Start allocating with a guessed minimum size. */
|
|
|
@@ -162,10 +164,15 @@ EipStatus RetrieveAdapterAddressesTable(ULONG flags, PIP_ADAPTER_ADDRESSES *pp_a
|
|
|
do {
|
|
|
p_addr_table = (PIP_ADAPTER_ADDRESSES)MALLOC(outBufLen);
|
|
|
if (NULL == p_addr_table) {
|
|
|
- OPENER_TRACE_ERR("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
|
|
|
+ OPENER_TRACE_ERR(
|
|
|
+ "Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
- ret_val = GetAdaptersAddresses(AF_INET, flags, NULL, p_addr_table, &outBufLen);
|
|
|
+ ret_val = GetAdaptersAddresses(AF_INET,
|
|
|
+ flags,
|
|
|
+ NULL,
|
|
|
+ p_addr_table,
|
|
|
+ &outBufLen);
|
|
|
|
|
|
if (ERROR_BUFFER_OVERFLOW == ret_val) {
|
|
|
FREE(p_addr_table);
|
|
|
@@ -212,7 +219,7 @@ static DWORD WideToCipString(const WCHAR *const src,
|
|
|
* the nul terminator.
|
|
|
*/
|
|
|
const size_t num_chars = wcstombs(NULL, src, 0);
|
|
|
- if ((size_t)-1 == num_chars) {
|
|
|
+ if ( (size_t)-1 == num_chars ) {
|
|
|
return ERROR_NO_UNICODE_TRANSLATION;
|
|
|
}
|
|
|
if (num_chars >= UINT16_MAX) {
|
|
|
@@ -250,7 +257,7 @@ static DWORD WideToCipString(const WCHAR *const src,
|
|
|
* @return IPv4 address taken from @p socket_address
|
|
|
*/
|
|
|
static CipUdint GetIpFromSocketAddress(const SOCKET_ADDRESS *socket_address) {
|
|
|
- SOCKADDR_IN *sin = ((SOCKADDR_IN*)socket_address->lpSockaddr);
|
|
|
+ SOCKADDR_IN *sin = ( (SOCKADDR_IN *)socket_address->lpSockaddr );
|
|
|
return sin->sin_addr.S_un.S_addr;
|
|
|
}
|
|
|
|
|
|
@@ -258,10 +265,11 @@ static CipUdint GetIpFromSocketAddress(const SOCKET_ADDRESS *socket_address) {
|
|
|
/* ---------- Public functions implementation ---------- */
|
|
|
|
|
|
/* For Doxygen descriptions see opener_api.h. */
|
|
|
-EipStatus IfaceGetMacAddress(const char *iface, uint8_t *physical_address) {
|
|
|
- ULONG iface_idx;
|
|
|
+EipStatus IfaceGetMacAddress(const char *iface,
|
|
|
+ uint8_t *physical_address) {
|
|
|
+ ULONG iface_idx;
|
|
|
|
|
|
- if(kEipStatusOk != DetermineIfaceIndexByString(iface, &iface_idx)) {
|
|
|
+ if(kEipStatusOk != DetermineIfaceIndexByString(iface, &iface_idx) ) {
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
|
|
|
@@ -270,7 +278,7 @@ EipStatus IfaceGetMacAddress(const char *iface, uint8_t *physical_address) {
|
|
|
GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER |
|
|
|
GAA_FLAG_SKIP_FRIENDLY_NAME;
|
|
|
PIP_ADAPTER_ADDRESSES p_addr_table = NULL;
|
|
|
- if (kEipStatusOk != RetrieveAdapterAddressesTable(flags, &p_addr_table)) {
|
|
|
+ if (kEipStatusOk != RetrieveAdapterAddressesTable(flags, &p_addr_table) ) {
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
|
|
|
@@ -280,12 +288,17 @@ EipStatus IfaceGetMacAddress(const char *iface, uint8_t *physical_address) {
|
|
|
if (iface_idx == p_addr_entry->IfIndex) {
|
|
|
/* Get MAC address from matched interface */
|
|
|
OPENER_TRACE_INFO("MAC address: %02" PRIX8 "-%02" PRIX8 "-%02" PRIX8
|
|
|
- "-%02" PRIX8 "-%02" PRIX8 "-%02" PRIX8 "\n",
|
|
|
- p_addr_entry->PhysicalAddress[0], p_addr_entry->PhysicalAddress[1],
|
|
|
- p_addr_entry->PhysicalAddress[2], p_addr_entry->PhysicalAddress[3],
|
|
|
- p_addr_entry->PhysicalAddress[4], p_addr_entry->PhysicalAddress[5]);
|
|
|
- memcpy_s(physical_address, 6,
|
|
|
- p_addr_entry->PhysicalAddress, p_addr_entry->PhysicalAddressLength);
|
|
|
+ "-%02" PRIX8 "-%02" PRIX8 "-%02" PRIX8 "\n",
|
|
|
+ p_addr_entry->PhysicalAddress[0],
|
|
|
+ p_addr_entry->PhysicalAddress[1],
|
|
|
+ p_addr_entry->PhysicalAddress[2],
|
|
|
+ p_addr_entry->PhysicalAddress[3],
|
|
|
+ p_addr_entry->PhysicalAddress[4],
|
|
|
+ p_addr_entry->PhysicalAddress[5]);
|
|
|
+ memcpy_s(physical_address,
|
|
|
+ 6,
|
|
|
+ p_addr_entry->PhysicalAddress,
|
|
|
+ p_addr_entry->PhysicalAddressLength);
|
|
|
break; /* leave search after iface_idx match */
|
|
|
}
|
|
|
p_addr_entry = p_addr_entry->Next;
|
|
|
@@ -298,11 +311,10 @@ EipStatus IfaceGetMacAddress(const char *iface, uint8_t *physical_address) {
|
|
|
|
|
|
/* For Doxygen descriptions see opener_api.h. */
|
|
|
EipStatus IfaceGetConfiguration(const char *iface,
|
|
|
- CipTcpIpInterfaceConfiguration *iface_cfg)
|
|
|
-{
|
|
|
- ULONG iface_idx;
|
|
|
+ CipTcpIpInterfaceConfiguration *iface_cfg) {
|
|
|
+ ULONG iface_idx;
|
|
|
|
|
|
- if(kEipStatusOk != DetermineIfaceIndexByString(iface, &iface_idx)) {
|
|
|
+ if(kEipStatusOk != DetermineIfaceIndexByString(iface, &iface_idx) ) {
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
|
|
|
@@ -310,7 +322,7 @@ EipStatus IfaceGetConfiguration(const char *iface,
|
|
|
const ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
|
|
|
GAA_FLAG_INCLUDE_GATEWAYS | GAA_FLAG_INCLUDE_PREFIX;
|
|
|
PIP_ADAPTER_ADDRESSES p_addr_table = NULL;
|
|
|
- if (kEipStatusOk != RetrieveAdapterAddressesTable(flags, &p_addr_table)) {
|
|
|
+ if (kEipStatusOk != RetrieveAdapterAddressesTable(flags, &p_addr_table) ) {
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
|
|
|
@@ -323,13 +335,15 @@ EipStatus IfaceGetConfiguration(const char *iface,
|
|
|
if (iface_idx == p_addr_entry->IfIndex) {
|
|
|
|
|
|
if (IfOperStatusUp != p_addr_entry->OperStatus) {
|
|
|
- OPENER_TRACE_ERR("IfaceGetConfiguration(): Interface '%s' is not up.\n", iface);
|
|
|
+ OPENER_TRACE_ERR("IfaceGetConfiguration(): Interface '%s' is not up.\n",
|
|
|
+ iface);
|
|
|
FREE(p_addr_table);
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
/* Extract ip_addr, netmask, gateway, nameserver, nameserver 2, domain ... */
|
|
|
{
|
|
|
- PIP_ADAPTER_UNICAST_ADDRESS pUnicast = p_addr_entry->FirstUnicastAddress;
|
|
|
+ PIP_ADAPTER_UNICAST_ADDRESS pUnicast =
|
|
|
+ p_addr_entry->FirstUnicastAddress;
|
|
|
if (NULL != pUnicast) {
|
|
|
local_cfg.ip_address = GetIpFromSocketAddress(&pUnicast->Address);
|
|
|
}
|
|
|
@@ -337,22 +351,26 @@ EipStatus IfaceGetConfiguration(const char *iface,
|
|
|
{
|
|
|
PIP_ADAPTER_PREFIX pPrefix = p_addr_entry->FirstPrefix;
|
|
|
if (NULL != pPrefix) {
|
|
|
- local_cfg.network_mask = htonl(0xffffffff << (32U - pPrefix->PrefixLength));
|
|
|
+ local_cfg.network_mask =
|
|
|
+ htonl(0xffffffff << (32U - pPrefix->PrefixLength) );
|
|
|
}
|
|
|
}
|
|
|
{
|
|
|
- PIP_ADAPTER_GATEWAY_ADDRESS pGateway = p_addr_entry->FirstGatewayAddress;
|
|
|
+ PIP_ADAPTER_GATEWAY_ADDRESS pGateway =
|
|
|
+ p_addr_entry->FirstGatewayAddress;
|
|
|
if (NULL != pGateway) {
|
|
|
local_cfg.gateway = GetIpFromSocketAddress(&pGateway->Address);
|
|
|
}
|
|
|
}
|
|
|
{
|
|
|
- IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = p_addr_entry->FirstDnsServerAddress;
|
|
|
+ IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer =
|
|
|
+ p_addr_entry->FirstDnsServerAddress;
|
|
|
if (NULL != pDnServer) {
|
|
|
local_cfg.name_server = GetIpFromSocketAddress(&pDnServer->Address);
|
|
|
pDnServer = pDnServer->Next;
|
|
|
if (NULL != pDnServer) {
|
|
|
- local_cfg.name_server_2= GetIpFromSocketAddress(&pDnServer->Address);
|
|
|
+ local_cfg.name_server_2 =
|
|
|
+ GetIpFromSocketAddress(&pDnServer->Address);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -384,21 +402,23 @@ EipStatus IfaceGetConfiguration(const char *iface,
|
|
|
}
|
|
|
|
|
|
/* For Doxygen descriptions see opener_api.h. */
|
|
|
-EipStatus IfaceWaitForIp(const char *iface, int timeout, volatile int *abort_wait) {
|
|
|
- ULONG iface_idx;
|
|
|
+EipStatus IfaceWaitForIp(const char *iface,
|
|
|
+ int timeout,
|
|
|
+ volatile int *abort_wait) {
|
|
|
+ ULONG iface_idx;
|
|
|
|
|
|
- if(kEipStatusOk != DetermineIfaceIndexByString(iface, &iface_idx)) {
|
|
|
+ if(kEipStatusOk != DetermineIfaceIndexByString(iface, &iface_idx) ) {
|
|
|
return kEipStatusError;
|
|
|
}
|
|
|
|
|
|
{
|
|
|
- PMIB_IPADDRTABLE pmib_ipaddr_table = NULL;
|
|
|
+ PMIB_IPADDRTABLE pmib_ipaddr_table = NULL;
|
|
|
ULONG addr_table_sz = 0;
|
|
|
uint32_t ipaddr;
|
|
|
|
|
|
#define WAIT_CYCLE_MS 100
|
|
|
/* Calculate cycles of SleepEx(WAIT_CYCLE_MS) needed. */
|
|
|
- timeout *= (1000/WAIT_CYCLE_MS);
|
|
|
+ timeout *= (1000 / WAIT_CYCLE_MS);
|
|
|
do {
|
|
|
DWORD dw_ret;
|
|
|
ipaddr = 0U;
|
|
|
@@ -426,10 +446,11 @@ EipStatus IfaceWaitForIp(const char *iface, int timeout, volatile int *abort_wai
|
|
|
}
|
|
|
|
|
|
/* Search entry matching the interface index and determine IP address. */
|
|
|
- for (int i=0; i < (int) pmib_ipaddr_table->dwNumEntries; i++) {
|
|
|
+ for (int i = 0; i < (int) pmib_ipaddr_table->dwNumEntries; i++) {
|
|
|
if (pmib_ipaddr_table->table[i].dwIndex == iface_idx) {
|
|
|
if (0 == (pmib_ipaddr_table->table[i].wType &
|
|
|
- (MIB_IPADDR_DELETED | MIB_IPADDR_DISCONNECTED | MIB_IPADDR_TRANSIENT))) {
|
|
|
+ (MIB_IPADDR_DELETED | MIB_IPADDR_DISCONNECTED |
|
|
|
+ MIB_IPADDR_TRANSIENT) ) ) {
|
|
|
ipaddr = pmib_ipaddr_table->table[i].dwAddr;
|
|
|
}
|
|
|
}
|
|
|
@@ -438,10 +459,12 @@ EipStatus IfaceWaitForIp(const char *iface, int timeout, volatile int *abort_wai
|
|
|
if (timeout > 0) {
|
|
|
--timeout;
|
|
|
}
|
|
|
- } while ((0 == ipaddr) && (0 != timeout) && (0 == *abort_wait) &&
|
|
|
- (0 == SleepEx(WAIT_CYCLE_MS, FALSE)));
|
|
|
+ } while ( (0 == ipaddr) && (0 != timeout) && (0 == *abort_wait) &&
|
|
|
+ (0 == SleepEx(WAIT_CYCLE_MS, FALSE) ) );
|
|
|
|
|
|
- OPENER_TRACE_INFO("IP=%08" PRIx32 ", timeout=%d\n", (uint32_t)ntohl(ipaddr), timeout);
|
|
|
+ OPENER_TRACE_INFO("IP=%08" PRIx32 ", timeout=%d\n",
|
|
|
+ (uint32_t)ntohl(ipaddr),
|
|
|
+ timeout);
|
|
|
if (pmib_ipaddr_table) {
|
|
|
FREE(pmib_ipaddr_table);
|
|
|
}
|
|
|
@@ -450,8 +473,7 @@ EipStatus IfaceWaitForIp(const char *iface, int timeout, volatile int *abort_wai
|
|
|
}
|
|
|
|
|
|
#define HOST_NAME_MAX 256 /* Should be long enough according rfc1132. */
|
|
|
-void GetHostName(CipString *hostname)
|
|
|
-{
|
|
|
+void GetHostName(CipString *hostname) {
|
|
|
CipWord wVersionRequested;
|
|
|
WSADATA wsaData;
|
|
|
int err;
|
|
|
@@ -464,19 +486,19 @@ void GetHostName(CipString *hostname)
|
|
|
/* Tell the user that we could not find a usable Winsock DLL. */
|
|
|
char *error_message = GetErrorMessage(err);
|
|
|
printf("WSAStartup failed with error: %d - %s\n",
|
|
|
- err, error_message);
|
|
|
+ err, error_message);
|
|
|
FreeErrorMessage(error_message);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
char name_buf[HOST_NAME_MAX] = "";
|
|
|
- err = gethostname(name_buf, sizeof(name_buf));
|
|
|
+ err = gethostname(name_buf, sizeof(name_buf) );
|
|
|
if (0 != err) {
|
|
|
int error_code = GetSocketErrorNumber();
|
|
|
char *error_message = GetErrorMessage(error_code);
|
|
|
printf("gethostname() failed, %d - %s\n",
|
|
|
- error_code,
|
|
|
- error_message);
|
|
|
+ error_code,
|
|
|
+ error_message);
|
|
|
FreeErrorMessage(error_message);
|
|
|
return;
|
|
|
}
|