Added network interface enumeration

This commit is contained in:
Jonny007-MKD 2014-05-08 08:26:26 +00:00
parent 340c950984
commit 584a787814

View file

@ -22,63 +22,80 @@ variables
} }
dword SetupIp(char Local_IP[]) dword TcpSetupIp(char Local_IP[])
{ {
int adapterIndex; int adapterIndex;
char text[512] = ""; const int size = 512;
char info[512] = ""; char text[size] = "";
int size = 512; dword addresses[1];
dword addresses[1]; dword address;
dword address; word adapterCount;
word i;
writeClear(0);
if (IpGetAdapterCount() < 1)
{
writelineex(0, 3, "<%BASE_FILE_NAME%> Error: There is no network interface available!");
stop();
}
adapterCount = IpGetAdapterCount();
adapterIndex = @sysvar::TCPIP::AdapterIndex; 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) if (IpGetAdapterAddress(adapterIndex, addresses, 1) != 0)
{ {
writelineex(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve ip address!"); writelineex(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve IP address!");
stop(); stop();
return INVALID_IP;
} }
address = addresses[0]; // the interface used address = addresses[0]; // the interface used
if (address == INVALID_IP) if (address == INVALID_IP)
{ {
writelineex(0, 3, "<%BASE_FILE_NAME%> Error: ip address to be used is invalid!"); writelineex(0, 3, "<%BASE_FILE_NAME%> Error: IP address to be used is invalid!");
stop(); stop();
return INVALID_IP;
} }
IpGetAdapterDescription(adapterIndex, text, size); IpGetAdapterDescription(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Interface: %s", text); writelineex(0, 1, "<%BASE_FILE_NAME%> Interface: %s", text);
writelineex(0, 1, info); writelineex(0, 1, "<%BASE_FILE_NAME%> Wrong interface? Change sysvar::TCPIP::AdapterIndex");
IpGetAdapterAddressAsString(adapterIndex, text, size); IpGetAdapterAddressAsString(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Ip address: %s", text); writelineex(0, 1, "<%BASE_FILE_NAME%> Ip address: %s", text);
strncpy(Local_IP, text, 16); strncpy(Local_IP, text, 16);
writelineex(0, 1, info);
IpGetAdapterMaskAsString(adapterIndex, text, size); IpGetAdapterMaskAsString(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Subnet mask: %s", text); writelineex(0, 1, "<%BASE_FILE_NAME%> Subnet mask: %s", text);
writelineex(0, 1, info);
IpGetAdapterGatewayAsString(adapterIndex, text, size); IpGetAdapterGatewayAsString(adapterIndex, text, size);
snprintf(info, size, "<%BASE_FILE_NAME%> Gateway address: %s", text); writelineex(0, 1, "<%BASE_FILE_NAME%> Gateway address: %s", text);
writelineex(0, 1, info);
return address; return address;
} }
word OpenSocket() word TcpOpenSocket()
{ {
char Local_IP[16]; char Local_IP[16];
dword localIp; dword localIp;
@ -86,9 +103,12 @@ word OpenSocket()
dword i = 0; dword i = 0;
CHAR errorText[200]; CHAR errorText[200];
localIp = SetupIp(Local_IP); localIp = TcpSetupIp(Local_IP);
localPort = random(65535-10240)+10240; localPort = random(65535-10240)+10240;
if (localIp == INVALID_IP)
return INVALID_IP;
// Try to open socket // Try to open socket
do do
{ {
@ -120,7 +140,7 @@ word TcpConnectTo(char Remote_IP[], word remotePort)
long fehler; long fehler;
// Try to open a socket // Try to open a socket
fehler = OpenSocket(); fehler = TcpOpenSocket();
if (fehler != 0) if (fehler != 0)
{ {
return fehler; return fehler;
@ -236,10 +256,16 @@ void TcpSnd(byte buffer[])
{ {
char str[20*3]; char str[20*3];
if (g_%NODE_NAME%_TcpState != OK) switch (g_%NODE_NAME%_TcpState)
{ {
writelineex(0, 2, "TcpSnd: Socket status is not OK!"); case CLOSED:
return; TcpOpenSocket();
break;
case OK:
break;
default:
writelineex(0, 2, "TcpSnd: Socket status is not OK!");
return;
} }
bin_to_strhex(buffer, str); bin_to_strhex(buffer, str);