Changeset 647 for trunk/ab5.0
- Timestamp:
- Oct 18, 2008, 9:06:10 PM (16 years ago)
- Location:
- trunk/ab5.0/ablib/src/Classes/System/Net
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/src/Classes/System/Net/IPAddress.ab
r639 r647 3 3 @brief IPアドレスを表すクラス 4 4 @author OverTaker 5 @date 2008/10/ 055 @date 2008/10/18 6 6 */ 7 7 … … 11 11 12 12 Class IPAddress 13 Address As *sockaddr 14 Length As Long 13 item As *Byte 14 size As Long 15 family As DWord 15 16 Public 16 17 17 18 /*! 19 @brief アドレスをバイト配列で取得する 20 @return アドレスを表したバイト列 21 @note このメソッドはライブラリ内のみで使用する目的で作られています。 22 */ 23 Function Address() As *Byte 24 Return item As *Byte 25 End Function 26 27 /*! 18 28 @brief コンストラクタ 19 29 */ 20 30 Sub IPAddress() 21 Address = NULL22 31 End Sub 23 32 … … 27 36 */ 28 37 Sub IPAddress(ip As DWord) 29 Length = SizeOf(sockaddr_in) 30 Dim temp = calloc(Length) As *sockaddr_in 31 temp->sin_addr.s_addr = ip 32 temp->sin_family = AF_INET 33 Address = temp 38 family = AF_INET 39 size = SizeOf(in_addr) 40 item = malloc(size) 41 memcpy(item, VarPtr(ip), size) 34 42 End Sub 35 43 … … 38 46 */ 39 47 Sub ~IPAddress() 40 If Address Then free(Address)48 If item <> NULL Then free(item) 41 49 End Sub 50 51 Function Operator== (address As IPAddress) As Boolean 52 Return Equals(address) 53 End Function 42 54 43 55 /*! … … 46 58 */ 47 59 Function AddressFamily() As Sockets.AddressFamily 48 Return New S ockets.AddressFamily(Address->sa_family, "AddressFamily")60 Return New System.Net.Sockets.AddressFamily(family, "AddressFamily") 49 61 End Function 50 62 … … 53 65 @return スコープ 54 66 */ 55 Function ScopeId() As DWord 56 If AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6 Then 57 Dim sock = Address As *sockaddr_in6 58 Return sock->sin6_scope_id 59 Else 60 Throw New System.Net.Sockets.SocketException("IPAddress.ScopeId: AddressFamily is not IPv6.") 61 End If 62 End Function 63 64 /*! 65 @brief スコープを設定する。 66 @param スコープ 67 */ 68 Sub ScopeId(id As DWord) 69 If id > &HFFFFFFFF Then 70 Throw New ArgumentOutOfRangeException("IPAddress.ScopeId", New Int32(id), "id") 71 End If 72 73 If AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6 Then 74 Dim sock = Address As *sockaddr_in6 75 sock->sin6_scope_id = id 76 Else 77 Throw New System.Net.Sockets.SocketException("IPAddress.ScopeId: AddressFamily is not IPv6.") 78 End If 79 End Sub 80 67 /* Function ScopeId() As DWord 68 End Function 69 */ 81 70 /*! 82 71 @brief リンクローカルアドレスかどうかを取得する。 … … 85 74 */ 86 75 Function IsIPv6LinkLocal() As Boolean 87 If AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6 Then 88 Dim sock = Address As *sockaddr_in6 89 Return (sock->sin6_addr.s6_addr[0] = &HFF) And (sock->sin6_addr.s6_addr[1] = &H00) 76 If family = AF_INET6 Then 77 Return (item[0] = &HFF) And (item[1] = &H00) 90 78 Else 91 79 Throw New System.Net.Sockets.SocketException("IPAddress.IsIPv6LinkLocal: AddressFamily is not IPv6.") … … 99 87 */ 100 88 Function IsIPv6Multicast() As Boolean 101 If AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6 Then 102 Dim sock = Address As *sockaddr_in6 103 Return (sock->sin6_addr.s6_addr[0] = &HFE) And (sock->sin6_addr.s6_addr[1] = &H80) 89 If family = AF_INET6 Then 90 Return (item[0] = &HFE) And (item[1] = &H80) 104 91 Else 105 92 Throw New System.Net.Sockets.SocketException("IPAddress.IsIPv6Multicast: AddressFamily is not IPv6.") … … 111 98 @retval True 等しい 112 99 @retval Flase 等しくない 113 @note 手抜き実装 114 */ 115 Override Function Equals(ip As Object) As Boolean 116 Return This.ToString() = ip.ToString() 100 */ 101 Function Equals(ip As IPAddress) As Boolean 102 If family = ip.family Then 103 Dim i As Long 104 For i = 0 To ELM(size) 105 If item[i] <> ip.item[i] Then Return False 106 Next 107 Return True 108 Else 109 Return False 110 End If 117 111 End Function 118 112 … … 123 117 Override Function ToString() As String 124 118 Dim temp[ELM(64)] As TCHAR 125 If WSAAddressToString(Address As sockaddr, Length, ByVal 0, temp, Len(temp)) Then 126 Throw New System.Net.Sockets.SocketException(WSAGetLastError()) 119 Dim ret As Long 120 Select Case family 121 Case AF_INET 122 Dim addr4 As sockaddr_in 123 addr4.sin_family = family As Integer 124 addr4.sin_addr.s_addr = GetDWord(item) 125 ret = WSAAddressToString(addr4 As sockaddr, SizeOf(sockaddr_in), ByVal 0, temp, Len(temp)) 126 Case AF_INET6 127 Dim addr6 As sockaddr_in6 128 addr6.sin6_family = family As Word 129 memcpy(addr6.sin6_addr.s6_addr, item, size) 130 ret = WSAAddressToString(addr6 As sockaddr, SizeOf(sockaddr_in6), ByVal 0, temp, Len(temp)) 131 Case Else 132 Throw New NotSupportedException("IPAddress.ToString: Unknown Protocol.") 133 End Select 134 135 If ret Then 136 Throw New System.Net.Sockets.SocketException(WSAGetLastError(), "IPAddress.ToString: Failed to WSAAddressToString.") 127 137 Else 128 138 Return New String(temp) 129 139 End If 130 140 End Function 131 132 133 141 134 142 … … 140 148 */ 141 149 Static Function Parse(ip As String) As IPAddress 142 Dim address = New IPAddress143 If TryParse(ip, address) Then144 Return address145 Else 146 Throw New System.Net.Sockets.SocketException(WSAGetLastError() )150 Dim ipAddress = New IPAddress 151 If TryParse(ip, ipAddress) Then 152 Return ipAddress 153 Else 154 Throw New System.Net.Sockets.SocketException(WSAGetLastError(), "IPAddress.Parse: Failed to Parse.") 147 155 End If 148 156 End Function … … 155 163 @retval False 失敗 156 164 */ 157 Static Function TryParse(ip As String, ByRef ipAddress As IPAddress) As Boolean 158 If ipAddress.Address Then free(ipAddress.Address) 159 165 Static Function TryParse(ip As String, ByRef address As IPAddress) As Boolean 166 address = New IPAddress 160 167 If ip.Contains(".") Then 161 ipAddress.Address = Detail.CreateAddressFromIPv4String(ip) 162 ipAddress.Length = SizeOf(sockaddr_in) 168 address.family = AF_INET 169 address.item = Detail.CreateAddressFromIPv4String(ip) 170 address.size = SizeOf(in_addr) 163 171 ElseIf ip.Contains(":") Then 164 ipAddress.Address = Detail.CreateAddressFromIPv6String(ip) 165 ipAddress.Length = SizeOf(sockaddr_in6) 172 address.family = AF_INET6 173 address.item = Detail.CreateAddressFromIPv6String(ip) 174 address.size = SizeOf(in6_addr) 166 175 Else 167 176 Return False 168 177 End If 169 178 170 If ipAddress.AddressThen179 If address.item <> NULL Then 171 180 Return True 172 181 Else 182 address = Nothing 173 183 Return False 174 184 End If … … 196 206 */ 197 207 Static Function Loopback() As IPAddress 198 Return New IPAddress( HostToNetworkOrder(&H FF000000) )208 Return New IPAddress( HostToNetworkOrder(&H7F000001) ) 199 209 End Function 200 210 … … 273 283 274 284 /*! 275 @brief IPv4アドレスを表した文字列から 、IPアドレスが設定されたsockaddr構造体を作成します276 @param IPv4アドレスを表した 文字列277 @return sockaddr構造体を示すアドレス278 */ 279 Function CreateAddressFromIPv4String(ip As String) As * sockaddr280 Dim addr ess = malloc(SizeOf(sockaddr_in)) As *sockaddr_in281 If WSAStringToAddress(ToTCStr(ip), AF_INET, ByVal 0, addr essAs sockaddr, SizeOf(sockaddr_in)) Then285 @brief IPv4アドレスを表した文字列からアドレスを作成する 286 @param IPv4アドレスを表したバイト列 287 @return バイト列 288 */ 289 Function CreateAddressFromIPv4String(ip As String) As *Byte 290 Dim addr4 As sockaddr_in 291 If WSAStringToAddress(ToTCStr(ip), AF_INET, ByVal 0, addr4 As sockaddr, SizeOf(sockaddr_in)) Then 282 292 Return NULL 283 293 Else 284 Return address 285 End If 286 End Function 287 288 /*! 289 @brief IPv6アドレスを表した文字列からIPアドレスが設定されたsockaddr構造体を作成します 290 @param IPv6アドレスを表した文字列 291 @return sockaddr構造体を示すアドレス 292 */ 293 Function CreateAddressFromIPv6String(ip As String) As *sockaddr 294 Dim address = malloc(SizeOf(sockaddr_in6)) As *sockaddr_in6 295 If WSAStringToAddress(ToTCStr(ip), AF_INET6, ByVal 0, address As sockaddr, SizeOf(sockaddr_in6)) Then 294 Dim item = malloc(SizeOf(in_addr)) As *Byte 295 memcpy(item, VarPtr(addr4.sin_addr.s_addr), SizeOf(in_addr)) 296 Return item 297 End If 298 End Function 299 300 /*! 301 @brief IPv6アドレスを表した文字列からアドレスを作成する 302 @param IPv6アドレスを表したバイト列 303 @return バイト列 304 */ 305 Function CreateAddressFromIPv6String(ip As String) As *Byte 306 Dim addr6 As sockaddr_in6 307 If WSAStringToAddress(ToTCStr(ip), AF_INET6, ByVal 0, addr6 As sockaddr, SizeOf(sockaddr_in6)) Then 296 308 Return NULL 297 309 Else 298 Return address 310 Dim item = malloc(SizeOf(in6_addr)) As *Byte 311 memcpy(item, addr6.sin6_addr.s6_addr, SizeOf(in6_addr)) 312 Return item 299 313 End If 300 314 End Function
Note:
See TracChangeset
for help on using the changeset viewer.