{"title":"funcptr - development","subtitle":"An engineer's technical notebook","link":[{"@attributes":{"rel":"self","type":"application\/atom+xml","href":"https:\/\/funcptr.net\/category\/development\/atom.xml"}},{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/"}}],"generator":"Zola","updated":"2018-05-21T14:00:00+00:00","id":"https:\/\/funcptr.net\/category\/development\/atom.xml","entry":[{"title":"Python Packaging and Distribution","published":"2018-05-21T14:00:00+00:00","updated":"2018-05-21T14:00:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2018\/python-packaging\/"}},"id":"https:\/\/funcptr.net\/2018\/python-packaging\/","content":"<p>There's been many a discussion online related to a variety of tools related to\nPython packaging and distribution. There is <a rel=\"external\" href=\"https:\/\/pip.pypa.io\">pip<\/a>, <a rel=\"external\" href=\"https:\/\/docs.pipenv.org\">pipenv<\/a> and\n<a rel=\"external\" href=\"https:\/\/poetry.eustace.io\">poetry<\/a> that have been the tools under discussion.<\/p>\n<p>As an open source maintainer as part of the <a rel=\"external\" href=\"https:\/\/pylonsproject.org\">Pylons Project<\/a>, while I would\nlove to be writing code I end up spending a lot of time dealing with user\nquestions around packaging\/distributing their source code using the software\nI've helped build, and as we move forward myself and other maintainers were\nwondering if we were actually helping users move forward in the best way\npossible using best of breed tools.<sup class=\"footnote-reference\" id=\"fr-201805tooldiscussionpyramid-1\"><a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fn-201805tooldiscussionpyramid\">[1]<\/a><\/sup><\/p>\n<p>As the Python community has moved from <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20180527214739\/http:\/\/setuptools.readthedocs.io\/en\/latest\/easy_install.html#easy-install\">easy_install<\/a> to pip, we too have\nkept the documentation up to date. We went from <code>python setup.py develop<\/code> to\n<code>pip install -e .<\/code> to create editable installs of local projects, and try to\nlet people know the pitfalls of using both easy_install and pip in the same\nproject (mostly with an answer that falls in line with: remove your virtual\nenvironment and start over, just use pip).<\/p>\n<p>As part of <a rel=\"external\" href=\"https:\/\/trypyramid.com\">Pyramid<\/a> we have developed and maintain various different\n<a rel=\"external\" href=\"https:\/\/cookiecutter.readthedocs.io\/en\/latest\/\">cookiecutter<\/a> templates, and our goal is to attempt to provide templates\nthat are both useful, but also follow best practices that are being adopted\nwithin the community at large so that newcomers can use their existing\nskills\/knowledge and those that are starting with us walk away with a knowledge\nand experience that applies not just to development of Pyramid applications,\nbut also applies to the broader community as a whole.<\/p>\n<h2 id=\"pip\">pip<\/h2>\n<p>Pip is a great tool that has simplified installation of packages, it supports\nusing binary distributions named <a rel=\"external\" href=\"https:\/\/github.com\/pypa\/wheel\">wheels<\/a> and has a way to easily install\nsoftware from the <a rel=\"external\" href=\"https:\/\/pypi.org\">Python Packaging Index<\/a>. It has a rather naive\ndependency resolution process, but for the most part it works and works well.\nIt replaced easy_install as the tool to use for installing packages.<\/p>\n<p>While you can use a <code>requirements.txt<\/code> file with pip to install a \"blessed\"\nlist of software there is no good way to \"lock\" the dependencies of\ndependencies without manually adding it to the list of requirements. This ends\nup making it very difficult to manage, and it is very difficult to know that\nwhat has been tested is what the user is actually going to get because packages\nmay be updated at anytime, and re-creating the same exact environment is\ndifficult and fraught with errors.<\/p>\n<p>This is where <a rel=\"external\" href=\"https:\/\/github.com\/pypa\/pipfile\">Pipfile<\/a> is supposed to help. This is a project to add a\nnew, more descriptive requirements file, as well as allowing for a lockfile\nthat would lock not just your primary packages you have listed, but also all\ndependencies of dependencies all the way down the tree. This helps with\nreproducibility and allows for the same installation on two different systems\nto have the exact same software\/dependencies installed.<\/p>\n<h2 id=\"pipenv\">pipenv<\/h2>\n<p>While pip is a great tool, and with the Pipfile changes it would allow for\nlocking of dependencies, there is one more puzzle piece missing. When\ninstalling packages while you can install them into the global namespace, the\nrecommended way is to install all packages for a particular tool\/project into a\nvirtual environment.<\/p>\n<p>Normally you'd invoke <a rel=\"external\" href=\"https:\/\/virtualenv.pypa.io\/en\/stable\/\">virtualenv<\/a>, to create this environment and then\nyou'd make sure to install all packages within it, thereby isolating it from\nthe rest of the system.<\/p>\n<p>pipenv automates this for you, as well as using <code>Pipfile<\/code> it also supports\nlocking using <code>Pipfile.lock<\/code> and provides a bunch of tooling around\nadding\/removing dependencies from a local project.<\/p>\n<p>pipenv allows you to easily create an environment and manage dependencies, but\nit makes no effort to solve the problem of distributing and building a package\nthat may be installed by third parties.<\/p>\n<h2 id=\"poetry\">poetry<\/h2>\n<p>Poetry is a similar project to pipenv, with a major difference being that it\nwas built to help with distributing\/developing applications and building a\ndistributable package that may then be installed using pip.<\/p>\n<p>Instead of using a <code>Pipfile<\/code> it uses a recently standardised <code>pyproject.toml<\/code>\nfile instead. Like pipenv it also supports locking, and it provides tooling\naround adding\/removing dependencies as well as managing what versions are\nrequired.<\/p>\n<p>Ultimately those dependencies are going to end up as metadata in a\ndistributable package.<\/p>\n<p>Poetry makes it easier to manage a software development project, whether that\nis for an application using various libraries for internal use, or for\nlibraries that are going to be distributed to other developers.<\/p>\n<h2 id=\"the-divide\">The divide<\/h2>\n<p>This is where the divide really starts, while you can use pipenv with a\nstandard <a rel=\"external\" href=\"https:\/\/setuptools.readthedocs.io\/en\/latest\/\">setuptools<\/a> project, any dependencies you add to the Pipfile\nusing pipenv's tooling will not be listed as a dependencies for your project\nwhen you distribute it, this either means you need to duplicate the list in\nboth <code>setup.py<\/code> as well as the <code>Pipfile<\/code>, or you have to add your current\nproject as an editable install within your <code>Pipfile<\/code> which means your <code>Pipfile<\/code>\nis now not as easily distributable.<\/p>\n<p>There are work-arounds that people have used, such as having <code>setup.py<\/code> read a\n<code>requirements.txt<\/code>, so that you could have all your requirements listed in a\ntext file, and not in <code>setup.py<\/code>, but asking to do the same with a <code>Pipfile<\/code> in\npipenv was met with a <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20201020045235\/https:\/\/github.com\/pypa\/pipenv\/issues\/209#issuecomment-332553113\">\"Do not do this.\"<\/a>.<\/p>\n<p>poetry explicitly allows you to add dependencies in one place, and those\ndependency listings are then automatically inserted into the package metadata\nthat is created when you build your distributable package.<\/p>\n<h2 id=\"the-two-use-cases\">The two use cases<\/h2>\n<p>There are two competing use cases, one is the deployment of software packages\nand being able to run them, but not as a developer, the other is a developer of\nsoftware packages that needs to define dependencies for the project to run.<\/p>\n<p>pipenv solves the deployment case. If I was a user I could very simply grab a\nknown good <code>Pipfile.lock<\/code> and use pipenv to install a known good set of\nsoftware, this is great when I am deploying a project. It is the use case that\nmany in the <a rel=\"external\" href=\"https:\/\/www.pypa.io\/en\/latest\/\">Python Packaging Authority<\/a> also seem to be optimizing for.<\/p>\n<p>The other use case is for developers that are building new software, either by\nusing a list of existing packages and deploying privately, or people developing\nsoftware for other developers to be published on the Python Packaging Index.<\/p>\n<p>This latter group of people is under represented due to it likely being much\nsmaller, and existing tools like <code>setuptools<\/code> and <code>setup.py<\/code> already providing\na \"good enough\" experience. Innovation in this area is something that readily\nneeds to be improved upon to make it easier to create new libraries\/packages\nthat follow best practices. The amount of copy and pastes people have done for\nadding a <code>setup.py<\/code> to their projects or to make something work is long. It's\nall a little bit of black magic, and there is a great many things that have\nbeen carried over because of cargo cult programming.<\/p>\n<h2 id=\"explicit-mentions-by-the-python-packaging-authority\">Explicit mentions by the Python Packaging Authority<\/h2>\n<p>Reading the packaging guide on <a rel=\"external\" href=\"https:\/\/packaging.python.org\/tutorials\/managing-dependencies\/\">managing dependencies<\/a>, pipenv is the\nrecommended tool:<\/p>\n<blockquote>\n<p>This tutorial walks you through the use of Pipenv to manage dependencies for\nan application. It will show you how to install and use the necessary tools\nand make strong recommendations on best practices.<\/p>\n<\/blockquote>\n<p>this language, along with what <code>packaging.python.org<\/code> implies as a URL makes it\ndifficult as a project maintainer to recommend alternate tools, becuase even if\nthose tools are superior for the use case we are recommending them for it is\nalways going to lead to questions from users, such as:<\/p>\n<blockquote>\n<p>Why are you not using pipenv, the official tool recommended by Python.org?<\/p>\n<\/blockquote>\n<p>We get similar questions about <code>easy_install<\/code> vs <code>pip<\/code> all of the time, as\nwell as why people should switch, and we can point to various bits of\ndocumentation that explains why pip is a better choice.<\/p>\n<p>If we were to recommend an alternate the appeal to authority that <code>python.org<\/code>\nimplies is going to make it much more difficult, and the question will become\n\"why is the Pylons Project not using recommended tooling?\"<\/p>\n<p>poetry is listed as a footnote on that page, alongside <a rel=\"external\" href=\"https:\/\/github.com\/jazzband\/pip-tools\">pip-tools<\/a> and\n<a rel=\"external\" href=\"https:\/\/github.com\/ofek\/hatch\">hatch<\/a>, and is mentioned only for doing library development, with no\nmention of other requirements that may make it a much better tool for\ndeveloping locally.<\/p>\n<h2 id=\"deployment-is-not-development\">Deployment is not development<\/h2>\n<p>If I am using pipenv with a non-installable project (no setup.py) I end up\nhaving to figure out how to get the code, and the <code>Pipfile<\/code>\/<code>Pipfile.lock<\/code> to\nmy environment I am deploying into. pipenv's install provides a way to make\nsure to only install if the <code>Pipfile.lock<\/code> is up to date or otherwise will fail\nto continue. If you are using a local project though, and it uses <code>setup.py<\/code>\nthe only way that the <code>Pipfile.lock<\/code> will contain any sub-dependencies of your\n<code>setup.py<\/code> project is if you install it as <code>editable<\/code>. Otherwise\nsub-dependencies are not locked.<sup class=\"footnote-reference\" id=\"fr-201805subdepends-1\"><a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fn-201805subdepends\">[2]<\/a><\/sup><\/p>\n<p>If I am using poetry I get an pip installable project, but it doesn't contain\nany hard pins or lock files. I'd have to distribute <code>pyproject.lock<\/code> as well as\nmy wheel. This gets me a little closer, but still no lock file that includes my\nnewly produced wheel, and has all of its dependencies locked.<\/p>\n<p>The Python Packaging Authority based on Twitter conversations with its members\nand the documentation on <code>packaging.python.org<\/code> suggest using pipenv for\ndevelopment. pipenv is particular ill-suited for development if the goal is to\ncreate a package to be deployed to production. With two locations to define\ndependencies it leaves people scratching their heads as to which is canonical,\nand if a dependency is added to <code>Pipfile<\/code> but not <code>setup.py<\/code> it may leave a\ndeveloper thinking their package is ready for distribution when in reality it\nis missing a dependency that is required to run\/use said distribution.<\/p>\n<p>At this point using both projects seems like a win-win. Use poetry to\nbuild\/develop a package, then use pipenv in the integration phase to create a\n<code>Pipfile.lock<\/code> that is used to deploy in production. This way you get the best\nof both worlds. A great tool that can help you register entry points and\nanother that can help you with deploying a known good set of dependencies.<\/p>\n<p>Interestingly, even the pipenv docs seem to agree that it is a deployment tool:<\/p>\n<blockquote>\n<p>Specify your target Python version in your Pipfile\u2019s [requires] section.\nIdeally, you should only have one target Python version, as this is a\ndeployment tool.<\/p>\n<p>-- <a rel=\"external\" href=\"https:\/\/docs.pipenv.org\/basics\/#general-recommendations-version-control\">Pipenv - General Recommendations &amp; Version Control<\/a><\/p>\n<\/blockquote>\n<p>Use pipenv if you have a script that requires a couple of dependencies and\ndoesn't need all of the extra overhead of packaging metadata\/packaging. Use\npoetry if you want to build a distributable project that can easily be deployed\nby others, and use both if you develop a project and need a known good\nenvironment to deploy.<\/p>\n<h2 id=\"in-summary\">In summary<\/h2>\n<p>There will likely never be a time that one single tool is considered good\nenough, and competition between tools is a way to keep advancing forward.\nPackaging in the Python community for a long time has been difficult. Wheels\nhas made things a little better. pip has made management of installing new\npackages easier and improved upon easy_install. Here's to the next evolution.<\/p>\n<hr \/>\n<p>Now, can we talk about standardising on <code>pyproject.toml<\/code> since that is already\nwhere \"project\" metadata needs to go, might as well re-use the name instead of\nhaving two different names\/files. Oh, and <a rel=\"external\" href=\"https:\/\/www.python.org\/dev\/peps\/pep-0517\/\">PEP 517<\/a> can't come soon enough\nso that alternate tools like <a rel=\"external\" href=\"https:\/\/flit.readthedocs.io\/en\/latest\/\">flit<\/a> can be used instead of\nsetuptools\/<code>setup.py<\/code>.<\/p>\n<section class=\"footnotes\">\n<ol class=\"footnotes-list\">\n<li id=\"fn-201805tooldiscussionpyramid\">\n<p>We created an issue named <a rel=\"external\" href=\"https:\/\/github.com\/Pylons\/pyramid\/issues\/3270\">Support poetry,\nflit, pipenv, or ...?<\/a> that attempts to go over the pros and cons of the\nvarious tools and how we currently support our users in our documentation on\nbuilding projects using pyramid, including how to create a project that is\ndistributable.  Pyramid heavily uses <a rel=\"external\" href=\"http:\/\/setuptools.readthedocs.io\/en\/latest\/pkg_resources.html\"><code>pkg_resources<\/code><\/a> and entry\npoints.  The way to register the entry points is to have an installable\npackage. The framework is flexible enough that there is no requirement for\nentry points, but at that point you are in territory where the default\ntooling provided by the project will not work, and some of the convenience\ntools\/functionality that Pyramid provides it's users\/developers is not\navailable. <a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fr-201805tooldiscussionpyramid-1\">\u21a9<\/a><\/p>\n<\/li>\n<li id=\"fn-201805subdepends\">\n<p>See documentation for <a rel=\"external\" href=\"https:\/\/docs.pipenv.org\/basics\/#editable-dependencies-e-g-e\">Editable Dependencies (e.g.  <code>-e   .<\/code>)<\/a> which as of this writing states: <blockquote>Sub-dependencies are <strong>not<\/strong> added to the Pipfile.lock if you leave the -e option out.<\/blockquote> <a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fr-201805subdepends-1\">\u21a9<\/a><\/p>\n<\/li>\n<\/ol>\n<\/section>\n"},{"title":"Build numbers in binaries using waf","published":"2014-06-10T17:32:03+00:00","updated":"2014-06-10T17:32:03+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2014\/build-numbers-in-binaries-using-waf\/"}},"id":"https:\/\/funcptr.net\/2014\/build-numbers-in-binaries-using-waf\/","content":"<p>My build system of choice these days for any C++ project is <a rel=\"external\" href=\"https:\/\/waf.io\">waf<\/a>. One of\nthe things I always like havig is the build number included in the final\nbinary, so that with a simple <code>.\/binary --version<\/code> or even <code>.\/binary<\/code> the\nversion is printed that it was built from. This can make it much simpler to\ndebug any potential issues, especially if fixes may have already been made but\na bad binary was deployed.<\/p>\n<h2 id=\"setup-the-wscript\">Setup the wscript<\/h2>\n<p>Make sure that your wscript somewhere near the top contains the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;\">APPNAME<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">myapp<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">0.0.0<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span><\/code><\/pre>\n<p>Then in your <code>configure(cfg)<\/code> add the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span>cfg.env.<\/span><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> VERSION<\/span><\/span>\n<span class=\"giallo-l\"><span>cfg.env.<\/span><span style=\"color: #82AAFF;\">APPNAME<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> APPNAME<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>git_version<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #B2CCD6;\"> try_git_version<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> git_version:<\/span><\/span>\n<span class=\"giallo-l\"><span>    cfg.env.<\/span><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> +=<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">-<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #C792EA;\"> +<\/span><span> git_version<\/span><\/span><\/code><\/pre>\n<p>The <code>try_git_version()<\/code> function is fairly simple and looks like this:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> try_git_version<\/span><span style=\"color: #D9F5DD;\">()<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    import<\/span><span> os<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    import<\/span><span> sys<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>    version<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #FF5874;\"> None<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    try<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>        version<\/span><span style=\"color: #C792EA;\"> =<\/span><span> os.<\/span><span style=\"color: #B2CCD6;\">popen<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">git describe --always --dirty --long<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">read<\/span><span>().<\/span><span style=\"color: #B2CCD6;\">strip<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    except<\/span><span style=\"color: #C5E478;\"> Exception<\/span><span style=\"color: #C792EA;font-style: italic;\"> as<\/span><span> e:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        print<\/span><span> e<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    return<\/span><span> version<\/span><\/span><\/code><\/pre>\n<p>It runs <code>git describe --always --dirty --long<\/code> which will return something\nalong these lines: <code>401b85f-dirty<\/code>. If you have any annoted tags, it will\nreturn the tag name as well.<\/p>\n<p>If <code>git<\/code> is not installed, or it is not a valid <code>git<\/code> directory, then it will\nsimply return <code>None<\/code>. At that point all we have to go on is the <code>VERSION<\/code>\nvariable set at the top of the wscript.<\/p>\n<p>Now that we have our configuration environment set up with the <code>VERSION<\/code> we\nwant to get that into a file that we can then include in our C++ source code.<\/p>\n<h2 id=\"create-a-build-version-h-in-file\">Create a <code>build_version.h.in<\/code> file<\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">ifndef<\/span><span style=\"color: #7FDBCA;font-style: italic;\"> BUILD_VERSION_H_IN_941AD1F24D0A9D<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">define<\/span><span style=\"color: #7FDBCA;font-style: italic;\"> BUILD_VERSION_H_IN_941AD1F24D0A9D<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">char<\/span><span style=\"color: #C5E478;\"> VERSION<\/span><span>[]<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">@VERSION@<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">endif<\/span><span style=\"color: #637777;font-style: italic;\"> \/* BUILD_VERSION_H_IN_941AD1F24D0A9D *\/<\/span><\/span><\/code><\/pre><h2 id=\"add-the-following-to-build-ctx\">Add the following to <code>build(ctx)<\/code><\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #B2CCD6;\">ctx<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">features<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">subst<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        source<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">build_version.h.in<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        target<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">build_version.h<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> ctx.env<\/span><span>[<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">VERSION<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>]<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span>        )<\/span><\/span><\/code><\/pre>\n<p>This uses the substitution feature to transform <code>build_version.h.in<\/code> into\n<code>build_version.h<\/code>, while inserting the version into the file.<\/p>\n<h2 id=\"include-build-version-h-in-your-source-code\">Include <code>build_version.h<\/code> in your source code<\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">build_version.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span><\/code><\/pre>\n<p>And add something along these lines to your <code>main()<\/code>:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span>std::<\/span><span style=\"color: #C5E478;\">cerr<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">Version: <\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span style=\"color: #C5E478;\"> VERSION<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span> std::<\/span><span style=\"color: #C5E478;\">endl<\/span><span>;<\/span><\/span><\/code><\/pre>\n<p>This will print out the <code>VERSION<\/code> that has been stored in <code>build_version.h<\/code>.<\/p>\n<h2 id=\"full-example\">Full example<\/h2>\n<p>Check out my <a rel=\"external\" href=\"https:\/\/github.com\/digitalresistor\/mdns-announce\">mdns-announce<\/a> project on Github for an example of how this is\nimplemented.<\/p>\n"},{"title":"User sessions, what data should be stored where?","published":"2013-08-25T13:05:00+00:00","updated":"2013-08-25T13:05:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2013\/user-sessions-and-the-data-to-be-stored\/"}},"id":"https:\/\/funcptr.net\/2013\/user-sessions-and-the-data-to-be-stored\/","content":"<p><em>This article has been updated. For the old version please check\n<a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20130826031727\/http:\/\/funcptr.net\/2013\/08\/25\/user-sessions,-what-data-should-be-stored-where-\/\">Archive.org<\/a><\/em><\/p>\n<p>A couple of days ago on <a rel=\"external\" href=\"http:\/\/reddit.com\/\">reddit.com<\/a>'s <a rel=\"external\" href=\"http:\/\/reddit.com\/r\/netsec\/\">\/r\/netsec<\/a> a poster by the name\nof <a rel=\"external\" href=\"http:\/\/danweber.blogspot.com\/\">Dan Weber<\/a> posted what he believed to be an attack on PHP sessions:\n<a rel=\"external\" href=\"http:\/\/danweber.blogspot.com\/2013\/08\/hacking-php-sessions-by-running-out-of.html\">Hacking PHP sessions by running out of memory<\/a> <a rel=\"external\" href=\"http:\/\/www.reddit.com\/r\/netsec\/comments\/1k7khy\/i_know_its_not_that_hard_but_i_think_i_found_a\/\">(reddit link)<\/a>. The way\nthe \"attack\" works is as follows:<\/p>\n<ol>\n<li>Create a new session<\/li>\n<li>Assign some new data to the session, in this case a username which is used\nto signify that the user is logged in.<\/li>\n<li>Check to verify that the user should be logged in<\/li>\n<li>If the user should not be logged in, destroy the session.<\/li>\n<\/ol>\n<p>The \"attack\" would be to run the <a rel=\"external\" href=\"http:\/\/php.net\/\">PHP<\/a> script out of memory on number 3, since\nonce something is set on the session it is immediately stored, so even if the\nuser is not supposed to be logged in, they are now logged in since their\nsession says they are.<\/p>\n<p>I wouldn't necessarily call this a PHP hack, this is really just bad practice\nin terms of programming, the logic should be reversed.<\/p>\n<ol>\n<li>Create a new session<\/li>\n<li>Verify the user should be logged in<\/li>\n<li>If so, set the session data<\/li>\n<li>If the user should not be logged in, don't set the session data<\/li>\n<\/ol>\n<p>That would solve the problem at hand, and now there is no way for the user to\ntrick the PHP script into believing she is logged in when that is not the case.<\/p>\n<p>However as the discussion went on on Reddit, it became even more clear that\nthere are no good resources on what you should store in the user session, and\nwhat you shouldn't store in the user session. Some of these things may seem\nlike common knowledge, but sadly this is something every single new person to\nprogramming has to learn on their own.<\/p>\n<h2 id=\"some-assumptions\">Some assumptions<\/h2>\n<p>Let's get this out of the way, this is in no way limited to PHP, but it is the\none I will be using as an example. This can all apply to Ruby (<a rel=\"external\" href=\"http:\/\/rubyonrails.org\">Ruby on\nRails<\/a>), Python (<a rel=\"external\" href=\"http:\/\/www.pylonsproject.org\">Pyramid<\/a>) or many other frameworks.<\/p>\n<p>The basic problem is that generally writing to the session is not an atomic\ntransaction based on the page accessed, so the assumption made in this article\nis that when you write to the session it is instantly committed, and there is\nno way to roll it back upon failure. If there was, our first example listed\nwouldn't be able to occur since upon running out of memory at step 3, the\nsession would have been rolled back and cleaned up.<\/p>\n<h2 id=\"what-should-you-store-in-the-users-session\">What should you store in the users session?<\/h2>\n<p>You should only really store anything in the session that if it were made\npublic it would do very little harm.<\/p>\n<p>Really the list of items to be stored in a session are as follows:<\/p>\n<ol>\n<li>The users unique id (The ID that allows you to retrieve the users\ninformation from storage)<\/li>\n<li>Temporary state (i.e. Flash messages)<\/li>\n<li>CSRF token<\/li>\n<\/ol>\n<p>More importantly, don't store permission bits, or group memberships, or\nanything that is used to allow\/deny access to particular resources. You want to\nstore just enough information that upon a user accessing your site you are able\nto retrieve the users information from storage, and based upon that information\nfrom storage you then make decisions such as permissions\/group memberships.<\/p>\n<h3 id=\"why-is-this-important\">Why is this important?<\/h3>\n<p>One of the things that Dan Weber brought up in the Reddit post was storing the\nusers permission level and group membership in the session. If your code is\nthen relying on the session to always contain the right permission level, then\nthere is no way to expire someones access to the data.<\/p>\n<ol>\n<li>A user logs in<\/li>\n<li>A user gets various permissions, and they are set in the session<\/li>\n<li>The user has been fired from his job, and an administrator removes the\nusers permissions<\/li>\n<li>Since the user is still logged in, the users permission bits are still\nset, and he continues to have access to parts of the site\/information he\nshouldn't have access to.<\/li>\n<\/ol>\n<p>If instead on every page visit we simply pull out the users unique id and\nverify the permissions upon access, as soon as the permissions are revoked by\nthe administrator the user no longer has access to the various resources.<\/p>\n<h3 id=\"temporary-state\">Temporary state<\/h3>\n<p>There has to be an easy way to remember something from page visit to page visit\nthat isn't considered detrimental if the information gets lost. One of those\nthings is flash messages. Flash messages are generally used to provide the user\nindication that something has changed, they are shown once and then never\nagain.<\/p>\n<p>Storing these as session data makes sense. If the flash message gets set,\ngreat, if it doesn't get set, it doesn't matter. Flash messages are simply a\nnotification tool, if the user misses them it isn't important.<\/p>\n<h2 id=\"what-you-shouldn-t-store-in-the-session\">What you shouldn't store in the session<\/h2>\n<p>Definitely don't store any kind of permission bits, groups a user is a part of\nor anything that would allow the user access that they normally would not be\nable to access.<\/p>\n<p>On each page access check what permissions the user has. While it may mean a\nlittle more heavy lifting server side it provides extra security, and the means\nto enforce changes in permissions instantly.<\/p>\n<h2 id=\"good-secure-programming-practices\">Good secure programming practices<\/h2>\n<p>Keep secure programming practices in mind at all times, always consider how the\ninformation you are storing\/processing is accessed\/viewed\/administered. More\nimportantly think about the access controls that are in place, and how one\ncould expire access to a particular resource without requiring a co-operative\nclient.<\/p>\n<p>The ordering of how variables are set, and when they are set are very\nimportant.  <code>$_SESSION['isadmin'] = True<\/code> at the top of a PHP script, and then\nremoving it by checking to see if the user is actually an administrator later\non in the script is a bad idea.<\/p>\n<p>Always order your code so that if a failure does occur there is no chance that\na critical section of your code is executed by accident, or that information is\nstored in a half-verified state. This is especially important for access\ncontrol.<\/p>\n"},{"title":"Embedding ZeroMQ in the libev Event Loop","published":"2013-04-20T18:22:00+00:00","updated":"2013-04-20T18:22:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/"}},"id":"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/","content":"<p>In a <a href=\"\/2012\/zeromq-edge-triggered\/\">previous article on ZeroMQ<\/a> we went over how ZeroMQ is triggered when\nyou use the socket that ZeroMQ returns, in that article there was some\ndiscussion of embedding ZeroMQ into another event loop. Let's do that.<\/p>\n<p><a rel=\"external\" href=\"http:\/\/software.schmorp.de\/pkg\/libev.html\">libev<\/a> is an absolutely fantastic library that helps make it easy to write\nevented programs. Evented programs work by getting notified that an action has\nhappened, and acting upon it. Unlike threaded where multiple pieces of work are\nbeing executed at the same time, in an evented system you move every item that\ncould block to an event loop, that then calls back into user code with a\nnotification to continue. If one event uses up more than its fair share of CPU\ntime because it is busy doing a long calculation, every single other event that\nis waiting will never get notified.<\/p>\n<p>Now, as previously discussed ZeroMQ is edge triggered, so embedding it into an\nevent loop that is level triggered doesn't do us much good, because we will miss\ncertain ZeroMQ notifications.<\/p>\n<p>One way to solve this problem is by looping over ZeroMQ's event system until we\nget back a notification that it no longer has anything else for us to process,\nthat would look something like this<sup class=\"footnote-reference\" id=\"fr-20130420zeromqedge-1\"><a href=\"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/#fn-20130420zeromqedge\">[1]<\/a><\/sup>:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">do<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ We can read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><span style=\"color: #C792EA;font-style: italic;\"> else<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check to see if there is more to read ...<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><span style=\"color: #C792EA;font-style: italic;\"> while<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ If neither of the above is true, then it was a false positive<\/span><\/span><\/code><\/pre>\n<p>However if we are receiving information from ZeroMQ remote endpoints faster\nthan we can process them, we end up being stuck in that <code>do<\/code> ... <code>while<\/code> loop\nforever. If we have other events we want to process, that isn't entirely fair\nsince they will never ever get called again. Especially in a server application\nwhere it may be servicing thousands of clients this is simply not acceptable.<\/p>\n<h2 id=\"libev\">libev<\/h2>\n<p>libev provides various different event notifications, to be able to get around\nedge triggered notifications, and still provide fair round-robin for all events\nwe are going to have to build on top of multiple different events.<\/p>\n<p>The events used will be:<\/p>\n<ul>\n<li><a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_io_code_is_this_file_descrip\"><code>ev::io<\/code><\/a>: This one is pretty self explanatory, this is for getting notified\nabout input output changes. This is the one we are going to use on the\n<code>ZMQ_FD<\/code>.<\/li>\n<li><a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_prepare_code_and_code_ev_che\"><code>ev::prepare<\/code><\/a> and <a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_prepare_code_and_code_ev_che\"><code>ev::check<\/code><\/a>: These two are generally used together, they\ncan be used to change the event loop and or make modifications on the fly to\nevents that have been registered with the event loop.<\/li>\n<li><a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_idle_code_when_you_ve_got_no\"><code>ev::idle<\/code><\/a>: This is an event that gets fired whenever the event loop has\nnothing else to do, so no other events fired, this will fire.<\/li>\n<\/ul>\n<h2 id=\"plan-of-attack\">Plan of attack<\/h2>\n<p>Since the <code>prepare<\/code> and <code>check<\/code> run before and after the loop, we are going to\nbe using those to do most of the work. We use an <code>io<\/code> so that we can turn off\nthe <code>idle<\/code> when we can actually wait for a result from ZeroMQ's file\ndescriptor, otherwise we use <code>idle<\/code> so that we will always get called once\nevery loop.<\/p>\n<p>In the <code>prepare<\/code> watcher callback we do the following:<\/p>\n<ol>\n<li>Check to see what events ZeroMQ has for us, and check what events the user\nhas requested.<\/li>\n<li>If the ZeroMQ has an event for us that we want, and the user has requested\nthat event, we start the idle watcher.<\/li>\n<li>If ZeroMQ has no events, we start the io watcher.<\/li>\n<\/ol>\n<p>In the <code>check<\/code> watcher callback we do the following:<\/p>\n<ol>\n<li>Stop both the <code>io<\/code> and <code>idle<\/code> watchers, they were only there to make sure\nthat our <code>check<\/code> watcher was called.<\/li>\n<li>See what event ZeroMQ has for us, and check that against what the user\nwants. Depending on the event, call user defined function write() or user\ndefined function read().<\/li>\n<li>If this was a spurious wake-up on the part of ZeroMQ we simply ignore it\nand let libev go on to other events.<\/li>\n<\/ol>\n<p>We could make all of this work by simply using the <code>prepare<\/code>, <code>check<\/code> and\n<code>idle<\/code> watchers, but that would mean libev would be busy-waiting for something\nto happen on the ZeroMQ socket. The <code>io<\/code> watcher is required simply so in times\nof nothing happening libev in its library can call into the kernels event\nhandling mechanism and go to sleep. We can't use just the <code>io<\/code> watcher due to\nthe edge-triggered notification, because we'd miss all kinds of ZeroMQ\nmessages. So all four watchers are required, and play crucial parts in making\nthis work.<\/p>\n<h2 id=\"let-s-get-down-to-code\">Let's get down to code<\/h2>\n<p>Below you will find example code, it is not complete. Do note that I am using\nsome C++11isms, error checking code may not be complete\/correct and in general\nI don't suggest you copy and paste this without reading and understanding what\nit does.<\/p>\n<p>The <code>zmq_event<\/code> class is meant to be used as a base class, inherit from it, and\ncreate the <code>write()<\/code> and <code>read()<\/code> functions. These functions will be called\nwhen you are able to read from the ZeroMQ socket, or when you are able to write\nto the ZeroMQ socket. You are guaranteed to be able to read one whole ZeroMQ\nmessage, so if it is a multi-part message, do make sure to loop on\n<code>ZMQ_SNDMORE<\/code> as required.<\/p>\n<p>Upon instantiation it will automatically start being notified about events, we\nstart off with <code>ev::READ<\/code>. When your sub-class wants to write to ZeroMQ it\nshould put the messages to be written into a list somewhere, and set\n<code>ev::READ | ev::WRITE<\/code> on <code>watcher_io<\/code>, by calling\n<code>watcher_io.set(socket_fd, ev::READ | ev::WRITE)<\/code>. <code>write()<\/code> will then be\ncalled, write a single message to ZeroMQ, and if necessary when finished\nwriting, unset <code>ev::WRITE<\/code> using <code>watcher_io.set(socket_fd, ev::READ)<\/code>. If you\nare not finished writing, after writing that singular message you may return\nand <code>write()<\/code> will be called again the next loop iteration. This way if you\nhave a lot of data to write you don't starve the other events from receiving\ntheir notifications.<\/p>\n<h4 id=\"zmq-event-h\"><code>zmq_event.h<\/code><\/h4>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">string<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">ev++.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">zmq.hpp<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span> zmq_event {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    public:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">        zmq_event<\/span><span>(zmq::<\/span><span style=\"color: #C5E478;\">context_t<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> context<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> type<\/span><span>,<\/span><span style=\"color: #C792EA;font-style: italic;\"> const<\/span><span> std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> connect<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #82AAFF;font-style: italic;\"> ~zmq_event<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    protected:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ This gets fired before the event loop, to prepare<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> before<\/span><span>(ev::prepare<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> prep<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> revents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ This is fired after the event loop, but before any other type of events<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> after<\/span><span>(ev::check<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> check<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> revents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ We need to have a no-op function available for those events that we<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ want to add to the list, but should never fire an actual event<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        template<\/span><span> &lt;<\/span><span style=\"color: #C792EA;\">typename<\/span><span> T&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            inline<\/span><span style=\"color: #C792EA;\"> void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> noop<\/span><span>(T<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> w<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> revents<\/span><span>) {};<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Function we are going to call to write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #C792EA;\"> void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> write<\/span><span>()<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Function we are going to call to read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #C792EA;\"> void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> read<\/span><span>()<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Some helper function, one to start notifications<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> start_notify<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ And one to stop notifications.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> stop_notify<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Our event types<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::io<\/span><span style=\"color: #C5E478;\">      watcher_io<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::prepare<\/span><span style=\"color: #C5E478;\"> watcher_prepare<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::check<\/span><span style=\"color: #C5E478;\">   watcher_check<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::idle<\/span><span style=\"color: #C5E478;\">    watcher_idle<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Our ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>        zmq::<\/span><span style=\"color: #C5E478;\">socket_t socket<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\">           socket_fd<\/span><span style=\"color: #C792EA;\"> = -<\/span><span style=\"color: #F78C6C;\">1<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>};<\/span><\/span><\/code><\/pre><h4 id=\"zmq-event-cc\"><code>zmq_event.cc<\/code><\/h4>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">stdexcept<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">zmq_event.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">zmq_event<\/span><span>(zmq::<\/span><span style=\"color: #C5E478;\">context_t<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span style=\"color: #C5E478;\"> context<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #C5E478;\"> type<\/span><span>,<\/span><span style=\"color: #C792EA;font-style: italic;\"> const<\/span><span> std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span style=\"color: #C5E478;\"> connect<\/span><span>) :<\/span><span style=\"color: #82AAFF;font-style: italic;\"> socket<\/span><span>(<\/span><span style=\"color: #C5E478;\">context<\/span><span>,<\/span><span style=\"color: #C5E478;\"> type<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Get the file descriptor for the socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    size_t<\/span><span style=\"color: #C5E478;\"> fd_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">_socket_fd<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_FD<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">socket_fd<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">fd_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Actually connect to the ZeroMQ endpoint, could replace this with a bind as well ...<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">bind<\/span><span>(<\/span><span style=\"color: #C5E478;\">connect<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">c_str<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up all of our watchers<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Have our IO watcher check for READ on the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">set<\/span><span>(<\/span><span style=\"color: #C5E478;\">socket_fd<\/span><span>, ev::<\/span><span style=\"color: #C5E478;\">READ<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ This watcher has a no-op callback<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">noop<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up our prepare watcher to call the before() function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_prepare<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">before<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up the check watcher to call the after() function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_check<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">after<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up our idle watcher, once again a no-op<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_idle<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">noop<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Tell libev to start notifying us!<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">    start_notify<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #C792EA;\">~<\/span><span style=\"color: #82AAFF;font-style: italic;\">zmq_event<\/span><span>() {}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">before<\/span><span>(ev::<\/span><span style=\"color: #C5E478;\">prepare<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">EV_ERROR<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">libev error<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Get any events that may be waiting<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    uint32_t<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Lucky for us, getting the events available doesn&#39;t invalidate the<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ events, so that calling this in `before()` and in `after()` will<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ give us the same results.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check what events exists, and check it against what event we want. We<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ &quot;abuse&quot; our watcher_io.events for this information.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">WRITE<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        watcher_idle<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">READ<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        watcher_idle<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ No events ready to be processed, we&#39;ll just go watch some io<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">after<\/span><span>(ev::<\/span><span style=\"color: #C5E478;\">check<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">EV_ERROR<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">libev error<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Stop both the idle and the io watcher, no point in calling the no-op callback<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ One of them will be reactived by before() on the next loop<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_idle<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Get the events<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    uint32_t<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check the events and call the users read\/write function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">READ<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">        this<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">read<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">WRITE<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">        this<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">write<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">start_notify<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_check<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_prepare<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop_notify<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_check<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_prepare<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span><\/code><\/pre><h2 id=\"other-event-loops\">Other event loops<\/h2>\n<p>libev is but just one of many event loops that exist out there, hopefully\nthis shows how it is possible to embed ZeroMQ into an event loop, thereby\nmaking it easier to embed ZeroMQ into any other event loops.<\/p>\n<section class=\"footnotes\">\n<ol class=\"footnotes-list\">\n<li id=\"fn-20130420zeromqedge\">\n<p>This snippet was from my older article regarding\n<a href=\"\/2012\/zeromq-edge-triggered\/\">ZeroMQ edge triggered notifications<\/a>. I would highly suggest reading that\narticle for more information and even more background on what is going on. <a href=\"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/#fr-20130420zeromqedge-1\">\u21a9<\/a><\/p>\n<\/li>\n<\/ol>\n<\/section>\n"},{"title":"ZeroMQ - Edge Triggered Notification","published":"2012-09-10T19:01:50+00:00","updated":"2012-09-10T19:01:50+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2012\/zeromq-edge-triggered\/"}},"id":"https:\/\/funcptr.net\/2012\/zeromq-edge-triggered\/","content":"<p><a rel=\"external\" href=\"http:\/\/www.zeromq.org\">ZeroMQ<\/a> is an absolutely fantastic communications library that has allowed\nme to very easily create networks\/connections between multiple different\napplications to communicate in an extremely fast yet directed manner and allow\nscaling of the connected entities with almost no second thought.<\/p>\n<p>I have used ZeroMQ within Python and C++, and it has been fantastic leaving\nthat part of the communication layer to a library rather than writing something\nsimilar myself. However, embedding ZeroMQ into an another event loop has proven\nto have some quirks. They are documented in the API, but not so much by other\nusers\/examples found around the web, and caught me by surprise.<\/p>\n<p>This post mainly goes over the issue, the difference between edge and level\ntriggered and how ZeroMQ works. I am hoping to spend some time documenting how\nto embed ZeroMQ correctly inside <a rel=\"external\" href=\"http:\/\/software.schmorp.de\/pkg\/libev.html\">libev<\/a> at a later date.<\/p>\n<h2 id=\"the-quirk\">The quirk<\/h2>\n<p>ZeroMQ allows you to get an underlying file descriptor (<code>ZMQ_FD<\/code>) for a ZeroMQ\nsocket using <a rel=\"external\" href=\"http:\/\/api.zeromq.org\/2-2:zmq-getsockopt#toc21\"><code>getsockopt()<\/code><\/a>, this is not the actual ZeroMQ file descriptor\nbut a stand-in:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> fd<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> fd_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">fd<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_FD<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">fd<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">fd_len<\/span><span>);<\/span><\/span><\/code><\/pre>\n<p>This file descriptor can then be passed on to <code>poll()<\/code>, <code>select()<\/code>, <code>kqueue()<\/code>\nor whatever your event notification library of choice may be, the idea being\nthat if the file descriptor is notified to be available for reading then you\ncheck the sockets <a rel=\"external\" href=\"http:\/\/api.zeromq.org\/2-2:zmq-getsockopt#toc22\"><code>ZMQ_EVENTS<\/code><\/a> and find out if you are allowed to read or\nwrite to the ZeroMQ socket, or even possibly neither (false positive). The same\nfile descriptor is also used internally within the ZeroMQ library for\nnotification and thus we may receive an event notification that we can't do\nanything with.<\/p>\n<p>As stated in the <a rel=\"external\" href=\"http:\/\/api.zeromq.org\/2-2:zmq-getsockopt#toc21\">ZeroMQ API guide for <code>getsockopt()<\/code> under <code>ZMQ_FD<\/code><\/a>:<\/p>\n<blockquote>\n<p>the \u00d8MQ library shall signal any pending events on the socket in an\n<em>edge-triggered<\/em> fashion by making the file descriptor become ready for\nreading.<\/p>\n<\/blockquote>\n<p>and further it states:<\/p>\n<blockquote>\n<p>The ability to read from the returned file descriptor does not necessarily\nindicate that messages are available to be read from, or can be written to,\nthe underlying socket; applications must retrieve the actual event state with\na subsequent retrieval of the <code>ZMQ_EVENTS<\/code> option.<\/p>\n<\/blockquote>\n<p>So far so good, so after we return from our event notification on the file\ndescriptor we simply ask ZeroMQ whether or not we can read or write (or\npossibly both):<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ If neither of the above is true, then it was a false positive<\/span><\/span><\/code><\/pre>\n<p>So now you are happily reading from ZeroMQ and using the data as you wish,\nexcept you start to notice that sometimes it can take a little while for data\nto show up, or that if the end-point you are connected to on ZeroMQ sends a lot\nof data it is not delivered in a timely fashion. This is where the\ndocumentation for edge triggered comes into play, and that is something that is\ndifferent about ZeroMQ compared to for example BSD sockets.<\/p>\n<h2 id=\"edge-triggered-versus-level-triggered\">Edge triggered versus level triggered<\/h2>\n<p>Edge and level triggered refer to what kind of signal a device will output in\nan attempt to gain the attention of another device. Edge and level triggered\nhave their history in electronics and hardware, where they play a very crucial\nrole.<\/p>\n<p>For both level and edge triggered assume the data is represented by this simple string:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>00000000000111111111111111111111100000000011111111111000000000<\/span><\/span><\/code><\/pre>\n<p>The zeroes (0) are where no data is available, and the ones (1) are where data\nis available for processing.<\/p>\n<h3 id=\"level-triggered\">Level triggered<\/h3>\n<p>Level triggered devices when they require attention, and until they no longer\nrequire attention will signal with a high voltage (going back to early\nelectronics), and when they no longer require attention signal with a low\nvoltage.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>Data:  00000000001111111111111111111111000000000011111111111000000000<\/span><\/span>\n<span class=\"giallo-l\"><span>Block:      A               B              C         D<\/span><\/span>\n<span class=\"giallo-l\"><span>+ 5v              ____________________           _________<\/span><\/span>\n<span class=\"giallo-l\"><span>  -    __________|                    |_________|         |__________<\/span><\/span>\n<span class=\"giallo-l\"><span>  0v<\/span><\/span>\n<span class=\"giallo-l\"><span>State: 0          1                    0          1        0<\/span><\/span><\/code><\/pre>\n<p>At this point, once the signal turns to 1 in block B we can start processing\ndata, and until we are done processing data the signal will stay 1. Once we are\ndone processing the device will signal that we are done by setting the state to\n0 in block C, and the process can repeat itself with block D.<\/p>\n<h3 id=\"edge-triggered\">Edge triggered<\/h3>\n<p>Edge triggered devices when they require attention will simply turn on the\nsignal for a short period of time and then turn it back off until all of the\nrequireding processing is complete at which point they will once again signal\nthat they need processing.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>Data:  00000000001111111111111111111111100000000011111111111000000000<\/span><\/span>\n<span class=\"giallo-l\"><span>Block:      A     B              C                 D<\/span><\/span>\n<span class=\"giallo-l\"><span>+ 5v              __                              __<\/span><\/span>\n<span class=\"giallo-l\"><span>  -    __________|  |____________________________|  |________________<\/span><\/span>\n<span class=\"giallo-l\"><span>  0v<\/span><\/span>\n<span class=\"giallo-l\"><span>State: 0          1              0                1        0<\/span><\/span><\/code><\/pre>\n<p>In this case when the trigger arrives you have to process all of the data\navailable. When block B arrives and we get 1 as our signal, we have to process\nall of the data, we won't know when we are done other than some other\nnotification scheme by the device. (Such as a special message that says we are\ndone). Then when we are done we can go back to listening to waiting for a\ntrigger.<\/p>\n<p>If however in the time that we are processing the data from the trigger at B\nreceive more data to process we won't get notified because we weren't\nofficially done processing data from the device, and thus the device will not\nnotify us of the new data again until we have processed all of the data.<\/p>\n<p>There are some devices that as soon as you start processing their data, you\nre-enable the trigger, so if you receive new data while still processing the\nold you will get a new notification.<\/p>\n<h2 id=\"zeromq-uses-edge-triggered\">ZeroMQ uses edge triggered<\/h2>\n<p>When multiple message arrive for the ZeroMQ socket, one notification is sent.\nYou check to see if the <code>ZMQ_EVENTS<\/code> actually contains <code>ZMQ_POLLIN<\/code> and then\nread a single message from ZeroMQ. Content that you have processed the data\nZeroMQ has for you, you add the file descriptor back to the event notification\nlibrary and wait. However, you know you sent two messages, but all you got was\none. The next time you send data to the socket, it returns the second message,\nand you have now send three but the third doesn't seem to arrive.<\/p>\n<p>This is where the edge triggered comes in. ZeroMQ only notifies you once, and\nafter it has notified you it won't notify you again until it receives new\nmessages, <em>EVEN<\/em> if you have messages waiting for you. In BSD sockets when you\n<code>recv()<\/code> data from the socket, if it is not all of the data it will simply tell\nyou, and until all of the data has been read that the socket has to offer it is\ngoing to tell you (level triggered).<\/p>\n<p>This means that when you get a notification from ZeroMQ you have to use a loop\nto process all of the messages that are coming your way:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">do<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ We can read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><span style=\"color: #C792EA;font-style: italic;\"> else<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check to see if there is more to read ...<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><span style=\"color: #C792EA;font-style: italic;\"> while<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ If neither of the above is true, then it was a false positive<\/span><\/span><\/code><\/pre>\n<p>The same is true if you need to send multiple messages, you will need to check\n<code>ZMQ_EVENTS<\/code> for <code>ZMQ_POLLOUT<\/code> and send messages so long as that holds true, as\nsoon as it is no longer true you will need to wait until you get triggered\nagain by using the file descriptor in your event notification library.<\/p>\n<h2 id=\"embedding-or-co-existing-with-another-event-loop\">Embedding or co-existing with another event loop<\/h2>\n<p>Embedding ZeroMQ into a different event loop becomes more difficult when you\nrealise that it is edge triggered versus level triggered. If you want to allow\nfair processing of data between events it becomes harder to accomplish with\nZeroMQ because you are required to process all of the data before going back to\nyour event loop or requires the setting up of different types of events\ndepending on the state that the ZeroMQ socket is in to make sure you process\nall of the data and don't forget any.<\/p>\n<p>Edge triggered for the ZeroMQ file descriptor was a design decision which\nunfortunately made the use of ZeroMQ sockets more difficult and different from\nBSD sockets which makes it harder for application programmers to do the right\nthing. There are plenty of questions on the internet regarding the proper usage\nof <code>ZMQ_FD<\/code> with various different event notification schemes\/implementations.<\/p>\n"},{"title":"IPv6 -- getaddrinfo() and bind() ordering with V6ONLY","published":"2012-08-07T22:39:56+00:00","updated":"2012-08-07T22:39:56+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2012\/ipv6-and-getaddrinfo\/"}},"id":"https:\/\/funcptr.net\/2012\/ipv6-and-getaddrinfo\/","content":"<p>Recently I ran into an issue that took me a while to sort out, and it is\nregarding inconsistent behaviour on various OS's with regards to IPv6 sockets\n(<a rel=\"external\" href=\"http:\/\/pubs.opengroup.org\/onlinepubs\/9699919799\/basedefs\/sys_socket.h.html\"><code>AF_INET6<\/code><\/a><sup class=\"footnote-reference\" id=\"fr-20120807afvspf-1\"><a href=\"https:\/\/funcptr.net\/2012\/ipv6-and-getaddrinfo\/#fn-20120807afvspf\">[1]<\/a><\/sup>) and calling <a rel=\"external\" href=\"http:\/\/www.freebsd.org\/cgi\/man.cgi?query=bind&amp;apropos=0&amp;sektion=2&amp;manpath=FreeBSD+9.0-RELEASE&amp;arch=default&amp;format=html\"><code>bind(2)<\/code><\/a> after getting the\nresults back from <a rel=\"external\" href=\"http:\/\/www.freebsd.org\/cgi\/man.cgi?query=getaddrinfo&amp;apropos=0&amp;sektion=3&amp;manpath=FreeBSD+9.0-RELEASE&amp;arch=default&amp;format=html\"><code>getaddrinfo(3)<\/code><\/a>.<\/p>\n<p>A call to <code>getaddrinfo()<\/code> with the hints set to <a rel=\"external\" href=\"http:\/\/pubs.opengroup.org\/onlinepubs\/9699919799\/basedefs\/sys_socket.h.html\"><code>AF_UNSPEC<\/code><\/a> in <code>ai_family<\/code>\nand <a rel=\"external\" href=\"http:\/\/www.freebsd.org\/cgi\/man.cgi?query=getaddrinfo&amp;apropos=0&amp;sektion=3&amp;manpath=FreeBSD+9.0-RELEASE&amp;arch=default&amp;format=html\"><code>AI_PASSIVE<\/code><\/a> in <code>ai_flags<\/code> will return to us 1 or more results that we\ncan <code>bind()<\/code> to. Sample code for that looks like this:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"c\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">struct<\/span><span> addrinfo hints,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span>addrlist;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">memset<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">&amp;<\/span><span style=\"color: #D7DBE0;\">hints<\/span><span>,<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(hints));<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ Ask for TCP<\/span><\/span>\n<span class=\"giallo-l\"><span>hints.ai_socktype <\/span><span style=\"color: #C792EA;\">=<\/span><span> SOCK_STREAM;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ Any family works for us ...<\/span><\/span>\n<span class=\"giallo-l\"><span>hints.ai_family <\/span><span style=\"color: #C792EA;\">=<\/span><span> AF_UNSPEC;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ Set some hints<\/span><\/span>\n<span class=\"giallo-l\"><span>hints.ai_flags <\/span><span style=\"color: #C792EA;\">=<\/span><span> <\/span><\/span>\n<span class=\"giallo-l\"><span>    AI_PASSIVE    <\/span><span style=\"color: #7FDBCA;\">|<\/span><span style=\"color: #637777;font-style: italic;\"> \/\/ We want to use this with bind<\/span><\/span>\n<span class=\"giallo-l\"><span>    AI_ADDRCONFIG;<\/span><span style=\"color: #637777;font-style: italic;\">  \/\/ Only return IPv4 or IPv6 if they are configured<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span> rv;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> ((rv <\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;font-style: italic;\"> getaddrinfo<\/span><span>(<\/span><span style=\"color: #F78C6C;\">0<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">7020<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> &amp;<\/span><span style=\"color: #D7DBE0;\">hints<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> &amp;<\/span><span style=\"color: #D7DBE0;\">addrlist<\/span><span>))<\/span><span style=\"color: #C792EA;\"> !=<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">    fprintf<\/span><span style=\"color: #82AAFF;\">(stderr,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">getaddrinfo: <\/span><span style=\"color: #82AAFF;\">%s<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #82AAFF;\">,<\/span><span style=\"color: #82AAFF;font-style: italic;\"> gai_strerror<\/span><span style=\"color: #82AAFF;\">(rv))<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    return<\/span><span style=\"color: #F78C6C;\"> 1<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ Use the list in *addrlist<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">for<\/span><span> (addr <\/span><span style=\"color: #C792EA;\">=<\/span><span> addrlist; addr <\/span><span style=\"color: #C792EA;\">!=<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>; addr <\/span><span style=\"color: #C792EA;\">=<\/span><span> addr<\/span><span style=\"color: #7FDBCA;\">-<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>ai_next) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ use *addr as appropriate<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ Clean up the memory from getaddrinfo()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">freeaddrinfo<\/span><span>(addrlist);<\/span><\/span><\/code><\/pre>\n<p>On Linux there are two entries returned when the host it is run on has both\nIPv4 and IPv6 enabled. An <code>AF_INET<\/code> which was followed by an <code>AF_INET6<\/code>. Now,\nit is not said that you are required to use all of the results that are\nreturned, but if you want to listen on all address families it is off course\nsuggested.<\/p>\n<p>Following the steps below for each of the returned results should result in\nhaving 1 or more different sockets that are bound to a single port.<\/p>\n<ol>\n<li>Create the socket()<\/li>\n<li>Set any socket options you want (<code>SO_REUSEADDR<\/code> for example)<\/li>\n<li>Then bind() the socket<\/li>\n<li>After that call listen() (followed off course by accept() on the socket)<\/li>\n<\/ol>\n<p>Only for some unknown reason (and <code>errno<\/code> is no help) <code>bind()<\/code> fails when you\nget to the <code>AF_INET6<\/code>, which was returned second. Searching online as to why\nthe bind would fail doesn't give you any good results and the thing that is\neven worse is that if you run the same code on another platform such as\n<a rel=\"external\" href=\"http:\/\/www.freebsd.org\">FreeBSD<\/a>, <a rel=\"external\" href=\"http:\/\/openindiana.org\">OpenIndiana<\/a> or <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20120808044613\/http:\/\/www.apple.com\/osx\/\">Mac OS X<\/a> no such failure exists. However\nI started suspecting something was up when I started looking at the output from\n<code>netstat -lan | grep 7020<\/code> on Mac OS X. Where 7020 is the port I passed into\n<code>getaddrinfo()<\/code>.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>tcp46      0      0  *.7020                 *.*  LISTEN     <\/span><\/span>\n<span class=\"giallo-l\"><span>tcp4       0      0  *.7020                 *.*  LISTEN <\/span><\/span><\/code><\/pre>\n<p>Wait a minute ... one of the sockets is on both IPv4 and on IPv6. Some more\ntime spent searching the internet I came across <a rel=\"external\" href=\"http:\/\/tools.ietf.org\/html\/rfc3493#page-22\">RFC 3493 section 5.3<\/a>,\nwhich is titled \"<code>IPV6_V6ONLY<\/code> option for <code>AF_INET6<\/code> Sockets\".<\/p>\n<blockquote>\n<p>As stated in section &lt;3.7 Compatibility with IPv4 Nodes&gt;, <code>AF_INET6<\/code> sockets\nmay be used for both IPv4 and IPv6 communications.  Some applications may\nwant to restrict their use of an <code>AF_INET6<\/code> socket to IPv6 communications\nonly.<\/p>\n<\/blockquote>\n<p>This was going down the right route, so I changed my code so that in the steps\nlisted above in number 2 I added the following code if the socket type is\n<code>AF_INET6<\/code>:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"c\"><span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #82AAFF;font-style: italic;\">setsockopt<\/span><span>(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,<\/span><span style=\"color: #7FDBCA;\"> &amp;<\/span><span style=\"color: #D7DBE0;\">yes<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C792EA;\">int<\/span><span>))<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #7FDBCA;\"> -<\/span><span style=\"color: #F78C6C;\">1<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">    close<\/span><span style=\"color: #82AAFF;\">(sockfd)<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">    fprintf<\/span><span style=\"color: #82AAFF;\">(stderr,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">setsockopt: <\/span><span style=\"color: #82AAFF;\">%s<\/span><span style=\"color: #ECC48D;\"> IPV6_V6ONLY<\/span><span style=\"color: #F78C6C;\">\\n<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #82AAFF;\">,<\/span><span style=\"color: #82AAFF;font-style: italic;\"> strerror<\/span><span style=\"color: #82AAFF;\">(errno))<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    continue<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span><\/code><\/pre>\n<p>The RFC 3493 section 5.3 also states that this option should be turned off by\ndefault, which means that all IPv6 sockets can also communicate over IPv4. Thus\ntechnically setting the option manually in code the best way to fix the issue.\nFreeBSD has had this feature turned on (as in IPv6 sockets can only communicate\nwith IPv6 and NOT IPv4) since 5.x.<\/p>\n<p>The biggest issue is that the remaining operating systems (OS X and\nOpenIndiana) don't have the same behaviour as Linux which makes troubleshooting\nthis issue more difficult than it should be. The issue is that the RFC doesn't\nspecify what exactly the operating should do when it encounters a request to\nbind to the same port on IPv4 and IPv6. The only place where I have found this\ndocumented is in <a rel=\"external\" href=\"http:\/\/www.amazon.com\/Network-Programming-Jun-ichiro-itojun-Hagino\/dp\/1555583180\">\"IPv6 Network Programming\"<\/a> under \"Tips in IPv6\nProgramming\" chapter 4, section 4, appropriately titled \"bind(2) Ordering and\nConflicts\".<\/p>\n<hr \/>\n<p>If you get a <code>bind()<\/code> error when attempting to bind to an <code>AF_INET6<\/code> socket\nplease make sure that you set the socket option <code>IPV6_V6ONLY<\/code> on the <code>AF_INET6<\/code>\nsocket. The default as required by RFC 3493 is to have that option be off. The\ndefault is wrong, and the RFC should have been more specific regarding what the\nright behaviour is when attempting to bind on an <code>AF_INET6<\/code> socket when already\nbound on an <code>AF_INET<\/code> while <code>IPV6_V6ONLY<\/code> is set to false.<\/p>\n<p>The full code that I used for testing, along with a little bit more information\nis available as a <a rel=\"external\" href=\"https:\/\/gist.github.com\/3231892\">gist on github<\/a>.<\/p>\n<section class=\"footnotes\">\n<ol class=\"footnotes-list\">\n<li id=\"fn-20120807afvspf\">\n<p>The old BSD style <a rel=\"external\" href=\"http:\/\/www.freebsd.org\/cgi\/man.cgi?query=socket&amp;apropos=0&amp;sektion=2&amp;manpath=FreeBSD+9.0-RELEASE&amp;arch=default&amp;format=html\"><code>socket()<\/code><\/a> called for defines\nstarting with <code>PF_<\/code> such as <code>PF_INET<\/code> and <code>PF_INET6<\/code> with the <code>PF<\/code> standing for\nprotocol family. POSIX starts them with <code>AF_<\/code>, and calls them an address\nfamily. On almost every operating system <code>PF_INET<\/code> is the same as <code>AF_INET<\/code>. If\nthe define doesn't exist you can always create it. <a href=\"https:\/\/funcptr.net\/2012\/ipv6-and-getaddrinfo\/#fr-20120807afvspf-1\">\u21a9<\/a><\/p>\n<\/li>\n<\/ol>\n<\/section>\n"},{"title":"OpenSSL as a Filter (or non-blocking OpenSSL)","published":"2012-04-08T16:50:14+00:00","updated":"2012-04-08T16:50:14+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2012\/openssl-as-a-filter\/"}},"id":"https:\/\/funcptr.net\/2012\/openssl-as-a-filter\/","content":"<p><a rel=\"external\" href=\"http:\/\/openssl.org\/\">OpenSSL<\/a> as a library at first glance is complicated, and then you realise\nthat a lot of the <a rel=\"external\" title=\"ssl(3) documentation\" href=\"http:\/\/openssl.org\/docs\/ssl\/ssl.html\">documentation<\/a> seems to be incomplete or missing.\nGenerally the individual man pages for the various functions are not bad, and\nthey will give you relevant information to help you along, but it can be hard\nto find.<\/p>\n<p>The idea is to write a filter in such a way that OpenSSL can easily be disabled\nor removed. We don't want to rely on OpenSSL specific functionality in our\ncode, for instance in the future we may want to change to using <a rel=\"external\" href=\"http:\/\/botan.randombit.net\/\">Botan<\/a> or\nanother SSL\/TLS library without having to change a lot of core functionality.<\/p>\n<h2 id=\"possible-methods\">Possible methods<\/h2>\n<p>There are various ways of doing non-blocking OpenSSL, the main one is to simply\nset the underlying socket to non-blocking and pass it into OpenSSL, at that\npoint the functions <code>SSL_write()<\/code> and <code>SSL_read()<\/code> will enter various error\nstates that can be read by <a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/ssl\/SSL_get_error.html\"><code>SSL_get_error()<\/code><\/a>, more specifically the\nfunction will return either <code>SSL_ERROR_WANT_READ<\/code> or <code>SSL_ERROR_WANT_WRITE<\/code>.<\/p>\n<p>The other method, the one used below, was described by <a rel=\"external\" href=\"http:\/\/lists.schmorp.de\/pipermail\/libev\/2008q2\/000325.html\">Marc Lehmann in a post\nto the libev mailling list<\/a> and uses <a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/crypto\/bio.html\">memory BIO<\/a> objects. Specifically\nhe linked to some Perl sample code; the OpenSSL <a rel=\"external\" href=\"http:\/\/ue.tst.eu\/f7383715351b4427b6b9db660b6289f8.txt\">setup<\/a>, and the function\ncalled for new incoming data\/data to be written: <a rel=\"external\" href=\"http:\/\/ue.tst.eu\/2f4cfee5166db411bd85a40e06de6dbf.txt\">dotls function<\/a>.<\/p>\n<blockquote>\n<p>The code is very new and might still be buggy, but it outlines the\nprinciples: use a memory stream, which will avoid all issues with blocking in\nopenssl<\/p>\n<\/blockquote>\n<p>It shows the idea pretty clearly but unless you know Perl it can be really\nconfusing as to what is going on.<\/p>\n<h2 id=\"the-ssl-filter-implementation\">The SSL Filter implementation<\/h2>\n<p>The <a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/ssl\/SSL_CTX_new.html\"><code>SSL_CTX<\/code><\/a> that is passed in contains all of the initialisation that is\ndone for OpenSSL in general, which is not shown here. The <code>SSLFilter<\/code> is\ncreated for each time you want to have a socket start doing SSL.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ openssl_filter.h<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span> SSLFilter {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    public:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">        SSLFilter<\/span><span>(SSL_CTX<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> ctxt<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> nread<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> nwrite<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> aread<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> awrite<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #82AAFF;font-style: italic;\"> ~SSLFilter<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> update<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    private:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        bool<\/span><span style=\"color: #82AAFF;font-style: italic;\"> continue_ssl_<\/span><span>(<\/span><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #D7DBE0;\"> function_return<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        SSL<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\"> ssl<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        BIO<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\"> rbio<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        BIO<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\"> wbio<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>};<\/span><\/span><\/code><\/pre>\n<p>The class contains mainly various different bits of state, when you create the\nfilter you pass in a pointer to four different strings, they are used as\nfollows:<\/p>\n<ul>\n<li><code>nread<\/code>: This contains data to be processed by the filter, from the network<\/li>\n<li><code>nwrite<\/code>: This contains data that has been processed by the filter, and has\nto be sent to the network<\/li>\n<li><code>aread<\/code>: This contains data that has been processed by the filter, and is\nready to be processed by the application<\/li>\n<li><code>awrite<\/code>: This contains data that the application wants to send out to the\nnetwork and is ready for processing by the filter.<\/li>\n<\/ul>\n<p>The other private variables contain various different pieces of state and are\ncreated by the filters constructors.<\/p>\n<ul>\n<li><code>ssl<\/code>: Contains the state for the current SSL connection.<\/li>\n<li><code>rbio<\/code>: Contains the data that the SSL functions will read from, data put in\nhere is copied from <code>nread<\/code>.<\/li>\n<li><code>wbio<\/code>: Contains the data that the SSL functions will write to, data from\nthis BIO is copied to <code>nwrite<\/code>.<\/li>\n<\/ul>\n<p>The implementation is the really interesting part.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  1<\/span><span style=\"color: #637777;font-style: italic;\">\/\/ openssl_filter.cc<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  2<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  3<\/span><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">stdexcept<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  4<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  5<\/span><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">openssl_filter.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  6<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  7<\/span><span>SSLFilter::<\/span><span style=\"color: #82AAFF;font-style: italic;\">SSLFilter<\/span><span>(<\/span><span style=\"color: #C5E478;\">SSL_CTX<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> ctxt<\/span><span>, <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  8<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>, <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  9<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 10<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 11<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 12<\/span><span>                      :<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 13<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     nread<\/span><span>(<\/span><span style=\"color: #C5E478;\">nread<\/span><span>), <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 14<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     nwrite<\/span><span>(<\/span><span style=\"color: #C5E478;\">nwrite<\/span><span>), <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 15<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     aread<\/span><span>(<\/span><span style=\"color: #C5E478;\">aread<\/span><span>), <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 16<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     awrite<\/span><span>(<\/span><span style=\"color: #C5E478;\">awrite<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 17<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 18<\/span><span style=\"color: #C5E478;\">    rbio<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_new<\/span><span>(<\/span><span style=\"color: #82AAFF;font-style: italic;\">BIO_s_mem<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 19<\/span><span style=\"color: #C5E478;\">    wbio<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_new<\/span><span>(<\/span><span style=\"color: #82AAFF;font-style: italic;\">BIO_s_mem<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 20<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 21<\/span><span style=\"color: #C5E478;\">    ssl<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_new<\/span><span>(<\/span><span style=\"color: #C5E478;\">ctxt<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 22<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 23<\/span><span style=\"color: #82AAFF;font-style: italic;\">    SSL_set_accept_state<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 24<\/span><span style=\"color: #82AAFF;font-style: italic;\">    SSL_set_bio<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> rbio<\/span><span>,<\/span><span style=\"color: #C5E478;\"> wbio<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 25<\/span><span>}<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 26<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 27<\/span><span>SSLFilter::<\/span><span style=\"color: #C792EA;\">~<\/span><span style=\"color: #82AAFF;font-style: italic;\">SSLFilter<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 28<\/span><span style=\"color: #82AAFF;font-style: italic;\">    SSL_free<\/span><span>(<\/span><span style=\"color: #C5E478;\">_ssl<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 29<\/span><span>}<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 30<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 31<\/span><span style=\"color: #C792EA;\">void<\/span><span> SSLFilter::<\/span><span style=\"color: #82AAFF;font-style: italic;\">update<\/span><span>(Filter::FilterDirection) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 32<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ If we have data from the network to process, put it the memory BIO for OpenSSL<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 33<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #C5E478;\">nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">empty<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 34<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> written<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_write<\/span><span>(<\/span><span style=\"color: #C5E478;\">rbio<\/span><span>,<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">c_str<\/span><span>(),<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 35<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">written<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">erase<\/span><span>(<\/span><span style=\"color: #F78C6C;\">0<\/span><span>,<\/span><span style=\"color: #C5E478;\"> written<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 36<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 37<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 38<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ If the application wants to write data out to the network, process it with SSL_write<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 39<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #C5E478;\">awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">empty<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 40<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> written<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_write<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">c_str<\/span><span>(),<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 41<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 42<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #82AAFF;font-style: italic;\">continue_ssl_<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 43<\/span><span style=\"color: #C792EA;font-style: italic;\">            throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">An SSL error occured.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 44<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 45<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 46<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">written<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">erase<\/span><span>(<\/span><span style=\"color: #F78C6C;\">0<\/span><span>,<\/span><span style=\"color: #C5E478;\"> written<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 47<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 48<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 49<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ Read data for the application from the encrypted connection and place it in the string for the app to read<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 50<\/span><span style=\"color: #C792EA;font-style: italic;\">    while<\/span><span> (<\/span><span style=\"color: #F78C6C;\">1<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 51<\/span><span style=\"color: #C792EA;\">        char<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\">readto<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> new<\/span><span style=\"color: #C792EA;\"> char<\/span><span>[<\/span><span style=\"color: #F78C6C;\">1024<\/span><span>];<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 52<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> read<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_read<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>,<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 53<\/span><span>       <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 54<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #82AAFF;font-style: italic;\">continue_ssl_<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 55<\/span><span style=\"color: #7FDBCA;\">            delete<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 56<\/span><span style=\"color: #C792EA;font-style: italic;\">            throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">An SSL error occured.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 57<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 58<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 59<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">read<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 60<\/span><span style=\"color: #C792EA;\">            size_t<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 61<\/span><span style=\"color: #C5E478;\">            aread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">resize<\/span><span>(<\/span><span style=\"color: #C5E478;\">cur_size<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 62<\/span><span>            std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">copy<\/span><span>(<\/span><span style=\"color: #C5E478;\">readto<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>,<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">begin<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 63<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 64<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 65<\/span><span style=\"color: #7FDBCA;\">        delete<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 66<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 67<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #7FDBCA;\">static_cast<\/span><span style=\"color: #C792EA;\">&lt;size_t&gt;<\/span><span>(<\/span><span style=\"color: #C5E478;\">read<\/span><span>)<\/span><span style=\"color: #C792EA;\"> !=<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span style=\"color: #C792EA;\"> ||<\/span><span style=\"color: #C5E478;\"> written<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C792EA;font-style: italic;\"> break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 68<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 69<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 70<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ Read any data to be written to the network from the memory BIO and copy it to nwrite<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 71<\/span><span style=\"color: #C792EA;font-style: italic;\">    while<\/span><span> (<\/span><span style=\"color: #F78C6C;\">1<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 72<\/span><span style=\"color: #C792EA;\">        char<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\">readto<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> new<\/span><span style=\"color: #C792EA;\"> char<\/span><span>[<\/span><span style=\"color: #F78C6C;\">1024<\/span><span>];<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 73<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> read<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_read<\/span><span>(<\/span><span style=\"color: #C5E478;\">wbio<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>,<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 74<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 75<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">read<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 76<\/span><span style=\"color: #C792EA;\">            size_t<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 77<\/span><span style=\"color: #C5E478;\">            nwrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">resize<\/span><span>(<\/span><span style=\"color: #C5E478;\">cur_size<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 78<\/span><span>            std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">copy<\/span><span>(<\/span><span style=\"color: #C5E478;\">readto<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>,<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">begin<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 79<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 80<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 81<\/span><span style=\"color: #7FDBCA;\">        delete<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 82<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 83<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #7FDBCA;\">static_cast<\/span><span style=\"color: #C792EA;\">&lt;size_t&gt;<\/span><span>(<\/span><span style=\"color: #C5E478;\">read<\/span><span>)<\/span><span style=\"color: #C792EA;\"> !=<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span style=\"color: #C792EA;\"> ||<\/span><span style=\"color: #C5E478;\"> read<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C792EA;font-style: italic;\"> break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 84<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 85<\/span><span>}<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 86<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 87<\/span><span style=\"color: #C792EA;\">bool<\/span><span> SSLFilter::<\/span><span style=\"color: #82AAFF;font-style: italic;\">continue_ssl_<\/span><span>(<\/span><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #D7DBE0;\"> function_return<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 88<\/span><span style=\"color: #C792EA;\">    int<\/span><span style=\"color: #C5E478;\"> err<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_get_error<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> function_return<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 89<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 90<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_NONE<\/span><span style=\"color: #C792EA;\"> ||<\/span><span style=\"color: #C5E478;\"> err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_WANT_READ<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 91<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #82AAFF;\"> true<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 92<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 93<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 94<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_SYSCALL<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 95<\/span><span style=\"color: #82AAFF;font-style: italic;\">        ERR_print_errors_fp<\/span><span>(<\/span><span style=\"color: #C5E478;\">stderr<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 96<\/span><span style=\"color: #82AAFF;font-style: italic;\">        perror<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">syscall error: <\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 97<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #82AAFF;\"> false<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 98<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 99<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">100<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_SSL<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">101<\/span><span style=\"color: #82AAFF;font-style: italic;\">        ERR_print_errors_fp<\/span><span>(<\/span><span style=\"color: #C5E478;\">stderr<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">102<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #82AAFF;\"> false<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">103<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">104<\/span><span style=\"color: #C792EA;font-style: italic;\">    return<\/span><span style=\"color: #82AAFF;\"> true<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">105<\/span><span>}<\/span><\/span><\/code><\/pre><h3 id=\"explanation-of-the-ssl-filter-class\">Explanation of the SSL Filter class<\/h3>\n<p><strong>Line 7 - 25<\/strong>, we create the two memory BIO's and then call <code>SSL_new()<\/code> using\nthe passed in <code>SSL_CTX<\/code>, set the accept set for SSL and then we hook up the two\nBIO objects to the <code>ssl<\/code> object.<\/p>\n<p><strong>Line 27 - 29<\/strong>, All we do in the destructor is <code>SSL_free()<\/code> the SSL state,\nwhich automatically takes care of freeing the two memory BIOs that were given\nto it.<\/p>\n<p><strong>Line 33 - 36<\/strong>, if <code>nread<\/code> is not empty we copy the data into <code>rbio<\/code>. <code>rbio<\/code>\nis used by the SSL functions to read from (as if it were reading from a\nsocket). We read into the memory BIO as much as possible, technically this\nshould be everything, but it is possible it won't read everything due to memory\nconstraints, next time <code>SSLFilter::update()<\/code> is called it will get emptied as\nmuch as possible again.<\/p>\n<p><strong>Line 39 - 47<\/strong>, we see if there is anything that the app wants to write out\nto the remote client, if there is we call <code>SSL_write()<\/code>. After the call to\n<code>SSL_write()<\/code> we call <code>continue_ssl_()<\/code> to see if we can safely continue using\nSSL.<\/p>\n<p><strong>Line 50 - 68<\/strong>, this is where try to read as much data from <code>SSL_read()<\/code> as\nwe can and place it in <code>aread<\/code>. This is data that has been decrypted by OpenSSL\nand can be used by the application. After the call to <code>SSL_read()<\/code> we call\n<code>continue_ssl_()<\/code> to see if we can safely continue using SSL.<\/p>\n<p><strong>Line 71 - 84<\/strong>, if OpenSSL has to write something to the network, either\ndirectly due to something the app did (by filling <code>awrite<\/code>) or because of\nsomething the remote client sent, it will place it in its memory buffer\n(<code>wbio<\/code>), what we do here is drain that memory buffer and fill <code>nwrite<\/code>.<\/p>\n<p><strong>Line 87 - 105<\/strong>, the <code>continue_ssl_()<\/code> function gets the SSL errors using\n<a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/ssl\/SSL_get_error.html\"><code>SSL_get_error<\/code><\/a> and checks that either it is <code>SSL_ERROR_NONE<\/code> or\n<code>SSL_ERROR_WANT_READ<\/code> both of which are acceptable errors, any other errors and\nwe check to see if it is an error due to a system call or due to an SSL error,\nwe print out the errors to standard error and return false. At that point the\ncalling code is free to go about its business as it pleases (most likely\nthrowing an error).<\/p>\n<h4 id=\"notes-regarding-this-sample-code\">Notes regarding this sample code ...<\/h4>\n<p>The only thing missing in this example code is checking to see if the SSL\nconnection has gone into an <code>SSL_shutdown()<\/code> mode. This should be added so that\nyou cleanly shut down an SSL connection, but it is not absolutely required,\nespecially since it seems to be shaky as to whether or not it will work,\nespecially if you are using SSLv3 or SSLv2.<\/p>\n<h3 id=\"how-to-use-the-ssl-filter\">How to use the SSL Filter<\/h3>\n<p>Here are the steps to using this filter:<\/p>\n<ol>\n<li>At program startup set up the <code>SSL_CTX<\/code> structure as required, so that it\nbe used in the <code>SSLFilter<\/code><\/li>\n<li>Set up socket\/event loop and accept() new socket.<\/li>\n<li>Create four new <code>std::string<\/code>'s and create a new SSLFilter, pass in the\n<code>SSL_CTX<\/code> you created, and the four buffers.<\/li>\n<li>Add the new socket to the event loop to wait for reading.<\/li>\n<\/ol>\n<p>Now upon receiving a read ready status from the event loop, the following should be done:<\/p>\n<ol>\n<li>Read data into <code>nread<\/code> buffer.<\/li>\n<li>call <code>SSLFilter::update()<\/code><\/li>\n<li>See if <code>aread<\/code> is not empty, process the data as you please<\/li>\n<li>Write data to <code>awrite<\/code> as needed, if data is written to <code>awrite<\/code>, call\n<code>SSLFilter::update(WRITE)<\/code> to process the data<\/li>\n<li>See if <code>nwrite<\/code> is not empty, if so add socket to event loop for write\nreadiness.<\/li>\n<\/ol>\n<p>Once you receive a write ready status from the event loop, you should do the following:<\/p>\n<ol>\n<li>Write data in <code>nwrite<\/code> to the socket.<\/li>\n<li>If <code>nwrite<\/code> is empty, remove the socket from the event loop for writing (so\nthat you don't have the event loop notifying you of the ability to write,\neven-though you have nothing to write).<\/li>\n<\/ol>\n<p>Now it becomes simple to add OpenSSL to any open socket, in a way that is easy\nto refactor later and or disable depending on parameters passed in as arguments\nto the program. If you set <code>nread<\/code> to the same string as <code>aread<\/code> and <code>nwrite<\/code>\nto <code>awrite<\/code> and don't create an SSLFilter you have bypassed the filter...<\/p>\n"},{"title":"Magic Properties on Google AppEngine","published":"2010-03-24T02:15:33+00:00","updated":"2010-03-24T02:15:33+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2010\/magic-properties-on-google-appengine\/"}},"id":"https:\/\/funcptr.net\/2010\/magic-properties-on-google-appengine\/","content":"<p>Update to my previous <a href=\"\/2010\/auto-updating-properties-in-google-appengine-models\/\">Google AppEngine auto-updating properties post<\/a>.<\/p>\n<p>In my last post I was talking about:<\/p>\n<blockquote>\n<p>[...] however there is still an issue, for now the MagicProperty is only\nupdated the first time the property it affects is set<\/p>\n<\/blockquote>\n<p>I fixed that issue, it has a local cache that is added to the model, so if the\nvalue does change, the cache would no longer be valid and the value is\nrecreated, otherwise the already computed value is returned. There was however\nanother issue I had to deal with, and that was the fact that the cache was not\nbeing written to the datastore, this meant that upon getting the data back from\nthe datastore we recomputed it anyway. Now the programmer has to do some extra\nwork, they have to add a cache unindexed property to the model, and pass it\ninto the <code>MagicProperty<\/code> to be used as the caching variable. Within the\n<code>save()<\/code> they have to prime the cache by setting a temporary variable equal to\nthe to be computed variable just in case the cache variables have not been\nprimed, this has not yet been documented other than in my SVN history.<\/p>\n<p>We still don't allow setting of the magic property, however upon returning from\nthe datastore it calls <code>make_value_from_datastore<\/code>, which returns a\n<code>_MagicDatastore<\/code>, as long as we set with an instance of <code>_MagicDatastore<\/code> it will\nallow it to go through, so we can get the value from the datastore without\nhaving to recompute the value.<\/p>\n<p>Read the code for an example on how to use this.<\/p>\n<p>Code is added below, feel free to use it as you wish under the license that is\nattached.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  1<\/span><span style=\"color: #637777;font-style: italic;\">###<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  2<\/span><span style=\"color: #637777;font-style: italic;\"> # Copyright (c) 2010 Delta Regeer;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  3<\/span><span style=\"color: #637777;font-style: italic;\"> #<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  4<\/span><span style=\"color: #637777;font-style: italic;\"> # Permission to use, copy, modify, and distribute this software for any<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  5<\/span><span style=\"color: #637777;font-style: italic;\"> # purpose with or without fee is hereby granted, provided that the above<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  6<\/span><span style=\"color: #637777;font-style: italic;\"> # copyright notice and this permission notice appear in all copies.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  7<\/span><span style=\"color: #637777;font-style: italic;\"> #<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  8<\/span><span style=\"color: #637777;font-style: italic;\"> # THE SOFTWARE IS PROVIDED &quot;AS IS&quot; AND THE AUTHOR DISCLAIMS ALL WARRANTIES<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  9<\/span><span style=\"color: #637777;font-style: italic;\"> # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 10<\/span><span style=\"color: #637777;font-style: italic;\"> # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 11<\/span><span style=\"color: #637777;font-style: italic;\"> # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 12<\/span><span style=\"color: #637777;font-style: italic;\"> # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 13<\/span><span style=\"color: #637777;font-style: italic;\"> # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 14<\/span><span style=\"color: #637777;font-style: italic;\"> # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 15<\/span><span style=\"color: #637777;font-style: italic;\"> #<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">###<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 17<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 18<\/span><span style=\"color: #C792EA;font-style: italic;\">import<\/span><span> logging<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 19<\/span><span style=\"color: #C792EA;font-style: italic;\">import<\/span><span> hashlib<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 20<\/span><span style=\"color: #C792EA;font-style: italic;\">from<\/span><span> google.appengine.ext<\/span><span style=\"color: #C792EA;font-style: italic;\"> import<\/span><span> db<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 21<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 22<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 23<\/span><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> MagicProperty<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func=<\/span><span style=\"color: #FF5874;\">None<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> cache_prop=<\/span><span style=\"color: #FF5874;\">None<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> pass_instance=<\/span><span style=\"color: #FF5874;\">False<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 24<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> magic_func:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 25<\/span><span style=\"color: #637777;font-style: italic;\">        # No pants required.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 26<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cache_prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> pass_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 27<\/span><span style=\"color: #C792EA;font-style: italic;\">    else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 28<\/span><span style=\"color: #637777;font-style: italic;\">        # We are putting some pants on the function.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 29<\/span><span style=\"color: #C792EA;\">        def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> pants<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">magic_func<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 30<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cache_prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> pass_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 31<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> pants<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 32<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 33<\/span><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> _MagicDatastore<\/span><span>():<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 34<\/span><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><span style=\"color: #ECC48D;\">This is an internal class for _MagicProperty.<\/span><span style=\"color: #D9F5DD;\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 35<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 36<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __init__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> val<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 37<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.value<\/span><span style=\"color: #C792EA;\"> =<\/span><span> val<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 38<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 39<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> retval<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 40<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.value<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 41<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 42<\/span><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #C5E478;\">db<\/span><span>.<\/span><span style=\"color: #C5E478;\">Property<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 43<\/span><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty which will modify output based on a function that it is given.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 44<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 45<\/span><span style=\"color: #ECC48D;\">    This has several ways in which it may be called:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 46<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 47<\/span><span style=\"color: #ECC48D;\">    In this example we create the model, no caching is done, so any data that is returned from the datastore<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 48<\/span><span style=\"color: #ECC48D;\">    will be erased the first time that the MagicProperty is accessed and recomputed.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 49<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 50<\/span><span style=\"color: #ECC48D;\">        class MagicTest(db.Model):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 51<\/span><span style=\"color: #ECC48D;\">            title = db.StringProperty(required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 52<\/span><span style=\"color: #ECC48D;\">            chars = utils.MagicProperty(title, len, required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 53<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 54<\/span><span style=\"color: #ECC48D;\">        mytest = MagicTest(title=&quot;It was for the good of the school!&quot;)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 55<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 56<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 57<\/span><span style=\"color: #ECC48D;\">        It was for the good of the school!<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 58<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.chars<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 59<\/span><span style=\"color: #ECC48D;\">        34<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 60<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 61<\/span><span style=\"color: #ECC48D;\">        mytest = MagicTest.all().get()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 62<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 63<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 64<\/span><span style=\"color: #ECC48D;\">        Hello<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 65<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.chars\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 66<\/span><span style=\"color: #ECC48D;\">        5\t# Do note, this is recalculated the first time it is called, as long as title does not change it won&#39;t be recomputed.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 67<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 68<\/span><span style=\"color: #ECC48D;\">    In this example we create the model, and we also create a caching property so that even when we get values back <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 69<\/span><span style=\"color: #ECC48D;\">    from the datastore we use the cached computed value rather than running the function again. Do note that this requires overriding put()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 70<\/span><span style=\"color: #ECC48D;\">    to prime the cache as there is currently no way to specify that certain properties should be &quot;saved&quot; before others.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 71<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 72<\/span><span style=\"color: #ECC48D;\">        class MagicTesting(db.Model):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 73<\/span><span style=\"color: #ECC48D;\">            title = db.StringProperty(required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 74<\/span><span style=\"color: #ECC48D;\">            cache = db.UnindexedProperty()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 75<\/span><span style=\"color: #ECC48D;\">            chars = utils.MagicProperty(title, len, cache_prop=cache, required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 76<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 77<\/span><span style=\"color: #ECC48D;\">            def put(self, *args, **kw):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 78<\/span><span style=\"color: #ECC48D;\">                prime_cache = self.chars<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 79<\/span><span style=\"color: #ECC48D;\">                super(MagicTesting).put(*args, **kw)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 80<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 81<\/span><span style=\"color: #ECC48D;\">        mytesting = MagicTesting(title=&quot;Get the pocket knife out of my boot.&quot;)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 82<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 83<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 84<\/span><span style=\"color: #ECC48D;\">        Get the pocket knife out of my boot.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 85<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.chars<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 86<\/span><span style=\"color: #ECC48D;\">        36<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 87<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 88<\/span><span style=\"color: #ECC48D;\">        mytesting = MagicTest.all().get()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 89<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 90<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 91<\/span><span style=\"color: #ECC48D;\">        How are you?<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 92<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.chars\t<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 93<\/span><span style=\"color: #ECC48D;\">        12\t# This is not recomputed so long as the title has not changed, however it uses more datastore space to store a hash.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 94<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 95<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 96<\/span><span style=\"color: #ECC48D;\">    Inspired by: <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 97<\/span><span style=\"color: #ECC48D;\">    http:\/\/appengine-cookbook.appspot.com\/recipe\/custom-model-properties-are-cute<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 98<\/span><span style=\"color: #ECC48D;\">    http:\/\/code.google.com\/appengine\/articles\/extending_models.html<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 99<\/span><span style=\"color: #ECC48D;\">    http:\/\/googleappengine.blogspot.com\/2009\/07\/writing-custom-property-classes.html<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">100<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">101<\/span><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">102<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __init__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> cache_prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> pass_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">103<\/span><span style=\"color: #D9F5DD;\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">104<\/span><span style=\"color: #ECC48D;\">        Extra parameters you can give this initializer.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">105<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">106<\/span><span style=\"color: #ECC48D;\">            prop\t\t= Property to be acted upon<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">107<\/span><span style=\"color: #ECC48D;\">            magic_func\t= The function to be called when the property is accessed<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">108<\/span><span style=\"color: #ECC48D;\">            cache_prop\t= The property that can hold our cache, I suggest it is an db.UnindexedProperty() since it just stores sha1 hashes<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">109<\/span><span style=\"color: #D9F5DD;\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">110<\/span><span style=\"color: #C5E478;\">        super<\/span><span>(<\/span><span style=\"color: #82AAFF;\">_MagicProperty<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>).<\/span><span style=\"color: #C5E478;\">__init__<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">*<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">111<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_func<\/span><span style=\"color: #C792EA;\"> =<\/span><span> magic_func<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">112<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_prop<\/span><span style=\"color: #C792EA;\"> =<\/span><span> prop<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">113<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_cache<\/span><span style=\"color: #C792EA;\"> =<\/span><span> cache_prop<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">114<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_pass<\/span><span style=\"color: #C792EA;\"> =<\/span><span> pass_instance<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">115<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">116<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> get_cache_val<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> class_instance<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">117<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_cache<\/span><span style=\"color: #C792EA;\"> is not<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">118<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_cache.<\/span><span style=\"color: #C5E478;\">__get__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">119<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #C5E478;\"> getattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">orig<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><span style=\"color: #FF5874;\"> None<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">120<\/span><span>            <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">121<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> set_cache_val<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> val<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">122<\/span><span>        val<\/span><span style=\"color: #C792EA;\"> =<\/span><span> hashlib.<\/span><span style=\"color: #B2CCD6;\">sha1<\/span><span>(<\/span><span style=\"color: #82AAFF;\">val<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">hexdigest<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">123<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">124<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_cache<\/span><span style=\"color: #C792EA;\"> is not<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">125<\/span><span style=\"color: #8EACE3;\">            self<\/span><span>.magic_cache.<\/span><span style=\"color: #C5E478;\">__set__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> val<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">126<\/span><span style=\"color: #C5E478;\">        setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">orig<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><span style=\"color: #82AAFF;\"> val<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">127<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">128<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> attr_name<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">129<\/span><span style=\"color: #637777;font-style: italic;\">        # In google.appengine.ex.db there is an explicit warning not to use this method, so we test for it first.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">130<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>._attr_name:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">131<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">_attr_name<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">132<\/span><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">133<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">_<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.name<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">134<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">135<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __get__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> class_instance<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">136<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> model_instance<\/span><span style=\"color: #C792EA;\"> is<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">137<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">138<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">139<\/span><span>        cur<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_prop.<\/span><span style=\"color: #C5E478;\">__get__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">140<\/span><span>        cur<\/span><span style=\"color: #C792EA;\"> =<\/span><span> cur.<\/span><span style=\"color: #B2CCD6;\">encode<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">utf-8<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>)\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">141<\/span><span>        last<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">get_cache_val<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">142<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> last<\/span><span style=\"color: #C792EA;\"> ==<\/span><span> hashlib.<\/span><span style=\"color: #B2CCD6;\">sha1<\/span><span>(<\/span><span style=\"color: #82AAFF;\">cur<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">hexdigest<\/span><span>():<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">143<\/span><span>            logging.<\/span><span style=\"color: #B2CCD6;\">info<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Cache hit: <\/span><span style=\"color: #82AAFF;\">%s<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> %<\/span><span style=\"color: #82AAFF;\"> (cur)<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">144<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #C5E478;\"> getattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #FF5874;\"> None<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">145<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">146<\/span><span>        logging.<\/span><span style=\"color: #B2CCD6;\">info<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Cache miss: <\/span><span style=\"color: #82AAFF;\">%s<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> %<\/span><span style=\"color: #82AAFF;\"> (cur)<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">147<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">148<\/span><span>        magic_done<\/span><span style=\"color: #C792EA;\"> = u<\/span><span style=\"color: #D9F5DD;\">&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">149<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_pass:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">150<\/span><span>            magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">magic_func<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cur<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">151<\/span><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">152<\/span><span>            magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">magic_func<\/span><span>(<\/span><span style=\"color: #82AAFF;\">cur<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">153<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">154<\/span><span style=\"color: #637777;font-style: italic;\">        # Set the attribute in the model<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">155<\/span><span style=\"color: #C5E478;\">        setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_done<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">156<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">set_cache_val<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cur<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">157<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">158<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> magic_done<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">159<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">160<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">161<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __set__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> value<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">162<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #C5E478;\"> isinstance<\/span><span>(<\/span><span style=\"color: #82AAFF;\">value<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> _MagicDatastore<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">163<\/span><span style=\"color: #C5E478;\">            setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> value.<\/span><span style=\"color: #B2CCD6;\">retval<\/span><span>())<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">164<\/span><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">165<\/span><span style=\"color: #C792EA;font-style: italic;\">            raise<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">DerivedPropertyError<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty is magic. Magic may not be modified.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">166<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">167<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> make_value_from_datastore<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> value<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">168<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #B2CCD6;\"> _MagicDatastore<\/span><span>(<\/span><span style=\"color: #82AAFF;\">value<\/span><span>)<\/span><\/span><\/code><\/pre>"},{"title":"Auto-updating Properties in Google AppEngine Models","published":"2010-02-21T04:03:42+00:00","updated":"2010-03-24T00:00:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2010\/auto-updating-properties-in-google-appengine-models\/"}},"id":"https:\/\/funcptr.net\/2010\/auto-updating-properties-in-google-appengine-models\/","content":"<p>EDIT: I have an updated version of this code available in a newer post, please\ngo to <a href=\"\/2010\/magic-properties-on-google-appengine\/\">Magic Properties on Google AppEngine<\/a> for the updated information.\nFor the generic information that has not changed, and the idea behind the code\nplease continue below.<\/p>\n<hr \/>\n<p>In Google AppEngine a model may contain many different properties, from this\nmodel you can generate a form using djangoforms. This is all good and well\nuntil you have a required property on a model that needs to have part of it be\ncalculated based upon something the user types in. For example, a blog entry\ngenerally has a \"slug\" which is generated from the title, now in Google\nAppEngine's models, if a property is required it has to be set at object\ncreation time.<\/p>\n<p>Djangoforms has a <code>form.save(committed=False)<\/code> that allows one to get back an\nunsaved <code>Model<\/code>, however since slug is not in the form (since it is auto\ngenerated) it cannot be set at model creation time. Even adding a hidden field\nto the form with the name of the property is not going to satisfy the model\nsince a value of <code>None<\/code> is considered to be empty, which means it does not meet\nthe <code>required=True<\/code> status.<\/p>\n<p>After trying a multitude of different options I ran across an <a rel=\"external\" href=\"http:\/\/code.google.com\/appengine\/articles\/extending_models.html\">article about\nextending models<\/a> that specifically talked about adding custom properties\nthat were saved into the DB, and could be searched on, this got me digging and\nI found <a rel=\"external\" href=\"http:\/\/googleappengine.blogspot.com\/2009\/07\/writing-custom-property-classes.html\">a blog post on the Google AppEngine blogspot<\/a> about the exact same\nthing, using yet again a different example. The one that got me on the right\ntrack was the following post by <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20090517114229\/http:\/\/appengine-cookbook.appspot.com\/recipe\/custom-model-properties-are-cute\/\">Rodrigo Moraes on his custom model\nproperties<\/a>.<\/p>\n<p>I came up with the following fairly quickly after also taking a look at\n<code>googleappengine.ext.db<\/code> within the Google AppEngine framework, however there is\nstill an issue, for now the <code>MagicProperty<\/code> is only updated the first time the\nproperty it affects is set. I am not yet sure how to work around that, I will\nhave to do some more digging, maybe there is a way to get notified if another\nproperty gets modified or updated.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">import<\/span><span> logging<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">from<\/span><span> google.appengine.ext<\/span><span style=\"color: #C792EA;font-style: italic;\"> import<\/span><span> db<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> MagicProperty<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func=<\/span><span style=\"color: #FF5874;\">None<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> magic_func:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # No pants required.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # We are putting some pants on the function.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> pants<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">magic_func<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> pants<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #C5E478;\">db<\/span><span>.<\/span><span style=\"color: #C5E478;\">Property<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty which will modify output based on a function that it is given.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    Inspired by: <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    http:\/\/appengine-cookbook.appspot.com\/recipe\/custom-model-properties-are-cute<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    http:\/\/code.google.com\/appengine\/articles\/extending_models.html<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    http:\/\/googleappengine.blogspot.com\/2009\/07\/writing-custom-property-classes.html<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __init__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        super<\/span><span>(<\/span><span style=\"color: #82AAFF;\">_MagicProperty<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>).<\/span><span style=\"color: #C5E478;\">__init__<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">*<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_func<\/span><span style=\"color: #C792EA;\"> =<\/span><span> magic_func<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_prop<\/span><span style=\"color: #C792EA;\"> =<\/span><span> prop<\/span><\/span>\n<span class=\"giallo-l\"><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> attr_name<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # In google.appengine.ex.db there is an explicit warning not to use this method, so we test for it first.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>._attr_name:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">_attr_name<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">_<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.name<\/span><\/span>\n<span class=\"giallo-l\"><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __get__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> class_instance<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> model_instance<\/span><span style=\"color: #C792EA;\"> is<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><\/span>\n<span class=\"giallo-l\"><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        #<\/span><span style=\"color: #C792EA;\"> TODO<\/span><span style=\"color: #637777;font-style: italic;\">: If the property that this one is functioning on is changed, we need a way to know that so that we recalculate<\/span><\/span>\n<span class=\"giallo-l\"><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # Original __get__ in google.appengine.ext.db has getattr to retrieve the value from the model instance<\/span><\/span>\n<span class=\"giallo-l\"><span>        magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> getattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #FF5874;\"> None<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> magic_done<\/span><span style=\"color: #C792EA;\"> is<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>            magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">magic_func<\/span><span>(<\/span><span style=\"color: #8EACE3;\">self<\/span><span style=\"color: #82AAFF;\">.magic_prop.<\/span><span style=\"color: #C5E478;\">__get__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>))<\/span><\/span>\n<span class=\"giallo-l\"><span>            <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">            # Set the attribute in the model<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">            setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">_attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_done<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> magic_done<\/span><\/span>\n<span class=\"giallo-l\"><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __set__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        raise<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">DerivedPropertyError<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty is magic. Magic may not be modified.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span><\/code><\/pre>\n<p>The approach that the <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20100208035715\/http:\/\/code.google.com\/appengine\/docs\/python\/datastore\/typesandpropertyclasses.html#DateTimeProperty\">DateTimeProperty<\/a> takes is one that I can't use\nbecause my class is dependant upon an external property, not whether or not a\nvalue was set when the object was instantiated.<\/p>\n<p>Google AppEngine is posing some rather interesting challenges. Although, for\nslugs it may not be entirely bad that it can only be set once, as that means\ntitle changes on the article won't cause the slug to change possibly breaking\nold links. This definitely requires more research and more thinking.<\/p>\n"},{"title":"Property class on AppEngine does not support a tuple for choices","published":"2010-02-15T04:56:59+00:00","updated":"2010-02-15T04:56:59+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2010\/property-class-on-google-appengine-not-supporting-a-tuple-of-choices\/"}},"id":"https:\/\/funcptr.net\/2010\/property-class-on-google-appengine-not-supporting-a-tuple-of-choices\/","content":"<p><a rel=\"external\" href=\"http:\/\/code.google.com\/appengine\/\">Google AppEngine<\/a> does not allow one to set the choices field of a <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20100212221437\/http:\/\/code.google.com\/appengine\/docs\/python\/datastore\/propertyclass.html#Property\">model\nproperty<\/a><\/a> to a tuple; this workaround will have have a form give the\nuser the choices with the user friendly items but have the backend store the\nnon-user friendly version.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># Set up our non-user friendly and user friendly choices<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;\">REDIRECT_CHOICES<\/span><span style=\"color: #C792EA;\"> =<\/span><span> (<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">301<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">301 - Permanent Redirect<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">302<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">302 - Temporary Redirect<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">403<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">403 - Access Denied<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">404<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">404 - File Not Found<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">500<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">500 - Internal Server Error<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span>)<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># Sample model that has the required Property with the choices set to the<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># non-user friendly values<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> Shorturl<\/span><span>(<\/span><span style=\"color: #C5E478;\">db<\/span><span>.<\/span><span style=\"color: #C5E478;\">Model<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span>    uripath<\/span><span style=\"color: #C792EA;\"> =<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">StringProperty<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">verbose_name<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Path<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><span style=\"color: #D7DBE0;\"> required<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #FF5874;\">True<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span>    httpcode<\/span><span style=\"color: #C792EA;\"> =<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">IntegerProperty<\/span><span>(<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        verbose_name<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">HTTP code<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        required<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #FF5874;\">True<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        default<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #F78C6C;\">301<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        choices<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">[<\/span><span style=\"color: #82AAFF;\">x<\/span><span>[<\/span><span style=\"color: #F78C6C;\">0<\/span><span>]<\/span><span style=\"color: #C792EA;font-style: italic;\"> for<\/span><span style=\"color: #82AAFF;\"> x<\/span><span style=\"color: #C792EA;font-style: italic;\"> in<\/span><span style=\"color: #82AAFF;\"> REDIRECT_CHOICES<\/span><span style=\"color: #D9F5DD;\">]<\/span><\/span>\n<span class=\"giallo-l\"><span>    )<\/span><\/span>\n<span class=\"giallo-l\"><span>    location<\/span><span style=\"color: #C792EA;\"> =<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">StringProperty<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">verbose_name<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Location<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># This is our djangoform that has the choices set as required so that the user<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># gets the user friendly version and not just simply the numbers.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> ShorturlForm<\/span><span>(<\/span><span style=\"color: #C5E478;\">djangoforms<\/span><span>.<\/span><span style=\"color: #C5E478;\">ModelForm<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span>    httpcode<\/span><span style=\"color: #C792EA;\"> =<\/span><span> forms.<\/span><span style=\"color: #B2CCD6;\">IntegerField<\/span><span>(<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        widget<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;\">forms.<\/span><span style=\"color: #B2CCD6;\">Select<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">choices<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;\">REDIRECT_CHOICES<\/span><span>)<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        required<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #FF5874;\">True<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        label<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">HTTP code<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span>\n<span class=\"giallo-l\"><span>    )<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    class<\/span><span style=\"color: #FFCB8B;\"> Meta<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>        model<\/span><span style=\"color: #C792EA;\"> =<\/span><span> Shorturl<\/span><\/span><\/code><\/pre>\n<p>Given the above tuple it will set the choices in the model for Google AppEngine\nand pass it to the field on the Django form allowing user friendly output. This\nwill allow the user to pick the right redirect choice without having to know\nthat various different redirect codes or have to look them up.<\/p>\n"},{"title":"addnumbers.asm updated","published":"2008-02-03T09:59:04+00:00","updated":"2008-02-03T09:59:04+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2008\/addnumbers-asm-updated\/"}},"id":"https:\/\/funcptr.net\/2008\/addnumbers-asm-updated\/","content":"<p>If you have no clue what I am talking about, check out <a href=\"\/2008\/introduction-to-intel-assembly\/\">my previous post<\/a>.\nI have fixed the flaws, and now it will parse the commands it is handed on\nthe command line, and stick them into an integer. It will now print it to\nstdout.<\/p>\n<p>This has been an awesome learning experience for me. Especially with regards\nto how to do recursion, how to debug a pure assembly program with gdb and\nwhatnot.<\/p>\n<p>Here comes the code, as I said before, see my previous thread if you don't\nknow what I am talking about, as well as instructions on how to compile the\nprogram.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"asm\"><span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; File: addnumbers2.asm<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Delta Regeer<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; 2008-01-27<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Function:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tAdd numbers together that are provided as arguments to the program in argv[1] and argv[2].<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Known limitations:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tThis will hopefully be fixed in the next revision. Floating point numbers will not work.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tAny input that is larger than an integer will cause overflows, and thus will not work.<\/span><\/span>\n<span class=\"giallo-l\"><span> <\/span><\/span>\n<span class=\"giallo-l\"><span>section .data<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; Define some strings that are going to be used throughout the program<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This string is to let the user know they failed to provide the proper amount of arguments.<\/span><\/span>\n<span class=\"giallo-l\"><span>args\t<\/span><span style=\"color: #C792EA;\">db<\/span><span>\t&quot;Program <\/span><span style=\"color: #82AAFF;font-style: italic;\">addnumbers<\/span><span style=\"color: #82AAFF;\">:<\/span><span> &quot;, <\/span><span style=\"color: #F78C6C;\">0xa<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x9<\/span><span>, &quot;addnumbers &lt;number <\/span><span style=\"color: #F78C6C;\">1<\/span><span>&gt; &lt;number <\/span><span style=\"color: #F78C6C;\">2<\/span><span>&gt;&quot;, <\/span><span style=\"color: #F78C6C;\">0xa<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x9<\/span><span>, &quot;Arguments <\/span><span style=\"color: #F78C6C;\">1<\/span><span style=\"color: #7FDBCA;\"> and<\/span><span style=\"color: #F78C6C;\"> 2<\/span><span> are required.&quot;, <\/span><span style=\"color: #F78C6C;\">0xa<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x9<\/span><span>, &quot;Anything that will cause addition to overflow an <\/span><span style=\"color: #7FDBCA;\">int<\/span><span> (<\/span><span style=\"color: #F78C6C;\">2<\/span><span>,<\/span><span style=\"color: #F78C6C;\">147<\/span><span>,<\/span><span style=\"color: #F78C6C;\">483<\/span><span>,<\/span><span style=\"color: #F78C6C;\">647<\/span><span>), will fail! :P&quot;, <\/span><span style=\"color: #F78C6C;\">0xa<\/span><\/span>\n<span class=\"giallo-l\"><span>largs\t<\/span><span style=\"color: #C5E478;\">equ<\/span><span>\t$ - args<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This string contains part of the output that we are going to send to the terminal. The last two<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; bytes will be filled automatically by the program, before it is output to stdout.<\/span><\/span>\n<span class=\"giallo-l\"><span>msg\t<\/span><span style=\"color: #C792EA;\">db<\/span><span>\t&#39;<\/span><span style=\"color: #82AAFF;font-style: italic;\">Answer<\/span><span style=\"color: #82AAFF;\">:<\/span><span> &#39;,\t<\/span><span style=\"color: #F78C6C;\">0<\/span><\/span>\n<span class=\"giallo-l\"><span>lmsg\t<\/span><span style=\"color: #C5E478;\">equ<\/span><span>\t$ - msg<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>num1\t<\/span><span style=\"color: #C792EA;\">dd<\/span><span style=\"color: #F78C6C;\">\t0<\/span><\/span>\n<span class=\"giallo-l\"><span>num2\t<\/span><span style=\"color: #C792EA;\">dd<\/span><span style=\"color: #F78C6C;\">\t0<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>section .bss<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This is where I am going to store the output of my conversion from an integer to a char<\/span><\/span>\n<span class=\"giallo-l\"><span>answer\t<\/span><span style=\"color: #C792EA;\">resb<\/span><span style=\"color: #F78C6C;\">\t64<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>section .text<\/span><\/span>\n<span class=\"giallo-l\"><span>global start\t\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Linker defined entry point. Mac OS X this is start. <\/span><\/span>\n<span class=\"giallo-l\"><span>global _start\t\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; FreeBSD and others _start.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">_start<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">esp<\/span><span style=\"color: #637777;font-style: italic;\"> \t\t; Set up the stack frame<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">ebp<\/span><span> + <\/span><span style=\"color: #F78C6C;\">4<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Get argc, we check if it set to at least 3<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Put the base pointer into edx, so we can use that in <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t\t; our dereferences coming up<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span>, <\/span><span style=\"color: #F78C6C;\">8<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Add 8. We want to skip ebp and argc<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcmp<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span>, <\/span><span style=\"color: #F78C6C;\">3<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Check if we have at least 3 arguments to the program. <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t\t; At least two arguments are required, and the 3rd one is <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t\t; the name of the program<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjl<\/span><span>\texit\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; If the value in ecx is less than 3, jump to exit<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tesi<\/span><span>, <\/span><span style=\"color: #F78C6C;\">1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Set the index to 1<\/span><\/span>\n<span class=\"giallo-l\"><span>\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">edx<\/span><span> + <\/span><span style=\"color: #82AAFF;\">esi<\/span><span> * <\/span><span style=\"color: #F78C6C;\">4<\/span><span>]\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the pointer to the character array into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push eax onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span>\tnum1\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the pointer to num1 onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcall<\/span><span>\tctoi\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Call my char to int function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 8<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Put the stack pointer back to where it was.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tinc<\/span><span style=\"color: #82AAFF;\">\tesi<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Increase the index<\/span><\/span>\n<span class=\"giallo-l\"><span>\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">edx<\/span><span> + <\/span><span style=\"color: #82AAFF;\">esi<\/span><span> * <\/span><span style=\"color: #F78C6C;\">4<\/span><span>]\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the pointer to the character array into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push eax onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span>\tnum2\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the pointer to num2 onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcall<\/span><span>\tctoi\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Call my char to int function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 8<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Put the stack pointer back to where it was.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [num1]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move value stored in num1 into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [num2]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Add num2 to eax, this will now be stored in eax<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the new calculated number onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcall<\/span><span>\titoa\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Convert the integer to a character array<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> lmsg\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the length of the string<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span>\tmsg\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the location of the string in memory<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the file descriptor to write to<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Clean up the stack<\/span><\/span>\n<span class=\"giallo-l\"><span> <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span>\tanswer\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push answer onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcall<\/span><span>\tlen\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Get it&#39;s length<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tedi<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the length onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span>\tanswer\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the pointer to the character string onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the file descriptor to write to<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Clean up the stack<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjmp<\/span><span>\tdone\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Program is done. Jump to done<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">exit<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This label is jumped to when we want to exit the program and let the user know how<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; to run the program. Like for instance what paramaters to send the program.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; Call sys_write<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> largs\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the length of the string<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> args\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the location of the string in memory<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the file descriptor to write to<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Clean up the stack<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">done<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This is the label we jump to when we want to exit the program, we set the exit code<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; to 0.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; Call sys_exit<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x0<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the value to return to the operating system<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; We never return to this function, so no need to clean the stack.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">ctoi<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; char to i. We actually convert entire character array&#39;s to integers.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; We get two paramaters on the stack. The first one we grab is the pointer to the place to store<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; the number. The second is the pointer to the character array.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the old base pointer onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">esp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Create a new base pointer<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tesi<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Store all the original registers<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span>\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push edx, so that we can overwrite it<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tsub<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #F78C6C;\">4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; We get another storage space on the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[<\/span><span style=\"color: #82AAFF;\">esp<\/span><span>], <\/span><span style=\"color: #C792EA;\">dword<\/span><span style=\"color: #F78C6C;\"> 10<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; This is the number we are going to multiply by<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">ebp<\/span><span> + <\/span><span style=\"color: #F78C6C;\">12<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the pointer to the character array into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the pointer to the character array onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcall<\/span><span>\tlen\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Call the string length versoin<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Reclaim the space we lost when we pushed eax onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span>\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">ebp<\/span><span> + <\/span><span style=\"color: #F78C6C;\">8<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; This is where we are going to store the numbers<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tesi<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">ebp<\/span><span> + <\/span><span style=\"color: #F78C6C;\">12<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; This is the pointer to the character array<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmovzx<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span>, <\/span><span style=\"color: #82AAFF;\">di<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; move with extended zero edi.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tedi<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Clean up edi<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">\tctoi_loop<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">ebx<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the value stored in ebx into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmul<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> [<\/span><span style=\"color: #82AAFF;\">esp<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move it over a 10s place.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[<\/span><span style=\"color: #82AAFF;\">ebx<\/span><span>], <\/span><span style=\"color: #82AAFF;\">eax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Move the new number back into ebx<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmovzx<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span> [<\/span><span style=\"color: #82AAFF;\">esi<\/span><span> + <\/span><span style=\"color: #82AAFF;\">edi<\/span><span>]\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the character into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmovsx<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #82AAFF;\">al<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; We just want the lower part of the character<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tsub<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x30<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Subtract 0x30, ASCII 0 so that it is an actual number<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span>\t[<\/span><span style=\"color: #82AAFF;\">ebx<\/span><span>], <\/span><span style=\"color: #82AAFF;\">eax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Add the new number to the old number that has been multiplied by 10<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tinc<\/span><span style=\"color: #82AAFF;\">\tedi<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Increase the counter<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tloop<\/span><span> ctoi_loop\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Loop into cx is 0<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 4<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Restore all the registers<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\teax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tesi<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Make esp the original base pointer again<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Pop the original base pointer into the register<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tret<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; Return caller<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">itoa<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; Recursive function. This is going to convert the integer to the character.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Setup a new stack frame<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">esp<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Save the registers<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, [<\/span><span style=\"color: #82AAFF;\">ebp<\/span><span> + <\/span><span style=\"color: #F78C6C;\">8<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; eax is going to contain the integer<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><span>, <\/span><span style=\"color: #C792EA;\">dword<\/span><span style=\"color: #F78C6C;\"> 10<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; This is our &quot;stop&quot; value as well as our value to divide with<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span>, answer\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Put a pointer to answer into ecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push ebx on the field for our &quot;stop&quot; value<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">\titoa_loop<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcmp<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Compare eax, and ebx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjl<\/span><span>\titoa_unroll\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Jump if eax is less than ebx (which is 10)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\txor<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span>, <\/span><span style=\"color: #82AAFF;\">edx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Clear edx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tdiv<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Divide by ebx (10)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the remainder onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjmp<\/span><span>\titoa_loop\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Jump back to the top of the loop<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">\titoa_unroll<\/span><span style=\"color: #82AAFF;\">:<\/span><span>\t\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tal<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x30<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Add 0x30 to the bottom part of eax to make it an ASCII char<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[<\/span><span style=\"color: #82AAFF;\">ecx<\/span><span>], <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #82AAFF;\"> al<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Move the ASCII char into the memory references by ecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tinc<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Increment ecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Pop the next variable from the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcmp<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Compare if eax is ebx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjne<\/span><span>\titoa_unroll\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; If they are not equal, we jump back to the unroll loop<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; else we are done, and we execute the next few commands<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[<\/span><span style=\"color: #82AAFF;\">ecx<\/span><span>], <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 0xa<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Add a newline character to the end of the character array<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tinc<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Increment ecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[<\/span><span style=\"color: #82AAFF;\">ecx<\/span><span>], <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Add a null byte to ecx, so that when we pass it to our<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; len function it will properly give us a length<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tedx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Restore registers<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\teax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebp<\/span><span>\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tret<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">len<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; Returns the length of a string. The string has to be null terminated. Otherwise this function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; will fail miserably. <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; Upon return. edi will contain the length of the string.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Save the previous stack pointer. We restore it on return<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">esp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; We setup a new stack frame<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Save registers we are going to use. edi returns the length of the string<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\"> \tecx<\/span><\/span>\n<span class=\"giallo-l\"><span>\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span>,  [<\/span><span style=\"color: #82AAFF;\">ebp<\/span><span> + <\/span><span style=\"color: #F78C6C;\">8<\/span><span>]\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the pointer to eax; we want an offset of one, to jump over the return address<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tedi<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Set the counter to 0. We are going to increment this each loop<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">\tlen_loop<\/span><span style=\"color: #82AAFF;\">:<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Just a quick label to jump to<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmovzx<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span> [<\/span><span style=\"color: #82AAFF;\">ecx<\/span><span> + <\/span><span style=\"color: #82AAFF;\">edi<\/span><span>]\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the character to eax.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmovsx<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #82AAFF;\">al<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move al to eax. al is part of eax.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tinc<\/span><span style=\"color: #82AAFF;\">\tdi<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Increase di.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tcmp<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Compare eax to 0.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjnz<\/span><span> \tlen_loop\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; If it is not zero, we jump back to len_loop and repeat.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tdec<\/span><span style=\"color: #82AAFF;\">\tdi<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Remove one from the count<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Restore registers<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\teax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Set esp back to what ebp used to be.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tebp<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Restore the stack frame<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tret<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; Return to caller<\/span><\/span><\/code><\/pre>"},{"title":"Introduction to Intel Assembly","published":"2008-01-27T03:42:44+00:00","updated":"2008-01-27T03:42:44+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2008\/introduction-to-intel-assembly\/"}},"id":"https:\/\/funcptr.net\/2008\/introduction-to-intel-assembly\/","content":"<p>This semester I am taking a class on Intel assembly, because I want more of an\ninsight into how the computer works, and it will allow me to better reverse\nengineer new viruses and spyware. The class is also required if one is a\nSoftware Engineering major, so that means I have to take it.<\/p>\n<p>We had our first assignment. Sum two numbers and then output them to the\nscreen. We were supposed to write inline assembly using Visual Studio C++,\nbut if we are to do an assembly class, then we should learn how to write\nassembly, not have some parts assembly and other parts the compiler. Sure\nit makes it easy as you will get immediate access to the standard C\nlibrary, but if you want that, you can just link against it.<\/p>\n<p>The following code examples were written on Mac OS X, and will work on\nFreeBSD. Linux uses a different calling convention for its syscalls, and as\nsuch this code will not run on Linux, unless it is modified. Do note, you\nneed an Intel Mac for this to work. This is Intel assembly.<\/p>\n<p>Compile the code with (Mac OS X):<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"shellscript\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">nasm<\/span><span style=\"color: #82AAFF;\"> -f<\/span><span style=\"color: #ECC48D;\"> macho addnumbers.asm<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">ld<\/span><span style=\"color: #82AAFF;\"> -o<\/span><span style=\"color: #ECC48D;\"> addnumbers addnumbers.o<\/span><\/span><\/code><\/pre>\n<p>or (FreeBSD):<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"shellscript\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">nasm<\/span><span style=\"color: #82AAFF;\"> -f<\/span><span style=\"color: #ECC48D;\"> elf addnumbers.asm<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">ld<\/span><span style=\"color: #82AAFF;\"> -o<\/span><span style=\"color: #ECC48D;\"> addnumbers addnumbers.o<\/span><\/span><\/code><\/pre>\n<p>Then you can run it with:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"shellscript\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">.\/addnumbers<\/span><span style=\"color: #F78C6C;\"> 1 5<\/span><\/span><\/code><\/pre>\n<p>As you can see in the comments of the source code, there are still some\nlimitations, but the rest of the source code should be made readable by the\ncomments that are provided.<\/p>\n<p>Porting to Linux: Please double check that all the syscall numbers are the\nsame. There are some differences between Linux and FreeBSD\/Mac OS X in that\nregard.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"asm\"><span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; File: addnumbers.asm<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Delta Regeer<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; 2008-01-27<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Function:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tAdd numbers together that are provided as arguments to the program in argv[1] and argv[2].<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Known limitations:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tAs of right now, the numbers that are provided may not add up to anything more than 9.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tThis will hopefully be fixed in the next revision. Floating point numbers will not work.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Todo:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tWrite conversion routine, to convert a string of numbers into a real integer on which<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">;\tmath may be performed.<\/span><\/span>\n<span class=\"giallo-l\"><span> <\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>section .data<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; Define some strings that are going to be used throughout the program<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This string is to let the user know they failed to provide the proper amount of arguments.<\/span><\/span>\n<span class=\"giallo-l\"><span>args\t<\/span><span style=\"color: #C792EA;\">db<\/span><span>\t&quot;You failed to provide the proper amount of arguments&quot;, <\/span><span style=\"color: #F78C6C;\">0xa<\/span><\/span>\n<span class=\"giallo-l\"><span>largs\t<\/span><span style=\"color: #C5E478;\">equ<\/span><span>\t$ - args<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; This string contains part of the output that we are going to send to the terminal. The last two<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; bytes will be filled automatically by the program, before it is output to stdout.<\/span><\/span>\n<span class=\"giallo-l\"><span>msg\t<\/span><span style=\"color: #C792EA;\">db<\/span><span>\t&#39;<\/span><span style=\"color: #82AAFF;font-style: italic;\">Answer<\/span><span style=\"color: #82AAFF;\">:<\/span><span> &#39;,\t<\/span><span style=\"color: #F78C6C;\">0<\/span><span>,\t<\/span><span style=\"color: #F78C6C;\">0<\/span><\/span>\n<span class=\"giallo-l\"><span>lmsg\t<\/span><span style=\"color: #C5E478;\">equ<\/span><span>\t$ - msg<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>section .text<\/span><\/span>\n<span class=\"giallo-l\"><span>global start\t\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Linker defined entry point. Mac OS X this is start. FreeBSD and others _start.<\/span><\/span>\n<span class=\"giallo-l\"><span>global _start<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">_start<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">; Start the program here.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 8<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; We don&#39;t care about argc or argv[0]<\/span><\/span>\n<span class=\"giallo-l\"><span>\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\"> \tecx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Get the first argument or argv[1]<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjecxz<\/span><span>\texit\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; If there was no argument. Exit. Let the user know why<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; Change the number from a character to an actual dword<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #C792EA;\">dword<\/span><span> [<\/span><span style=\"color: #82AAFF;\">ecx<\/span><span>]\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the character into eax so we can manipulate it<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tsub<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x30<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Remove 0x30 from the character. To make it an actual number, not an ASCII number.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpop<\/span><span style=\"color: #82AAFF;\">\tecx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; get the second argument or argv[2]<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjecxz<\/span><span>\texit\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; If there was no second argument. Exit. Let the user know why<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><span>, <\/span><span style=\"color: #C792EA;\">dword<\/span><span> [<\/span><span style=\"color: #82AAFF;\">ecx<\/span><span>]\t<\/span><span style=\"color: #637777;font-style: italic;\">; Move the character into ebx so we can manipulate it<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tsub<\/span><span style=\"color: #82AAFF;\">\tebx<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x30<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Remove 0x30 from the character. To make it an actual number, not an ASCII number.<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #82AAFF;\">ebx<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Add the two numbers together<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>, <\/span><span style=\"color: #F78C6C;\">0x30<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Make it an ASCII number again<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[msg+lmsg-<\/span><span style=\"color: #F78C6C;\">2<\/span><span>], <\/span><span style=\"color: #82AAFF;\">eax<\/span><span style=\"color: #637777;font-style: italic;\">\t; Replace the null character in the msg with the answer<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span>\t[msg+lmsg-<\/span><span style=\"color: #F78C6C;\">1<\/span><span>], <\/span><span style=\"color: #C792EA;\">dword<\/span><span style=\"color: #F78C6C;\"> 0xa<\/span><span style=\"color: #637777;font-style: italic;\">\t; Add an newline character so that when it spits it out it is neatly formatted<\/span><\/span>\n<span class=\"giallo-l\"><span>\t\t\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; Call sys_write<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> lmsg\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the length of the string<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span>\tmsg\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the location of the string in memory<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the file descriptor to write to<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Advance the stack pointer<\/span><\/span>\n<span class=\"giallo-l\"><span> <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tjmp<\/span><span>\tdone\t\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Program is done. Jump to done<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">exit<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; Call sys_write<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> largs\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the length of the string<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span> args\t\t<\/span><span style=\"color: #637777;font-style: italic;\">; Push the location of the string in memory<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the file descriptor to write to<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">4<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tadd<\/span><span style=\"color: #82AAFF;\">\tesp<\/span><span>, <\/span><span style=\"color: #C792EA;\">byte<\/span><span style=\"color: #F78C6C;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Advance esp past the part we were just at<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">done<\/span><span style=\"color: #82AAFF;\">:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t\t\t\t; sys_exit<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #C792EA;\">\tdword<\/span><span style=\"color: #F78C6C;\"> 0x1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t; Push the value to return to the operating system<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tmov<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span>,<\/span><span style=\"color: #F78C6C;\">1<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Move the syscall number into eax<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tpush<\/span><span style=\"color: #82AAFF;\">\teax<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Push the syscall onto the stack<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">\tint<\/span><span style=\"color: #F78C6C;\">\t0x80<\/span><span style=\"color: #637777;font-style: italic;\">\t\t\t; Interrupt 80, go to kernel<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\t; We never return to this function, so no need to clean the stack.<\/span><\/span><\/code><\/pre>"},{"title":"X11 mouse button pressed","published":"2007-03-16T04:51:34+00:00","updated":"2007-03-16T04:51:34+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2007\/x11-mouse-button-pressed\/"}},"id":"https:\/\/funcptr.net\/2007\/x11-mouse-button-pressed\/","content":"<p>For a Rube Goldberg device I needed a way to know if a mouse button was\npressed on the root window of an X11 screen. Here is the code for it:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"c\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">stdio.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">stdlib.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">string.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">unistd.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">X11\/Xlib.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">X11\/Xutil.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #82AAFF;font-style: italic;\"> main<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        Display <\/span><span style=\"color: #7FDBCA;\">*<\/span><span>dsp <\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;font-style: italic;\"> XOpenDisplay<\/span><span style=\"color: #82AAFF;\">( NULL )<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span>(<\/span><span style=\"color: #C792EA;\"> !<\/span><span>dsp ){<\/span><span style=\"color: #C792EA;font-style: italic;\"> return<\/span><span style=\"color: #F78C6C;\"> 1<\/span><span>; }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        int<\/span><span> screenNumber <\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;font-style: italic;\"> DefaultScreen<\/span><span style=\"color: #82AAFF;\">(dsp)<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        long<\/span><span> eventMask <\/span><span style=\"color: #C792EA;\">=<\/span><span> StructureNotifyMask;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">        XSelectInput<\/span><span style=\"color: #82AAFF;\">( dsp,<\/span><span style=\"color: #82AAFF;font-style: italic;\"> DefaultRootWindow<\/span><span style=\"color: #82AAFF;\">(dsp), eventMask )<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        XEvent evt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        eventMask <\/span><span style=\"color: #C792EA;\">=<\/span><span> ButtonPressMask<\/span><span style=\"color: #7FDBCA;\">|<\/span><span>ButtonReleaseMask;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">        XSelectInput<\/span><span style=\"color: #82AAFF;\">(dsp,<\/span><span style=\"color: #82AAFF;font-style: italic;\"> DefaultRootWindow<\/span><span style=\"color: #82AAFF;\">(dsp), eventMask)<\/span><span>;<\/span><span style=\"color: #637777;font-style: italic;\"> \/\/ override prev<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        do<\/span><span>{<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">                XNextEvent<\/span><span style=\"color: #82AAFF;\">( dsp,<\/span><span style=\"color: #7FDBCA;\"> &amp;<\/span><span style=\"color: #82AAFF;\">evt )<\/span><span>;<\/span><span style=\"color: #637777;font-style: italic;\">   \/\/ calls XFlush()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">                if<\/span><span> ( (<\/span><span style=\"color: #C5E478;\">evt<\/span><span>.<\/span><span style=\"color: #C5E478;\">type<\/span><span style=\"color: #C792EA;\"> ==<\/span><span> ButtonRelease)<\/span><span style=\"color: #C792EA;\"> ||<\/span><span> (<\/span><span style=\"color: #C5E478;\">evt<\/span><span>.<\/span><span style=\"color: #C5E478;\">type<\/span><span style=\"color: #C792EA;\"> ==<\/span><span> ButtonPress) )<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">                exit<\/span><span style=\"color: #82AAFF;\">(<\/span><span style=\"color: #F78C6C;\">2<\/span><span style=\"color: #82AAFF;\">)<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        }<\/span><span style=\"color: #C792EA;font-style: italic;\"> while<\/span><span>(<\/span><span style=\"color: #F78C6C;\"> 1<\/span><span> );<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">return<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span><\/code><\/pre>\n<p>Compile:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"shellscript\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">gcc<\/span><span style=\"color: #82AAFF;\"> -lX11 -L\/usr\/local\/include<\/span><span style=\"color: #ECC48D;\"> x11.c<\/span><span style=\"color: #82AAFF;\"> -o<\/span><span style=\"color: #ECC48D;\"> x11-mousebutton<\/span><\/span><\/code><\/pre>\n<p>Upon a mouse button being pressed it will exit 2, so using that return value\nin some script you can execute a command. For example, to open a CD rom\ndrive so a domino falls over.<\/p>\n"}]}