darkerseid

darkerseid

I am trying to get low level socket programming in elixir

{:ok, socket} = :socket.open(:inet, :raw, :icmp)
:socket.bind(socket, %{:family => :inet, :port => 0})
:socket.ioctl(socket, :gifconf)

Gives me

{:ok, [%{addr: %{addr: {127, 0, 0, 1}, family: :inet, port: 0}, name: 'lo'}]}

But ip link gives me

#ip link
#1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
#    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
#2: veth0a@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default #qlen 1000
#    link/ether 5e:c8:38:a3:e6:50 brd ff:ff:ff:ff:ff:ff link-netns netns_br0

How do you get these two interfaces?

For testing, you can create execute this script, A bunch of helper functions to create Linux bridges, network namespaces, and interconnect everything using veth pairs. · GitHub

Showing Posts 1 to 7

moogle19

moogle19

With which user to you run this?
When you are on Linux you normally need to be root to access raw ICMP sockets.

darkerseid

darkerseid OP

I created a new linux namespace with the help of attached gist.

sudo nsenter --net=/var/run/netns/netns_veth0
moogle19

moogle19

If I understand nsenter correctly, it just spawns a new shell inside the network namespace, but you probably still need root priviliges or the CAP_NET_RAW capability to access raw sockets (but I never worked with these namespaces).

hauleth

hauleth

Not really, you can run ICMP socket as a regular user if your user ID is within sysctl net.ipv4.ping_group_range. However that will require you to use datagram socket instead of raw socket:

{:ok, socket} = :socket.open(:inet, :dgram, :icmp)

It also has slightly reduced API, as for example on Linux you cannot specify id and seq fields for ICMP packet, as these will be always set for you.

There is library that I have created in the past that provide slightly higher API for ICMP sockets (still quite limited):

It can be used in form of (to match the top message):

{:ok, socket} = :gen_icmp.open(raw: true)

:gen_icmp.echoreq(socket, {8, 8, 8, 8}, <<1,2,3,4>>)

receive do
  {:icmp, _addr, {:echorep, %{data: <<1,2,3,4>>}}} ->
    IO.puts("received pong")
end

And about the question of “how to get list of interfaces in the system”. Instead of playing with socket directly (especially as you are doing network namespacing, so obviously your view on interfaces will be limited) you should use:

:inet.getifaddrs()

That will return data like that:

{:ok,
 [
   {'lo0',
    [
      flags: [:up, :loopback, :running, :multicast],
      addr: {127, 0, 0, 1},
      netmask: {255, 0, 0, 0},
      addr: {0, 0, 0, 0, 0, 0, 0, 1},
      netmask: {65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535},
      addr: {65152, 0, 0, 0, 0, 0, 0, 1},
      netmask: {65535, 65535, 65535, 65535, 0, 0, 0, 0}
    ]},
   {'gif0', [flags: [:pointtopoint, :multicast]]},
   {'stf0', [flags: []]},
   ...]}

That describe all interfaces available in your system and do not require any special privileges like being able to create ICMP raw socket.

darkerseid

darkerseid OP

To add some context, I was trying to replicate Networking Lab: Ethernet Broadcast Domains in elixir, the code in there uses python

hauleth

hauleth

But in this case you should not use ICMP protocol, but “just raw socket”, aka:

{:ok, socket} = :socket.open(:inet, :raw)

So this is pretty much different from your original question where you ask for the fetching list of the interfaces from raw ICMP socket.

:socket.ioctl(socket, :gifconf)

Will will return you only the configuration for the interface the socket is listening right now, not all that are available in the system.

Also I see the problem with your code - you define address map like %{:family => :inet, :port => 0} which is incorrect (weird that socket do not scream about it, or maybe it does, but you do not check the value returned by :socket.bind/2). It should be %{:family => :inet, :port => 0, :addr => address} as :addr field is mandatory according to the specs.

BradS2S

BradS2S

I’m learning a ton about low level socket work from @whatyouhide’s protohacker YouTube videos.

Definitely worth a watch

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews