<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mustafa Abdalwahab on Medium]]></title>
        <description><![CDATA[Stories by Mustafa Abdalwahab on Medium]]></description>
        <link>https://medium.com/@msf37?source=rss-2cc301916c22------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*r0ULsSBXPOwJcYiy.jpg</url>
            <title>Stories by Mustafa Abdalwahab on Medium</title>
            <link>https://medium.com/@msf37?source=rss-2cc301916c22------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:18:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@msf37/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Boolean Logic Can Be Used to Build Firewall ACLs]]></title>
            <link>https://msf37.medium.com/how-boolean-logic-can-be-used-to-build-firewall-acls-c98e80cc8d00?source=rss-2cc301916c22------2</link>
            <guid isPermaLink="false">https://medium.com/p/c98e80cc8d00</guid>
            <category><![CDATA[boolean-logic]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[firewall]]></category>
            <category><![CDATA[networking]]></category>
            <dc:creator><![CDATA[Mustafa Abdalwahab]]></dc:creator>
            <pubDate>Tue, 02 Jun 2026 12:43:13 GMT</pubDate>
            <atom:updated>2026-06-02T12:43:13.248Z</atom:updated>
            <content:encoded><![CDATA[<p>An <strong>Access Control List (ACL)</strong> is a set of rules used to filter network traffic and perform actions based on packet information — more specifically, packet header information. These actions may include blocking traffic, redirecting it, or performing any other operation. ACLs are mainly used to control access to network resources and manage traffic flow.</p><p>They are commonly used in many network devices such as routers, NATs, switches, firewalls, proxies, and many others.</p><h3>Example of a Simple ACL Rule</h3><pre>Action Protocol Source-ip Source-port Destination-ip Destination-port</pre><pre>permit tcp 192.168.1.0 0.0.0.255 8080 any 80</pre><p>This rule can be divided into:</p><ul><li><strong>Action</strong>: Specifies whether matching traffic is allowed or denied.</li><li><strong>Protocol</strong>: Specifies the traffic protocol (in this case TCP).</li><li><strong>Source IP</strong>: The source IP range (192.168.1.0 to 192.168.1.255).</li><li><strong>Source Port</strong>: The source port is 8080.</li><li><strong>Destination IP</strong>: Any destination IP address.</li><li><strong>Destination Port</strong>: The destination port is 80.</li></ul><h3>First Match Wins</h3><p>This means the <strong>order of rules</strong> in an ACL is critical because ACLs are processed sequentially from top to bottom.</p><p>But why is that? :)</p><p>It is common practice in firewalls to deny every packet by default unless it is explicitly allowed. To achieve this, we first add rules that allow specific traffic and then add another rule that blocks everything else.</p><p>Keep this point in mind — we will get back to it soon.</p><h3>Building a Simple ACL</h3><p>If I wanted to build a simple ACL, my naive approach would be to use some sort of dictionary for lookups. The dictionary key would contain all packet properties, while the value would be the action.</p><pre>packet-hash = protocol + source-ip + source-port + destination-ip + destination-port<br>ACL[packet-hash] = action</pre><p>Okay, this is straightforward and gives us very fast lookups, but I have a few issues with it:</p><ul><li><strong>How do we handle IP ranges?</strong> :( To encode IP ranges, I would have to loop through every IP address in the range. This applies to both source and destination addresses. Memory usage would grow massively and would not scale well.</li><li><strong>Order please!</strong> Remember the point about rule ordering? If we use a dictionary, we cannot guarantee rule priority.</li><li><strong>Can’t grow.</strong> This design is limited and difficult to extend with additional features.</li></ul><p>To solve these issues, I had to research a different method: representing ACL rules as Boolean functions. This would allow me to add logic and ordering to the matching process while keeping lookups efficient.</p><p>That search eventually led me to BDDs.</p><h3>Reduced Ordered Binary Decision Diagram (ROBDD)</h3><p>The Reduced Ordered Binary Decision Diagram (ROBDD), commonly referred to as a BDD, was introduced by Bryant in 1986.</p><p>A BDD is a data structure that uses a directed acyclic graph (DAG) for representing and manipulating Boolean functions. It has been used in model checking, digital circuit synthesis, Boolean optimization, and many other cool applications.</p><p>But here we will use it for ACLs :)</p><p>Every BDD consists of:</p><ul><li><strong>Root Node</strong>: Serves as the entry point for evaluation.</li><li><strong>Decision Nodes</strong>: Each node represents a Boolean variable and has two outgoing edges: a low edge (0) and a high edge (1).</li><li><strong>Terminal Nodes</strong>: Represent the final Boolean outcomes.</li></ul><h3>Example of a BDD</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*VRdt95bH2XUJtXxjHQS1Hg.png" /><figcaption>example of OBDD Representation from <a href="https://www.cs.cmu.edu/~bryant/pubdir/hmc-bdd18.pdf">https://www.cs.cmu.edu/~bryant/pubdir/hmc-bdd18.pdf</a></figcaption></figure><h3>Okay, How Can I Convert an ACL Rule to a BDD?</h3><p>To do this, I first need to convert the rule information into binary form so it can be represented as a Boolean function.</p><p>A simple way is to combine all components using the AND operator:</p><pre>Rule = Protocol ∧ Src-IP ∧ Src-Port ∧ Des-IP ∧ Des-Port ∧ Action</pre><p>I will assume we are only building ACLs for IPv4 — I don’t want to complicate my life :)</p><p>IPv4 is a 32-bit value consisting of four octets separated by dots, for example:</p><pre>192.0.2.50</pre><p>We can represent an IP address as:</p><pre>IP = ip[0] ∧ ip[1] ∧ ip[2] ∧ ip[3]</pre><p>Each octet is converted into an 8-bit vector, which can then be transformed into a Boolean expression.</p><p>Similarly, we can represent ports. Since ports range from 0 to 65535, we need a <strong>16-bit vector</strong>.</p><p>For protocols, in my case I only support TCP and UDP. A single bit is enough:</p><pre>0 = TCP<br>1 = UDP</pre><p>Of course, you could extend this to support more protocols.</p><p>The action can also be represented using a single bit:</p><pre>1 = Accept<br>0 = Deny</pre><p>Again, this could be extended later if you wanted more actions.</p><p>Now that we have converted all packet information into binary form, the next step is to represent it in a BDD.</p><p>If a bit is 1, we use the variable node (N).</p><p>If a bit is 0, we use the negated variable node (~N).</p><h3>Combining Multiple Rules While Preserving Order</h3><p>Okay, I have encoded a single rule into binary form.</p><p>Now how do I combine multiple rules into an ACL while preserving rule order?</p><p>This is where Boolean operators come in.</p><p>Suppose we have three rules: R1, R2, and R3.</p><p>The ACL can be represented as:</p><pre>ACL = R1 ∨ (~R1 ∧ R2) ∨ (~R1 ∧ ~R2 ∧ R3)</pre><p>The expression means:</p><ul><li>Execute R1.</li><li>If R1 does not match, execute R2.</li><li>If neither R1 nor R2 matches, execute R3.</li></ul><p>This preserves the <strong>first match wins</strong> behavior because each rule is only evaluated if all previous rules failed to match.</p><h3>Performing Lookups</h3><p>Now that the ACL is encoded as a BDD, how do we perform lookups?</p><p>To do this, I take advantage of the absorption law:</p><pre>(A ∨ B) ∧ A = A</pre><p>For an incoming packet, I build a BDD representation of the packet and perform an AND operation with the ACL BDD.</p><p>If the result equals the packet BDD, we have a match:</p><pre>if (bdd_packet AND acl) == bdd_packet then<br>    we have match</pre><p>So the lookup process becomes very simple.</p><h3>Storing ACLs</h3><p>Now I have a fully functioning ACL that can encode rules and match packets.</p><p>However, from an operational perspective, I wouldn’t want to rebuild and re-encode the ACL every time the firewall starts.</p><p>A better approach is to build the ACL once and store it as a binary file that can be loaded at startup.</p><p>This raises another question:</p><p><strong>What if I want to update a rule?</strong></p><h3>Updating Rules Using XOR</h3><p>Here we can take advantage of another useful property from Boolean algebra:</p><pre>A ⊕ B = X<br>X ⊕ A = (A ⊕ B) ⊕ A = B</pre><p>To update a rule:</p><ol><li>Convert the old rule into a BDD.</li><li>Convert the new rule into a BDD.</li><li>XOR them together.</li><li>XOR the result with the ACL.</li></ol><pre>tmp_rule = old_rule ⊕ new_rule<br>acl = acl ⊕ tmp_rule</pre><p>This effectively swaps the old rule with the new one.</p><h3>Deleting Rules</h3><p>What if we want to delete a rule?</p><p>By now, I guess we all know there is always a Boolean trick :)</p><p>The ACL is a superset of rules and we want to remove one specific rule.</p><p>Mathematically:</p><pre>acl = acl - rule</pre><p>Unfortunately, BDDs do not directly support the difference operation.</p><p>However, we can transform it into an intersection with the complement:</p><pre>A − B = A ∧ ~B</pre><p>So deleting a rule becomes:</p><pre>ACL = ACL ∧ ~rule</pre><p>The complement of the rule can be calculated using DeMorgan’s law on the Boolean expression that represents the rule.</p><h3>Closing Thoughts</h3><p>There are more tricks up the sleeve, such as detecting intersecting rules or identifying when one rule shadows another. But I’m tired of writing for today :)</p><p>If for some reason you want to see some weekend code that makes all of this work, I have a GitHub repository where I built a simple firewall that doesn’t actually block anything — it just prints to the logs whenever a packet matches <a href="https://github.com/msf37/bdd-acl">https://github.com/msf37/bdd-acl</a> .</p><p>BDD is an old technique, but it is still used in production systems today. It works, and it is definitely worth reading Bryant’s original paper if you are interested in the topic <a href="https://www.cs.cmu.edu/~bryant/pubdir/hmc-bdd18.pdf">https://www.cs.cmu.edu/~bryant/pubdir/hmc-bdd18.pdf</a> .</p><p>It is also worth mentioning that i found a group of researchers approached the problem from a completely different angle. Instead of representing packet headers as Boolean functions, they introduced the concept of <strong>Header Space Analysis</strong>.</p><p>The idea is surprisingly simple and mind-blowing at the same time: what if packet headers were treated as points in a geometric space, and we applied geometric operations to reason about networks?</p><p>Their paper is available here:</p><p><a href="https://www.usenix.org/system/files/conference/nsdi12/nsdi12-final8.pdf">https://www.usenix.org/system/files/conference/nsdi12/nsdi12-final8.pdf</a></p><p>It would be fun to explore that approach another time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c98e80cc8d00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Boosting Local Communication Performance with Local Sockets]]></title>
            <link>https://msf37.medium.com/boosting-local-communication-performance-with-local-sockets-f6e5e498f163?source=rss-2cc301916c22------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6e5e498f163</guid>
            <category><![CDATA[interprocesscommunication]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[system-performance]]></category>
            <dc:creator><![CDATA[Mustafa Abdalwahab]]></dc:creator>
            <pubDate>Tue, 01 Apr 2025 10:01:26 GMT</pubDate>
            <atom:updated>2025-04-01T10:09:28.639Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is a Socket? <br></strong>A socket enables communication between processes. In operating system terminology, this is known as inter-process communication (IPC).</p><p>More specifically, this post focuses on Unix Domain Sockets. A Unix socket creates an endpoint for communication and returns a descriptor (as per the Unix man page), which can be used to send and receive data.</p><p><strong>Types of Sockets</strong></p><p>In Unix/Linux systems, sockets are created using the<em> socket()</em> function (<a href="https://man7.org/linux/man-pages/man2/socket.2.html">man page</a>) . The first parameter of this function is an integer value called <em>domain ( </em>😅 not types ), which defines the communication domain — essentially, where the communication will take place.</p><p>Unix/Linux provides several domains for sockets: <br>- macOS has 9 domains, one of them is deprecated. <br>- FreeBSD has 12 domains. <br>- Linux? Well, it has more, why not…</p><p>The most commonly used domains are: <br>- <em>PF_INET</em> (for IPv4 communication over the network) <br>- <em>PF_INET6</em> (for IPv6 communication over the network)</p><p>However, in this post, we’re particularly interested in <em>PF_LOCAL</em>, also known as Local Sockets.</p><p><strong>Local Sockets:</strong></p><p>Sockets in the <em>PF_LOCAL</em> domain facilitate communication between processes on the same machine (local IPC). Instead of relying on IP addresses or network protocols, they use the file system as a communication medium. Each Unix socket is represented as a special file.</p><p><strong>Why to use Local Sockets? </strong><br>In my case since Local sockets don’t pass through the network stack, they offer: <br>- Low-latency communication<br>- Reduced overhead (no need for IP routing, TCP handshakes, etc.)</p><p>Just like network sockets, <em>socket()</em> returns a file descriptor, which can be used to send and receive data through standard read/write operations.</p><p><strong>Where Are Local Sockets Used?</strong></p><p>A well-known example is the <em>Docker API</em>. Instead of exposing the API over an IP address, Docker uses a Unix socket for security and performance reasons. The Docker daemon typically listens on:</p><pre>/var/run/docker.sock</pre><p>You can find more details in the <a href="https://docs.docker.com/extensions/extensions-sdk/guides/use-docker-socket-from-backend/">Docker documentation</a>.</p><p><strong>My Use Case</strong></p><p>I explored Unix sockets as a communication method between two services inside the same Kubernetes pod (sidecar pattern).</p><p>- Service A provides an API. <br>- Service B sends data to Service A for validation. <br>- Data is sent using a normal HTTP GET request, but instead of using TCP, it uses a Unix socket.</p><p><strong>Benchmarking (Performance Comparison)</strong></p><p>I built a simple server that operates in two modes: <br>1. HTTP over TCP<br>2. HTTP over a Local Sockets.</p><p>To benchmark performance, I used a simple Bash script that runs <em>curl</em> requests and measures response times. The code is available <a href="https://github.com/msf37/local-socket">here</a>.<br><em>Results</em></p><pre>TCP Benchmark Results:<br> - - - - - - - - - - -<br>Latency Statistics (seconds):<br>Minimum: 0.002243s<br>Maximum: 0.002243s<br>Average: 0.002243s<br>Unix Domain Socket Benchmark Results:<br> - - - - - - - - - - - - - - - - - -<br>Latency Statistics (seconds):<br>Minimum: 0.000176s<br>Maximum: 0.000176s<br>Average: 0.000176s<br>Summary Statistics:<br> - - - - - - - - - <br>Number of requests: 1000<br>Concurrency level: 1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QkDAUfA36i6C0qxctxTjVg.png" /></figure><p><strong>Key Takeaways <br>- </strong>Local Sockets are ~92.2% faster than TCP for local communication. <br>- Both show stable performance across 1,000 requests with 1 thread. <br>- These numbers aren’t perfect, but they give a good starting point for further testing.</p><p><strong>Final Thoughts</strong></p><p>- Local sockets offer low-latency communication without significant changes to how applications work. <br>- Best suited for one-to-one communication. <br>- Not ideal for multi-party communication because each connection requires a separate socket file (unless managed via a central node).</p><p>👋 Salam</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6e5e498f163" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>