dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/System/Net IPAddress.cs,1.10,1.11 IP


From: Gopal.V <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/System/Net IPAddress.cs,1.10,1.11 IPv6Address.cs,1.1,NONE
Date: Tue, 27 May 2003 12:27:48 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/System/Net
In directory subversions:/tmp/cvs-serv23422/System/Net

Modified Files:
        IPAddress.cs 
Removed Files:
        IPv6Address.cs 
Log Message:
Remove IPv6Address and implement all it had into IPAddress


Index: IPAddress.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System/Net/IPAddress.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -r1.10 -r1.11
*** IPAddress.cs        27 May 2003 07:54:25 -0000      1.10
--- IPAddress.cs        27 May 2003 16:27:46 -0000      1.11
***************
*** 25,29 ****
--- 25,31 ----
  
  using System;
+ using System.Text;
  using System.Net.Sockets;
+ using System.Globalization;
  using System.Runtime.CompilerServices;
  
***************
*** 94,98 ****
                                }
                        }
! 
        // Determine if two objects are equal.
        public override bool Equals(Object comparand)
--- 96,108 ----
                                }
                        }
!                       
!       public IPAddress(ushort[] address, long scopeid)
!                       {
!                               this.family = AddressFamily.InterNetworkV6;
!                               this.value = scopeid;
!                               this.ipv6=new ushort[8];
!                               Array.Copy(address,this.ipv6,8);
!                       }
!       
        // Determine if two objects are equal.
        public override bool Equals(Object comparand)
***************
*** 204,212 ****
        extern public static short NetworkToHostOrder(short network);
  
