# Network programming with sockets

**URL:** <https://forum.ada-lang.io/t/network-programming-with-sockets/671>\
**Category:** General\
**Created:** [February 11, 2024, 11:14am UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671 "2024-02-11T11:14:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cunger](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cunger/32/322_2.png) [@cunger](https://forum.ada-lang.io/u/cunger)\
**Post date:** [February 11, 2024, 11:14am UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/1 "2024-02-11T11:14:28Z")

</div>

There’s [GNAT.Sockets](https://en.wikibooks.org/wiki/Ada_Programming/Libraries/GNAT.Sockets) and [AdaSockets by Samuel Tardieu](https://rfc1149.net/devel/adasockets.html). 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](http://verisimilitudes.net/2022-04-05), I guess?

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

Does the question even make sense? 😅

_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…

---

<div class="post-metadata">

**Author:** ![pyj](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/pyj/32/5_2.png) [@pyj](https://forum.ada-lang.io/u/pyj)\
**Post date:** [February 11, 2024, 1:24pm UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/2 "2024-02-11T13:24:17Z")

</div>

> 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](https://learn.adacore.com/courses/intro-to-ada/chapters/interfacing_with_c.html). It’s actually [super well defined and supported by the language](https://docs.adacore.com/live/wave/arm05/html/arm05/RM-B-3.html), 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](https://search.synack.me/) 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.

---

<div class="post-metadata">

**Author:** ![Verisimilitude](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/verisimilitude/32/384_2.png) [@Verisimilitude](https://forum.ada-lang.io/u/Verisimilitude)\
**Post date:** [February 11, 2024, 6:46pm UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/3 "2024-02-11T18:46:00Z")

</div>

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.

> [@cunger](#):
>
> What’s a good guide to start looking into calling C system functions?

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_.

---

<div class="post-metadata">

**Author:** ![JeremyGrosser](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jeremygrosser/32/3_2.png) [@JeremyGrosser](https://forum.ada-lang.io/u/JeremyGrosser)\
**Post date:** [February 11, 2024, 6:56pm UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/4 "2024-02-11T18:56:14Z")

</div>

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](https://github.com/JeremyGrosser/linux_hal)

> <https://github.com/JeremyGrosser/linux_hal/blob/c7c2f276f55fcab601ccd247d3db14433bd179fe/src/linux_ioctl_wrap.c>

---

<div class="post-metadata">

**Author:** ![cunger](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cunger/32/322_2.png) [@cunger](https://forum.ada-lang.io/u/cunger)\
**Post date:** [February 11, 2024, 7:19pm UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/5 "2024-02-11T19:19:36Z")

</div>

> [@Verisimilitude](#):
>
> Read the documentation about the standard `Interfaces.C` package and its children.

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

---

<div class="post-metadata">

**Author:** ![cunger](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cunger/32/322_2.png) [@cunger](https://forum.ada-lang.io/u/cunger)\
**Post date:** [February 11, 2024, 7:21pm UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/6 "2024-02-11T19:21:08Z")

</div>

> [@JeremyGrosser](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![Verisimilitude](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/verisimilitude/32/384_2.png) [@Verisimilitude](https://forum.ada-lang.io/u/Verisimilitude)\
**Post date:** [February 11, 2024, 7:26pm UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/7 "2024-02-11T19:26:59Z")

</div>

> [@cunger](#):
>
> 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

The devil’s in the Linux kernel’s [bespoke interface for this](https://www.man7.org/linux/man-pages/man7/raw.7.html).

> [@cunger](#):
>
> (P.S. Changed „their“ to „his“; no offense meant. Just wasn’t sure.)

I appreciate it.

---

<div class="post-metadata">

**Author:** ![anuj-seth](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/anuj-seth/32/48_2.png) [@anuj-seth](https://forum.ada-lang.io/u/anuj-seth)\
**Post date:** [February 13, 2024, 1:19am UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/8 "2024-02-13T01:19:31Z")

</div>

If you are new to network programming then [Protohackers](https://protohackers.com/) 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.

---

<div class="post-metadata">

**Author:** ![cunger](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cunger/32/322_2.png) [@cunger](https://forum.ada-lang.io/u/cunger)\
**Post date:** [February 13, 2024, 9:29am UTC](https://forum.ada-lang.io/t/network-programming-with-sockets/671/9 "2024-02-13T09:29:16Z")

</div>

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