?
Solved

getting my ip address

Posted on 2006-03-26
4
Medium Priority
?
363 Views
Last Modified: 2010-04-15
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
Comment
Question by:soloboso
2 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16294054
Hi soloboso,

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

Paul
0
 
LVL 11

Accepted Solution

by:
x4u earned 2000 total points
ID: 16295011
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question