« Don't panic. | Main | Mmmm. »

My Newest Thing

Here's an algorithm for Second Lifers. It turns a SL-formatted UUID into an IPv6 address. I'm using it in my project, so feel free to comment if you see places where it could be replaced or enhanced.

string formatIP(key ip)
{
    list temp = llParseString2List(ip, ["-"], []);
    string tempt = llDumpList2String(temp, "");
    string tempth;
    integer i;
    
    for(i=0;i<32;++i)
    {
        if(i!=0&&!(i%4))   //****
            tempth=(tempth="")+tempth+":";
        else
            tempth=(tempth="")+tempth+llGetSubString(tempt,i,i);
    }
    return tempth;
}

**** This was fixed by me. It used to say "32%4==0", and now it says "!(32%4)". I even kept the code valid to star it. xD

TrackBack

TrackBack URL for this entry:
http://www.isowantone.com/mt/mt-tb.cgi/185

Comments

This one has functions for normalising any form of IPv6 address ever. Also hyphenated UUIDs.


string NULL_ADDRESS = "0000:0000:0000:0000:0000:0000:0000:0000";
string LOOPBACK_ADDRESS = "0000:0000:0000:0000:0000:0000:0000:0001";


string Int2Hex(integer x)
{
string hexc="0123456789abcdef";
integer x0 = x & 0xF;
string res = llGetSubString(hexc, x0, x0);
if(( x = ((x >> 4) & 0x0FFFFFFF) ))
{
do{
res = llGetSubString(hexc, x0 = (x & 0xF), x0) + res;
}while(x = x >> 4);
}
return res;
}

string UUID2IPv6(key id)
{
// 00000000-0000-0000-0000-000000000000
// 012345678901234567890123456789012345
return FormatIPv6(
llGetSubString(id, 0, 3 ) + ":" +
llGetSubString(id, 4, 7 ) + ":" +
llGetSubString(id, 9, 12) + ":" +
llGetSubString(id, 14, 17) + ":" +
llGetSubString(id, 19, 22) + ":" +
llGetSubString(id, 24, 27) + ":" +
llGetSubString(id, 28, 31) + ":" +
llGetSubString(id, 32, 35));
}

string FormatIPv6(string ip)
{
if(~llSubStringIndex(ip,"::"))
{
ip = ExpandIPv6(ip);
}
list parts = llParseString2List(ip, [":"], []);
integer i;
for(i = 0; i usingstop - usingstart)
{
usingstart = start;
usingstop = stop;
start = -1;
stop = -1;
}
}
}
if(stop - start > usingstop - usingstart)
{
usingstart = start;
usingstop = stop;
start = -1;
stop = -1;
}
if(usingstart > -1)
{
parts = llListReplaceList(parts, [""], usingstart, usingstop);
if(usingstart == 0)
{
parts = (parts=[]) + "" + parts;
}
}
ip = llDumpList2String(parts,":");
return ip;
}

string ExpandIPv6(string ip)
{
list parts = llParseStringKeepNulls(ip,[":"], []);
if(~llSubStringIndex(ip,"."))
{
string ipv4 = llList2String(parts,-1);
list octets = llParseString2List(ipv4,["."],[]);
list v6ending = [Int2Hex(llList2Integer(octets,0)) + Int2Hex(llList2Integer(octets,1)),
Int2Hex(llList2Integer(octets,2)) + Int2Hex(llList2Integer(octets,3))];
parts = llList2List(parts,0,-2) + v6ending;
}
integer length = llGetListLength(parts);
integer i;
for(i = 0; i 0; missing -= 4)
{
toadd += "0000:";
}
ip = llDumpList2String(llParseStringKeepNulls(ip,["::"],[]),toadd);
if(llStringLength(ip) != 39)
{
llSay(DEBUG_CHANNEL, "Invalid IP address!");
return NULL_ADDRESS;
}
return ip;
}

default
{
state_entry()
{
llSay(0,(string)llGetOwner()+" -convert-> "+UUID2IPv6(llGetOwner()));
llSay(0,(string)llGetKey()+" -convert-> "+UUID2IPv6(llGetKey()));
llSay(0,"::1 -expand-> "+ExpandIPv6("::1"));
llSay(0,"2001:0db8:0000:0000:0000:0000:1428:57ab -format-> "+FormatIPv6("2001:0db8:0000:0000:0000:0000:1428:57ab"));
llSay(0,"::ffff:12.34.56.78 -format-> "+FormatIPv6("::ffff:12.34.56.78"));
}
}

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)