Changeset 647 for trunk/ab5.0


Ignore:
Timestamp:
Oct 18, 2008, 9:06:10 PM (16 years ago)
Author:
OverTaker
Message:

IPAddress修正。SocketAddressクラスを実装。IPAdderssのスコープがよくわからないので、とりあえずコメントアウト

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  
    33    @brief  IPアドレスを表すクラス
    44    @author OverTaker
    5     @date   2008/10/05
     5    @date   2008/10/18
    66*/
    77
     
    1111
    1212Class IPAddress
    13     Address As *sockaddr
    14     Length As Long
     13    item As *Byte
     14    size As Long
     15    family As DWord
    1516Public
    1617
    1718    /*!
     19    @brief  アドレスをバイト配列で取得する
     20    @return アドレスを表したバイト列
     21    @note   このメソッドはライブラリ内のみで使用する目的で作られています。
     22    */
     23    Function Address() As *Byte
     24        Return item As *Byte
     25    End Function
     26
     27    /*!
    1828    @brief コンストラクタ
    1929    */
    2030    Sub IPAddress()
    21         Address = NULL
    2231    End Sub
    2332
     
    2736    */
    2837    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)
    3442    End Sub
    3543
     
    3846    */
    3947    Sub ~IPAddress()
    40         If Address Then free(Address)
     48        If item <> NULL Then free(item)
    4149    End Sub
     50
     51    Function Operator== (address As IPAddress) As Boolean
     52        Return Equals(address)
     53    End Function
    4254
    4355    /*!
     
    4658    */
    4759    Function AddressFamily() As Sockets.AddressFamily
    48         Return New Sockets.AddressFamily(Address->sa_family, "AddressFamily")
     60        Return New System.Net.Sockets.AddressFamily(family, "AddressFamily")
    4961    End Function
    5062
     
    5365    @return スコープ
    5466    */
    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*/
    8170    /*!
    8271    @brief リンクローカルアドレスかどうかを取得する。
     
    8574    */
    8675    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)
    9078        Else
    9179            Throw New System.Net.Sockets.SocketException("IPAddress.IsIPv6LinkLocal: AddressFamily is not IPv6.")
     
    9987    */
    10088    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)
    10491        Else
    10592            Throw New System.Net.Sockets.SocketException("IPAddress.IsIPv6Multicast: AddressFamily is not IPv6.")
     
    11198    @retval True  等しい
    11299    @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
    117111    End Function
    118112
     
    123117    Override Function ToString() As String
    124118        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.")
    127137        Else
    128138            Return New String(temp)
    129139        End If
    130140    End Function
    131 
    132 
    133141
    134142
     
    140148    */
    141149    Static Function Parse(ip As String) As IPAddress
    142         Dim address = New IPAddress
    143         If TryParse(ip, address) Then
    144             Return address
    145         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.")
    147155        End If
    148156    End Function
     
    155163    @retval False 失敗
    156164    */
    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
    160167        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)
    163171        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)
    166175        Else
    167176            Return False
    168177        End If
    169178
    170         If ipAddress.Address Then
     179        If address.item <> NULL Then
    171180            Return True
    172181        Else
     182            address = Nothing
    173183            Return False
    174184        End If
     
    196206    */
    197207    Static Function Loopback() As IPAddress
    198         Return New IPAddress( HostToNetworkOrder(&HFF000000) )
     208        Return New IPAddress( HostToNetworkOrder(&H7F000001) )
    199209    End Function
    200210
     
    273283
    274284    /*!
    275     @brief IPv4アドレスを表した文字列から、IPアドレスが設定されたsockaddr構造体を作成します
    276     @param IPv4アドレスを表した文字
    277     @return sockaddr構造体を示すアドレス
    278     */
    279     Function CreateAddressFromIPv4String(ip As String) As *sockaddr
    280         Dim address = malloc(SizeOf(sockaddr_in)) As *sockaddr_in
    281         If WSAStringToAddress(ToTCStr(ip), AF_INET, ByVal 0, address As sockaddr, SizeOf(sockaddr_in)) Then
     285    @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
    282292            Return NULL
    283293        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
    296308            Return NULL
    297309        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
    299313        End If
    300314    End Function
Note: See TracChangeset for help on using the changeset viewer.