B
Brad Walton
I'm stuck here... I have a sample script which accomplishes the task I need
done, but it's done in another programing language (of which I have no
understanding). I have researched this for hours and hours, but no luck.
The script I need 'deciphered' to Perl:
----
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <netinet/tcp.h>
int main(int argc, char *argv[] )
{
int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( sock == -1 )
{
printf( "Socket creation failed" );
exit(-1);
}
struct sockaddr_in rcon_server;
rcon_server.sin_family = AF_INET;
rcon_server.sin_addr.s_addr = inet_addr( "127.0.0.1" );
rcon_server.sin_port = htons( 27015 );
if ( connect( sock, (const struct sockaddr *)&rcon_server,sizeof(
rcon_server ) )!= 0 )
{
printf( "Unable to connect\n");
exit(-1);
}
unsigned char send_buf[4096];
unsigned char *send_ptr = send_buf + sizeof(int);
*(int *)send_ptr = 0x0001; // request id 1
send_ptr += sizeof(int);
*(int *)send_ptr = 0x0003; // command id 3
send_ptr += sizeof(int);
strcpy( (char *)send_ptr, "password" ); // the rcon password
send_ptr += strlen("password") +1; //+1 for null terminator
*(int *)send_ptr = 0; // 2nd string is just null
send_ptr++;
(*(int *)send_buf) = (int)(send_ptr - send_buf - sizeof(int)); //
now setup the size of this packet (NOTE the subtraction of sizeof(int)!!)
printf( "Sending packet (%d bytes)\n", *(int *)(send_buf) );
send( sock, send_buf, *(int *)(send_buf) + sizeof(int), 0 ); // send
the auth request
sleep(1); // ugly hack to give the server time to respond
long readLen;
ioctl( sock, FIONREAD, &readLen );
printf("Got %d bytes from server\n", readLen );
int len = recv( sock, send_buf, readLen, 0);
if ( len < 14 )
{
printf("Didn't read enough data (%i)( TODO: block on
reads)\n", len);
exit(-1);
}
printf( "packet size: %d, request id:%d, command:%d\n",
(int)send_buf[0],(int)send_buf[4],(int)send_buf[8]);
if ( len > 14 ) // a 2nd packet is in the response
{
printf( "packet size: %d, request id:%d, command:%d\n",
(int)send_buf[14],(int)send_buf[18],(int)send_buf[22]);
}
close( sock );
}
----
This is a longshot.. but I don't know what else to do.
Thanks for any help,
Brad
done, but it's done in another programing language (of which I have no
understanding). I have researched this for hours and hours, but no luck.
The script I need 'deciphered' to Perl:
----
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <netinet/tcp.h>
int main(int argc, char *argv[] )
{
int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( sock == -1 )
{
printf( "Socket creation failed" );
exit(-1);
}
struct sockaddr_in rcon_server;
rcon_server.sin_family = AF_INET;
rcon_server.sin_addr.s_addr = inet_addr( "127.0.0.1" );
rcon_server.sin_port = htons( 27015 );
if ( connect( sock, (const struct sockaddr *)&rcon_server,sizeof(
rcon_server ) )!= 0 )
{
printf( "Unable to connect\n");
exit(-1);
}
unsigned char send_buf[4096];
unsigned char *send_ptr = send_buf + sizeof(int);
*(int *)send_ptr = 0x0001; // request id 1
send_ptr += sizeof(int);
*(int *)send_ptr = 0x0003; // command id 3
send_ptr += sizeof(int);
strcpy( (char *)send_ptr, "password" ); // the rcon password
send_ptr += strlen("password") +1; //+1 for null terminator
*(int *)send_ptr = 0; // 2nd string is just null
send_ptr++;
(*(int *)send_buf) = (int)(send_ptr - send_buf - sizeof(int)); //
now setup the size of this packet (NOTE the subtraction of sizeof(int)!!)
printf( "Sending packet (%d bytes)\n", *(int *)(send_buf) );
send( sock, send_buf, *(int *)(send_buf) + sizeof(int), 0 ); // send
the auth request
sleep(1); // ugly hack to give the server time to respond
long readLen;
ioctl( sock, FIONREAD, &readLen );
printf("Got %d bytes from server\n", readLen );
int len = recv( sock, send_buf, readLen, 0);
if ( len < 14 )
{
printf("Didn't read enough data (%i)( TODO: block on
reads)\n", len);
exit(-1);
}
printf( "packet size: %d, request id:%d, command:%d\n",
(int)send_buf[0],(int)send_buf[4],(int)send_buf[8]);
if ( len > 14 ) // a 2nd packet is in the response
{
printf( "packet size: %d, request id:%d, command:%d\n",
(int)send_buf[14],(int)send_buf[18],(int)send_buf[22]);
}
close( sock );
}
----
This is a longshot.. but I don't know what else to do.
Thanks for any help,
Brad