Network programming with sockets

There’s GNAT.Sockets and AdaSockets by Samuel Tardieu. Both implement TCP stream sockets and UDP datagram sockets, which probably covers 99% of use cases people have.

Now, imagine I want to bypass the transport layer and send a handcrafted package to a raw socket. I guess I need to directly bind to the system call, socket(AF_INET, SOCK_RAW, ...) or socket(AF_PACKET, SOCK_RAW, ...)? Similar to the bindings that Verisimilitude wrote for his UDP library, I guess?

What’s a good guide to start looking into calling C system functions?

Does the question even make sense? :sweat_smile:

Background: I came across a coding challenge to build a port scanner and for some reason thought it sounds intriguing. Suddenly I find myself in this rabbit hole of network programming. I’m completely new to it, as well as to low-level programming. So consider me a noob just starting to explore this territory…


What’s a good guide to start looking into calling C system functions?

I don’t remember finding one when I wrote Trendy Terminal, looks like AdaCore docs now have something - interfacing with c. It’s actually super well defined and supported by the language, and super easy, the big problem I ran into was dealing with all of the macros people like to use.

I poked around what’s on Alire to find some other libraries to look at, there’s a code search tool you can use to look for code across Alire. The big shenanigans I ran into was dealing with conditional compilation across platforms and getting gprbuild to deal with that.

1 Like

I intend to write something using the so-called raw sockets, and it’s such a mess of C language text-replacement nonsense that I was just going to write a C language program for the very simple task I had in mind.

Read the documentation about the standard Interfaces.C package and its children. The number one pain in the ass is going to be binding things defined with text-replacement macros, and hunting down which types things use to get a definition not already handled by Interfaces.C.

Lastly, I’d appreciate being referred to as a man. I hate the singular they.

When binding to C programs that use a lot of macros, I’ve found that it’s easier to write a small wrapper in C that you call from Ada. This way you have control over which types and constants need to be defined in Ada.

I did this a lot in linux_hal


Will do. My naive view was that it’s enough to use socket() and sendto() (passing the raw bytes of an assembled packet), but I guess the devil‘s in the detail - and I have no clue yet how to intercept the response.

(P.S. Changed „their“ to „his“; no offense meant. Just wasn’t sure.)

That seems like a smart strategy indeed. Thanks for the example! :slight_smile:

The devil’s in the Linux kernel’s bespoke interface for this.

I appreciate it.

If you are new to network programming then Protohackers is a gentler introduction to the topic via programming challenges since it stays at the application layer.
I am making my way through protohackers in Ada and enjoying it since it involves a bit of tasks, timing, selectors and even some business logic.


This looks really nice! Thanks a lot for the pointer.