# Create RAW socket?

**URL:** <https://discuss.ocaml.org/t/create-raw-socket/3239>\
**Category:** Learning\
**Created:** [January 28, 2019, 12:44pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239 "2019-01-28T12:44:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [January 28, 2019, 12:44pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/1 "2019-01-28T12:44:34Z")

</div>

Dear all,

I was looking at OCaml to build some pretty simple vSwitch and so I need to open a RAW socket in order to manage Ethernet frames.

But I’m unable to bind the socket to an interface.  
This is how I create the socket

```auto
let create_socket iface = 
  let sock = Lwt_unix.socket Unix.PF_INET Unix.SOCK_RAW 0 in
  let sockaddr = .... ? What is supposed to be there? in
  bind sock sockaddr;
  sock

```

It is possible to create RAW socket in OCaml?  
I’m supposed to wrap some C code like this lib [ocaml-rawlink](https://github.com/haesbaert/rawlink)  
Thanks,  
Gabriele

---

<div class="post-metadata">

**Author:** ![CatseyeChandra](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/catseyechandra/32/4537_2.png) [@CatseyeChandra](https://discuss.ocaml.org/u/CatseyeChandra)\
**Post date:** [July 1, 2023, 12:59pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/2 "2023-07-01T12:59:09Z")

</div>

Did you get an answer or solution to this by another mean? Got the same problem.

---

<div class="post-metadata">

**Author:** ![dangdennis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dangdennis/32/2594_2.png) [@dangdennis](https://discuss.ocaml.org/u/dangdennis)\
**Post date:** [July 1, 2023, 5:41pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/3 "2023-07-01T17:41:40Z")

</div>

> **[ChatGPT](https://chat.openai.com/share/839935d4-9d23-4eee-8471-49ff1a429932)**
>
> A conversational AI system that listens, learns, and challenges

Here’s an example. chatgpt’s fast becoming my new let-me-google-it-for-you tool.

Then there’s this excellent read. [Modern communication: sockets](https://ocaml.github.io/ocamlunix/sockets.html)

To create a socket address, you need to use one of the sockaddr variant type.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [July 1, 2023, 5:45pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/4 "2023-07-01T17:45:01Z")

</div>

The problem with this is that the original question was using Lwt, and ChatGPT is using Unix. To someone who is not very familiar with both of these libraries and their differences, this will lead to more confusion.

---

<div class="post-metadata">

**Author:** ![dangdennis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dangdennis/32/2594_2.png) [@dangdennis](https://discuss.ocaml.org/u/dangdennis)\
**Post date:** [July 1, 2023, 5:53pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/5 "2023-07-01T17:53:20Z")

</div>

That’s good to note. When I skimmed the original code, I saw only Unix.X 😇.

Fortunately, lwt\_unix reuses the standard Unix types.

> **[lwt 2.7.0 · OCaml Package](https://ocaml.org/p/lwt/2.7.0/doc/Lwt_unix/index.html#sockets)**
>
> lwt 2.7.0: Monadic promises and concurrent I/O

One should be able to define socket addresses with the Unix.sockaddr types and use it with lwt’s socket apis.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [July 1, 2023, 11:44pm UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/6 "2023-07-01T23:44:59Z")

</div>

Before you try to do this with `lwt`, why not try to do it with straight-up `Unix` ? That is, the `unix` library in OCaml? Indeed, _first_ do it with C code, and you can then compare the `strace` output of your C program to that of your OCaml program written using `unix`. Once the latter works, you can then switch to `lwt`.

ETA: what I’m saying is: first get yourself a “known good” (C program version); then “baby-step” to a “known good OCaml version” (the version using `unix`) and only then baby-step to `lwt` (your current “known bad”).

---

<div class="post-metadata">

**Author:** ![haesbaert](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haesbaert/32/3608_2.png) [@haesbaert](https://discuss.ocaml.org/u/haesbaert)\
**Post date:** [July 3, 2023, 8:08am UTC](https://discuss.ocaml.org/t/create-raw-socket/3239/7 "2023-07-03T08:08:11Z")

</div>

Rawlink has Lwt bindings, you give the interface name and voilat:

> <https://github.com/haesbaert/rawlink/blob/master/lib/lwt_rawlink.mli#L28>

> <https://github.com/haesbaert/rawlink/blob/master/lib/lwt_rawlink.mli#L37>

follows a usage:

> <https://github.com/mirage/charrua/blob/7a874ed49d433f83442261da64bda18ac8331905/unix/charruad.ml#L98>
