Results 1 to 10 of 21

Thread: My new sendCommand function with libcod (windows)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Private
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    6
    Thanked 5 Times in 1 Post

    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 *charnumint **number)
    {
      
    int inum 0;

      if(
    charnum == NULL)
      {
        return 
    0;
      }

      for(
    0strlen(charnum); i++)
      {
        if(
    charnum[i] >= '0' && charnum[i] <= '9')
        {
          
    num += pow(10.0, (int)(strlen(charnum) - 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 >= && (int)num <= 255)
      {
        return 
    1;
      }

      return 
    0;
    }

    int isValidAddress(const char *ip)
    {
      
    int idots 0;
      
    char *token;

      for(
    0strlen(ip); i++)
      {
        if(
    ip[i] == '.')
          
    dots++;
      }

      if(
    dots != 3)
      {
        return 
    0;
      }

      for(
    0dots 1i++)
      {
        if(
    == 0)
        {
          
    token strtok(strdup(ip), ".");
        }
        else
        {
          
    token strtok(NULL".");
        }
        if(!
    isValidRange(token))
        {
          return 
    0;
        }
      }

      return 
    1;
    }

    charsendCommand(char *ipint portchar *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(
    04i++)
      {
        
    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(22), &WSAData))
      {
        return 
    "0";
      }

      
    Socket socket(AF_INETSOCK_DGRAMIPPROTO_UDP);
      if(
    Socket == INVALID_SOCKET)
      {
        
    WSACleanup();
        return 
    "0";        
      }

      if(
    setsockopt(SocketSOL_SOCKETSO_RCVTIMEO, (char *)&timeoutsizeof(timeout)) < 0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      if(
    setsockopt(SocketSOL_SOCKETSO_SNDTIMEO, (char *)&timeoutsizeof(timeout)) < 0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      if(
    connect(Socket, (SOCKADDR *)&serversizeof(server)) == SOCKET_ERROR)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      
    sendto(Socketdatastrlen(data), 0, (SOCKADDR *)&serversizeof(server));
      
    recvfrom(Socketresponsesizeof(response), 000);

      if(
    == SOCKET_ERROR)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      
    response[i] = '\0';

      
    closesocket(Socket);
      
    WSACleanup();

      
    fixed strtok(response"\\");
      
    fixed strtok(NULL"\0");

      for(
    0strlen(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) == || port == || strlen(data) == 0)
      {
        
    stackPushUndefined();
        return;
      }

      if(
    isValidAddress(ip))
      {
        
    stackPushString(sendCommand(ipportdata));
      }
      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:

    Click image for larger version. 

Name:	Screenshot 2015-01-10 00.41.43.png 
Views:	191 
Size:	1.66 MB 
ID:	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"); 

  2. The Following 5 Users Say Thank You to Kantin For This Useful Post:

    kung foo man (10th January 2015),Mitch (10th January 2015),Ni3ls (10th January 2015),voron00 (9th January 2016),YuriJurek (10th January 2015)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •