#!/usr/bin/perl
################### 
##
##  FreeDNS.Afraid.org Dynamic IP script
##  v2.0
##  April 1, 2012
##
###################

use Sys::Hostname;
use LWP::UserAgent;
# deal with stdin, stdout, stderr, control terminal, and put self
# in background
$TIOCNOTTY = 0x20007471;
close(stdin);
open(stdout, ">/dev/null");
open(stderr, ">/dev/null");
if (open(tty, "/dev/tty")) {
    ioctl(tty, $TIOCNOTTY, 0);
    close(tty);
    } # if on opening tty
if (fork) {
    exit 0;
    } # if on successful forking



## set log file and ip file names
## default path is current folder, 
## can change it to whatever... ie: /var/log or c:/temp
$logfile   = 'fda-ip.log';
$ipfile    = 'fda-ip.txt';

#clear the logfile
clear();

mark("START","Starting dynamic IP program");

while (1) {
    #read the previous ip address
    open S, "$ipfile";
    $prev_ip = <S>;
    close S;

    #get the current ip address
    ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname(Sys::Hostname::ghname());

    ($a,$b,$c,$d) = unpack('C4',$addrs[0]);

    $ip = "$a.$b.$c.$d";

    if (!($ip eq $prev_ip)) {

        mark("CHANGE","Updating dynamic IPs");

        ###################################################
        # CUSTOM RECORD UPDATES GO HERE!!!
        ###################################################
        ## repeat for each A Record to be updated
        ##update("http://freedns.afraid.org/dynamic/update.php?your-update-url-here");
        ###################################################

        #update ip file
        open S, ">$ipfile";
        print S $ip;
        close S;
    }
    
    sleep(60);
}


sub update {
    my ($url) = @_;

    $type = "UPDATE";
    
    $ua = new LWP::UserAgent;
    $request = new HTTP::Request('GET', $url);
    $response = $ua->request($request);
    $result = $response->content();

    open  E, ">>$logfile";
    print localtime()."\t$type\t".$result."\n";
    print E localtime()."\t$type\t".$result."\n";
    close E;
}


sub mark {
    my ($type, $message) = @_;
    
    open  E, ">>$logfile";
    print localtime()."\t$type\t".$message."\n";
    print E localtime()."\t$type\t".$message."\n";
    close E;
}

sub clear {
    open  E, ">$logfile";
    close E;
}

