Results 1 to 10 of 21

Thread: My new sendCommand function with libcod (windows)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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

  3. #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
    If have been trying it for a while now, but I cant get it working. What I did.

    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
    Those errors are because they are only used by Windows.

    You should look at examples how to setup a UDP client on Linux.

    http://www.linuxhowtos.org/C_C++/socket.htm
    http://www.binarytides.com/programmi...ckets-c-linux/

    If you need more help try googling 'linux c udp socket'.

    Edit: there is also a simpler way to validate the IP address:
    http://stackoverflow.com/questions/7...p-address-in-c
    PHP Code:
    #include <arpa/inet.h>

    bool isValidIpAddress(char *ipAddress)
    {
        
    struct sockaddr_in sa;
        
    int result inet_pton(AF_INETipAddress, &(sa.sin_addr));
        return 
    result != 0;

    Last edited by Mitch; 9th January 2016 at 15:35.

  4. The Following User Says Thank You to Mitch For This Useful Post:

    Ni3ls (9th January 2016)

Posting Permissions

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