Object DNS

This is a very simple Object DNS server written in PHP backed with an SQL database I've included an LSL script that can be dropped into an object and used to register DNS, instead of having to write DNS code into every LSL script. It utilizes link messages to communicate with the calling script. You must provide your own URL request/release code in your scripts, as it appears that only the script that called for a particular URL, will receive http_response events for that URL.

LSL

Object DNS

////OBJECT DNS FRAMEWORK////
/////////By Zetaphor////////
//This is a simple framework for LSL HTTP Server DNS
//The script is utilizing a PHP/SQL setup on my webserver
/////////////////////////////////////////////////////

////////////////////////////////////////////////////
///////////////USAGE INSTRUCTIONS///////////////////
//This script does not handle the request/release of URL's,
//you must handle that in your script, this simply handles
//registering a DNS name on my server with your current URL
////////////////////////////////////////////////////

//To register an object, follow the below steps:
//    Send a linkmessage to this prim with the integer 100
//    The string will contain the desired DNS name, and your
//    current object URL, separated by a carat (^)
//      Example: llMessageLinked(LINK_THIS,100,"TestObject^"+CurrentURL,"");
//    This will either create your DNS Name and URL on the server
//    or update your name with your latest URL.
//
//
//    The object will then send the following link message
//    on failure/success:
//      llMessageLinked(LINK_THIS,201,ErrorData,""); //Failure, errordata contains the response
//      llMessageLinked(LINK_THIS,200,"",""); //Success!

string url;
key url_register;
string name;
default
{
    link_message(integer s, integer n, string str, key id)
    {
        if (n==100)
        {
            llReleaseURL(url);
            list temp = llParseString2List(str,["^"],[""]);
            name=llList2String(temp,0);
            url=llList2String(temp,1);
            url_register = llHTTPRequest("http://yoursite.com/lsl_object_dns.php",[HTTP_METHOD,"POST",HTTP_MIMETYPE, "application/x-www-form-urlencoded"],"table=dns&name="+name+"&URL="+llEscapeURL(url)+"&method=set");            
        }
    }

    http_response(key id, integer status, list meta, string body)
    {
        if (id==url_register)
        {
            if (llSubStringIndex(body,"Error!")!=-1)
            {
                llMessageLinked(LINK_SET,201,body,"");
                llResetScript();                
            }
            else
            {
                llMessageLinked(LINK_SET,200,"","");
            }
        }
    }
}

DNS Register Example

string DNSName="Change Me!";
string url;
integer CHANGED_URL = 1792;

default
{
    state_entry()
    {
        llRequestURL();
    }

    changed(integer c)
    {
        if (c & (CHANGED_URL) )llRequestURL(); 
    }

    link_message(integer s, integer n, string str, key id)
    {
        if (n==201)
        {
            llSay(0,"Error registering DNS!\n"+str);
        }
        else if (n==200)
        {
            llSay(0,"DNS Registered Succesfully!");
        }
    }

    http_request(key id, string method, string body)
    {
        if (method == URL_REQUEST_GRANTED)
        {
            url = body;
            llSay(0,"Local URL Recieved! Registering with DNS...");
            llMessageLinked(LINK_THIS,100,DNSName+"^"+url,"");
        }
        else if (method == URL_REQUEST_DENIED)
        {
            llSay(0, "Something went wrong, no url. " + body);
        }
        else if (method == "GET")
        {
            llHTTPResponse(id,200,"Hello World!");
        }
        else
        {
            llHTTPResponse(id,405,"Unsupported Method");
        }
    }
}

PHP

DNS Backend

<?php
$username = "username";
$password = "password";
$database = "db";
$method = $_POST['method'];
$name = $_POST['name'];
$name = urldecode($name);
$URL = $_POST['URL'];
$table = $_POST['table'];
 
function recordExists($idval,$table) {//check for id=idval in table and return TRUE or FALSE
   $result = mysql_query("SELECT * FROM ".$table." WHERE name='".$idval."'") or die(mysql_error());
   if($row = mysql_fetch_array($result)) {//if we did return a record
      return 1;
   }//end if row
   return 0;
}//end fuction recordExists
 
if ($name=="")
{
    die("No Object name provided!");
}
else if ($URL=="" && $method!="get" && $method!="remove")
{
    die("No Object URL provided!");
}
else if (method=="")
{
    die("No Method provided!");
}
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
if ($method=="set")
{
    if (recordExists($name,$table)==0)
    {
        $query = "INSERT INTO ".$table." VALUES ('$name','$URL')";
        $result=mysql_query($query);
        if ($result==1)
        {
            echo "DNS record created!";
        }
        else
        {
            echo "Error!\n".$result;
        }
    }
    else
    {
        $query = "UPDATE ".$table." SET url='$URL' WHERE name='$name'";
        $result=mysql_query($query);
        if ($result==1)
        {
            echo "DNS record Updated!";
        }
        else
        {
            echo "Error!\n".$result;
        }
    }
}
else if ($method=="get")
{
    $query="SELECT url FROM ".$table." WHERE name='$name'";
    $result=mysql_query($query);
    $row = mysql_fetch_array($result);
    echo $row[0];
}
else if (method=="remove")
{
    $query="DELETE FROM ".$table." WHERE name='$name'";
    $result=mysql_query($query);
    print_f($query); 
}
mysql_close();
?>

SQL

 CREATE TABLE `objectdns`.`dns_example` (
`name` TEXT NOT NULL ,
`url` TEXT NOT NULL
) ENGINE = MYISAM
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License