<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
    <title>La Forge - optimisation</title>
    <subtitle>Le blog de la Forge</subtitle>
    <link rel="self" type="application/atom+xml" href="https://staging.lafor.ge/tags/optimisation/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://staging.lafor.ge"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-08-31T00:00:00+00:00</updated>
    <id>https://staging.lafor.ge/tags/optimisation/atom.xml</id>
    <entry xml:lang="fr">
        <title>Un serveur HTTP de moins de 20 Ko</title>
        <published>2024-08-31T00:00:00+00:00</published>
        <updated>2024-08-31T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://staging.lafor.ge/http-smol/"/>
        <id>https://staging.lafor.ge/http-smol/</id>
        
        <content type="html" xml:base="https://staging.lafor.ge/http-smol/">&lt;p&gt;Bonjour à toutes et à tous 😀&lt;/p&gt;
&lt;h2 id=&quot;avant-propos&quot;&gt;Avant propos&lt;/h2&gt;
&lt;h3 id=&quot;contexte&quot;&gt;Contexte&lt;/h3&gt;
&lt;p&gt;Un peu de contexte du pourquoi et du comment.&lt;/p&gt;
&lt;p&gt;Lorsque l’on créé une application sur &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.clever-cloud.com/fr/home/&quot;&gt;Clever Cloud&lt;/a&gt;, il faut qu’elle soit capable de répondre en HTTP une OK 200, sinon le déploiement
est considéré comme échoué.&lt;/p&gt;
&lt;p&gt;Pour un usage qu’il n’est pas nécessaire de détailler ici (mais ce n’est pas la prod, rangez vos fourches 😅), je possède une application qui est incapable de répondre à cette appel HTTP.&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;En vrai, c’est surtout répondre à l’ouverture d’un socket plus que répondre de l’HTTP.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Cette application est légèrement trop complexe à faire tourner sur un runtime classique ce qui m’amène à utiliser un runtime Docker. Celui-ci n’a besoin que d’un Dockerfile
pour fonctionner.&lt;/p&gt;
&lt;p&gt;L’application que je dois dockerisé requiert un environnement complexe et très lourd (20 Go !!!), c’est beaucoup trop gros!&lt;/p&gt;
&lt;p&gt;Le plan est alors de construire dans cet environnement démentiel et venir utiliser le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://docs.docker.com/build/building/multi-stage/&quot;&gt;multi-stage building&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Nous allons donc avoir des Dockerfiles de la forme suivante.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; environement_trop_lourd &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on construit ce que l&amp;#39;on veut&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /path/in/builder/something /path/to/something&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Je veux alors quelque chose qui réponde en HTTP le plus léger possible et qui a le moins de dépendance également.&lt;/p&gt;
&lt;p&gt;Je souhaite avoir un serveur HTTP minimal de moins de 1 Mo. Mais on va voir que l’on peut descendre bien plus bas! 😄&lt;/p&gt;
&lt;p&gt;Je ne vais pas reprendre des produits déjà existants car ce n’est pas le but de la manœuvre, à la place je vais le coder, c’est bien plus amusant. 😎&lt;/p&gt;
&lt;p&gt;J’ai choisi 5 langages de programmation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.rust-lang.org/fr&quot;&gt;Rust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://go.dev/&quot;&gt;Golang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://vlang.io/&quot;&gt;Vlang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://ziglang.org/&quot;&gt;Zig&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;C&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Mention honorables aux langages “C compatible” mais que je ne détaillerai pas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://odin-lang.org/&quot;&gt;Odin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://crystal-lang.org/&quot;&gt;Crystal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://nim-lang.org/&quot;&gt;Nim&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Et pour l’intérêt pédagogique, on finira par l’&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.nasm.us/&quot;&gt;assembleur x86&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Pour chacune des implémentations je vais construire le serveur HTTP et vérifier la taille finale de l’image docker résultante.&lt;/p&gt;
&lt;h3 id=&quot;disclaimer&quot;&gt;Disclaimer&lt;/h3&gt;
&lt;blockquote class=&quot;markdown-alert-caution&quot;&gt;
	&lt;p&gt;&lt;strong&gt;Je NE suis PAS expert&lt;/strong&gt; dans les différents langages, il est donc probable que je fasse des &lt;strong&gt;erreurs&lt;/strong&gt; dans les implémentations.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote class=&quot;markdown-alert-caution&quot;&gt;
	&lt;p&gt;&lt;strong&gt;Le contenu de cet article n’est pas fait pour finir en prod !!!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Je suis juste en train de tirer une pelote pour comprendre les choses.&lt;/p&gt;
