Thread: working out an IP address

  1. #1
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63

    working out an IP address

    Hi ..

    Been trying to work out an dot style IP address, but I keep getting a 400 Bad request..
    I have tried with yahoo and seems to work ok...

    I know little about the internet technicals....working on that.

    The site is .. Database | WSPRnet

    The IP I am trying was from an online IP checker...

    John

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The error is on line 53 of your code: you forgot to carry the one.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    All kidding aside, I do not understand what your problem is. And you have shown no code to help us guess, either. That's why you've not gotten any sensible answers.

    Let's say you use getaddrinfo("wsprnet.org", "http", &hints, &list) to obtain a list of sockets to try to connect to, using the Client example shown on that manual page, with hints.ai_socktype = SOCK_STREAM. One of the sockets will connect(), so you use that one (the first that succeeds), and free the list (using freeaddrinfo(&list)).

    If you send the C string "GET /drupal/wsprnet/spots HTTP/1.0\r\nHost: wsprnet.org\r\nConnection: close\r\n\r\n" , the other end will respond with
    Code:
    HTTP/1.1 200 OK
    Date: Tue, 24 Nov 2015 11:58:58 GMT
    Server: Apache/2.4.7 (Ubuntu)
    X-Powered-By: PHP/5.5.9-1ubuntu4.11
    Expires: Sun, 19 Nov 1978 05:00:00 GMT
    Last-Modified: Tue, 24 Nov 2015 11:58:58 GMT
    Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
    ETag: "1448366338"
    Content-Language: en
    X-Generator: Drupal 7 (http://drupal.org)
    Vary: Accept-Encoding
    Connection: close
    Content-Type: text/html; charset=utf-8
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
      "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" version="XHTML+RDFa 1.0" dir="ltr">
    
    <head profile="http://www.w3.org/1999/xhtml/vocab">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="shortcut icon" href="http://wsprnet.org/drupal/sites/all/themes/newsflash/favicon.ico" type="image/vnd.microsoft.icon" />
    <meta name="Generator" content="Drupal 7 (http://drupal.org)" />
    <meta http-equiv="refresh" content="300" />
      <title>Database | WSPRnet</title>
      <link type="text/css" rel="stylesheet" href="http://wsprnet.org/drupal/sites/wsprnet.org/files/css/css_dRT-6rU1dRftKRQwz-1EJWE6PP3Qm_cmfDslBM1Fs78.css" media="all" />
    <link type="text/css" rel="stylesheet" href="http://wsprnet.org/drupal/sites/wsprnet.org/files/css/css_pIuJfEyn0ZF4gpwpFGeZt0xKRlQvHYLbXYW0olAdO18.css" media="all" />
    <link type="text/css" rel="stylesheet" href="http://wsprnet.org/drupal/sites/wsprnet.org/files/css/css_DLLt4riJFIkzM0ffJFQnPYT75f8XMoQxj0QnAelgsmY.css" media="all" />
    <style type="text/css" media="all">
    (the rest of the response snipped out for brevity).

    You can even test this on the command line using netcat:
    Code:
    ipaddr=`LANG=C LC_ALL=C host wsprnet.org | sed -ne 's|^.* has address \(.*\)$|\1|p'`
    printf 'GET /drupal/wsprnet/spots HTTP/1.0\r\nHost: wsprnet.org\r\nConnection: close\r\n\r\n' | nc $ipaddr 80
    So, as you can see, not only do I not understand your question, but even a straightforward test using the little information you gave works, making it impossible for me to reproduce the problem you're seeing.

    Please elaborate.

  4. #4
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi..

    Thank you for the reply...now have the Bgnet. pdf ...and going thru it... I was using dot format as in 96.126.107.76 which was not getting me far... so will work on your suggestion....

  5. #5
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi...

    I have put a program together, that now gives me similar printout as your suggestion.....Nominal Animal
    Have since found some information on HTTP commands......and understand how you came to your C string above.
    I tried similar to access an older database on the same site....but get a 400 Bad Request.....but at least I know the software is working this time....

    This is m request for info...

    "GET /olddb?mode=html&band=all&limit=50&findcall=&findre porter=&sort=date HTTP/1.1\r\n Host:www.wsprnet.org\r\n Connection:close\r\n\r\n";


    This is the site address..

    WSPR Spots

    if you have the time, can you see an issue...with my request...


    my reference to HTTP... http://www.w3.org/Protocols/rfc2616/....html#sec5.1.1
    Last edited by JohnGM; 11-25-2015 at 10:19 PM.

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Again, I do not get the error you describe. The most common reason for a 400 error from that server is missing the initial slash from the path part.

    In other words, make sure the first line (ending with "\r\n") of your request is
    Code:
    GET /olddb?mode=html&band=all&limit=50&findcall=&findreporter=&sort=date HTTP/1.0
    and not
    Code:
    GET olddb?mode=html&band=all&limit=50&findcall=&findreporter=&sort=date HTTP/1.0
    I probably should not do this -- spoonfeeding code like this is rarely constructive --, but here is a very crude bare-bones example I used to verify this; client.c:
    Code:
    #define _POSIX_C_SOURCE 200809L
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <string.h>
    #include <netdb.h>
    #include <errno.h>
    #include <stdio.h>
    
    #define HAD_EACCES       (1<<0)
    #define HAD_ETIMEDOUT    (1<<1)
    #define HAD_ECONNREFUSED (1<<2)
    #define HAD_ENETUNREACH  (1<<3)
    
    size_t http_get(const char  *const host,
                    const char  *const port,
                    const char  *const path,
                    char       **const result_data,
                    size_t      *const result_size)
    {
        const char *const serv = (port == NULL || port[0] == '\0') ? "http" : port;
        const char *const abspath = (path == NULL || path[0] == '\0') ? "/" : path;
        struct addrinfo   hints, *curr, *list = NULL;
        int               result, descriptor;
        unsigned char    *data;
        size_t            size, used = 0;
    
        if (!host || !result_data || !result_size) {
            errno = EINVAL;
            return 0;
        }
    
        if (*result_size) {
            data = (unsigned char *)(*result_data);
            size = *result_size;
        } else {
            data = NULL;
            size = 0;
        }
    
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;     /* IPv4 or IPv6 */
        hints.ai_socktype = SOCK_STREAM; /* TCP */
    
        result = getaddrinfo(host, serv, &hints, &list);
        if (result) {
            errno = ENOENT; /* No such host/port */
            return 0;
        }
    
        result = 0;
        descriptor = -1;
        for (curr = list; curr != NULL; curr = curr->ai_next) {
    
            descriptor = socket(curr->ai_family, curr->ai_socktype, curr->ai_protocol);
            if (descriptor == -1)
                continue;
    
            if (connect(descriptor, curr->ai_addr, curr->ai_addrlen) != -1)
                break;
    
            switch (errno) {
            case EACCES:
            case EPERM:
                result |= HAD_EACCES;
                break;
            case ECONNREFUSED:
                result |= HAD_ECONNREFUSED;
                break;
            case ENETUNREACH:
                result |= HAD_ENETUNREACH;
                break;
            case ETIMEDOUT:
                result |= HAD_ETIMEDOUT;
                break;
            }
    
            close(descriptor);
            descriptor = -1;
        }
    
        freeaddrinfo(list);
    
        if (descriptor == -1) {
            if (result & HAD_EACCES)
                errno = EACCES;
            else
            if (result & HAD_ETIMEDOUT)
                errno = ETIMEDOUT;
            else
            if (result & HAD_ECONNREFUSED)
                errno = ECONNREFUSED;
            else
            if (result & HAD_ENETUNREACH)
                errno = ENETUNREACH;
            else
                errno = EHOSTUNREACH;
            return 0;
        }
    
        while (1) {
            result = snprintf((char *)data, size, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", abspath, host);
            if (result < 0) {
                close(descriptor);
                errno = EINVAL;
                return 0;
            }
    
            if ((size_t)result < size) {
                used = (size_t)result;
                break;
            }
    
            size = ((size_t)result | 2047) + 2049;
            data = realloc(data, size);
            if (!data) {
                close(descriptor);
                errno = ENOMEM;
                return 0;
            }
            *result_data = (char *)data;
            *result_size = size;
        }
    
        /* Send request. */
        {
            const char       *p = (char *)data;
            const char *const q = (char *)data + used;
            ssize_t           n;
    
            while (p < q) {
                n = write(descriptor, p, (size_t)(q - p));
                if (n > 0)
                    p += n;
                else
                if (n != (ssize_t)-1) {
                    close(descriptor);
                    errno = EIO;
                    return 0;
                } else
                if (errno != EAGAIN && errno != EINTR) {
                    const int saved_errno = errno;
                    close(descriptor);
                    errno = saved_errno;
                    return 0;
                }
            }
        }
    
        used = 0;
    
        /* Receive all. */
        while (1) {
            ssize_t n;
    
            if (used >= size) {
                size = (used | 2047) + 2049;
                data = realloc(data, size);
                if (!data) {
                    close(descriptor);
                    errno = ENOMEM;
                    return 0;
                }
                *result_data = (char *)data;
                *result_size = size;
            }
    
            n = read(descriptor, data + used, size - used);
            if (n > 0)
                used += n;
            else
            if (n == 0)
                break;
            else
            if (n != (ssize_t)-1) {
                close(descriptor);
                errno = EIO;
                return 0;
            } else
            if (errno != EAGAIN && errno != EINTR) {
                const int saved_errno = errno;
                close(descriptor);
                errno = saved_errno;
                return 0;
            }
        }
    
        close(descriptor);
    
        if (used < 1) {
            errno = ECONNREFUSED;
            return 0;
        }
    
        if (used >= size) {
            size = used + 1;
            data = realloc(data, size);
            if (!data) {
                errno = ENOMEM;
                return 0;
            }
            *result_data = (char *)data;
            *result_size = size;
        }
    
        errno = 0;
        data[used] = '\0';
        return used;
    }
    
    
    int main(int argc, char *argv[])
    {
        char   *response = NULL;
        size_t  response_maxlength = 0;
        size_t  response_length;
    
        if (argc != 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
            fprintf(stderr, "\n");
            fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
            fprintf(stderr, "       %s HOST PATH\n", argv[0]);
            fprintf(stderr, "\n");
            return EXIT_FAILURE;
        }
    
        response_length = http_get(argv[1], NULL, argv[2], &response, &response_maxlength);
        if (!response_length) {
            fprintf(stderr, "%s.\n", strerror(errno));
            return EXIT_FAILURE;
        }
    
        fprintf(stderr, "Received a %zu-byte response:\n", response_length);
        fflush(stderr);
        fwrite(response, response_length, 1, stdout);
        fflush(stdout);
    
        return EXIT_SUCCESS;
    }
    compiling it using
    gcc -Wall -Wextra -std=c99 -O2 client.c -o example
    and running it using
    Code:
    ./example www.wsprnet.org '/olddb?mode=html&band=all&limit=50&findcall=&findreporter=&sort=date'
    works just fine, producing the output
    Received a 29562-byte response:
    to standard error, and
    Code:
    HTTP/1.1 200 OK
    Date: Thu, 26 Nov 2015 15:52:12 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Content-Location: olddb.php
    Vary: negotiate,Accept-Encoding
    TCN: choice
    X-Powered-By: PHP/5.5.9-1ubuntu4.11
    Connection: close
    Content-Type: text/html
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
       <meta name="MSSmartTagsPreventParsing" content="true">
       <meta http-equiv="Pragma" content="no-cache">
       <meta http-equiv="Refresh" content="300">
       <title>WSPR Spots</title>
       <link rel="alternate" title="MEPT Spots RSS" href="http://wsprnet.org/olddb?mode=rss" type="application/rss+xml">
       <style type="text/css">
          #oddrow { background-color: #382836; }
          #evenrow { background-color: #30202d; }
          A:link { color: #ffff00; }
          A:visited { color: #ffff00; }
          A:active { color: #ffff00; }
       </style>
    </head>
    to standard output (most of the output omitted for brevity).

  7. #7
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi.

    Thanks for going to such effort...

    I found a problem with the string I was using, I had a space or 2 in it that was causing an issue. Mind you I could swear blind I had tried changing things around...its possible I had more than one thing changed and so hiding the actual problem...But to be honest I would have been guessing, not a good way to program.....so have some reading to do

    Your effort wont be wasted....as I will take a good look at the way you layed it out , which is one of the things that separates the Beginners from the experts.

    Thanks again..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. Replies: 1
    Last Post: 11-07-2010, 11:39 PM
  3. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  4. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM
  5. My IP Address
    By CppProgrammer in forum Tech Board
    Replies: 10
    Last Post: 06-28-2003, 05:10 PM