2014-05-07 12:13:24 +02:00
/*@!Encoding:1252*/
includes
{
#include "Common.cin"
}
variables
{
const long WSA_IO_PENDING = 997;
const long WSAEWOULDBLOCK = 10035;
const dword INVALID_IP = 0xffffffff;
long gIpLastErr = 0;
char gIpLastErrStr[512] = "";
TcpSocket g_%NODE_NAME%_Socket;
2014-05-08 15:34:28 +02:00
dword g_%NODE_NAME%_RemoteIP = INVALID_IP;
word g_%NODE_NAME%_RemotePort = 0;
2014-05-07 12:13:24 +02:00
enum TcpState { NULL, OK, ERROR, CLOSED };
enum TcpState g_%NODE_NAME%_TcpState = NULL;
byte gTcpRxBuffer[8192];
}
2014-05-08 10:26:26 +02:00
dword TcpSetupIp(char Local_IP[])
2014-05-07 12:13:24 +02:00
{
2014-05-08 10:26:26 +02:00
int adapterIndex;
const int size = 512;
char text[size] = "";
dword addresses[1];
dword address;
word adapterCount;
word i;
adapterCount = IpGetAdapterCount();
adapterIndex = @sysvar::TCPIP::AdapterIndex;
2014-05-07 12:13:24 +02:00
2014-05-08 10:26:26 +02:00
switch (adapterCount)
2014-05-07 12:13:24 +02:00
{
2014-05-08 10:26:26 +02:00
case 0:
2014-05-08 15:34:28 +02:00
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: There is no network interface available!");
2014-05-08 10:26:26 +02:00
stop();
return INVALID_IP;
break;
case 1:
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Info: There is 1 network interface available!");
2014-05-08 10:26:26 +02:00
if (adapterIndex != 1)
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: You have not selected the first adapter!");
2014-05-08 10:26:26 +02:00
stop();
return INVALID_IP;
}
break;
default:
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Info: There are %d network interfaces available!", adapterCount);
2014-05-08 10:26:26 +02:00
// // // // TEST \\ \\ \\ \\
for (i = 1; i <= adapterCount; i++)
{
IpGetAdapterDescription(i, text, size);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Info: Interface %d: %s", i, text);
2014-05-08 10:26:26 +02:00
}
break;
2014-05-07 12:13:24 +02:00
}
if (IpGetAdapterAddress(adapterIndex, addresses, 1) != 0)
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve IP address!");
2014-05-07 12:13:24 +02:00
stop();
2014-05-08 10:26:26 +02:00
return INVALID_IP;
2014-05-07 12:13:24 +02:00
}
address = addresses[0]; // the interface used
if (address == INVALID_IP)
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 3, "<%BASE_FILE_NAME%> Error: IP address to be used is invalid!");
2014-05-07 12:13:24 +02:00
stop();
2014-05-08 10:26:26 +02:00
return INVALID_IP;
2014-05-07 12:13:24 +02:00
}
IpGetAdapterDescription(adapterIndex, text, size);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Interface: %s", text);
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Wrong interface? Change sysvar::TCPIP::AdapterIndex");
2014-05-07 12:13:24 +02:00
IpGetAdapterAddressAsString(adapterIndex, text, size);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Ip address: %s", text);
2014-05-07 12:13:24 +02:00
strncpy(Local_IP, text, 16);
IpGetAdapterMaskAsString(adapterIndex, text, size);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Subnet mask: %s", text);
2014-05-07 12:13:24 +02:00
IpGetAdapterGatewayAsString(adapterIndex, text, size);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Gateway address: %s", text);
2014-05-07 12:13:24 +02:00
return address;
}
2014-05-08 10:26:26 +02:00
word TcpOpenSocket()
2014-05-07 12:13:24 +02:00
{
char Local_IP[16];
dword localIp;
word localPort;
dword i = 0;
CHAR errorText[200];
2014-05-08 10:26:26 +02:00
localIp = TcpSetupIp(Local_IP);
2014-05-07 12:13:24 +02:00
localPort = random(65535-10240)+10240;
2014-05-08 10:26:26 +02:00
if (localIp == INVALID_IP)
return INVALID_IP;
2014-05-07 12:13:24 +02:00
// Try to open socket
do
{
g_%NODE_NAME%_Socket = TcpSocket::Open(localIp, localPort);
if (g_%NODE_NAME%_Socket.GetLastSocketError() != 0)
{
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(errorText, elcount(errorText));
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Error: could not open Tcp socket on %s:%d, %s (%d)!", Local_IP, localPort, errorText, g_%NODE_NAME%_Socket.GetLastSocketError());
2014-05-07 12:13:24 +02:00
}
}
while (g_%NODE_NAME%_Socket.GetLastSocketError() != 0 && i++ < 9);
if (g_%NODE_NAME%_Socket.GetLastSocketError() != 0)
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Error: could not open Tcp socket!");
2014-05-07 12:13:24 +02:00
return g_%NODE_NAME%_Socket.GetLastSocketError();
}
else
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Tcp socket opened on %s:%d.", Local_IP, localPort);
2014-05-07 12:13:24 +02:00
}
return 0;
}
word TcpConnectTo(char Remote_IP[], word remotePort)
{
dword remoteIp;
2014-05-08 15:34:28 +02:00
// Convert IP string to Number
remoteIp = IpGetAddressAsNumber(Remote_IP);
if (remoteIp == INVALID_IP)
{
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Error: invalid server Ip address!");
return 1;
}
return TcpConnectTo(remoteIp, remotePort);
}
word TcpConnectTo(dword remoteIp, word remotePort)
{
2014-05-07 12:13:24 +02:00
long fehler;
// Try to open a socket
2014-05-08 10:26:26 +02:00
fehler = TcpOpenSocket();
2014-05-07 12:13:24 +02:00
if (fehler != 0)
{
2014-05-08 15:34:28 +02:00
g_%NODE_NAME%_TcpState = ERROR;
2014-05-07 12:13:24 +02:00
return fehler;
}
2014-05-08 15:34:28 +02:00
g_%NODE_NAME%_RemoteIP = remoteIp;
g_%NODE_NAME%_RemotePort = remotePort;
2014-05-07 12:13:24 +02:00
// Connect to Server
if (g_%NODE_NAME%_Socket.Connect(remoteIp, remotePort) != 0)
{
fehler = g_%NODE_NAME%_Socket.GetLastSocketError();
if (fehler != WSAEWOULDBLOCK) // OnTcpConnect will be called otherwise
{
write("<%BASE_FILE_NAME%> No Port-Connection: %d", fehler);
g_%NODE_NAME%_TcpState = ERROR;
return fehler;
}
return 0;
}
else
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Successfully connected to server");
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_TcpState = OK;
return 0;
}
}
void OnTcpConnect(dword socket, long result)
{
if (result != 0)
{
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "<%BASE_FILE_NAME%> OnTcpConnect error (%d): %s", g_%NODE_NAME%_Socket.GetLastSocketError(), gIpLastErrStr);
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_TcpState = ERROR;
return;
}
else
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> Successfully connected to server");
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_TcpState = OK;
}
}
void TcpRecv()
{
int result;
if (g_%NODE_NAME%_TcpState != OK)
{
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "TcpRecv: Socket status is not OK!");
2014-05-07 12:13:24 +02:00
return;
}
result = g_%NODE_NAME%_Socket.Receive(gTcpRxBuffer, elcount(gTcpRxBuffer));
if (result != 0) // Calling OnTcpReceive otherwise
{
gIpLastErr = g_%NODE_NAME%_Socket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING) // Calling OnTcpReceive otherwise
{
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "<%BASE_FILE_NAME%> TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_Socket.Close();
g_%NODE_NAME%_TcpState = CLOSED;
}
}
return;
}
2014-05-08 15:34:28 +02:00
/*
2014-05-07 12:13:24 +02:00
void OnTcpReceive(dword socket, long result, dword address, dword port, char buffer[], dword size)
{
char str[20*3];
if (result == 0)
{
if (size == 0)
{
// Size of zero indicates that the socket was closed by the communication peer.
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "<%BASE_FILE_NAME%> OnTcpReceive: Socket closed by peer");
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_Socket.Close();
g_%NODE_NAME%_TcpState = CLOSED;
}
else
{
// Sucessfully received some bytes over the TCP/IP connection.
bin_to_strhex(gTcpRxBuffer, str);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> OnTcpReceive: %s", str);
2014-05-07 12:13:24 +02:00
}
}
else
{
gIpLastErr = g_%NODE_NAME%_Socket.GetLastSocketError();
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "<%BASE_FILE_NAME%> OnTcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_Socket.Close();
g_%NODE_NAME%_TcpState = CLOSED;
}
}
2014-05-08 15:34:28 +02:00
*/
2014-05-07 12:13:24 +02:00
void TcpSnd(byte buffer[])
{
char str[20*3];
2014-05-08 10:26:26 +02:00
switch (g_%NODE_NAME%_TcpState)
2014-05-07 12:13:24 +02:00
{
2014-05-08 10:26:26 +02:00
case CLOSED:
2014-05-08 15:34:28 +02:00
TcpConnectTo(g_%NODE_NAME%_RemoteIP, g_%NODE_NAME%_RemotePort);
if (g_%NODE_NAME%_TcpState != OK)
{
writeLineEx(0, 2, "TcpSnd: Reconnecting failed!");
return;
}
2014-05-08 10:26:26 +02:00
case OK:
break;
default:
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "TcpSnd: Socket status is not OK!");
2014-05-08 10:26:26 +02:00
return;
2014-05-07 12:13:24 +02:00
}
bin_to_strhex(buffer, str);
2014-05-08 15:34:28 +02:00
writeLineEx(0, 1, "<%BASE_FILE_NAME%> TcpSnd: %s (L<> nge: %d)", str, elCount(buffer));
2014-05-07 12:13:24 +02:00
if (g_%NODE_NAME%_Socket.Send(buffer, elCount(buffer)) != 0)
{
gIpLastErr = g_%NODE_NAME%_Socket.GetLastSocketError();
if (gIpLastErr != WSA_IO_PENDING)
{
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
2014-05-08 15:34:28 +02:00
writeLineEx(0, 2, "<%BASE_FILE_NAME%> TcpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
2014-05-07 12:13:24 +02:00
g_%NODE_NAME%_Socket.Close();
g_%NODE_NAME%_TcpState = CLOSED;
}
}
}