<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Untitled &#187; networking</title>
	<atom:link href="http://ammonlauritzen.com/blog/tag/networking/feed/" rel="self" type="application/rss+xml" />
	<link>http://ammonlauritzen.com/blog</link>
	<description>and still for good reason.</description>
	<lastBuildDate>Tue, 13 Dec 2011 21:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>ccent</title>
		<link>http://ammonlauritzen.com/blog/2008/07/02/ccent/</link>
		<comments>http://ammonlauritzen.com/blog/2008/07/02/ccent/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 21:25:20 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[cisco]]></category>
		<category><![CDATA[networking]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=390</guid>
		<description><![CDATA[So&#8230; I just finished the first half of my CCNA today. I never really cared about networking much beyond that needed to make sure clients on a lan can talk to their dns server&#8230; but we&#8217;ve been growing enough here at work that the needs quickly outpaced my prior skillset. And since I was the [...]]]></description>
			<content:encoded><![CDATA[<p>So&#8230; I just finished the <a href='http://www.cisco.com/web/learning/le3/current_exams/640-822.html'>first half</a> of my <a href='http://www.cisco.com/web/learning/le3/le2/le0/le9/learning_certification_type_home.html'>CCNA</a> today.</p>
<p>I never really cared about networking much beyond that needed to make sure clients on a lan can talk to their dns server&#8230; but we&#8217;ve been growing enough here at work that the needs quickly outpaced my prior skillset. And since I was the closest thing we had to a network admin, I got signed up for classes <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>It&#8217;s been fun and profoundly enlightening. I didn&#8217;t expect to have my way of thinking so radically altered, but I&#8217;m hardly complaining.</p>
<p>I&#8217;ll be starting up the <a href='http://www.cisco.com/web/learning/le3/current_exams/640-816.html'>second class</a> in a week or two. This&#8217;ll include such topics as VLANs, IPv6, and fancy routing protocols. I&#8217;m stoked.</p>
<p>It&#8217;s funny. I never finished college (though I took classes for roughly 10 years), so this is actually the first certificate of education I&#8217;ve received since highschool.</p>
<p>The comment was made at work that I&#8217;d dinged as a sysadmin. I haven&#8217;t. I&#8217;m just cherry picking my next few levels in netadmin <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/07/02/ccent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>flash policy service daemon</title>
		<link>http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/</link>
		<comments>http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 09:05:10 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=362</guid>
		<description><![CDATA[Sorry it took me so long to post this, but WordPress 2.5 doesn't seem to like me trying to upload gz/zip files, so I had to upload the source manually. Well, it's been months since I promised to post some usable socket policy service code, so I will. The script here is meant to serve [...]]]></description>
			<content:encoded><![CDATA[<p><i>Sorry it took me so long to post this, but WordPress 2.5 doesn't seem to like me trying to upload gz/zip files, so I had to upload the source manually.</i></p>
<p>Well, it's been months since <a href='http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/'>I promised</a> to post some usable socket policy service code, so I will.</p>
<p>The script here is meant to serve as a good starting point for people whose servers need to allow flash clients to make socket connections. I have not actually used this exact code in a production environment, but I have been using code that is 99% identical for a while now. I am confident that any blatant flaws are the result of simple copy-paste errors as I compiled the package. Please let me know if you find any.</p>
<p>I <u>have</u> however, stress tested the heck out of this service. One instance successfully served up over 16000 policy file requests fed into it as rapidly as I could send them. The same networking code has also handled requests from at least 100 different hosts at roughly the same time.</p>
<p>Everything has been combined into a single cli php script that requires no special installation. Just plop it down on the server and run it as root. It will take care of the rest. The config defaults should be safe, but you probably want to specify them more clearly - just to be safe.</p>
<p>The daemon is made of three classes:</p>
<ul>
<li><b>Logger</b> - A rudimentary log file management class that I copy from project to project in one form or another. The included version is stripped down from some of the other versions I've written, and I'm planning on releasing a more feature-rich version in the future.</li>
<li><b>Daemon</b> - A simple class for daemonizing a process. Adapted and re-adapted countless times from an original php4 class I found on the net a few years ago by some guy named Seth (whose email domain no longer exists).</li>
<li><b>FlashPolicyService</b> - The meat and potatoes, a child of Daemon. Mostly, this is just the requisite networking code and glue to make everything work together.</li>
</ul>
<p>As with any of my other code, this is licensed under <a href='http://creativecommons.org/licenses/by/3.0/'>CC Attribution 3.0</a>.</p>
<p>Download:</p>
<ul>
<li><a href='http://ammonlauritzen.com/FlashPolicyService-09c.zip'>FlashPolicyService-09c.zip</a> (4.4kb)</li>
</ul>
<p>Source code after the jump.<br />
<span id="more-362"></span></p>
<div class="syntax_hilite">
<div id="php-2">
<div class="php"><span style="color:#008000; font-style:italic;">#!/usr/local/bin/php</span><br />
<span style="color:#000000; font-weight:bold;">&lt;?php</span><br />
<span style="color:#008000;">/**<br />
&nbsp;* Flash Policy Service v0.9.c<br />
&nbsp;*<br />
&nbsp;* This script listens for &lt;policy-file-request/&gt; on port 843 and serves up<br />
&nbsp;* an xml crossdomain policy file. This sort of service is necessary if any<br />
&nbsp;* flash content is going to connect to sockets on the running host.<br />
&nbsp;*<br />
&nbsp;* I have made every effort to package everything you need into a single<br />
&nbsp;* file here, even though it could easily have been split into 3 or more<br />
&nbsp;* scripts.<br />
&nbsp;*<br />
&nbsp;* Requirements:<br />
&nbsp;*&nbsp; - PHP 5 with sockets, pcntl, and posix extensions<br />
&nbsp;*&nbsp; - Root access (to bind to a &lt;1024 port)<br />
&nbsp;*<br />
&nbsp;* Use:<br />
&nbsp;*&nbsp; Simply execute the script from the command line as root:<br />
&nbsp;*&nbsp; &nbsp; # ./FlashPolicyService.php<br />
&nbsp;*&nbsp; You can enable debug mode by invoking the script with '-d' as a parameter:<br />
&nbsp;*&nbsp; &nbsp; # ./FlashPolicyService.php -d<br />
&nbsp;*&nbsp; To stop the daemon, simply send it a SIGTERM and it should attempt to<br />
&nbsp;*&nbsp; exit cleanly.<br />
&nbsp;*<br />
&nbsp;*&nbsp; If you get 'bad interpreter' errors or the like, change the #! line to<br />
&nbsp;*&nbsp; reflect the actual installed location of your php cli.<br />
&nbsp;*<br />
&nbsp;* Configuration:<br />
&nbsp;*&nbsp; At present, there aren't very many config options. Simply edit the<br />
&nbsp;*&nbsp; section immediately following this header. Options are commented.<br />
&nbsp;*<br />
&nbsp;* License:<br />
&nbsp;*&nbsp; This code is made available under a Creative Commons Attribution 3.0<br />
&nbsp;*&nbsp; License. Basically, you can use it however you like, but I would<br />
&nbsp;*&nbsp; appreciate some credit when you do.<br />
&nbsp;*<br />
&nbsp;* Disclaimer:<br />
&nbsp;*&nbsp; I make no guarantees that this code won't make your server explode in a<br />
&nbsp;*&nbsp; shower of blue flames. However, I don't expect that it will. I am actually<br />
&nbsp;*&nbsp; confident that this code will be helpful.<br />
&nbsp;*<br />
&nbsp;*&nbsp; That said, this is still my beta release. If you find any bugs, please<br />
&nbsp;*&nbsp; let me know so I can fix them.<br />
&nbsp;*<br />
&nbsp;* - Ammon Lauritzen [Apr 21, '08]<br />
&nbsp;*&nbsp; &nbsp;http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/<br />
&nbsp;*<br />
&nbsp;* Changelog<br />
&nbsp;* 0.9.c<br />
&nbsp;*&nbsp; &nbsp;- Fixed some typoes that were the result of how I combined everything<br />
&nbsp;*&nbsp; &nbsp; &nbsp;into a single file. Thanks Alex!<br />
&nbsp;* 0.9.b<br />
&nbsp;*&nbsp; &nbsp;- Original version to be posted at this url.<br />
&nbsp;* 0.9.a<br />
&nbsp;*&nbsp; &nbsp;- Original proof of concept code posted online.<br />
&nbsp;*/</span></p>
<p><span style="color:#008000;">/*** Config ***/</span></p>
<p><span style="color:#FF9933; font-style:italic;">// where should we save the log output?</span><br />
<span style="color:#0000FF;">$log_filename</span> = <span style="color:#FF0000;">"/tmp/flash-policy.log"</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// uncomment these lines to choose which user to run the daemon as</span><br />
<span style="color:#FF9933; font-style:italic;">// default behavior is to look up and attempt to run as 'nobody'</span><br />
<span style="color:#008000; font-style:italic;"># $daemon_uid = 99;</span><br />
<span style="color:#008000; font-style:italic;"># $daemon_gid = 99;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// set this if you want to use an external file in stead of the default</span><br />
<span style="color:#0000FF;">$xml_filename</span> = <span style="color:#FF0000;">""</span>;<br />
<span style="color:#0000FF;">$default_xml</span> =<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;'</span>.<span style="color:#FF0000;">'?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?'</span>.<span style="color:#FF0000;">'&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;cross-domain-policy xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd&quot;&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;allow-access-from domain=&quot;*&quot; to-ports=&quot;*&quot; secure=&quot;false&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;site-control permitted-cross-domain-policies=&quot;all&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;/cross-domain-policy&gt;'</span>;</p>
<p><span style="color:#008000;">/*** You shouldn't have to edit anything below here ***/</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#000000; font-weight:bold;">class</span> Logger <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$logfile</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span> = <span style="color:#0000FF;">$logfile</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">open_log</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: constructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fflush"><span style="color:#000066;">fflush</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: destructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> <a href="http://www.php.net/log"><span style="color:#000066;">log</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// deal with redundant log spam</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> == <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Last message repeated "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span>.<span style="color:#FF0000;">" times."</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">lasg_msg</span> = <span style="color:#0000FF;">$msg</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span> = <span style="color:#CC66CC;">0</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// actually write the log message out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: log</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> write<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$msg</span> = <a href="http://www.php.net/sprintf"><span style="color:#000066;">sprintf</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"[%s] %s<span style="color:#000099; font-weight:bold;">\\</span>n"</span>,<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"y-m-d H:i:s"</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#0000FF;">$msg</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span>, <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$succ</span> === <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$msg</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: write</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> open_log<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/touch"><span style="color:#000066;">touch</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chmod"><span style="color:#000066;">chmod</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span>, <span style="color:#CC66CC;">0664</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span> = @<a href="http://www.php.net/fopen"><span style="color:#000066;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span>, <span style="color:#FF0000;">"a"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: logger class</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#000000; font-weight:bold;">class</span> Daemon <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/error_reporting"><span style="color:#000066;">error_reporting</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/set_time_limit"><span style="color:#000066;">set_time_limit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$log_filename</span>, <span style="color:#0000FF;">$pid_filename</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logger</span> = <span style="color:#000000; font-weight:bold;">new</span> Logger<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$log_filename</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pid_filename</span> = <span style="color:#0000FF;">$pid_filename</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log_tag</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> = <span style="color:#FF0000;">"launcher"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"constructed"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: constructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"destructing"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: destructor</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> <a href="http://www.php.net/log"><span style="color:#000066;">log</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$tag</span> = <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$tag</span> == <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$tag</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log_tag</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logger</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$tag</span>.<span style="color:#FF0000;">": "</span>.<span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: log</span><br />
&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> debug<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$tag</span> = <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> DEBUG <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: debug</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> main<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"override main() in daemon subclass"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">stop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: main</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"starting daemon"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;_start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to start daemon"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// report execution details</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"uid = "</span>.<a href="http://www.php.net/posix_getuid"><span style="color:#000066;">posix_getuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">", gid = "</span>.<a href="http://www.php.net/posix_getgid"><span style="color:#000066;">posix_getgid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"cwd = "</span>.<a href="http://www.php.net/getcwd"><span style="color:#000066;">getcwd</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// invoke main loop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">running</span> = <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">running</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">main</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: start</span><br />
&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> _start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;_fork<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: try to fork</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/posix_setsid"><span style="color:#000066;">posix_setsid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to setsid()"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: try to set sid</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;_suid<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: try to set uid</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// register signal handler</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks = <span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGTERM, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>, <span style="color:#FF0000;">"on_sigterm"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// chdir somewhere moderately safe by default</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chdir"><span style="color:#000066;">chdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">'/tmp'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: _start</span><br />
&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> _fork<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"forking..."</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$pid</span> = pcntl_fork<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$pid</span> == -<span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// error</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to fork"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$pid</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// parent</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"done with parent"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// child</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> = <span style="color:#FF0000;">"daemon"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">child</span> = <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pid</span> = <a href="http://www.php.net/posix_getpid"><span style="color:#000066;">posix_getpid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"child pid = "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pid</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: _fork</span><br />
&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> _suid<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$daemon_uid</span>, <span style="color:#0000FF;">$daemon_gid</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$daemon_uid</span><span style="color:#006600; font-weight:bold;">&#41;</span> || !<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$daemon_gid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// we didn't get a uid/gid, try to make one up</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"searching for info on 'nobody'"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$res</span> = <a href="http://www.php.net/posix_getpwnam"><span style="color:#000066;">posix_getpwnam</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"nobody"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$res</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$uid</span> = <span style="color:#0000FF;">$res</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'uid'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$gid</span> = <span style="color:#0000FF;">$res</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'gid'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// the 'nobody' user doesn't exist on this system, refuse</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// to daemonize as root</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to find info on 'nobody' user"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got uid/gid of $daemon_uid/$daemon_gid"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$uid</span> = <span style="color:#0000FF;">$daemon_uid</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$gid</span> = <span style="color:#0000FF;">$daemon_gid</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// actually try to switch now</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/posix_setgid"><span style="color:#000066;">posix_setgid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$gid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to set gid to "</span>.<span style="color:#0000FF;">$gid</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/posix_setuid"><span style="color:#000066;">posix_setuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$uid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to set uid to "</span>.<span style="color:#0000FF;">$uid</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: _suid</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> stop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"stopping daemon"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">running</span> = <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: stop</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> on_sigterm<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sig</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sig</span> == SIGTERM <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got SIGTERM"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">stop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: on_sigterm</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: daemon class</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#000000; font-weight:bold;">class</span> FlashPolicyService extends Daemon <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log_tag</span> = <span style="color:#FF0000;">"fps"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"constructing"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">request_str</span> = <span style="color:#FF0000;">"&lt;policy-file-request/&gt;"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">port</span> = <span style="color:#CC66CC;">843</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get our xml</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$xml_filename</span>, <span style="color:#0000FF;">$default_xml</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color:#000066;">strlen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$xml_filename</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;">0</span> || !<a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$xml_filename</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to read xml from '$xml_filename', using default"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> = <span style="color:#0000FF;">$default_xml</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"reading policy xml from $xml_filename"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> = <a href="http://www.php.net/file_get_contents"><span style="color:#000066;">file_get_contents</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$xml_filename</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"policy xml: "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// make sure we're null terminated</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> = <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\\</span>n<span style="color:#000099; font-weight:bold;">\\</span>0"</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// and get going</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">init</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: constructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent::__destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: destructor</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> init<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"init..."</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">check_socket</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::<span style="color:#006600;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"not starting daemon"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: init</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> main<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">accept_socket</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"server socket is closed?!"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::<span style="color:#006600;">stop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// paranoia to keep from absolutely hosing cpu if something goes wrong</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/usleep"><span style="color:#000066;">usleep</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">10000</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// 10ms</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: main</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> check_socket<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> = @<a href="http://www.php.net/socket_create"><span style="color:#000066;">socket_create</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> AF_INET, SOCK_STREAM, SOL_TCP <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to create socket"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_bind"><span style="color:#000066;">socket_bind</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span>, <span style="color:#FF0000;">"0.0.0.0"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">port</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to bind to port "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">port</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$backlog</span> = <span style="color:#CC66CC;">100</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_listen"><span style="color:#000066;">socket_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span>, <span style="color:#0000FF;">$backlog</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to listen with backlog of $backlog"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// everything's good</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: able to bind</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: able to create socket</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// if we got here, it's an error. abort.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errno</span> = <a href="http://www.php.net/socket_last_error"><span style="color:#000066;">socket_last_error</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errstr</span> = <a href="http://www.php.net/socket_strerror"><span style="color:#000066;">socket_strerror</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$errno</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"socket error: $errno: $errstr"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: check_socket</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> accept_socket<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$r_socks</span> = <a href="http://www.php.net/array_merge"><span style="color:#000066;">array_merge</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"selecting on "</span>.<a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$r_socks</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">" sockets"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// block until something interesting happens</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$select</span> = @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r_socks</span>, <span style="color:#0000FF;">$w_socks</span> = <span style="color:#000000; font-weight:bold;">NULL</span>, <span style="color:#0000FF;">$e_socks</span> = <span style="color:#000000; font-weight:bold;">NULL</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$select</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// did we get a new connection?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/in_array"><span style="color:#000066;">in_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span>, <span style="color:#0000FF;">$r_socks</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$conn</span> = @<a href="http://www.php.net/socket_accept"><span style="color:#000066;">socket_accept</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> !== <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/socket_getpeername"><span style="color:#000066;">socket_getpeername</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$addr</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"connection accepted from $addr, $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$conn</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: got a new connection</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// check for policy requests</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r_socks</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$conn</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ignore the server socket</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> == <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// read from the client</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$data</span> = @<a href="http://www.php.net/socket_read"><span style="color:#000066;">socket_read</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#CC66CC;">1024</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$data</span> === <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got disconnect from $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: client closed connection</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"read '"</span>.<a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">"' from $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color:#000066;">strpos</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">request_str</span><span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"sending policy xml to $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/socket_write"><span style="color:#000066;">socket_write</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got invalid request from $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: got data from the client</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// and always disconnect after having read something, whether</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// it was a valid request or not - especially if it wasn't <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/socket_close"><span style="color:#000066;">socket_close</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$key</span> = <a href="http://www.php.net/array_search"><span style="color:#000066;">array_search</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$key</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/unset"><span style="color:#000066;">unset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$key</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: foreach socket</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: accept_socket</span></p>
<p><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: flash policy service class</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#008000;">/**<br />
&nbsp;* Actual execution code here. This checks if the php install has all of our<br />
&nbsp;* requisite extensions, makes sure we're launching as root, and checks if<br />
&nbsp;* debug mode was requested before actually starting the daemon.<br />
&nbsp;*/</span></p>
<p><span style="color:#FF9933; font-style:italic;">// make sure we have required extensions</span><br />
<span style="color:#0000FF;">$required_extensions</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"sockets"</span>, <span style="color:#FF0000;">"posix"</span>, <span style="color:#FF0000;">"pcntl"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$missing_extension</span> = <span style="color:#000000; font-weight:bold;">false</span>;<br />
<span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$required_extensions</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$ext</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/extension_loaded"><span style="color:#000066;">extension_loaded</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$ext</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Missing required php extension '$ext'.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$missing_extension</span> = <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$missing_extension</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// make sure we launch as root, otherwise we can't bind to 843</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/posix_getuid"><span style="color:#000066;">posix_getuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#CC66CC;">0</span> || <a href="http://www.php.net/posix_getgid"><span style="color:#000066;">posix_getgid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Policy service must be started as root.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// see if we're in debug mode</span><br />
<a href="http://www.php.net/define"><span style="color:#000066;">define</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"DEBUG"</span>, <a href="http://www.php.net/in_array"><span style="color:#000066;">in_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'-d'</span>,<span style="color:#0000FF;">$argv</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// start the daemon</span><br />
<span style="color:#0000FF;">$fps</span> = <span style="color:#000000; font-weight:bold;">new</span> FlashPolicyService<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>flash socket policies update</title>
		<link>http://ammonlauritzen.com/blog/2008/04/21/flash-socket-policies-update/</link>
		<comments>http://ammonlauritzen.com/blog/2008/04/21/flash-socket-policies-update/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 18:57:02 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=361</guid>
		<description><![CDATA[I've been getting a lot of positive feedback recently on the post I made last December on this topic, so it's probably time to post an update. Last week, Adobe finally released some help for people who need to provide socket policy services. They've got python and perl versions of the socket policy daemon that [...]]]></description>
			<content:encoded><![CDATA[<p>I've been getting a lot of positive feedback recently on <a href='http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/'>the post</a> I made last December on this topic, so it's probably time to post an update.</p>
<p>Last week, Adobe finally released <a href='http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html'>some help</a> for people who need to provide socket policy services. They've got python and perl versions of the socket policy daemon that look like they can run either standalone or through xinetd. They don't claim that the code is production quality yet... but it's at least something.</p>
<p>Of course, I released sample PHP code 5 months ago... shrug.</p>
<p>My confederates and I have spent the intervening months sniffing packets, yelling at flash, rewriting daemons, etc... until settling upon a version of the service that appears to function reliably now. I am currently running it on 6 different servers and have seen it successfully handle requests from &gt;100 simultaneous users during stress tests.</p>
<p>The biggest problem with my previous versions of the code was how I handled closing of connections. Previously, I had simply accepted connections and fired off the policy xml without waiting for a request from the client.</p>
<p>This worked 95% of the time over a good net connection, and more like 80% of the time over a poor connection.</p>
<p>The reason it didn't work consistently was because of weird tcp ordering issues. Connections would be closed out of order, the response would be received before the request was dispatched, etc..</p>
<p>My new version is much more robust and actually waits for flash to submit the request before sending the XML. I consider it <strike>release candidate</strike> beta quality code and hope to be able to release it later today.</p>
<h3>update</h3>
<p>Version 0.9.b is <a href='http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/'>available</a> now.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/04/21/flash-socket-policies-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>new flash security policies</title>
		<link>http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/</link>
		<comments>http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 22:14:20 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/</guid>
		<description><![CDATA[So... I am not happy with Adobe right now. With the push of Flash Player 9,0,115,0 "moviestar", which included such awesome features as H.264 and AAC codec support and improvements to fullscreen mode, they kind of ambushed me with some sweeping changes to their security policy. I'd been running pre-release nightly builds of the player [...]]]></description>
			<content:encoded><![CDATA[<p>So... I am not happy with Adobe right now. With the push of Flash Player 9,0,115,0 "moviestar", which included such awesome features as <a href='http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player.html'>H.264 and AAC</a> codec support and <a href='http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html'>improvements to fullscreen mode</a>, they kind of ambushed me with some sweeping changes to their security policy.</p>
<p>I'd been running pre-release nightly builds of the player since 9,0,60,x... and had noticed some strange warnings. Mysterious "Socket Security Error #2048" exceptions that were being thrown at random - even though I was serving an appropriate (for the time) crossdomain.xml file, unexplained timeouts attempting to talk to an xml socket server when I was very clearly not attempting to do any such thing, etc... My regularly repeated attempts to find documentation on what the warnings actually meant proved fruitless. I believe that is because <a href='http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security.html'>the appropriate document</a> was not actually released to the public until 9,0,115,0 was released.</p>
<p>Now, the bit where they improved the format for <a href='http://marstonstudio.com/index.php/2007/12/06/new-security-policy-settings-in-flash-moviestar/'>crossdomain.xml</a> files doesn't really affect me one way or the other. I approve of the improvements but could really care less in this case. They don't really affect anything I'm doing.</p>
<p>The part that really chaps my hide is the fact that they've completely redone the way that socket security policies are handled. The important parts:</p>
<blockquote>
<ul>
<li>A SWF file may no longer make a socket connection to its own domain without a socket policy file. Prior to version 9,0,115,0, a SWF file was permitted to make socket connections to ports 1024 or greater in its own domain without a policy file.</li>
<li>HTTP policy files may no longer be used to authorize socket connections. Prior to version 9,0,115,0, an HTTP policy file, served from the master location of /crossdomain.xml on port 80, could be used to authorize a socket connection to any port 1024 or greater on the same host.</li>
</ul>
</blockquote>
<p>That's right. Your socket policy data can't live in the sitewide crossdomain.xml file that Apache serves any more.</p>
<blockquote><p>
Flash Player 9,0,115,0 introduces a concept of socket master policy files, which are served from the fixed TCP port number 843.
</p></blockquote>
<blockquote><p>
Socket policy files may be obtained from the same port as a main connection (the socket connection being made by ActionScript, which is authorized by a socket policy file), or from a different port, separate from the main connection. If you opt to serve a socket policy file from the same port as a main connection, the server listening on that port must understand socket policy file requests (which are indicated by a transmission of
<policy-file-request/> from Flash Player), and must respond differently for policy file requests and main connection requests.
</p></blockquote>
<blockquote>
<ul>
<li>When a SWF file attempts to make a socket connection, even to its own domain, Flash Player will first attempt to contact port 843 to see if the host is serving a socket master policy file.</li>
</ul>
</blockquote>
<p>So... regardless of whether you're even using a custom port 843 client, the Flash Player is going to try to hit it. What if your firewall doesn't allow/route traffic to sub-1024 ports w/o special configuration? What if you don't have the access to bind to a sub-1024 port and can't rewrite your other server process to serve the policy data on its port?</p>
<blockquote>
<ul>
<li>Socket meta-policies can only be declared in a socket master policy file. The syntax is the same as for declaring a meta-policy in an URL master policy file, using the &lt;site-control&gt; tag. Socket meta-policies cannot be declared in HTTP response headers, as HTTP is not involved.</li>
</ul>
</blockquote>
<p>This implies that you can't even tell apache to listen to port 843 and serve up the data. You HAVE to either maintain a separate server process specifically for the purpose of serving this policy data, or you have to edit the process that SWF's are connecting to and make them serve the data..</p>
<p>As of the time of this writing (10 days after moviestar's release), they have yet to release <a href='http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html'>promised help</a> on how to deploy a solution to these new changes. Granted, the one article they did release explains what needs to be done in high level terms. It was sufficient to help me out. I wrote a server that simply listens on port 843 and spews the required xml. But... I'd have really appreciated specific examples, and I suspect plenty of people would appreciate drop-in solutions to the issue.</p>
<p>A 5-minute skeleton implementation (not recommended for production use by any means) written as a PHP cli script might look something like this:</p>
<div class="syntax_hilite">
<div id="php-4">
<div class="php"><span style="color:#008000; font-style:italic;">#!/usr/bin/php</span><br />
<span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#008000;">/**<br />
&nbsp;* Ugly Flash socket policy file service. This script must be run as root from<br />
&nbsp;* the command line. It binds to port 843 on all interfaces and waits<br />
&nbsp;* indefinitely for connections. When a connection is detected, the script spits<br />
&nbsp;* out a chunk of xml and disconnects. It can only serve one request at a time,<br />
&nbsp;* but that shouldn't be much of a problem.<br />
&nbsp;*<br />
&nbsp;* One potential problem with this script is that you can easily lock port 843<br />
&nbsp;* up for an indeterminate amount of time if the script doesn't exit cleanly.<br />
&nbsp;* The OS should clear the port up for you eventually, but you could be stuck<br />
&nbsp;* playing the waiting game.<br />
&nbsp;*<br />
&nbsp;* This particular version of the script has only been tested very lightly.<br />
&nbsp;* Deploy at your own peril <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  YMMV.<br />
&nbsp;*<br />
&nbsp;* - Ammon Lauritzen [12/13/07]<br />
&nbsp;*/</span></p>
<p><span style="color:#FF9933; font-style:italic;">// define the xml policy &quot;file&quot;</span><br />
<span style="color:#0000FF;">$policy_file</span> = <br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;'</span>.<span style="color:#FF0000;">'?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?'</span>.<span style="color:#FF0000;">'&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;cross-domain-policy xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd&quot;&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;allow-access-from domain=&quot;*&quot; to-ports=&quot;*&quot; secure=&quot;false&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;site-control permitted-cross-domain-policies=&quot;master-only&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;/cross-domain-policy&gt;'</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// make sure everything launches correctly</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/posix_getuid"><span style="color:#000066;">posix_getuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"You must run this script as root.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$sock</span> = @<a href="http://www.php.net/socket_create"><span style="color:#000066;">socket_create</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> AF_INET, SOCK_STREAM, SOL_TCP <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Unable to create socket.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_bind"><span style="color:#000066;">socket_bind</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span>, <span style="color:#FF0000;">"0.0.0.0"</span>, <span style="color:#CC66CC;">843</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Unable to bind to port 843.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_listen"><span style="color:#000066;">socket_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Unable to start listening.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// start serving policies</span><br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$r</span> = <span style="color:#0000FF;">$w</span> = <span style="color:#0000FF;">$e</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$w</span>, <span style="color:#0000FF;">$e</span>, <span style="color:#000000; font-weight:bold;">null</span> <span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$conn</span> = @<a href="http://www.php.net/socket_accept"><span style="color:#000066;">socket_accept</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> !== <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// somebody connected, just dump the xml and close</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/socket_write"><span style="color:#000066;">socket_write</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$policy_file</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/socket_close"><span style="color:#000066;">socket_close</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"socket_accept() failed?<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"socket_select() failed?<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: listen forever</span></p>
<p><span style="color:#FF9933; font-style:italic;">// clean up</span><br />
<a href="http://www.php.net/socket_close"><span style="color:#000066;">socket_close</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
I'll try to make my production version of this a bit more suitable for public consumption and release it as soon as I can.</p>
<p>The random #2048 security errors continue, despite having deployed my port 843 policy xml server. Granted, they happen less than before... but they still happen. And even when my policy server isn't running, the errors aren't thrown 100% of the time. This just baffles me. If they were consistent, that would be one thing. But when you get a security error 1 time in 20... that's not security, that's not even a lame deterrent. It's just incentive to hammer the same port over and over again until something finally gives.</p>
<p>Now, I admit that I could be wrong here... but I've re-re-read the documentation on these policies a few times now, and cannot find any reason for the behaviors I'm seeing.</p>
<h3>update</h3>
<p>On April 22nd, 2008, I released a much better, much more reliable version of this daemon. Head <a href='http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/'>over there</a> for more details and source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>php post method</title>
		<link>http://ammonlauritzen.com/blog/2007/10/18/php-post-method/</link>
		<comments>http://ammonlauritzen.com/blog/2007/10/18/php-post-method/#comments</comments>
		<pubDate>Thu, 18 Oct 2007 18:52:51 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2007/10/18/php-post-method/</guid>
		<description><![CDATA[Yes, I know you can do this via cURL. This was explicitly written for a case when that particular luxury was not available to me. So... it's been a while since I've written a post, and I'm feeling the withdrawals. I've got 16 half-written posts waiting in the queue and two more that I want [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
<i>Yes, I know you can do this via <a href='http://php.net/curl'>cURL</a>. This was explicitly written for a case when that particular luxury was not available to me.</i>
</p></blockquote>
<p>So... it's been a while since I've written a post, and I'm feeling the withdrawals. I've got 16 half-written posts waiting in the queue and two more that I want to write but haven't even started. It's <u>also</u> time for another round of anime reviews; I've got two video games I want to review...</p>
<p>And I am having to play the "too busy with RL" card.</p>
<p>In stead, I present a function that I wrote this morning. It's been done countless times before, and you can probably find incredibly similar code out there already, but it's the first time I've ever actually needed something quite like this.</p>
<div class="syntax_hilite">
<div id="php-6">
<div class="php"><span style="color:#008000;">/**<br />
&nbsp;* Send post data somewhere <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> <br />
&nbsp;* <br />
&nbsp;* @param $host The domain name of the host to send the data to.<br />
&nbsp;* @param $path The path of the script to receive the post.<br />
&nbsp;* @param $data An array of key/value pairs to send.<br />
&nbsp;* @return&nbsp; &nbsp; &nbsp; &nbsp;The HTTP response, headers and all.<br />
&nbsp;*/</span><br />
<span style="color:#000000; font-weight:bold;">function</span> http_post<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$host</span>, <span style="color:#0000FF;">$path</span>, <span style="color:#0000FF;">$data</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// build a query string from the data array</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$arr</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$data</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$key</span> =&amp;gt; <span style="color:#0000FF;">$val</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_push"><span style="color:#000066;">array_push</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$arr</span>, &amp;quot;<span style="color:#0000FF;">$key</span>=&amp;quot;.<a href="http://www.php.net/urlencode"><span style="color:#000066;">urlencode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$val</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$data</span> = <a href="http://www.php.net/implode"><span style="color:#000066;">implode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;&amp;amp;&amp;quot;, <span style="color:#0000FF;">$arr</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// send that post</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$fh</span> = <a href="http://www.php.net/fsockopen"><span style="color:#000066;">fsockopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$host</span>, <span style="color:#CC66CC;">80</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, &amp;quot;POST <span style="color:#0000FF;">$path</span> HTTP/<span style="color:#CC66CC;">1</span>.<span style="color:#CC66CC;">1</span>\\\r\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, &amp;quot;Host: <span style="color:#0000FF;">$host</span>\\\r\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, &amp;quot;Content-type: application/x-www-form-urlencoded\\\r\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, &amp;quot;Content-length: &amp;quot;.<a href="http://www.php.net/strlen"><span style="color:#000066;">strlen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span>.&amp;quot;\\\r\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, &amp;quot;Connection: close\\\r\\\n\\\r\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, <span style="color:#0000FF;">$data</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get the response</span><br />
&nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/feof"><span style="color:#000066;">feof</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fh</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$buf</span> .= <a href="http://www.php.net/fgets"><span style="color:#000066;">fgets</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$buf</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: http_post </span></div>
</div>
</div>
<p></p>
<p>This method could easily be modified to send GET data or to talk over a different port. It could also probably actually do something with the response buffer (check for 200, etc...). It could be a bit more fault tolerant, etc... but that's not what I need for the application at hand.</p>
<p>For other examples of how people have done similar/related things, take a look at the comments on the <a href='http://www.php.net/fsockopen'>fsockopen()</a> documentation page.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2007/10/18/php-post-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>udp in flash</title>
		<link>http://ammonlauritzen.com/blog/2007/07/16/udp-in-flash/</link>
		<comments>http://ammonlauritzen.com/blog/2007/07/16/udp-in-flash/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 17:19:30 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[speculation]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2007/07/16/udp-in-flash/</guid>
		<description><![CDATA[This is a memo to myself more than anything else... but there has got to be a way to use UDP from a SWF running through your web browser. Pieces of the puzzle: Flash 9 has binary TCP socket support, but no UDP. Standalone projectors like MDM Zinc can add UDP capabilities to the mix. [...]]]></description>
			<content:encoded><![CDATA[<p>This is a memo to myself more than anything else... but there has <u>got</u> to be a way to use UDP from a SWF running through your web browser.</p>
<p>Pieces of the puzzle:</p>
<ul>
<li>Flash 9 has binary TCP socket support, but no UDP.</li>
<li>Standalone projectors like MDM Zinc can add UDP capabilities to the mix.</li>
<li>A projector and a SWF in the browser can communicate together very happily via the LocalConnection interface.</li>
</ul>
<p>But web browsers are understandably reluctant to download and run a 3rd party UDP proxy application for you <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  It's also not terribly platform independent to require something running in userspace outside of the VM.</p>
<p>Java has UDP support. You could write a UDP proxy in Java that uses a standard local TCP socket to talk to the SWF - both the SWF and the applet would have to load from the same page. Don't know whether Java's and Flash's security policies would allow this sort of behavior... probably not. I know applets have difficulties connecting to anything but the originating server - so they probably can't listen for traffic on the local host either. And if they can, there's probably some big security certificate you have to sign in blood and offer to the Sun gods or something...</p>
<p>Of course, the dumb option would be to have a <i>remote</i> TCP->UDP proxy... but that eliminates any sort of performance gain that might have been achieved by using UDP in the first place, so that's only valid in the weird case where you honestly need to talk to a UDP server for whatever reason and don't care about the lag.</p>
<p>No, the proxy must run locally, somehow.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2007/07/16/udp-in-flash/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>adventures in wireless networking</title>
		<link>http://ammonlauritzen.com/blog/2007/05/12/adventures-in-wireless-networking/</link>
		<comments>http://ammonlauritzen.com/blog/2007/05/12/adventures-in-wireless-networking/#comments</comments>
		<pubDate>Sat, 12 May 2007 08:49:14 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[wifi]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2007/05/12/adventures-in-wireless-networking/</guid>
		<description><![CDATA[Ever since we moved into our new house, I've had dreams about setting up a killer network. Every room in the building wired with gigabit ethernet, etc... but... we have no practical application for it, and the budget is insufficient to allow me to do that kind of stuff just for fun. Our current situation [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since we moved into our new house, I've had dreams about setting up a killer network. Every room in the building wired with gigabit ethernet, etc... but... we have no practical application for it, and the budget is insufficient to allow me to do that kind of stuff just for fun.</p>
<p>Our current situation has computers in 3 rooms of the house: my office, wife's computer in our bedroom, and the PS3 in the living room. Oh, and the laptop, whenever it's turned on.</p>
<p>Well, we've been using wireless for maybe two years now - and through two wireless routers. We initially got an old D-Link router to go with our first cable modem, but it got fried during a power outage in our apartment (I'd neglected to put it on any sort of power conditioner). Much of the networking hardware died in that. Actually, that was my second wholesale networking hardware failure - the first one happened at our first apartment when (I am guessing) a spike came over the phone line of all things (fried dsl router and the switch it was plugged into).</p>
<p>Anyhow.</p>
<p>The cheap old D-Link 802.11g router was replaced by a Linksys model, since it matched my new modem. They stacked together, which I thought was neat... but it turned out to be <a href='http://ammonlauritzen.com/blog/2006/01/24/hot_router_blues/'>a terrible problem</a>. We've been suffering with the easily overheated router and modem for a while now without many problems, but then we moved into the new house.</p>
<p>Heat no longer was a problem, my office is huge. I have plenty of room to put the modem and router on opposite ends of their little shelf and not feel bad about it.</p>
<p>The problem in the new place is the relative position of my bedroom with respect to the office. Or, more appropriately, the position of the master bathroom and all of its glorious plumbing (2-head shower, hot tub <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ) in between the two <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Tarma, my wife's machine, was using a little usb Linksys wifi adaptor, but it just couldn't get strong enough of a signal through all of the plumbing to be useful. So we bought her a new PCI card and plugged a ginormous antenna into it. And that worked, for a while, until the strain of a heavy antenna combined with an extended period of moving the machine around and fiddling with things broke the connector loose. It still works fine, but you have to jam it in there. I can probably just solder it in place, but I've been kind of lazy - and don't remember where my soldering stuff is packed anyway <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>And, we could have lived with that. The computer was stable and wasn't being moved around any more. Even with the connector loose, the big antenna meant that it could get 40-60% signal through all of the plumbing, except when water was running. Then she gets booted.</p>
<p>No, the last straw was our wireless router (the one that liked to overheat) finally giving up on talking to the modem. It... just refused to acquire an IP address any more. Modem's fine. ISP is fine. All of the computers in the house are fine. The LAN ports and wireless on the router are fine... but it simply ceased to be useful at actually talking to the internet. And that's no good.</p>
<p>So, thusly convinced that something needed to be done to feed her addiction, my wife approved the acquisition of some new networking hardware.</p>
<p>And when I arrived at the local emporium of all things expensive and electronic, I decided that 802.11n didn't sound like all that bad of an idea. It's backwards compatible with G (and B, for whatever that matters any more), and promises something like 6x the throughput and substantially higher signal range. Better signal means better chance of blasting through the hot tub...</p>
<p>So I bought a D-Link <a href='http://www.newegg.com/Product/Product.aspx?Item=N82E16833127215'>DIR-655</a> router and <a href='http://www.newegg.com/Product/Product.aspx?Item=N82E16833127219'>DWA-552</a> nic for Penny's machine. Set the router up, it worked flawlessly. It's got a decent admin interface, and all of my old 802.11g devices talk to it wonderfully. It also doesn't hurt that the wired ports are gigabit, which means the machines in my office are now talking to each other at the speed their ethernet adaptors intended <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>One thing I do like about the router is that it is capable of running a network in both WPA and WPA2 modes at once. This means that most of my machines are WPA2 encrypted, but those devices that don't quite support it (like the Playstation...) are at least able to fall back and use WPA. Say what you will about possibly questionable security practices, but I think that this is a fabulous solution - it gives me the highest security realistically available on every device on my network w/o having to spend gobs of money.</p>
<p>So, network upgraded, and all legacy clients confirmed to be operating with better signal quality and security options, it was time to tackle Penny's machine. This computer has been nothing but trouble since the day we got it. It has heat issues, it has power supply issues, it has WinXP MCE issues, etc... but, it's still a generally functional system and it still works like a TV+TiVo in our bedroom.</p>
<p>I popped the new NIC into the box and fired it up. Drivers were installed from the disk and networks were identified. Signal strength meter comes up - full bars. Connection speed identified itself at 300Mbit. Penny was filling the tub for Alanna at the time. I considered that a success at the time and called it a night.</p>
<p>Well, the following day, Penny started having stability problems with the machine. Nothing unusual about that. I look at Tarma funny and the audio decides to start stuttering for a month w/o any explanation and then go back to normal. The box is certainly near the top of my list of problem children over the years.</p>
<p>When I got a chance, I fiddled with the box a bit more, streamlined a few things, ran the obligatory spyware/av/adware battery of scans. Nothing came up, machine was clean. It... just lagged beyond reason. I took a look at the task manager and the cpu load was reported to never really drop below 90%, yet the box wasn't actually doing anything. 'System idle processes' were reported to be reliably occupying >80% of the processor's time.</p>
<p>My first thought was that it was the new wireless card, but it worked fine... it never lost signal, file transfers and net access worked fine. The machine itself was just so slow that we couldn't ever see anything like a decent transfer rate (so no streaming video from the file server). It couldn't even play the videos off of the hard drive any more. Heaven forbid you tried to watch television.</p>
<p>After a bit more pondering and discussing the problem with my father and with Danny, I decided to disable to network card and see if it was just the drivers pegging the cpu.</p>
<p>I finally did that tonight.</p>
<p>The instant the interface was disabled, the system load dropped to nil. I turned the device on, and load skyrocketed back to well over 90%.</p>
<p>A bit of very lazy searching didn't turn up anyone else reporting this problem. And I'd sat on the problem for too long to expect a return on the device from the store... so I decided to check if there were any new drivers.</p>
<p>Sure enough, D-Link had released a new driver last week. (Actually, the version number was several up from what came on the disc with my card, so I'll assume they'd released a few versions in the interim as well). I installed the new driver, and not only did it upgrade things to 802.11n draft 2.0 (from the draft 1 advertised on the box), it fixed the system load problem. I successfully streamed a 1.3gb video w/o a hitch.</p>
<p>I also upgraded the router's firmware at the same time, and haven't noticed any changes in its behavior, but the firmware upgrade process was painless and upgraded it to using draft 2 as well.</p>
<p>I'm happy. Penny's happy. Alanna'd prolly be happy too if she knew what the whole fuss had been about <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I'm kind of annoyed at myself that I didn't check for new drivers and firmware the minute I installed the devices, but oh well. It works now, and I'm not out $100 for an unusable network card after all. (The Linksys card with the loose antenna connector will probably be put into service elsewhere, I'm just not sure where yet).</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2007/05/12/adventures-in-wireless-networking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>flexunit basics</title>
		<link>http://ammonlauritzen.com/blog/2006/11/02/flexunit_basics/</link>
		<comments>http://ammonlauritzen.com/blog/2006/11/02/flexunit_basics/#comments</comments>
		<pubDate>Thu, 02 Nov 2006 16:40:12 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[flexunit]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[The following is adapted from a brief tutorial I wrote for our company's internal conventions wiki. And yes, I know the syntax hiliting could be better. I'm looking into switching over to WordPress and a new stylesheet, so hopefully this sort of thing will all be resolved in the nearish future. FlexUnit FlexUnit is a [...]]]></description>
			<content:encoded><![CDATA[<p>The following is adapted from a brief tutorial I wrote for <a href='http://terralever.com'>our company</a>'s internal conventions wiki.</p>
<p>And yes, I know the syntax hiliting could be better. I'm looking into switching over to WordPress and a new stylesheet, so hopefully this sort of thing will all be resolved in the nearish future.</p>
<hr />
<h3>FlexUnit</h3>
<p>FlexUnit is a powerful unit testing framework for as3/flex2 projects. There are two API's out there called FlexUnit, one is a <a href='http://sourceforge.net'>SourceForge</a> project and one is official from Adobe. The SF project is ancient and unmaintained, I will be referring to the official API.</p>
<p>FlexUnit also has some competition from the <a href='http://asunit.org '>AsUnit</a> project, but seems to be slightly less well suiteded for our needs when testing AS3 projects. It does, however, seem to offer fairly good support for AS2 projects. The two frameworks are generally very similar and switching between them should be fairly painless if necessary as they both evolved from and are modeled after <a href='http://junit.org'>JUnit</a>, the industry standard for unit testing in Java.</p>
<p>FlexUnit is available for download from <a href='http://labs.adobe.com/wiki/index.php/ActionScript_3:resources:apis:libraries#FlexUnit'>Adobe Labs</a>. You want the zip file.</p>
<p>The official API documentation for FlexUnit is at <a href='http://weblogs.macromedia.com/as_libraries/docs/flexunit'>http://weblogs.macromedia.com/as_libraries/docs/flexunit</a>.</p>
<p><u>Warning:</u> If your default run target in Flex doesn't use the right flash player version/options, it is entirely possible that your failures might not be documented. Run the swf again in the standalone debugging player and you should get the information you missed. I'm not entirely certain why this is happening but I don't much care at the moment.</p>
<h3>Using FlexUnit</h3>
<p>To make use of FlexUnit in a Flex 2 project only two bits of prep work need to be done:</p>
<ul>
<li>Add the flexunit.swc file (found in the zip on Adobe's site) to your project's library path.</li>
<li>Create a test runner application in the project. This is Flex, so remember that applications have to be in the root of the project tree.</li>
</ul>
<p>An example simple test runner (adapted very slightly from <a href='http://www.darronschall.com/weblog/archives/000216.cfm'>Darron Schall's howto post</a>):</p>
<div class="syntax_hilite">
<div id="xml-8">
<div class="xml"><span style="color: #ddbb00;">&amp;lt;</span>?xml version=<span style="color: #ddbb00;">&amp;quot;</span>1.0<span style="color: #ddbb00;">&amp;quot;</span> encoding=<span style="color: #ddbb00;">&amp;quot;</span>utf-8<span style="color: #ddbb00;">&amp;quot;</span>?<span style="color: #ddbb00;">&amp;gt;</span><br />
<span style="color: #ddbb00;">&amp;lt;</span>mx:Application xmlns:mx=<span style="color: #ddbb00;">&amp;quot;</span>http://www.adobe.com/2006/mxml<span style="color: #ddbb00;">&amp;quot;</span> xmlns=<span style="color: #ddbb00;">&amp;quot;</span>*<span style="color: #ddbb00;">&amp;quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlns:flexunit=<span style="color: #ddbb00;">&amp;quot;</span>flexunit.flexui.*<span style="color: #ddbb00;">&amp;quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; creationComplete=<span style="color: #ddbb00;">&amp;quot;</span>onCreationComplete()<span style="color: #ddbb00;">&amp;quot;</span><span style="color: #ddbb00;">&amp;gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #ddbb00;">&amp;lt;</span>/mx<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #ddbb00;">&amp;lt;</span>mx:Script<span style="color: #ddbb00;">&amp;gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ddbb00;">&amp;lt;</span>![CDATA[<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; import flexunit.framework.TestSuite;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; import test.*;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // After everything is built, configure the test<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // runner to use the appropriate test suite and<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // kick off the unit tests<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private function onCreationComplete() : void {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; testRunner.test = createSuite();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; testRunner.startTest();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Creates the test suite to run<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private function createSuite():TestSuite {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ts:TestSuite = new TestSuite();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ts.addTest( SomeTest.suite() );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ts;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; ]]<span style="color: #ddbb00;">&amp;gt;</span><br />
&nbsp; &nbsp; <span style="color: #ddbb00;">&amp;lt;</span>/mx:Script<span style="color: #ddbb00;">&amp;gt;</span></p>
<p>&nbsp; &nbsp; <span style="color: #ddbb00;">&amp;lt;</span>!-- flexunit provides a very handy default test runner GUI --<span style="color: #ddbb00;">&amp;gt;</span><br />
&nbsp; &nbsp; <span style="color: #ddbb00;">&amp;lt;</span>flexunit:TestRunnerBase id=<span style="color: #ddbb00;">&amp;quot;</span>testRunner<span style="color: #ddbb00;">&amp;quot;</span> width=<span style="color: #ddbb00;">&amp;quot;</span>100%<span style="color: #ddbb00;">&amp;quot;</span> height=<span style="color: #ddbb00;">&amp;quot;</span>100%<span style="color: #ddbb00;">&amp;quot;</span> /<span style="color: #ddbb00;">&amp;gt;</span><br />
<span style="color: #ddbb00;">&amp;lt;</span>/mx:Application<span style="color: #ddbb00;">&amp;gt;</span></div>
</div>
</div>
<p></p>
<h3>Test Cases</h3>
<p>The way the framework functions is by running a series of test cases. Each test case is an instance of the TestCase class which may or may not do any number of things. Test cases may also be organized into TestSuites which are little more than a list of test cases which will be run in order if desired. TestSuite.add() may accept either individual TestCase instances or TestSuites.</p>
<p>The constructor for a test case accepts one argument, the name of the method to be called when the test is run. Thus it is possible and advisable to create a single class for each group of related tests and instantiate multiple copies, each instructed to run a different test.</p>
<p>When a test runs it counts the results of all assertion methods called. These methods are:</p>
<ul>
<li>AssertEquals()</li>
<li>AssertFalse()</li>
<li>AssertNotNull()</li>
<li>AssertNotUndefined()</li>
<li>AssertNull()</li>
<li>AssertStrictlyEquals()</li>
<li>AssertTrue()</li>
<li>AssertUndefined()</li>
<li>fail()</li>
</ul>
<p>Each of the assertX() methods takes two parameters, a string describing the error to describe if the test fails and a value to be tested.</p>
<p>Fail may be used to throw error messages if you want to perform more complex tests than the prefab asserts provide.</p>
<p>A sample test case class:<br />
[as3]<br />
package test {<br />
	import flexunit.framework.TestCase;<br />
	import flexunit.framework.TestSuite;</p>
<p>	public class SomeTest extends TestCase {</p>
<p>		// declaring the redundant constructor saves you from having to cast when you construct<br />
		public function SomeTest( methodName:String ) {<br />
			super( methodName );<br />
		}// end: constructor</p>
<p>		// this method returns a suite containing all of the tests provided by this class in<br />
		// the order they should be run<br />
		public static function suite() : TestSuite {<br />
			var ts:TestSuite = new TestSuite();</p>
<p>			ts.addTest( new SomeTest( &quot;testA&quot; ) );<br />
			ts.addTest( new SomeTest( &quot;testB&quot; ) );<br />
			return ts;<br />
		}// end: suite</p>
<p>		public function testA() : void {<br />
			AssertTrue( &quot;True does not equal true?!&quot;, true );</p>
<p>			var tmp:Object;<br />
			AssertNull( &quot;Tmp should be null here.&quot;, tmp );<br />
			tmp = {};<br />
			AssertNotNull( &quot;Tmp shouldn't be null any more.&quot;, tmp );<br />
		}// end: testA</p>
<p>		public function testB() : void {<br />
			fail( &quot;This test always fails&quot; );<br />
		}// end: testB</p>
<p>	}// end: class<br />
}// end: package<br />
[/as3]</p>
<h3>Asynchronous Tests</h3>
<p>FlexUnit offers good support for asynchronous event-driven test cases. AsUnit apparently has also recently (ie, as of early-mid October, 2006) added this support for AS3 projects.</p>
<p>There are a number of asynchronous methods that tests can call but the actual 1st-party documentation on how to use them is a bit thin. In stead, Adobe refers us to hunt down and comprehend the test cases written for the Mappr API. I've attempted a bit of this comprehension. The only one of their test suites to use asynchronous calls is the <a href='http://labs.adobe.com/svn/flashplatform/?/projects/mappr/trunk/src/actionscript3/com/adobe/webapis/mappr/tests/MapprServiceTest.as'>MapprServiceTest</a> class.</p>
<p>Even in this case, they're only using one aspect of the functionality. That is the <a href='http://weblogs.macromedia.com/as_libraries/docs/flexunit/flexunit/framework/TestCase.html#addAsync()'>TestCase.addAsync() </a>call. This function looks like it might be enough for what we need, it adds a checkpoint to the queue (and pauses it?) and then returns a proxy function that looks like it should clear the checkpoint and then execute the next function in your list.</p>
<p>The framework looks like it has more robust options than this for writing asynchronous tests, but I believe this is all we need for now.</p>
<p>A sample class that tests loading data from an external source might look something like this:<br />
[as3]<br />
package test<br />
{<br />
	import flexunit.framework.TestCase;<br />
	import flexunit.framework.TestSuite;<br />
	import flash.net.URLLoader;<br />
	import flash.net.URLRequest;</p>
<p>	public class RemoteXMLTest extends TestCase {<br />
		private static var TIMEOUT:int= 10000;	// 10 seconds<br />
		private var loader:URLLoader;</p>
<p>		public function RemoteXMLTest( methodName:String ) {<br />
			super( methodName );<br />
		}</p>
<p>		public static function suite() : TestSuite {<br />
			var ts:TestSuite = new TestSuite();<br />
			ts.addTest( new RemoteXMLTest(&quot;doIt&quot;) );<br />
			return ts;<br />
		}</p>
<p>		public function doIt() : void {<br />
			var request:URLRequest = new URLRequest(&quot;http://somewhere.com&quot;);<br />
			assertNotNull( &quot;request is null&quot;, request );</p>
<p>			loader = new URLLoader();<br />
			assertNotNull( &quot;loader is null&quot;, loader );</p>
<p>			loader.addEventListener( &quot;complete&quot;, addAsync( onLoaded, TIMEOUT ) );<br />
			loader.load( request );<br />
		}</p>
<p>		public function onLoaded( result:Object ) : void {<br />
			// don't actually have to do anything here, but eh, why not?<br />
			assertNotUndefined( &quot;we got no result?&quot;, result );<br />
		}<br />
	}<br />
}<br />
[/as3]</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2006/11/02/flexunit_basics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>hot router blues</title>
		<link>http://ammonlauritzen.com/blog/2006/01/24/hot_router_blues/</link>
		<comments>http://ammonlauritzen.com/blog/2006/01/24/hot_router_blues/#comments</comments>
		<pubDate>Wed, 25 Jan 2006 04:25:46 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[wifi]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I'm not quite sure when it began, but this problem has been plaguing us for months now. My home network seems to have acquired a sudden and violent allergy to downloading torrents of all shapes and sizes. We have a total of four computers currently on the network, and in regular use. Of them, Rincewind [...]]]></description>
			<content:encoded><![CDATA[<p>I'm not quite sure when it began, but this problem has been plaguing us for months now. My home network seems to have acquired a sudden and violent allergy to downloading <a href='http://www.bittorrent.com/FAQ.html'>torrents</a> of all shapes and sizes.</p>
<p>We have a total of four computers currently on the network, and in regular use. Of them, <a href='http://en.wikipedia.org/wiki/Rincewind'>Rincewind</a> (the old server turned secondary windows desktop) and <a href='http://en.wikipedia.org/wiki/Samuel_Vimes'>Vimes</a> ("my" laptop that Penny monopolizes) generally shun the torrents, letting <a href='http://en.wikipedia.org/wiki/Hedwig_%28Harry_Potter%29'>Hedwig</a> (linux server) and <a href='http://en.wikipedia.org/wiki/By_the_Sword'>Tarma</a> (windows <a href='http://www.microsoft.com/windowsxp/mediacenter/default.mspx'>media center</a> type bedroom computer) handle all the dirty work. Which is fine by them, since they're the machines with the big processors, big ram, and big hard drives (though Rincewind does have a gig of ram and 300gb drive of his own now, shrug).</p>
<p>Traditionally, I have <u>always</u> preferred the command line python scripts to bulky gui applications. So... Hedwig has had a long and only slightly besmirched career as our primary download and redistrobution center.</p>
<p>When Hedwig moved home from the office after I quit my last job, I expected some slowdown - after all, we only have a 5 mbit downstream and 512 kbit upstream. The results of my first batch of downloads led me to believe that the torrent itself was somehow bugged - they were all healthy files with 10 or more seeds and 100 or more general users, yet even after a few hours running, I couldn't manage to eke out more than 5kbps per torrent.</p>
<p>I let this slide for a while in the face of a new problem. One particular studio whose shows I was interested in watching was having problems with their single-episode torrents and recommended people to just use the batch torrents in stead. This is usually fine by me, but in this case, the batches were 25 episodes huge (~4.5gb) and I only needed one file out of the mix.</p>
<p>So, I hunted around for a client that would allow me to selectively download individual files from batch torrents, and was pleased to find <a href='http://www.bitcomet.com/'>BitComet</a>. I installed it on Tarma and fired it up.</p>
<p>Instantly, I noticed a huge difference. BitComet was actually able to utilize my bandwidth and get me transfers at the rates my connection would actually support. It was great. But, I was still running the mud from home and didn't want to lock out players, so I throttled things way back - leaving plenty of bandwidth open for no less than 10 users to do their bidding w/o any problems.</p>
<p>Yet... even with a dedicated portion of the pipe, users suddenly began to complain about inability to log in to the game. Once an active socket was opened, there were no problems, but people were generally incapable of opening said sockets...</p>
<p>About the same time, Penny started complaining from the couch that the laptop was having problems browsing web pages. I noticed that I too began to have problems opening up outgoing connections from any of our machines. I throttled the torrents waay back - leaving 3/4 of our bandwidth unused. And still, the problem did not go away.</p>
<p>I quit BitComet. The problem did not go away. I rebooted Tarma. No such luck there either.</p>
<p>Finally, I gave our router a violent kick to the head and rebooted it forcibly. The network was immediately cleansed and back to normal.</p>
<p>This problem has repeated itself numerous times since that first event. It became so reliable - if I so much as looked at BitComet, my network would sieze up - that I stopped downloading files all together until the mud had left the building. And then, I got into the habit of stopping torrents during the day so I could work, and then turning them back on when I went to sleep. I had assumed that BitComet was somehow opening up so many connections that it managed to exhaust the router's capacity to handle them all.</p>
<p>Until today. <i>[Cue dramatic music and lighting]</i></p>
<p>The answer (I hope) to all of my woes and problems finally hit me. BitComet was not leaving stale connections around. It has been overheating the router. See... they're both fairly new (&lt;1 year old) Linksys models, and this particular line of networking hardware is designed to stack together, so the router was sitting directly on top of the modem.</p>
<p>I figure the high number of different sockets created when added to the high amount of wan communication <b>and</b> the fact that Tarma is connected to the network via encrypted wifi (high amounts of radio energy plus extra computational power spent on every packet to maintain security) probably generated more heat than anything else I've done to this network.</p>
<p>Since, previously, my high traffic transactions had been mostly one-way communication (sending a file between two machines) and my torrents had been on Hedwig (who is plugged into the network via good old fashioned cat6 twisted pair)... yeah.</p>
<p>When I checked on my torrents this morning (<a href='http://www.mahou.org/Projects/Aquarion/'>Sousei no Aquarion</a> for Penny), I noticed that they were acting unusually sluggish. Like really really slow. So, since I needed to get onto the web anyways to work on things, I went in to physically reboot the router. It was almost painfully hot to the touch. The modem underneath it was also practically a puddle of molten plastic.</p>
<p>I smacked myself in the head and proceeded to fashion a cardboard stand for the router from the packaging of some Atari computer game or another. There is now at least an inch of airflow between the two devices and I've had torrents running all day w/o interrupting my work.</p>
<p>I can even post rants on here and stuff <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2006/01/24/hot_router_blues/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

