2014-12-11 19 views
0

SharpPcap 프로젝트가 Mono와 호환 가능하다고 주장하기 때문에 Mac에서 프로젝트를 실행하기로 결정했습니다.LibPcapLiveDevice의 IP 주소 받기

이렇게하려면 WinPcapDeviceLibPcapLiveDevice으로 만들고 WinPcap.dlllibpcap.dylib으로 매핑해야합니다.

SharpPcap.dll.config :

<configuration> 
    <dllmap dll="wpcap" target="libpcap.1.6.2.dylib" /> 
</configuration> 

코드 :

private static string GetFilterString(ICaptureDevice captureDevice) 
{ 
    var device = (LibPcapLiveDevice) captureDevice; 
    return String.Format("((tcp dst port 80) and (src net {0})) or ((dst net {0}) and (tcp src port 80))", device.Addresses[1]); 
} 

문제이며, 재산 device.Addresses이 비어있는 것을 나는 IP 주소를 포함하는 어떤 다른 속성을 찾을 수 없습니다 . 사실 거의 모든 속성은 장치 이름과 MAC 주소를 제외하고는 비어 있습니다. SharpPcap 또는 Libpcap로 인한 문제인지 확실하지 않습니다. 가이 해리스 내가 애플 OS 참조 문서로 PcapUnmanagedStructures.cs에 사용되는 구조체 및 유형을 비교하여 제안한 것처럼

편집

. 나는 그들을 적응하려고, 그래서 그들은 OS '사양 일치합니다 :

[StructLayout(LayoutKind.Sequential)] 
public struct sockaddr 
{ 
    public byte  sa_family;  /* address family */ 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=14)] 
    public byte[]  sa_data;  /* 14 bytes of protocol address */ 
}; 

public struct in_addr 
{ 
    public UInt32 s_addr; //in_addr_t 
} 

[StructLayout(LayoutKind.Sequential)] 
public struct sockaddr_in 
{ 
    public byte  sa_family;  /* address family */ 
    public UInt16  sa_port;  /* port */ 
    public in_addr  sin_addr;  /* address */ 

    // char sin_zero[8]; not sure this whether to add this as it was contained in the doc 

    // pad the size of sockaddr_in out to 16 bytes 
    MarshalAs(UnmanagedType.ByValArray, SizeConst=8)] 
    // Disable warnings around this unused field 
    #pragma warning disable 0169 
    private byte[]  pad; // not sure about this one either 
    #pragma warning restore 0169 
}; 

[StructLayout(LayoutKind.Sequential)] 
internal struct sockaddr_in6 
{ 
    public byte  sin6_family; /* address family */ 
    public UInt16  sin6_port;  /* Transport layer port # */ 
    public UInt32  sin6_flowinfo; /* IPv6 flow information */ 

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] // raised it to 32 as the struct in6_addr contains an element of type __uint32_t 
    public byte[]  sin6_addr;  /* IPv6 address */ 
    public UInt32  sin6_scope_id; /* scope id (new in RFC2553) */ 
}; 

나는 ipAddress는 여전히 비어 있기 때문에 나는 바로 그것을했다 모르겠어요 : screenshot

EDIT 2

가이 해리스는 그 길이 필드에 대해 옳았습니다. 나는 또한 sa에서 sin 일부 필드의 이름 접두어를 변경

[StructLayout(LayoutKind.Sequential)] 
public struct sockaddr 
{ 
    public byte   sa_len; 
    public byte  sa_family; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=14)] 
    public byte[] sa_data; 
}; 

public struct in_addr 
{ 
    public UInt32 s_addr; 
} 

[StructLayout(LayoutKind.Sequential)] 
public struct sockaddr_in 
{ 
    public byte   sin_len; 
    public byte   sin_family; 
    public UInt16  sin_port; 
    public in_addr  sin_addr; 

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)] 
    public byte   sin_zero; 
}; 

참고 :이 지금 나를 위해 작동하는 것이다. 또한 pad의 이름을 sin_zero으로 변경했습니다. 결국 꽤 쉽습니다. PcapUnmanagedStructures.cs는 OS에 대한 네이티브 C 구조의 레이아웃을 설명하려고 시도하는 경우

답변

1

글쎄, 나는, C#을 잘 알고 아니지만, 그 선언은 BSD-을 포함하여, 많은 UN * XES에 대한 정확하지 않습니다 오에스 텐 같은 유향의 UN * XES는 이러한 구조가 1 바이트 길이 필드 다음에 2 바이트의 주소 패밀리 필드가 아닌 1 바이트의 주소 패밀리 필드로 시작됩니다.

그래서 sockaddr 구조의 모든 패밀리 필드는 OS X 또는 {Free, Net, Open, FreeBSD} BSD의 구조 레이아웃과 일치하지 않습니다. Linux에서 일치 할지라도 다른 UN * Xes에서도 일치하지 않을 수 있습니다.

이것이 문제의 원인인지 여부는 다른 문제입니다.

+0

이러한 기본 C 구조에 대한 설명서를 제공 할 수 있습니까? 어쩌면이 코드를 적용하는 데 도움이 될 것입니다. 나는 확실히 모른다, 어디에서보기 시작할 지. – wodzu

+0

OS X의 경우 ['struct sockaddr' 및'struct sockaddr_storage'] (http://opensource.apple.com/source/xnu/xnu-2782.1.97/bsd/sys/socket.h), ['struct sockaddr_in '] (http://opensource.apple.com/source/xnu/xnu-2782.1.97/bsd/netinet/in.h), ['struct sockaddr_in6'] (http://opensource.apple.com/ source/xnu/xnu-2782.1.97/bsd/netinet6/in6.h). 'struct sockaddr_ll'은 리눅스에 고유하며 OS X에는 존재하지 않습니다. 동일한 구조가 언급 된 BSD를 포함하여 모든 4.4-Lite 파생 OS에 있어야합니다. –

+0

당신이 제공 한 문서에 따라 변경 한 내용으로 질문을 업데이트했습니다. 불행히도 그것은 여전히 ​​작동하지 않습니다 ... – wodzu