1 Attachment(s)
My new sendCommand function with libcod (windows)
Hey,
So with this function you can send data to another server (tested with cod2 and cod4 servers) (like rcon commands) or you can get data from it. If you want to use it you have to add my source to Mitch's libcod_win project (https://github.com/M-itch/libcod_win).
Changes:
[gsc.cpp] Add the following code to the scriptFunctions array.
PHP Code:
{"sendCommand" , gsc_utils_sendCommand , 0},
[gsc_utils.h]
PHP Code:
void gsc_utils_sendCommand();
[gsc_utils.cpp]
PHP Code:
#include <math.h>
#include <winsock2.h>
#include <windows.h>
BOOL isValidNumber(const char *charnum, int **number)
{
int i, num = 0;
if(charnum == NULL)
{
return 0;
}
for(i = 0; i < strlen(charnum); i++)
{
if(charnum[i] >= '0' && charnum[i] <= '9')
{
num += pow(10.0, (int)(strlen(charnum) - i - 1)) * (charnum[i] - '0');
}
else
{
return 0;
}
}
*number = (int *)num;
return 1;
}
BOOL isValidRange(const char *range)
{
int *num;
if(!isValidNumber(range, &num))
{
return 0;
}
if((int)num >= 0 && (int)num <= 255)
{
return 1;
}
return 0;
}
int isValidAddress(const char *ip)
{
int i, dots = 0;
char *token;
for(i = 0; i < strlen(ip); i++)
{
if(ip[i] == '.')
dots++;
}
if(dots != 3)
{
return 0;
}
for(i = 0; i < dots + 1; i++)
{
if(i == 0)
{
token = strtok(strdup(ip), ".");
}
else
{
token = strtok(NULL, ".");
}
if(!isValidRange(token))
{
return 0;
}
}
return 1;
}
char* sendCommand(char *ip, int port, char *message)
{
SOCKET Socket;
WSADATA WSAData = {0};
SOCKADDR_IN server;
struct timeval timeout;
int i;
char *data;
char *fixed;
char response[16384];
data = strcat(strdup("00000"), message);
for(i = 0; i < 4; i++)
{
data[i] = 0xFF;
}
data[i] = 0x02;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
timeout.tv_sec = 2;
timeout.tv_usec = 2;
if(WSAStartup (MAKEWORD(2, 2), &WSAData))
{
return "0";
}
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(Socket == INVALID_SOCKET)
{
WSACleanup();
return "0";
}
if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
closesocket(Socket);
WSACleanup();
return "0";
}
if(setsockopt(Socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
closesocket(Socket);
WSACleanup();
return "0";
}
if(connect(Socket, (SOCKADDR *)&server, sizeof(server)) == SOCKET_ERROR)
{
closesocket(Socket);
WSACleanup();
return "0";
}
sendto(Socket, data, strlen(data), 0, (SOCKADDR *)&server, sizeof(server));
i = recvfrom(Socket, response, sizeof(response), 0, 0, 0);
if(i == SOCKET_ERROR)
{
closesocket(Socket);
WSACleanup();
return "0";
}
response[i] = '\0';
closesocket(Socket);
WSACleanup();
fixed = strtok(response, "\\");
fixed = strtok(NULL, "\0");
for(i = 0; i < strlen(fixed); i++)
{
if(fixed[i] == '\n')
{
fixed[i] = '\t';
}
if(fixed[i] == '"')
{
fixed[i] = '\t';
}
if(fixed[i] == '\\')
{
fixed[i] = '\t';
}
}
return fixed;
}
void gsc_utils_sendCommand()
{
char *ip;
char *data;
int port = 0;
if(!stackGetParams((char *)"sis", &ip, &port, &data) || strlen(ip) == 0 || port == 0 || strlen(data) == 0)
{
stackPushUndefined();
return;
}
if(isValidAddress(ip))
{
stackPushString(sendCommand(ip, port, data));
}
else
{
stackPushInt(0);
}
}
Add the Ws2_32.lib to the project and compile it!
How to use:
I created an ingame server tracker, so I need the another server's parameters.
PHP Code:
resp = sendCommand("148.251.81.10", 28844, "getstatus");
array = StrTok(resp, "\t");
The datas should store as a clientcvar and set inside a menu's itemdef to the dvar attribute, and here is the result:
Attachment 820
Or with this you can create an ingame rcon tool, and can connect to another server via it.
PHP Code:
sendCommand("148.251.81.10", 28844, "rcon <password> map mp_toujane");