• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

getting my ip address

i need a routine that will in C that will get my internet ip address not lan ip address,print it on screen and then send me an email of the ip address
0
soloboso
Asked:
soloboso
1 Solution
 
PaulCaswellCommented:
Hi soloboso,

Unfortunately, this code will be system-specific so what system are you running on?

Paul
0
 
x4uCommented:
There is no easy way to find your external IP from inside lan. You need to rely on some external service that responds with your IP in some way. Here is some code that gets your IP addresse from http://checkip.dyndns.org/. It compiles and runs on Windows 2k/XP and Linux but should also work on Unix.

// save as myip.c
// WINDOWS: cl -o myip.exe myip.c /link ws2_32.lib
// LINUX:   gcc -o myip myip.c

#include <stdio.h>

#ifdef _WIN32
#include <winsock2.h>
#else
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#endif

struct in_addr GetMyIP()
{
    static char *req = "GET / HTTP/1.0\r\nHost: checkip.dyndns.org\r\nConnection: close\r\n\r\n";
    static char *hostname = "checkip.dyndns.org";
    char buf[ 2048 ];
    int sock;
    struct sockaddr_in addr = { AF_INET, htons( 80 ), 0 };
    unsigned long nodelay = 1;
    struct hostent *host;
    unsigned long myip = 0;

#ifdef WINSOCK_VERSION
    InitSockets(); // only needed for Windows OS
#endif

    if( host = gethostbyname( hostname ) )
    {
        addr.sin_addr = *(struct in_addr *) host->h_addr;
        if( ( sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) != -1 )
        {
            setsockopt( sock, IPPROTO_TCP, TCP_NODELAY, (char *) &nodelay, 4 );
            if( connect( sock, (struct sockaddr *) &addr, sizeof( addr ) ) != -1 )
            {
                if( send( sock, req, strlen( req ), 0 ) != -1 )
                {
                    int len = recv( sock, buf, sizeof( buf ) - 1, 0 );
                    if( len > 0 )
                    {
                        char *pos, *end, *token = "Current IP Address: ";
                        buf[ len ] = '\0';
                        if( ( pos = strstr( buf, token ) ) && ( end = strstr( pos, "<" ) ) )
                        {
                            pos += strlen( token );
                            *end = '\0';
                            myip = inet_addr( pos );
                        }
                        else
                            printf( "no address in response\n" );
                    }
                    else
                        printf( "failed to read response from %s\n", hostname );
                }
                else
                    printf( "failed to send request to %s\n", hostname );
            }
            else
                printf( "cannot connect to %s\n", hostname );
            close( sock );
        }
        else
            printf( "failed to create socket\n" );
    }
    else
        printf( "not connected to the internet or %s is gone\n", hostname );

    if( !myip )
        printf( "error: %s\n", errno );

    return *(struct in_addr *) &myip;
}

int main( int argc, int **argv )
{
    struct in_addr myip = GetMyIP();
    unsigned char *ip = (unsigned char *) &myip;
    printf( "myip: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );

#ifdef WINSOCK_VERSION
    WSACleanup();
#endif
}

#ifdef WINSOCK_VERSION
static WSADATA wsaData = { 0 };
int InitSockets()
{
    int error = 0;
    if( !wsaData.wVersion && ( error = WSAStartup( MAKEWORD( 2, 0 ), &wsaData ) ) )
        return error;
    return 0;
}
#endif
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now