!       [TODO]
!       public static IPAddress Parse(String ipString)
                        {
-                               // TODO: ipv6 support
- 
                                int parsed;
                                String[]  tokenizedString;
--- 214,219 ----
        extern public static short NetworkToHostOrder(short network);
  
!       private static IPAddress ParseIPv4(String ipString)
                        {
                                int parsed;
                                String[]  tokenizedString;
***************
*** 216,226 ****
                                int quadD;
                                bool  numbersign;
! 
!                               if (ipString == null)
!                               {
!                                       throw new ArgumentNullException
!                                               ("ipString", 
S._("Arg_NotNull"));
!                               }
! 
                                // this only takes char[]. not String
                                tokenizedString = ipString.Split(new 
char[]{'.'}, 4);
--- 223,227 ----
                                int quadD;
                                bool  numbersign;
!                               
                                // this only takes char[]. not String
                                tokenizedString = ipString.Split(new 
char[]{'.'}, 4);
***************
*** 249,261 ****
  
                                return new 
IPAddress((long)(uint)HostToNetworkOrder(parsed));
                        }       
        public override string ToString()
                        {
!                               // TODO: ipv6 support
!                               int host = NetworkToHostOrder((int)value);
!                               return ((host >> 24) & 0xFF).ToString() + "." +
!                                          ((host >> 16) & 0xFF).ToString() + 
"." +
!                                          ((host >> 8) & 0xFF).ToString() + 
"." +
!                                          (host & 0xFF).ToString();
                        }
  
--- 250,424 ----
  
                                return new 
IPAddress((long)(uint)HostToNetworkOrder(parsed));
+                       }
+       
+       // IPv6 Parsing
+       
+       private static ushort[] ParseHex(String input)
+                       {
+                               if(input==String.Empty)
+                               {
+                                       return new ushort[0];
+                               }
+                               String [] toks = input.Split(':');
+                               ushort [] retval = new ushort[toks.Length];
+                               for(int i=0; i< toks.Length; i++)
+                               {
+                                       try
+                                       {
+                                               retval[i] =
+                                                       
UInt16.Parse("0x"+toks[i],NumberStyles.HexNumber);
+                                       }
+                                       catch(OverflowException)
+                                       {
+                                               throw new 
FormatException(S._("Format_IP"));
+                                       }
+                               }
+                               return retval;
+                       }
+               
+       private static ushort[] ParseNoCompress(String input)
+                       {
+                               if(input==String.Empty)
+                               {
+                                       return new ushort[0];
+                               }
+                               int ipv4Start=input.IndexOf(".");
+                               int lastPart = input.LastIndexOf(":");
+                               String ip4 = null;
+                               ushort [] retval;
+                               if(ipv4Start!=-1 && lastPart!=-1)
+                               {
+                                       // F00F::0:13.2.3.4
+                                       if(lastPart > ipv4Start)
+                                       {
+                                               throw new 
FormatException(S._("Format_IP"));
+                                       }
+                                       ip4 = 
input.Substring(input.LastIndexOf(":")+1);
+                                       input = input.Substring(0, 
input.LastIndexOf(":"));
+                               }
+                               else if(ipv4Start!=-1 && lastPart == -1)
+                               {
+                                       // F00F::13.2.2.3
+                                       ip4 = 
input.Substring(input.LastIndexOf(":")+1);
+                                       input = String.Empty;
+                               }
+                               ushort[] hex4 = ParseHex(input);
+                               
+                               retval = new ushort[hex4.Length + ((ip4!=null) 
? 2 : 0)];
+                               
+                               Array.Copy(hex4,retval,hex4.Length);
+ 
+                               if(ip4!=null)
+                               {
+                                       long ipValue = ParseIPv4(ip4).value; 
+                                       retval [hex4.Length] = (ushort) 
+                                                                               
(((int) (ipValue & 0xff) << 8) + 
+                                                                               
((int) ((ipValue >> 8) & 0xff)));
+                                                                               
                
+                                       retval [hex4.Length + 1] = (ushort) 
+                                                                       (((int) 
((ipValue >> 16) & 0xff) << 8)
+                                                                       + 
((int) ((ipValue >> 24) & 0xff)));
+                               }
+                               return retval;
+                       }
+       
+       public static IPAddress ParseIPv6(String input)
+                       {
+                               long scopeid=0;
+                               bool zeroCompress, multipleCompress ;
+ 
+                               if(input==null)
+                               {
+                                       throw new 
ArgumentNullException("input");
+                               }
+                               if(input.IndexOf('/')!=-1)
+                               {
+                                       String 
prefix=input.Substring(input.IndexOf('/')+1);
+                                       try
+                                       {
+                                               scopeid=Int64.Parse(prefix);
+                                       }
+                                       catch
+                                       {
+                                               throw new 
FormatException("Invalid prefix");
+                                       }
+                                       
input=input.Substring(0,input.IndexOf('/'));
+                               }
+                               
+                               zeroCompress = (input.IndexOf("::") != -1);
+                               multipleCompress = 
+                                                       (input.IndexOf("::") != 
input.LastIndexOf("::"));
+                               if(!zeroCompress)
+                               {
+                                       ushort[] retval=ParseNoCompress(input);
+                                       if(retval.Length != 8)
+                                       {
+                                               throw new 
FormatException(S._("Format_IP"));
+                                       }
+                                       return new IPAddress(retval,scopeid);
+                               }
+                               else if(!multipleCompress)
+                               {
+                                       String 
part1=input.Substring(0,input.IndexOf("::"));
+                                       String 
part2=input.Substring(input.IndexOf("::")+2);
+ 
+                                       // Parse the two peices independently 
+                                       ushort[] retval1=ParseNoCompress(part1);
+                                       ushort[] retval2=ParseNoCompress(part2);
+                                       
+                                       if((retval1.Length + retval2.Length) >= 
8)
+                                               throw new 
FormatException(S._("Format_IP"));
+ 
+                                       ushort[] retval = new 
ushort[8]{0,0,0,0,0,0,0,0};
+ 
+                                       // fill in the peices from either end , 
leaving the
+                                       // zero block to remain between.
+ 
+                                       Array.Copy(retval1, retval, 
retval1.Length);
+                                       Array.Copy(retval2, 0, retval, 
8-retval2.Length, 
+                                                                               
                        retval2.Length);
+                                       return new IPAddress(retval,scopeid);
+                               }
+                               throw new FormatException(S._("Format_IP"));
+                       }
+ 
+       public static IPAddress Parse(String ipString)
+                       {
+                               if (ipString == null)
+                               {
+                                       throw new ArgumentNullException
+                                               ("ipString", 
S._("Arg_NotNull"));
+                               }
+                               if(ipString.IndexOf(":")!=-1)
+                               {
+                                       return ParseIPv6(ipString);
+                               }
+                               else
+                               {
+                                       return ParseIPv4(ipString);
+                               }
                        }       
+       
        public override string ToString()
                        {
!                               if(family==AddressFamily.InterNetworkV6)
!                               {
!                                       StringBuilder sb=new StringBuilder(8*5);
!                                       for(int i=0;i<7;i++)
!                                       {
!                                               
sb.Append(ipv6[i].ToString("X4"));
!                                               sb.Append('.');
!                                       }
!                                       sb.Append(ipv6[7].ToString("X4"));
!                                       return sb.ToString();
!                               }
!                               else
!                               {
!                                       int host = 
NetworkToHostOrder((int)value);
!                                       return ((host >> 24) & 0xFF).ToString() 
+ "." +
!                                                  ((host >> 16) & 
0xFF).ToString() + "." +
!                                                  ((host >> 8) & 
0xFF).ToString() + "." +
!                                                  (host & 0xFF).ToString();
!                               }
                        }
  

--- IPv6Address.cs DELETED ---





reply via email to

[Prev in Thread] Current Thread [Next in Thread]