std.socket and raw socket for Layer 2 capture.
Adam D Ruppe
destructionator at gmail.com
Mon Jun 5 21:03:12 UTC 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.
>
> ```d
> import core.stdc ... # there is no socket.d ...
That'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.
More information about the Digitalmars-d
mailing list