2012-11-29 5 views
2

(vb) .NET에서 ARP 프로토콜의 성능을 활용하여 가정용 LAN의 모든 네트워크 장치 목록과 하드웨어 (MAC) 주소 목록을 작성하려고합니다. 이미 서브넷의 모든 IP 주소를 계산하는 방법을 알아 냈지만 ARP 요청을 보내고 응답을 읽을 수있는 확실한 방법을 찾지 못했습니다.관리 코드에서 ARP 요청

모든 네트워크 장치에는 Windows 이외의 장치 (예 : 스마트 폰)가 포함됩니다. 즉, WMI는이 아닙니다.

이제 어느 쪽이 더 귀찮은 지 알 수 없습니다. A) SendArp 메소드를 사용하기 위해 기본 IP helper API를 관리 코드로 래핑하거나 B) ARP를 채우기 위해 각 호스트에 ping 요청 보내기 캐시를 수행 한 다음 arp.exe -a의 출력을 구문 분석하십시오.

관리 코드 만 사용하여 ARP 요청/응답을 처리하는 방법을 아는 사람이 있습니까? 소켓 객체와 약간의 마술을 사용하여 구현하는 것은 그리 어렵지는 않지만 ARP API를 제공하는 타사 네트워킹 라이브러리를 찾을 수 없습니다. 나는 나 자신을 창조 할만큼 충분히 지식이 없다는 것을 두려워한다.

답변

0

관련 :

  • How do I access ARP-protocol information through .NET?
  • 허용 대답은 COM을 사용하지만 C 번호는 패킷 캡처 라이브러리를 기반 만든 것으로 보인다 개발자에게 매우 흥미있는 링크도있다 인용

      , ARP 정보를 얻는 방법에 대한 예제가 나와 있습니다. 아마 그게 너에게 도움이 될거야?

      "SharpPcap"이 관리되지 않는 코드를 래핑하는지 알 수 없습니다.

      http://www.tamirgal.com/blog/post/ARP-Resolver-C-Class.aspx

      에는 내장 된 관리 코드 ARP AFAIK 직접 처리 할 수 ​​없습니다.

    1

    그래서 나는 윈도우 API를 포장하는 것이 가장 지저분한 해결책이 될 것이라고 결정하고 모두가 볼 수 있도록 다음과 같은 내놓았다했습니다

    먼저 나는라는 이름의 개인 생성자와 Friend NotInheritable Class을 만들어 NativeMethods 약 (서브 클래스 (static internal) 인 IPHelper이있는 C#의 정적 내부 클래스와 동일합니다. 이것은 내가 뻔뻔하게 pinvoke.net (source)에서 복사 한 DllImport를 넣는 곳입니다.

    Friend NotInheritable Class NativeMethods 
        Private Sub New() 
        End Sub 
    
        Friend NotInheritable Class IPHelper 
          Private Sub New() 
          End Sub 
    
          ' Possible return values 
          Friend Const NO_ERROR As Integer = 0 
          Friend Const ERROR_BAD_NET_NAME As Integer = 67 
          Friend Const ERROR_BUFFER_OVERFLOW As Integer = 111 
          Friend Const ERROR_GEN_FAILURE As Integer = 31 
          Friend Const ERROR_INVALID_PARAMETER As Integer = 87 
          Friend Const ERROR_INVALID_USER_BUFFER As Integer = 1784 
          Friend Const ERROR_NOT_FOUND As Integer = 1168 
          Friend Const ERROR_NOT_SUPPORTED As Integer = 50 
    
          ' API function declaration. 
          <DllImport("iphlpapi.dll", SetLastError:=True)> 
          Friend Shared Function SendARP(
            DestIP As UInt32, 
            SrcIP As UInt32, 
            pMacAddr() As Byte, 
            ByRef PhyAddrLen As Int32) As UInt32 
          End Function 
    
        End Class 
    End Class 
    

    지금 그 위에 나는 공용 클래스 SendARP 방법을 소비 ArpRequest을 썼다.

    Imports System.Net 
    Imports System.Runtime.InteropServices 
    Imports System.ComponentModel 
    Imports System.IO 
    Imports System.Net.NetworkInformation 
    
    Public Class ArpRequest 
    
        Private _address As IPAddress 
    
        Public Sub New(address As IPAddress) 
          _address = address 
        End Sub 
    
        ''' <summary> 
        ''' Gets the MAC address that belongs to the specified IP address. 
        ''' </summary> 
        ''' <remarks>This uses a native method and should be replaced when a managed alternative becomes available.</remarks> 
        Public Function GetResponse() As PhysicalAddress 
          Dim ip As UInteger = BitConverter.ToUInt32(_address.GetAddressBytes(), 0) 
          Dim mac() As Byte = New Byte(5) {} 
    
          Dim ReturnValue As Integer = CInt(NativeMethods.IPHelper.SendARP(CUInt(ip), 0, mac, mac.Length)) 
    
          If ReturnValue = NativeMethods.IPHelper.NO_ERROR Then 
            Return New PhysicalAddress(mac) 
          Else 
            ' TODO: handle various SendARP errors 
            ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa366358(v=vs.85).aspx 
            Throw New Win32Exception(CInt(ReturnValue)) 
          End If 
        End Function 
    End Class 
    

    사용법은 간단합니다 (그러나 Win32Exceptions 조심) :

    Dim ip = System.Net.IPAddress.Parse("0.0.0.0") ' replace with actual ip 
        Dim arp = New ArpRequest(ip) 
        Dim hardwareAddress = arp.GetResponse() 
    
    관련 문제