<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title>Samer Afach</title>
    <subtitle>Personal blog</subtitle>
    <link rel="self" type="application/atom+xml" href="https://blog.afach.de/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://blog.afach.de"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2023-05-20T17:03:41Z</updated>
    <id>https://blog.afach.de/atom.xml</id>
    <entry xml:lang="en">
        <title>The software security argument why Ledger Recover is a security risk</title>
        <published>2023-05-20T17:03:41Z</published>
        <updated>2023-05-20T17:03:41Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/ledger-recover/"/>
        <id>https://blog.afach.de/ledger-recover/</id>
        <content type="html" xml:base="https://blog.afach.de/ledger-recover/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Ledger has rolled out a &quot;feature&quot; where users can store their Ledger Nano seed-phrase remotely against a payment of $10/month. The feature allows for extracting an encrypted seed-phrase of cryptocurrency &lt;strong&gt;outside the hardware wallet&lt;/strong&gt;! This has led to too much controversy. Some people think it&#39;s the cool thing to give Ledger a break after endangering people&#39;s money to make money off its users. Besides that ledger admitted that a government Subpoena can lead to handing over cryptocurrency to government with this feature, there&#39;s a technical argument why this is inexcusable.&lt;/p&gt;
&lt;p&gt;It&#39;s attractive to just say &quot;I&#39;m outside the hive-mind, and hence ledger did nothing wrong, or that the security model is unchanged&quot;, but this misses the whole point of why Ledger Recover is a problem, from a software security point of view.&lt;/p&gt;
&lt;p&gt;As a software engineer who deals with systems programming languages, there are lots of misconceptions about this whole debacle. &lt;a rel=&quot;external&quot; href=&quot;https://www.reddit.com/r/ledgerwallet/comments/13layt7/my_personal_view_on_the_pr_disaster_from_a_ledger/&quot;&gt;Even the former Ledger&#39;s chairman seems to skip this point and claim it&#39;s just a trust issue&lt;/a&gt;. I&#39;d like to explain the nuance as an expert who dealt with these issues and worked in this field for 15 years.&lt;/p&gt;
&lt;h2 id=&quot;it-s-not-about-trusting-or-not-trusting-ledger&quot;&gt;It&#39;s not about trusting or not trusting ledger&lt;/h2&gt;
&lt;p&gt;Even if we trust ledger 100%, the problem I&#39;m about to describe isn&#39;t related to that. The problem is: We&#39;re all humans, and human software engineers all do the same mistakes… we have over 50 years of experience in that.&lt;/p&gt;
&lt;h2 id=&quot;all-software-has-bugs-and-that-s-the-problem&quot;&gt;All software has bugs, and that&#39;s the problem&lt;/h2&gt;
&lt;p&gt;Over the course of humanity writing software, we learned how bugs happen, how bugs appear in the code, and how little control we have over them. The ledger software is most likely written in C, which is the language that&#39;s most vulnerable to these kinds of issues. If you&#39;d like to see how dumb, little bugs can lead to compromising the whole machine, I recommend &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=eTcVLqKpZJc&quot;&gt;PwnFunction&#39;s video &quot;Dangerous Code Hidden in Plain Sight for 12 years&quot;&lt;/a&gt;. It&#39;s an 18 minutes video with a great example in Linux and how a dumb mistake lead to exploits in Linux, including unprivileged root access. Doesn&#39;t matter how arrogant software engineers are, they do dumb mistakes, because they&#39;re human. We all do.&lt;/p&gt;
&lt;p&gt;This problem is so bad, that there&#39;s a (relatively) new programming language that advertises that it solves lots of the memory issues that programming languages like C and C++ easily allow. It&#39;s called &lt;a rel=&quot;external&quot; href=&quot;https://www.rust-lang.org/&quot;&gt;Rust&lt;/a&gt;, you probably heard about it. It&#39;s not a silver bullet, but it tries to solve most of these issues… and we&#39;re not done yet with this problem. That&#39;s a topic for another day.&lt;/p&gt;
&lt;h2 id=&quot;but-how-can-an-attacker-exploit-ledger-recover&quot;&gt;But how can an attacker exploit Ledger Recover?&lt;/h2&gt;
&lt;p&gt;We don&#39;t know, and that&#39;s the scary part. If we knew, we would&#39;ve fixed it, right? But I&#39;ll give you an introduction into how these bugs happen and how they&#39;re exploited.&lt;/p&gt;
&lt;p&gt;Here&#39;s an example in the next section… in a nutshell, without going into too much details.&lt;/p&gt;
&lt;h2 id=&quot;programs-are-dumb-counters-and-bugs-allow-hackers-to-exploit-them&quot;&gt;Programs are dumb counters, and bugs allow hackers to exploit them&lt;/h2&gt;
&lt;p&gt;When you start a program (whether in Ledger or on your PC or otherwise), the operating system maps your program to memory, and runs what&#39;s called a &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Program_counter&quot;&gt;Program Counter&lt;/a&gt;. The program is just a dumb counter that is incremented. If your system is 32-bits (CPU instructions are 4-bytes wide), and your program is mapped to start at byte number 10000 in the memory array, then the counter simply is incremented on every instruction: 10000, then 10004, then 10008, then 10012, etc. And on every increment, the CPU reads the instruction in memory and executes the instruction in there.&lt;/p&gt;
&lt;p&gt;(for the &quot;actually&quot; people, I&#39;m leaving out tons of details such as caching… but let&#39;s keep this simple).&lt;/p&gt;
&lt;p&gt;In all CPU architectures, there&#39;s an instruction called the &lt;a rel=&quot;external&quot; href=&quot;https://chortle.ccsu.edu/AssemblyTutorial/Chapter-17/ass17_2.html&quot;&gt;&quot;jump&quot; instruction&lt;/a&gt;. The jump instruction gives the CPU a new address in memory to &quot;jump&quot; to, where the program counter changes its value, so that different code is executed, like in conditional statements… &quot;if this is larger than that, do this, or do that&quot;.&lt;/p&gt;
&lt;h2 id=&quot;functions-and-jumps-bugs-disaster&quot;&gt;Functions and jumps + bugs = disaster&lt;/h2&gt;
&lt;p&gt;A function, in programming, is defined as a group of instructions/functionality that&#39;s grouped in one place in memory (again, simplification). This is one of the jobs of a jump instruction… to do &quot;function calls&quot;, and run a group of instructions when necessary.&lt;/p&gt;
&lt;p&gt;What can happen is that a software has something called a &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Buffer_overflow&quot;&gt;buffer-overflow bug&lt;/a&gt;. Buffer-overflow basically gives the program access to other sections in memory it&#39;s not supposed to access. This is, kind-of, the main cause of problems in C programming.&lt;/p&gt;
&lt;p&gt;If you&#39;re old enough in Cryptocurrency, &lt;a rel=&quot;external&quot; href=&quot;https://thehackernews.com/2018/05/eos-blockchain-smart-contract.html&quot;&gt;remember when EOS had a bug that was discovered by a security company that allowed for remote code-execution?&lt;/a&gt; That was a buffer-overflow.&lt;/p&gt;
&lt;p&gt;If a hacker discovers a buffer overflow, they can write jump instructions, for example, to force the program to call functions it&#39;s not supposed to call… say… call the function that extracts the seed-phrase and pass it through USB? Or maybe skip the encryption function and make it an identity function that does nothing? That&#39;s the problem.&lt;/p&gt;
&lt;h2 id=&quot;it-s-a-new-attack-vector&quot;&gt;&quot;It&#39;s a new attack vector&quot;&lt;/h2&gt;
&lt;p&gt;When Ledger writes a function in the firmware that extracts the seed phrase, they basically gifted all hackers a function they can try to reverse-engineer, and see if they can abuse it due to some bug a ledger engineer created, just like all humans have been writing bugs for the last 50 years. That&#39;s why people are saying &quot;it&#39;s a new attack vector&quot;.&lt;/p&gt;
&lt;p&gt;To Ledger staff: You&#39;re not gods. You&#39;re humans. Just like us. We all write bugs, and you&#39;re not an exception.&lt;/p&gt;
&lt;p&gt;That&#39;s why a firmware without this functionality at all is valuable.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>How to backup and end-to-end encrypt your remote server data without manually inputting a password</title>
        <published>2023-01-03T12:21:47Z</published>
        <updated>2023-01-03T12:21:47Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/encrypted-backup/"/>
        <id>https://blog.afach.de/encrypted-backup/</id>
        <content type="html" xml:base="https://blog.afach.de/encrypted-backup/">&lt;h1 id=&quot;introduction-the-problem&quot;&gt;Introduction - The problem&lt;/h1&gt;
&lt;p&gt;The problem is the following: You run some service where you need to have regular backups. The data to be backed up can be represented as something you can put in a tarred archive. The common way to do this is to pipe the tar output to OpenSSL and encrypt with AES-256, which is fine, except that it requires inputting the password constantly or putting a static password on the server. If you&#39;ve guessed that we&#39;re going to use &lt;strong&gt;public key cryptography&lt;/strong&gt;, you&#39;re right, &lt;em&gt;&lt;strong&gt;but there is a problem there&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;h1 id=&quot;the-typical-way-of-doing-this-with-passwords&quot;&gt;The typical way of doing this with passwords&lt;/h1&gt;
&lt;p&gt;If you research this topic online, you&#39;ll find the internet flooded with solutions that look like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ tar -czpf - /path/to/directory-to-backup | openssl aes-256-cbc -salt -pbkdf2 -iter 100000 -pass | dd of=output.aes&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are a few problems with this… let me state them and let&#39;s see whether we can do better:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You have to input the password manually for this to work, so this cannot go into a cron/periodic job unless you expose the password&lt;/li&gt;
&lt;li&gt;Since you have to put the password manually, it has to be some text, which lacks entropy, given that pbkdf2 (as far as I know) uses SHA256; where if you know anything about bitcoin mining, you&#39;ll know that SHA256 calculations have become very cheap… meaning that your password that you write with your hand, most likely, lacks entropy for security&lt;/li&gt;
&lt;li&gt;It uses AES-256-CBC, which lacks authentication… but let&#39;s not get into that since OpenSSL has support issues for the better alternative, GCM, but I&#39;ll provide a simple solution to this at the end&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s see if we can do better.&lt;/p&gt;
&lt;h1 id=&quot;how-can-public-key-cryptography-solve-these-problems&quot;&gt;How can public key cryptography solve these problems?&lt;/h1&gt;
&lt;h3 id=&quot;introduction-to-public-key-cryptography&quot;&gt;Introduction to public key cryptography&lt;/h3&gt;
&lt;p&gt;Feel free to skip this little section if you know what public key crypto is.&lt;/p&gt;
&lt;p&gt;We call symmetric key cryptography, the kind of cryptography where the encryption and decryption, or signing and verifying, uses the same key. In public key cryptography, or asymmetric key cryptography, we use the public key to encrypt data, and private key to decrypt data. Or we use the private key to sign data, and public key to verify signatures.&lt;/p&gt;
&lt;h3 id=&quot;but-public-key-encryption-doesn-t-really-help-as-a-replacement&quot;&gt;But public key encryption doesn&#39;t really help as a replacement&lt;/h3&gt;
&lt;p&gt;It may be a surprise, but there&#39;s no way to encrypt large data with public keys. The reason for this is that encryption algorithms usually are either done in blocks (like in AES), or in streams (like Chacha20). Public key encryption that we have is either considered a block cipher (like RSA encryption), or is used to generate symmetric keys using some agreement protocol, like Diffie-Hellman, which then uses symmetric encryption. And given that RSA encryption is very expensive, because it&#39;s an exponentiation of a large number, it&#39;s not really practical to use it for large data, so no standard algorithms are found in software like OpenSSL to encrypt your backups. You&#39;ll never see RSA-4096-CBC or RSA-4096-GCM for that reason.&lt;/p&gt;
&lt;h3 id=&quot;how-then-do-we-encrypt-without-a-password&quot;&gt;How then do we encrypt without a password?&lt;/h3&gt;
&lt;p&gt;The trick is simple. We generate a truly random password, encrypt the password with RSA and store it in a file, use the password for encryption, then we forget the password and move on. The password then can only be recovered with a private key (which will be encrypted). Let&#39;s see how we do this.&lt;/p&gt;
&lt;p&gt;First, in your local machine where you&#39;re OK with unpacking the data, generate your RSA keys. Generate your decryption private key (encrypted with a passphrase):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ openssl genrsa -aes256 -out priv_key.pem 4096&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and from that, generate your public key&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ openssl rsa -in priv_key.pem -outform PEM -pubout -out pub_key.pem&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now this password that you use to encrypt this key will be the only password you&#39;ll ever type. Your backups don&#39;t need it.&lt;/p&gt;
&lt;p&gt;Now copy the &lt;code&gt;pub_key.pem&lt;/code&gt; file to the remote machine where backups have to happen.&lt;/p&gt;
&lt;p&gt;If you have a backup bash script, you can do the following now:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;#!/bin/bash&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 style=&quot;color: #6A737D;&quot;&gt;# generate a random password with 256-bit entropy, so PBKDF2 won&amp;#39;t be a problem&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;password&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt;$(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;openssl&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; rand&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -hex 64&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 style=&quot;color: #6A737D;&quot;&gt;# tar and encrypt your data with the password&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;tar&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -czpf&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; - /path/to/directory-to-backup&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; openssl&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; aes-256-cbc&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -salt -pbkdf2 -iter 100000 -pass&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; pass:&lt;/span&gt;&lt;span&gt;${password}&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; dd&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; of=output.aes&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 style=&quot;color: #6A737D;&quot;&gt;# encrypt the password using your RSA key, and write the output to a file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;echo&lt;/span&gt;&lt;span&gt; ${password}&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; openssl&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; rsautl&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -encrypt -inkey&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; pub_key.pem&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -pubin -in&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -out&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; output.password.enc&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To view the password in that file, you can view it in stdout using this command:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ openssl rsautl -decrypt -inkey priv_key.pem -in output.password.enc -out -&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will print the password to your terminal, which you can copy and use to decrypt your encrypted archive.&lt;/p&gt;
&lt;p&gt;Finally, to decrypt your data, run the command:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ dd if=output.aes | openssl aes-256-cbc -d -salt -pbkdf2 -iter 100000 | tar -xpzf -&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and when asked, input the password you copied from the private key decryption of the command before&lt;/p&gt;
&lt;p&gt;WARNING: It&#39;s WRONG to use the same password and encrypt it again and again and again with RSA (with this scheme, at least). Be aware that encrypting the same password with the same public key will yield the same output. This means that someone with access to the encrypted files can recognize that you&#39;re using the same password everywhere.&lt;/p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;No need, anymore, to choose between encrypting your backups and not being there. You can have both with this simple trick. This is also more secure because the 256-bit password generated randomly can never be brute-forced through the cheap pbkdf2, which basically means that brute-forcing the encryption itself is cheaper than brute-forcing the password that derived the key, which will guarantee the 256-bit encryption strength.&lt;/p&gt;
&lt;p&gt;Now about using CBC instead of GCM, you can create your own HMAC to authenticate your archives. Or, even easier, you can call &lt;code&gt;sha256sum&lt;/code&gt; on your archive and encrypt the output with your public key or even sign it. That&#39;s good enough and can be streamlined into anything you automate for your backups.&lt;/p&gt;
&lt;p&gt;I just wanna mention also that this isn&#39;t an innovation of mine by any means… this is called in OpenSSL the &quot;envelope encryption&quot;, but for some reason it&#39;s only supported when using the C library and not when using the OpenSSL executable. This is also a common way for encryption in many applications because asymmetric encryption for data is impractical with the available tooling. I wrote this article because it seems that no one talks about this and everyone is busy typing possibly insecure passwords in their encryption archives. Hopefully this will become more common after this article.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Abandoning the server provider from 10-years: Stay away from myLoc/ServDiscount/Webtropia (review)</title>
        <published>2023-01-02T22:37:00Z</published>
        <updated>2023-01-02T22:37:00Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/myloc-review/"/>
        <id>https://blog.afach.de/myloc-review/</id>
        <content type="html" xml:base="https://blog.afach.de/myloc-review/">&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;It&#39;s a hot question what server providing service to use nowadays. The industry has boomed. The only thing that matters, though, is the reliability of a provider. With reliable I mean, the continuity of the service with no interruptions, the mutual understanding that problems can happen, and the urgency to correct problems to continue operation with minimal down-time. Ten years ago, myLoc was a good company. Nowadays, they&#39;re so cheap and bad I&#39;m leaving them. I guess inflation didn&#39;t just inflate the currency.&lt;/p&gt;
&lt;h3 id=&quot;why-write-this-in-a-blog-post&quot;&gt;Why write this in a blog post?&lt;/h3&gt;
&lt;p&gt;Because people on the internet have become so un-empathetic, robotic, and exhausting to talk to or discuss with, and my blog will minimize that because no one cares about my blog. Maybe it&#39;s a diary at this point. I can&#39;t remember in the last few years when I posted something on social media, and didn&#39;t get people licking the boot of corporations under the guise of &quot;violating their terms&quot; or whatever other dumb reason. As if common sense doesn&#39;t have any value anymore. Trust isn&#39;t of any value. I&#39;m quite sure some genius out there will find a way to twist this story and make it entirely my fault, because humanity now evaluates moral decisions based on legality... what a joke we&#39;ve become. Nowadays, you&#39;re not allowed to be angry. You can&#39;t be upset. You shouldn&#39;t be frustrated. You can&#39;t do mistakes. If you express any of your feelings towards an experience; your expression, regardless of how reasonable it&#39;s, will be called a rant and dismissed. So, I guess my blog is the best place to put this &quot;rant&quot;, if you will. Who cares after all?!&lt;/p&gt;
&lt;p&gt;I guess this will have a better chance to reach people than any robotic social media out there.&lt;/p&gt;
&lt;h3 id=&quot;story-starts-with-bad-internet-connection&quot;&gt;Story starts with bad internet connection&lt;/h3&gt;
&lt;p&gt;A few months ago I contacted ServDiscount (a subsidiary of myLoc, where my server is, IIUC) about constant internet connection problems (ticket 854255-700134-52). The internet connection problems started like a year ago, but recently got so bad (while my home&#39;s internet is around 1 Gbps). The problem was illustrated in many ways&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;File copying through sftp/ssh, was almost impossible. The speed throttles continuously and the download stops.&lt;/li&gt;
&lt;li&gt;If I use any ssh tunnel, the terminal will not be usable anymore while the tunnel is doing any transfers, even small data was enough to block it&lt;/li&gt;
&lt;li&gt;http(s) downloads of large files have become impossible; a simple attempt to download a file of 400 MB can&#39;t be accomplished.&lt;/li&gt;
&lt;li&gt;Games with server software running on my server there could never be continued due to connection loss&lt;/li&gt;
&lt;li&gt;ftps downloads constantly show &quot;connection reset by peer&quot;, which I believe is the problem with everything else. The server-side connection was just resetting all the time.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a patient and busy person, I didn&#39;t care. This went on for MONTHS. I&#39;m not exaggerating. It was when this problem went out of control. Then, under the assumption that &quot;it&#39;s my fault&quot;, I rented a specific VPS from ServDiscount, and guess what? Same problems. The VPS had NOTHING but a game server in it (plus iptables firewall with blocked input policy + open some ports), yet it was impossible to finish a game or two. People from all around the world would get disconnected after a while.&lt;/p&gt;
&lt;h3 id=&quot;bad-support-all-the-way-starting-october&quot;&gt;Bad support, all the way (starting October)&lt;/h3&gt;
&lt;p&gt;When I contacted support about this connection problem, the first blind response was &quot;it&#39;s my fault&quot;. Because for this (bad) company, it can never be their fault. All their configuration is perfect. Everything they do is perfect. They can never do mistakes. That&#39;s what pissed me off. They couldn&#39;t explain the disconnects in the gaming server OR my original server. But of course, they claimed it might be a &quot;botnet&quot;... because why not? Since it&#39;s virtually impossible to deny the presence of botnet except with plausible deniability, they can never lose this argument. Yet with the presence of the second server, it was easy to prove that something is wrong there. It was easy to reproduce the problem, since an http download would fail. That was ready to view, test and debug. So, after that pressure, some guy in their support said &quot;I&#39;ll take a look&quot;, and asked for access. I took the risk and created an account for them with sudoers power and added their public key, but they never logged in. It was easily observable in the logs, for weeks. Just empty promises with zero accountability.&lt;/p&gt;
&lt;p&gt;After nagging them, they said &quot;they&#39;re busy&quot;... great support. As if I&#39;m asking for a favor or something.&lt;/p&gt;
&lt;p&gt;Then I got exhausted, and to appease them, I decided to wipe my server. I wish it were that easy.&lt;/p&gt;
&lt;h3 id=&quot;get-your-backups-if-you-can&quot;&gt;Get your backups, if you can&lt;/h3&gt;
&lt;p&gt;Because of these internet issues, my backups were trapped on that server. I&#39;ve tried to download them in many ways, but my backup&#39;s server software were constantly resetting the connection. Keep in mind that the software was used for years in the past. It&#39;s not like I invented these solutions recently or something. In fact, my only &quot;invention&quot;, because of these problems, was to make these scripts run multiple times a day... just in an attempt to get the downloads to happen. For over a month, I couldn&#39;t download any backups. My backup logs were flooded with &quot;connection reset by peer&quot; errors. What the hell am I supposed to do now?&lt;/p&gt;
&lt;p&gt;Well, I remained patient. It was a mistake. I should&#39;ve probably escalated and gotten angry? But I don&#39;t think they would&#39;ve done anything. When it&#39;s a bad company, it&#39;s a bad company.&lt;/p&gt;
&lt;h3 id=&quot;docker-as-a-tool-to-easily-move-away-from-bad-situations&quot;&gt;Docker, as a tool to easily move away from bad situations&lt;/h3&gt;
&lt;p&gt;I didn&#39;t know too much about docker other than it&#39;s a containerization service that I barely used. In fact I used LXD more because it&#39;s modeled around preserving OSs, while docker is modeled around stateless preservation of data, where the software is renewed constantly, but the data is preserved.&lt;/p&gt;
&lt;p&gt;The idea was simple: Instead of having to install all services on bare-metal and suffer when incompetent support treats me like this in migrations, I could just tar the containers with their data, move them somewhere else, and continue operation as if nothing happened. This way, I&#39;ll save time I don&#39;t really have, since I work now as a lead engineer and I can&#39;t really actively solve the same problems like I did back then when I was in academia. Sad fact of life: The older you grow, the less time you have.&lt;/p&gt;
&lt;p&gt;So, I started learning docker. Watched tutorials, read articles, how-to, and many other things. I built images, did many things, and things were going fine. I containerized almost all my services. But one service was the cause of all evil...&lt;/p&gt;
&lt;h3 id=&quot;containerizing-email-services-big-mistake-and-lots-learned&quot;&gt;Containerizing email services... big mistake and lots learned&lt;/h3&gt;
&lt;p&gt;The problem with containerization is that often you&#39;ll need a proxy to deliver your connections. You can setup your docker container to share the host&#39;s network, but I find that ugly and defeats the isolation picture of docker. My email server was proxied through haproxy.&lt;/p&gt;
&lt;p&gt;Unfortunately, because of proxying, and because of my lack of understanding of how that worked (because I was learning), I misconfigured my email proxy and all the incoming connections to the email server were seen as &quot;internal&quot; connections because the source was the proxy software from within the server&#39;s network, which basically meant that all incoming email relay requests were accepted.&lt;/p&gt;
&lt;h3 id=&quot;our-nigerian-friends-and-their-prince-were-ready&quot;&gt;Our Nigerian friends and their prince were ready&lt;/h3&gt;
&lt;p&gt;Once that mistake was made, within a day, the server was being used as a relay for spam.&lt;/p&gt;
&lt;h3 id=&quot;servdiscount-and-their-reaction&quot;&gt;ServDiscount and their reaction&lt;/h3&gt;
&lt;p&gt;On 16.12.2022, because one email ended in a spam trap, the &quot;&lt;a rel=&quot;external&quot; href=&quot;http://www.uceprotect.wtf&quot;&gt;UCEProtect&lt;/a&gt;&quot; service flagged my server, and let&#39;s try to guess what ServDiscount/myLoc did:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Contacted me, informing me of the issue and asking to correct it&lt;/li&gt;
&lt;li&gt;Switching the server to rescue mode, so that I can investigate the issue through the logs&lt;/li&gt;
&lt;li&gt;Completely block access to my server with no way for me to access the logs to understand the problem&lt;/li&gt;
&lt;li&gt;Refuse to unblock the server (even in rescue mode) unless UCEProtect gives the stamp of approval&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Yep, it&#39;s 3 and 4.&lt;/p&gt;
&lt;p&gt;It&#39;s completely understandable to block my server. After all, it was sending spam. No question there. But the annoying part is that I only knew about this whole thing by coincidence, when I attempted access to my server and couldn&#39;t. They only sent an email, which I didn&#39;t receive because my email server was blocked. They didn&#39;t attempt a phone-call, SMS or anything. They didn&#39;t even open a ticket IN MY ACCOUNT WITH THEM!&lt;/p&gt;
&lt;p&gt;You might wonder whether it can get any more careless, passive and negligent from their end. Yes, it can!&lt;/p&gt;
&lt;p&gt;Remember that I couldn&#39;t even download my backups because of their crappy internet connection, which they didn&#39;t diagnose or help fix. So I&#39;m literally now trapped with no email access, my data is locked (for who knows how long), and... it gets worse.&lt;/p&gt;
&lt;p&gt;I contacted their customer support. What do you think the answer was?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We understand. Let us switch your server to rescue mode so that you can understand what happened&lt;/li&gt;
&lt;li&gt;Remove your server from UCEProtect&#39;s list (by paying them money) then come back&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You guessed it. It&#39;s no. 2.&lt;/p&gt;
&lt;p&gt;And again, remember, I can&#39;t even gain rescue access to my server. So until this point, I don&#39;t even have an idea what happened.&lt;/p&gt;
&lt;p&gt;I go to UCEProtect, pay them around $100 (it was a Friday). Then went back to customer support. Guess their response:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Thank you for removing your server from the list. Here&#39;s access back so that you can investigate the issue.&lt;/li&gt;
&lt;li&gt;We have zero communication with the &quot;abuse department&quot;, so wait for them to react... fill the form they sent you to the email that you can&#39;t access.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You also guessed it. No. 2.&lt;/p&gt;
&lt;p&gt;No amount of tickets, phone-calls, or begging helped. A robot would&#39;ve understood better that I couldn&#39;t access my email so I have no way of responding.&lt;/p&gt;
&lt;p&gt;Whether it&#39;s with bad policy or otherwise, they never help when you need help. They couldn&#39;t care less. So what if your server was stopped for days or even weeks based on UCEProtect listing? We don&#39;t care, and we won&#39;t help you fix the problem.&lt;/p&gt;
&lt;h3 id=&quot;dns-change&quot;&gt;DNS change&lt;/h3&gt;
&lt;p&gt;While support was completely ignoring me, it occurred to me that I could switch my email MX DNS record to another service and receive my emails. Thanks to how MTAs (mail transfer agents) work, they don&#39;t give up so easily. On 17.12.2022, I got the email on another email provider, who I paid just to solve this problem, filled the form (without knowing why the issue happened, because again, how the hell will I know without access to my logs, even in rescue mode?!).&lt;/p&gt;
&lt;h3 id=&quot;the-holy-abuse-department-is-on-the-other-side-of-the-universe&quot;&gt;The holy abuse department is on the other side of the universe&lt;/h3&gt;
&lt;p&gt;So while I did everything they asked for. I filled the form, I removed my IP address from UCECrap, I didn&#39;t get my server until days later. Because the abuse department is simply unreachable. There&#39;s basically zero urgency to solve such a problem for a customer (who&#39;s been with them for 10 years). Who gives a flying f**k if your server and work halted for days?! Not myLoc, apparently.&lt;/p&gt;
&lt;h3 id=&quot;let-s-summarize&quot;&gt;Let&#39;s summarize&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Bad internet connection with no help, so I couldn&#39;t even get my backups&lt;/li&gt;
&lt;li&gt;No urgency to solve a problem I obviously didn&#39;t do deliberately, with zero value for being with them for 10 years&lt;/li&gt;
&lt;li&gt;No access to server in rescue mode or VPN, yet I have to explain how the outbreak happened without logs, who knows how&lt;/li&gt;
&lt;li&gt;Only contact through email, which was blocked, which they never addressed&lt;/li&gt;
&lt;li&gt;Even after fulfilling their conditions, no one cares. No one helped, because the &quot;abuse department&quot; is on the other side of the universe.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What&#39;s the point of having a server with a company that treats its customers like that?&lt;/p&gt;
&lt;p&gt;What&#39;s the point when accountability is too much to ask?&lt;/p&gt;
&lt;p&gt;I don&#39;t know.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I&#39;m not gonna sit here and claim that I&#39;m perfect and none of this is my responsibility. But at least I expect common sense treatment for customers, urgency to help and reasonable way to access the server to see the logs, whether in rescue mode or VPN or whatever, and a reachable &quot;abuse department&quot; that doesn&#39;t hold all the keys with no accountability to customers. All this, and add that I&#39;ve been with them for a decade.&lt;/p&gt;
&lt;p&gt;But basically, the lesson here is, if anything goes wrong with your server in myLoc, you can go f**k yourself. No one will help you. You don&#39;t really matter to them. Unlike other companies that will simply put your server in rescue mode and ask you to fix the problem. MyLoc will simply block you. Maybe they&#39;re taking a cut out of UCEProtect? I don&#39;t know. But you can only wonder why a company will be that stupid. I&#39;m out.&lt;/p&gt;
&lt;p&gt;Have a great one.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>A simple, lock-free object-pool</title>
        <published>2019-05-25T16:05:01Z</published>
        <updated>2019-05-25T16:05:01Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/object-pool/"/>
        <id>https://blog.afach.de/object-pool/</id>
        <content type="html" xml:base="https://blog.afach.de/object-pool/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Recently, I was using &lt;a rel=&quot;external&quot; href=&quot;https://thrift.apache.org/&quot;&gt;Apache Thrift&lt;/a&gt; for a distributed high performance C++ application. Apache Thrift is an &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Remote_procedure_call&quot;&gt;RPC&lt;/a&gt; for calling functions on other ends of networks and across different languages. On a single thread, using an RPC is a bad idea, if performance is the goal. So the way to get high performance from such a system is by putting multiple clients in many threads. However, the problem is that Apache Thrift&#39;s clients are not thread-safe! What do we do?&lt;/p&gt;
&lt;p&gt;The solution is an object pool of clients, and since the application is a high-performance, I chose it to be a lock-free object pool. Lock-free means that no mutexes are involved in the design, but only atomic variables. I couldn&#39;t find anything out there, so I implemented my own, which I hope will be useful to others, too. While this design is lock-free, it&#39;s not &quot;wait-free&quot;. Meaning that, obviously, if all objects in the pool are used, the system will block/wait until an object is available, or the programmer has to setup an asynchronous waiting mechanism. It&#39;s possible to &quot;try&quot; to get an object in this design.&lt;/p&gt;
&lt;p&gt;In the following I&#39;ll discuss my design of this object pool.&lt;/p&gt;
&lt;h2 id=&quot;the-design&quot;&gt;The design&lt;/h2&gt;
&lt;p&gt;The design of this object pool is simple.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There&#39;s a vector of size N that holds N initialized objects. The initialization of the objects is done in the constructor, by passing a functor/lambda that intializes every object and puts them in the vector (hence, the objects must be at least move-constructible).&lt;/li&gt;
&lt;li&gt;I used a lock-free queue from boost. The queue has integers ranging from 0 to N-1. In order to check whether an object is available, its index in the vector has to be in this queue.&lt;/li&gt;
&lt;li&gt;If borrowing is successful, a new object &quot;&lt;code&gt;BorrowedObject&lt;/code&gt;&quot; will be returned, which contains a pointer to the borrows object. This is a safe mechanism, where returning to the pool is done automatically when this object is destroyed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Shall we look at some code?&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;template&lt;/span&gt;&lt;span&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ObjectPool&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 style=&quot;color: #6F42C1;&quot;&gt;    AvailableObjectsQueueType&lt;/span&gt;&lt;span&gt; availableObjectsQueue;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;size_t&lt;/span&gt;&lt;span&gt;               objectCount;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;vector&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;            objects;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;atomic_uint64_t&lt;/span&gt;&lt;span&gt;      availableObjectsCount;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;atomic_bool&lt;/span&gt;&lt;span&gt;          shutdownFlag;&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 style=&quot;color: #D73A49;&quot;&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    friend&lt;/span&gt;&lt;span&gt; BorrowedObject&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&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 style=&quot;color: #6F42C1;&quot;&gt;    ObjectPool&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; Size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;function&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;size_t&lt;/span&gt;&lt;span&gt;)&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; objectInitializers&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ObjectPool&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                  = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ObjectPool&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ObjectPool&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ObjectPool&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ObjectPool&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ObjectPool&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ObjectPool&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ObjectPool&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ObjectPool&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&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 style=&quot;color: #6F42C1;&quot;&gt;    BorrowedObject&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; borrowObj&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; sleepTime_ms&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&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 style=&quot;color: #6F42C1;&quot;&gt;    BorrowedObject&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; try_borrowObj&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;bool&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; success&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;       size&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; const&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    getAvailableObjectsCount&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; const&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;vector&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;   getInternalObjects_unsafe&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;              shutdown&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ~ObjectPool&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;As you can see, the class is not copyable or movable, for simplicity. Besdies that, the following methods can be seen:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The constructor: Takes the size of the pool, and a functor/lambda to initialize the objects. The parameter in this functor/lambda is of type &lt;code&gt;std::size_t&lt;/code&gt;. The purpose of this is to be able to have the initialization done as a function of the number of the object in the pool.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;borrowObj()&lt;/code&gt;: A blocking method for borrowing an object from the pool.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;try_borrowObj()&lt;/code&gt;: A non-blocking method for borrowing an object from a the pool, returns a null object on failure.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;shutdown()&lt;/code&gt;: Shutdown the object pool, which calls the destructor of all objects and prevents borrowing further.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://git.afach.de/samerafach/objectpool&quot;&gt;You can find the full implementation here.&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Here&#39;s a simple design of a lock-free object pool. No more than 200 lines. The feature I find cool in this object pool is that the objects themselves are not being passed around, but only their indices, which can be done quite efficiently on most modern CPUs.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Safe resource allocation and deallocation in C++ (C++11 and C++03)</title>
        <published>2017-05-06T13:05:58Z</published>
        <updated>2017-05-06T13:05:58Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/cpp-allocation/"/>
        <id>https://blog.afach.de/cpp-allocation/</id>
        <content type="html" xml:base="https://blog.afach.de/cpp-allocation/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Back then before 2010, one always used Valgrind to make sure that their C++ program isn&#39;t leaking memory. There&#39;s a whole modern way of using C++ that doesn&#39;t even make you need that. If you&#39;re using a C++11 program (and even if you&#39;re not), and you still require to keep checking whether your code is leaking memory, then you&#39;re most likely doing it all wrong. Keep reading if you wanna know the right way that saves you the trouble having to memory check all the time, and uses true object-oriented programming.&lt;/p&gt;
&lt;p&gt;Basically this artice is about the concept named &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization&quot;&gt;RAII&lt;/a&gt;, Resource Allocation Is Initialization, and why it&#39;s important from my perspective.&lt;/p&gt;
&lt;h2 id=&quot;the-golden-rules-of-why-you-should-do-this&quot;&gt;The golden rules of why you should do this&lt;/h2&gt;
&lt;p&gt;My Golden Rules in C++ development:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Humans do mistakes, and just an arrogant would claim that he doesn&#39;t (why else do we have something called &quot;error handling&quot;?)&lt;/li&gt;
&lt;li&gt;Things can go wrong, no matter how robust your program is, and how careful you are, and you can&#39;t plan every possible outcome&lt;/li&gt;
&lt;li&gt;A problem-prone program is no better than a program with a problem; so why bother writing a program with problems?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Once you embrace these 3 rules, you&#39;ll never write bad code, because your code will be ready for worst case scenario.&lt;/p&gt;
&lt;p&gt;In the last few years, I never found a single lost byte in my Valgrind-analysed programs; and I&#39;m talking about big projects, not in the single class level. The difference will be clear soon.&lt;/p&gt;
&lt;p&gt;I&#39;m going to start from simple cases, up to more complicated scenarios.&lt;/p&gt;
&lt;h3 id=&quot;scenario-1-if-you-re-creating-and-deleting-objects-under-pointers&quot;&gt;Scenario 1: If you&#39;re creating and deleting objects under pointers&lt;/h3&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomethingElse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int*&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; var&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::cout&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt; *&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    //... do stuff with x&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 style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #D73A49;&quot;&gt;    int*&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = new int&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    DoSomethingElse&lt;/span&gt;&lt;span&gt;(x);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    delete&lt;/span&gt;&lt;span&gt; x;&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;Let me make this as clear as possible: If you ever, ever use a &lt;code&gt;new&lt;/code&gt; followed by &lt;code&gt;delete&lt;/code&gt;... you&#39;re breaking all the 3 rules we made up there. Why? Here are the rules and how you&#39;re breaking them:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You may do the mistake of forgetting to write that delete&lt;/li&gt;
&lt;li&gt;DoSomethingElse() might throw an exception, and hence that delete may not be called.&lt;/li&gt;
&lt;li&gt;This is a problem prone design, so it&#39;s a program with a problem.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The right way to do this: Smart pointers!&lt;/p&gt;
&lt;p&gt;What are smart pointers?&lt;/p&gt;
&lt;p&gt;I&#39;m sure you&#39;ve heard of them before, but if you haven&#39;t, the idea is very simple. If you define, for example, an integer like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SomeFunction&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 style=&quot;color: #D73A49;&quot;&gt;    int&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&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 style=&quot;color: #6A737D;&quot;&gt;    // do stuff with i&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //here you&amp;#39;re going out of the scope of the function&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You never worry about deleting &lt;code&gt;i&lt;/code&gt;. The reason is that once &lt;code&gt;i&lt;/code&gt; goes out of scope, it&#39;s deleted automatically (through a destructor). Smart pointers are just the same. They wrap your pointer, such that they are deleted once they are out of scope. Let&#39;s look at our function &lt;code&gt;DoSomething()&lt;/code&gt; again with smart pointers:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; x&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;new int&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //line 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    DoSomethingElse&lt;/span&gt;&lt;span&gt;(x.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;get&lt;/span&gt;&lt;span&gt;());&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        //line 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //once gone out of scope, x will be deleted automatically, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  //the destructor of unique_ptr will delete the integer&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s all the change you have to do, and you&#39;re done! In line 1, you&#39;re creating a &lt;code&gt;unique_ptr&lt;/code&gt;, which will encapsulate your pointer. The reason why it&#39;s &quot;unique&quot; will become soon clear. Once the a &lt;code&gt;unique_ptr&lt;/code&gt; goes out of scope, it&#39;ll delete the object under it. So you don&#39;t have to worry! This way, the 3 Golden Rules are served. In line 2, we&#39;re using &lt;code&gt;x.get()&lt;/code&gt; instead of &lt;code&gt;x&lt;/code&gt;, because the &lt;code&gt;get()&lt;/code&gt; method will return the raw pointer stored inside the &lt;code&gt;unique_ptr&lt;/code&gt;. If you&#39;d like to delete the object manually, use the method &lt;code&gt;x.reset()&lt;/code&gt;. The method &lt;code&gt;reset()&lt;/code&gt; can take a parameter to another pointer, or can be empty to become &lt;code&gt;nullptr&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;PS: &lt;code&gt;unique_ptr&lt;/code&gt; is C++11. If you&#39;re using C++03, you could use &lt;code&gt;unique_ptr&lt;/code&gt; from the &lt;a rel=&quot;external&quot; href=&quot;http://www.boost.org/&quot;&gt;boost library&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Why is it called &quot;unique&quot;?&lt;/p&gt;
&lt;p&gt;Generally, multiple pointers can point to the same object. So, going back to the initial example, the following is a possible scenario:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #D73A49;&quot;&gt;    int*&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = new int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&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 style=&quot;color: #D73A49;&quot;&gt;    int*&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //now x and y, both, point to the same integer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::cout&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt; *&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt; *&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::endl;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //both will print 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    *&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = *&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //add 1 to the object under x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::cout&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt; *&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt; *&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::endl;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //both will print 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    delete&lt;/span&gt;&lt;span&gt; x;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //you delete only 1 object, not 2!&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;But can you do this with &lt;code&gt;unique_ptr&lt;/code&gt;? The answer is *no*! That&#39;s why it&#39;s called unique, because it&#39;s a pointer that holds complete *ownership* of the object under it (the integer, in our case), and it&#39;s unique in that. If you try to do this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; x&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;new int&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //compile error!&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;your program won&#39;t compile! Think about it... if this were to compile, who should delete the pointer when the function ends, x or y? It&#39;s ambiguous and dangerous. In fact, this is exactly why &lt;code&gt;auto_ptr&lt;/code&gt; was deprecated in C++11. It allowed the operation mentioned before, which effectively *moved* the object under it. This was dangerous and unclear semantically, which is why it&#39;s deprecated.&lt;/p&gt;
&lt;p&gt;On the other hand, you can move an object from one &lt;code&gt;unique_ptr&lt;/code&gt; to another! Here&#39;s how:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; x&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;new int&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;move&lt;/span&gt;&lt;span&gt;(x);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    //now x is empty, and y has the integer, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    //and y is responsible for deleting the integer&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;with &lt;code&gt;std::move(x)&lt;/code&gt;, you convert &lt;code&gt;x&lt;/code&gt; to an rvalue reference, indicating that it can be safely moved/modified.&lt;/p&gt;
&lt;h5 id=&quot;shared-pointers&quot;&gt;Shared pointers&lt;/h5&gt;
&lt;p&gt;Since we established that unique pointers are &quot;unique&quot;, let&#39;s introduce the solution to the case where multiple smart pointers can point to the same object. The answer is: &lt;code&gt;shared_ptr&lt;/code&gt;. Here&#39;s the same example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; x&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;new int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //the value of *x is 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //this is valid!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    //now both x and y point to the integer&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;Who is responsible for deleting the object now? x or y? Generally, any of them! The way shared pointers work is that they have a common reference counter. They count how many &lt;code&gt;shared_ptrs&lt;/code&gt; point to the same object, and once the counter goes to zero (i.e., the last object goes out of scope), the last object is responsible for deleting.&lt;/p&gt;
&lt;p&gt;In fact, using the &lt;code&gt;new&lt;/code&gt; operator manually is highly discouraged. The alternative is use &lt;code&gt;make_shared&lt;/code&gt;, which covers some corner cases of possible memory leaks. For example, if the constructor of the class use in &lt;code&gt;shared_ptr&lt;/code&gt; has a multi-parameter constructor that may throw an exception. Here&#39;s how &lt;code&gt;make_shared&lt;/code&gt; is used:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoSomething&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; x&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;make_shared&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //the value of *x is 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; y&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //this is valid!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    //now both x and y point to the integer&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;Note: Shared pointers change a fundamental aspect of C++, which is &quot;ownership&quot;. When using shared pointers, it may be easy to lose track of the object. This is a common problem in asynchronous applications. This is a story for another day though.&lt;/p&gt;
&lt;p&gt;Note 2: The reference counter of &lt;code&gt;shared_ptr&lt;/code&gt; is thread-safe. You can pass it among threads with no problems. However, the thread-safety of the underlying object it points to is your responsibility.&lt;/p&gt;
&lt;h3 id=&quot;scenario-2-i-don-t-have-c-11-and-i-can-t-use-boost&quot;&gt;Scenario 2: I don&#39;t have C++11 and I can&#39;t use boost&lt;/h3&gt;
&lt;p&gt;This is a common scenario in organizations that maintain very old software. The solution to this is very easy. Write your own smart pointer class. How hard can it be? Here&#39;s a simple quick-and-dirty example that works:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;template&lt;/span&gt;&lt;span&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SmartPtr&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 style=&quot;color: #6F42C1;&quot;&gt;    T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span&gt; ptr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    // disable copying by making assignment and copy-construction private&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SmartPtr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SmartPtr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;) {} &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SmartPtr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SmartPtr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SmartPtr&lt;/span&gt;&lt;span&gt;();}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SmartPtr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; the_ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NULL&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;        ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NULL&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        reset&lt;/span&gt;&lt;span&gt;(the_ptr);&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 style=&quot;color: #6F42C1;&quot;&gt;    ~SmartPtr&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 style=&quot;color: #6F42C1;&quot;&gt;        reset&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 style=&quot;color: #D73A49;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; reset&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; the_ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NULL&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 style=&quot;color: #D73A49;&quot;&gt;        if&lt;/span&gt;&lt;span&gt;(ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NULL&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 style=&quot;color: #D73A49;&quot;&gt;            delete&lt;/span&gt;&lt;span&gt; ptr;&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;        ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; the_ptr;&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 style=&quot;color: #6F42C1;&quot;&gt;    T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; get&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //get the pointer&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 style=&quot;color: #D73A49;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ptr;&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 style=&quot;color: #6F42C1;&quot;&gt;    T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&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 style=&quot;color: #D73A49;&quot;&gt;        return *&lt;/span&gt;&lt;span&gt;ptr;&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 style=&quot;color: #6F42C1;&quot;&gt;    T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; release&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //release ownership of the pointer&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 style=&quot;color: #6F42C1;&quot;&gt;        T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span&gt; ptr_to_return&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; ptr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NULL&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ptr_to_return;&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;and that&#39;s it! The method &lt;code&gt;release()&lt;/code&gt;, I haven&#39;t explained. It simply releases the pointer without deleting it. So it&#39;s a way to tell the &lt;code&gt;unique_ptr&lt;/code&gt;: &quot;Give me the pointer, and forget about deleting it; I&#39;ll take care of that myself&quot;.&lt;/p&gt;
&lt;p&gt;You can now use this class exactly like you use &lt;code&gt;unique_ptr&lt;/code&gt;. Creating your own &lt;code&gt;shared_ptr&lt;/code&gt; is a little more complicated though, and depends on your needs. Here&#39;s the questions you need to ask yourself on how to design this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Do you need multithreading support? &lt;code&gt;shared_ptr&lt;/code&gt; supports thread-safe reference counting.&lt;/li&gt;
&lt;li&gt;Do you need to just count references, or also track them? For some cases, one might need to track all references with something like a vector of references or a map.&lt;/li&gt;
&lt;li&gt;Do you need to support &lt;code&gt;release()&lt;/code&gt;? Releasing is not supported in &lt;code&gt;shared_ptr&lt;/code&gt;, since it depends on reference counting, there&#39;s no way to tell other instances to release.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;More requirements will require more work, especially that prior to C++11, multithreading was not in the C++ standard, meaning that you&#39;re gonna have to use system-specific C++.&lt;/p&gt;
&lt;p&gt;For a strictly single-threaded application with C++03, I created a shared pointer implementation that supports releasing. &lt;a rel=&quot;external&quot; href=&quot;https://git.afach.de/samerafach/SharedPtr-cpp03&quot;&gt;Here&#39;s the source code&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;scenario-3-enable-a-flag-do-something-then-disable-it-again&quot;&gt;Scenario 3: Enable a flag, do something, then disable it again&lt;/h3&gt;
&lt;p&gt;Consider the following code, which is common in GUI applications:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; GUIClass&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;addTheFiles&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;vector&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;FileType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; files&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 style=&quot;color: #005CC5;&quot;&gt;    this&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;disableButton&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;/span&gt;&lt;span&gt;; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; files.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;size&lt;/span&gt;&lt;span&gt;(); i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&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;        fileManager.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;addFile&lt;/span&gt;&lt;span&gt;(files[i]);&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 style=&quot;color: #005CC5;&quot;&gt;    this&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;enableButton&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;While this looks legitimate way to do things, it&#39;s not. This is absolutely no different that the pointer situation. What if adding fails? Either because of a memory problem, or because of some exception? The function will exit without reenabling that button, and your program will become unusable and the user will probably have to restart it.&lt;/p&gt;
&lt;p&gt;Solution? Just like before. Don&#39;t do it yourself, and get the destructor of some class to do it for you. Let&#39;s do this. What do we need? We need a class that will call a function with a reference to some variable on exit. Consider the following class:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AutoHandle&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 style=&quot;color: #6F42C1;&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;function&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span&gt;()&amp;gt; func;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    bool&lt;/span&gt;&lt;span&gt; done&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //used to make sure the call is done only once&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    // disable copying and moving&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = delete&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;function&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span&gt;()&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; the_func&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;        func&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; the_func;&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 style=&quot;color: #D73A49;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; doCall&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 style=&quot;color: #D73A49;&quot;&gt;        if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;!&lt;/span&gt;&lt;span&gt;done) &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 style=&quot;color: #6F42C1;&quot;&gt;            func&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            done&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; true&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 style=&quot;color: #6F42C1;&quot;&gt;    ~AutoHandle&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 style=&quot;color: #6F42C1;&quot;&gt;        doCall&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;Let&#39;s use it!&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; GUIClass&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;addTheFiles&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;vector&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;FileType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; files&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 style=&quot;color: #005CC5;&quot;&gt;    this&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;disableButton&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle ah&lt;/span&gt;&lt;span&gt;([&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;this&lt;/span&gt;&lt;span&gt;](){&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;this&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;enableButton&lt;/span&gt;&lt;span&gt;();});&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //lambda function that contains the function to be called on exit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;/span&gt;&lt;span&gt;; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; files.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;size&lt;/span&gt;&lt;span&gt;(); i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&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;        fileManager.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;addFile&lt;/span&gt;&lt;span&gt;(files[i]);&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 style=&quot;color: #6A737D;&quot;&gt; //Now, the function enableButton() will definitely be called definitely on exit.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way, you guarantee that enableButton() will be called when the function exits. This whole thing here is C++11, but doing it in C++03 is not impossible, though I completely sympathize with you if you feel it&#39;s too much work for such a simple task, because:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Since there&#39;s no std::function in C++03, we&#39;re gonna have to make that class template that accepts functors (function objects)&lt;/li&gt;
&lt;li&gt;Since there&#39;s no lambda functions in C++03, we&#39;re gonna have to make the call a new functor for every case (depending on how much you would like to toy with templates, also another big topic)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Just for completeness, here&#39;s how you could use &lt;code&gt;AutoHandle&lt;/code&gt; in C++03 with a Functor:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;template&lt;/span&gt;&lt;span&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; CallFunctor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AutoHandle&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 style=&quot;color: #D73A49;&quot;&gt;    bool&lt;/span&gt;&lt;span&gt; done;&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //used to make sure the call is done only once&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    CallFunctor&lt;/span&gt;&lt;span&gt; func;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    // disable copying by making assignment and copy-construction private&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp; operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AutoHandle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;return *&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;this&lt;/span&gt;&lt;span&gt;;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; caller&lt;/span&gt;&lt;span&gt;) :&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;CallFunctor&lt;/span&gt;&lt;span&gt;(caller))&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;        done&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; false&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 style=&quot;color: #D73A49;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; doCall&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 style=&quot;color: #D73A49;&quot;&gt;        if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;!&lt;/span&gt;&lt;span&gt;done)&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 style=&quot;color: #6F42C1;&quot;&gt;            func&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            done&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; true&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 style=&quot;color: #6F42C1;&quot;&gt;    ~AutoHandle&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 style=&quot;color: #6F42C1;&quot;&gt;        doCall&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 style=&quot;color: #D73A49;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; DoEnableButtonFunctor&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 style=&quot;color: #6F42C1;&quot;&gt;    GUIClass&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span&gt; this_ptr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    DoEnableButtonFunctor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;GUIClass&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; thisPtr&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;        this_ptr&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; thisPtr;&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 style=&quot;color: #D73A49;&quot;&gt;    void operator&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&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;        this_ptr-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;enableButton&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;Here&#39;s how you can use this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; GUIClass&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;addTheFiles&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;vector&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;FileType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; files&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 style=&quot;color: #005CC5;&quot;&gt;    this&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;disableButton&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    AutoHandle&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;DoEnableButtonFunctor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;GUIClass&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ah&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;this&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; //functor will be called on exit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;/span&gt;&lt;span&gt;; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; files.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;size&lt;/span&gt;&lt;span&gt;(); i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&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;        fileManager.&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;addFile&lt;/span&gt;&lt;span&gt;(files[i]);&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 style=&quot;color: #6A737D;&quot;&gt; //Now, the function enableButton() will definitely be called definitely on exit.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, writing a functor for every case is a little painful, but depending on the specific case, you may decide. However, in C++11 projects, there&#39;s no excuse. You can easily make your code way more reliable with lambdas.&lt;/p&gt;
&lt;p&gt;Remember, you&#39;re not bound to the destructor to do the calls. You can also call &lt;code&gt;doCall()&lt;/code&gt; yourself anywhere (equivalent to &lt;code&gt;reset()&lt;/code&gt; in &lt;code&gt;unique_ptr&lt;/code&gt;). But the destructor will *guarantee* that the worst case scenario is covered if something went wrong.&lt;/p&gt;
&lt;h3 id=&quot;scenario-4-opening-and-closing-resources&quot;&gt;Scenario 4: Opening and closing resources&lt;/h3&gt;
&lt;p&gt;This could even be more dangerous than the previous cases. Consider the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ReadData&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 style=&quot;color: #D73A49;&quot;&gt;    int&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; OpenSerialPort&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;COM3&amp;quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ReadData&lt;/span&gt;&lt;span&gt;(handle);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Close&lt;/span&gt;&lt;span&gt;(handle);&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;This form is quite common in old libraries. I faced such a format with the HDF5 library. If you read the previous sections, you&#39;ll get the problem with such usage and the idea on how to fix it. It&#39;s all the same. You *should never* close resources manually. For my HDF5 problem, I wrote a &lt;code&gt;SmartHandle&lt;/code&gt; class that guarantees that HDF5 resources are correctly closed. &lt;a rel=&quot;external&quot; href=&quot;https://git.afach.de/samerafach/SmartHandle&quot;&gt;Find it here&lt;/a&gt;. Of course, the formal right way to do this is to write a whole wrapper for the library. This may be an over-kill depending on your project constraints.&lt;/p&gt;
&lt;h4 id=&quot;notes-on-valgrind&quot;&gt;Notes on Valgrind&lt;/h4&gt;
&lt;p&gt;If you follow these rules, you&#39;ll be 100% safe with the resources you use. You rarely will ever need to use Valgrind. However, when you write the classes that we talked about (such as AutoHandle, SmartPtr, etc), it&#39;s very, very important not only to test it with Valgrind, but also to write good tests that will cover every corner case. Because once you do these classes right, you never have to worry about them. If you do them wrong, the consequences could be catastrophic. Surprising? Welcome to object-orient programming! This is exactly what &quot;separation of concerns&quot; mean.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Whenever you have to do, then undo something, then keep in mind that you shouldn&#39;t have this manually done. Sometimes it&#39;s safe and trivial, but many times it may lead to simply bad and error-prone design. I covered a few cases and different ways to tackle the issue. By following these examples, I guarantee that your code will become more compact (given that you&#39;re using C++11) and way more reliable.&lt;/p&gt;
&lt;h5 id=&quot;just-a-side-note-on-standards-and-rules&quot;&gt;Just a side-note on standards and rules&lt;/h5&gt;
&lt;p&gt;Sometimes when I discuss such issues related to modern designs, people claim that they have old code, and they don&#39;t want to change the style of the old code for consistency. Other times they claim that this would be violating some standard issued by some authority. All these reasons don&#39;t show that doing or not doing this is right or wrong. It just shows that someone doesn&#39;t want to do them because they&#39;re &quot;following the rules&quot;. To me (and I can&#39;t emphasize enough that this is my opinion), it just is like asking someone &quot;Why do you do this twice a day, how does it help you?&quot;, and get the answer &quot;Because my religion tells me I have to do it&quot;. While I respect all ideologies, such an answer is not a rational answer that justifies the pros and cons of doing something. That answer doesn&#39;t tell why doing that twice a day helps that guy&#39;s health, physically or mentally or otherwise. He just is following a rule *that shouldn&#39;t be discussed*. I&#39;m a rational person and I like discussing things by putting them on the table, which helps in achieving the best outcome. If someone&#39;s answer is &quot;I have a standard I need to follow it&quot;, then that effectively and immediately closes the discussion. There&#39;s nothing else to add. Please note that I&#39;m not encouraging breaking the rules here. If your superior tells you how to do it, and you couldn&#39;t convince them otherwise, then just do what he tells you, because most likely your superior has a broader picture of other aspects of a project that don&#39;t depend on code only.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Can I use Qt Creator without Qt?</title>
        <published>2017-03-09T21:20:29Z</published>
        <updated>2017-03-09T21:20:29Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/qt-creator/"/>
        <id>https://blog.afach.de/qt-creator/</id>
        <content type="html" xml:base="https://blog.afach.de/qt-creator/">&lt;p&gt;Qt Creator... the best IDE for developing C and C++ I&#39;ve ever seen in my life. Since I like it that much, I&#39;m sharing some of what I know about it.
DISCLAIMER: I&#39;m not a lawyer, so don&#39;t hold me liable for anything I say here about licensing. Anything you do is your own responsibility. &lt;/p&gt;
&lt;h3 id=&quot;what-does-using-qt-creator-without-qt-mean&quot;&gt;What does using Qt Creator without Qt mean?&lt;/h3&gt;
&lt;p&gt;It simply means that you don&#39;t have to install or use the Qt libraries, including qmake. There are many reasons why that may be the case:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You may have an issue with licensing since qmake is LGPL licensed&lt;/li&gt;
&lt;li&gt;You may not have the possibility to install qmake alone without its whole gear, as is the case in Windows&lt;/li&gt;
&lt;li&gt;You may not want to compile the whole Qt libraries if the pre-compiled versions that work with your compiler is not available&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While I love Qt and use it all the time, I follow the principle of decoupling my projects from libraries if I don&#39;t use them. But since I love Qt Creator, I still want to use it! Reasons will become clear below.&lt;/p&gt;
&lt;h3 id=&quot;what-are-the-ingredients-of-this-recipe&quot;&gt;What are the ingredients of this recipe?&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Qt Creator&lt;/li&gt;
&lt;li&gt;CMake&lt;/li&gt;
&lt;li&gt;A compiler (gcc, MinGW or Visual Studio, or anything else)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You don&#39;t need to download the whole Qt SDK. Just Qt Creator. It&#39;s about 90 MB.&lt;/p&gt;
&lt;h2 id=&quot;basic-steps&quot;&gt;Basic steps&lt;/h2&gt;
&lt;p&gt;After installing the 3 ingredients, make sure that Qt Creator recognized that CMake exists on the computer. The next picture (click on it to magnify it) is how it looks like if CMake was found. If it doesn&#39;t find CMake, on Windows, most likely the reason is that you chose in the installation not to add CMake to the system&#39;s PATH. Eventually, you can just add it manually if it can&#39;t be found automatically.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/QtCreator-CMake.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
&lt;p&gt;Next, make sure that Qt recognizes the compiler and the debugger as you see in the next pictures. Again, you can add them manually.
For Visual Studio to be found automatically, I guess the environment variable &lt;code&gt;VS140COMNTOOLS&lt;/code&gt; has to be defined. &quot;140&quot; is version 14 of Visual Studio, which is the version number of Visual Studio 2015. It&#39;s defined by default when Visual Studio is installed. For MinGW to be detected automatically, the &lt;code&gt;bin&lt;/code&gt; of MinGW has to be in PATH.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/QtCreator-Compilers.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
 &lt;p&gt;&lt;img src=&quot;/media/QtCreator-Debuggers.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;adding-the-complete-tool-chain-kit&quot;&gt;Adding the complete tool-chain/kit&lt;/h2&gt;
&lt;p&gt;Go to the &quot;Kits&quot; tab. If you have Qt libraries installed and configured, you&#39;ll see them there. I don&#39;t like Qt SDK, and I usually compile my own versions of Qt. You&#39;ll see that in the next screenshot.
What you see in the next screenshot are 3 kits that use Qt libraries, and another that do not. The free version of Visual Studio (2012 and later) comes with both 32-bit and 64-bit compilers. You can choose any one of them, or both (like I do). I don&#39;t use MinGW often on Windows, so I install only 1 version of it (I use MinGW on Windows primarily because it offers the &quot;&lt;code&gt;-pedantic&quot;&lt;/code&gt; flag, which gives the chance to experiment with C++ standard-approved features).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/QtCreator-Kits-General.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
&lt;p&gt;Now click &quot;Add&quot;, and choose the fields as shown below (most importantly, configure CMake correctly, it&#39;s a little tricky, and the error on Qt Creator doesn&#39;t tell you what you did wrong)
If you&#39;re getting unexplained errors when running CMake, follow the following instructions carefully&lt;/p&gt;
&lt;h3 id=&quot;visual-studio&quot;&gt;Visual Studio&lt;/h3&gt;
&lt;p&gt;The following is a screenshot of how Visual Studio configuration should look like (screenshot is for 64-bit)&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/QtCreator-Kits-VS64.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
&lt;p&gt;Set the following&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For the 32-bit compiler: Choose the compiler with (x86)&lt;/li&gt;
&lt;li&gt;For the 64-bit compiler: Choose the compiler with (x86_am64) or (amd64)&lt;/li&gt;
&lt;li&gt;Choose &quot;None&quot; for Qt version&lt;/li&gt;
&lt;li&gt;Choose the correct debugger&lt;/li&gt;
&lt;li&gt;Most importantly: After choosing CMake from the drop-down list, make sure that &lt;code&gt;CMake generator&lt;/code&gt; is chosen to be &quot;NMake Makefiles&quot;, and the &lt;code&gt;Extra generator&lt;/code&gt; to be CodeBlocks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last piece of information is the invitation to all kinds of problems. If running CMake doesn&#39;t work, you most likely configured that part incorrectly.&lt;/p&gt;
&lt;h3 id=&quot;mingw&quot;&gt;MinGW&lt;/h3&gt;
&lt;p&gt;The following is a screenshot of how MinGW configuration should look like:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/QtCreator-Kits-MinGW.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Choose &quot;None&quot; for Qt version&lt;/li&gt;
&lt;li&gt;Choose the correct debugger&lt;/li&gt;
&lt;li&gt;Most importantly: After choosing CMake from the drop-down list, make sure that &lt;code&gt;CMake generator&lt;/code&gt; is chosen to be &quot;MinGW Makefiles&quot;, and the &lt;code&gt;Extra generator&lt;/code&gt; to be CodeBlocks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last piece of information is the invitation to all kinds of problems. If running CMake doesn&#39;t work, you most likely configured that part incorrectly.
I have to say that from my experience, Qt Creator some times fails to run CMake with MinGW with no good reason. I fix this by switching the &quot;Extra generator&quot; to &quot;CodeLite&quot;, and back to &quot;CodeBlocks&quot;. I&#39;m currently using Qt Creator 4.2.0. It might be a bug?&lt;/p&gt;
&lt;h3 id=&quot;gcc-g&quot;&gt;gcc/g++&lt;/h3&gt;
&lt;p&gt;It&#39;s very easy to get gcc/g++ to work. The following is a screenshot:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/QtCreator-Kits-gcc.gif&quot; alt=&quot;&quot; class=&quot;content-img&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Choose &quot;None&quot; for Qt version&lt;/li&gt;
&lt;li&gt;Most importantly: After choosing CMake from the drop-down list, make sure that &lt;code&gt;CMake generator&lt;/code&gt; is chosen to be &quot;CodeBlocks - Unix Makefiles&quot;.  It&#39;s chosen by default, so nothing to worry about.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And you&#39;re done!&lt;/p&gt;
&lt;h2 id=&quot;what-can-i-do-with-cmake-qt-creator&quot;&gt;What can I do with CMake + Qt Creator?&lt;/h2&gt;
&lt;h3 id=&quot;no-ultimate-dependence-on-qt-creator&quot;&gt;No ultimate dependence on Qt Creator!&lt;/h3&gt;
&lt;p&gt;Visual Studio solutions don&#39;t work without Visual Studio. Netbeans configuration is stored in &quot;netbeans project directory&quot;. it&#39;s very annoying that every IDE has its own weird format! I never stop hearing people complaining about porting their programs to other systems and having problems because of this.
One of the things I like most about Qt Creator is the fact that it doesn&#39;t have any special project files for itself. The Makefile itself (CMake file, in this case, or qmake otherwise) is the project file. This ensures not only 100% portability (since both make systems are cross-platform) but also independence of Qt Creator itself. In the future, if the whole Qt Creator project goes down, your project won&#39;t be affected at all.&lt;/p&gt;
&lt;h3 id=&quot;what-if-i-want-to-use-qt-creator-just-as-an-ide-without-having-to-build-a-project-through-it&quot;&gt;What if I want to use Qt Creator just as an IDE without having to build a project through it?&lt;/h3&gt;
&lt;p&gt;I had the &quot;luxury&quot; of getting a project from a space agency to add some features to it, where they had their own build system. I wasn&#39;t able to use Qt Creator to build the project, but...
But why use Qt Creator? Simply because you&#39;ll get&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Syntax highlighting&lt;/li&gt;
&lt;li&gt;target functions and classes following&lt;/li&gt;
&lt;li&gt;advanced refactoring options (like renaming classes, functions, etc)&lt;/li&gt;
&lt;li&gt;Search capabilities in source files.&lt;/li&gt;
&lt;li&gt;Repository updates checking&lt;/li&gt;
&lt;li&gt;Type hierarchy navigation with a click&lt;/li&gt;
&lt;li&gt;And lots more!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With all these, yet, while the project contains a few thousand source files, I was able to add *all* files and parse them with Qt using a few CMake lines. And it was fast enough to handle all that!
How to do it? How to add all these files in one step?
CMake supports recursive adding of source files. Consider the following CMake file (always called &lt;code&gt;CMakeLists.txt&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;cmake_minimum_required&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;VERSION&lt;/span&gt;&lt;span&gt; 3.0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;PROJECT&lt;/span&gt;&lt;span&gt;(MyProject)&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 style=&quot;color: #D73A49;&quot;&gt;file&lt;/span&gt;&lt;span&gt;(GLOB_RECURSE MySrcFiles&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;${CMAKE_SOURCE_DIR}/src/*.cpp&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;${CMAKE_SOURCE_DIR}/src/*.h&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 style=&quot;color: #D73A49;&quot;&gt;add_library&lt;/span&gt;&lt;span&gt;(MySrcFilesLib&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ${MySrcFiles}&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 style=&quot;color: #D73A49;&quot;&gt;add_executable&lt;/span&gt;&lt;span&gt;(MyExecutable main.cpp)&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 style=&quot;color: #D73A49;&quot;&gt;target_link_libraries&lt;/span&gt;&lt;span&gt;(MyExecutable MySrcFilesLib)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first two lines are obvious. The &quot;&lt;code&gt;file&quot;&lt;/code&gt; part recursively finds all the files under the directory mentioned and saves them in the variable &lt;code&gt;${MySrcFiles}&lt;/code&gt;. The variable &lt;code&gt;${CMAKE_SOURCE_DIR}&lt;/code&gt; is basically the directory where you cmake file &quot;&lt;code&gt;CMakeLists.txt&quot;&lt;/code&gt; is located. Feel free to set any directory you find fit.
The &quot;&lt;code&gt;add_library&lt;/code&gt;&quot; part creates a library from the source (and header) files saved in &lt;code&gt;${MySrcFiles}&lt;/code&gt;.
The &quot;&lt;code&gt;add_executable&lt;/code&gt;&quot; part  &quot;creates&quot; the executable, which is then linked to the libraries you added.
That last linking part is not necessary if you don&#39;t want to build in Qt Creator.
With such a simple CMake file, Qt Creator was smart enough to add all the source files, parse them, and give me all the functionality I needed to edit that project successfully.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>SSH proxy with Putty that reconnects automatically</title>
        <published>2016-07-31T22:47:41Z</published>
        <updated>2016-07-31T22:47:41Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/ssh-proxy/"/>
        <id>https://blog.afach.de/ssh-proxy/</id>
        <content type="html" xml:base="https://blog.afach.de/ssh-proxy/">&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;Putty can be used to tunnel your connection through your SSH server. It creates a SOCKS proxy that you can use anywhere. The annoying part there is that if Putty disconnects for any reason, you&#39;ll have to reestablish the connection manually. If you&#39;re happy reconnecting putty manually all the time, then this article is not for you. Otherwise, keep reading to see how I managed to find a fair solution to this problem.&lt;/p&gt;
&lt;h3 id=&quot;ingredients&quot;&gt;Ingredients&lt;/h3&gt;
&lt;p&gt;For this recipe, you need the following&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Putty, of course. You could use &quot;Putty Tray&quot;, which supports being minimized to taskbar.&lt;/li&gt;
&lt;li&gt;Python 3&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I understand that you may not have or use Python, but the solution I used depends on it, unfortunately. You could download &lt;a rel=&quot;external&quot; href=&quot;https://www.continuum.io/downloads&quot;&gt;Miniconda 3&lt;/a&gt;,  which is a smaller version of Anaconda 3. Anaconda is my recommended Python release.&lt;/p&gt;
&lt;h3 id=&quot;how-does-this-work-in-a-nutshell&quot;&gt;How does this work in a nutshell?&lt;/h3&gt;
&lt;p&gt;The idea is very simple.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a Putty session configuration that suites you&lt;/li&gt;
&lt;li&gt;Configure the session to close on disconnect&lt;/li&gt;
&lt;li&gt;Create a script that will relaunch your putty session on exit&lt;/li&gt;
&lt;li&gt;Make sure that the launcher you&#39;re gonna use doesn&#39;t keep a command prompt window open (which is why I&#39;m using Python; pythonw.exe solves this issue).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;configuring-putty&quot;&gt;Configuring Putty&lt;/h3&gt;
&lt;p&gt;To create a putty tunnel proxy, load your favorite session configuration, and then go to the tunnels configuration, and use the following settings, assuming you want the SOCKS proxy port to be 5150. Choose any port you like.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/PuttySOCKS.png&quot; alt=&quot;PuttySOCKS&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/PuttySOCKS.b1c1635dc1a4d448.png 400w, /media/PuttySOCKS.png 678w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;After connecting with this configuration, this creates a SOCKS that you can connect to with the loopback IP-Address, 127.0.0.1 at port 5150.
One more important thing to configure in putty, is to set putty to exit on failure. This is important because we&#39;re gonna set putty to reconnect through a program that detects that it exited to start it again.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/PuttySessions.jpg&quot; alt=&quot;PuttySessions&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/PuttySessions.8c2c336e7b45c9ac.jpg 400w, /media/PuttySessions.jpg 602w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;configuring-python-and-the-launch-script&quot;&gt;Configuring Python and the launch script&lt;/h3&gt;
&lt;p&gt;Assuming you installed Python 3 and included in your PATH, now you have to install a package called &lt;em&gt;tendo&lt;/em&gt;. This package is used to prevent running multiple instances of the program.
To install it, first, run the command prompt of Windows (in case Python is installed directly on the system drive, C:, you have to run it as administrator). In the command prompt, to ensure that Python is working fine, run:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;python -V&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If this responds with no error and gives a message like:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Python 3.5.1 :: Anaconda 4.1.0 (32-bit)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you&#39;re good to go! Otherwise, make sure that Python is added to PATH, and try again.
To install tendo, simply run in command prompt&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pip install tendo&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that, in the directory of putty, write this script to a file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; subprocess&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;/span&gt;&lt;span&gt; tendo&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;/span&gt;&lt;span&gt; singleton&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; time&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;me&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; singleton.SingleInstance()&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; #exits if another instance exists&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 style=&quot;color: #D73A49;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; True&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Starting putty session...&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; subprocess.call(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&amp;quot;putty.exe&amp;quot; -load &amp;quot;mysession&amp;quot;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Putty session closed... restarting...&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; time.sleep(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;   #sleep to avoid infinitely fast restarting if no connection is present&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The name &quot;mysession&quot; is the name of your session in putty. Replace it with your session name.
This script simply checks first that the current instance is the only instance, and then runs an infinite loop that keeps running the program every time it exits. So we made putty exit on disconnect, and this program will just run infinitely
Save this script to some file like &quot;MyLoop.pyw&quot;.&lt;/p&gt;
&lt;h3 id=&quot;testing-the-loop&quot;&gt;Testing the loop&lt;/h3&gt;
&lt;p&gt;Python has two executables. First is &quot;python.exe&quot;, and the other one is &quot;pythonw.exe&quot;. The difference is quite simple. The first one, &quot;python.exe&quot;, runs your script as a terminal program. The second one, &quot;pythonw.exe&quot;, runs your script without a terminal. It&#39;s designed for GUI applications. Now &quot;python.exe&quot; is not what we need, but it still is useful for debugging the script. So whenever you have a problem or when you want to run this for the first time, switch to/use &quot;python.exe&quot;. Once you&#39;re done and everything looks fine, switch to &quot;pythonw.exe&quot;.&lt;/p&gt;
&lt;h3 id=&quot;final-step-the-execution-shortcut&quot;&gt;Final step: The execution shortcut&lt;/h3&gt;
&lt;p&gt;This is not necessary, but it makes things easy. It makes it easy to control whether you want to use &quot;python.exe&quot; or &quot;pythonw&quot;. It makes it also possible to make your script handy. Simple create a shortcut to &quot;python.exe&quot; or &quot;pythonw.exe&quot;, and the first command line parameter should be your script. Remember that &quot;Start in&quot; has to be the directory, where Putty is located. Following picture is an example of how that shortcut should look like.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/PuttyLoopShortcut.jpg&quot; alt=&quot;PuttyLoopShortcut&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/PuttyLoopShortcut.2b968a35159d784b.jpg 400w, /media/PuttyLoopShortcut.jpg 480w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;And you&#39;re good to go! Start with &quot;python.exe&quot;, and once it works, and you find that every time you exit putty or a disconnection happens it relaunches it, switch to &quot;pythone.exe&quot;, and you&#39;re done.&lt;/p&gt;
&lt;h3 id=&quot;final-notes&quot;&gt;Final notes&lt;/h3&gt;
&lt;p&gt;This is not a super-fancy solution. This is a solution that&#39;ll get you through and get the job done. If you want to exit the looping script, you&#39;ll have to kill Python from your task-manager.
You may create a fancy taskbar app that&#39;ll do the looping and exits, which I would&#39;ve done if I had the time. So please share it if you do! You can use PyQt or PySide for that.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;With this, you&#39;ll keep reconnecting on disconnect, and you can get all your software to use your ssh server as a SOCKS proxy. Cheers!&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Tunnel through https to your ssh server, and bypass all firewalls - The perfect tunnel! (HAProxy + socat)</title>
        <published>2016-06-10T17:56:03Z</published>
        <updated>2016-06-10T17:56:03Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/ssh-tunnel/"/>
        <id>https://blog.afach.de/ssh-tunnel/</id>
        <content type="html" xml:base="https://blog.afach.de/ssh-tunnel/">&lt;h3 id=&quot;disclaimer&quot;&gt;Disclaimer&lt;/h3&gt;
&lt;p&gt;Perhaps there&#39;s no way to emphasize this more, but I don&#39;t encourage violation of corporate policy. I do this stuff for fun, as I love programming and I love automating my life and gaining more convenience and control with technology. I&#39;m not responsible for any problem you might get with your boss in your job for using this against your company&#39;s firewall, or any similar problem for that matter.&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I was in a hotel in Hannover, when I tried to access my server&#39;s ssh. My ssh client, Putty, gave this disappointing message&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/PuttyConnectionRefused.png&quot; alt=&quot;PuttyConnectionRefused&quot; class=&quot;content-img&quot; srcset=&quot;/media/PuttyConnectionRefused.png 297w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;At first I got scared as I thought my server is down, but then I visited the websites of that server, and they were fine. After some investigation, I found that my hotel blocks any access to many ports, including port 22, i.e., ssh. Did this mean that I won&#39;t have access to my server during my trip? Not really!
I assume you&#39;re using a Windows client, but in case you&#39;re using linux, the changes you have to do are minimal, and I provide side-by-side how to do the same on a linux client. Let me know if you have a problem with any of this.&lt;/p&gt;
&lt;h2 id=&quot;tunneling-mechanism-and-problems-with-other-methods-that-are-already-available&quot;&gt;Tunneling mechanism, and problems with other methods that are already available&lt;/h2&gt;
&lt;p&gt;There are software that does something similar for you automatically, like &lt;a rel=&quot;external&quot; href=&quot;http://www.rutschle.net/tech/sslh.shtml&quot;&gt;sslh&lt;/a&gt;, but there&#39;s a problem there.&lt;/p&gt;
&lt;h6 id=&quot;what-does-sslh-do&quot;&gt;What does sslh do?&lt;/h6&gt;
&lt;p&gt;When you install sslh on your server, you choose, for example, port 443 for it. Port 443 is normally for http-ssl (https), that&#39;s normally taken by your webserver. So you change also your webserver&#39;s port to some arbitrary port, say 22443. Then, say you want to connect to that server: sslh analyzes and detects whether the incoming network packets are ssh or http. If the packets are ssh, it forwards them to port 22. If the packets looks like https, it forwards them to the dummy port you chose, which is 22443 as we assumed.&lt;/p&gt;
&lt;h6 id=&quot;what-s-the-problem-with-sslh-and-similar-programs&quot;&gt;What&#39;s the problem with sslh, and similar programs?&lt;/h6&gt;
&lt;p&gt;It all depends on how sophisticated the firewall you&#39;re fighting is. Some firewalls are mediocre, and they just blindly open port 443, and you can do your sslh trick there and everything will work fine. But smart firewalls are not that dull; they analyze your packets and then judge whether you&#39;re allowed to be connected. Hence, a smart firewall will detect that you&#39;re trying to tunnel ssh, and will stop you!&lt;/p&gt;
&lt;h6 id=&quot;how-do-we-solve-this-problem&quot;&gt;How do we solve this problem?&lt;/h6&gt;
&lt;p&gt;The solution is: Masquerade the ssh packets inside an https connection, hence, the firewall will have to do a &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Man-in-the-middle_attack&quot;&gt;man-in-the-middle attack&lt;/a&gt; in order to know what you&#39;re trying to do. This will never happen! Hence, I call this solution: &quot;&lt;strong&gt;The perfect solution&lt;/strong&gt;&quot;.&lt;/p&gt;
&lt;h2 id=&quot;how-to-create-the-tunnel&quot;&gt;How to create the tunnel?&lt;/h2&gt;
&lt;p&gt;I use &lt;a rel=&quot;external&quot; href=&quot;http://www.haproxy.org/&quot;&gt;HAProxy&lt;/a&gt; for this purpose. You need that on your server. It&#39;s available in standard linux systems. In Debian and Ubuntu, you can install it using&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; apt-get install haproxy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You will need &quot;socat&quot; on your client to connect to this tunnel. This comes later after setting up HAProxy.&lt;/p&gt;
&lt;h6 id=&quot;how-does-haproxy-work&quot;&gt;How does HAProxy work?&lt;/h6&gt;
&lt;p&gt;I don&#39;t have a PhD in HAProxy, it&#39;s fairly a complicated program that can be used for many purposes, including load balancing and simple internal proxying between different ports, and I use it only for this purpose. Let me give a brief explanation on how it works. HAProxy uses the model of frontends and backends. A frontend is what a client sees. You set a port there, and a communication mode (tcp, for example). You tell also a frontend &quot;where these packets should go&quot;, based on some conditions (called ACL, Access Control Lists). You choose to which &lt;em&gt;backend&lt;/em&gt; the packets have to go. The backend contains information about the target local port. So in short words, you tell HAProxy where to forward these packets from the frontend to the backend based on some conditions.&lt;/p&gt;
&lt;h6 id=&quot;a-little-complication-if-you-use-https-websites-on-the-same-server&quot;&gt;A little complication if you use https websites on the same server&lt;/h6&gt;
&lt;p&gt;If you use https webserver on the same machine, you&#39;ll have a problem. The problem is that you&#39;ll need to check whether the packets are ssh &lt;strong&gt;before decrypting them&lt;/strong&gt;, because once you decrypt them, you can&#39;t use them as non-encrypted again (hence haproxy doesn&#39;t support forking encrypted and decrypted packets side-by-side). This is because you choose to decrypt in your frontend. That&#39;s why we use &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Server_Name_Indication&quot;&gt;SNI (Server Name Indication)&lt;/a&gt; and do a trick:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If there&#39;s no SNI (no server name, just IP address), then forward to ssh&lt;/li&gt;
&lt;li&gt;If server name used is ssh.example.com (some subdomain you choose), then forward to ssh (optional)&lt;/li&gt;
&lt;li&gt;If anything else is the case, forward to the https web server port&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We also use 2-frontends. The first one is the main one, and the second is a dummy frontend, and is only used to decrypt the ssh connection&#39;s https masquerade. HAProxy decrypts only in frontends.&lt;/p&gt;
&lt;h2 id=&quot;let-s-do-it&quot;&gt;Let&#39;s do it!&lt;/h2&gt;
&lt;p&gt;The configuration file of HAProxy in Debian/Ubuntu is&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;/etc/haproxy/haproxy.cfg&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You could use nano, vi or vim to edit it (you definitely have to be root or use sudo). For example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; nano /etc/haproxy/haproxy.cfg&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h6 id=&quot;assumptions&quot;&gt;Assumptions&lt;/h6&gt;
&lt;ol&gt;
&lt;li&gt;Your main https port is 443&lt;/li&gt;
&lt;li&gt;Your main ssh port is 22&lt;/li&gt;
&lt;li&gt;Your https webserver is now on 22443&lt;/li&gt;
&lt;li&gt;The dummy ssh port is 22222 (used just for decryption, it doesn&#39;t matter what you put it)&lt;/li&gt;
&lt;/ol&gt;
&lt;h6 id=&quot;main-frontend&quot;&gt;Main frontend&lt;/h6&gt;
&lt;p&gt;This is the frontend that will take care of the main port (supposedly 443). Everything after a sharp sign (#) on a line is a comment.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#here&amp;#39;s a definition of a frontend. You always give frontends and backends a name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;frontend TheMainSSLPort&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; mode tcp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option tcplog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; bind 0.0.0.0:443 #listen to port 443 under all ip-addresses&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; timeout client 5h #timeout is quite important, so that you don&amp;#39;t get disconnected on idle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option clitcpka&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; tcp-request inspect-delay 5s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; tcp-request content accept if { req_ssl_hello_type 1 }&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; #here you define the backend you wanna use. The second parameter is the backend name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; use_backend sshDecrypt if !{ req_ssl_sni -m found } #if no SNI is given, then go to SSH&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; use_backend sshDecrypt if { req_ssl_sni -i ssh.exmple.com } #if SNI is ssh.example.com, also go to ssh&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;default_backend sslWebServerPort #if none of the above apply, then this is https&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the previous configuration, we have two paths for the packets, i.e., two backends:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If the connection is ssh, the backend named &quot;sshDecrypt&quot; will be used.&lt;/li&gt;
&lt;li&gt;If the connection is https, the backend named &quot;sslWebServerPort&quot; will be used.&lt;/li&gt;
&lt;/ol&gt;
&lt;h6 id=&quot;the-https-backend&quot;&gt;The https backend&lt;/h6&gt;
&lt;p&gt;I put this here first because it&#39;s easier. All you have to do here is forward the packets to your webserver&#39;s port, which we assumed to be port 22433. The following is the relevant configuration:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;backend sslWebServerPort&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; mode tcp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option tcplog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; server local_https_server 127.0.0.1:22443 #forward to this server, port 22443&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the https part is done. Let&#39;s work on the ssh part.&lt;/p&gt;
&lt;h6 id=&quot;the-ssh-front-and-backends&quot;&gt;The ssh front- and backends&lt;/h6&gt;
&lt;p&gt;We&#39;ll have to use a trick, as mentioned before, to get this to work. Once a judgment is done for packets to go to ssh (using SNI), the packets have to be decrypted. This is not possible in a backend, thus we use a backend to forward the packets to a dummy frontend that decrypts the packets, and then send these to another backend to send the packets to the ssh server.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;backend sshToDecryptor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; mode tcp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option tcplog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; server sshDecFrontend 127.0.0.1:22222&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; timeout server 5h&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This forwards the packets to port 22222. Now we build a frontend at that port that decrypts the packets.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;frontend sshDecyprtionPort&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; timeout client 5h&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option clitcpka&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; bind 0.0.0.0:22222 ssl crt /path/to/combined/certs.pem no-sslv3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; mode tcp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option tcplog&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; tcp-request inspect-delay 5s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; tcp-request content accept if HTTP&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;default_backend sshServ #forward to the ssh server backend&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The file /path/to/combined/certs.pem has to contain your private key, certificate and certificate chain in one file of your SSL. Concatenate them all in one file.
Finally, the back end to the ssh server:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;backend sshServ&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; mode tcp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; option tcplog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; server sshServer1 127.0.0.1:22&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; timeout server 5h&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s all you need to create the tunnel.&lt;/p&gt;
&lt;h6 id=&quot;test-your-haproxy-configuration-on-the-server&quot;&gt;Test your haproxy configuration on the server&lt;/h6&gt;
&lt;p&gt;To test your configuration, stop HAProxy using&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; service haproxy stop&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and run the following command to start HAProxy in debug mode:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; haproxy&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -d -f&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; /etc/haproxy/haproxy.cfg&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &quot;-d&quot; flag is debug mode, and the &quot;-f&quot; flag is used to choose the config file. The typical output looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Available polling systems :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; epoll : pref=300, test result OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; poll : pref=200, test result OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; select : pref=150, test result FAILED&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Total: 3 (2 usable), will use epoll.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Using epoll() as the polling mechanism.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you don&#39;t get any errors, then your configuration is OK. Press ctrl+c to close this foreground version of HAProxy, and start the HAProxy service:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; service haproxy start&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h6 id=&quot;test-your-tunnel-from-your-client&quot;&gt;Test your tunnel from your client&lt;/h6&gt;
&lt;p&gt;To test your client, you can use OpenSSL. The following command will connect to the server.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;openssl s_client -connect ssh.example.com:443&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also use your IP address. This will connect to HAProxy, and will be interpreted as ssh, if your configuration is correct. Once it reaches the ssh server, you&#39;re good! You&#39;ll see lots of stuff from OpenSSL, and finally a few seconds later the following message will appear if you&#39;re using a Debian server:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The message will change depending on your server&#39;s linux distribution and OpenSSH server version. Once you see this message, this shows that you reached your ssh server successfully. You now have to setup a connection to your server&#39;s tunnel.&lt;/p&gt;
&lt;h2 id=&quot;connecting-to-the-ssh-server-using-the-tunnel&quot;&gt;Connecting to the ssh server using the tunnel&lt;/h2&gt;
&lt;p&gt;You need &lt;em&gt;socat&lt;/em&gt; to connect to the https tunnel, and then you ssh to that tunnel. The program, socat, can be downloaded either as a zip package (please google it and try it, if it works, great. I had problem with OpenSSL dlls back then when I first tried this). Or you can use &lt;a rel=&quot;external&quot; href=&quot;https://www.cygwin.com/&quot;&gt;Cygwin&lt;/a&gt; to get it. Cygwin is a set of linux programs compiled for Windows. Don&#39;t get too confident in the installer and just download all its components or you&#39;ll easily consume 30 GB of diskspace and consume 10 hours installing these components. Just download what you need.
In case you&#39;re using a linux client, socat is a standard program in linux. Just install it with your default package manager, e.g. in Debian/Ubuntu&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; apt-get install socat&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h6 id=&quot;running-the-socat-tunnel&quot;&gt;Running the socat tunnel&lt;/h6&gt;
&lt;p&gt;Open your Windows command prompt &lt;strong&gt;as administrator&lt;/strong&gt; (or linux terminal), and use the following command to connect to your server using socat&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;socat -d TCP-LISTEN:8888,fork,range=127.0.0.1/32 OPENSSL-CONNECT:ssh.example.com:443,verify=0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we use port 8888 as an intermediate local port on your client. Once this works with no errors, you&#39;re good to use an ssh client.
Warning: A &quot;-v&quot; flag is verbose for socat. Don&#39;t do this for serious connections, but only for tests, as it writes everything on the terminal where socat is running, and since Windows Command Prompt prints messages synchronously, it&#39;ll slow down everything for you.&lt;/p&gt;
&lt;h6 id=&quot;connect-with-your-ssh-client&quot;&gt;Connect with your ssh client&lt;/h6&gt;
&lt;p&gt;Assuming you&#39;re using putty, this is how your client should look like&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/PuttySSHTunnel.png&quot; alt=&quot;PuttySSHTunnel&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/PuttySSHTunnel.56ccb28eff8abb0a.png 400w, /media/PuttySSHTunnel.png 681w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;Or if you&#39;re using a linux client, simply use this in your terminal&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ssh 127.0.0.1 -p 443&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And you should connect, and you&#39;re done! Congratulations! You&#39;re connected to your ssh server through https.&lt;/p&gt;
&lt;h2 id=&quot;what-about-the-other-ports-other-than-ssh&quot;&gt;What about the other ports, other than ssh?&lt;/h2&gt;
&lt;p&gt;Once you got ssh working, everything else is easy. You can use an ssh SOCKS proxy tunnel. Putty does this easily for you. All you have to do is configure your connection as in the picture:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/PuttySOCKS.png&quot; alt=&quot;PuttySOCKS&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/PuttySOCKS.b1c1635dc1a4d448.png 400w, /media/PuttySOCKS.png 678w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;This creates a SOCKS proxy. To use it, I provide the following example that I do on Thunderbird to secure my e-mail connections. You can do the same on any program you like, even on your web browser:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/SOCKSProxySettingsThunderbird.png&quot; alt=&quot;SOCKSProxySettingsThunderbird&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/SOCKSProxySettingsThunderbird.46881140ccb293c2.png 400w, /media/SOCKSProxySettingsThunderbird.png 638w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;You can do the same on linux. Please google how to create an ssh tunnel on linux for SOCKS proxy.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;You&#39;re connected! You can bypass any firewall you want just by being given access port 443. The only way to stop you is by cutting off your internet completely :-)
Share this if you like it! I welcome your questions and any other thoughts in the comments.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Start your computer remotely using Raspberry Pi</title>
        <published>2015-08-30T20:55:47Z</published>
        <updated>2015-08-30T20:55:47Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/raspberry-remote/"/>
        <id>https://blog.afach.de/raspberry-remote/</id>
        <content type="html" xml:base="https://blog.afach.de/raspberry-remote/">&lt;h3 id=&quot;why-do-that&quot;&gt;Why do that?&lt;/h3&gt;
&lt;p&gt;I have my server at home, which contains multiple hard-drives with all my data on them in RAID configuration. I have my way to access this server remotely from any where in the world, but in order to access the server, it has to be turned on! So the problem is: How do I turn my server when I need it?
This whole thing took me like 4 hours of work. It turns out it&#39;s much easier than it looks like.&lt;/p&gt;
&lt;h3 id=&quot;why-is-keeping-the-server-turned-on-all-the-time-a-bad-idea&quot;&gt;Why is keeping the server turned on all the time a bad idea?&lt;/h3&gt;
&lt;p&gt;Of course, a web-server can be left turned on all the time to be accessed from everywhere at any time, but a server that is used to store data... I don&#39;t see why one would turn it on unless one needs something from it. In fact, I see the following disadvantages in keeping the server on all the time and benefits for being able to turn it on remotely:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;High power consumption... although the server I use is low-power, but why use 150 W all the time with no real benefit?&lt;/li&gt;
&lt;li&gt;Reduction of the server life-span, components like the processor has a mean life-time that will be consumed by continuous operation.&lt;/li&gt;
&lt;li&gt;Fans wear out and become noisier when used for longer times.&lt;/li&gt;
&lt;li&gt;What if the server froze? I should be able to restart it remotely.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;what-do-you-need-to-pull-this-off&quot;&gt;What do you need to pull this off?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (1 or 2, doesn&#39;t matter, but I&#39;ll be discussing 2)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/media/Pi2ModB1GB_-comp.jpeg&quot; alt=&quot;Pi2ModB1GB_-comp&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/Pi2ModB1GB_-comp.3c8634b6715255a2.jpg 400w, https://blog.afach.de/processed_images/Pi2ModB1GB_-comp.ff1f8ec766082d8e.jpg 800w, /media/Pi2ModB1GB_-comp.jpeg 845w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;5 Volts Relay. I use a 4 Channel relay module. It costs like $7 on eBay or Amazon depending on how many channels you need.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/media/Relay5V-4Ch.jpg&quot; alt=&quot;Relay5V-4Ch&quot; class=&quot;content-img&quot; srcset=&quot;/media/Relay5V-4Ch.jpg 350w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Jumper cables (female-female specifically if you&#39;re using Raspberry Pi + a similar Relay Module) to connect the Raspberry Pi to the Relay Module&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/media/JumperCables.jpg&quot; alt=&quot;JumperCables&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/JumperCables.50c2c98149ff64c8.jpg 400w, /media/JumperCables.jpg 650w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More wires and connectors to connect the server to the Raspberry Pi cleanly, without having a permanent long cord permanently connected to the server. I used scrap Molex 4-pin connectors:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/media/MolexConnector.jpg&quot; alt=&quot;MolexConnector&quot; class=&quot;content-img&quot; srcset=&quot;/media/MolexConnector.jpg 255w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;I cut a similar connector in half and used one part as a permanent connector to the server, and the other part went to the wire that goes to the Relay Module.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Finally, you need some expertise in Linux and SSH access as the operating system I use on my Raspberry Pi is &lt;a rel=&quot;external&quot; href=&quot;https://www.raspbian.org/&quot;&gt;Raspbian&lt;/a&gt;. This I can&#39;t teach here, unfortunately, as it&#39;s an extensive topic. Please learn how to access Raspberry Pi using SSH and how to install &lt;a rel=&quot;external&quot; href=&quot;https://www.raspbian.org/&quot;&gt;Raspbian&lt;/a&gt;. There are tons of tutorials for that online on the &lt;a rel=&quot;external&quot; href=&quot;https://www.raspbian.org/&quot;&gt;Raspbian&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://www.raspberrypi.org/&quot;&gt;Raspberry&lt;/a&gt; websites that teach it extensively. If you&#39;re using Windows on your laptop/desktop to SSH to the Raspberry Pi, you can use &lt;a rel=&quot;external&quot; href=&quot;http://www.putty.org/&quot;&gt;Putty&lt;/a&gt; as an SSH client.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you&#39;re in the terminal of your Raspberry Pi, you&#39;re ready to go!&lt;/p&gt;
&lt;h3 id=&quot;how-control-is-done-using-raspberry-pi&quot;&gt;How control is done using Raspberry Pi:&lt;/h3&gt;
&lt;p&gt;If you already know how to control Raspberry Pi 2 GPIO pins, you can skip this section.
On Raspberry Pi 2, there is a set of 40 pins that contain 26 pins that are called GPIO (General Purpose Input/Output) pins. GPIO pins can be controlled from the operating system of Raspberry Pi. I use &lt;a rel=&quot;external&quot; href=&quot;https://www.raspbian.org/&quot;&gt;Raspbian&lt;/a&gt; as an operating system of my Raspberry Pi 2 and the Python scripting language. In Raspbian, python is pre-equipped with what&#39;s necessary to start controlling GPIO pins very easily.
Why Python? Because it&#39;s super-easy and is very popular (it took me a few days to become very familiar with everything in that language... yes, it&#39;s that simple). Feel free to use anything else you find convenient for you. However, I provide here only Python scripts.
The following is a map of these pins:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/Raspberry-Pi-GPIO-Layout-Worksheet-page-001.jpg&quot; alt=&quot;Raspberry-Pi-GPIO-Layout-Worksheet-page-001&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/Raspberry-Pi-GPIO-Layout-Worksheet-page-001.b37066567b20c1ac.jpg 400w, https://blog.afach.de/processed_images/Raspberry-Pi-GPIO-Layout-Worksheet-page-001.2f6b544bbf3b6c0b.jpg 800w, https://blog.afach.de/processed_images/Raspberry-Pi-GPIO-Layout-Worksheet-page-001.536cec437e9467e8.jpg 1200w, https://blog.afach.de/processed_images/Raspberry-Pi-GPIO-Layout-Worksheet-page-001.5e8cbe523452087c.jpg 1600w, /media/Raspberry-Pi-GPIO-Layout-Worksheet-page-001.jpg 2479w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt; 
And the following is a video where I used them to control my 4-channel Relay Module:&lt;/p&gt;
&lt;iframe src=&quot;https://www.youtube.com/embed/3WGfOPYNAo0&quot; width=&quot;420&quot; height=&quot;315&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;allowfullscreen&quot;&gt;&lt;/iframe&gt;
&lt;p&gt;And following is the Python script I used to do that. Lines that start with a sharp (#) are comments:
Note 1: Be aware that indentation matters in Python for each line (that&#39;s how you identify scopes in Python). If you get an indent error when you run the script, that only means that the indentation of your script is not consistent. Read a little bit about indentation in Python if my wording for the issue isn&#39;t clear.
Note 2: You MUST run this as super-user.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;#!/usr/bin/python3&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 style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; RPi.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; GPIO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; time&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 style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.setmode(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;BCM&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 style=&quot;color: #6A737D;&quot;&gt;#The following is a function that inverts the current pin value and saves the new state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; switchPortState&lt;/span&gt;&lt;span&gt;(portMapElement):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    GPIO&lt;/span&gt;&lt;span&gt;.output(portMapElement[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;not&lt;/span&gt;&lt;span&gt; portMapElement[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&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;    pe&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; [portMapElement[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;not&lt;/span&gt;&lt;span&gt; portMapElement[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&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 style=&quot;color: #D73A49;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; pe&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 style=&quot;color: #6A737D;&quot;&gt;#There&amp;#39;s no easy way to know the current binary state of a pin (on/off, or 1/0, or True/False), so I use this structure, which is a dictionary array that goes from up to the number of channels one wants to control (I used GPIO channels 2,3,5,6). The first element of each element is the GPIO port number, and the second element is the assumed initial condition. The latter will invert in each step as in the video&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;portMap&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&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;portMap[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;portMap[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;portMap[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;5&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;portMap[&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&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 style=&quot;color: #D73A49;&quot;&gt;for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; range&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;len&lt;/span&gt;&lt;span&gt;(portMap)):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    GPIO&lt;/span&gt;&lt;span&gt;.setup(portMap[i][&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; GPIO&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;OUT&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 style=&quot;color: #D73A49;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; True&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; range&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;len&lt;/span&gt;&lt;span&gt;(portMap)):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    portMap[i]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; switchPortState(portMap[i])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    time.sleep(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you access your Raspberry Pi using SSH, then you can use &quot;nano&quot; as an easy text editor to paste this script. Say if you wanna call the script file &quot;script.py&quot;, then:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;nano&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; script.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will open a text editor where you can paste this script. After you&#39;re done, press Ctrl+X to exit and choose to save your script. Then make this script executable (linux thing), using:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +x script.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then run the script using&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; ./script.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will start the script and the leds will flash every half a second.
Again, we&#39;re using &quot;sudo&quot; because we can only control Raspberry Pi&#39;s GPIO pins as super user. There are ways to avoid putting your password each time you wanna run this, which will be explained later.&lt;/p&gt;
&lt;h3 id=&quot;get-a-grasp-on-the-concept-of-turning-the-computer-on-off&quot;&gt;Get a grasp on the concept of turning the computer on/off:&lt;/h3&gt;
&lt;p&gt;There are two ways to turn your computer on/off electronically without using the switch and without depending on the bios (LAN wake-up, etc...):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If you&#39;re lucky, the power button&#39;s wires will be exposed and you can immediately make a new connection branch in the middle and lead it outside the computer. Shorting the wires is equivalent to pressing the power button.&lt;/li&gt;
&lt;li&gt;Use the power supply&#39;s motherboard green wire. Shorting this wire to ground (to any black wire) will jump the computer and start it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The following is a random picture for a computer power supply. A clip is used to short green with ground.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/ATX-Power-Supply-Connector.jpg&quot; alt=&quot;ATX-Power-Supply-Connector&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/ATX-Power-Supply-Connector.fe97d5f9b1e52420.jpg 400w, https://blog.afach.de/processed_images/ATX-Power-Supply-Connector.48547944a3899f3f.jpg 800w, /media/ATX-Power-Supply-Connector.jpg 1024w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
I used the first way of the two ways I mentioned. Here&#39;s a video showing how it looks like:
&lt;iframe src=&quot;https://www.youtube.com/embed/I1uXdHmkf18&quot; width=&quot;420&quot; height=&quot;315&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;allowfullscreen&quot;&gt;&lt;/iframe&gt;
&lt;p&gt;So shorting these two wires that come from the power button for some time (half a second) is what I did and that works as being equivalent to pressing the power button. After you manage how to connect these, then you can go to the next step.&lt;/p&gt;
&lt;h3 id=&quot;connecting-the-power-wires-to-the-relay-module&quot;&gt;Connecting the power-wires to the Relay Module:&lt;/h3&gt;
&lt;p&gt;After learning how to control the Relay Module, and learning how to take a branch from the computer case that if you would short the computer starts, the remaining part is to connect the power-wires, that you got from your computer power button or green+black power supply cords, and connect them to the Relay Module. The following video shows the concept and the result.&lt;/p&gt;
&lt;iframe src=&quot;https://www.youtube.com/embed/WZ2nedUKZW0&quot; width=&quot;420&quot; height=&quot;315&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;allowfullscreen&quot;&gt;&lt;/iframe&gt;
&lt;p&gt;Now you have the two terminals that if you short together the computer starts, let&#39;s get into a little more details.
&lt;strong&gt;Important:&lt;/strong&gt; One important thing to keep in mind when doing the wire connection to the Relay Module, is that we need to connect them in a way that does not trigger the power switch if the Raspberry Pi is restarted. Therefore, choose the terminal connections to be disconnected by default, as the following picture shows:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/Relay5V-4Ch-defaults.jpg&quot; alt=&quot;Relay5V-4Ch-defaults&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/Relay5V-4Ch-defaults.6ff2332698f2c6fc.jpg 400w, /media/Relay5V-4Ch-defaults.jpg 700w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt; 
Connect your power-wires two terminals to any of the marked two in the picture. The way the Relay Module works is that when it&#39;s turned off, it switches whether the middle terminal is connected to left or right. By default it&#39;s connected to right, and that&#39;s what we can see in the small schematic under the terminals.
After doing the connections properly, now you can use the following script turn your computer on:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;#!/usr/bin/python3&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 style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; RPi.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; GPIO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span&gt; argparse&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 style=&quot;color: #6A737D;&quot;&gt;#initialize GPIO pins&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.setmode(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;BCM&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 style=&quot;color: #6A737D;&quot;&gt;#use command line arguments parser to decide whether switching should be long or short&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;#The default port I use here is 6. You can change it to whatever you&amp;#39;re using to control your computer.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;parser&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; argparse.ArgumentParser()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;-port&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;--portnumber&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; dest&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;port&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;6&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; help&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Port number on GPIO of Raspberry Pi&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;#This option can be either long or short. Short is for normal computer turning on and off, and long is for if the computer froze.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;-len&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;--len&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; dest&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;length&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;short&amp;quot;&lt;/span&gt;&lt;span&gt; ,&lt;/span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; help&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Length of the switching, long or short&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;args&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; parser.parse_args()&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 style=&quot;color: #6A737D;&quot;&gt;#initialize the port that you&amp;#39;ll use&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.setup(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;/span&gt;&lt;span&gt;(args.port),&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; GPIO&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;OUT&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 style=&quot;color: #6A737D;&quot;&gt;#switch relay state, wait some time (long or short) then switch it back. This acts like pressing the switch button.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.output(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;/span&gt;&lt;span&gt;(args.port),&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;if&lt;/span&gt;&lt;span&gt; args.length&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;long&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;    time.sleep(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;elif&lt;/span&gt;&lt;span&gt; args.length&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;short&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;    time.sleep(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Error: parameter -len can be only long or short&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;GPIO&lt;/span&gt;&lt;span&gt;.output(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;/span&gt;&lt;span&gt;(args.port),&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;True&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Save this script as, say, &quot;switch.py&quot;, and make it executable as we did before:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +x switch.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now you can test running this script, and this is supposed to start your computer!&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; ./switch.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;running-the-program-from-a-web-browser&quot;&gt;Running the program from a web browser&lt;/h3&gt;
&lt;p&gt;You could be already satisfied by accessing switching the computer remotely using ssh, but I made the process a little bit fancier using a php webpage.&lt;/p&gt;
&lt;h5 id=&quot;caveat&quot;&gt;CAVEAT:&lt;/h5&gt;
&lt;p&gt;Here, I explain how you could get the job done to have a webpage that turns on/off your computer. I don&#39;t focus on security. Be careful not to make your home/work network&#39;s components accessible to the public. Please consult some expert to verify that what you&#39;re doing is acceptable and does not create a security threat for others&#39; data.&lt;/p&gt;
&lt;h6 id=&quot;using-superuser-s-sudo-without-having-to-put-the-password-everytime&quot;&gt;Using superuser&#39;s sudo without having to put the password everytime&lt;/h6&gt;
&lt;p&gt;In order to access this from the web, you have to change pins states without having to enter the password.
To do this, run the command&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; visudo&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will open a text editor. If your username for Raspberry Pi&#39;s linux is myuser, then add the following lines there in that file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;www-data ALL=(myuser) NOPASSWD: ALL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;myuser ALL=(ALL) NOPASSWD: ALL&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will allow the Apache user to execute the sudo command as you, and you have absolute super-user power. Now notice that this is not the best solution from a security point of view, but it just works. The best solution is to allow the user www-data to run a specific command as root. Just replace the last &quot;ALL&quot; of www-data with a comma separated list of the commands you wanna allow www-data to run, and replace &quot;myuser&quot; between the parenthesis with &quot;root&quot;. I recommend you do that after having succeeded, to minimize the possible mistakes you could do. This is a legitimate development technique, we start with something less perfect, test it, then perfect it one piece at a time.&lt;/p&gt;
&lt;h6 id=&quot;installing-apache-web-server&quot;&gt;Installing Apache web-server&lt;/h6&gt;
&lt;p&gt;First, install the web-server on your Raspberry Pi. Do this by running this set of commands in your terminal:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; apt-get install apache2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; apt-get install php5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; apt-get install libapache2-mod-php5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; a2enmod php5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; a2enmod alias&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; service apache2 restart&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I hope I haven&#39;t forgotten any more necessary components, but there are, too, many tutorials out there and forums discussing how to start an apache webserver.
If the apache installation is a success, then you could go to your web-browser and see whether it&#39;s working. First, get the hostname of your Raspberry Pi by running this command in Raspberry Pi&#39;s terminal&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hostname&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s say your hostname is &quot;myhostname&quot;. Now go to your browser, and enter this address:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http://myhostname/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If this gives you a webpage, then the web-server is working fine and you can proceed. Otherwise, if the browser gives an error, you have to debug your web-server and get it working. Please consult some tutorial online to help you run the apache server.&lt;/p&gt;
&lt;h6 id=&quot;creating-the-webpage&quot;&gt;Creating the webpage:&lt;/h6&gt;
&lt;p&gt;The default directory where the main webpage is stored in apache is either &quot;/var/www/&quot; or &quot;/var/www/html/&quot;. Check where the index.html that you saw is, and place the new php file there. Say that php file has the name &quot;control.php&quot;, and say the default directory is &quot;/var/www/&quot;. Then, go to that directory using&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; /var/www/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now create the new php page using the command&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; nano control.php&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And use the script&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;php&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;!&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;DOCTYPE html&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;html&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;head&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Control page&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;head&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;form action&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;#&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; method&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;post&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;center&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;select name&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;switchlen&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;option value&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;short&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Short&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;option&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;option value&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;long&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Long&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;option&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;input type&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Switch server&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;center&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;form&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&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 style=&quot;color: #D73A49;&quot;&gt;        &amp;lt;?&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;php&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;isset&lt;/span&gt;&lt;span&gt;($_POST[&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;submit&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 style=&quot;color: #D73A49;&quot;&gt;            if&lt;/span&gt;&lt;span&gt;($_POST[&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;switchlen&amp;#39;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;long&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 style=&quot;color: #005CC5;&quot;&gt;                echo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;This is long&amp;quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                echo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&amp;lt;br&amp;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;                $command&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;sudo -u myuser sudo /usr/bin/python3 /home/myuser/switch.py -len long &amp;gt; debug.log 2&amp;gt;&amp;amp;1&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 style=&quot;color: #D73A49;&quot;&gt;            else if&lt;/span&gt;&lt;span&gt;($_POST[&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;switchlen&amp;#39;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;short&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 style=&quot;color: #005CC5;&quot;&gt;                echo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;This is short&amp;quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                echo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&amp;lt;br&amp;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;                $command&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;sudo -u myuser sudo /usr/bin/python3 /home/myuser/switch.py -len short &amp;gt; debug.log 2&amp;gt;&amp;amp;1&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;            $output&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; shell_exec&lt;/span&gt;&lt;span&gt;($command);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            echo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Script return: &amp;quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            var_dump&lt;/span&gt;&lt;span&gt;($output);&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 style=&quot;color: #D73A49;&quot;&gt;        ?&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;html&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Don&#39;t forget to change the path of the script to the correct path of your script &quot;switch.py&quot;, and change &quot;myuser&quot; to the username you&#39;re using in your Raspberry Pi. After you&#39;re done, press Ctrl+X to save and exit.
To use this page and have it successfully run the script, you have to do one more thing, which is making apache&#39;s user own this file. The username for the apache web-server is called &quot;www-data&quot;, so assuming you called the file &quot;control.php&quot;, you have to run this command:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; chown www-data:www-data control.php&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After you run this command, you should use &quot;sudo nano&quot; to edit this file instead of only nano, since your linux user doesn&#39;t own the file anymore.
Also, don&#39;t save the file in any place other than the original folder of apache (like /var/www), at least not before you make sure it works. Adding new folders that apache recognizes is something that requires additional steps that I don&#39;t discuss here. Please consult an apache tutorial for that.
To test the new php page, go to the link:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;http://myhostname/control.php&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the website doesn&#39;t work, check the the file &quot;debug.log&quot; in the same path of control.php. It will tell you what was wrong in the script.&lt;/p&gt;
&lt;h3 id=&quot;controlling-your-computer-from-outside-your-home&quot;&gt;Controlling your computer from outside your home&lt;/h3&gt;
&lt;p&gt;If you&#39;re in a home network, then you can only access that webpage from within the network. If you would like to access it from outside the network, you have to have VPN access to your home network. Consider achieving this using &lt;a rel=&quot;external&quot; href=&quot;https://openvpn.net/&quot;&gt;OpenVPN&lt;/a&gt;. That&#39;s how I do it. I may write an article about it some time in the future.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I hope this article has given you an idea on how to control your appliances using Raspberry Pi. We have shown how to turn a computer on/off remotely.
I do this for fun, but also more professional tasks can be achieved using similar scripts, such as controlling scientific experiments.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Designing high magnetic field coils efficiently</title>
        <published>2015-03-21T16:56:27Z</published>
        <updated>2015-03-21T16:56:27Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/magnetic-coils/"/>
        <id>https://blog.afach.de/magnetic-coils/</id>
        <content type="html" xml:base="https://blog.afach.de/magnetic-coils/">&lt;p&gt;It sounds simple, I agree. But the problem is like designing a car. You could simply put some wheels together with simple set of chains and create a &quot;car&quot; that moves in your front lawn. But then if you want to design a professional car, you&#39;ll spend much more time and professionalism to do it. And if you wanna build a sports car, you&#39;ll have to fine tune every single parameter of your components to push the edge of what you can achieve.
This story doesn&#39;t apply only to coils, but everything you design (actually I learned it from a book called &quot;Professional C++&quot; in a chapter discussing efficiency of algorithm writing, it&#39;s not far from this case).
Concerning coils, if you just want to generate a few micro Tesla with no concerns about the homogeneity for the coil you&#39;re designing, then it doesn&#39;t matter how you do it. You could just wind a bunch of wires on your hand and your done. Today I&#39;m not talking about such a thing. Today I&#39;m gonna talk about coils that you want to use to generate relatively huge fields, with reasonable homogeneity.
For such specifications, if you&#39;re careless about the details, you may end up wasting lots of time for experimental experience and money for cooling and power supplies. If you plan it carefully, you&#39;ll save lots of money.&lt;/p&gt;
&lt;h3 id=&quot;parameters-of-the-system&quot;&gt;Parameters of the system&lt;/h3&gt;
&lt;p&gt;Any coils you design to generate magnetic fields will have a set of parameters that you have to tune. From those parameters I mention:
• Dimensions and geometry of the coils
• Magnetic field to be generated
• Thickness of the wires to be used&lt;/p&gt;
&lt;h3 id=&quot;where-to-start&quot;&gt;Where to start&lt;/h3&gt;
&lt;p&gt;The first thing one should determine for a coil system is the geometry of the coils. This is because the purpose of the coils is not just to generate a field, period. But it&#39;s to generate a field over some region that is well defined for a specific experiment. One should know in advance what sample will be exposed to the magnetic field. If you don&#39;t know that, you could spend days working on your coils, and eventually find out that your coils don&#39;t fit your sample, or that your coils are so huge that you&#39;re wasting so much power to generate fields you don&#39;t need (math will show how important the size of your coils vs power is).
Therefore, start by defining the geometry of your coils. Once you know some characteristic radius or length of your coils, you have one step done. One more thing to pre-define, is how much wiring volume you want to use.
Optimum coil thickness
Any person starting with designing coils will have this question in mind: What&#39;s the optimum wire thickness that should be used to have the optimum magnetic field. Guess what? If you know how much wire-volume you will use, then it doesn&#39;t matter at all. Let&#39;s do the math to verify this.
Suppose you have a slot to wind your wires that have a diameter $d$, and the slot has width $w$ and height $h$, as the figure shows. How can we optimally fill your wires in it? The optimal way is what the figure shows; it&#39;s called “hexagonal packing”. This is in case we ignore third dimension&#39;s problems, such as diagonal wires in the slots. This is a good approximation if the wires are wound carefully.&lt;/p&gt;
&lt;figure class=&quot;wp-caption&quot; style=&quot;width:300px&quot;&gt;&lt;a href=&quot;/media/Circle_packing.png&quot;&gt;&lt;img src=&quot;/media/Circle_packing.png&quot; alt=&quot;Cross section of packed wires (or packed cylinders)&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/Circle_packing.58f94bcb778ef974.png 400w, https://blog.afach.de/processed_images/Circle_packing.446f8d61beca3505.png 800w, /media/Circle_packing.png 906w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot; width=&quot;300&quot;&gt;&lt;/a&gt;&lt;figcaption&gt;Cross section of packed wires (or packed cylinders)&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Notice that it&#39;s very important to have a high number of circles per direction. This is to make sure that the flow of the current will be homogeneous in the volume. Imagine the opposite case, if you have one circle with diameter $d=h$. This will modify the magnetic field you expect from the wire as all the calculations of the magnetic field from a wire assume an infinitesimally thin wire.
With that said, let&#39;s calculate the magnetic field from such a configuration. Any magnetic field generated from a current has the following form using the Biot-Savart law or Ampere&#39;s law:
$$B=U\left(r\right)\cdot n\cdot I,$$
where $U\left(r\right)$ is a coefficient that depends on the dimensions of the coils with radius $r$, and $I$ is the electrical current, and $n$ is the number of total turns of the coil. We assumed that we know the geometry of your coil, meaning that $U\left(r\right)$ is fixed before starting this calculation. Let&#39;s see how the wire diameter will modify your magnetic field.
From the definition of resistivity of a wire, we have
$$R=\rho\frac{\ell}{A}=\rho\frac{4\ell}{\pi d^{2}},$$
where R
is the resistance of the wire, $\ell$ is its length, $A=\pi\left(\frac{d} {2}\right)^{2}$ is its cross sectional area and $\rho$ is its resistivity. On the other hand, the power to be exerted on the coil is given by the formula
$$P=I^{2}R\quad\rightrightarrows \quad I=\sqrt{\frac{P}{R}},$$
and from the resistivity formula
$$I=\sqrt{\frac{P}{R}}=\frac{d}{2}\sqrt{\frac{\pi P}{\rho\ell}},$$
and we can put this in a form proportional to the magnetic field
$$B\propto n\cdot I=n\frac{d}{2}\sqrt{\frac{\pi P}{\rho\ell}}$$
There&#39;s still one more question remaining to settle this; what&#39;s the length of the wire required? The length of the wire depends on the diameter of the wire that will be used, since a thicker wire will occupy the area of the slot faster. Whether you wind your wire on a circle or a square, the length of the wire used will depend linearly on the side length or diameter of that geometry. If we call that $r$, then the length of the wire going to be $\ell=n\cdot k\cdot r$, where $k$ is $2\pi$ for a coil on the shape of a circle, and $4$ for a square coil, and $n$ is the number of turns. The formula of current then becomes:
$$n\cdot I=n\frac{d}{2}\sqrt{\frac{\pi P}{\rho\ell}}=n\frac{d}{2}\sqrt{\frac{\pi P}{\rho n\cdot k\cdot r}}=\frac{d}{2}\sqrt{\frac{\pi nP}{\rho\cdot k\cdot r}}.$$
Finally, the number of turns can be seen as the number of circles stacked in a hexagonal packing (as shown in the figure of the hexagonal packing). The number of circles (or cylindrical wires) with diameter $d$ packed in a slot of width $w$ and height $h$ can be approximated assuming a high number of circles (cylinders) per direction
$$n=\frac{w}{d}\cdot\frac{h}{d\cos30^{\circ}}=\frac{2}{\sqrt{3}}\frac{wh}{d^{2}},$$
where the factor $\cos30^{\circ}$ is simply due to the space saved with the hexagonal packing. In fact it doesn&#39;t matter what packing you choose, since all packings will depend inversely on $d^{2}$, which is the important part. With this, the current formula becomes:
$$n\cdot I=\frac{d}{2}\sqrt{\frac{\pi nP}{\rho\cdot k\cdot r}}=\frac{1}{2}\sqrt{\frac{2\pi}{\sqrt{3}}}\sqrt{\frac{ whP}{\rho k r}}.$$
This result shows that &lt;strong&gt;the field doesn&#39;t depend on the wire that we choose or the current we use. The magnetic field depends only on the power if the geometry of the coil is fixed.&lt;/strong&gt;
&lt;strong&gt;One more important&lt;/strong&gt; result here is that &lt;strong&gt;the power required is linearly proportional to&lt;/strong&gt; $r$, which means that &lt;strong&gt;if your coils would have half the radius, half the power will be required&lt;/strong&gt;. Thus it&#39;s considerably important to make the coils as small as possible.&lt;/p&gt;
&lt;h3 id=&quot;how-to-choose-the-wire-diameter&quot;&gt;How to choose the wire diameter?&lt;/h3&gt;
&lt;p&gt;Having the magnetic field not depend on the wire thickness doesn&#39;t mean we can choose it arbitrarily. In fact, the wire determines a very important characteristic, which is the voltage and amperage that we will need to generate. It doesn&#39;t make sense to make a coil that will require 150 amperes and 0.1 volts, and that&#39;s what the wire diameter determines for you. The thinner the wire is, the more resistance the coil will have, and thus more voltage will be required.
Thus, to fine-tune the production of your coil, decide the diameter of the wire after choosing the power supply that has the appropriate voltage and amperage.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;It&#39;s intuitive to think that the amount of current will tell how much field you&#39;ll get, this is from simple, classical electromagnetism. However, in reality, it doesn&#39;t really depend on the current. This is because the choice of the current depends on other parameters that are prioritized, such as the geometry of the coils. If the geometry of the coils is fixed, then it doesn&#39;t matter how much current you put, but what matters is practically how much power one will put in.
This result is important for high field generators, since higher fields will entail higher power requirement, leading to heating problems. If your coils generate more heat that your system can dissipate, then it makes sense to think beforehand whether to whether the geometry should be made smaller or different, since smaller geometry means less power requirement.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>What is a neutron Electric Dipole Moment (EDM)? How is it realized?</title>
        <published>2014-11-18T20:17:49Z</published>
        <updated>2014-11-18T20:17:49Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/neutron-edm/"/>
        <id>https://blog.afach.de/neutron-edm/</id>
        <content type="html" xml:base="https://blog.afach.de/neutron-edm/">&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;It&#39;s funny how some times scientists could have conflicts over equivalent concepts without realizing the equivalence in what they&#39;re saying. Recently, a conflict happened between a PhD student, in his PhD defense, and an examining professor. The conflict&#39;s topic was: How is CP violation realized in permanent EDMs?
The funny thing is that both their pictures were correct. It&#39;s just that one picture has an assumption on how an EDM should be quantum mechanically defined, and the other measures the existence of a neutron EDM through a CP violating energy.&lt;/p&gt;
&lt;h3 id=&quot;introduction-to-permanent-edm-and-cp-violation&quot;&gt;Introduction to permanent EDM and CP violation&lt;/h3&gt;
&lt;p&gt;You could skip this part if you&#39;re familar with CP violation and EDMs.
The concept of CP violation is a modern topic. It&#39;s been out there for about 50 years, since the Nobel Prize discovery of CP violating Kaon dacays (Christenson et al. 1964). It also gained intresest because Sakharov (Sakharov 1991) postulated that the existence of matter in the universe instead of annihilating with anti-matter can be explained with interactions that should be CP violating (not only that, but this is one important condition).&lt;/p&gt;
&lt;h5 id=&quot;what-is-cp-violation&quot;&gt;What is CP violation?&lt;/h5&gt;
&lt;p&gt;CP is a combination of two quantum mechanical operators, $C$ and $P$. The $C$ operator is called the &lt;em&gt;Charge Conjugation Operator&lt;/em&gt;; it simply converts each particle to an anti-particle. The $P$ operator is called the &lt;em&gt;Parity Operator&lt;/em&gt;; it flips the coordinate system as a mirror would do, so that every right is left and every left is right. In other words, one could mathematically say that a coordinate $\vec{r}$ becomes $-\vec{r}$ after a parity operation. One more important operator that comes into play in the same game is $T$, the &lt;em&gt;Time Reversal Operator&lt;/em&gt;. It simply reverses time progression. From this, CP violation is defined to be that systems in our world will have different physical laws when applying the $CP$ operators.
In 2002, an article was published (Greenberg 2002) that states that violating CPT symmetry implies breaking &lt;em&gt;Lorentz Invariance&lt;/em&gt;, which is like the most fundamental symmetry we understand in our universe. The Lorentz symmetry simply means that Einsteins special relativity holds. This means that if CPT is violated, then relativity is broken, thus we would be in big trouble in our path in understanding the universe. Since all experiments agree with Lorentz invarinace, it is widely accepted that CPT symmetry is a fundamental symmetry in the universe.
The implication of CPT symmetry is that if CP is violated, then T is violated as well, to compensate for the former&#39;s violation and result in CPT symmetry.&lt;/p&gt;
&lt;h4 id=&quot;what-is-an-edm&quot;&gt;What is an EDM?&lt;/h4&gt;
&lt;p&gt;An Electric Dipole Moment (EDM) is defined in classical electromagnetism in its simplest form to be two charges with opposite signs and a fixed distance between them. Such a system is interesting in classical physics because applying an electric field on it will create a torque that gets it to rotate. Besides that it&#39;s a part of an expansion called the Multipole Expansion that simplifies the treatment of many complex systems with complicated charge distribution.
For a group of particles, an EDM can be defined as:
$$\vec{d}=\sum_{i} \vec{r}_i q_i,$$
where $r_i$ is the position of particle number $i$, and $q_i$ is the charge of particle number $i$.&lt;/p&gt;
&lt;h3 id=&quot;what-is-the-reason-for-the-conflict&quot;&gt;What is the reason for the conflict?&lt;/h3&gt;
&lt;p&gt;It&#39;s well known that a neutron possesses a magnetic moment. The magnetic moment is associated with the spin of the neutron. In quantum mechanics, measurements can be done along one axis only. Measurements using multiple axes can have many problems, in which the quantum mechanical observables (quantum numbers) become time dependant, or in other words we could say: Measurements on multiple axes would lead to &lt;strong&gt;not&lt;/strong&gt; having &lt;em&gt;Good Quantum Numbers&lt;/em&gt;. A good quantum number is a mark of a good measurement and is reproducible. Measurements that change over time (except for approximations and eigenvalues that we understand) are useless, because they&#39;re not reproducible. Some people like to pretend to be smarter than nature, and try to find ways to trick quantum measurements on multiple axes, but such people are either mistaken or haven&#39;t yet realized that they&#39;re doing a mistake. Humanity spent decades trying to trick the quantum world, but failed, and a good experimental example of this is in the following paragraph.
To illustrate good measurements, I&#39;ll refer to the well-known &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Double-slit_experiment&quot;&gt;Double Slit Experiment&lt;/a&gt;. See the picture for an illustration of the experiment. In this experiment, there&#39;s a light source, which shines light on two open slits. You would think that you&#39;ll have two spots on the screen, but this doesn&#39;t happen due to quantum mechanical intereference effects.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/DoubleSlitExperiment.png&quot; alt=&quot;DoubleSlitExperiment&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/DoubleSlitExperiment.896127deb2dfdb45.png 400w, https://blog.afach.de/processed_images/DoubleSlitExperiment.1328fe734a4696de.png 800w, /media/DoubleSlitExperiment.png 879w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
&lt;p&gt;In the double slit experiment, if only one slit is open, then the position of photons can be determined (by tolerating the spread due to diffraction, which is a good approximation). Thus we can say that the position of a photon in that case is a good measurement and is well defined along the path of the photon, from the light source to the detection screen.
If we open both slits, &lt;em&gt;phase interference&lt;/em&gt; will show up. This leads to having an interference pattern (multiple maxima and minima in intensity), in which the position of the photon is not defined and the precision is worse than before! You can never, ever define a position of a photon after the slits with the same precision that you had in one slit. In this case, a measurement of a position is considered a bad measurement. There has been many tries to trick the system, but it never works. This is due to the wave-nature of the system that puts the phases of the constituent waves into account. This can be mathematically realized by the so called &lt;em&gt;Uncertainty Principle&lt;/em&gt;. A position ($x$) and velocity (or momentum, $p_x$) of a particle can never be determined with infinite precision. This can be mathematically written with the commutator
$$\left[ x,p_x \right]=x p_x - p_x x = i\hbar$$
If the commutator would have been zero, then a simultaneous measurement with infinite precision (up to experimental constraints) is possible. A similar commutator exists between angular momentum $\vec{L}$ that is measured on different axes.
$$\left[ L_x,L_y \right]=i\hbar L_z$$
Therefore, the angular momentum can &lt;strong&gt;never&lt;/strong&gt; be measured on multiple axes together. Actually, it&#39;s not only the angular momentum. &lt;strong&gt;Any&lt;/strong&gt; vector quantity cannot be measured on multiple axes. This follows from a famous mathematical theorem called the &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Wigner%E2%80%93Eckart_theorem&quot;&gt;&lt;em&gt;Wigner-Eckart&lt;/em&gt; theorem&lt;/a&gt;. This theorem shows that all vectors in quantum mechanics are the same up to a scaling factor. That&#39;s why, tricking the system to measure on multiple axes is just pointless! Hence, in every quantum measurement, a single axis is defined to measure all vector quantities. It&#39;s called the &lt;em&gt;quantization axis&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&quot;quantization-axis-of-a-neutron-is-the-problem&quot;&gt;Quantization axis of a neutron... is the problem!&lt;/h3&gt;
&lt;p&gt;Trouble starts now! We already know that there&#39;s an axis for the neutron that is defined with its spin (or its magnetic moment). What if an EDM exists? An EDM is a vector quantity! Can it have another axis?
The conflict is exactly here. There are two schools of thought for this:
1- An EDM can physically exist in any direction, but we can only measure it along the spin, the well known quantization axis. If a measurement is done along a different axis, it cannot be distinguished.
2- An EDM must only exist along the spin, without caring what the nature of that EDM is, whether it&#39;s a separation of charges or not.
This is exactly the source of conflict between the professor and the student. In fact, there&#39;s no difference between those pictures. The reason of this is that no matter how you look at it, what matters is what we can measure, which is the same for both cases.&lt;/p&gt;
&lt;h5 id=&quot;first-picture&quot;&gt;First picture:&lt;/h5&gt;
&lt;p&gt;For the first picture, we say that if a neutron EDM is measured, it has to be measured along the spin. If the neutron wave-function is $\left| J,m_J \right&amp;gt;$, where $J$ is the angular momentum eigenvalue and $m_J$ is the magnetic quantum number, then
$$\left&amp;lt;J, m_J \left| \hat{\vec{d}}_\mathrm{n} \right| J, m_J \right&amp;gt;= k\left&amp;lt;J, m_J \left| \hat{\vec{J}} \right| J, m_J \right&amp;gt;$$
or in short
$$\left&amp;lt; \hat{\vec{d}}_\mathrm{n}  \right&amp;gt;= k\left&amp;lt; \hat{\vec{J}}  \right&amp;gt;,\quad (*)$$
where $\hat{\vec{d}}_\mathrm{n}$ is nothing but an quantum mechanical operator that is constructed from the classical dipole moment defined above. This operator can be written as from before
$$\hat{\vec{d}}_\mathrm{n} = \sum_{i} \hat{\vec{r}}_i q_i, $$
where $\hat{\vec{r}}_i$ is the position of constituent number $i$. Let&#39;s test $\hat P$ (parity) operator on this system
$$\left&amp;lt;\hat{P}^\dagger \hat{\vec{d}}_\mathrm{n}  \hat{P} \right&amp;gt;=-\left&amp;lt; \hat{\vec{d}}_\mathrm{n} \right&amp;gt;$$
$$\left&amp;lt;\hat{P}^\dagger \hat{\vec{J}} \hat{P} \right&amp;gt;=\left&amp;lt; \hat{J} \right&amp;gt;$$
Parity operator changes the sign of the EDM because it changes $r$ to $-r$. The spin doesn&#39;t flip under parity because it&#39;s an &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Pseudovector&quot;&gt;axial vector&lt;/a&gt; (opposite of polar vector); thus it&#39;s a product of two other vectors. Notice that Eq. $(*)$ doesn&#39;t hold anymore when parity is applied and $k \rightarrow -k$. Therefore, a permanent EDM violates parity!
Let&#39;s test time reversal, $\hat{T}$
$$\left&amp;lt;\hat{T}^\dagger \hat{\vec{d}}_\mathrm{n}  \hat{T} \right&amp;gt;=\left&amp;lt; \hat{\vec{d}}_\mathrm{n} \right&amp;gt;$$
$$\left&amp;lt;\hat{T}^\dagger \hat{\vec{J}}  \hat{T} \right&amp;gt;=-\left&amp;lt; \hat{J} \right&amp;gt;$$
Time reversal doesn&#39;t affect the EDM because such an EDM doesn&#39;t depend on time. Spin or magnetic moments, however, reverse sign by time reversal, since magnetic moments are generated by the motion of charges, and a backwards motion will reverse the magnetic moment. Thus, a permanent EDM violates time reversal symmetry.
This means that CP is violated when a permanent EDM exists.&lt;/p&gt;
&lt;h5 id=&quot;second-picture&quot;&gt;Second picture:&lt;/h5&gt;
&lt;p&gt;In the second picture, it&#39;s claimed that we don&#39;t know what&#39;s going on inside, but there&#39;s strictly an EDM that is proportional to spin. The game can be played in a different way. We define a Hamiltonian of the system
$$\hat{H}=-\hat{\vec{\mu}}_\mathrm{n}\cdot \vec{B}-\hat{\vec{d}}_\mathrm{n}\cdot \vec{E}.$$
By calculating the expectation value of this Hamiltonian and relating it to the numerical magnetic moment of the neutron, $\mu_\mathrm{n}$, and a hypothetical numerical value of the EDM, $d_\mathrm{n}$, and by considering that the magnetic moment and the EDM are along a single axis, the precession energy of the neutron becomes
$$E=\hbar \omega_\mathrm{L}=2\mu_\mathrm{n} B+ 2 d_\mathrm{n} E .$$
The factor 2 comes from the convention of $\mu_n$ being meaured from the eigen-energy of the system with no electric and magnetic fields up to the maximum magnetic moment (which is 1/2), leading to energy spacing of twice that for a spin-1/2 particle. And $\omega_\mathrm{L}$ is the Larmor frequency, which is the transition frequency from the two possible eigenstates of the spin.
By applying the parity operator, $\hat P$, to the energy term, we get
$$E=\hbar \omega_\mathrm{L}=2\mu_\mathrm{n} B- 2 d_\mathrm{n} E .$$
This is because $E$ is a polar vector, and $B$ is an axial vector. This shows that the existence of an EDM modifies the energy of the system under parity! This is another way to look at the effect of an EDM on the energy of the system upon applying $P$ operator. The same can be tested for $T$ operator which will flip only the magnetic field, since it&#39;s an axial vector.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;It doesn&#39;t really matter how we look at it. The reason for this is that what matters in the experiment is what we measure! The picture of an intrinsic EDM that consists of separation of charges is violated through $P$ and $T$ operators. The other picture with electric and magnetic fields is violated because the system&#39;s energy is different when appying those operators.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>How to organize scientific literature?</title>
        <published>2014-11-14T11:36:09Z</published>
        <updated>2014-11-14T11:36:09Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/organize-literature/"/>
        <id>https://blog.afach.de/organize-literature/</id>
        <content type="html" xml:base="https://blog.afach.de/organize-literature/">&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;A huge burden for a scientist is the organiation of their literature. I, as a physicist, have over 1000 articles that I used in my work along the history of my career in physics. Besides many, many books that I also would need at any second while working.
How can I organize all those books and all those articles?
How can I reach any piece of information I need whenever I need it without wasting an hour digging in my collection of articles by filename?
This question has been going in my head for a long time, and I started a few months ago to dig for an answer. I was thinking of writing a php program that organizes my literature with copies of literature inside it and with bibtex support, but guess what... a solution with those features is already there! I just found that there are many, many organizations that have written such programs. Examples are &lt;a rel=&quot;external&quot; href=&quot;http://www.mendeley.com/&quot;&gt;Mendeley&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://www.zotero.org/&quot;&gt;Zotero&lt;/a&gt;.
There are more, but I only tested these.&lt;/p&gt;
&lt;h3 id=&quot;both-are-great-but&quot;&gt;Both are great, but...&lt;/h3&gt;
&lt;p&gt;Both programs &lt;a rel=&quot;external&quot; href=&quot;http://www.mendeley.com/&quot;&gt;Mendeley&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://www.zotero.org/&quot;&gt;Zotero&lt;/a&gt; are great when considered locally on your computer! They&#39;re so good that they can take your PDF, read it, index it, and automatically find reference details for you such as Author list, DOI, ISBN, title, and many others and make this data available when you search for something and simply display the article when you click on its information. You can even add a note to some article, attach it to the article and make it reachable when needed through the note. This is extremely helpful when you want to mark a specific piece of information in some literature, making it not necessary to dig in all your literature.
I started with Mendeley, I was happy with it, until I realized that I also need a synchronization mechanism among my computers at home and at work. Mendeley is a commercial software, it offers very limited diskspace (about 2 GB), and stuff are stored on their server, making it a problem for some people due to corporate policy, which prevents them from storing work information on 3rd party servers. Besides, even if that&#39;s allowed, 2 GB is not really good enough. That&#39;s why, I left Mendeley.
Then I found Zotero, which is technically a program derived from Firefox. Zotero creates a local database folder on your computer with local copies of your literature inside it. You can copy the database wherever you want and even move it between computers. Then you can easily choose what directory you want to use to store your data. This is SUPER CONVENIENT! It&#39;s very practical for making backups of everything, and not losing anything on long term use. There&#39;s even an option for synchronization with the web. They offer also little diskspace (about 1 GB), but then if you pay 10$, they give you infinite diskspace, which is convient, and gives you the option to synchronize your literature among multiple computers.&lt;/p&gt;
&lt;h3 id=&quot;what-if-corporate-policy-prevents-me-from-storing-literature-on-a-3rd-party-server&quot;&gt;What if corporate policy prevents me from storing literature on a 3rd party server?&lt;/h3&gt;
&lt;p&gt;Actually it&#39;s not only corporate policy that prevents me from doing that. What prevents me, too, is that I&#39;m not convinced that such a service is worth 10$ per month, especially that I own a linux server, for which I pay 30$ per month with 1 TB diskspace. Does it make sense to pay 30$ for a full-featured linux server and 10$ just for literature? Not at all!&lt;/p&gt;
&lt;h3 id=&quot;proposing-a-solution-to-avoid-3rd-party-cloud-intervention&quot;&gt;Proposing a solution to avoid 3rd party cloud intervention&lt;/h3&gt;
&lt;p&gt;The solution is very simple, and I could implement this solution due to the nice way Zotero stores data. Since Zotero stores data in a single, defined folder, all you have to do is synchronize this folder among the computers you have to use! A method to do this is by using a repository system, like GIT, which I find not convenient, since I manually have to commit, push and pull every change. So the better method I found is a synchronization system driven from my 30$ server, called &lt;a rel=&quot;external&quot; href=&quot;http://www.seafile.com&quot;&gt;Seafile&lt;/a&gt;.
Seafile is an opensource cloud system (similar to Dropbox) that can be run from your own server! It uses client-side encryption and is the safest I know and most recommended, so far. I have been using it for all my work and data, and I find it very convenient. So, all you have to do is synchronize your Zotero data folder among the PCs you want to use.
If you don&#39;t have a server for yourself, simply use some 3rd party cloud, like &lt;a rel=&quot;external&quot; href=&quot;http://www.dropbox.com&quot;&gt;Dropbox&lt;/a&gt;, which will anyway give you more diskspace than the standard Zotero cloud offers. However, you&#39;re, again, limited by diskspace eventually. In case you need more diskspace, I really recommend that you rent (or probably buy for your home) your own linux server. You learn a lot, and you save a lot of money and you can use it for multiple purposes for yourself and your family.
Or... you could use servers from your own institution, which are normally offered at good universities (normally universities offer free diskspace for employees and students which is globally accessible or at least through a VPN service).&lt;/p&gt;
&lt;h3 id=&quot;risks&quot;&gt;Risks?&lt;/h3&gt;
&lt;p&gt;There&#39;s some risk when doing this, but it&#39;s not that bad for a reason. The main risk when using this method is that you could open the same Zotero database from different computers. I&#39;m not sure whether just opening from different computers would create a problem, but I could almost be certain that if you make changes on different computers simultaneously you&#39;ll induce a problem if your cloud tries to merge databases. However, it&#39;s not that bad, because cloud systems usually create full history of your files with a revision for every change you make, meaning that if your database files (files with extention *.sqlite in Zotero) get corrupted, you can always roll back to a previous version and have zero losses.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;You can create a very good and reliable scientific literature database system using Zotero, and a cloud. This is a perfect solution for personal literature. However, I still don&#39;t have a solution for groups that won&#39;t involve storing data on a 3rd party server.
PS: It could be possible to use your own server to synchronize Zotero database as if you would be synchronizing with the official Zotero server. However, this would involve recompiling the source code of Zotero with your server address, which, I think, is a huge burden. This depends on whether your group wants to be commited to such a solution.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>What is exactly non-deterministic in our universe?</title>
        <published>2014-08-11T21:56:05Z</published>
        <updated>2014-08-11T21:56:05Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/non-determinism/"/>
        <id>https://blog.afach.de/non-determinism/</id>
        <content type="html" xml:base="https://blog.afach.de/non-determinism/">&lt;h4 id=&quot;what-is-determinism&quot;&gt;What is determinism?&lt;/h4&gt;
&lt;p&gt;Determinism is the concept that the physical world that we live in is wound like a clock. The concept says that if we would know every law that governs the universe, and we have the computational power to compute those laws; then, we may know the future with 100% certainty.&lt;/p&gt;
&lt;h4 id=&quot;bad-news-is&quot;&gt;Bad news is...&lt;/h4&gt;
&lt;p&gt;This universe is non-deterministic. We know this, now, with great certainty, and with many experiments and many successful models that have shown, so far, that this is true. In fact, Einstein had fought his last 20 years, before his death, to disprove this fact, and he failed.
Will someone else disprove it? A Nobel prize awaits this Genius, if he could do it!&lt;/p&gt;
&lt;h4 id=&quot;main-question-of-the-article&quot;&gt;Main question of the article&lt;/h4&gt;
&lt;p&gt;What if an observer lives outside our universe and monitors the universe from the outside, and knows the internal parameters that control our universe. Will he be able to determine the future infinitely precisely?
It&#39;s a very complicated question in fact to think about. However, in order to answer the question, we have to understand what is non-deterministic in our universe.&lt;/p&gt;
&lt;h4 id=&quot;how-can-we-tackle-this-question&quot;&gt;How can we tackle this question?&lt;/h4&gt;
&lt;p&gt;In order to understand the answer to this question, we have to understand what it is that we have to predict. And understand why uncertainty shows up in the first place.&lt;/p&gt;
&lt;h4 id=&quot;where-does-uncertainty-come-from&quot;&gt;Where does uncertainty come from?&lt;/h4&gt;
&lt;p&gt;Uncertainty comes mainly from the fact that we deal with a world governed by some classical parameters. For example, we deal with energy and position. Those parameters, if known very well, describe our systems very accurately.
In other words, in the simplest form, if we know the positions and energies of a set of particles; we may, as well, predict the future and dynamics of the system very accurately.&lt;/p&gt;
&lt;h4 id=&quot;what-is-it-that-we-re-uncertain-about-in-our-universe&quot;&gt;What is it that we&#39;re uncertain about in our universe?&lt;/h4&gt;
&lt;p&gt;Here comes the problem. When we go to the microscopic level of particles, we find that the macroscopic (large scale) description of systems is very different. In quantum mechanics (QM), systems are defined by the so called &quot;wave packets&quot;. They&#39;re not anymore &quot;objects&quot; like we thought of them before.&lt;/p&gt;
&lt;figure class=&quot;wp-caption&quot; style=&quot;width:235px&quot;&gt;&lt;a href=&quot;/media/Wave_particle_duality_p_unknown.png&quot;&gt;&lt;img src=&quot;/media/Wave_particle_duality_p_unknown.png&quot; alt=&quot;Wave_particle_duality_p_unknown&quot; class=&quot;content-img&quot; srcset=&quot;/media/Wave_particle_duality_p_unknown.png 235w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot; width=&quot;235&quot;&gt;&lt;/a&gt;&lt;figcaption&gt;The above picture shows that a particle in its microscopic description is not a particle, as we had learned about it before. It&#39;s a wave packet. If we try to look at this wave packet from our classical view, the transition from the wave description to the macroscopic, classical description, will suffer uncertainties. Look at the particle at the bottom. While the wave on the top is very clear and well defined with no uncertainties whatsoever, the bottom, classical, picture of the particle suffers uncertainties.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h4 id=&quot;the-problem-is-the-transition&quot;&gt;The problem is the transition&lt;/h4&gt;
&lt;p&gt;Our particles are described by wave packets in the microscopic level. But in our world, we don&#39;t deal with wave packets. We deal with particles. We deal with well defined energies and well defined positions. Therefore, we need a transformation that will take our wave packet from its wavy picture, to a picture that is compatible with our classical observations. This transformation is called in QM the &lt;strong&gt;expectation value&lt;/strong&gt;. When performing this transition, there is no way we can do it without uncertainty. For example, in the particle seen in the picture, we can never, ever, define a single point that characterizes the position of the particle. There&#39;s no position! The particle is smeared over a volume of space. Therefore, the transformation from the QM picture, with wavy properties, to the classical picture, is the cause of uncertainties.&lt;/p&gt;
&lt;h4 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;The reason of non-determinism in this world is not that we don&#39;t know the characteristics of a particle in its wave nature. The main problem causing uncertainty to appear is that the description of particles in their microscopic form is always coherent with uncertainties, due to the incompatibility of the world views when taking the step from the microscopic world to the classical world.
&lt;strong&gt;Answering the main question:&lt;/strong&gt; Will someone outside the universe, who knows the parameters of those wave packets, be able to predict the future with 100% certainty? The answer is &lt;em&gt;NO&lt;/em&gt;. Because the fact that a transformation from the wavy form to the classical form contains uncertainties is not related to our physical world. It&#39;s rather related to the mathematical nature of this transformation, which inherently will cause uncertainties to appear, independent of the knowledge of the entity performing this transformation.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Physics models nature, it doesn&#39;t find its laws</title>
        <published>2014-08-10T20:57:38Z</published>
        <updated>2014-08-10T20:57:38Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/physics-models/"/>
        <id>https://blog.afach.de/physics-models/</id>
        <content type="html" xml:base="https://blog.afach.de/physics-models/">&lt;h4 id=&quot;one-huge-misconception-of-physics-is-that-it-seeks-laws-that-are-presumed-to-exist-in-it&quot;&gt;One huge misconception of physics is that it seeks laws that are presumed to exist in it&lt;/h4&gt;
&lt;p&gt;No! Physics does not presume that nature has laws and tries to find them. Physics simply studies a phenomenon, and then tries to create a law that is accurate enough to reproduce the phenomenon, or at least to predict its existence in the future.&lt;/p&gt;
&lt;h4 id=&quot;do-those-sound-not-different-from-each-other&quot;&gt;Do those sound not different from each other?&lt;/h4&gt;
&lt;p&gt;They are very different! In the assumption that nature contains laws that we try to find, we assume that the laws that we find in nature are 100% accurate. Not only this, but we also assume that the laws of physics represent the system in its roots. Both assumptions are not true!&lt;/p&gt;
&lt;h4 id=&quot;why-is-this-wrong&quot;&gt;Why is this wrong?&lt;/h4&gt;
&lt;p&gt;Because the laws of physics that we create depend solely on our observations of those phenomena. With no doubt, our observations are simply a &lt;strong&gt;projection of reality&lt;/strong&gt; and not reality.&lt;/p&gt;
&lt;h4 id=&quot;has-there-been-incidents-that-show-that-this-is-the-case&quot;&gt;Has there been incidents that show that this is the case?&lt;/h4&gt;
&lt;p&gt;Yes! Along the history of physics, we have always seen that the laws we discover are simply a superset of older laws. For example, take a look at Newtonian Mechanics (NM) and Quantum Mechanics (QM). In NM, we created a physical quantity called &quot;Energy&quot;, and this energy played like the very main role in everything in classical physics, starting from simple motion, Lagrangian and Hamiltonian mechanics and ending with fundamental thermodynamics laws. However, in QM, we found tha energy, that we thought is fundamental, is not fundamental anymore! Not only that, but we also found that positions are not fundamental, and those characteristics that we used to use in classical mechanics and were absolute, do not work in QM anymore, not absolutely. Consequently, uncertainty principles showed up for position and energy.&lt;/p&gt;
&lt;figure class=&quot;wp-caption&quot; style=&quot;width:235px&quot;&gt;&lt;a href=&quot;/media/Wave_particle_duality_p_unknown.png&quot;&gt;&lt;img src=&quot;/media/Wave_particle_duality_p_unknown.png&quot; alt=&quot;Wave_particle_duality_p_unknown&quot; class=&quot;content-img&quot; srcset=&quot;/media/Wave_particle_duality_p_unknown.png 235w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot; width=&quot;235&quot;&gt;&lt;/a&gt;&lt;figcaption&gt;Subatomic particles are waves. What&#39;s the position of this particle?&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h4 id=&quot;newer-concepts&quot;&gt;Newer concepts&lt;/h4&gt;
&lt;p&gt;This means that the observable physical quantities that we see are not real! They&#39;re not how nature fundamentally works. Those concepts that we use are nothing but &lt;strong&gt;approximations&lt;/strong&gt; to reality. A newer concept came in the field called &quot;A wave function&quot;. It describes the behavior of our systems in a better way than before. The &lt;em&gt;Ehrenfest theorem&lt;/em&gt; showed also that Newtonian mechanics is nothing but a special case that is true as an approximation of the more general case found in quantum mechanics. The journey, actually, doesn&#39;t end there. After quantum mechanics, Quantum Field Theory (QFT) came up to provide a deeper view of nature&#39;s phenomena, destroying another concept in physics, &lt;strong&gt;time order&lt;/strong&gt;, and saying that time order isn&#39;t really as absolute as we thought it&#39;s. After that, string theory came up and claims (it&#39;s not tested yet) that dimensions are not as fundamental as we think they are. It claims that the dimensions that we live in are nothing but a special case of a more general concept.&lt;/p&gt;
&lt;h4 id=&quot;conclusion-are-we-ever-going-to-find-the-ultimate-laws-of-nature&quot;&gt;Conclusion: Are we ever going to find the ultimate laws of nature?&lt;/h4&gt;
&lt;p&gt;My discussion doesn&#39;t say that we may never find the ultimate laws of nature. It simply says that the claim that nature has a single set of laws that we think is ultimately what physics is looking for (Theory of Everything), is simply wrong. Even if physicists dream of this coming up eventually, this doesn&#39;t mean that this is what we&#39;re doing. And as Feynman said when asked the same question, &quot;if nature turned our to be a multi-layer onion with more, and more layers that come when we dig deeper, then that&#39;s the way it&#39;s&quot;. We simply don&#39;t know.
Physics doesn&#39;t presume anything. Right now we try to unify the laws that we know with the parameters we think are right. No one knows what kind of parameters govern the universe. We simply try to model the universe with the simple picture that we can understand with our small, simple brains. It could be true that positions, time, energy, and everything we use to model our universe is probably nothing but superficial parameters that approximate the real parameters that govern the universe.
Are we ever going to know? Let&#39;s dig further and find out!&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Electrons do not &quot;jump&quot; or teleport from one energy level to the other</title>
        <published>2014-08-03T11:11:30Z</published>
        <updated>2014-08-03T11:11:30Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/electron-jumps/"/>
        <id>https://blog.afach.de/electron-jumps/</id>
        <content type="html" xml:base="https://blog.afach.de/electron-jumps/">&lt;h3 id=&quot;neil-degrasse-tyson-you-gotta-fix-this&quot;&gt;Neil deGrasse Tyson, you gotta fix this!&lt;/h3&gt;
&lt;p&gt;I am very happy that Neil Tyson made the series &quot;Cosmos&quot;, where it is another way to communicate science to people, which is necessary in this era. I, personally, haven&#39;t watched it, because I&#39;m a physicist and the guy usually talks about things I learned academically. However, my wife watched it... and she told me once: &quot;Neil Tyson said that electrons disappear from one orbit and appear in another&quot;... and she continued talking, while I interrupted and asked... what?! How could a physicist say that? That destroys the simplest rule in relativity!
And yes, he did say that, which is crazy actually, and I&#39;m pretty shocked that this kind of mistake would come out of such a famous scientist. Look for yourself:&lt;/p&gt;
&lt;p&gt;&lt;video controls width=&quot;320&quot; height=&quot;180&quot; src=&quot;/media/CosmosQMError.mp4&quot;&gt;&lt;/video&gt;&lt;/p&gt;
&lt;h3 id=&quot;why-is-that-wrong&quot;&gt;Why is that wrong?&lt;/h3&gt;
&lt;p&gt;Simply, because there is no reason to believe that this is the case. Back then, when Bohr provided his semi-classical solution of the hydrogen atom, those transitions were not understood very well, and they would&#39;ve lead to such conflicts. But, do we still deal with Bohr&#39;s model? Definitely not! We now know Quantum Mechanics.
Before delving into Quantum Mechanics, let me pose this question: Is there any experimental evidence that electrons &quot;teleport&quot; from one orbit to the other as Neil Tyson said? The answer is: NO! And if there is, please let me know about them in the comments.
So, even if we would assume that Bohr made a successful model that explains the hydrogen energy levels in steady state, does that mean that it can be blindly extended to explain the dynamics of electronic transitions? Definitely not! That&#39;s not scientific at all.
Why is this not scientific? Because in science, we create models of natural phenomena, and then test them and try to disprove them. Now what we see in the case of Bohr&#39;s model, is that it successfully explained atomic energy levels to a good accuracy, but there is no part in Bohr&#39;s model that talks about transitions. Therefore, inferring blindly that electrons are only in those levels is... crazy!
On the other hand, this easily breaks special relativity&#39;s main result: Particles do not exceed the speed of light. So, what does this mean? This means that if what Neil Tyson said is true, then Quantum Field Theory, which is a superset of Quantum Mechanics, agrees that nothing exceeds the speed of light, but the very simple hydrogen atom in Quantum Mechanics... does not. How crazy is that?&lt;/p&gt;
&lt;h3 id=&quot;quantum-mechanics-and-the-hydrogen-atom&quot;&gt;Quantum Mechanics and the hydrogen atom&lt;/h3&gt;
&lt;p&gt;Explaining the Quantum Mechanics (QM) model, the QM model comes up when solving the &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Schr%C3%B6dinger_equation&quot;&gt;Schrödinger equation&lt;/a&gt; (time independent version of it), and the result from solving that is a wave-function, where this wave-function is directly related to the probability of finding an electron spatially somewhere.
In the case of a hydrogen atom, the Schrödinger equation is solved for simply a negative electron and a positive proton. The result of this problem is presented in a wave-function that uses complicated mathematical functions, called Legendre Polynomials and Spherical Harmonics. The result is presented in a nice picture that I found on Wikipedia.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/1126px-Hydrogen_Density_Plots.png&quot; alt=&quot;Hydrogen_Density_Plots&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/1126px-Hydrogen_Density_Plots.6f8faa2194126acb.png 400w, https://blog.afach.de/processed_images/1126px-Hydrogen_Density_Plots.3f1c0da3b7ea67b0.png 800w, /media/1126px-Hydrogen_Density_Plots.png 1126w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot;&gt;&lt;/p&gt;
Notice that the solution is not &quot;black and white&quot; like Neil Tyson described it. There&#39;s a key on the right, where a $+$ and $-$ can be seen. The $+$ represents higher probability than the $-$ regions. The first row shows the typical spherical orbits that we understand from classical mechanics (the Bohr model), while the other rows show more complicated solutions that involve angular momentum.
Notice that in those solutions, the wave-function is never zero anywhere but at infinity and specific points (lines, or nodes) in space that are infinitely small (Thanks to Lance for making me notice that more nodes exist in the wave-function)! So, according to our current knowledge of the hydrogen atom, why should we believe that electrons disappear from level to another? I think there&#39;s no reason whatsoever.
&lt;h3 id=&quot;a-little-more-detail-on-transitions&quot;&gt;A little more detail on transitions&lt;/h3&gt;
&lt;p&gt;Many atomic physics books treated the problem of atomic transitions in a model called &quot;Dipole Transitions&quot;. The model is usually accurate with relative accuracy of around $10^{-6}$. In that model, the problem of transitions is very well understood. For example, in the book &lt;a rel=&quot;external&quot; href=&quot;http://www.amazon.com/Optically-Polarized-Atoms-Understanding-interactions/dp/0199565120&quot;&gt;Optically Polarized Atoms: Understanding light-atom interactions&lt;/a&gt;, there is a section called &quot;Visualization of atomic transitions&quot;. In it, the author shows that a transition from one state to another can be well modeled with a simple time evolution operator that incorporates the two involved states.
For a transition from state $\left|2P\right\rangle$ to the state $\left|1S\right\rangle$ can be modeled with a simple wave function
$$\psi=a_{1}\left|1S\right\rangle +e^{-i\frac{E_{2}-E_{1}}{\hbar}t}a_{2}\left|2P\right\rangle$$
where $a_1,a_2$ are normalization factors, and $E_1,E_2$ are the energies of the states. We see that an oscillation of frequency (in units of energy) $E_2-E_1$ would happen, leading to the production of a photon. Then, again, why should we ever believe that electrons teleport from one atomic state to the other?&lt;/p&gt;
&lt;h3 id=&quot;is-it-just-simplicity&quot;&gt;Is it just simplicity?&lt;/h3&gt;
&lt;p&gt;Probably some people will argue that Neil Tyson was simplifying the atomic model for common people, but then I would ask the question: When did simplifications start to communicate false or wrong information? I think simplifying does not justify giving people wrong information at all.&lt;/p&gt;
&lt;h3 id=&quot;another-simpler-mistake&quot;&gt;Another simpler mistake&lt;/h3&gt;
&lt;p&gt;One more simple mistake Neil Tyson did in that video, is that he claimed that spontaneous decays are not understood (with why they happen). This is actually not true. In the same book I mentioned above, a discussion was put on that spontaneous decays happen due to spontaneous quantum fluctuations, that act as a stimulus for atoms and hit them. Therefore, technically, spontaneous decays do not exist; they&#39;re just another form of stimulated emission.
This is not a big deal, though. I think this is an advanced issue, and claiming that &quot;we don&#39;t know&quot; is better than posing wrong information.&lt;/p&gt;
&lt;h3 id=&quot;conclusion-and-discussion&quot;&gt;Conclusion and discussion&lt;/h3&gt;
&lt;p&gt;I didn&#39;t make this article to blame Neil Tyson, and actually he&#39;s done a very good job with Cosmos. But I made this article because I found it common in social networks that people use this wrong information, and it has to be cleared out. I actually would be very grateful to him if he would fix this mistake and replace the episode.
The conclusion of this article, is that electrons do not teleport from one energy level to the other. There&#39;s no evidence on that whatsoever! Electrons are, also, not strictly bound to those energy levels. According to our understanding of the quantum world, electrons have a probability cloud; and an electronic transition (dipole transition) will just make this cloud oscillate continuously from one energy level to another one continuously.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Should we trust eBay? Short answer: No!</title>
        <published>2014-07-29T19:08:04Z</published>
        <updated>2014-07-29T19:08:04Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/ebay-trust/"/>
        <id>https://blog.afach.de/ebay-trust/</id>
        <content type="html" xml:base="https://blog.afach.de/ebay-trust/">&lt;p&gt;Unlike all large corporations that try to become famous and trustworthy through good products, eBay proves, day after day, that they become famous by monopolizing the internet selling business.
Every time, and I really have to say this clearly: Every time I sell something on eBay there has to be something hidden in the deal. From the outside, they write that your offer costs: 0.00. But would it end there? Definitely not! You sell your product and you&#39;re happy, but eventually you get an invoice that you have to pay something like 3%-5% of the price of whatever you sold.
If one reads the policy of eBay in their help section, one will find that they clearly state that personal selling is free for up to 20 items per month. What does this mean in the real world? Nothing whatsoever! They still charge you.
In the following picture, you see that I sold components from my older computer. They charge me for them, although I&#39;m not selling them as commercial.
 &lt;/p&gt;
&lt;figure class=&quot;wp-caption&quot; style=&quot;width:550px&quot;&gt;&lt;a href=&quot;/media/eBayInvoice.png&quot;&gt;&lt;img src=&quot;/media/eBayInvoice.png&quot; alt=&quot;eBayInvoice&quot; class=&quot;content-img&quot; srcset=&quot;https://blog.afach.de/processed_images/eBayInvoice.ba64f3c7b29c8557.png 400w, https://blog.afach.de/processed_images/eBayInvoice.25667554607e3ab6.png 800w, https://blog.afach.de/processed_images/eBayInvoice.fd1d22771ff2cc53.png 1200w, https://blog.afach.de/processed_images/eBayInvoice.a7992cff2b9342ab.png 1600w, /media/eBayInvoice.png 1840w&quot; sizes=&quot;(max-width: 900px) 100vw, 640px&quot; width=&quot;550&quot;&gt;&lt;/a&gt;&lt;figcaption&gt;eBay charges you for personal transactions, even though they clearly say that there are no charges for up to 20 items of noncommercial deals (click to view full size).&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;I&#39;m not happy as a customer. What should I do? Of course, they have no e-mail to contact, because although they suck millions from customers through a robot server that practically doesn&#39;t cost anything, they can&#39;t hire some customer service to answer your questions! So, if you have a problem, please use the non-free phone number, where they&#39;ll make you pay more than the amount you&#39;re complaining about with your phone!
Is this the only time? Not really... I don&#39;t remember once when I sold stuff with eBay peacefully with no problems.
Let me top that with a nice story. Like a few years ago, I was stupid enough not to use PayPal and buy a processor for 400 euros. The seller didn&#39;t send anything and it turned out it&#39;s a scam. How did the guy do it? He simply put an external picture (from outside eBay) which was white. And after the payment, he changed the external link to contain text that says &quot;The item will be sent with no insurance, and the buyer will be responsible&quot;. I realized the trick, and informed eBay. Did they help? Actually they didn&#39;t even try! They made me pay around 45 euros for phone calls with them, but they did nothing! They didn&#39;t cooperate in any way, and my money was gone.
The morale out of this? Don&#39;t use eBay unless you must. Because there&#39;s always something hidden in their &quot;copy of the rules&quot; that you won&#39;t know about until you&#39;re given a good hit.
Peace!&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Tired from LaTeX tedious coding? Try LyX!</title>
        <published>2014-07-26T10:06:04Z</published>
        <updated>2014-07-26T10:06:04Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/lyx/"/>
        <id>https://blog.afach.de/lyx/</id>
        <content type="html" xml:base="https://blog.afach.de/lyx/">&lt;p&gt;I was really tired from latex and it&#39;s extremely tedious way of writing every equation. Of course, the resulting documents from latex are wonderfully organized and perfect for all scientific purposes.
For example, writing equations with latex is horribly complicated... It&#39;s almost a rule whenever you use latex that you miss a bracket that your latex compiler complains or you get the wrong equation. So what&#39;s wrong with the philosophy &quot;What You See Is What You Get&quot;? Nothing except that people often feel they don&#39;t have the same degree of freedom.
The question is: Is there a way to use the full power and facilities of latex, without the suffer one has to live with it? I think the answer is Yes! The answer is LyX.
&lt;a rel=&quot;external&quot; href=&quot;http://www.lyx.org&quot;&gt;LyX&lt;/a&gt; is a software that&#39;s based on latex, where it codes latex in the background. It can do everything latex does, with all the freedom you require in latex (that could go to the level of looking into the source), while at the same time, you could work in an environment of &quot;What You See Is What You Get&quot; (at least for equations). LyX in reality uses the philosophy &quot;What You See Is What You Mean&quot;, which is the same philosophy of latex but with no coding, except that you can see how your text looks like in the parts that matter, like text style and equations and figures.
In the worst case, if LyX has a function does not have a function that latex has, one can still write latex code inside lyx!
People who&#39;re used to using latex will definitely complain and say &quot;latex for the win&quot;, while I agree that it&#39;s a matter of taste. But I&#39;m a person who likes dedicating more time on the idea rather than on styling, especially when it comes to documentation. There&#39;s no doubt that making the same document on LyX takes much less time than it does on LaTeX. Therefore, the question rises: Why should I spend more time on coding when I still can do the same with a better tool?
Try lyx, and try to make use of it. It does nothing but make you spend half as much time you do to write an article in a scientific journal.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Faith is the enemy of science</title>
        <published>2014-07-24T20:50:38Z</published>
        <updated>2014-07-24T20:50:38Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/faith-science/"/>
        <id>https://blog.afach.de/faith-science/</id>
        <content type="html" xml:base="https://blog.afach.de/faith-science/">&lt;p&gt;As cliché as this phrase would sound, I had the luxury of learning it from practice rather than from hearing.
I spent 3 years of my life working on optically pumped vapor cesium magnetometers in the nEDM (neutron Electric Dipole Moment) experiment at PSI. For almost 10 years, it was believed by experts that cesium magnetometers are absolute magnetometers, with 100% accuracy, and it was believed that a magnetometer is only limited by its sensitivity.
This was believed in an experiment where the relative precision of the measurement required is $10^{-10}$. After spending three years with this belief, which was as long as 10 years for others in my group, and while I was investigating another quantum mechanical effect called &quot;The Quadratic Zeeman Effect&quot;, I stumbled upon an effect that relates the laser power to an absolute shift that could reach 100 pT; which is a relative accuracy shift of $10^{-4}$. Yes, 6 orders of magnitude were omitted for all those years, just because an inherited belief was carried on from one person to the other; with no experimental measurement that proves that cesium magnetometers are really absolute magnetometers.
Although I left religions long years ago, where I realized back then that faith never leads to anything related to the truth; I never realized that faith could find its way into my life again, in some other way.
Opposing faith while seeking the truth about reality is one of the greatest challenges a researcher could face. One would never realize where faith could sneak into your science career to ruin something you&#39;re doing.
Be aware of it!
Cheers, I learned my lesson :)&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Starting...</title>
        <published>2014-07-24T17:50:08Z</published>
        <updated>2014-07-24T17:50:08Z</updated>
        <author>
          <name>Samer Afach</name>
        </author>
        <link rel="alternate" type="text/html" href="https://blog.afach.de/starting/"/>
        <id>https://blog.afach.de/starting/</id>
        <content type="html" xml:base="https://blog.afach.de/starting/">&lt;p&gt;Starting this blog. I just hope I&#39;ll add one more useful thing for humanity to make the world a better place.&lt;/p&gt;
</content>
    </entry>
</feed>