&lt;p&gt;Cela étant maintenant dis, on peut commencer !&lt;/p&gt;
&lt;h2 id=&quot;rust&quot;&gt;Rust&lt;/h2&gt;
&lt;p&gt;Rust est le langage que je connais le mieux, et celui qui donne les meilleur garantie de sécurité, par contre cette assurance à un coût en taille de binaire. Je ne m’attends pas à descendre en dessous du méga-octet.&lt;/p&gt;
&lt;p&gt;Voici une implémentation en Rust de notre serveur HTTP, il gère une connexion à la fois.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.rs&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;Write&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;net&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt;TcpListener&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; TcpStream&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; handle_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;acceptation_result&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;TcpStream&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; stream&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; acceptation_result&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    stream&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;write_all&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length:2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;as_bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; TcpListener&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0:8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        for&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; acceptation_result&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;incoming&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; let&lt;/span&gt;&lt;span&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;response_error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; handle_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;acceptation_result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;An error occurred &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;response_error&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;profil-de-debug&quot;&gt;Profil de debug&lt;/h3&gt;
&lt;p&gt;On lance:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ cargo run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   Compiling http-okay v0.1.0 (/mnt/f/Projets/Travail/Clever/Rust/http-okay)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     Running `target/debug/http-okay`&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on teste.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Succès ☑️&lt;/p&gt;
&lt;p&gt;Et la taille?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh target/debug/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3.9M    target/debug/http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ah ouais quand même !! 😅&lt;/p&gt;
&lt;h3 id=&quot;profil-de-release&quot;&gt;Profil de release&lt;/h3&gt;
&lt;p&gt;Comme le &lt;code&gt;[unoptimized + debuginfo]&lt;/code&gt; l’indique la compilation est réalisée en mode debug et donc contient tout un tas d’informations supplémentaires que l’on appelle des symboles ayant pour but de faciliter la compréhension des problèmes. La contrepartie c’est que ces symboles viennent largement alourdir le binaire final.&lt;/p&gt;
&lt;p&gt;Heureusement, il existe un mode de production qui va venir nettoyer tout ça.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ cargo build --release&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et c’est cette fois-ci bien plus acceptable.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;448K    target/release/http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nous avons déjà descendu sous la barre des 1Mo! 🥳&lt;/p&gt;
&lt;p&gt;On peut même encore diminuer la taille du binaire en suivant &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/johnthagen/min-sized-rust&quot;&gt;repo github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On applique en rajoutant une section &lt;code&gt;[profile.release]&lt;/code&gt; au Cargo.toml:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;profile&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;release&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; exécute la commande strip sur le binaire pour retirer les symboles d&amp;#39;informations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 448K =&amp;gt; 368K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;strip&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on optimise en taille le binaire&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 368K =&amp;gt; 368K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;opt-level&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on active le LTO : optimisation pendant le linkage (suppression de code mort)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 368K =&amp;gt; 320K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;lto&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on empêche le compilateur de paralléliser pour lui ouvrir des chemins d&amp;#39;optimisation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 320K =&amp;gt; 320K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;codegen-units&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on enlève la possibilité d&amp;#39;unwind la stacktrace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 320K =&amp;gt; 316K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;panic&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;abort&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le stripping du binaire est essentielle, on réduit de plus de 20% la taille du binaire final.&lt;/p&gt;
&lt;p&gt;Ensuite vient le LTO qui arrive à grapiller 40Ko de code mort, puis le panic=abort qui récupère 4Ko supplémentaire.&lt;/p&gt;
&lt;h3 id=&quot;optimisations&quot;&gt;Optimisations&lt;/h3&gt;
&lt;p&gt;Pour y voir un peu plus, je pars d’un programme vide, et je vais optimiser son poids jusqu’à arriver au limite du possible.&lt;/p&gt;
&lt;p&gt;Puis faire machine arrière et utiliser les optimisation sur &lt;code&gt;http-okay&lt;/code&gt; notre répondeur HTTP.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.rs&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;300K    target/release/http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On en déduit que le poids minimal d’une application Rust est de 300 Ko.&lt;/p&gt;
&lt;p&gt;Enfin ça c’est si on s’arrête à la surface des choses.&lt;/p&gt;
&lt;p&gt;On peut réduire tout ça biiiiiiiieeeeen plus loin! 🤩&lt;/p&gt;
&lt;h4 id=&quot;no-main&quot;&gt;no_main&lt;/h4&gt;
&lt;p&gt;On peut empêcher Rust d’émettre son &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://doc.rust-lang.org/reference/crates-and-source-files.html#the-no_main-attribute&quot;&gt;symbole “main”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cela diminue très légèrement le poids du binaire.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.rs&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on indique à Rust que l&amp;#39;on ne veut pas de main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on indique à rust d&amp;#39;appeler &amp;quot;main&amp;quot; réellement &amp;quot;main&amp;quot; dans le binaire&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_mangle&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cela nous donne&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;284K    target/release/http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Oui, vous avez bien lu! Emettre le main augmente de 26Ko la taille du binaire.&lt;/p&gt;
&lt;p&gt;Ce 284Ko est l’ultime fontière la dernière est inutile pour nous, mais intéressante pour la culture.&lt;/p&gt;
&lt;h4 id=&quot;no-std&quot;&gt;no_std&lt;/h4&gt;
&lt;p&gt;On a alors un code encore plus étrange&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.rs&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_std&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; crate&lt;/span&gt;&lt;span&gt; libc&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; core&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;panic&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;PanicInfo&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_mangle&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; u8&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;    0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Cette fonction est nécessaire, sinon cela ne compile pas&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;panic_handler&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; panic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;_info&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;PanicInfo&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; !&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et contre-intuivement, il faut rajouter une dépendance à la libc. En effet, le compilateur recherche le symbole &lt;code&gt;__libc_start_main&lt;/code&gt;, et il n’est pas &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://fasterthanli.me/series/making-our-own-executable-packer/part-12&quot;&gt;trivial&lt;/a&gt; du tout de le
reconstruire.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;libc&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; version&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.2.158&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; default-features&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; false&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et moteur!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   Compiling http-okay v0.1.0 (/mnt/f/Projets/Travail/Clever/Rust/http-okay)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Finished `release` profile [optimized] target(s) in 0.40s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;8K     target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ echo $?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, ça ne fait rien à part un exit(0), mais sur 8ko, il ne fallait pas demander la lune. 😄&lt;/p&gt;
&lt;h4 id=&quot;http-okay-en-no-main&quot;&gt;http-okay en no_main&lt;/h4&gt;
&lt;p&gt;La version &lt;code&gt;no_std&lt;/code&gt; est trop extrême pour nous, nous avons besoin de l’API de haut niveau de manipulation des sockets.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.rs&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;Write&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;use&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;net&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt;TcpListener&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; TcpStream&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; handle_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;acceptation_result&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;TcpStream&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; stream&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; acceptation_result&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    stream&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;write_all&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length:2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;as_bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;no_mangle&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; TcpListener&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0:8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        for&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; acceptation_result&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;incoming&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; let&lt;/span&gt;&lt;span&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;response_error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; handle_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;acceptation_result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;An error occurred &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;response_error&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on fini sur 300 Ko.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   Compiling http-okay v0.1.0 (/mnt/f/Projets/Travail/Clever/Rust/http-okay)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Finished `release` profile [optimized] target(s) in 2.61s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;300K    target/release/http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On gagne cette fois-ci 16 Ko sur les 316 Ko que l’on avait précédement.&lt;/p&gt;
&lt;p&gt;Bien-sûr, rien n’a changé sur le comportement final.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl -v localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;*   Trying 127.0.0.1:8080...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;* Connected to localhost (127.0.0.1) port 8080 (#0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; GET / HTTP/1.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Host: localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; User-Agent: curl/7.88.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Accept: */*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt; HTTP/1.1 200 OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt; Content-Length:2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;* Connection #0 to host localhost left intact&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;docker&quot;&gt;Docker&lt;/h3&gt;
&lt;p&gt;Je suis à la moitié du chemin. J’ai le binaire, mais pas encore l’image docker.&lt;/p&gt;
&lt;h4 id=&quot;build-dynamique&quot;&gt;Build dynamique&lt;/h4&gt;
&lt;p&gt;Pour construire le Dockerfile, je vais utilisé un stage &lt;code&gt;rust&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cat &amp;lt;&amp;lt;EOF &amp;gt; Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[package]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;name = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;version = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.1.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;edition = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;2021&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[profile.release]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;strip = true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;opt-level = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lto = true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;codegen-units = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;panic = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;abort&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le fichier source&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; mkdir src &amp;amp;&amp;amp; cat &amp;lt;&amp;lt;EOF &amp;gt; src/main.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;![no_main]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use std::io;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use std::io::Write;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use std::net::{TcpListener, TcpStream};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fn handle_connection(acceptation_result: io::Result&amp;lt;TcpStream&amp;gt;) -&amp;gt; io::Result&amp;lt;()&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let mut stream = acceptation_result?;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stream.write_all(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length:2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.as_bytes())?;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ok(())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;[no_mangle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fn main() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let server = TcpListener::bind(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0:8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;).unwrap();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    for acceptation_result in server.incoming() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        if let Err(response_error) = handle_connection(acceptation_result) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            println!(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;An error occurred {response_error}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cargo build --release&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On build et on exécute l’image.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-rust .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it -p 8080:8080 http-okay-rust target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Parfait. 😎&lt;/p&gt;
&lt;p&gt;Maintenant que j’ai le binaire, plus qu’à faire l’image &lt;code&gt;FROM scratch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ... &amp;lt;le reste du stage&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder target/release/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-rust .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it -p 8080:8080 http-okay-rust /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;exec /usr/bin/http-okay: no such file or directory&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ah! Comment ça ???!&lt;/p&gt;
&lt;p&gt;Vous vous souvenez quand on a fait du &lt;code&gt;no_std&lt;/code&gt;, j’ai du rajouter la &lt;code&gt;libc&lt;/code&gt; comme dépendance externe. Et bien si elle n’est pas dans le binaire dans notre version, elle est
forcément quelque part.&lt;/p&gt;
&lt;p&gt;Ce quelque part, c’est l’OS lui-même. Il existe un fichier qui contient la “libc”.&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-question&quot;&gt;
	&lt;p&gt;Mais c’est quoi la “libc”?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Pour discuter avec l’OS un programme a besoin d’une API qui permet à un programme de demander l’accès à une ressource. Et nous notre ressource que l’on veut accéder c’est
la carte réseau et plus particulièrement un socket TCP.&lt;/p&gt;
&lt;p&gt;Tout ce travail extrêmement complexe est prémâché par la libc qui donne tous les moyens de demander (entre autre) et de manipuler un socket. Nous avons donc besoin
de la libc dans notre stage &lt;code&gt;FROM scratch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;On appelle ces demandes des “appels systèmes” ou &lt;code&gt;syscalls&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Et du coup elle est où ?&lt;/p&gt;
&lt;p&gt;Si on garde que le stage &lt;code&gt;rust&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ... &amp;lt;le reste du stage&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cargo build --release&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On peut utiliser la commande &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://man7.org/linux/man-pages/man1/ldd.1.html&quot;&gt;ldd&lt;/a&gt; qui permet d’afficher les liens vers les morceaux de codes nécessaires au bon
fonctionnement du binaire. On appelle ça des bibliothèques de liens dynamiques.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it http-okay-rust  ldd target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        linux-vdso.so.1 (0x00007ffe657c9000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libgcc_s.so.1 =&amp;gt; /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8cc223f000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8cc205e000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        /lib64/ld-linux-x86-64.so.2 (0x00007f8cc22b3000)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;Les &lt;code&gt;(0x.........)&lt;/code&gt; sont des addresses dans la mémoire.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;On obtient une liste de 4 éléments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;linux-vdso.so.1&lt;/li&gt;
&lt;li&gt;/lib/x86_64-linux-gnu/libgcc_s.so.1&lt;/li&gt;
&lt;li&gt;/lib/x86_64-linux-gnu/libc.so.6&lt;/li&gt;
&lt;li&gt;/lib64/ld-linux-x86-64.so.2&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Le &lt;code&gt;linux-vdso.so.1&lt;/code&gt; est cas &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://man7.org/linux/man-pages/man7/vdso.7.html&quot;&gt;particulier&lt;/a&gt;, elle est automatiquement mappé au processus par le kernel du système d’exploitation.&lt;/p&gt;
&lt;p&gt;On a donc 3 chemins:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;/lib/x86_64-linux-gnu/libgcc_s.so.1&lt;/li&gt;
&lt;li&gt;/lib/x86_64-linux-gnu/libc.so.6&lt;/li&gt;
&lt;li&gt;/lib64/ld-linux-x86-64.so.2&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On peut alors les copier dans le stage &lt;code&gt;FROM scratch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ... &amp;lt;le reste du stage&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder target/release/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/libgcc_s.so.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-rust .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et cette fois-ci c’est bon ! 🍾🎉🥳&lt;/p&gt;
&lt;p&gt;Bon, et la taille ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY       TAG       IMAGE ID       CREATED         SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-rust   latest    1e3876fe8aee   7 minutes ago   2.56MB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;3 Mo, joli !!&lt;/p&gt;
&lt;p&gt;Via l’outil &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/wagoodman/dive&quot;&gt;dive&lt;/a&gt; on peut avoir une vision du contenu de l’image&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ dive http-okay-rust&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img class=&quot;&quot; width=&quot;100%&quot; src=&quot;/assets/images/http-smol/dive-rust-dynamic.png&quot; /&gt;
&lt;p&gt;On y voit les 2 Mo de la libc et aussi qu’il n’y a rien de superflu.&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-caution&quot;&gt;
	&lt;p&gt;ldd ne voit que les liens dynamiques, il est incapables de détecter les &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://man7.org/linux/man-pages/man3/dlopen.3.html&quot;&gt;dlopen&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cette technique ne marche que si vous êtes certain que votre binaire n’utilise pas l’ouverture à la volée de DLL&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4 id=&quot;build-statique&quot;&gt;Build statique&lt;/h4&gt;
&lt;p&gt;Une dernière optimisation que l’on peut réaliser et qui n’a de sens que dans notre cas est de builder statiquement le binaire. C’est à dire faire rentrer la libc et libc++
dans le binaire.&lt;/p&gt;
&lt;p&gt;Pour cela, je rajoute un env juste avant le &lt;code&gt;cargo build&lt;/code&gt; : &lt;code&gt;RUSTFLAGS=&#39;-C target-feature=+crt-static&#39;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ... &amp;lt;le reste du stage&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; RUSTFLAGS=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-C target-feature=+crt-static&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder target/release/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le stage &lt;code&gt;FROM scratch&lt;/code&gt; est alors bien amaigri.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-rust .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bon, tout marche.&lt;/p&gt;
&lt;p&gt;Un petit coup de dive.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ dive http-okay-rust&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img class=&quot;&quot; width=&quot;100%&quot; src=&quot;/assets/images/http-smol/dive-rust-static.png&quot; /&gt;
&lt;p&gt;Il n’y a plus que notre binaire+libc_estropié dans l’image.&lt;/p&gt;
&lt;p&gt;et le poids ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY       TAG       IMAGE ID       CREATED          SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-rust   latest    82c6fdabed5f   27 seconds ago   1.22MB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et bien, 2 fois moins !&lt;/p&gt;
&lt;p&gt;Quelle est cette sorcellerie ?!!&lt;/p&gt;
&lt;p&gt;Et bien c’est le LTO qui fait tout le travail, ce n’est pas la libc qui est embarquée, seulement les bouts intéressants.&lt;/p&gt;
&lt;p&gt;Et si on veut grapiller les dernière miettes, on peut passer en nightly et &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std&quot;&gt;recompiler la rust-std&lt;/a&gt; nous même pour espérer l’optimiser.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ... &amp;lt;le reste du stage&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on ajoute le channel nightly&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; rustup install nightly&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on récupère les source de la lib std pour l&amp;#39;architecture linux x64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on compile la lib-std pour l&amp;#39;architecture linux x64 en même temps que le binaire&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; RUSTFLAGS=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-C target-feature=+crt-static&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; cargo +nightly build -Zbuild-std=core,std,panic_abort --target x86_64-unknown-linux-gnu --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; La source dans le builder est différente&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder target/x86_64-unknown-linux-gnu/release/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Toujours debout!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-rust .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on racle les fonds de tiroirs ^^&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY       TAG       IMAGE ID       CREATED         SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-rust   latest    b6df1c6617f4   7 minutes ago   1.14MB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;upx&quot;&gt;UPX&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://upx.github.io/&quot;&gt;UPX&lt;/a&gt; est compresseur binaire. Son rôle est de réduire la taille d’un binaire. Et pas qu’un peu.&lt;/p&gt;
&lt;p&gt;On va l’utiliser avec les réglages les plus violents&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;upx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;-ultra-brute&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; /path/to/binary&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui donne le Dockerfile suivant.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ... &amp;lt;le reste du stage&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; RUSTFLAGS=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-C target-feature=+crt-static&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; cargo +nightly build -Zbuild-std=core,std,panic_abort --target x86_64-unknown-linux-gnu --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; exécution de UPX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;lt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wget &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tar -xf upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mv upx-4.2.4-amd64_linux/upx /usr/bin/upx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;upx -9 --ultra-brute target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; La source dans le builder est différente&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder target/x86_64-unknown-linux-gnu/release/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et donc, ça tourne toujours? et la taille ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-rust .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY       TAG       IMAGE ID       CREATED              SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-rust   latest    a3a324028a28   About a minute ago   440kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;440 Ko !! Joli ! Et là c’est libc comprise ❤️&lt;/p&gt;
&lt;details &gt;
	&lt;summary&gt;&lt;span&gt;Le Dockerfile complet&lt;/span&gt;&lt;/summary&gt;
	&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; rust &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cat &amp;lt;&amp;lt;EOF &amp;gt; Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[package]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;name = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;version = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.1.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;edition = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;2021&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[profile.release]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;strip = true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;opt-level = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lto = true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;codegen-units = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;panic = &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;abort&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; mkdir src &amp;amp;&amp;amp; cat &amp;lt;&amp;lt;EOF &amp;gt; src/main.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;![no_main]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use std::io;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use std::io::Write;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use std::net::{TcpListener, TcpStream};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fn handle_connection(acceptation_result: io::Result&amp;lt;TcpStream&amp;gt;) -&amp;gt; io::Result&amp;lt;()&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let mut stream = acceptation_result?;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stream.write_all(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length:2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.as_bytes())?;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ok(())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;[no_mangle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fn main() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let server = TcpListener::bind(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0:8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;).unwrap();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    for acceptation_result in server.incoming() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        if let Err(response_error) = handle_connection(acceptation_result) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            println!(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;An error occurred {response_error}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; rustup install nightly&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; RUSTFLAGS=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-C target-feature=+crt-static&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; cargo build --release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;lt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wget &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tar -xf upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mv upx-4.2.4-amd64_linux/upx /usr/bin/upx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;upx -9 --ultra-brute target/release/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder target/release/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;p&gt;On en a fini avec Rust qui m’a largement surpris dans la légèreté du binaire créé.&lt;/p&gt;
&lt;h2 id=&quot;golang&quot;&gt;Golang&lt;/h2&gt;
&lt;p&gt;On peut s’attaquer à son “concurrent” : le Golang.&lt;/p&gt;
&lt;p&gt;Alors qu’en vrai le Rust et le Go n’ont rien à voir entre eux, Rust est langage de haut niveau pour manipuler du bas niveau, il est très proche de la machine.&lt;/p&gt;
&lt;p&gt;Contrairement au Go qui possède un &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://golab.io/talks/understanding-the-go-runtime&quot;&gt;runtime&lt;/a&gt; au même titre que du javascript par exemple.&lt;/p&gt;
&lt;p&gt;Ce runtime gère une foultitude de choses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;liaison avec l’OS au travers de la libc&lt;/li&gt;
&lt;li&gt;scheduler : ordonnance les tâches à réaliser (très proche d’un tokio pour Rust)&lt;/li&gt;
&lt;li&gt;allocation mémoire&lt;/li&gt;
&lt;li&gt;cryptographie&lt;/li&gt;
&lt;li&gt;garbage collector&lt;/li&gt;
&lt;li&gt;…&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Bref, avant d’arriver à notre code, une montagne de choses se passent.&lt;/p&gt;
&lt;img class=&quot;&quot; width=&quot;100%&quot; src=&quot;/assets/images/http-smol/go_runtime.png&quot; /&gt;
&lt;p&gt;Ce runtime va nous freiner très fort dans notre optimisation du binaire.&lt;/p&gt;
&lt;p&gt;Mais bon, j’ai été surpris du résultat en Rust, alors pourquoi pas en Go ^^&lt;/p&gt;
&lt;h3 id=&quot;implementation&quot;&gt;Implémentation&lt;/h3&gt;
&lt;p&gt;Voici une implémentation en Go&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.go&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;package&lt;/span&gt;&lt;span&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;import&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Fermeture du socket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; closeConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;conn&lt;/span&gt;&lt;span&gt; net&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Conn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;	fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Closing connection &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RemoteAddr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;	err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	if&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; nil&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Fprintf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Stderr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Exit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Logique de gestion du socket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; handleConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;conn&lt;/span&gt;&lt;span&gt; net&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Conn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; fonction lancée à la sortie de la fonction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	defer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; closeConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;conn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	if&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; nil&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Fprintf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Stderr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;		//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; il faut l&amp;#39;appelée explicitement ici car les defer ne sont pas pris en compte en cas d&amp;#39;exit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;		closeConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;conn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Exit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on écrit dans le socket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;	_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length:2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	if&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; nil&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Fprintf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Stderr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write to connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;	address&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0:8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on créé le listener&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;	listener&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; net&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Listen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;tcp&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	if&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; nil&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Fprintf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Stderr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to listen to port : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&amp;gt; &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;		//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on quitte sinon c&amp;#39;est segfault&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		os&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Exit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on boucle sur les évènements à venir&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;		//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on accepte les connexions entrantes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;		conn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; listener&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Accept&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;		handleConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;conn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On build!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;go build -o http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le code fonctionne.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Closing connection 127.0.0.1:48006&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et arrive à:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3.0M    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C’est moins pire que ce que je ne pensais. Mais en Rust aussi au premier coup on était sur du 3.9 Mo, soit même plus.&lt;/p&gt;
&lt;h3 id=&quot;remarques&quot;&gt;Remarques&lt;/h3&gt;
&lt;p&gt;Avant d’optimiser, j’ai quelques que remarque de noob en Go, qui passe sa vie en Rust.&lt;/p&gt;
&lt;p&gt;Go a des petites surprises, on lui donne comme réputation d’être un langage simple, mais en fait il y a surtout des commodités dont j’ai trop pris l’habitude en Rust qui
m’ont manqué.&lt;/p&gt;
&lt;p&gt;Par exemple, il est assez facile de by-pass les vérifications d’erreurs et de créer des états inconsistents dans le programme. Bon souvent ça finit en segfault et donc ça dead
mais ça dead sale.&lt;/p&gt;
&lt;p&gt;Voici ma première tentative quand je ne faisais pas de &lt;code&gt;os.Exit(1)&lt;/code&gt; après que le &lt;code&gt;net.Listen&lt;/code&gt; m’ait renvoyé son erreur que j’avais copieusement ignorée ^^&lt;/p&gt;
&lt;img class=&quot;&quot; width=&quot;100%&quot; src=&quot;/assets/images/http-smol/go_erreur.png&quot; /&gt;
&lt;p&gt;Ensuite, des choses dont je ne préoccupe plus car c’est le langage qui le fait pour moi en Rust, c’est la libération des ressources système. En Go, elle est explicite.&lt;/p&gt;
&lt;p&gt;En Rust c’est implicite. Et bien pratique. 😃&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;The connection will be closed when the value is dropped&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Il existe un mot-clef &lt;code&gt;defer&lt;/code&gt; qui a le même comportement que le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://fr.wikipedia.org/wiki/Resource_acquisition_is_initialization&quot;&gt;RAII&lt;/a&gt; de Rust.&lt;/p&gt;
&lt;p&gt;En Go. il faut créé le RAII car le mécanisme d’ownership n’existe pas.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; toto&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    x&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;Ressource&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    defer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; x&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Clean&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    x&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Init&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on fait des trucs ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; x sort du scope de toto -&amp;gt; x.Clean() est appelée et x marquée comme nettoyable par le GC&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ça y est, j’ai fini de me plaindre 🤣&lt;/p&gt;
&lt;h3 id=&quot;optimisations-1&quot;&gt;Optimisations&lt;/h3&gt;
&lt;p&gt;Comme pour Rust, je vais suivre un &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://dev.to/aryaprakasa/the-trade-offs-of-optimizing-and-compressing-go-binaries-492d&quot;&gt;cookbook d’optimisation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Le premier conseil est de supprimer les symboles de débug &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://dwarfstd.org/&quot;&gt;DWARF&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ go build -o http-okay -ldflags &amp;quot;-w&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2.2M    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On descend déjà.&lt;/p&gt;
&lt;p&gt;Le seconde est d’enlever la table de symboles, ceci fait perdre aux profiler la possibilités de comprendre ce qui se passe mais réduit encore un peu la taille.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ go build -o http-okay -ldflags &amp;quot;-w -s&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2.0M    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Les optimisations suivantes sont plus tricky.&lt;/p&gt;
&lt;p&gt;La première désactive l’inlining, ce qui veut dire que l’on perd des performances pour gagner de la taille de binaire.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ go build -o http-okay -ldflags &amp;quot;-w -s&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -s http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2000    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ go build -o http-okay -ldflags &amp;quot;-w -s&amp;quot; -gcflags=all=&amp;quot;-l&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -s http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1952    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dans notre cas cela ne change pas grand chose, mais on voit 50 octets de moins.&lt;/p&gt;
&lt;p&gt;La dernière optimisation c’est n’importe quoi, on casse le langage en deux en désactivant le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://en.wikipedia.org/wiki/Bounds_checking&quot;&gt;bound check&lt;/a&gt;, c’est à dire que l’on
ne quitte pas une slice (coucou buffer overflow 👋).&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ go build -o http-okay -ldflags &amp;quot;-w -s&amp;quot; -gcflags=all=&amp;quot;-l -B&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -s http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1916    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on peut même faire complètement n’importe quoi en désactivant les &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://ihagopian.com/posts/write-barriers-in-the-go-garbage-collector&quot;&gt;Writes Barrier&lt;/a&gt; 🤣&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ go build -o http-okay -ldflags &amp;quot;-w -s&amp;quot; -gcflags=all=&amp;quot;-l -B -wb=false&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -s http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1872    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Faire cela empêche Go de fonctionner correctement en concurrence. Ne savant pas ce que cela peut donner, je vais rester au &lt;code&gt;-ldflags &quot;-w -s&quot;&lt;/code&gt; et ses 2 Mo de binaire compilé.&lt;/p&gt;
&lt;h3 id=&quot;docker-1&quot;&gt;Docker&lt;/h3&gt;
&lt;p&gt;Même principe que pour Rust, mais on va directement passer par UPX cette fois-ci.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; golang &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;as&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; main.go .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cat &amp;lt;&amp;lt;EOF &amp;gt; go.mod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;module http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; go build -o http-okay -ldflags &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-w -s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;lt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;apt update&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;apt install -y xz-utils&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wget &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tar -xf upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mv upx-4.2.4-amd64_linux/upx /usr/bin/upx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;upx -9 --ultra-brute http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /go/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Résultat 🥁&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build . -t http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY     TAG       IMAGE ID       CREATED         SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-go   latest    64684cbf3426   5 minutes ago   699kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Que 700 Ko! 🤩&lt;/p&gt;
&lt;p&gt;Franchement, je ne pensais pas que l’on descendrait aussi bas 😅&lt;/p&gt;
&lt;p&gt;Mais wait a minute&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it -p 8080:8080 http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ echo $?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;127&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;En fait ça crash parce que comme pour Rust, on a besoin de la libc qui s’est fait la malle dans le builder.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        linux-vdso.so.1 (0x00007fffce7c7000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f706d33a000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        /lib64/ld-linux-x86-64.so.2 (0x00007f706d555000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Du coup nouvelle essaie avec la libc.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; golang &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le reste du stage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; upx -9 --ultra-brute http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /go/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et maintenant?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it -p 8080:8080 http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Closing connection 172.17.0.1:60978&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C’est mieux! Et la taille?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY     TAG       IMAGE ID       CREATED          SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-go   latest    d055eae0f1cb   10 minutes ago   2.83MB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Aouch ! Ah oui ça coûte cher!&lt;/p&gt;
&lt;p&gt;Heureusement, on peut utiliser le même trick que pour Rust et &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://mt165.co.uk/blog/static-link-go/&quot;&gt;compiler statiquement&lt;/a&gt; le binaire.&lt;/p&gt;
&lt;p&gt;Pour cela, on rajoute la variable d’environnement &lt;code&gt;CGO_ENABLED=0&lt;/code&gt;, qui va désactiver le pont vers le C et donc forcer à tout faire rentrer dans le Go.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; golang &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENV&lt;/span&gt;&lt;span&gt; CGO_ENABLED=0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; go build -o http-okay -ldflags &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-w -s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le reste du stage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; upx -9 --ultra-brute http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /go/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Résultat ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build . -t http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it -p 8080:8080 http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Closing connection 172.17.0.1:52148&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et la taille?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images http-okay-go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REPOSITORY     TAG       IMAGE ID       CREATED              SIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-go   latest    1de49c02ca76   About a minute ago   683kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Allez! 700 Ko tout frais compris 🤩&lt;/p&gt;
&lt;h3 id=&quot;tinygo&quot;&gt;TinyGo&lt;/h3&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://tinygo.org/&quot;&gt;TinyGo&lt;/a&gt; est une variante de Go faite pour l’embarqué et le WASM et donc a comme promesse d’être plus léger et plus efficace.&lt;/p&gt;
&lt;p&gt;Malheureusement, cette légèreté vient avec un coût bien trop important: il n’y a pas de possibilité de discuter avec la carte réseau !!&lt;/p&gt;
&lt;p&gt;En effet, la lib &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://pkg.go.dev/net#ListenConfig.Listen&quot;&gt;net&lt;/a&gt; classique a été entièrement &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/tinygo-org/net/blob/main/dial.go#L200&quot;&gt;réécrite&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Compiler notre code va provoquer cette erreur.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Unable to listen to port : 0.0.0.0:8080 =&amp;gt; Lookup of host name &amp;#39;0.0.0.0&amp;#39; failed: Netdev not set&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pourquoi? Et bien parce que tiny-go ne fourni pas d’implémentation de “Netdev”, à part &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/tinygo-org/net/blob/main/netdev.go#L162&quot;&gt;une&lt;/a&gt; qui renvoie systématiquement une erreur.&lt;/p&gt;
&lt;p&gt;Les implémentations sont trouvables dans le repo &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/tinygo-org/drivers&quot;&gt;drivers&lt;/a&gt;, la &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/tinygo-org/drivers/blob/release/rtl8720dn/rtl8720dn.go#L576&quot;&gt;rtl8720dn&lt;/a&gt; par exemple est une antenne wifi.&lt;/p&gt;
&lt;p&gt;Mais rien qui ressemble de près ou de loin à une implémentation générique passant par la libc et délégant le travail
au kernel de gestion de drivers.&lt;/p&gt;
&lt;p&gt;Donc pas de tiny-go pour nous. 😥&lt;/p&gt;
&lt;h2 id=&quot;vlang&quot;&gt;Vlang&lt;/h2&gt;
&lt;p&gt;Alors &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://vlang.io/&quot;&gt;Vlang&lt;/a&gt;, on a vu le Rust, on a vu le Golang, et bien en très gros vlang c’est la fusion de Rust et de Go mélangé à plein d’autres influences et qui ce
compile très bien C, mais en WASM et plus curieusement en JS !!!&lt;/p&gt;
&lt;p&gt;Il possède une &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://modules.vlang.io/&quot;&gt;librairie standard pantagruélique&lt;/a&gt;, un garbage collector débrayable et le modèle de concurrence des go routines.&lt;/p&gt;
&lt;p&gt;Nous allons effleurer ses capacités avec notre sujet.&lt;/p&gt;
&lt;h3 id=&quot;implementation-1&quot;&gt;Implémentation&lt;/h3&gt;
&lt;p&gt;Voici une implémentation de notre répondeur http en V.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.v&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;v&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;module&lt;/span&gt;&lt;span&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;import&lt;/span&gt;&lt;span&gt; net&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    mut&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; listener&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span&gt; net&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;listen_tcp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;ip&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0:8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; or&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        panic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to listen to port 8080&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        mut&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; conn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; :=&lt;/span&gt;&lt;span&gt; listener&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;accept&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; or&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; panic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept connection&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length:2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; or&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; or&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; panic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to close connection&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; or&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; panic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to close connection&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pour la suite des opérations je vais utiliser la version actuelle de V, c’est un projet qui évolue très vite donc je préfère le préciser.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;V 0.4.8 da3112e&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On build, vous aller voir que V va droit au but 😄&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;❯&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; main.v&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Voilà c’est compilé.&lt;/p&gt;
&lt;p&gt;Il devrait vous avoir créé ce qu’il faut&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ tree&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── main.v&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── v.mod&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Si vous voulez renommer le programme créé vous pouvez:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;❯&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; main.v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;❯&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; tree&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;├──&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;├──&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; main.v&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;└──&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; v.mod&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Est-ce qu’il fonctionne?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Visiblement oui.&lt;/p&gt;
&lt;p&gt;Du coup, le poids du binaire est de combien ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1.3M    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bon, début mais pas fantastique.&lt;/p&gt;
&lt;p&gt;Et build statique ou dynamique ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        linux-vdso.so.1 (0x00007ffead9e9000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2d7ed0f000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        /lib64/ld-linux-x86-64.so.2 (0x00007f2d7ef2a000)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dynamique, donc.&lt;/p&gt;
&lt;p&gt;Si l’on veut du static on peut faire&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v main.v -cflags &amp;quot;-static&amp;quot; -o http-okay-static&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay-static &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        not a dynamic executable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay-static &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2.0M    http-okay-static&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;optimisations-2&quot;&gt;Optimisations&lt;/h3&gt;
&lt;p&gt;C’est cool, mais ça ne nous avance pas beaucoup. C’est le moment d’optimiser !&lt;/p&gt;
&lt;p&gt;Vlang fourni un livre de &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://docs.vlang.io/performance-tuning.html&quot;&gt;recettes&lt;/a&gt; pour diminuer tout ça.&lt;/p&gt;
&lt;p&gt;Et comment l’on fait ça ?&lt;/p&gt;
&lt;p&gt;Comme d’habitude, on retire la première étapes consiste à retirer les symboles de debug.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v main.v -prod -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;200K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v main.v -prod -cflags &amp;quot;-static&amp;quot; -o http-okay-static&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay-static &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1.4M    http-okay-static&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ah oui tout de suite on est sur un autre game.&lt;/p&gt;
&lt;p&gt;On va couper d’autres branches.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v main.v -prod -skip-unused -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;196K    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;4Ko, c’est mieux que rien…&lt;/p&gt;
&lt;p&gt;Celui-là c’est presque de la triche 😄. C’est un UPX intégré.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v main.v -prod -skip-unused -compress  -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;84K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ v main.v -cflags &amp;quot;-static&amp;quot; -prod -skip-unused -compress -o http-okay-static&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;456K    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Mais du coup le binaire est coupé en deux 🤩&lt;/p&gt;
&lt;p&gt;Et je n’ai pas réussis à descendre plus bas.&lt;/p&gt;
&lt;h3 id=&quot;docker-2&quot;&gt;Docker&lt;/h3&gt;
&lt;p&gt;Fabriquons notre container&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; thevlang/vlang &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;as&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;WORKDIR&lt;/span&gt;&lt;span&gt; /opt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; main.v .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENV&lt;/span&gt;&lt;span&gt; CGO_ENABLED=0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; v main.v -cflags &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;-static&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; -prod -skip-unused -compress -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /opt/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Est-ce que ça continue fonctionner?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-vlang  .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-vlang&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Oui! Et le poids ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images | grep http-okay-vlang&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-vlang                  latest           9ce570aed25e   6 minutes ago    213kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Oh on atteint les 200ko tout frais compris!! 😍&lt;/p&gt;
&lt;p&gt;Mais préparez-vous à faire un bond de géant dans la prochaine partie.&lt;/p&gt;
&lt;h2 id=&quot;ziglang&quot;&gt;Ziglang&lt;/h2&gt;
&lt;p&gt;Je vais vous laisser découvrir le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://ziglang.org/&quot;&gt;langage&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;J’en ai réellement très peu fait, je ne vais pas pouvoir vous donner beaucoup de détails sur le langage en lui-même. Je peux simplement vous dire qu’il est compatible avec
le C/C++ et donc que l’on peut mixer du zig avec du C.&lt;/p&gt;
&lt;p&gt;Il fourni tout un tas de garantie sur la manipulation de la mémoire et de son allocation via le même système de &lt;code&gt;defer&lt;/code&gt; que Golang.&lt;/p&gt;
&lt;h3 id=&quot;implementation-2&quot;&gt;Implémentation&lt;/h3&gt;
&lt;p&gt;Voici une implémentation possible.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.zig&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; @import&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; net&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;net&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;() &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;anyerror&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; localhost&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; net&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;parseIp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0.0.0.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 8080&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; unreachable&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    var&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; localhost&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;listen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;server&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;accept&lt;/span&gt;&lt;span&gt;()) &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;stream&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;writer&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;        client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;stream&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;close&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    } &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; err&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On build!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ zig build-exe main.zig --name http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Au moins ça fonctionne !&lt;/p&gt;
&lt;p&gt;Et le poids ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2.2M    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C’est un peu beaucoup … 😥 J’avoue ma déception.&lt;/p&gt;
&lt;p&gt;Mais attendez, c’est du dynamique ou du statique ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        not a dynamic executable&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ah! Mais ça change tout, on peut alors optimiser !&lt;/p&gt;
&lt;h3 id=&quot;optimisations-3&quot;&gt;Optimisations&lt;/h3&gt;
&lt;p&gt;Il existe un petit &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://zig.guide/build-system/emitting-an-executable/&quot;&gt;guide&lt;/a&gt;, nous allons le suivre. 😎&lt;/p&gt;
&lt;p&gt;Nous allons rajouter les paramètres un à un et voir ce qui se passe.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ zig build-exe main.zig -O ReleaseSmall --name http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;20K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Je… hein !! De quoi ?! Bon ben fin du game 🤣 20 ko !!!!!!!!!&lt;/p&gt;
&lt;p&gt;On va rajouter la suite mais je suis déjà très satisfait.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ zig build-exe main.zig -O ReleaseSmall -fstrip -fsingle-threaded --name http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;12K     http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et ben OK, 8ko de moins, arriver à ce niveau, on accepte. 😄&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-question&quot;&gt;
	&lt;p&gt;Comment une telle sorcellerie est possible ?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Je vous est dit que l’on pouvait mixer du C et du zig. C’est justement ce qui se passe, la libc est mixé avec le zig et complètement dépouillé de ce qui n’est pas appellé dedans.&lt;/p&gt;
&lt;p&gt;Résultat, le binaire possède les syscalls nécessaire et rien de plus ! Ou en tout cas du code compréhensible comme tel.&lt;/p&gt;
&lt;h3 id=&quot;docker-3&quot;&gt;Docker&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; chainguard/zig  &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;WORKDIR&lt;/span&gt;&lt;span&gt; /opt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; main.zig .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; zig build-exe main.zig -O ReleaseSmall -fstrip -fsingle-threaded --name http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /opt/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-zig .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-zig&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images | grep http-okay-zig&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-zig                    latest           708eb4985347   About a minute ago   12.1kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et du coup notre image finale fonctionnelle, fait 12 ko. 😍&lt;/p&gt;
&lt;h2 id=&quot;c&quot;&gt;C&lt;/h2&gt;
&lt;p&gt;La question est donc. Est ce que l’on peut mieux faire ?&lt;/p&gt;
&lt;p&gt;C’est à ce moment que je risque de me prendre des coups de fourches dans le derge ^^’&lt;/p&gt;
&lt;p&gt;Je vais tenter une implémentation en C qui ne sera pas parfaite mais qui sera suffisante.&lt;/p&gt;
&lt;h3 id=&quot;implementation-3&quot;&gt;Implémentation&lt;/h3&gt;
&lt;p&gt;Tout étant très compliqué, je vais y aller pas à pas pour ne pas vous paumer.&lt;/p&gt;
&lt;p&gt;Parce que clairement, là on va faire du bas-niveau: jusqu’à maintenant la libc était un mirage au loin, on en avait besoin mais on ne la voyait pas directement.&lt;/p&gt;
&lt;p&gt;On commence par un main tout simple.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.c&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pour construire notre executable on utilise &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://gcc.gnu.org/&quot;&gt;gcc&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;16K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        linux-vdso.so.1 (0x00007ffc059e5000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f539e48f000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        /lib64/ld-linux-x86-64.so.2 (0x00007f539e6af000)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Il est comme vous pouvez le voir dynamique et fait 16ko.&lt;/p&gt;
&lt;p&gt;Mais pour le moment, il ne fait rien.&lt;/p&gt;
&lt;p&gt;Nous allons remédier à ça.&lt;/p&gt;
&lt;h4 id=&quot;listener-tcp&quot;&gt;Listener TCP&lt;/h4&gt;
&lt;p&gt;La première étape est de récupérer un listener TCP.&lt;/p&gt;
&lt;p&gt;Mais cela ne va pas être si simple.&lt;/p&gt;
&lt;p&gt;Dans les autres langages, il y avait des &lt;code&gt;net.Listen(&quot;tcp&quot;, &quot;0.0.0.0:8080&quot;)&lt;/code&gt; qui facilitait la création du listener.&lt;/p&gt;
&lt;p&gt;Cette fois-ci, il faut le faire à la main et donc comprendre les rouages.&lt;/p&gt;
&lt;p&gt;Ce qu’il faut comprendre c’est que dans un linux/windows/macos, le programme que vous exécutez n’a pas tous les droits, il ne peut pas atteidre la carte réseau par exemple.&lt;/p&gt;
&lt;p&gt;Il doit demander des accès au système d’exploitation et plus particulièrement à une partie privilégiée appelée le &lt;code&gt;kernel&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Comme dit précédemment, on parle avec lui au-travers des syscalls fournis par la libc.&lt;/p&gt;
&lt;p&gt;Pour ranger les choses, le kernel défini des enclaves qui restraignent les processus utilisateurs dans leurs actions.&lt;/p&gt;
&lt;p&gt;Une carte réseau ne peut-faire qu’une chose à la fois. Si deux processus y accédaient directement, ils se marcheraient sur les pieds et casserait le fonctionnement de la carte
réseau.&lt;/p&gt;
&lt;p&gt;Pour pallier à ce problème, un seul processus ne peut y accéder, et ce processus est le kernel, les autres processus n’ont droit que de lui demander gentillement de faire des
choses.&lt;/p&gt;
&lt;p&gt;Le kernel lui-même, ne pouvant faire qu’une chose à la fois, il va pour satisfaire tout le monde faire ce que l’on a dans le monde réel: des guichets et des files d’attentes.&lt;/p&gt;
&lt;p&gt;Il faut imaginer un fast-food ou l’on passe commande, on vous fourni un ticket et vous patientez jusqu’à ce que ça soit votre tour. La cuisine c’est le kernel, le comptoir ou la borne c’est les syscalls.&lt;/p&gt;
&lt;h5 id=&quot;socket-fd&quot;&gt;Socket FD&lt;/h5&gt;
&lt;p&gt;Du coup, passons commande à la cuisine !&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.c&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; socket&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nous allons lui demander de nous fournir un objet spécial qui se nomme un &lt;code&gt;socket&lt;/code&gt; et nous allons lui demander que la ressource ressemble à &lt;code&gt;addresse:port&lt;/code&gt; avec &lt;code&gt;AF_INET&lt;/code&gt; et du TCP via &lt;code&gt;SOCK_STREAM&lt;/code&gt;. Le &lt;code&gt;0&lt;/code&gt; signifie, “choisi le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.ibm.com/docs/en/aix/7.2?topic=protocols-socket&quot;&gt;protocole&lt;/a&gt;”.&lt;/p&gt;
&lt;p&gt;Le retour de la fonction est un entier, littérallement le numéro de guichet où se rendre pour sa commande. En programmation système, on nomme ça un &lt;code&gt;file descriptor&lt;/code&gt; ou “fd”.
D’où le nom de ma variable. En linux tout est fichier.&lt;/p&gt;
&lt;p&gt;Mais il se peut que la cuisine soit débordé, qu’il n’y ait pas le plat ou que l’on ne veuille pas vous servir pour une raison ou pour une autre.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;socket&lt;/code&gt; marche de même:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;socket() renvoie un descripteur référençant la socket créée en cas de réussite. En cas d&amp;#39;échec -1 est renvoyé, et errno contient le code d&amp;#39;erreur.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;details &gt;
	&lt;summary&gt;&lt;span&gt;Liste des errurs possible&lt;/span&gt;&lt;/summary&gt;
	&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EACCES&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    La création d&amp;#39;une socket avec le type et le protocole indiqués n&amp;#39;est pas autorisée.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EAFNOSUPPORT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    L&amp;#39;implémentation ne supporte pas la famille d&amp;#39;adresses indiquée.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EINVAL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Protocole inconnu, ou famille de protocole inexistante.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EINVAL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Attributs incorrects dans type.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EMFILE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    La table des fichiers est pleine.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ENFILE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    La limite du nombre total de fichiers ouverts sur le système a été atteinte.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ENOBUFS ou ENOMEM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Pas suffisamment d&amp;#39;espace pour allouer les tampons nécessaires. La socket ne peut être créée tant que suffisamment de ressources ne sont pas libérées.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EPROTONOSUPPORT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Le type de protocole, ou le protocole lui-même n&amp;#39;est pas disponible dans ce domaine de communication.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.c&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;errno.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdio.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdlib.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;string.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; demande du socket fd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; socket&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; vérification que tout s&amp;#39;est bien passé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to create socket file descriptor: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on quitte en erreur&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Je vous fais grâce du &lt;code&gt;if&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;fprintf&lt;/code&gt; permet d’écrire dans un fd, et nous utilisons un le fd &lt;code&gt;stderr&lt;/code&gt; pour avertir d’un problème, en linux tout est fichier. Ensuite on formate une chaîne de caractères
avec l’erreur du &lt;code&gt;errno&lt;/code&gt; mais au lieu d’afficher un code d’erreur il existe une méthode &lt;code&gt;strerror&lt;/code&gt; qui converti le code en erreur lisible par un humain.&lt;/p&gt;
&lt;p&gt;Si ça se passe mal, on coupe tout avec &lt;code&gt;exit(1)&lt;/code&gt;.&lt;/p&gt;
&lt;h5 id=&quot;socket-addr&quot;&gt;Socket Addr&lt;/h5&gt;
&lt;p&gt;Bon nous avons notre socket, mais pour le moment il ne fait rien, on a juste dit qu’on passerait commande à la cuisine, mais on a pas encore choisi les détails du repas.&lt;/p&gt;
&lt;p&gt;Pour cela nous avons besoin de créer deux structures &lt;code&gt;sockaddr_in&lt;/code&gt;. Celle représente un socket Internet avec un port en adresse.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr_in &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    short&lt;/span&gt;&lt;span&gt;            sin_family&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;   //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; e.g. AF_INET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span&gt;   sin_port&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;     //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; e.g. htons(3490)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; in_addr   sin_addr&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;     //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; see struct in_addr, below&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; in_addr &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; long&lt;/span&gt;&lt;span&gt; s_addr&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; load with inet_aton()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Une fois que l’on a déclaré le &lt;code&gt;server_address&lt;/code&gt;, il faut maintenant le configurer, mais avant cela, il faut faire le ménage.&lt;/p&gt;
&lt;p&gt;En c et en fait dans n’importe quel langage, même si c’est caché, une structure est un espace contigue de données. Ces données en mémoire sont des cases. Et ces cases sont
régulièrement réutilisée sans faire le nettoyage derrière. (ouais comme des soirées BDE craignos…).&lt;/p&gt;
&lt;p&gt;Or, le kernel n’accepte que les choses bien propre et impose que notre structures se termine par des zéros.&lt;/p&gt;
&lt;p&gt;Pour cela, il existe une méthode &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.man-linux-magique.net/man3/bzero.html&quot;&gt;bzero&lt;/a&gt; qui écrit des zéro pour nous. Il lui faut l’adresse de départ et le nombre de zéros à écrire.&lt;/p&gt;
&lt;p&gt;Pour l’adresse de départ, c’est facile, c’est celle de la structure &lt;code&gt;server_address&lt;/code&gt;, donc &lt;code&gt;&amp;amp;server_address&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Pour le nombre de zéros on utilise la taille de la structure via &lt;code&gt;sizeof&lt;/code&gt; fourni par le langage.&lt;/p&gt;
&lt;p&gt;Au final, cela donne.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;bzero&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;server_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;server_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bon, on a une structure toute propre, maintenant nous pouvons définir ses champs.&lt;/p&gt;
&lt;p&gt;D’abord le &lt;code&gt;sin_family&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;server_address.sin_family &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C’est le même que pour la déclaration du &lt;code&gt;socket_fd&lt;/code&gt;, pas de difficultés.&lt;/p&gt;
&lt;p&gt;Ensuite le &lt;code&gt;sin_port&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Cela se complique un peu.&lt;/p&gt;
&lt;p&gt;Pour des raisons de lisibilité, on déclare une constante qui contient le port d’écoute.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span&gt; PORT &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 8080&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis l’on défini le champ de la structure, mais attention, il y a un piège, le &lt;code&gt;8080&lt;/code&gt; ne veut rien dire pour la carte réseau qui va faire n’importe quoi!&lt;/p&gt;
&lt;p&gt;Pour cela il existe une &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/3/htons&quot;&gt;série de fonctions&lt;/a&gt; qui va convertir notre nombre en un autre en changeant l’ordre de lecture des bits.&lt;/p&gt;
&lt;p&gt;Notre &lt;code&gt;PORT&lt;/code&gt; étant un &lt;code&gt;unsigned short&lt;/code&gt;, nous utilisons la variante &lt;code&gt;htons&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;server_address.sin_port &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htons&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;PORT&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pour l’adresse &lt;code&gt;sin_addr&lt;/code&gt;, on s’aperçoit que c’est elle même une structure du type &lt;code&gt;in_addr&lt;/code&gt; qui possède un unique champ &lt;code&gt;s_addr&lt;/code&gt; qui est cette fois-ci un &lt;code&gt;unsigned long&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Nous utiliserons donc la méthode &lt;code&gt;htonl&lt;/code&gt; pour faire la conversion.&lt;/p&gt;
&lt;p&gt;Mais la conversion de quoi?&lt;/p&gt;
&lt;p&gt;Nous vous lui dire: “écoute sur n’importe quel adresse”, ce qui se traduit par &lt;code&gt;0.0.0.0&lt;/code&gt; pour les humains et &lt;code&gt;0x0&lt;/code&gt; pour la machine, mais au-lieu d’écrire zéro, nous allons
utiliser une constante fourni par la libstd: &lt;code&gt;INADDR_ANY&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;server_address.sin_addr.s_addr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htonl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;INADDR_ANY&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;Ici le &lt;code&gt;htonl&lt;/code&gt; ne sert à rien car &lt;code&gt;0x00000000&lt;/code&gt; lu dans un sens ou dans un autre reste &lt;code&gt;0x00000000&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Mais c’est une bonne pratique de le faire.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Pour résumer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;netinet/in.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; demande du socket fd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On déclare deux structure de type `sockaddr_in`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; l&amp;#39;une pour le serveur, l&amp;#39;autre pour les clients &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; qui arriverons&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; sockaddr_in server_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client_address&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On nettoie les données précédentes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    bzero&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On définie les caractéristiques voulues pour notre socket &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_family&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_port&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htons&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_addr&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;s_addr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htonl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;INADDR_ANY&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h5 id=&quot;port-reuse&quot;&gt;Port reuse&lt;/h5&gt;
&lt;p&gt;Mais nous avons un problème, un port ne peut être attibué qu’à une seul processus.&lt;/p&gt;
&lt;p&gt;Ce sont les fameuses erreurs &lt;code&gt;address already in use&lt;/code&gt; que vous avez dans vos langages préférés.&lt;/p&gt;
&lt;p&gt;Mais le kernel est bête et discipliné, si on a dit qu’on voulait le port, il nous le donne, et si personne ne lui dit le contraire, il gardera cette information en
mémoire et refusera que tout autre processus ne puisse l’utiliser.&lt;/p&gt;
&lt;p&gt;Y compris notre processus si on le redémarre. 🙄&lt;/p&gt;
&lt;p&gt;Nous allons devoir demander gentillement au kernel de pouvoir réutiliser le port si on n’est plus là.&lt;/p&gt;
&lt;p&gt;Pour cela il existe une méthode &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/2/setsockopt&quot;&gt;setsockopt&lt;/a&gt; qui permet de définir une variable  &lt;code&gt;SO_REUSEADDR&lt;/code&gt; qui peut valoir &lt;code&gt;1&lt;/code&gt; ou &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Le prototype de &lt;code&gt;setsockopt&lt;/code&gt; est un peu complexe.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; setsockopt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; sockfd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; level&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; optname&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;optval&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; socklen_t&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; optlen&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Il demande un fd, ça on a.&lt;/p&gt;
&lt;p&gt;Puis un “level”, assez énigmatique, mais la description est assez claire.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;When manipulating socket options, the level at which the option resides and the name of the option must be specified. To manipulate options at the sockets API level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;En gros toutes les options qui commmence par &lt;code&gt;SO_*&lt;/code&gt; sont du &lt;code&gt;SOL_SOCKET&lt;/code&gt;, &lt;code&gt;SOL&lt;/code&gt; signifiant &lt;code&gt;socket_level&lt;/code&gt;&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;On aurait très bien pu modifier des options sur le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/7/tcp&quot;&gt;TCP&lt;/a&gt; (section Socket options), on aurait alors utilisé &lt;code&gt;IPPROTO_TCP&lt;/code&gt; comme “level” à la place.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Ensuite, on nous demande une référence vers une valeur &lt;code&gt;const void *optval&lt;/code&gt;, (le &lt;code&gt;void*&lt;/code&gt; peut-être n’importe quoi, c’est juste une adresse).&lt;/p&gt;
&lt;p&gt;Et finalement, la taille de la valeur.&lt;/p&gt;
&lt;p&gt;Le retour lui est un nombre qui vaut &lt;code&gt;-1&lt;/code&gt; si l’opération a échoué. Genre réutiliser un port déjà utilisé.&lt;/p&gt;
&lt;p&gt;Cela donne avec la gestion d’erreur.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; setsockopt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;socket_fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; SOL_SOCKET&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; SO_REUSEADDR&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;reuse&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reuse&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to set SO_REUSEADDR to socket&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Voilà, nous avons demandé poliment de pouvoir réutiliser le port! ❤️&lt;/p&gt;
&lt;h5 id=&quot;bind&quot;&gt;Bind&lt;/h5&gt;
&lt;p&gt;C’est bien beau d’avoir décrit ce que l’on désirait via &lt;code&gt;server_address&lt;/code&gt;, mais pour l’instant c’est comme si c’était sur le bon commande non transmis à la cuisine: votre
plat n’arrivera jamais !&lt;/p&gt;
&lt;p&gt;Il faut créer une commande à partir de ce bon de commande.&lt;/p&gt;
&lt;p&gt;Pour cela il existe un syscall nommé &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://man7.org/linux/man-pages/man2/bind.2.html&quot;&gt;bind&lt;/a&gt; qui vient lier notre structure avec le socket créé précédemment.&lt;/p&gt;
&lt;p&gt;Voici, son prototype.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; sockfd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;addr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; socklen_t&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; addrlen&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Premier paramètre est connu.&lt;/p&gt;
&lt;p&gt;Le second est plus énigmatique &lt;code&gt;struct sockaddr*&lt;/code&gt;, il s’agit de l’adresse d’une structure &lt;code&gt;sockaddr&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; sa_family&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;               sa_data&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;14&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Mais on ne possède pas cette structure, notre type est &lt;code&gt;sockaddr_in&lt;/code&gt; et non &lt;code&gt;sockaddr&lt;/code&gt;. Du coup, comment on peut faire?&lt;/p&gt;
&lt;p&gt;En C, on raisonne en case mémoires, chaque type possède une taille en mémoire qui est fournie par &lt;code&gt;sizeof&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;char  =&amp;gt; 1 case&lt;/li&gt;
&lt;li&gt;short =&amp;gt; 2 cases&lt;/li&gt;
&lt;li&gt;long  =&amp;gt; 4 cases&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Pour rappel, sa déclaration est:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr_in &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    short&lt;/span&gt;&lt;span&gt;            sin_family&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;       //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 2 case&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span&gt;   sin_port&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;         //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 2 cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; long&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    sin_addr&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;s_addr&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 4 cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;             sin_zero&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;       //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 8 cases &amp;lt;= des zéros rajoutés automatiquement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;                                     //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;----------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;                                       //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 16 cases&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notre structure, rentre dans 16 cases.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sa_data[14]&lt;/code&gt; défini un tableau de 14 éléments, ces 14 éléments sont de type &lt;code&gt;char&lt;/code&gt;, pour avoir sa taille on fait donc &lt;code&gt;1*14 = 14&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; sa_family&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 2 cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;               sa_data&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;14&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 14 cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;                                     //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;--------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;                                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 16 cases&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notre structure rentre dans les 16 cases. Mais pourquoi faire ses calculs d’apothicaires?&lt;/p&gt;
&lt;p&gt;Et bien au lieu de refabriquer une strucure à la main, et de concaténer (sin_port, sin_addr, sin_zero), on va dire au C, “tkt frère c’est bien l’adresse d’un &lt;code&gt;sockaddr&lt;/code&gt;”.&lt;/p&gt;
&lt;p&gt;Cette opération se nomme un &lt;code&gt;cast&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;server_address&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le troisième paramètre, permet d’éviter de lire plus loin que prévu et ne pas dépasser les zéros que l’on a initialisé via &lt;code&gt;bzero&lt;/code&gt; précédemment.&lt;/p&gt;
&lt;p&gt;On rassemble le tout et en réalisant la gestion d’erreur&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;socket_fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;server_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;server_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to bind the socket to file descriptor : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;blockquote class=&quot;markdown-alert-question&quot;&gt;
	&lt;p&gt;&lt;strong&gt;Mais pourquoi on ne peut pas lui donner le &lt;code&gt;sockaddr_in&lt;/code&gt; directement?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Parce que &lt;code&gt;sockaddr_in&lt;/code&gt; n’est pas la seule structure que l’on peut &lt;code&gt;bind&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Il existe une version &lt;code&gt;sockaddr_in6&lt;/code&gt; pour manipuler de l’IPV6 qui est plus grosse.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr_in6 &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    sa_family_t&lt;/span&gt;&lt;span&gt;     sin6_family&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    in_port_t&lt;/span&gt;&lt;span&gt;       sin6_port&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt;        sin6_flowinfo&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; in6_addr sin6_addr&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 16 &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt;        sin6_scope_id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;                                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; -----&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;                                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 28&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; in6_addr &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    uint8_t&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;   s6_addr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;16&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 16 * 1 = 16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C’est de cette manière que l’on gère la généricité en C. On donne une adresse de début et de fin, et le kernel se débrouille avec ce tableau de cases qui n’a plus son
sens mais qui retransformable en la bonne donnée au besoin via un cast inverse de &lt;code&gt;sockaddr&lt;/code&gt; vers &lt;code&gt;sockaddr_in&lt;/code&gt; ou &lt;code&gt;sockaddr_in6&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;La taille fournie, permettant de dépasser les limites des 16 cases.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h5 id=&quot;listen&quot;&gt;Listen&lt;/h5&gt;
&lt;p&gt;Nous avons enfin envoyé notre structure socket “entrante” dans le kernel.&lt;/p&gt;
&lt;p&gt;Mais si on ne fait rien, il ne se passera… rien.&lt;/p&gt;
&lt;p&gt;Il faut maintenant activement demander au kernel “d’écouter” les connection entrantes.&lt;/p&gt;
&lt;p&gt;Pour cela il existe un autre syscall qui se nomme &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://man7.org/linux/man-pages/man2/listen.2.html&quot;&gt;listen&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Son prototype est beaucoup plus simple:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; listen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; sockfd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; backlog&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le paramètre &lt;code&gt;backlog&lt;/code&gt; est le nombre de connexions entrantes que le kernel va bufferiser avant de les refuser. C’est en gros la taille de la file d’attente.&lt;/p&gt;
&lt;p&gt;On va mettre de manière arbitraire, 5 connexions max.&lt;/p&gt;
&lt;p&gt;Ce qui nous donne le bout de code suivant:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; listen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;socket_fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to listen to port &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Félicitation vous avez un listener TCP 😄&lt;/p&gt;
&lt;h5 id=&quot;on-resume&quot;&gt;On résume&lt;/h5&gt;
&lt;p&gt;C’est le moment de rassembler les bouts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;main.c&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;errno.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdio.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdlib.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;string.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;netinet/in.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span&gt; PORT &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 8080&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Création du socket côté kernel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; socket&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to create socket file descriptor: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Création des structures qui vont acceuillir la connexion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; d&amp;#39;écoute et les connexions entrantes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; sockaddr_in server_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client_address&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On nettoie les données précédemment existantes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    bzero&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On indique que l&amp;#39;on veut une IPV4 sur 0.0.0.0:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_family&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_port&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htons&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_addr&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;s_addr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htonl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;INADDR_ANY&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On demande de pouvoir réutiliser le port si c&amp;#39;est possible&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; setsockopt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOL_SOCKET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SO_REUSEADDR&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;reuse&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;reuse&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to set SO_REUSEADDR to socket&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On lie notre socket avec la structure dans notre code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; bind&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to bind the socket to file descriptor : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On demander de bufferiser 5 connexions entrantes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; listen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to listen to port &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Si on graphe nos appels systèmes, cela donne:&lt;/p&gt;
&lt;pre class=&quot;mermaid&quot;&gt;
  

flowchart TD
    bind(&quot;bind()&quot;)
    socket(&quot;socket()&quot;)
    setoptsock(&quot;setsockopt()&quot;)
    listen(&quot;listen()&quot;)
    socket -. optionel .-&gt; setoptsock
    setoptsock --&gt; bind
    socket --&gt; bind
    bind --&gt; listen


&lt;/pre&gt;
&lt;h4 id=&quot;boucle-d-acceptation-et-de-reponses&quot;&gt;Boucle d’acceptation et de réponses&lt;/h4&gt;
&lt;p&gt;Actuellement le kernel est abonné aux événement sur le &lt;code&gt;0.0.0.0:8080&lt;/code&gt;, bufferise les demandes jusqu’à 5, puis commence à les refuser.&lt;/p&gt;
&lt;h5 id=&quot;accept&quot;&gt;Accept&lt;/h5&gt;
&lt;p&gt;Il manque une pièce de puzzle dans notre implémentation: nous devons accepter les connexion entrantes.&lt;/p&gt;
&lt;p&gt;Pour cela il existe un autre syscall qui se nomme &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/2/accept&quot;&gt;accept&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Son prototype est&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; accept&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; sockfd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;addr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; socklen_t&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;addrlen&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Premier paramètre, notre bon vieux socket.&lt;/p&gt;
&lt;p&gt;Deuxième paramètre, le cast de l’adresse de la structure qui va accueillir la connexion entrante.&lt;/p&gt;
&lt;p&gt;Troisième paramètre, la taille de la structure pour les raisons exprimées précédemment.&lt;/p&gt;
&lt;p&gt;Le retour est également un entier qui si différent de -1, sera un autre socket FD créé par le kernel pour l’occasion.&lt;/p&gt;
&lt;p&gt;La structure “entrante” sera la &lt;code&gt;client_address&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; client_socket_len &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; accept&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;socket_fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; sockaddr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;client_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;client_socket_len&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept incoming connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h5 id=&quot;read&quot;&gt;Read&lt;/h5&gt;
&lt;p&gt;Maintenant que l’on sait que quelqu’un tape à la porte, et que l’on a ouvert la porte, nous allons attendre de savoir s’il y a réellement quelqu’un derrière ou si c’est
juste une plaisanterie.&lt;/p&gt;
&lt;p&gt;Pour vérifier cela, nous allons attendre que notre interlocuteur ne parle le premier.&lt;/p&gt;
&lt;p&gt;Pour cela, nous allons avoir besoin de place pour acceillir sa réponse.&lt;/p&gt;
&lt;p&gt;Nous allons nous contenter de 1ko de message en définissant un tableau de 1024 &lt;code&gt;char&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis nous allons utiliser le syscall &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/2/read&quot;&gt;read&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Qui a comme prototype:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;ssize_t&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;buf&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; size_t&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; count&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le premier paramètre est le socket de la connexion entrante.&lt;/p&gt;
&lt;p&gt;Le deuxième l’adresse du tableau de 1ko appelé &lt;em&gt;buffer de lecture&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;&lt;code&gt;void*&lt;/code&gt; veut dire : “adresse de quelque chose, quelque soit son type”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Et le troisième paramètre, la taille de ce quelque chose, ici de notre buffer &lt;code&gt;buf&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;La valeur de retour si différente de -1 est la taille à lire vu de notre perspective et la taille écrite vu de la perspective du kernel.&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-warning&quot;&gt;
	&lt;p&gt;Si on lit plus loin, c’est potentiellement n’importe quoi qui peut s’y trouver: un précédent appel, des données qui n’ont rien à voir, etc …&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Si c’est -1 le retour, alors il y a eu une erreur. La plupart des erreurs sont des échecs de lecture, sauf un qui se nomme &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://android.googlesource.com/platform/bionic/+/master/docs/EINTR.md&quot;&gt;EINTR&lt;/a&gt;. Le kernel, n’est pas à votre service exclusif, il fait d’autres choses et des choses potentiellement urgentes.&lt;/p&gt;
&lt;p&gt;Il peut décider en plein milieu de l’appel que finalement: “ouais, nan, j’ai plus envie de te répondre”.&lt;/p&gt;
&lt;p&gt;Il faut alors lui refaire la demande.&lt;/p&gt;
&lt;p&gt;On va donc boucler sur l’appel et le retenter si ça se passe mal.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; read&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to read socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A l’instant ou vous sortirez de la boucle, vous saurez que le client est toujours là. (oui on s’en fiche de ce qu’il raconte 🤣).&lt;/p&gt;
&lt;h5 id=&quot;write&quot;&gt;Write&lt;/h5&gt;
&lt;p&gt;Maintenant que l’on a la certitude que l’on ne va pas parler dans le vide, il est temps de répondre !&lt;/p&gt;
&lt;p&gt;Pour cela on va, vous commencer à connaître la chanson, utiliser un syscall &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/3/write&quot;&gt;write&lt;/a&gt;, qui comme son nom l’indique permet d’écrire. Et d’écrire
où? Sur la socket du client bien-sûr.&lt;/p&gt;
&lt;p&gt;Son prototype est:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;ssize_t&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;buf&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; size_t&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; nbyte&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Premier paramètre, le socket, deuxième le buffer de données à écrire, troisième, le nombre de bytes à écrire depuis ce buffer.&lt;/p&gt;
&lt;p&gt;Le retour si ce n’est pas -1 sera la taille écrite, si la taille écrite est inférieur à la taille voulu alors il faut écrire la taille manquante.&lt;/p&gt;
&lt;p&gt;On va d’abord se faire notre message à écrire&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; char&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ensuite on créé notre boucle d’écriture.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On définie un &lt;code&gt;offset&lt;/code&gt; à 0, et si on a pas tout écrit au tour courant, on recommence en se déplaçant d’autant de caractères écrits.&lt;/p&gt;
&lt;p&gt;On rajoute la gestion d’erreur.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Même principe que pour &lt;code&gt;read&lt;/code&gt;, le kernel à peut-être d’autre chose à faire et peu interrompre le syscall.&lt;/p&gt;
&lt;p&gt;On wrap notre boucle dans une autre.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Boucle d&amp;#39;écriture&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Boucle d&amp;#39;interruption&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Gestion d&amp;#39;erreur&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On rejoue l&amp;#39;écriture&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Ou on quitte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h5 id=&quot;on-rassemble-tout&quot;&gt;On rassemble tout&lt;/h5&gt;
&lt;p&gt;Voici à la fois la lecture et l’écriture.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On attend au moins un paquet de l&amp;#39;utilisateur&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; read&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to read socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; char&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;offset : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; replay writing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On va enfin pouvoir compiler !&lt;/p&gt;
&lt;details &gt;
	&lt;summary&gt;&lt;span&gt;main.c&lt;/span&gt;&lt;/summary&gt;
	&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;errno.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdio.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdlib.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;string.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;unistd.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;netinet/in.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span&gt; PORT &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 8080&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; socket&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to create socket file descriptor: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Socket struct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; sockaddr_in server_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client_address&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Fill with zeros the structure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    bzero&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Define socket characteristics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_family&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_port&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htons&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_addr&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;s_addr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htonl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;INADDR_ANY&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; setsockopt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOL_SOCKET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SO_REUSEADDR&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;reuse&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;reuse&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to set SO_REUSEADDR to socket&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Bind struct to fd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; bind&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to bind the socket to file descriptor : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Listen for connections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; listen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to listen to port &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; client_socket_len &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; accept&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sockaddr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;client_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;client_socket_len&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept incoming connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Wait for client message&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; read&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to read socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; char&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; replay writing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                    continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on oublie pas de fermer le socket lorsque l&amp;#39;on en a plus besoin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    close&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    close&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;Ce &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://man7.org/linux/man-pages/man2/close.2.html&quot;&gt;close&lt;/a&gt; est nécessaire pour expliquer au programme que l’on n’a plus besoin du socket, sinon il restera
réservé.&lt;/p&gt;
&lt;p&gt;Et au bout de quelques milliers de requêtes. Le système plantera.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bon, c’est prometteur 😍&lt;/p&gt;
&lt;p&gt;Mais petit soucis, le programme s’arrête à la première réponse donnée, on accepte une connexion, on répond et on meurt.&lt;/p&gt;
&lt;h5 id=&quot;boucler-sur-les-connexions-entrantes&quot;&gt;Boucler sur les connexions entrantes&lt;/h5&gt;
&lt;p&gt;Il est temps de boucler.&lt;/p&gt;
&lt;p&gt;Pour commencer on vient wrapper notre système de réponse dans une fonction qui prend en paramètre le socket de la connexion entrante.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; response_to_client&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; connection_fd&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Wait for client message&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; read&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to read socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; char&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; replay writing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                    continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis boucler sur les acceptations.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; client_socket_len &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; accept&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sockaddr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;client_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;client_socket_len&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept incoming connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    response_to_client&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    close&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On peut désormais lancer autant de requête que l’on veut. 🤩&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h5 id=&quot;sortie-du-programme&quot;&gt;Sortie du programme&lt;/h5&gt;
&lt;p&gt;Si vous faite un ctrl+c pour quitter le programme, il sort avec un code d’erreur différent de 0.&lt;/p&gt;
&lt;p&gt;C’est un peu sale, on peut améliorer les choses en rajoutant ce que l’on appel un &lt;em&gt;signal handler&lt;/em&gt;, qui a pour tâche de faire ce qui correct pour gérer le signal, ici de sortie.&lt;/p&gt;
&lt;p&gt;Puion déclare s une fonction sans paramètre.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; on déclare le socket_fd en global pour pouvoir le fermer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; socket_fd&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; signal_int_handler&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Exiting&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    close&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ensuite, au début du programme, nous enregistrons ce handler pour le signal &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://fr.wikipedia.org/wiki/SIGINT_(POSIX)&quot;&gt;SIGINT&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    signal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;SIGINT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; signal_int_handler&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Désormais, le programme se quittera sans erreurs.&lt;/p&gt;
&lt;details &gt;
	&lt;summary&gt;&lt;span&gt;main.c&lt;/span&gt;&lt;/summary&gt;
	&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;errno.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;signal.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdio.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;stdlib.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;string.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;unistd.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;netinet/in.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; short&lt;/span&gt;&lt;span&gt; PORT &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 8080&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; socket_fd&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; signal_int_handler&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Exiting&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    close&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; response_to_client&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; connection_fd&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Wait for client message&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; read&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to read socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si on ne lit rien c&amp;#39;est que le client est parti&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;read_size &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; char&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            int&lt;/span&gt;&lt;span&gt; write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; write&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strlen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; replay writing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;write_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;errno &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span&gt; EINTR&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;                    continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                printf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to write socket : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;                exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            offset &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; write_response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    signal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;SIGINT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; signal_int_handler&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; socket&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;socket_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to create socket file descriptor: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Socket struct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; sockaddr_in server_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client_address&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Fill with zeros the structure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;    bzero&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Define socket characteristics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_family&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_port&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htons&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    server_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;sin_addr&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;s_addr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; htonl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;INADDR_ANY&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; setsockopt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SOL_SOCKET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; SO_REUSEADDR&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;reuse&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;reuse&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;reuse_addr_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to set SO_REUSEADDR to socket&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Bind struct to fd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; bind&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sockaddr &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;server_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;bind_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to bind the socket to file descriptor : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Listen for connections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; listen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;listen_response &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to listen to port &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; PORT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        exit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        unsigned&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; client_socket_len &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span&gt; connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; accept&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socket_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sockaddr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;client_address&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;client_socket_len&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;connection_fd &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;            fprintf&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;Unable to accept incoming connection : &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-11&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; strerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        response_to_client&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;        close&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;connection_fd&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;p&gt;Bon on a enfin notre programme. 🤣&lt;/p&gt;
&lt;p&gt;Cela a necessité un peu plus de casse-tête que prévu.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -o http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bon et sa taille et son linkage ?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;20K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        linux-vdso.so.1 (0x00007ffd5916e000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f45f6c83000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        /lib64/ld-linux-x86-64.so.2 (0x00007f45f6ea3000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Okay ! 20ko et dynamique.&lt;/p&gt;
&lt;h3 id=&quot;optimisations-4&quot;&gt;Optimisations&lt;/h3&gt;
&lt;p&gt;Maintenant il est temps d’optimiser la taille.&lt;/p&gt;
&lt;h4 id=&quot;compilation-statique&quot;&gt;Compilation statique&lt;/h4&gt;
&lt;p&gt;On sait que &lt;code&gt;lib.c.so.6&lt;/code&gt; eest gros, bien plus que ce que l’on désire.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh /lib/x86_64-linux-gnu/libc.so.6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2.0M    /lib/x86_64-linux-gnu/libc.so.6&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;La somme de la libc et du binaire, va forcément être largement supérieur au 1 Mo, or, nous on veut descendre sous les 20ko.&lt;/p&gt;
&lt;p&gt;Nous savons que le C est suffisamment intelligent pour optimiser ce dont il a besoin. Comme nous ne partageront avec personne la libc puisque notre image docker sera
le seul binaire de notre image docker. Il n’est pas déconnant de la faire rentrer dans notre binaire.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;764K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    not a dynamic executable&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C fait son boulot et nettoie ce qui doit l’être, la somme des deux fait largement moins que 2 Mo. On progresse 😃&lt;/p&gt;
&lt;h4 id=&quot;supression-des-symboles-de-debug&quot;&gt;Supression des symboles de debug&lt;/h4&gt;
&lt;p&gt;Afin que les outils puisse marcher lors du debug, le compilateur va créer ce que l’on nomme une &lt;em&gt;table des symboles&lt;/em&gt; qui peut-être visualisé via &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://sourceware.org/binutils/docs/binutils/nm.html&quot;&gt;nm&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ nm ./http-okay | head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000004845e0 r CSWTCH.100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0000000000484600 r CSWTCH.97&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000004845e0 r CSWTCH.98&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0000000000484600 r CSWTCH.99&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000004a8108 V DW.ref.__gcc_personality_v0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;000000000047c008 R PORT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0000000000497d94 W _.stapsdt.base&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;000000000041c000 W _Exit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000004a7fe8 d _GLOBAL_OFFSET_TABLE_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000004a8140 D _IO_2_1_stderr_&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C’est bien pour le debug en dev, mais ça rajoute du poids supplémentaire donc “AFUERA!!” 🪒&lt;/p&gt;
&lt;p&gt;Pour cela on utilise un autre utilitaire qui se nomme &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://sourceware.org/binutils/docs/binutils/strip.html&quot;&gt;strip&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ strip http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ nm ./http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nm: ./http-okay: no symbols&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On gagne 80ko. 😃&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;Désormais toutes les tailles seront strippées.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4 id=&quot;optimisation-par-le-compilateur&quot;&gt;Optimisation par le compilateur&lt;/h4&gt;
&lt;p&gt;Le compilateur est capable de tout un tas &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html&quot;&gt;d’optimisations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Malheureusement, elles n’ont aucun impact sur notre programme qui est très simple et déjà très compact 😥&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -static -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -static -O1 -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -static -O3 -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -static -Os -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -static -Oz -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;remplacer-le-compilateur&quot;&gt;Remplacer le compilateur&lt;/h4&gt;
&lt;p&gt;Il existe d’autre compilateur nommé &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://clang.llvm.org/docs/CommandGuide/clang.html&quot;&gt;Clang&lt;/a&gt; qui possède également son set &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://gist.github.com/lolo32/fd8ce29b218ac2d93a9e&quot;&gt;d’optimisations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;En théorie, il peut créer des binaires plus petits.&lt;/p&gt;
&lt;p&gt;En théorie…&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ clang main.c -static -Oz -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;684K    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;changer-de-libc&quot;&gt;Changer de libc&lt;/h4&gt;
&lt;p&gt;La libc est lourde, elle fait 2 Mo, elle contient toute l’histoire de l’informatique, jamais on ne battera zig en essayant d’optimiser ça.&lt;/p&gt;
&lt;p&gt;Nous avons besoin que de syscalls très rudimentaires.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;socket&lt;/li&gt;
&lt;li&gt;setsockopt&lt;/li&gt;
&lt;li&gt;bind&lt;/li&gt;
&lt;li&gt;listen&lt;/li&gt;
&lt;li&gt;accept&lt;/li&gt;
&lt;li&gt;read&lt;/li&gt;
&lt;li&gt;write&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Et quelques structures comprises par le kernel. Mais nous embarquons la terre entière.&lt;/p&gt;
&lt;p&gt;Cette problématique je ne suis pas le seul à l’avoir, d’autre l’on eu et cela à déboucher au projet &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://musl.libc.org/&quot;&gt;musl&lt;/a&gt;, qui n’est ni plus ni moins qu’une
réimplémentation de la libc de manière &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://wiki.musl-libc.org/functional-differences-from-glibc.html&quot;&gt;opinated&lt;/a&gt; pour la rendre la plus légère possible.&lt;/p&gt;
&lt;p&gt;Elle vient avec son propre compilateur.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ musl-gcc main.c -static -Oz -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;32K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le code est parfaitement fonctionnel et ne fait plus que 32Ko. 😍&lt;/p&gt;
&lt;h4 id=&quot;upx-1&quot;&gt;UPX&lt;/h4&gt;
&lt;p&gt;Mais on peut encore descendre en compressant le bianaire via UPX.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ upx -9 --ultra-brute http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;20K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on descends à 20ko comme promis dans le titre de l’article. 😎&lt;/p&gt;
&lt;h4 id=&quot;utiliser-zig-pour-compiler-le-c&quot;&gt;Utiliser zig pour compiler le C&lt;/h4&gt;
&lt;p&gt;Mais du coup, si zig est si efficace à compiler du C, pourquoi ne pas le faire compiler notre programme ?&lt;/p&gt;
&lt;p&gt;Il clame&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Zig is better at using C libraries than C is at using C libraries&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Et bien vérifions leurs dires.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ zig build-exe main.c --library c --name http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;8.0K    http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        linux-vdso.so.1 (0x00007ffe5a7da000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f23b9c54000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        /lib64/ld-linux-x86-64.so.2 (0x00007f23b9e6f000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ gcc main.c -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;16K    http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, ça compile bien et ça créé bien un binaire 2 fois inférieur en taille à celui produit par gcc.&lt;/p&gt;
&lt;p&gt;Mais pour le moment c’est du dynamique.&lt;/p&gt;
&lt;p&gt;Pour passer en statique, il faut lui dire d’utiliser musl à la place de glibc. Pour cela nous utilisons la &lt;code&gt;-target x86_64-linux-musl&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ zig build-exe main.c --library c --name http-okay -target x86_64-linux-musl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;24K     http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        not a dynamic executable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ upx -9 --ultra-brute http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -sh http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;20K     http-okay&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on retombe sur les 20ko.&lt;/p&gt;
&lt;h3 id=&quot;docker-4&quot;&gt;Docker&lt;/h3&gt;
&lt;p&gt;Je fais le build avec zig mais ça aurait également pu se faire avec musl-gcc&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; chainguard/zig  &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; zig_build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;WORKDIR&lt;/span&gt;&lt;span&gt; /opt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; void.c main.c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; zig build-exe main.c --library c --name http-okay -target x86_64-linux-musl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; gcc &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;as&lt;/span&gt;&lt;span&gt; upx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;WORKDIR&lt;/span&gt;&lt;span&gt; /opt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=zig_build /opt/http-okay http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; wget https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; tar -xf upx-4.2.4-amd64_linux.tar.xz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; cp upx-4.2.4-amd64_linux/upx /usr/bin/upx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; strip http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=upx /opt/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-c .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -p 8080:8080 http-okay-c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images | grep http-okay-c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-c                    latest           708eb4985347   About a minute ago   22.1kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On a frôlé les 20ko, mais je n’ai pas trouvé comment descendre.&lt;/p&gt;
&lt;p&gt;J’ai tenté de remplacer le &lt;code&gt;main&lt;/code&gt; pour une méthode &lt;code&gt;_start&lt;/code&gt;, mais je n’ai pas réussi à compiler après. Je me retrouvais avec des duplications de symboles, impossibles à
régler.&lt;/p&gt;
&lt;h2 id=&quot;asm&quot;&gt;ASM&lt;/h2&gt;
&lt;p&gt;Cette section c’est juste la récréation, je ne vais pas essayer d’expliquer tout ^^’&lt;/p&gt;
&lt;p&gt;L’assembleur est au plus prêt de la machine, c’est ici que l’on peut faire les plus grosse optimisations.&lt;/p&gt;
&lt;p&gt;On va donc complètement se débarasser de la libc ou de musl et réécrire à la main les instructions que la libc génère pour le CPU.&lt;/p&gt;
&lt;h3 id=&quot;implementation-et-construction-du-binaire&quot;&gt;Implémentation et construction du binaire&lt;/h3&gt;
&lt;p&gt;Le code vient de ce &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://github.com/margaretbloom/nash-f/tree/main&quot;&gt;repo&lt;/a&gt;. Il est entièrement commenté. 😎&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;http.asm&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GLOBAL _start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; AF_INET 	2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; SOCK_STREAM 	1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; IPPROTO_TCP 	6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; SYS_SOCKETCALL	102&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; SYS_WRITE	4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; SYS_CLOSE	6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; CALL_SOCKET	1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; CALL_BIND	2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; CALL_LISTEN	4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; CALL_ACCEPT	5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span&gt; QUEUE_SIZE	0x7f	;Keep it 7-bit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;macro&lt;/span&gt;&lt;span&gt; MAKE_PORT 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	db&lt;/span&gt;&lt;span&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;8&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt; &amp;amp; &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0xff&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%endm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;macro&lt;/span&gt;&lt;span&gt; MAKE_INTERFACE 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;	db&lt;/span&gt;&lt;span&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;3&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%endm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;macro&lt;/span&gt;&lt;span&gt; smov 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;defstr&lt;/span&gt;&lt;span&gt; reg %1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;substr&lt;/span&gt;&lt;span&gt; reg_id reg 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	%strcat byte_reg_str reg_id, &amp;quot;l&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;deftok&lt;/span&gt;&lt;span&gt; byte_reg byte_reg_str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;	xor&lt;/span&gt;&lt;span&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;	mov&lt;/span&gt;&lt;span&gt; byte_reg, %&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%endm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SECTION &lt;/span&gt;&lt;span&gt;.text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;_start&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;--- Create the socket  ---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span&gt; IPPROTO_TCP			&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;Making DWORDs from 8-bit constants using the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span&gt; SOCK_STREAM			&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;stack is 2*n_const vs 4*n_const of using dd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  smov &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;ebx&lt;/span&gt;&lt;span&gt;, CALL_SOCKET			&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;Shorter form of mov r32, imm8 using xor + mov&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  call&lt;/span&gt;&lt;span&gt; socketcall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;--- Bind the socket  ---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0x10&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;				;Using the stack here is again a win&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span&gt; inet_addr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; eax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; bl&lt;/span&gt;&lt;span&gt;, CALL_BIND&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  call&lt;/span&gt;&lt;span&gt; socketcall			&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;NOTE: this will put the first parameter in edi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  					;In this case it will put eax (socket descriptor)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  					;in edi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;--- Make the socket listen ---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;_listen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span&gt; QUEUE_SIZE			&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;Again using the stack we get away with&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; edi&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;				;just 4 bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; bl&lt;/span&gt;&lt;span&gt;, CALL_LISTEN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  call&lt;/span&gt;&lt;span&gt; socketcall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;_server_loop&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;--- Accept ---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; edi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; bl&lt;/span&gt;&lt;span&gt;, CALL_ACCEPT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  call&lt;/span&gt;&lt;span&gt; socketcall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  push&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; eax&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;				;Save for later&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;--- Write the response ---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; ebx&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;eax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  smov &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;eax&lt;/span&gt;&lt;span&gt;, SYS_WRITE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; ecx&lt;/span&gt;&lt;span&gt;, html&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  smov &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;edx&lt;/span&gt;&lt;span&gt;, html_len&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0x80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;--- Close the new socket ---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  pop&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; ebx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; al&lt;/span&gt;&lt;span&gt;, SYS_CLOSE		&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;eax[31:8] was 0 from the previous syscall (if no errors)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0x80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; jmp&lt;/span&gt;&lt;span&gt; _server_loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;====================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;socketcall&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;ebx must be set by the caller&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  smov &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;eax&lt;/span&gt;&lt;span&gt;, SYS_SOCKETCALL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  lea&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; ecx&lt;/span&gt;&lt;span&gt;, [&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;esp&lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 80h&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  mov&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; edi&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;DWORD&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;esp&lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;4&lt;/span&gt;&lt;span&gt;]	&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;Return in edi the first parameter of syscall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  				;This is usually the socket descriptor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  ret&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;				;We clean up only ONE parameter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  				;This is ok since every use has at least one&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  				;parameter and in the server_loop spares us from&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  				;rebalancing the stack&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;;====================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;We can put read only data in the code section. It&amp;#39;s bad for performance but&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;  ;we only care about space&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;  html&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; db&lt;/span&gt;&lt;span&gt; &amp;quot;HTTP/&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 200&lt;/span&gt;&lt;span&gt; OK&amp;quot;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;13&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;10&lt;/span&gt;&lt;span&gt;, &amp;quot;Content-&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;Length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span&gt;&amp;quot;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;13&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;10&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;13&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;10&lt;/span&gt;&lt;span&gt;, &amp;quot;Ok&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  html_len &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;EQU&lt;/span&gt;&lt;span&gt; $-html&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;  inet_addr&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;  	dw&lt;/span&gt;&lt;span&gt; AF_INET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  	MAKE_PORT &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  	MAKE_INTERFACE &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	;The MAKE_INTERFACE is commented because on x86 a page is always allocated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	;and zeroed by the kernel. So we know there are zeros after our code.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	;Thus we get a free MAKE_INTERFACE 0, 0, 0, 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	;One can also fuse AF_INET and the port, if this is lower than 256, using:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;	;db AF_INET, 0, &amp;lt;port-number&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Contrairement a du C, l’assembleur ne se compile pas. Il se traduit.&lt;/p&gt;
&lt;p&gt;On utilise pour cela un utilitaire nommé &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.nasm.us/&quot;&gt;nasm&lt;/a&gt; qui va traduire pour nous cet assembleur en un fichier appelé objet.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ nasm -f elf32 http.asm -o http-okay.o&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le &lt;code&gt;elf32&lt;/code&gt; signifie “architecture 32 bits”.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ file http-okay.o &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce fichier n’est pas encore executable, il a besoin d’être “lié” via l’utilitaire &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://linux.die.net/man/1/ld&quot;&gt;ld&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Il est nécessaire de lui fournir l’architecture ici du 32 bits symbolisé par &lt;code&gt;elf_i386&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ld -m elf_i386 http-okay.o -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -b http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;4756 http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        not a dynamic executable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
	&lt;p&gt;&lt;code&gt;du&lt;/code&gt; n’est plus assez fin pour déterminer la taille du fichier, c’est pour ça qu’on le passe en “compte d’octets” car sinon il renverra 4 Ko pour
tout nos essais.&lt;/p&gt;
&lt;p&gt;C’est dû à la taille des bloc dans le disque&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Et bien ça fait 4ko et on a même pas optimisé. 🤣&lt;/p&gt;
&lt;h3 id=&quot;optimisations-5&quot;&gt;Optimisations&lt;/h3&gt;
&lt;p&gt;On a deux optimisations.&lt;/p&gt;
&lt;p&gt;Une est assez violente, on va faire sauter une sécu qui va rendre inscrivible une partie de mémoire qui devrait être “read-only”.&lt;/p&gt;
&lt;p&gt;Et fait sauter tout un tas de garde-fous qui rende définitivement notre monstre de frankenstein très dangereux pour sa petite taille. 😅&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;--omagic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Set the text and data sections to be readable and writable. Also, do not page-align the data segment, and disable linking against shared libraries. If the output format supports Unix style magic numbers, mark the output as &amp;quot;OMAGIC&amp;quot;. Note: Although a writable text section is allowed for PE-COFF targets, it does not conform to the format specification published by Microsoft.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Mais nous ne sommes pas là pour ça. Donc on brûle, on pille, on détruit “ARHH”.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ld -m elf_i386 --omagic http-okay.o -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ld: warning: http-okay has a LOAD segment with RWX permissions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -b http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;756 http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ldd http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        not a dynamic executable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;ld&lt;/code&gt; fait clairement la gueule, mais laisse passer et notre binaire toujours fonctionnel descend sous le 1ko.&lt;/p&gt;
&lt;p&gt;Si on fait un coup de &lt;code&gt;nm&lt;/code&gt;, on s’aperçoit qu’il y a du monde. Très peu certes. Mais comme notre unité est maintenant l’octet, ça commence à se sentir.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ nm ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;080480ee T __bss_start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;080480ee T _edata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;080480f0 T _end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0804807e t _listen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;08048088 t _server_loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;08048060 T _start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;080480be t html&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000028 a html_len&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;080480e6 t inet_addr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;080480ad t socketcall&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et oui, ce sont les étiquettes de notre assembleur, écrit en toutes lettres dans le binaire.&lt;/p&gt;
&lt;p&gt;On continue notre épuration.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ strip http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ nm http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nm: http-okay: no symbols&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ du -b http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;376 http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ upx -9 --ultra-brute http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;upx: http-okay: IOException: file is too small&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;UPX rend les armes.&lt;/p&gt;
&lt;p&gt;Et hop, binaire coupe 2 ! 🔪 376 octets 🤩🤩🤩&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ ./http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et il continue à tourner. 😎&lt;/p&gt;
&lt;h3 id=&quot;docker-5&quot;&gt;Docker&lt;/h3&gt;
&lt;p&gt;Notre beau docker.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;docker&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; emilienmottet/nasm &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AS&lt;/span&gt;&lt;span&gt; builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;WORKDIR&lt;/span&gt;&lt;span&gt; /opt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; http.asm .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; nasm -felf32 http.asm -o http-okay.o&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;RUN&lt;/span&gt;&lt;span&gt; ld --strip-all --omagic -melf_i386 http-okay.o -o http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;FROM&lt;/span&gt;&lt;span&gt; scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;COPY&lt;/span&gt;&lt;span&gt; --from=builder /opt/http-okay /usr/bin/http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;/usr/bin/http-okay&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Que l’on build et exécute.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker build -t http-okay-asm .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images | grep http-okay-asm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-asm                    latest           20664594c5a5   18 seconds ago       376B &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker run -it -p 8080:8080 http-okay-asm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ curl localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On arrive donc à une image de 376 octets qui fait aussi bien que des images 1000 fois plus lourde.&lt;/p&gt;
&lt;blockquote class=&quot;markdown-alert-caution&quot;&gt;
	&lt;p&gt;&lt;strong&gt;N’UTILISEZ PAS CA EN PROD !!!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;On résume&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;❯ docker images | grep http-okay&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-asm                    latest           20664594c5a5   19 minutes ago   376B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-zig                    latest           708eb4985347   7 days ago       12.1kB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-c                      latest           c8e644f8d1ec   5 hours ago      22.1kB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-vlang                  latest           9ce570aed25e   7 days ago       213kB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-rust                   latest           a3a324028a28   6 weeks ago      440kB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http-okay-go                     latest           8d401a6283f4   7 days ago       683kB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;L’assembleur est donc premier avec une avance absurde.&lt;/p&gt;
&lt;p&gt;Le zig est le seul qui descend sous les 20 ko.&lt;/p&gt;
&lt;p&gt;Puis vient le C à tout juste 20 ko.&lt;/p&gt;
&lt;p&gt;Puis le V qui est extraordinairement efficace.&lt;/p&gt;
&lt;p&gt;Viens ensuite le Rust qui est handicapé par son ownership et toutes les sécurités qu’il porte.&lt;/p&gt;
&lt;p&gt;Puis le Go qui a son runtime à intégrer dans le binaire.&lt;/p&gt;
&lt;p&gt;Je suis certain que Rust et Vlang et peut-être go peuvent descendre en utilisant musl au lieu de la glibc, mais je n’ai pas réussi.&lt;/p&gt;
&lt;p&gt;Comme vous l’avez remarquez, le but de l’article n’était qu’un immense prétexte pour parler de pleins de choses. 😄&lt;/p&gt;
&lt;p&gt;J’espère que vous avez pu apprendre deux ou trois choses.&lt;/p&gt;
&lt;p&gt;Merci de votre lecture. ❤️&lt;/p&gt;
</content>
        
    </entry>
</feed>
