summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Ehlers <danielehlers@mindeye.net>2012-10-04 00:16:01 +0200
committerDaniel Ehlers <danielehlers@mindeye.net>2012-10-04 00:16:01 +0200
commitd64e512dd0e3e1da90759f6f1bc086f3c393590d (patch)
treec97ddff1709064d881a5bcc6305988e0db202c6f
parent427cd598938a994669fb050bf4d52723ef532dcc (diff)
Simple MAC-IPv6 Address convertion script
-rw-r--r--ipaddrcalc.html563
1 files changed, 563 insertions, 0 deletions
diff --git a/ipaddrcalc.html b/ipaddrcalc.html
new file mode 100644
index 0000000..1cc5747
--- /dev/null
+++ b/ipaddrcalc.html
@@ -0,0 +1,563 @@
+<html>
+<head>
+<title>IPv6 Address Calculator</title>
+
+</head>
+
+<body>
+<h1>Address Calculator</h1>
+
+<p id="warn" class="warning">Warning: you need Javascript to use this form! Please activate it.</p>
+
+<!-- ++++++++++++++++ IPv6 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
+<a name="ip6"/>
+
+<h2>MAC -- EUI-64 Converter</h2>
+
+With Auto-Configuration the 64-bit host ID (also called EUI-64 in IPv6 speak) of
+an IPv6 address is generated from the MAC address of the network card. This tool
+allows to convert between MAC and EUI-64. Additional the Freifunk Kiel Network
+Prefix is applied.<p/>
+
+<table>
+<tr><td>48bit MAC:</td><td><input id="mac48" type="text" size="30" value="00-11-22-33-44-55"/></td><td>Please use dashes or colons to separate bytes</td></tr>
+<tr><td>64bit Host ID:</td><td><input id="eui64" type="text" size="30" value="::211:22ff:fe33:4455"/></td><td>Please use IPv6 notation with ::/64 as prefix</td></tr>
+</table><br/>
+<input type="button" value="MAC -> Host ID" onClick="mactoeui()"/><br/>
+
+<!-- +++++++++++++++++++++++++++++++ end notes ++++++++++++++++++++++++++++++++++++++++++++++ -->
+<hr/>
+
+<p class="copy"><b><i>JS Addr Calc</i></b> revision 20120802<br/>&copy; Konrad
+Rosenbaum, 2012<br/>This script is protected under the <a
+ href="http://www.gnu.org/licenses/gpl.html" target="_blank">GNU GPL version
+ 3</a> or at your option any newer. </p>
+<p class="copy">
+This is a stripped down version of the <a href="http://silmor.de/ipaddrcalc.html">original</a> script!</p>
+
+<p class="copy">Please mail patches to me (&#x6b;on<!-- obfuscated -->rad<!-- -->&#x40;<!-- -->sil<!-- ly -->&#x6d;or&#x2e;<!-- ty -->d<!-- yup -->e) if you have any interesting additions for it.</p></font>
+
+
+<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+Below are the actual scripts that make the forms above work.
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
+<script language="JavaScript">
+<!--
+//hide the warning, JavaScript works...
+document.getElementById("warn").style.display="none";
+
+// //////////////////////////////////////////////
+// generic functions
+
+//convert hex to decimal
+function hex2dec(val)
+{
+ return parseInt("0x"+val);
+}
+
+function dec2hex(val)
+{
+ var str="";
+ var minus=false;
+ if(val<0){minus=true;val*=-1;}
+ val=Math.floor(val);
+ while(val>0){
+ var v=val%16;
+ val/=16;val=Math.floor(val);
+ switch(v){
+ case 10:v="A";break;
+ case 11:v="B";break;
+ case 12:v="C";break;
+ case 13:v="D";break;
+ case 14:v="E";break;
+ case 15:v="F";break;
+ }
+ str=v + str;
+ }
+ if(str=="")str="0";
+ if(minus)str="-"+str;
+ return str;
+}
+
+function dec2hexlen(val,minlen)
+{
+ var str=dec2hex(val);
+ while(str.length<minlen)str="0"+str;
+ return str;
+}
+
+//generate a null ipv6 array
+function ip6null()
+{
+ var ar=new Array;
+ for(var i=0;i<8;i++)ar[i]=0;
+ return ar;
+}
+
+//convert ipv6 string to array
+function parseIp6(str)
+{
+ //init
+ var ar=new Array;
+ for(var i=0;i<8;i++)ar[i]=0;
+ //check for trivial IPs
+ if(str=="::")return ar;
+ //parse
+ var sar=str.split(':');
+ var slen=sar.length;
+ if(slen>8)slen=8;
+ var j=0;
+ for(var i=0;i<slen;i++){
+ //this is a "::", switch to end-run mode
+ if(i && sar[i]==""){j=9-slen+i;continue;}
+ ar[j]=parseInt("0x0"+sar[i]);
+ j++;
+ }
+
+ return ar;
+}
+
+//convert ipv6 array to string
+function ip6toString(ar)
+{
+ //init
+ var str="";
+ //find longest stretch of zeroes
+ var zs=-1,zsf=-1;
+ var zl=0,zlf=0;
+ var md=0;
+ for(var i=0;i<8;i++){
+ if(md){
+ if(ar[i]==0)zl++;
+ else md=0;
+ }else{
+ if(ar[i]==0){zs=i;zl=1;md=1;}
+ }
+ if(zl>2 && zl>zlf){zlf=zl;zsf=zs;}
+ }
+ //print
+ for(var i=0;i<8;i++){
+ if(i==zsf){
+ str+="";
+ i+=zlf-1;
+ if(i>=7)str+=":";
+ continue;
+ }
+ if(i)str+=":";
+ str+=dec2hex(ar[i]);
+ }
+// alert("printv6 str="+str+" zsf="+zsf+" zlf="+zlf);
+ return str;
+}
+
+//create a mask from a prefix
+function ip6prefixToMask(prf)
+{
+ var ar=new Array;
+ for(var i=0;i<8;i++){
+ if(prf>=16)ar[i]=0xffff;
+ else switch(prf){
+ case 1:ar[i]=0x8000;break;
+ case 2:ar[i]=0xc000;break;
+ case 3:ar[i]=0xe000;break;
+ case 4:ar[i]=0xf000;break;
+ case 5:ar[i]=0xf800;break;
+ case 6:ar[i]=0xfc00;break;
+ case 7:ar[i]=0xfe00;break;
+ case 8:ar[i]=0xff00;break;
+ case 9:ar[i]=0xff80;break;
+ case 10:ar[i]=0xffc0;break;
+ case 11:ar[i]=0xffe0;break;
+ case 12:ar[i]=0xfff0;break;
+ case 13:ar[i]=0xfff8;break;
+ case 14:ar[i]=0xfffc;break;
+ case 15:ar[i]=0xfffe;break;
+ default:ar[i]=0;break;
+ }
+ prf-=16;
+ }
+ return ar;
+}
+
+function ip6mask(ip,prf)
+{
+ if(typeof(prf)=="number")prf=ip6prefixToMask(prf);
+ var ip2=new Array;
+ for(var i=0;i<8;i++)ip2[i] = ip[i] & prf[i];
+ return ip2;
+}
+
+function ip6maskHost(ip,prf)
+{
+ if(typeof(prf)=="number")prf=ip6prefixToMask(prf);
+ else prf=ip6copy(prf);
+ for(var i=0;i<8;i++)prf[i]=(~prf[i])&0xffff;
+ var ip2=new Array;
+ for(var i=0;i<8;i++)ip2[i] = ip[i] & prf[i];
+ return ip2;
+}
+
+function ip6copy(ip)
+{
+ var r=new Array;
+ for(var i=0;i<8;i++)r[i]=ip[i];
+ return r;
+}
+
+function ip6shiftleft(ip,left)
+{
+ if(left==0)return ip;
+ if(left<0)return ip6shiftright(ip,-left);
+ if(left>=128)return ip6null();
+ var legs=Math.floor(left/16);
+ var bits=Math.floor(left&0xf);
+ var r=ip6null()
+ for(var i=0;(i+legs)<8;i++){
+ //move by legs
+ r[i]=ip[i+legs];
+ //move by bits
+ if(bits<=0)continue;
+ if(i)r[i-1]|=Math.floor(r[i]>>(16-bits));
+ r[i]<<=bits;
+ r[i]&=0xffff;
+ }
+ return r;
+}
+
+function ip6shiftright(ip,right)
+{
+ if(right==0)return ip;
+ if(right<0)return ip6shiftleft(ip,-right);
+ if(right>=128)return ip6null();
+ var legs=Math.floor(right/16);
+ var bits=Math.floor(right&0xf);
+ var r=ip6null()
+ for(var i=7;i>=legs;i--){
+ //move by legs
+ r[i]=ip[i-legs];
+ //move by bits
+ if(bits<=0)continue;
+ if(i<=7)r[i+1]|=Math.floor(r[i]<<(16-bits))&0xffff;
+ r[i]>>=bits;
+ r[i]&=0xffff;
+ }
+ return r;
+}
+
+function ip6merge(net,host,prefix)
+{
+ net=ip6mask(net,prefix);
+ host=ip6maskHost(host,prefix);
+ for(var i=0;i<8;i++)net[i] |= host[i];
+ return net;
+}
+
+// //////////////////////////////////////////////
+// hide and seek^W show
+function showIp4()
+{
+ document.getElementById("ip4show").style.display="none";
+ document.getElementById("ip4").style.display="block";
+}
+function hideIp4()
+{
+ document.getElementById("ip4show").style.display="block";
+ document.getElementById("ip4").style.display="none";
+}
+
+function showIp46()
+{
+ document.getElementById("ip46show").style.display="none";
+ document.getElementById("ip46").style.display="block";
+}
+function hideIp46()
+{
+ document.getElementById("ip46show").style.display="block";
+ document.getElementById("ip46").style.display="none";
+}
+
+function showIp6()
+{
+ document.getElementById("ip6show").style.display="none";
+ document.getElementById("ip6").style.display="block";
+}
+function hideIp6()
+{
+ document.getElementById("ip6show").style.display="block";
+ document.getElementById("ip6").style.display="none";
+}
+
+// //////////////////////////////////////////////
+// IPv4 functions
+
+//used by URLs to set IPv4 address from a specific example
+function setip4(addr,prefix)
+{
+ document.getElementById("ip4addr").value=addr;
+ document.getElementById("ip4prefix").value=prefix;
+ setIp4ByPrefix();
+ return false;
+}
+
+//called if the address or prefix changes
+function setIp4ByPrefix()
+{
+ calcIp4Mask();
+ calcIp4Base();
+}
+
+//called if the mask changes
+function setIp4ByMask()
+{
+ calcIp4Prefix();
+ calcIp4Base();
+}
+
+function calcIp4Mask()
+{
+ var prf=document.getElementById("ip4prefix").value;
+ if(prf>32)prf=32;if(prf<1)prf=1;
+ var msk="";
+ for(var i=0;i<4;i++){
+ if(i)msk+=".";
+ if(prf>8){
+ msk += "255";
+ prf-=8;
+ continue;
+ }
+ if(prf<=0){
+ msk += "0";
+ continue;
+ }
+ m=256-(1<<(8-prf));
+ msk+=m;
+ prf-=8;
+ }
+ document.getElementById("ip4mask").value=msk;
+ document.getElementById("ip4mask").style.color="black";
+}
+
+function calcIp4Prefix()
+{
+ var msk=document.getElementById("ip4mask").value.split(".");
+ document.getElementById("ip4mask").style.color="black";
+ var prf=0;
+ for(var i=0;i<4;i++){
+ switch(parseInt(msk[i])){
+ case 0:prf+=0;break;
+ case 128:prf+=1;break;
+ case 192:prf+=2;break;
+ case 224:prf+=3;break;
+ case 240:prf+=4;break;
+ case 248:prf+=5;break;
+ case 252:prf+=6;break;
+ case 254:prf+=7;break;
+ case 255:prf+=8;break;
+ default:document.getElementById("ip4mask").style.color="red";break;
+ }
+ }
+ document.getElementById("ip4prefix").value=prf;
+}
+
+function calcIp4Base()
+{
+ var ip=document.getElementById("ip4addr").value.split('.');
+ var msk=document.getElementById("ip4mask").value.split('.');
+ var base="";
+ var first="",last="",bcast="",dgw="";
+ for(var i=0;i<4;i++){
+ if(i){base+=".";first+=".";last+=".";dgw+=".";bcast+=".";}
+ b = parseInt(ip[i]) & parseInt(msk[i]);
+ base += "" + b;
+ l=b | (255-parseInt(msk[i]));
+ bcast+=l;
+ if(i<3){
+ first+=b;
+ last+=l;
+ dgw+=l;
+ }else{
+ first+=b+1;
+ last+=l-2;
+ dgw+=l-1;
+ }
+ }
+ document.getElementById("ip4base").innerHTML=base;
+ var prf=parseInt(document.getElementById("ip4prefix").value);
+ if(prf<=30){
+ document.getElementById("ip4first").innerHTML=first;
+ document.getElementById("ip4last").innerHTML=last;
+ document.getElementById("ip4bcast").innerHTML=bcast;
+ document.getElementById("ip4gw").innerHTML=dgw;
+ }else if(prf==31){
+ document.getElementById("ip4first").innerHTML=base;
+ document.getElementById("ip4last").innerHTML=bcast;
+ document.getElementById("ip4bcast").innerHTML="n/a";
+ document.getElementById("ip4gw").innerHTML="n/a";
+ }else{
+ document.getElementById("ip4first").innerHTML=base;
+ document.getElementById("ip4last").innerHTML=base;
+ document.getElementById("ip4bcast").innerHTML="n/a";
+ document.getElementById("ip4gw").innerHTML="n/a";
+ }
+}
+
+
+// ///////////////////////////////////////////////////////
+// IPv4 to IPv6 translation functions
+
+function teredocalc()
+{
+ var tip=parseIp6(document.getElementById("teredo").value);
+ var prefix=ip6toString(ip6mask(tip,ip6prefixToMask(32)))+"/32";
+ if(tip[0]!=0x2001 || tip[1]!=0)prefix+=" not a Teredo address!";
+ document.getElementById("teredo-prefix").innerHTML=prefix;
+ var ip4 = (tip[2]>>8) + "." + (tip[2]&0xff) + "." + (tip[3]>>8) + "." + (tip[3]&0xff);
+ document.getElementById("teredo-server").innerHTML=ip4;
+ ip4 = (tip[6]>>8^0xff) + "." + (tip[6]&0xff^0xff) + "." + (tip[7]>>8^0xff) + "." + (tip[7]&0xff^0xff);
+ document.getElementById("teredo-ip").innerHTML =ip4;
+ document.getElementById("teredo-port").innerHTML =tip[5]^0xffff;
+ var flags="";
+ if(tip[4]&0x8000)flags="Cone NAT";else flags="Non Cone NAT";
+ flags+=" ("+tip[4]+")";
+ document.getElementById("teredo-flags").innerHTML =flags;
+}
+teredocalc();
+
+function ip6fromip4(ip4str)
+{
+ var ip4=ip4str.split(".");
+ var ip6=ip6null();
+ ip6[6]=parseInt(ip4[0])<<8 | parseInt(ip4[1]);
+ ip6[7]=parseInt(ip4[2])<<8 | parseInt(ip4[3]);
+ return ip6;
+}
+
+function ipcnvfrom4()
+{
+ var ip6=ip6fromip4(document.getElementById("ipcnv4").value);
+ document.getElementById("ipcnv6").value=ip6toString(ip6);
+}
+
+function ipcnvfrom6()
+{
+ var ip6=parseIp6(document.getElementById("ipcnv6").value);
+ var ip4=(ip6[6]>>8)+"."+(ip6[6]&0xff)+"."+(ip6[7]>>8)+"."+(ip6[7]&0xff);
+ document.getElementById("ipcnv4").value=ip4;
+}
+
+function set6to4()
+{
+ document.getElementById("64net").value="2002::";
+ document.getElementById("64netprf").value="16";
+ conv64fromisp();
+}
+
+function conv64fromisp()
+{
+ var pprf=parseInt(document.getElementById("64netprf").value);
+ var ip6=ip6mask(parseIp6(document.getElementById("64net").value),pprf);
+ //convert ip4 to ip6
+ var ip4=document.getElementById("64ip4").value;
+ var ip4bits=parseInt(document.getElementById("64ip4bits").value);
+ if(ip4bits>32)ip4bits=32;
+ if(ip4bits<1)ip4bits=1;
+ var part=ip6shiftleft(ip6fromip4(ip4),128-ip4bits);
+ part=ip6shiftright(part,pprf);
+ document.getElementById("64ip6").value=ip6toString(ip6merge(ip6,part,pprf));
+ document.getElementById("64ip6len").value=ip4bits+pprf;
+}
+
+function conv64toisp()
+{
+ var ip6=parseIp6(document.getElementById("64ip6").value);
+ var ip6prf=parseInt(document.getElementById("64ip6len").value);
+ var pprf=parseInt(document.getElementById("64netprf").value);
+ document.getElementById("64net").value=ip6toString(ip6mask(ip6,pprf));
+ ip6=ip6shiftright(ip6maskHost(ip6,pprf),128-ip6prf);
+ var ip4=(ip6[6]>>8)+"."+(ip6[6]&0xff)+"."+(ip6[7]>>8)+"."+(ip6[7]&0xff);
+ document.getElementById("64ip4").value=ip4;
+ var ip4len=ip6prf-pprf;
+ if(ip4len>32)ip4len=32;
+ document.getElementById("64ip4bits").value=ip4len;
+}
+
+// ///////////////////////////////////////////////////////
+// IPv6 functions
+
+function mactoeui()
+{
+ var mac=document.getElementById("mac48").value.replace(/:/g,"-").split("-");
+ if(mac.length!=6){
+ alert("Not a MAC address.");
+ return;
+ }
+ var ip6=ip6null();
+ ip6[4]=hex2dec(mac[0])<<8 | hex2dec(mac[1]);
+ ip6[4] ^= 0x200;
+ ip6[5]=hex2dec(mac[2])<<8 | 0xff;
+ ip6[6]=hex2dec(mac[3]) | 0xfe00;
+ ip6[7]=hex2dec(mac[4])<<8 | hex2dec(mac[5]);
+ document.getElementById("eui64").value="fda1:384a:74de:4242"+ ip6toString(ip6);
+}
+
+function extractmac(ip6)
+{
+ if((ip6[5]&0xff)!=0xff || (ip6[6]>>8)!=0xfe)return "(no MAC)";
+ var mac=dec2hexlen((ip6[4]>>8)^2,2)+"-";
+ mac+=dec2hexlen(ip6[4]&0xff,2)+"-";
+ mac+=dec2hexlen(ip6[5]>>8,2)+"-";
+ mac+=dec2hexlen(ip6[6]&0xff,2)+"-";
+ mac+=dec2hexlen(ip6[7]>>8,2)+"-";
+ mac+=dec2hexlen(ip6[7]&0xff,2);
+ return mac;
+}
+
+function euitomac()
+{
+ document.getElementById("mac48").value=extractmac(parseIp6(document.getElementById("eui64").value));
+}
+
+function splitUpIp6()
+{
+ var ip6=parseIp6(document.getElementById("ip6full").value);
+ var prf=parseInt(document.getElementById("ip6fprefix").value);
+ document.getElementById("ip6nprefix").value=prf;
+ document.getElementById("ip6net").value=ip6toString(ip6mask(ip6,prf));
+ document.getElementById("ip6host").value=ip6toString(ip6maskHost(ip6,prf));
+ document.getElementById("ip6mac").innerHTML=extractmac(ip6);
+}
+
+function joinUpIp6()
+{
+ var prf=parseInt(document.getElementById("ip6nprefix").value);
+ document.getElementById("ip6fprefix").value=prf;
+ var ip6n=parseIp6(document.getElementById("ip6net").value);
+ var ip6h=parseIp6(document.getElementById("ip6host").value);
+ document.getElementById("ip6full").value=ip6toString(ip6merge(ip6n,ip6h,prf));
+ document.getElementById("ip6mac").innerHTML=extractmac(ip6h);
+}
+
+// end of scripts
+// ///////////////////////////////////////////////////////
+
+-->
+</script>
+
+<style>
+.copy {
+ color: black;
+ font-size: 80%;
+}
+.warning {
+ color: darkred;
+ font-size: 120%;
+}
+.hidden {
+ display: none;
+}
+
+</style>
+
+</body></html>