Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: My new sendCommand function with libcod (windows)

  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)

  3. #2
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    Is this also possible in libcod for Linux?

  4. #3
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 450 Times in 305 Posts
    Quote Originally Posted by Ni3ls View Post
    Is this also possible in libcod for Linux?
    Yes, but you need other headers.

    http://linux.die.net/man/2/socket

  5. #4
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Why dont you just store the stuff in a mysql database as it seems you control all the servers that need it...
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  6. #5
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    So to get this working in linux
    Code:
    {"sendCommand"             , gsc_utils_sendCommand                 , 0},
    to gsc.cpp

    Code:
    void gsc_utils_sendCommand();
    to gsc_utils.h

    Code:
    #include <math.h> 
    #include <sys/types.h>
    #include <sys/socket.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); 
      } 
    }
    to gsc_utils.cpp

    with these headers
    Code:
    #include <math.h> 
    #include <sys/types.h>
    #include <sys/socket.h>
    and then compile like the normal way?
    Last edited by Ni3ls; 2nd January 2016 at 10:54.

  7. #6
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    Hey I just asked you,
    And this is crazy.
    I still dont know it,
    So answer maybe?
    İCarly Rae Jepsen

  8. #7
    Private
    Join Date
    Feb 2013
    Posts
    27
    Thanks
    7
    Thanked 12 Times in 5 Posts
    It's very simple. Do a console app or whatever which send the udp package to the server and catch the answer. It's the easiest way to test and debug your solution. You can also track your messages with the glorious Wireshark software. The basic has already been written in the post above, it's not so hard. That's how do it yourself. And of course here is the helping hand http://bfy.tw/3YA8

    Hint:
    I did the same stuff with C# tool to communicate cod servers, and between the send and receive timestamp there is (small) delay, so it can lag your server I guess.

  9. #8
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    I suck with linux :P And Kantin converted it to a "cod2" function

  10. #9
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    But Which headers to use?

  11. #10
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    If have been trying it for a while now, but I cant get it working. What I did.
    Code:
    {"sendCommand"             , gsc_utils_sendCommand                 , 0},
    to gsc.cpp

    Cant do this
    Code:
    void gsc_utils_sendCommand();
    to gsc_utils.h because there is no utils.h

    Code:
    #include "gsc_utils.hpp"
    
    #if COMPILE_UTILS == 1
    
    #include <dirent.h> // dir stuff
    #include <assert.h>
    #include <ctype.h> // toupper
    #include <sys/types.h>// new
    #include <sys/socket.h> //new
    to the top of gsc_utils.cpp

    Code:
    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); 
      } 
    }
    to the end of gsc_utils.cpp, but before the last #endif

    It compiles with a lot of errors and I cant start a server
    Code:
    gsc_utils.cpp:1163: error: ‘BOOL’ does not name a type
    gsc_utils.cpp: In function ‘int isValidAddress(const char*)’:
    gsc_utils.cpp:1206: error: ‘isValidRange’ was not declared in this scope
    gsc_utils.cpp: In function ‘char* sendCommand(char*, int, char*)’:
    gsc_utils.cpp:1217: error: ‘SOCKET’ was not declared in this scope
    gsc_utils.cpp:1217: error: expected ‘;’ before ‘Socket’
    gsc_utils.cpp:1218: error: ‘WSADATA’ was not declared in this scope
    gsc_utils.cpp:1218: error: expected ‘;’ before ‘WSAData’
    gsc_utils.cpp:1219: error: ‘SOCKADDR_IN’ was not declared in this scope
    gsc_utils.cpp:1219: error: expected ‘;’ before ‘server’
    gsc_utils.cpp:1235: error: ‘server’ was not declared in this scope
    with a lot more "not declared" errors

Posting Permissions

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