Bachelorthesis/Modbus/include/TcpUdpCommon.cin

100 lines
2.7 KiB
Text

/*@!Encoding:1252*/
variables
{
const long WSA_IO_PENDING = 997;
const long WSAEWOULDBLOCK = 10035;
const dword INVALID_IP = 0xffffffff;
long gIpLastErr = 0;
char gIpLastErrStr[512] = "";
enum SocketState { NULL, OK, ERROR, CLOSED };
enum SocketState gSocketState = NULL;
dword gRemoteIP = INVALID_IP;
word gRemotePort = 0;
}
dword SetupIp(char Local_IP[])
{
int adapterIndex;
const int size = 512;
char text[size] = "";
dword addresses[8];
dword address;
word adapterCount;
word i;
long error;
adapterCount = IpGetAdapterCount();
adapterIndex = @sysvar::Config::TcpIp::AdapterIndex;
switch (adapterCount)
{
case 0:
writeLineEx(0, 3, "<%NODE_NAME%> Error: There is no network interface available!");
stop();
return INVALID_IP;
break;
case 1:
writeLineEx(0, 1, "<%NODE_NAME%> Info: There is 1 network interface available!");
if (adapterIndex != 1)
{
writeLineEx(0, 3, "<%NODE_NAME%> Error: You have not selected the first adapter!");
stop();
return INVALID_IP;
}
break;
default:
writeLineEx(0, 1, "<%NODE_NAME%> Info: There are %d network interfaces available!", adapterCount);
// // // // TEST \\ \\ \\ \\
for (i = 1; i <= adapterCount; i++)
{
IpGetAdapterDescription(i, text, size);
writeLineEx(0, 1, "<%NODE_NAME%> Info: Interface %d: %s", i, text);
}
break;
}
error = IpGetAdapterAddress(adapterIndex, addresses, elCount(addresses));
if (error == 8)
{
writeLineEx(0, 3, "<%NODE_NAME%> Error 8: Could not retrieve IP address from interface #%d: The address array was insufficient", adapterIndex);
stop();
return INVALID_IP;
}
else if (error == 87)
{
writeLineEx(0, 3, "<%NODE_NAME%> Error 87: Could not retrieve IP address from interface #%d: The specified interface index was invalid", adapterIndex);
stop();
return INVALID_IP;
}
address = addresses[0]; // the interface used
if (address == INVALID_IP)
{
writeLineEx(0, 3, "<%NODE_NAME%> Error: IP address to be used is invalid!");
stop();
return INVALID_IP;
}
IpGetAdapterDescription(adapterIndex, text, size);
writeLineEx(0, 1, "<%NODE_NAME%> Interface: %s", text);
writeLineEx(0, 1, "<%NODE_NAME%> Wrong interface? Change sysvar::TCPIP::AdapterIndex");
IpGetAdapterAddressAsString(adapterIndex, text, size);
writeLineEx(0, 1, "<%NODE_NAME%> Ip address: %s", text);
strncpy(Local_IP, text, 16);
IpGetAdapterMaskAsString(adapterIndex, text, size);
writeLineEx(0, 1, "<%NODE_NAME%> Subnet mask: %s", text);
IpGetAdapterGatewayAsString(adapterIndex, text, size);
writeLineEx(0, 1, "<%NODE_NAME%> Gateway address: %s", text);
return address;
}