Dynamic DNS Woes

 

What is Dynamic DNS
If you wish to make a webcam  or weathercam available on your website or simply view them yourself away from home then the chances are that you will need a dynamic dns service that will allow you to access your home network via a url.

As most home broadband connections have a dynamic IP address there needs to be a mechanism to modify the DNS  for the url to point to whatever the IP address is at any given time. Many home routers and webcams provide automated update for a limited set of dynamic dns services – predominantly those from dyndns.com and no-ip.org. Alternately an updater program is usually available from the service provider.

Can I get this free?
Unfortunately DynDNS stopped providing free hosts which lead to me using no-ip.org for several years. As my router only updates the DNS when there is an IP change and mine only changes infrequently I was getting a monthly email telling me I had to log in and verify I still needed the service. Then I got an email in September 2013 telling me the host had been deleted with no prior email at all. It looks to me like no-ip may be heading the same way as dyndns by making the free accounts less attractive. There are other free services out there but as they are unsupported by routers etc. force you to run an updater program.



D-Link free DynDNS service
My D-Link NAS supports a free service provided through DynDNS and I imagine D-Link routers will do the same. I set up digitalham.dlinkddns.com and set the NAS to update it. After finding the cameras weren’t accessible on two occasions because the IP had been incorrectly set to the local IP address I’ve disabled the updating by the NAS. Luckily my router can update DynDNS and the free DLink account has a DynDNS login so that is what I am currently using by setting cam.digitalham.co.uk to use it as a CNAME in the DNS.

An alternative
As I no longer trust no-ip and have no experience of the reliability of the D-Link service I decided to set up an alternative approach for the cameras on DigitalHam which I have tested and found to work. This will probably not appeal to many but I’ll pass on what I did in case it does help somebody else. It is really only suitable for webcams provided on websites or if you also adopt my Portable Browser Favourites where it would be possible to modify the script to pick up the correct IP if it was stored on the same server. In essence I have a self refreshing web page which sits in a browser at home an updates a database with my home IP address. The pages which provide links to the cameras pull the home IP address from the database and use that as the url for the camera instead of a url which needs to be translated to that IP by a dynamic dns service server.

The database
This is trivial and comprises a single two column table…

CREATE TABLE `homeip`
(
`type` char(5) NOT NULL,
`home_url` varchar(100) NOT NULL,
PRIMARY KEY (`type`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

The update page
To ensure that crawlers will not access this page I have included simple basic auth coding which requires a hardcoded userid and password of your choice to be entered before the IP update will run. You should also put it in a directory blocked from crawlers by your robots.txt. To run simply open the page in your home PC browser, log in, and leave it running..

<?php
if (isset($_COOKIE['auth'])) $auth = $_COOKIE['auth'];
if (!$auth == "yes")
 {
  if (isset($_SERVER['PHP_AUTH_USER'])) $AUTH_USER  = $_SERVER['PHP_AUTH_USER'];
  if (isset($_SERVER['PHP_AUTH_PW']))   $AUTH_PW    = $_SERVER['PHP_AUTH_PW'];

  if ((($AUTH_USER != "[your userid") || ($AUTH_PW != "your password")))
   {
    header('WWW-Authenticate: Basic realm="Private pages"');
    header('HTTP/1.0 401 Unauthorized');
    echo "Authorization required";
    exit;
   }
 }
setcookie ("auth","yes",time()+(365*24*3600));

$connect = @mysql_connect('localhost','[DB User]','[DB Password]');
@mysql_select_db('[DB Name]');
$query  = "SELECT * FROM homeip where type = 'ip'";
$result = @mysql_query($query);
$row    = @mysql_fetch_array($result);
$old_ip = stripslashes($row['home_url']);

$home_ip = $_SERVER['REMOTE_ADDR'];
if ($home_ip <> $old_ip)
 {
  $query    = "UPDATE homeip set home_url = '$home_ip' where type = 'ip'";
  $result   = @mysql_query($query);
 }

 @mysql_close($connect);   
?>
<html>
 <head>
 <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <META name="robots" content="noindex,nofollow">
 <meta http-equiv='refresh' content='3600'>
 </head>
 <body bgcolor="white">
<h2>Only run this from home</h2>
<p> Sets IP on server to IP this script is running on.
<?php
$timestamp = date('r');
echo "<br>$timestamp<br>";
if ($home_ip <> $old_ip)
 echo "Database updated. Home IP set to $home_ip";
else
 echo "IP is $home_ip - unchanged";
?>
 </body>
</html>

The scripting to retrieve the IP
Use this in pages where you need to provide a link to the webcam.

<?php
$connect = @mysql_connect('localhost','[database user]','[password]');
@mysql_select_db('[database name]');
$query = "SELECT * FROM homeip where type = 'ip'";
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
$home_url = stripslashes($row['home_url']);
@mysql_close($connect);
?>

IP Camera Access Page

You can use this simple page to either just access the camera via its defined port or optionally for cameras such as Foscams  pass control commands via the query string.

<?php
if (isset($_REQUEST['port'])) $port = $_REQUEST['port'];
if (isset($_REQUEST['page'])) $page = $_REQUEST['page'];
if (isset($_REQUEST['command'])) $command = $_REQUEST['command'];
if (isset($_REQUEST['onestep'])) $onestep = $_REQUEST['onestep'];
if (isset($_REQUEST['user'])) $user = $_REQUEST['user'];
if (isset($_REQUEST['pwd'])) $pwd = $_REQUEST['pwd'];
include("../includes/home_url.php");
$port = ":".$port;
if ($page) $page = "/$page?command=$command&onestep=$onestep&user=$user&pwd=$pwd";
header("HTTP/1.1 302 Moved Temporarily");
header("Location: http:/$home_url$port$page");
echo "";
?>