Click here to Skip to main content
13,297,878 members (44,908 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 25 Nov 2002

Trace Route using raw sockets

, 28 Jul 2003
Rate this:
Please Sign up or sign in to vote.
This Article Looks at a way to Trace Route by Raw Sockets
<!-- Article image -->

Sample Image - TraceRoutes.jpg

<!-- Add the rest of your HTML here -->


This Project was undertaken on the needs to explore Sockets and its different Uses . I started by making a Ping program which pinged the Network Addresses . The ping program was inspired by FAQs at and mostly used their Functions!!!. The Route Program is an enhancement of same program (Ping program ) which used Raw Sockets.

What and Why?

Ping program basically sends ICMP Packets to Remote Host and asks it to send back an reply and calculates the Time to get the reply back. Trace Route traces the Route of our packets via network , identifying the IP Address of every host .

How To Run it ?

The standard Trace Route (tracert.exe) which comes with Windows Operating System was basically an example for me to emulate. The short comings ( which is my point of view ) with the standard Trace Route was the delay it had which takes three (and I presume ICMP Packet replies ) . My program takes 1 ICMP reply. As soon as I get ICMP_ECHO( integer 0 ) reply, I know that the packet has reached the destination and the program exits.

Go to Command Prompt : in the Dos Prompt Type the Path of the EXE and with space type the URL or ip Address like C:\>TracesRoutes.exe x.x.x.x timeout(Optional). Remember to type a valid IP Address otherwise an error message "Destination Unreachable " would be given.

The Code

All of the code is the same except a few changes here and there. The main function is Decode_Reply
int decode_reply( IPHeader* reply, int bytes, sockaddr_in* from ) 
    // Make sure the reply is sane
    if (bytes < header_len + ICMP_MIN) 
        cerr << "too few bytes from " << inet_ntoa(from->sin_addr) << endl;
        return -1;
    else if ( icmphdr->type != ICMP_ECHO_REPLY ) 
        if ( icmphdr->type != ICMP_TTL_EXPIRE ) 
            if ( icmphdr->type == ICMP_DEST_UNREACH ) 
                cerr << "Destination unreachable" << endl;
                cerr << "Unknown ICMP packet type " << int(icmphdr->type) <<
                        " received" << endl;
            return -1;
        // If "TTL expired", fall through.  Next test will fail if we
        // try it, so we need a way past it.
    else if (icmphdr->id != (USHORT)GetCurrentProcessId()) 
        // Must be a reply for another pinger running locally, so just
        // ignore it.
        return -2;

    // Okay, we ran the gamut, so the packet must be legal -- dump it
    if (( icmphdr->type == ICMP_TTL_EXPIRE ) || 
        ( icmphdr->type == ICMP_ECHO_REPLY ) ) 
    in_addr in;
    in.S_un.S_addr = reply->source_ip; 
    cout << "\n Source IP " << inet_ntoa( in ) ; 
    int nTime = GetTickCount () - ulTimestamp ;
    if ( nTime < 0 )
      cout << "  Time: " << "<10 ms." << endl;
      cout << "  Time: " << ( GetTickCount() - ulTimestamp ) 
         << " ms." << endl;
    return 0;


If you are facing problems, contact your system administrator . I have tested this Software on both Private and Public IPs . Mostly System Administrators disable this functionality.If you are on a network you can trace some other Computer on same network to test it.


I have added Destination Unreachable Case. The code is messy so if you find anything which can make it better do send in a line at my Web Site or my Email.


Kindly Send ur Comments through Thanks.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Babar Qaisrani
Web Developer
Canada Canada
Smile | :)

You may also be interested in...


Comments and Discussions

General[Message Deleted] Pin
it.ragester2-Apr-09 22:50
memberit.ragester2-Apr-09 22:50 
GeneralRouter connection blocking Pin
Eru8727-Nov-07 13:59
memberEru8727-Nov-07 13:59 
GeneralA Big Mistake Pin
Prasshhant Pugalia11-Nov-07 19:53
memberPrasshhant Pugalia11-Nov-07 19:53 
Generalusing c# Pin
x_sanctus14-Nov-05 15:23
memberx_sanctus14-Nov-05 15:23 
QuestionPlagiarize? Pin
Anonymous22-May-04 16:03
sussAnonymous22-May-04 16:03 
AnswerRe: Plagiarize? Pin
Anonymous22-May-04 16:15
sussAnonymous22-May-04 16:15 
QuestionCan you give me a copy of src? Pin 18:09 18:09 
Can you give me a copy of src? Thanks. Smile | :)
My email:
AnswerRe: Can you give me a copy of src? Pin
Babar Qaisrani7-Jun-03 4:08
memberBabar Qaisrani7-Jun-03 4:08 
AnswerRe: Can you give me a copy of src? Pin
threebochen14-Jul-03 17:23
memberthreebochen14-Jul-03 17:23 
GeneralRe: Can you give me a copy of src? Pin
Babar Qaisrani28-Jul-03 20:01
memberBabar Qaisrani28-Jul-03 20:01 
AnswerRe: Can you give me a copy of src? Pin
AboutJune23-Jul-03 19:51
memberAboutJune23-Jul-03 19:51 
GeneralRe: Can you give me a copy of src? Pin
Babar Qaisrani28-Jul-03 20:00
memberBabar Qaisrani28-Jul-03 20:00 
AnswerRe: Can you give me a copy of src? Pin
Babar Qaisrani28-Jul-03 20:02
memberBabar Qaisrani28-Jul-03 20:02 
GeneralWonderful Article!! Pin
Aisha Ikram26-Nov-02 20:54
memberAisha Ikram26-Nov-02 20:54 
GeneralRe: Wonderful Article!! Pin
Normski28-Jul-03 22:00
memberNormski28-Jul-03 22:00 
GeneralRe: Wonderful Article!! Pin
Babar Qaisrani30-Jul-03 20:14
memberBabar Qaisrani30-Jul-03 20:14 
GeneralRe: Wonderful Article!! Pin
Nishant S18-Sep-03 2:39
editorNishant S18-Sep-03 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 29 Jul 2003
Article Copyright 2002 by Babar Qaisrani
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid