std.socket and raw socket for Layer 2 capture.

zann zann at arklab.dev
Tue Jun 6 05:46:48 UTC 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:
>> 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.

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.


More information about the Digitalmars-d mailing list