Bachelorthesis/Modbus/TcpUdpCommon.cin

93 lines
2.5 KiB
Text
Raw Normal View History

/*@!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 g_%NODE_NAME%_SocketState = NULL;
dword g_%NODE_NAME%_RemoteIP = INVALID_IP;
word g_%NODE_NAME%_RemotePort = 0;
}
dword SetupIp(char Local_IP[])
{
int adapterIndex;
const int size = 512;
char text[size] = "";
dword addresses[1];
dword address;
word adapterCount;
word i;
adapterCount = IpGetAdapterCount();
adapterIndex = @sysvar::TCPIP::AdapterIndex;
switch (adapterCount)
{
case 0:
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: There is no network interface available!");
stop();
return INVALID_IP;
break;
case 1:
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Info: There is 1 network interface available!");
if (adapterIndex != 1)
{
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: You have not selected the first adapter!");
stop();
return INVALID_IP;
}
break;
default:
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Info: There are %d network interfaces available!", adapterCount);
// // // // TEST \\ \\ \\ \\
for (i = 1; i <= adapterCount; i++)
{
IpGetAdapterDescription(i, text, size);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Info: Interface %d: %s", i, text);
}
break;
}
if (IpGetAdapterAddress(adapterIndex, addresses, 1) != 0)
{
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve IP address!");
stop();
return INVALID_IP;
}
address = addresses[0]; // the interface used
if (address == INVALID_IP)
{
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: IP address to be used is invalid!");
stop();
return INVALID_IP;
}
IpGetAdapterDescription(adapterIndex, text, size);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Interface: %s", text);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Wrong interface? Change sysvar::TCPIP::AdapterIndex");
IpGetAdapterAddressAsString(adapterIndex, text, size);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Ip address: %s", text);
strncpy(Local_IP, text, 16);
IpGetAdapterMaskAsString(adapterIndex, text, size);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Subnet mask: %s", text);
IpGetAdapterGatewayAsString(adapterIndex, text, size);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Gateway address: %s", text);
return address;
}