Linux raw socket

Is there a way to open a raw linux socket through Julia? From the documentation, it seems that the answer to my question is no, but I wanted to double check.

Anything that can be done in C can be done in Julia. For example, following this tutorial I think you could do:

const AF_PACKET = 17
const SOCK_RAW = 3
const ETH_P_ALL = 0x0003

fd = ccall(:socket, Cint, (Cint, Cint, Cint), AF_PACKET, SOCK_RAW, hton(ETH_P_ALL))
io = fdio(fd)

and then read from the io object as a normal Julia I/O stream.

The only annoyance here is that Julia doesn’t have direct access to C header files, so you need to manually enter constants like SOCK_RAW.

6 Likes

The other “annoyance” is that any blocking calls will block the Julia thread, but if you don’t care about doing concurrent I/O that’s ok.

Once you have a file descriptor you can do non-blocking I/O with the io object just as with any other Julia stream, no?

Oh, that’s true since libuv uses non-blocking APIs for sockets on all platforms. I was thinking of using C APIs for reading and writing to sockets directly.

I haven’t touched ccall until now, but this seems quite straightforward. Thanks!