# Linux raw socket

**URL:** <https://discourse.julialang.org/t/linux-raw-socket/91044>\
**Category:** Web Stack\
**Tags:** networking, sockets\
**Created:** [November 30, 2022, 12:08pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044 "2022-11-30T12:08:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [November 30, 2022, 12:08pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044/1 "2022-11-30T12:08:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 30, 2022, 1:04pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044/2 "2022-11-30T13:04:05Z")

</div>

Anything that can be done in C can be done in Julia. For example, [following this tutorial](https://www.opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/) I think you could do:

```julia
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`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 30, 2022, 3:50pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044/3 "2022-11-30T15:50:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 30, 2022, 3:54pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044/4 "2022-11-30T15:54:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 30, 2022, 3:56pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044/5 "2022-11-30T15:56:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [November 30, 2022, 4:45pm UTC](https://discourse.julialang.org/t/linux-raw-socket/91044/6 "2022-11-30T16:45:15Z")

</div>

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