digitalmars.D - std.socket and raw socket for Layer 2 capture.
- zann (38/38) Jun 05 2023 Hello together
- Adam D Ruppe (13/32) Jun 05 2023 That's cuz socket isn't stdc, it is posix.
- zann (6/44) Jun 05 2023 Hehey, thanks a lot, that helps much.
Hello together I've a question about layer-2 (ethernet frames) capturing. I have a Network and Linux Admin background, but not a really heavy programmer. In C i can create a socket( AF_PACKET, SOCK_RAW, 0 ) and thats it. ```d $ grep -ir socket /usr/include/dlang/dmd/core/stdc/" ``` Ok so it's have to be possible with D there is all i need i think and at this moment i'm struggle on: ```d import std.stdio; import std.socket; Socket init_sock() { auto socket = new Socket( AddressFamily.INET, SocketType.RAW, ProtocolType.RAW ); socket.setOption( SocketOptionLevel.RAW, SocketOption.TYPE, 3 ); return socket; } void main() { ubyte[1500] socket_buffer; writeln("init socket."); auto sock = init_sock(); sock.receiveFrom( socket_buffer ); writeln(socket_buffer); } ``` But for the moment i think it's really impossible to capture ethernet frames with D. Or can someone explain where my "error in head" is? Capture ip packets or tcp / udp segments, thats all easy and i already did, but it is not what i need. I need the whole Ethernet Frame. Best regards Erich Zann
Jun 05 2023
On Monday, 5 June 2023 at 18:13:48 UTC, zann wrote:In C i can create a socket( AF_PACKET, SOCK_RAW, 0 ) and thats it. ```dThat's cuz socket isn't stdc, it is posix. // in C it is #include <sys/socket.h> // and it is "CONFORMING TO Posix..." // so that means in D you can generally find it under // core.sys.posix for the posix conformation, then // sys.socket cuz of sys/socket. thus this: import core.sys.posix.sys.socket; Lets you use the same functions as from C.$ grep -ir socket /usr/include/dlang/dmd/core/stdc/"expand the search to core, not just stdc as a general rule.Socket init_sock() { auto socket = new Socket( AddressFamily.INET, SocketType.RAW, ProtocolType.RAW ); socket.setOption( SocketOptionLevel.RAW, SocketOption.TYPE, 3 ); return socket; } void main() { ubyte[1500] socket_buffer; writeln("init socket."); auto sock = init_sock(); sock.receiveFrom( socket_buffer ); writeln(socket_buffer); }I've never actually used a raw socket but you might also have to pass an address buffer to receiveFrom and you definitely should be checking the return value minimally.
Jun 05 2023
On Monday, 5 June 2023 at 21:03:12 UTC, Adam D Ruppe wrote:On Monday, 5 June 2023 at 18:13:48 UTC, zann wrote:Hehey, thanks a lot, that helps much. Yes checking the return value is important, but first i want that the socket can be created. And thats not the case. If i can create the socket that i need, at this point, the clean coding with error checking starts.In C i can create a socket( AF_PACKET, SOCK_RAW, 0 ) and thats it. ```dThat's cuz socket isn't stdc, it is posix. // in C it is #include <sys/socket.h> // and it is "CONFORMING TO Posix..." // so that means in D you can generally find it under // core.sys.posix for the posix conformation, then // sys.socket cuz of sys/socket. thus this: import core.sys.posix.sys.socket; Lets you use the same functions as from C.$ grep -ir socket /usr/include/dlang/dmd/core/stdc/"expand the search to core, not just stdc as a general rule.Socket init_sock() { auto socket = new Socket( AddressFamily.INET, SocketType.RAW, ProtocolType.RAW ); socket.setOption( SocketOptionLevel.RAW, SocketOption.TYPE, 3 ); return socket; } void main() { ubyte[1500] socket_buffer; writeln("init socket."); auto sock = init_sock(); sock.receiveFrom( socket_buffer ); writeln(socket_buffer); }I've never actually used a raw socket but you might also have to pass an address buffer to receiveFrom and you definitely should be checking the return value minimally.
Jun 05 2023