Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: My new sendCommand function with libcod (windows)

  1. #11
    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.

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

    Ni3ls (9th January 2016)

  3. #12
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    PHP Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.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)
    {
        
    int Socket;
        
    struct 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;

        
    Socket socket(AF_INETSOCK_DGRAMIPPROTO_UDP);
        if(
    Socket == -1)
        {
            return 
    "0";
        }

        if(
    setsockopt(SocketSOL_SOCKETSO_RCVTIMEO, (char *)&timeoutsizeof(timeout)) == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        if(
    setsockopt(SocketSOL_SOCKETSO_SNDTIMEO, (char *)&timeoutsizeof(timeout)) == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        if(
    connect(Socket, (struct sockaddr *)&serversizeof(server)) == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

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

        if(
    == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        
    response[i] = '\0';

        
    close(Socket);

        
    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);

    PHP Code:
    resp sendCommand("185.58.205.123"28960"getstatus"); 
    array = 
    StrTok(resp"\t"); 
    for (
    i=0;i<array.size;i++)
    {
        
    printfline("[%]: %"i, array[i]);

    Works:
    Click image for larger version. 

Name:	Снимок экрана (8).png 
Views:	49 
Size:	42.7 KB 
ID:	1010

    But not sure about stability since i only done a quick-test, you may go ahead and improve this code if you need this
    Last edited by voron00; 9th January 2016 at 15:52.

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

    Ni3ls (9th January 2016)

  5. #13
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    @voron how/where to add that code?

  6. #14
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    Quote Originally Posted by Ni3ls View Post
    @voron how/where to add that code?
    Its in the first post, just use my code in gsc_utils instead of that one

  7. #15
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    But i dont have that .h file

  8. #16
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    [gsc.cpp]
    PHP Code:
    {"sendCommand"             gsc_utils_sendCommand                 0}, 
    [gsc_utils.hpp] <-----
    PHP Code:
    void gsc_utils_sendCommand(); 

  9. The Following User Says Thank You to voron00 For This Useful Post:

    Ni3ls (9th January 2016)

  10. #17
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    Its compiled now, however when I start server I get segmentation fault.
    Code:
    > [INFO] Compiled for: CoD2 1.0
    Compiled: Jan  9 2016 17:55:26 using GCC 4.4.7 20120313 (Red Hat 4.4.7-16)
    ./starttest: line 1: 21329 Segmentation fault      (core dumped) LD_PRELOAD="/home/olger/HIGHJUMP/libcod2_1_0new.so" ./cod2_lnxded_1_0a_va_loc_128 +set dedicated 2 +set net_ip 46.4.55.66 +set net_port 28972 +set sv_maxclients 52 +exec servertdm.cfg + exec pws.cfg +set fs_game test + set sv_cracked 1 + set logfile 2
    Startup line;
    Code:
    LD_PRELOAD="/home/olger/HIGHJUMP/libcod2_1_0new.so" ./cod2_lnxded_1_0a_va_loc_128 +set dedicated 2 +set net_ip 46.4.55.66 +set net_port 28972 +set sv_maxclients 52 +exec servertdm.cfg + exec pws.cfg +set fs_game test + set sv_cracked 1 + set logfile 2

  11. #18
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    I had that segmentation error before with my own compiled libcod files.
    http://killtube.org/showthread.php?2...3230#post13230

    Voron do you mind sending your build again please?

  12. #19
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    Quote Originally Posted by Ni3ls View Post
    I had that segmentation error before with my own compiled libcod files.
    http://killtube.org/showthread.php?2...3230#post13230

    Voron do you mind sending your build again please?
    Check ur pm

  13. The Following User Says Thank You to voron00 For This Useful Post:

    Ni3ls (10th January 2016)

  14. #20
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    After map restart I get this error;
    Code:
    *** glibc detected *** ./cod2_lnxded: free(): invalid pointer: 0x08cc1d88 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x9a4c81]
    /lib/libc.so.6[0x9a75c2]
    ./cod2_lnxded[0x80a9265]
    ./cod2_lnxded[0x80919cf]
    ./cod2_lnxded[0x8091d29]
    ./cod2_lnxded[0x808ad5f]
    ./cod2_lnxded[0x8060724]
    ./cod2_lnxded[0x805fc79]
    ./cod2_lnxded[0x808b228]
    ./cod2_lnxded[0x8060724]
    ./cod2_lnxded[0x805fdf5]
    ./cod2_lnxded[0x805fe07]
    ./cod2_lnxded[0x806249a]
    ./cod2_lnxded[0x806251d]
    ./cod2_lnxded[0x80d2b22]
    /lib/libc.so.6(__libc_start_main+0xe6)[0x94ad36]
    ./cod2_lnxded(gethostbyname+0x261)[0x804a4d1]
    ======= Memory map: ========
    001d3000-001f0000 r-xp 00000000 fd:00 2103750                            /lib/libgcc_s-4.4.7-20120601.so.1
    001f0000-001f1000 rwxp 0001d000 fd:00 2103750                            /lib/libgcc_s-4.4.7-20120601.so.1
    00912000-00930000 r-xp 00000000 fd:00 2103276                            /lib/ld-2.12.so
    00930000-00931000 r-xp 0001d000 fd:00 2103276                            /lib/ld-2.12.so
    00931000-00932000 rwxp 0001e000 fd:00 2103276                            /lib/ld-2.12.so
    00934000-00ac4000 r-xp 00000000 fd:00 2103745                            /lib/libc-2.12.so
    00ac4000-00ac5000 ---p 00190000 fd:00 2103745                            /lib/libc-2.12.so
    00ac5000-00ac7000 r-xp 00190000 fd:00 2103745                            /lib/libc-2.12.so
    00ac7000-00ac8000 rwxp 00192000 fd:00 2103745                            /lib/libc-2.12.so
    00ac8000-00acb000 rwxp 00000000 00:00 0
    00acd000-00ad0000 r-xp 00000000 fd:00 2103746                            /lib/libdl-2.12.so
    00ad0000-00ad1000 r-xp 00002000 fd:00 2103746                            /lib/libdl-2.12.so
    00ad1000-00ad2000 rwxp 00003000 fd:00 2103746                            /lib/libdl-2.12.so
    00ad4000-00aeb000 r-xp 00000000 fd:00 2103747                            /lib/libpthread-2.12.so
    00aeb000-00aec000 r-xp 00016000 fd:00 2103747                            /lib/libpthread-2.12.so
    00aec000-00aed000 rwxp 00017000 fd:00 2103747                            /lib/libpthread-2.12.so
    00aed000-00aef000 rwxp 00000000 00:00 0
    00af1000-00b19000 r-xp 00000000 fd:00 2103347                            /lib/libm-2.12.so
    00b19000-00b1a000 r-xp 00027000 fd:00 2103347                            /lib/libm-2.12.so
    00b1a000-00b1b000 rwxp 00028000 fd:00 2103347                            /lib/libm-2.12.so
    00b1d000-00b24000 r-xp 00000000 fd:00 2103748                            /lib/librt-2.12.so
    00b24000-00b25000 r-xp 00006000 fd:00 2103748                            /lib/librt-2.12.so
    00b25000-00b26000 rwxp 00007000 fd:00 2103748                            /lib/librt-2.12.so
    00cff000-00d11000 r-xp 00000000 fd:00 2103739                            /lib/libz.so.1.2.3
    00d11000-00d12000 r-xp 00011000 fd:00 2103739                            /lib/libz.so.1.2.3
    00d12000-00d13000 rwxp 00012000 fd:00 2103739                            /lib/libz.so.1.2.3
    00d92000-00da7000 r-xp 00000000 fd:00 2103735                            /lib/libresolv-2.12.so
    00da7000-00da8000 ---p 00015000 fd:00 2103735                            /lib/libresolv-2.12.so
    00da8000-00da9000 r-xp 00015000 fd:00 2103735                            /lib/libresolv-2.12.so
    00da9000-00daa000 rwxp 00016000 fd:00 2103735                            /lib/libresolv-2.12.so
    00daa000-00dac000 rwxp 00000000 00:00 0
    08048000-08186000 rwxp 00000000 fd:02 77858086                           /home/olger/HIGHJUMP/cod2_lnxded
    08186000-089c0000 rwxp 00000000 00:00 0
    08c94000-08fd4000 rwxp 00000000 00:00 0                                  [heap]
    e681f000-eb37a000 rwxp 00000000 00:00 0
    eb500000-eb521000 rwxp 00000000 00:00 0
    eb521000-eb600000 ---p 00000000 00:00 0
    eb63e000-eced0000 rwxp 00000000 00:00 0
    eced0000-ecedc000 r-xp 00000000 fd:00 2103475                            /lib/libnss_files-2.12.so
    ecedc000-ecedd000 r-xp 0000b000 fd:00 2103475                            /lib/libnss_files-2.12.so
    ecedd000-ecede000 rwxp 0000c000 fd:00 2103475                            /lib/libnss_files-2.12.so
    ecef4000-f6ef5000 rwxp 00000000 00:00 0
    f70c4000-f718f000 rwxp 00000000 00:00 0
    f718f000-f7270000 r-xp 00000000 fd:00 2242009                            /usr/lib/libstdc++.so.6.0.13
    f7270000-f7274000 r-xp 000e0000 fd:00 2242009                            /usr/lib/libstdc++.so.6.0.13
    f7274000-f7276000 rwxp 000e4000 fd:00 2242009                            /usr/lib/libstdc++.so.6.0.13
    f7276000-f727d000 rwxp 00000000 00:00 0
    f727d000-f7524000 r-xp 00000000 fd:02 77858364                           /home/olger/HIGHJUMP/libmysqlclient.so.18
    f7524000-f7527000 r-xp 002a7000 fd:02 77858364                           /home/olger/HIGHJUMP/libmysqlclient.so.18
    f7527000-f759d000 rwxp 002aa000 fd:02 77858364                           /home/olger/HIGHJUMP/libmysqlclient.so.18
    f759d000-f75a1000 rwxp 00000000 00:00 0
    f75a1000-f7650000 r-xp 00000000 fd:00 2251751                            /usr/lib/libstdc++.so.5.0.7
    f7650000-f7655000 rwxp 000ae000 fd:00 2251751                            /usr/lib/libstdc++.so.5.0.7
    f7655000-f765b000 rwxp 00000000 00:00 0
    f765b000-f7666000 rwxp 00000000 00:00 0
    f7666000-f766b000 r-xp 00000000 fd:00 2103309                            /lib/libnss_dns-2.12.so
    f766b000-f766c000 r-xp 00004000 fd:00 2103309                            /lib/libnss_dns-2.12.so
    f766c000-f766d000 rwxp 00005000 fd:00 2103309                            /lib/libnss_dns-2.12.so
    f766d000-f7670000 rwxp 00000000 00:00 0
    f7670000-f7691000 r-xp 00000000 fd:02 77872549                           /home/olger/HIGHJUMP/libcod2_1_0_w_sendcommand.so
    f7691000-f7692000 r-xp 00020000 fd:02 77872549                           /home/olger/HIGHJUMP/libcod2_1_0_w_sendcommand.so
    f7692000-f7693000 rwxp 00021000 fd:02 77872549                           /home/olger/HIGHJUMP/libcod2_1_0_w_sendcommand.so
    f7693000-f771b000 rwxp 00000000 00:00 0
    f771b000-f771c000 r-xp 00000000 00:00 0                                  [vdso]
    ffcc7000-ffcdc000 rwxp 00000000 00:00 0                                  [stack]
    ./starttest: line 1: 16729 Aborted
    Last edited by Ni3ls; 10th January 2016 at 13:14.

Posting Permissions

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