Create RAW socket?

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

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
Thanks,
Gabriele

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

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

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

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.

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

Fortunately, lwt_unix reuses the standard Unix types.

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

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”).

1 Like

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

follows a usage: