<?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>A day in the life of...</title>
	<atom:link href="http://www.adayinthelifeof.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adayinthelifeof.nl</link>
	<description></description>
	<lastBuildDate>Wed, 02 May 2012 12:49:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>301 vs 303</title>
		<link>http://www.adayinthelifeof.nl/2012/05/02/301-vs-303/</link>
		<comments>http://www.adayinthelifeof.nl/2012/05/02/301-vs-303/#comments</comments>
		<pubDate>Wed, 02 May 2012 12:49:45 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[see other]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1786</guid>
		<description><![CDATA[During a presentation I gave yesterday about REST, there was a discussion about redirection (more detailed, a redirection from a queue to the actual resource during asynchronous operations). During this presentation (and blog-post), I&#8217;m using a 303 HTTP status code to indicate that the operation has been completed and that the created resource can be [...]]]></description>
			<content:encoded><![CDATA[<p>During a <a href="http://speakerdeck.com/u/jaytaph/p/rest-in-practice-phpbenelux-meetup-may-2012">presentation</a> I gave yesterday about REST, there was a discussion about redirection (more detailed, a redirection from a queue to the actual resource during <a href="http://www.adayinthelifeof.nl/2011/06/02/asynchronous-operations-in-rest/">asynchronous operations)</a>. During this presentation (and blog-post), I&#8217;m using a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4">303 HTTP status code</a> to indicate that the operation has been completed and that the created resource can be found at another URI. So in essence, it makes sense to use a 303. At least to me, and quite possibly the rest of the world too.. But this triggered a side-discussion on which HTTP status code to use, and the more I think about it, the more complex it believe this problem actually is.<span id="more-1786"></span></p>
<p>This is the 303 code specification in the HTTP/1.1 RFC:</p>
<pre>The response to the request can be found under a different URI and SHOULD be retrieved
using a GET method on that resource. This method exists primarily to allow the output of a
POST-activated script to redirect the user agent to a selected resource. The new URI is not
a substitute reference for the originally requested resource. The 303 response MUST NOT be
cached, but the response to the second (redirected) request might be cacheable.</pre>
<p>OK, they are implying that you request &#8220;something&#8221;, but instead of that &#8220;something&#8221;, you are being redirection to &#8220;something else&#8221;. The 303 response should not be cached, but the location the 303 points to, can be cached. This makes sense and still is valid in redirecting a user to the actual resource after the job in the queue has finished. They even provide a use-case, where a POST to for instance a form, will redirect the user to another page. Key here, I think at least, is the fact that the new URI is not a substitute for the old URI. In other words: it is not the same resource, but on another place. The URI points to a completely different resource.</p>
<p>The next sentences:</p>
<pre>The different URI SHOULD be given by the Location field in the response. Unless the
request method was HEAD, the entity of the response SHOULD contain a short hypertext
note with a hyperlink to the new URI(s).</pre>
<p>So we should return the actual resource through the &#8220;Location:&#8221; field, and we should return a bit of hypertext with a href or rel to the new uri (there can be multiple!).</p>
<p>I still think it makes sense to use this code since we are doing two things after completing our asynchronous operation:</p>
<ol>
<li>We want to notify the user that the operation has been completed.</li>
<li>We want to redirect the user to the OUTPUT of the operation (ie, the newly created resource).</li>
</ol>
<p>Both can be fulfilled with a 303 response.</p>
<p>&nbsp;</p>
<p>Now, the suggestion came that you should 301 for this behavior instead of 303. So let&#8217;s read the docs:</p>
<pre>The requested resource has been assigned a new permanent URI and any future references
to this resource SHOULD use one of the returned URIs. Clients with link editing
capabilities ought to automatically re-link references to the Request-URI to one or
more of the new references returned by the server, where possible. This response is
cacheable unless indicated otherwise.</pre>
<p>It states that a 301 is a permanent redirection of the resource to another location (Like a &#8220;we&#8217;re moved!&#8221; sign). However, the actual resource we are asking when doing a /queue/12345 is not the resource it created, but is the actual queue-task itself. And that queue-task hasn&#8217;t moved! When we do a GET /queue/12345 and return a 301 Moved -&gt; /my/new/resource, it implies that this data was originally found on /queue/12345, but this was never the case! The rest of the sentences aren&#8217;t really important in deciding between a 301 or a 303.</p>
<pre>The new permanent URI SHOULD be given by the Location field in the response. Unless
the request method was HEAD, the entity of the response SHOULD contain a short
hypertext note with a hyperlink to the new URI(s).</pre>
<p>Again, this is the same as with a 303, so nothing strange here.</p>
<pre>If the 301 status code is received in response to a request other than GET or HEAD,
the user agent MUST NOT automatically redirect the request unless it can be confirmed
by the user, since this might change the conditions under which the request was issued.</pre>
<p>This is probably here to make sure that we don&#8217;t do a non-safe action to a resource which might have moved in the meantime. But I think this should be handled with etags and if-(not-)modified headers anyway. By the way, there is a draft of a status code that makes it mandatory to use conditional headers when accessing or modifying resources (428 <em>Precondition Required</em>).</p>
<p>&nbsp;</p>
<p>So in conclusion: I&#8217;m still not convinced, at least, according to how I interpret the http spec, that we should use 301 here, instead of 303. If you see it differently (and I know some of you will :p), I&#8217;d like to find out why a 301 is better in your view. But maybe the most important thing about this discussion, and thus this blog-post, is that interpretation of the rules can be difficult on occasion, and maybe the rules are ambiguous enough that there are no clear winners on some of these discussions. However, it&#8217;s good to discuss these things, it makes the whole REST &amp; HTTP way thinking stronger in the end, which in a API-centric world we are entering is definitely not a bad thing :)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F05%2F02%2F301-vs-303%2F&amp;title=301%20vs%20303" id="wpa2a_2"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/05/02/301-vs-303/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Conference retrospect</title>
		<link>http://www.adayinthelifeof.nl/2012/04/25/conference-retrospect/</link>
		<comments>http://www.adayinthelifeof.nl/2012/04/25/conference-retrospect/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 12:29:02 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[conference season]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1770</guid>
		<description><![CDATA[So even in the middle of conference season, I&#8217;d like to update you with some of the awesome things I&#8217;ve seen and experienced over the last few weeks. Whiskyweb &#8211; Edinburgh &#8211; Scotland Whiskyweb is a truly amazing conference. Especially if you know that this conference has been setup from scratch in about 3 months! [...]]]></description>
			<content:encoded><![CDATA[<p>So even in the middle of conference season, I&#8217;d like to update you with some of the awesome things I&#8217;ve seen and experienced over the last few weeks.</p>
<p><span id="more-1770"></span></p>
<h2>Whiskyweb &#8211; Edinburgh &#8211; Scotland</h2>
<p>Whiskyweb is a truly amazing conference. Especially if you know that this conference has been setup from scratch in about 3 months! Even most of the professional conferences out there cannot even organize what Juokaz, Micheal, Max, Paul and all the others involved have done in such a short time. Whiskyweb was held in Edinburgh, Scotland, which is one of the nicest cities I&#8217;ve recently been too. The city center consists of many old buildings and with all the pubs on the Royal Mile, you can&#8217;t go wrong.</p>
<p>&nbsp;</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-13-12.17.15.jpg"><img class="size-thumbnail wp-image-1772 alignright" style="margin-top: 5px; margin-bottom: 5px;" title="2012-04-13 12.17.15" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-13-12.17.15-e1335354300445-150x150.jpg" alt="" width="150" height="150" /></a>On the first day I arrived, I was just in time for the speakers diner where afterwards we all did a sort-of pub-crawl along many of the pubs on the Royal Mile. The next day we had a 15 minute walk to the venue together with BinaryKitten and Geekie who where staying in the same hotel as well. The conference venue was truly awesome: a big church modified for venues with room for a lot of attendees. Unfortunately, the registration process wasn&#8217;t very quick, so the introduction was already started by the time we got our passes. On the conference itself, there were many speakers from all over the world speaking about lots of different subjects. My own talk was in the second room (I get that a lot :p), which was warm and dark, and together with some fairly complex theoretical stuff, it doesn&#8217;t make a really nice combo. However, most people enjoyed it after a long day of the conference though. And off course, what&#8217;s a conference without a flying radio-controlled shark? Nothing off course, which was the reason whiskyweb was guarded by a flying shark (minus the laserbeam on its head). The day ended with a whisky-workshop where we could taste all kind of different wishky&#8217;s, and got an explanation on whisky in general.</p>
<p>&nbsp;</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-14-17.24.011.jpg"><img class="alignleft size-thumbnail wp-image-1776" style="margin: 5px;" title="2012-04-14 17.24.01" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-14-17.24.011-e1335357034640-150x150.jpg" alt="" width="150" height="150" /></a>The second day was all about hackathons. It was more a do-what-you-like venue, but groups were form quickly en0ugh doing all kinds of stuff. Josh Holmes gave a workshop on speaking, I did some introduction on Puppet and Lorna hacked away on the joind.in API so others could actually use them for their own projects they were working on. The coolest projects I&#8217;ve seen that day was from two guys who wanted to do some work with Puppet and Vagrant, and managed to setup the joind.in application through vagrant, which is now in the process of being added to the master branch (yay!). And this actually shows you what a hackathon is all about.</p>
<p>&nbsp;</p>
<h2>4developers &#8211; Poznan &#8211; Poland</h2>
<p>The following day I had to fly over to Poland for the 4developers conferences. This was my second time around at 4developers although the last time was in Warsaw. A lot of the speakers from Whiskyweb did an appearance at 4developers as well, so it was great fun to see the same group of people again, just in another country (that happens a lot by the way).</p>
<p>The 4developers also started with a really nice speakers dinner on the market square in the center of Poznan (at which we were drinking beer the whole afternoon already). Off course, a good speakers dinner in Poland cannot be ended without having a few bottles of wodka. There was even a point that we though that some of the speakers would never make it on time for their talk (or at least, not sober :p).</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-19-13.43.30.jpg"><img class="alignleft size-thumbnail wp-image-1778" style="margin: 5px;" title="2012-04-19 13.43.30" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-19-13.43.30-e1335357153427-150x150.jpg" alt="" width="150" height="150" /></a>Fortunately, the venue was just a 5 minute walk away from our hotel and it was really nice venue. This time, the PHP track was the biggest track, with some small java, scaling and management tracks on another floor. The venue looks really great and futuristic and there 4developers bug mascot looked really nice (we&#8217;ve got the t-shirt). Maybe not every track was properly programmed though: Rowan Merewood&#8217;s track was pretty popular but was in the IT -track, meaning there wasn&#8217;t even enough room for all the attendees.  At this conferences, both Skoop and I did our first joint talk about &#8220;the why and how of learning&#8221; in the PHP track, which was received really good by the audience. Off course, we need to do some more fine-tuning, but expect this talk at a conference near you :).  Since I did two talks, my second talk was about one of my favorite subjects: Puppet. Even though it was in a small room, the room was pretty full and most people seemed to enjoy the talk and learned a thing or two about configuration management. After the conference, we all did another speakers-dinner (yay!) which was quite nice as well. Back in the hotel we went over for a beer at the bar.</p>
<p>The next day we had to leave, but our taxi-ride to the airport was not there until 5 so we had time enough to spend our day in the center of Poznan as well. So with a gang of 5 we went into town for a beer and something to eat (not as easy as it might sound) and end up enjoying the &#8220;fake jet-set life&#8221; on a nice day in the sun in Poznan.</p>
<p>&nbsp;</p>
<h2>Mail.ru Techforum &#8211; Moscow &#8211; Russia</h2>
<p>With a whopping 2 days time off back home, I had to catch my next flight to Moscow. Never been there, so I really didn&#8217;t know what to expect. Actually, to be honest, I was expecting a lot, but not traffic jams. It took the taxi around 2.5 hours for a 40km ride between the airport and Moscow city center (at least I had time to look around).</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-23-23.11.47.jpg"><img class="alignright size-thumbnail wp-image-1773" title="2012-04-23 23.11.47" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/2012-04-23-23.11.47-e1335355859572-150x150.jpg" alt="" width="150" height="150" /></a>Luckily, some familiar faces like JP Mens and Kris Buyteart where also present at the conference, so when I checked in the hotel, they were all sitting in the lobby enjoying some beer. After dropping off my luggage in my room (room number 404, and still able to find it!), we went out for a nice Russian dinner. After getting back in the hotel at around 10, Garret Honeycutt and I decided to go for some sightseeing, so we went up to the Kremlin and the red square late in the evening, which was absolutely an awesome sight!</p>
<p>&nbsp;</p>
<p>The next morning we were picked up by Kate (Екатерина), who escorted us to the venue. This conference was organized by one of the biggest providers of europe: mail.ru, and it showed: it was a massively impressive conference where in total over 1100 attendants have shown up (mind you: it was a free event as well!). All the talks were done in Russian, and all the talks from the English speakers were translated in real time through an interpreter (very U.N. like). As for the rest, the conference itself was just awesome: photographers taking pictures of everybody and everything, which were printed on the fly so you could get a hard copy of those pictures if you wanted (for free). Bottled water with the logo and date of the conference, personalized badges for everybody with a QR code for easy scanning and exchange and even the talks were broadcasted into the lobby so others could see them as well. Oh, and we could give away a free ipad to the person with the best question for each presentation (which means about 22 ipads in total to give away :)).</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/IMG_1951.jpg"><img class="alignright size-thumbnail wp-image-1774" title="IMG_1951" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/04/IMG_1951-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>Unfortunately, I had to rush to the airport after my talk in order to catch my flight so I didn&#8217;t time enough to answers everybodies question after the talk (I&#8217;m really sorry guys, if i&#8217;m there next year I will make sure I have time enough to answer ALL your questions), so after running 10 minutes late already, plus doing a quick 5 minute interview, I was on route back to the airport after just over 24 hours of Moscow. And yes, traffic was hell again, and it took around 2 hours to get to the airport, just in time for catching the flight with a whopping 20 minutes to spare.</p>
<p>So.. it&#8217;s a few weeks off, doing some meetups in between, so don&#8217;t miss out on the PHPBenelux meetup on the first of may, and a rerun at may 3rd at Competa. Next up are a social / holiday-esque trip to Verona during the phpday Italy, and off course PHP|tek in Chicago plus the DPC in Amsterdam where I will be doing quite a number of talks on an even broader range of subjects. Don&#8217;t miss out!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F04%2F25%2Fconference-retrospect%2F&amp;title=Conference%20retrospect" id="wpa2a_4"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/04/25/conference-retrospect/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bloom filters</title>
		<link>http://www.adayinthelifeof.nl/2012/04/09/bloom-filters/</link>
		<comments>http://www.adayinthelifeof.nl/2012/04/09/bloom-filters/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 10:02:16 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1748</guid>
		<description><![CDATA[In a span of two months or so, I&#8217;ve noticed a peak in implementation of bloom filters. Maybe the &#8220;if you got a hammer, everything looks like a nail&#8221; applies here, but statistically I&#8217;m doing a larger number of bloom filter implementation as usual.  Yet, most of my co-workers never really heard of bloom filters, [...]]]></description>
			<content:encoded><![CDATA[<p>In a span of two months or so, I&#8217;ve noticed a peak in implementation of bloom filters. Maybe the &#8220;if you got a hammer, everything looks like a nail&#8221; applies here, but statistically I&#8217;m doing a larger number of bloom filter implementation as usual.  Yet, most of my co-workers never really heard of bloom filters, and I&#8217;m continuously need to explain what they are, what their purpose is and why it&#8217;s a better solution than other ones. So let&#8217;s do an introduction on bloom filters.</p>
<p><span id="more-1748"></span></p>
<h2>Defining the problem</h2>
<p>So we already know the solution will be a Bloom Filter, but what is the problem they can solve? Suppose you have a book which obviously contains a lot of words. My problem: I want to know if a certain word is actually inside the book. I don&#8217;t care where it is, or how many times it&#8217;s inside the book: I just want to know if this book contains this specific word or not. As a good PHP developer, you know PHP has got a fair amount of array functionality, so let&#8217;s try and use those:</p>
<pre class="brush: php">$words = array();
$book = file_get_contents("book.txt");
foreach (split(" ",$book) as $word) {
  $words[strtolower($word)] = 1;
}

if (array_key_exists("supercalifragilisticexpialidocious", $words)) {
  print "Yes, this word is used in the book";
} else{
  print "No, this book is probably not a Mary Poppins book";
}</pre>
<div>Now this is a perfectly valid solution: we store each word of the book as the key of the array so when we encounter a word we already have added, it will not be added twice. However, depending on the size of the book, this method is <a href="http://nikic.github.com/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html">quite memory consuming</a>. Luckily the size of the array doesn&#8217;t really affect the time to look for a word. Or to put it differently: the time it takes to check if a word exists is probably the same for an array with 10 words, as it is with an array with 10 million words (so called <a href="http://www.adayinthelifeof.nl/2009/12/21/big-o-notation/">O(1) or constant time operations</a>).</div>
<div></div>
<div>But still we are using a lot of memory, there must be better structures and there are: the bloom filter.</div>
<div></div>
<h2>Introducing the bloom filter</h2>
<div>Bloom filters have the property of being exceptionally fast AND exceptionally small compared to other structures but it comes with a price: it MIGHT be possible that our bloom filter thinks that an element is inside our set, when it really isn&#8217;t. Luckily, the reverse is not possible: when a bloom filter says something is NOT in the set, you are 100% sure that it isn&#8217;t part of the set.</div>
<div></div>
<div></div>
<div>Now I know most developers hate the word &#8220;maybe&#8221;, since you cannot really convert the term &#8220;maybe&#8221; into binary logic unless you start using fuzzy logic, so how does this help us? If we go back to our book-example, we could ask the bloom-filter if a word is inside the book. It will return with: no or maybe. When the answer is &#8220;no&#8221;, we know for a fact that the word is not in this book, and we could for instance, check the next book until we hit a &#8220;maybe&#8221; result. In this case, we need to check the book word for word in order to verify that the word is actually used.</div>
<div></div>
<div></div>
<div>This does speed up the search process a lot: first of all, we can store many bloom-filters in memory so we can scan very fast if words are definitely NOT present inside a range of books. When we do find a &#8220;maybe&#8221;, all we need to do is to get a the array of that specific book so we can find out for sure if the word is present (assuming you need 100% certainty, otherwise you don&#8217;t even need to do this). Our gain is in the fact that A) we can store many more bloom filters, and thus search more books in a smaller amount of time and B) we only need to verify the books where we actually might have a hit.</div>
<div></div>
<div></div>
<div>This is also a perfect filter for unnecessary disk access: suppose we store keys onto disk (or another slow medium like a database), where we need these keys on occasion. Now, first we ask the bloom filter if the key is present. If it says &#8220;no&#8221;, we know the key isn&#8217;t there so we don&#8217;t need to check any further. However, when it says &#8220;maybe&#8221;, we will fetch the key from that other medium (database or disk). If it&#8217;s there, we can return this info, but when we have &#8220;false positive&#8221;, (bloom filter said: maybe, but in the end, it wasn&#8217;t there), then we just have done a unnecessary read to that medium. However, we only do this on occasion, and not for ALL our requests. In the end, bloom filters are a great way to filter out unwanted slow access request.</div>
<div></div>
<div></div>
<h2>How does it work</h2>
<div>How can a data structure tell us either &#8220;no&#8221; or &#8220;maybe&#8221;. Can&#8217;t it just tell use &#8220;yes&#8221; or &#8220;no&#8221;? Well.,.. no.. :)  Let&#8217;s take a look how a bloom filter is made out of:</div>
<div></div>
<div>We have a bit-array of &#8220;m&#8221; bits. Let&#8217;s say, m = 20. Each of these bits are set to 0.</div>
<div></div>
<div>
<pre>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-----------------------------------------
                    1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0</pre>
</div>
<div></div>
<div>Now, we have a number of hash functions. This amount is called &#8220;k&#8221;. A hash function is a function that turns a block of data into a number, in our case a number between 0 and m. These hash functions are pretty similar to md5, crc32 and sha1, but are much faster. Now, what happens is that when we add data to our bloom filter, this data gets hashed by all the hashes in the bloom filter (so k times). Let&#8217;s assume the following:</div>
<div></div>
<ul>
<li>data: &#8220;key1&#8243;</li>
<li>k1(key1) = 4</li>
<li>k2(key1) = 3</li>
<li>k3(key1) = 15</li>
</ul>
<div></div>
<div>This means: our data &#8220;key1&#8243; is hashed to 4, 3 and 15 according to our hashes. Now, the bloom filter will set these bits in our array to 1.</div>
<div></div>
<div>
<pre>0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
-----------------------------------------
                    1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0</pre>
</div>
<div>Let&#8217;s add another key:</div>
<ul>
<li>data: &#8220;another key&#8221;</li>
<li>k1(key1) = 8</li>
<li>k2(key1) = 19</li>
<li>k3(key1) = 3</li>
</ul>
<div></div>
<div>And of course, add these numbers to our array as well:</div>
<div>
<pre>0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0
-----------------------------------------
                    1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0</pre>
</div>
<div>You see that the &#8220;3&#8243; was already set, so it stays set to 1. Now, let&#8217;s ask our bloom filter if &#8220;somekey&#8221; is present. We start the same way as adding data: we use the same hash functions:</div>
<div>
<ul>
<li>data: &#8220;some key&#8221;</li>
<li>k1(key1) = 1</li>
<li>k2(key1) = 10</li>
<li>k3(key1) = 19</li>
</ul>
</div>
<div></div>
<div>Now we search the bloom filter and find if ALL of these bits are set to 1. In this case, only bit 19 is set, but bit 1 and bit 10 aren&#8217;t. We can say for 100% certainty that &#8220;some key&#8221; is NOT in our bloom filter.</div>
<div></div>
<div>But let&#8217;s try with &#8220;key1&#8243;. We already know that the hashes will be 4, 3 and 15. And all these bits are set inside our bloom filter. Now, you might be tempted to think that we can just return a &#8220;yes&#8221;, but there is a problem with this. Let&#8217;s try another key:</div>
<div>
<div>
<ul>
<li>data: &#8220;unknown key&#8221;</li>
<li>k1(key1) = 3</li>
<li>k2(key1) = 19</li>
<li>k3(key1) = 4</li>
</ul>
</div>
<div></div>
<div>If we are checking for this key in our filter, we actually see that bits 3, 4 AND 19 are all set, yet we never have added this key to the bloom filter! This is because these hash all return numbers that have previously been set by other keys and this is where the &#8220;maybe&#8221; comes from. The bloom filter cannot know if these bits happen to be there because you&#8217;ve added the key to the set, or that by coincidence all bits happened to be set by other keys.</div>
</div>
<div></div>
<h2>Error rates</h2>
<div>This means that the more hash functions you use, the less chance of errors will be. Also, the more elements we add to the bloom filter, the higher the chance of errors. There are some mathematical formula&#8217;s available which you can calculate the chance of errors given the number of elements inside a bloom filter, but most implementations of bloom filters just give you 2 options: how many elements are you going to store, and what is the error rate you need. With these two variables, these implementations can calculate how many hash functions it needs, and/or how large the bit array should be.  There is actually <a href="http://hur.st/bloomfilter">a nice calculator</a> that shows you how this works.</div>
<div></div>
<div>There are other things to take care of: obviously: when ALL the bits of a bloom filter are set, it will ALWAYS return &#8220;maybe&#8221;. Some bloom implementations can also return the probability of the maybe so you can assume it&#8217;s a &#8220;yes&#8221; when it has a 90%+ probability, and do another check if it&#8217;s lower for instance. Again, this all depends on your implementation and needs.</div>
<div></div>
<div></div>
<h2>Conclusion</h2>
<div>For PHP, there is a really nice and simple PECL extension called <a href="http://pecl.php.net/package/bloomy">bloomy</a> which you can create large bloom filters quite easily, but better yet: why not try and create a bloom filter in PHP yourself! Bloom filters by itself can be very useful for large sets where you only need to determine presence. It&#8217;s not a key-value store so you probably need other structures as well, but simple bloom filters can be a really good way to speed up your performance and decrease your memory usage.</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F04%2F09%2Fbloom-filters%2F&amp;title=Bloom%20filters" id="wpa2a_6"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/04/09/bloom-filters/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHPShout : a shoutcast streamer in PHP: Part 4</title>
		<link>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-4/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-4/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 14:27:33 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1734</guid>
		<description><![CDATA[In the last post, we created a template extension for our shout class. Next up, we need to do the actual implementation. Part 5: creating setters Let&#8217;s take a look at the source from this commit: https://github.com/jaytaph/phpshout/blob/99fed246064b5046ec18b848bf779359353268a0/shout.c Creating the setters is just as easy as creating the getters. The same applies with the setters: we [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-3/">last post</a>, we created a template extension for our shout class. Next up, we need to do the actual implementation.<img title="More..." src="http://www.adayinthelifeof.nl/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" /></p>
<p><span id="more-1734"></span></p>
<h2>Part 5: creating setters</h2>
<p>Let&#8217;s take a look at the source from this commit: https://github.com/jaytaph/phpshout/blob/99fed246064b5046ec18b848bf779359353268a0/shout.c</p>
<p>Creating the setters is just as easy as creating the getters. The same applies with the setters: we can use a generic function for setting data (we need a few, depending on the type we need to set), but it all work the same way.</p>
<p>Check out line 161 to 212 on the generic setters. Notice that on line 197 we are getting a long integer from PHP, but we need to store a short int. So we mask only the short in part (0xFFFF). This means we can still add a value that is higher than 65535, but it will gets converted to a short. It also would be possible to check the value, and if the value is higher than 65535, it will throw an error.</p>
<p>Like 215 and 216 are special cases, since they will return the error number and error string of the shout library. There are not setters for this. The rest of the functions all have a getters and setters.</p>
<p>&nbsp;</p>
<h2>Part 6: Other functionality</h2>
<p>We&#8217;re getting REALLY close in finishing up our extension. All that is left to do is write the functionality for all the non-getter/setter functions. The first one is already there on line 263. This function returns a bool(true) when your  shout object is connected to a stream, or false otherwise. You will see we are doing a mapping on line 271 so it will return either true or false.</p>
<p>&nbsp;</p>
<p>Now, let&#8217;s take a look at all the functions added. This is done in commit: https://github.com/jaytaph/phpshout/blob/35ac2b32009936e53c3b336efed36a604d37d9f3/shout.c</p>
<p>First off all, I&#8217;ve changed the layout a bit for the getters and setters on line 220 to 373. The open(), close() and sync(), send() and delay() methods  should look really familar by now. The same goes for the get_audio_info() and set_audio_info() methods on line 472 and line 489. The only thing you will see is that the set_audio_info() accepts 2 strings (a key and a value) from PHP. We talk about the set_metadata() function later, but the get_queue_length() function is also a simple one.</p>
<p>On line 613, you will see our structure filling up quite nicely with public methods  now :-)</p>
<p>&nbsp;</p>
<p>Now, the only thing left to do now is talk about the set_metadata() function. This function is a bit different than the rest, since this function actually accepts an array. Internally in the libshout library, the metadata acts like an array. So from a PHP point of view, it makes sense to use arrays for this. So what we do on line 515 is accept an array. Then on line 520, we initialize a shout_metadata structure that will hold the data we want to set.</p>
<p>The for-loop on line 523 to 525 can been seen as a loop over all the elements from the array we just got. Then on line 533 we call  SEPARATE_ZVAL with the value for the current element. This separation actually creates a copy (but not really) of the current value so we can change it. If we leave this out, all changes to this value will also reflect back in the original PHP array, which we don&#8217;t want.</p>
<p>The convert_to_string makes sure that the value is always a string, which comes in handy since we need strings to store in our metadata structure.</p>
<p>Next on line 537, we try and get the key for the current value we are iterating. This key can either be a string or a number. If it&#8217;s a number, it means that the &#8220;str_key&#8221; value is not filled, and we will make sure that this number is converted into a string nevertheless. We do that on lines 538 to 541.</p>
<p>When all is done, we have a string with the key, and a string with the value (both not binary safe, but we still don&#8217;t care about that :)), and add it to our metadata structure through shout_metadata_add. The Z_STRVAL_PP is a macro that returns the string value for that current element.</p>
<p>When we are done with iterating, we can set the metadata on line 549, and afterwards we can free the metadata structure on line 550. We return the status code of the setting of the metadata on line 552.</p>
<p>&nbsp;</p>
<h2>And we&#8217;re done (for now)</h2>
<p>That&#8217;s it really. All the functionality from libshout is implemented! I&#8217;ve added an example on how to use the extension on github as well (https://github.com/jaytaph/phpshout/blob/35ac2b32009936e53c3b336efed36a604d37d9f3/example/example.php), but make sure you use the latest version from https://github.com/jaytaph/phpshout/.</p>
<p>&nbsp;</p>
<p>To test if it works, I&#8217;ve done the following. On my machine, I&#8217;ve setup an icecast server (I&#8217;m running OSX, so i&#8217;ve used a trail version of nicecast, but there are others). I&#8217;ve setup the server and changed this info in the example.php configuration (primarily the ip, port and password). Then, I added a file.mp3 to the directory so there is something to stream and started the example. If everything goes ok, you see a . appearing for every 4096kb of data that is stream to the server.</p>
<p>Next, I connected a few listeners (they connect to http://192.168.1.10:8000/listen.m3u, in my case) and dance away to my new PHP streamer!!  Oh the fun!</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-24-at-15.25.24.png"><img class="aligncenter size-full wp-image-1735" title="Screen Shot 2012-03-24 at 15.25.24" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-24-at-15.25.24.png" alt="" width="906" height="180" /></a></p>
<p>&nbsp;</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-24-at-15.25.181.png"><img class="aligncenter size-full wp-image-1737" title="Screen Shot 2012-03-24 at 15.25.18" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-24-at-15.25.181.png" alt="" width="671" height="531" /></a></p>
<h2>Conclusion</h2>
<p>Cool things are cool, and writing something from scratch is one of them. Off course, there are hundreds better ways to stream data, but now it&#8217;s possible to do this from PHP as well. Lets stream your MP3 data, change the set_metadata according to the ID3 tags inside, show an upcoming playlist, a previous playlist, or even let people up/down vote for their favourite tunes and create a php jukebox. Who needs spotify or last.fm anymore? :p</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F24%2Fphpshout-a-shoutcast-streamer-in-php-part-4%2F&amp;title=PHPShout%20%3A%20a%20shoutcast%20streamer%20in%20PHP%3A%20Part%204" id="wpa2a_8"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHPShout : a shoutcast streamer in PHP:  Part 3</title>
		<link>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-3/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-3/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 13:50:42 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1730</guid>
		<description><![CDATA[In the last post, we started with the implementation of the constructor and one method. Next up, let&#8217;s do a bunch more. Part 4: Populating our store Let&#8217;s take a look at the source from this commit: https://github.com/jaytaph/phpshout/blob/8675c69c27675d4e1159a754a634f08fffc07746/shout.c You see there is something changed in our store on line 40: there is a pointer to [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-2/">last post</a>, we started with the implementation of the constructor and one method. Next up, let&#8217;s do a bunch more.</p>
<p><span id="more-1730"></span></p>
<h2>Part 4: Populating our store</h2>
<p>Let&#8217;s take a look at the source from this commit: https://github.com/jaytaph/phpshout/blob/8675c69c27675d4e1159a754a634f08fffc07746/shout.c</p>
<p>You see there is something changed in our store on line 40: there is a pointer to a shout structure. This structure is used by the shout library so every time we create a new object store, we should also initialize a shout object. You will see that things have changed in the constructor method on line 85. We fetch the actual store, and create a new shout object by issuing a shout_new(). If something goes wrong, and the shout object is NULL, we throw an exception.</p>
<p>Because we are adding things to our store, we must also make sure these structures gets freed when we don&#8217;t need them anymore. So in our shout_object_free function (line 225), we also free our shout structure by calling shout_free().</p>
<p>If you look closely to this file, you will see that something also has changed on line 352. Here we actually set the handler for cloning objects to NULL, meaning we can&#8217;t clone the object (try it, it will fail now). This is done because when we need to clone, we should also clone our internal data like the shout structure. Unfortunately, this data is very complex which means we need to copy over all the data, but also duplicating some of this data (because otherwise we end up changing data in two different places, for two different objects, which is the whole point of cloning to get rid of). So instead of dealing with this issue, I&#8217;ve decided to not allow cloning at all for the time being.</p>
<p>Also another thing that has changed, is that i&#8217;ve implemented a PHP_MSHUTDOWN function on line 386, and added this to the module entry structure on line 409. This will call the shout_shutdown(), to make sure things gets shutdown properly.</p>
<h2></h2>
<h2>Part 5: Add getters.</h2>
<p>A lot of the libshout functionality are actually based on getting and setting information, just like &#8220;normal&#8221; PHP getters and setters. This shout.c file actually has two ways of dealing with getting values which I will show you both.</p>
<p>The first method is the easiest to explain. Take a look at line 194, where there is a get_host PHP_METHOD.</p>
<p>Since getting stuff do not require any php parameters, the zend_parse_parameters_none() will check if there are any. If so, the php method will return false. On line 201, it will load the internal store. Line 202 will return a string value with the host as returned by the shout_get_host() function. This function needs a shout_t structure, which is the one we are saving in our store. From a PHP point of view, nobody needs to know anything about this shout_t structure so all this is done internally. As said, it will return a string with the current host.</p>
<p>Let&#8217;s take a quick look at setting info as well. Line 205 creates a set_host method. In this case, we DO need parameters, namely a string from PHP, which is the host we need to set. Line 210 will parse the parameters we get from PHP, makes sure we only get 1 string (that is the &#8220;s&#8221;), and this information is placed in the &#8220;host&#8221; and &#8220;host_len&#8221; variables. Remember I was talking about binary safe strings? The host is just a block of data which may or may not have \0 in them. The host_len is a integer that actually holds the length of the string. If we are really correct, we cannot assume that we can just use the &#8220;host&#8221; as a normal string because it isn&#8217;t. But for now this doesn&#8217;t matter, if you want to set a non-binary safe string as a host, that is perfectly fine, but just don&#8217;t think it will work that way:</p>
<pre class="brush: php">$shout-&gt;set_host("Not a binary\x00 safe string");
print $shout-&gt;get_host();  #returns: "Not a binary"</pre>
<p>Line 215 will actually set the host name by supplying our shout-t structure and the (non-binary-safe) host string. As a result, it will return a status code (the RET_* constants), which are LONG values, hence the RETURN_LONG. Notice that we don&#8217;t need to duplicate longs just like we need to do with RETURN_STRING.</p>
<p>&nbsp;</p>
<p>Now, back to the getters, as you can see they are pretty easy to write, but in C, writing a function for each getter and for each setter would result in lots of duplicate code. There must be better ways to deal with this. And there is.</p>
<p>On line 95, I&#8217;ve added a (static) function called php_shout_get_handler_string. The argument list looks wierd, but again, it&#8217;s a ZEND macro:</p>
<div id="LC95">
<pre class="brush: cpp">static void php_shout_get_handler_string(INTERNAL_FUNCTION_PARAMETERS, const char *(*func)(shout_t *)) {</pre>
</div>
<div>The INTERNAL_FUNCTION_PARAMETERS is just a way to copy all the parameters over from another function. We deal with that later. The next argument is merely a pointer to a function (that returns a string, and needs a pointer shout_t structure). Line 98 is familiar: we check if there aren&#8217;t any PHP arguments given to this function. (don&#8217;t worry that this is not a PHP_METHOD).</div>
<div></div>
<div>Line 102, will fetch our internal store, and on line 103 we call the function we just supplied by our argument lists (ie: variable functions), together with our shout structure. There is something to consider, namely the fact that these &#8220;get&#8221;-functions that returns strings, can also return NULL values when nothing has been set. In that case, we like to return a php NULL value, which we do on line 105. Otherwise, we return the actual string on line 107.</div>
<div></div>
<div>This function is a generic getter function for strings, but we must deal with longs and booleans a little bit different (for instance, we must use RETURN_LONG and RETURN_BOOL). So we have multiple getters functions. Notice that we also have a getter for short integers, something that the libshout also can return (for the port numbers), but we convert that data to a long.</div>
<div></div>
<div>On line 147 and below, you see that we actually define the PHP_METHODS for each of these getters. We call the php_shout_get_handler_* function, depending on the type it will return, together with a INTERNAL_FUNCTION_PARAMETER_PASSTHROUGH, which means all the PHP parameters gets passed onto another function. The last parameter is the actuall libshout function that needs to be called. So in a sense we have written lots of getters in just a few lines of code (can maybe be written better and even in less code).</div>
<div></div>
<div>Again, don&#8217;t forget to add these functions to the shout_funcs structure on line 260!</div>
<div></div>
<div></div>
<p>&nbsp;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F24%2Fphpshout-a-shoutcast-streamer-in-php-part-3%2F&amp;title=PHPShout%20%3A%20a%20shoutcast%20streamer%20in%20PHP%3A%20%20Part%203" id="wpa2a_10"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHPShout : a shoutcast streamer in PHP:  Part 2</title>
		<link>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-2/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-2/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 13:15:26 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[icecast]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[streaming]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1722</guid>
		<description><![CDATA[In the last post, we created a template extension for our shout class. Next up, we need to do the actual implementation. Part 3: create initial functionality. Let&#8217;s take a look at the source from this commit: https://github.com/jaytaph/phpshout/blob/d9ad66512f23ff04ff2bb5822b833090d0a5d05b/shout.c Here we actually created some simple functionality so we can use our shout-class in PHP (although it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-1/">last post</a>, we created a template extension for our shout class. Next up, we need to do the actual implementation.<span id="more-1722"></span></p>
<h2>Part 3: create initial functionality.</h2>
<p>Let&#8217;s take a look at the source from this commit: https://github.com/jaytaph/phpshout/blob/d9ad66512f23ff04ff2bb5822b833090d0a5d05b/shout.c</p>
<p>Here we actually created some simple functionality so we can use our shout-class in PHP (although it&#8217;s not doing very much).</p>
<p>We&#8217;ve added a php method on line 49. We use the PHP_METHOD macro for this, so php knows this is a method called &#8220;get_version&#8221; for the shout class. Internally it will create a function called zim_shout_get_version method since C isn&#8217;t OO or namespaced. This way, we can still distinguish our php methods (among other things). Line 50 will check if there are any parameters given to this method. If there are, it will return false since we do not accept any parameters.</p>
<p>Upon return on line 54, it will return the version of our shout library by calling shout_version, and returning this information as a PHP string. This is what the RETURN_STRING does, which is a macro that creates a PHP value in C the easy way. the &#8220;1&#8243; parameter means that the string must be duplicated. Depending on your needs, this might be always the case since this will allow PHP to garbage collect unused variables.</p>
<p>So, this is a really easy function that does not much, but it shows you a bit on how &#8220;wrapping&#8221; a library works: we have a library function, we define a php function for that, we modify (optional) arguments from php so they can be fed into the library function and we return a result back to PHP. In fact, lot of PHP extensions work this way.</p>
<p>Now, let&#8217;s skip the constructor method on line 59 for now, and pay attention to line 148. Here you see we are filling our &#8220;shout_funcs&#8221; structure with the new get_version we just defined. we use ZEND_ACC_PUBLIC to tell that this function is a public function. So all that is left to do is create our PHP methods, add them to this structure and we&#8217;re done!</p>
<p>But there are some other things we can do, for instance creating class constants. This we do on line 240 to 261 (there are a lot of constants).</p>
<p>Again, PHP/Zend makes it really easy for use to create consants (strings or longs doesn&#8217;t matter). We just call zend_declare_class_constant_long, with our class_entry we just have saved ,the actual name and length of the class (the ERR_* constants for instance), and the actual value they represent. In our case, our constants are mapped to constants from the library file.</p>
<p><strong>The ZEND_STRL macro:</strong></p>
<p>Strings in PHP are binary safe. In C, they are not. Strings in C are normally terminated with a 0-byte, meaning that the string itself cannot hold a \0 value. And normally this is ok, exception when you are dealing with binary information in strings. So a lot of times, functions in PHP ask for a string PLUS the actual length of that string since it might not be possible for PHP itself to check the length by looking at the \0 byte. The ZEND_STRL macro returns the string PLUS the length of that string (assuming it&#8217;s NOT a binary string), so it&#8217;s easy to use in your code.</p>
<p>So in essence, these two functions are 100% similar:</p>
<pre class="brush: cpp">test("hello world", strlen("hello world")-1);
test(ZEND_STRL("hello world");</pre>
<p>Now, PHP has got lots of these nifty macro&#8217;s, and not everybody uses them, or even know them.</p>
<p><strong>The constructor</strong>:</p>
<p>Let&#8217;s go back to our constructor method. On line 147, we see the constructor is added to our shout_funcs structure again. You will see that is has another flag namely ZEND_ACC_CTOR, which tells us this is a constructor. (I don&#8217;t know what happens when you place this flag onto anther function though). And the actual constructor method we define from line 59.</p>
<p>This looks a bit complicated with a lof of #if statements, but this is because things have changed in the past on how to handle things. This way, we can be sure that our extension can run on older versions of the zend-module-api (and thus older php versions) as well. There are actually more places where stuff like this needs to be done (when you want to support REALLY old PHP versions), but we can assume we don&#8217;t (since we are doing OO here).</p>
<p>We are basically saving and setting our own exception class, so when exceptions are thrown, it will be handled by our exception class. (I noticed there is a (big) bug in this code, which I let you try to find, while I will update my code :p). On line 78 we fetch our store object, something we will do a lot and store this in intern. It&#8217;s a common and easy way to get to our internal data, in case we need to.  For now, this line doesn&#8217;t serve any point since we don&#8217;t use the intern value anywhere.</p>
<p>On line 81, we are making a call to shout_init(), which initializes the shout library. There are some things we need to consider.</p>
<p>Right now, we are calling shout_init for EVERY object we are creating since it&#8217;s in it&#8217;s __construct(). However, this might not be what we want. We only want to initialize it once. So another option would be to place it in our MINIT function, which gets called only once. A perfect spot, but this means the shout_init() gets called, even for scripts that do not even use any shout functionality, which is something we don&#8217;t want either. For now, we don&#8217;t really care, since calling shout_init() multiple times isn&#8217;t a problem, but some library inits might have a problem with this. So make sure that when you call initialization functions, it&#8217;s done at the proper time.</p>
<h2>Tying it all together:</h2>
<p>Why not try and see if this thing actually works? :-)</p>
<ol>
<li>Make sure you have got the libshout3-dev package installed.</li>
<li>Download the tarball for this blog-post: https://github.com/jaytaph/phpshout/tarball/blogpost-002</li>
<li>Untar this into a directory.</li>
<li>Run &#8220;phpize&#8221;.</li>
<li>Run &#8220;./configure&#8221;. If there is a complaint about not finding the library, install the library first or use &#8220;./configure &#8211;with-shout=&lt;path&gt;&#8221; with the path to the library (if it&#8217;s on /home/user/libshout/include/shout/shout.h&#8221;, use /home/usre/libshout as the path).</li>
<li>Run &#8220;make&#8221;, this will actually build the extension</li>
<li>Run &#8220;sudo make install&#8221;, (or make install as root) so the library will be installed.</li>
<li>Add the extension to your php.ini:   &#8220;extension=shout.so&#8221;</li>
<li>Check if the extension is working by issuing &#8220;php -m | grep shout&#8221;. This should show &#8220;shout&#8221;. You can also check phpinfo() on your webserver, but make sure the server has been restarted</li>
</ol>
<p>Now you run your own scripts with shout capabilities (allthough very limited :p).</p>
<pre class="brush: ruby">&lt;?php

$shout = new Shout();
print "Our shout vesion: ".$shout-&gt;get_version()."\n";</pre>
<p>which should output:</p>
<pre class="brush: plain">Our shout vesion: 2.2.2</pre>
<p><a href="http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-3/">In the next part, we will fill the rest of the functionality.</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F24%2Fphpshout-a-shoutcast-streamer-in-php-part-2%2F&amp;title=PHPShout%20%3A%20a%20shoutcast%20streamer%20in%20PHP%3A%20%20Part%202" id="wpa2a_12"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHPShout : a shoutcast streamer in PHP:  Part 1</title>
		<link>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-1/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-1/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 12:33:24 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[icecast]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[streaming]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1714</guid>
		<description><![CDATA[To continue our journey in pointless, but nevertheless fun things to create, I&#8217;ve created a simple PHP extension that allows you stream music data to an IceCast server in pure PHP. For this I&#8217;m using the libshout3 library which can stream both MP3 or OGG/Vorbis data to multiple stream servers (including IceCast, ShoutCast etc). In [...]]]></description>
			<content:encoded><![CDATA[<p>To continue our journey in pointless, but nevertheless fun things to create, I&#8217;ve created a simple PHP extension that allows you stream music data to an IceCast server in pure PHP. For this I&#8217;m using the libshout3 library which can stream both MP3 or OGG/Vorbis data to multiple stream servers (including IceCast, ShoutCast etc). In this blog-post I will try to explain how I&#8217;ve created this extension, and off course, how you can use it.<span id="more-1714"></span><strong></strong></p>
<h2><strong>Part 1: download and read up the specs</strong></h2>
<p>There are two ways to create such a system. The first way is to create a 100% pure PHP class and the second one by creating a wrapper-extension for an existing (C) library. The biggest advantage of writing a PHP class is that  you don&#8217;t need to compile a new extension, but a big drawback is that you are implementing things in PHP that are already implemented in the C library. Furthermore, the PHP class will never be as fast as the C library. The drawback of writing an extension, is &#8211; well &#8211; you need to know a bit about C and the PHP toolkit with all the macro&#8217;s and quirks which might not be easy. However, you don&#8217;t need to duplicate functionality and nowadays compiling and installing an extension is pretty much automated.</p>
<p>So an extension it is, but before we can actually write the extension, we need to know how we would call the functionality from a PHP point of view. Do we create procedural function, should we create an OO interface? Or maybe even multiple interfaces? For this we need to know how the library works in C, which takes a bit of reading up. But luckily, there are some good documents for the library we are using: http://www.aelius.com/njh/libshout-doc/libshout.html and the actual libshout <a href="http://www.icecast.org/download.php">download</a> has got some examples on how to use it.</p>
<p>The library itself is easy enough to install from a debian / ubuntu system, so I&#8217;d reckon CentOS would not be any different although I haven&#8217;t checked. A mere &#8220;apt-get install libshout3-dev&#8221; will do the trick in order to install the development files and the library itself. It&#8217;s not enough to get only the library, since we need to have the include files (*.h) files as well.</p>
<h2><strong>Part 2: create skeleton extension</strong></h2>
<p>Now for the fun part. I&#8217;ve decided to create a sort-of OO interface. Actually it&#8217;s has got not much to do with OO, but merely it&#8217;s a class that wraps our functionality. Might as well be a separate namespace, but this way it makes it easier to extend our class if you need to. Now, on to the actual creation of the extension. What I did was create a skeleton extension first. This skeleton holds not much info, but it&#8217;s easy to start from here.</p>
<p>You can find this at my GitHub commit: https://github.com/jaytaph/phpshout/tree/0fc0fe0fe3eb426591987194deff8bdecb717d34</p>
<p>This is pretty much the bare minimum for an (OO) extension. The most important files being config.m4 and shout.c.</p>
<p><strong>config.m4:</strong></p>
<p>If you are not using external libraries, your config.m4 doesn&#8217;t have to be this complex. This configuration &#8220;template&#8221; will be used to generate a configure script which in it&#8217;s turn creates a makefile to compile the actual extension. I&#8217;ve blogged about these configuration tools in another blog post <a href="http://www.adayinthelifeof.nl/2011/04/24/the-gnu-build-tools-part-1/">here</a> and there are still some in draft which I will release (very soon, I promise!). This config.m4 gives your ./configure script an option called &#8220;&#8211;with-shout&#8221;, where you can actually add the path to your shout.h include files. If you don&#8217;t specify it, it will check for /usr and /usr/local, but in case it&#8217;s somewhere else, you can easily specify it. That is basically what lines 5 to 25 do.</p>
<p>If this is found, lines 27 to 40 will check for the actual library called libshout(.so). This library should be in your LD_LIBRARY_PATH, which 9 out of 10 time is, so you don&#8217;t have to worry about it. If the library is found, it will do a sanity check if there is a function called shout_init() is defined. If so, we will assume you have to correct library. If not, it might be another libshout (either version, or maybe a complete other library).</p>
<p>&nbsp;</p>
<p><strong>shout.c</strong>:</p>
<p>Config.m4 should always be named config.m4, but shout.c is just the name of the extension, and might as well be named differently. For instance, when you have a &#8220;rainbow&#8221; extension (whatever that might be), the name of the main source file might be rainbow.c. Off course, your extension might need multiple source files (rainbow-pony.c, rainbow-unicorn.c etc), so you must add them to your config.m4 as well (on line 42).</p>
<p>Even though this file looks very complicated, it really isn&#8217;t.</p>
<p>Line 34 includes the shout library include file. This allows us to use the shout functions. Because we searched for this include file through the config.m4 and the configure script, the makefile automatically knows where this file is located so we don&#8217;t have to worry about it anymore. Line 38-40 defines an internal structure that holds all information for a single PHP shout-object, sometimes called the &#8220;object store&#8221;. So when we have 2 objects in PHP, we have two php_shout_obj structures or &#8220;stores&#8221;. For now, this only include the actual object information (the zend_object value), but later on we can fill it with internal data. You can see this a bit of the private values of our class if that makes sense to you.</p>
<p>Next up are some (static) functions for creating, freeing, and cloning our objects. These are standard boilerplate stuff. For instance the shout_object_free() function does nothing more than destroying our internal object through a zend_object_std_dtor() call, and free()&#8217;ing our store.</p>
<p>The shout_object_new_ex() and shout_object_new() are functions to create a new object store. The shout_object_new is nothing more than a redirect to shout_object_new_ex(). Lines 68 and 69 initializes some memory for our internal php_shout_object structure and clearing this memory. lines 75 and 76 initializes the internal zend_object in our structure with the default properties.</p>
<p>Lines 78 and 79 will set the handlers (functions) for this object, basically which function it will need to call when the object will be freed and the basic handlers (which are saved in shout_object_handlers, we set these values later). After that, we must return a zend_object_value object with this information.</p>
<p>Line 91 to 101 will deal with cloning an object. This function gets the old object store, creates a new object store (line 94) and line 96 copies/duplicate the actual zend_object data that is located in our store. If there is any other internal data in our store, we need to make sure we duplicate or copy this over. But since we don&#8217;t have anything stored yet, we skip this part for now.</p>
<p>Those are the house-keeping functions, but somehow this all must be connected together. This is where the next functions are structure come in. On line 105, we define a structure that holds all the functions as seen from our PHP class. You can see, we don&#8217;t have any and that&#8217;s ok, since this is a template.</p>
<p>Next up on line 112, the PHP_MINIT_FUNCTION. This is a macro (as you will see a lot in the PHP core and extensions), which defines a module initialization function for the &#8220;shout&#8221; extension. This function will copy the standard handlers that php uses into it&#8217;s own shout_object_handlers (line 117). This way, when we want handle something differently in our own shout-classes, we can easily do so. Remember that these handlers are copied over every time we create a new object (line 79).</p>
<p>The next lines &#8211; 122 creates a new class entry with our functions (which we don&#8217;t have yet). Line 123 tells it that we need to use shout_object_new() function in order to create new objects and line 124 sets our clone() handler to be our own customized clone-handler. Line 126 actually registers our class and we get our own shout class entry back which we save for later usage.</p>
<p>Lines 134 to 140 is a function that will define the information that is shown during php&#8217;s phpinfo(). This will print a table that shout support is enabled (obviously, otherwise this whole table would not be present), plus the version of our php extension plus the version of the library we are using (you see here we are using our first call to the actual shout library!).</p>
<p>Now, in order to connect all this together to PHP can actually use it, we have to create a zend_module_entry structure on line 144. It consists of standard module headers (STANDARD_MODULE_HEADER, the name of the extension (shout), the functions used (shout_funcs), our module startup function that will be run ONCE the extension is loaded, the module shutdown function that will be run ONCE the extension is unloaded (in our case, we haven&#8217;t defined any, so this is NULL). The next two NULL values are the functions loaded every time a request is made. Then the function that returns the information for phpinfo(), and last the version of our module, plus some module properties.</p>
<p>&nbsp;</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-24-at-13.31.11.png"><img class="aligncenter size-full wp-image-1720" title="Screen Shot 2012-03-24 at 13.31.11" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-24-at-13.31.11.png" alt="" width="641" height="319" /></a></p>
<p>&nbsp;</p>
<p><strong>TSRMLS_CC?</strong></p>
<p>On occasion, you will see argument lists ended with TSRMLS_CC. If you don&#8217;t know PHP, these doesn&#8217;t make any sense, since they are there for thread-safety. For more information about it, take a look at the excellent post about PHP and thread safety: http://blog.golemon.com/2006/06/what-heck-is-tsrmlscc-anyway.html</p>
<p>&nbsp;</p>
<p><a href="http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-2/">Next, up, we need to implement functionality.</a></p>
<p>&nbsp;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F24%2Fphpshout-a-shoutcast-streamer-in-php-part-1%2F&amp;title=PHPShout%20%3A%20a%20shoutcast%20streamer%20in%20PHP%3A%20%20Part%201" id="wpa2a_14"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/24/phpshout-a-shoutcast-streamer-in-php-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP has moved to git!</title>
		<link>http://www.adayinthelifeof.nl/2012/03/19/php-has-moved-to-git/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/19/php-has-moved-to-git/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 22:28:07 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1697</guid>
		<description><![CDATA[Good news everybody! PHP has (finally) moved their version control from subversion to git and placed their repository on github. Meaning it just got easier to maintain PHP  but also it makes it easier for external contributors (without any write-access) to create patches and for contributors to merge them. Hopefully this will mean the end [...]]]></description>
			<content:encoded><![CDATA[<p>Good news everybody! PHP has (finally) moved their version control from subversion to git and placed their repository on github. Meaning it just got easier to maintain PHP  but also it makes it easier for external contributors (without any write-access) to create patches and for contributors to merge them. Hopefully this will mean the end of waiting weeks or months before somebody gets around looking at your patch.</p>
<p><span id="more-1697"></span></p>
<p>So, if you like to contribute you can use the following workflow for now. <strong>The most up-to-date workflow should be available on the php.net wiki at https://wiki.php.net/vcs/gitworkflow (the workflow for external contributors section). Make sure you follow those directions if they do not match with these&#8230;</strong></p>
<p>&nbsp;</p>
<p><strong>Step 1: fork the php-src repo</strong></p>
<p>Assuming you already have created a github account, you must clone the php source repository to your own github account. This can be done by going to the following url: https://github.com/php/php-src, and click on the &#8220;fork&#8221; button in the top right. This will create a fork of the php source into your own github account.</p>
<p style="text-align: center;"><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-19-at-22.50.29.png"><img class="aligncenter  wp-image-1698" title="Screen Shot 2012-03-19 at 22.50.29" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-19-at-22.50.29.png" alt="" width="577" height="219" /></a></p>
<p><strong>Step 2: clone your own repository.</strong></p>
<p>Once you have created the repository, you can clone it onto your development machine.</p>
<pre class="brush: bash; gutter: false; toolbar: false; auto-links: false; wrap-lines: false">git clone git@github.com:&lt;yourname&gt;/php-src.git php-src</pre>
<p>This will take a while, since the repo is around 100MB in size. Just let git do his work for a while.</p>
<p>&nbsp;</p>
<p><strong>Step 3: add a remote branch.</strong></p>
<p>Next up, you must add a remote branch. Basically, what you are doing is creating a link between your repository on your development computer and the php-src &#8220;master&#8221; repository (not your github repository, but from php itself). This is called the &#8220;upstream&#8221;, and that&#8217;s why we name it upstream. Your own github repository is usually called &#8220;origin&#8221;.</p>
<pre>cd php-src
git remote add upstream https://github.com/php/php-src.git</pre>
<p>When you check the remotes with the command &#8220;git remote -v&#8221;, you can actually see both the origin and upstream remotes are available. In my case:</p>
<pre class="brush: bash; gutter: false; toolbar: false; auto-links: false; wrap-lines: false">jthijssen@debian-jth:~/php$ git remote -v
origin	git@github.com:jaytaph/php-src.git (fetch)
origin	git@github.com:jaytaph/php-src.git (push)
upstream	https://github.com/php/php-src.git (fetch)
upstream	https://github.com/php/php-src.git (push)</pre>
<p>&nbsp;</p>
<p><strong>Step 4: create a new (tracking) branch.</strong></p>
<p>Now we can actually create a new &#8220;branch&#8221; in which we are going to work. Every feature should have its own branch. When you have a issues a bug inside the bug database for PHP, use that number as the branch name:</p>
<pre>git branch --track issue-&lt;issuenr&gt; origin/PHP-5.4</pre>
<p>Now, a few things are important: first of all, PHP is using tracking-branches, which works a little bit different than local branches. You can read more about that here: http://gitready.com/beginner/2009/03/09/remote-tracking-branches.html.</p>
<p>Secondly, the &#8220;issue-&lt;issuenr&gt;&#8221; is the branch-name we are using. It might as well have been named &#8220;cool-new-awesome-feature&#8221;, but try to keep it as readable as possible. You are doing your mergers a big favor here.</p>
<p>Another important thing: we are creating an issue for the PHP-5.4 branch here. If you have a feature that isn&#8217;t ready for 5.4, or something that needs to be inside 5.3 (like a security fix that also need to be inside 5.4 and the &#8220;master&#8221; (or trunk as it used to be called)), always use the lowest version. In that case, it would be PHP-5.3. If you don&#8217;t know, ask, or use the master branch.</p>
<p>&nbsp;</p>
<p><strong>Step 5: switch to the new branch.</strong></p>
<p>Now, we must switch to the new branch, so we can actually work in it:</p>
<pre>git checkout issue-&lt;issuenr&gt;</pre>
<p>When you look with &#8220;git branch&#8221;, you will see your (local) branches, and with &#8220;git branch -v&#8221; you will see some more info:</p>
<pre class="brush: bash">jthijssen@debian-jth:~/php$ git branch -v
* issue-60742 91f2d38 [ahead 1] Issue-60742: Added FilesystemIterator::OTHER_MODE_MASK
  master      dea5376 Merge branch 'PHP-5.4'</pre>
<p>The * in front shows you which is the current branch you are working on. It probably has green color as well.</p>
<p>&nbsp;</p>
<p><strong>Step 6: do your stuff</strong><strong></strong><strong>.</strong></p>
<p>Now it&#8217;s time for the fun part: fixing the code, adding it and committing it.</p>
<p>Every time you want to create a commit, just do a &#8220;git add&#8221; on the files, following by a &#8220;git commit&#8221; and enter a log message. Don&#8217;t worry about over-committing. That actually doesn&#8217;t hurt, under-committing does.</p>
<p>&nbsp;</p>
<p><strong>Step 7: rebase your commits in case new commits have been added (optional)<br />
</strong></p>
<p>It might be possible that others have committed new stuff to the branch you like to push to. This would be visible in the &#8220;upstream&#8221; branch, but not in your &#8220;origin&#8221; branch. There we rebase our commits, basically telling the system to cut away all our commit, update to the latest upstream version and paste our commits to the end. We will do this with the following:</p>
<pre>git pull --rebase upstream PHP-5.4</pre>
<p>Make sure here that PHP-5.4 is actually the branch name you originated your feature branch from (so it could be PHP-5.3 or probably master as well).</p>
<p>Note though, that rebasing is one of the most difficult things to get right and understand when it comes to git. If you don&#8217;t get it working properly, that&#8217;s fine and just skip this step. The php developers can just merge your request with others instead, just like in the &#8220;old&#8221; svn way.</p>
<p>&nbsp;</p>
<p><strong>Step 8: push your commits of the new branch to your github<br />
</strong></p>
<p>Now we issue a:</p>
<pre>git push origin issue-&lt;issuenr&gt;</pre>
<p>This will push our new branch with all the commits to our github repository. When this is done, you can see the new branch on your github account, but you probably have to look for it in the &#8220;branches&#8221;:</p>
<p><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-19-at-23.16.041.png"><img class="aligncenter size-full wp-image-1700" title="Screen Shot 2012-03-19 at 23.16.04" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-19-at-23.16.041.png" alt="" width="616" height="433" /></a></p>
<p>&nbsp;</p>
<p><strong>Step 9: issue a pull request<br />
</strong></p>
<p>Now for the most rewarding part: we can finally issue a pull request to the PHP developers. First, switch to the branch you just have pushed to your repository and click on the &#8220;pull request&#8221; button next to the original &#8220;fork&#8221; button. You will get a screen like this:</p>
<p style="text-align: center;"><a href="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-19-at-23.19.24.png"><img class="aligncenter  wp-image-1701" title="Screen Shot 2012-03-19 at 23.19.24" src="http://www.adayinthelifeof.nl/wp-content/uploads/2012/03/Screen-Shot-2012-03-19-at-23.19.24.png" alt="" width="669" height="166" /></a></p>
<p>The only thing you need to make sure is that the &#8220;base branch&#8221; is set to the correct branch. In this case it says &#8220;php/php-src @ master&#8221;, but it should have been &#8220;php/php-src @ PHP-5.4&#8243;. Somehow this is not set correctly by default. You need to change this by typing in the correct name, and press &#8220;Update Commit Range&#8221; button. If everything goes to plan, you will see the number of commits and the actual files that have been changed.</p>
<p>Once done checking, you can press the big green button that submits the pull request to the PHP and hope for the best!</p>
<p>&nbsp;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F19%2Fphp-has-moved-to-git%2F&amp;title=PHP%20has%20moved%20to%20git%21" id="wpa2a_16"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/19/php-has-moved-to-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ideas of march</title>
		<link>http://www.adayinthelifeof.nl/2012/03/15/ideas-of-march/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/15/ideas-of-march/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 13:59:40 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1684</guid>
		<description><![CDATA[Yes, I&#8217;m a lemming. If a see a group of people jumping off a cliff, I will follow blindly. If somebody calls for more blogposts in the world, I happily write a blogpost for it. But, being the subordinate lemming I like to pretend I am, I don&#8217;t completely see myself in the situation that [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, I&#8217;m a lemming. If a see a group of people jumping off a cliff, I will follow blindly. If somebody calls for more blogposts in the world, I happily write a blogpost for it. But, being the subordinate lemming I like to pretend I am, I don&#8217;t completely see myself in the situation that <a href="http://shiflett.org/blog/2011/mar/ideas-of-march" target="_blank">Chris</a> is in. Basically what he says is that due to the many social media outlets available to us, blogging is taking a backseat when it comes to spreading information. And even though this might be the case for many out there, I don&#8217;t think I&#8217;m falling in this category.<span id="more-1684"></span></p>
<p>When I started blogging I did probably the same as anybody else: setup WordPress, added 2 blog-posts, did some Google Analytics setup and basically waited until anyone else than my mom visited the site. It didn&#8217;t happen..  So, after those two blog post, probably 95% of the users would just quit blogging and continue with their daily lives.</p>
<p>I sorta didn&#8217;t..</p>
<p>What happened, is that I started to blog more and started to realize the reason why I actually blogged: it was not show off to everyone else on how smart I am on certain subjects (which I am :P), but rather to figure out if I&#8217;m actually comprehending the matter where I&#8217;m blogging about. You see, it&#8217;s easy to read up some documentation, check some blogs and articles about a subject, and call yourself an expert. People tend to do that a lot. But what happens is that when you write a blog-post about a subject, you cannot write anything unless you are 100% clear about the subject. I try the same strategy when I&#8217;m teaching stuff: the best way for me to know if you understand the matter, is to let you explain it to me, not the other way around.</p>
<p>If you don&#8217;t know the matter well enough, then there is no possible way you can write a blog-post on it (or do a talk on the subject, or teaching it). So writing blog-posts for me was a &#8220;final test&#8221; to figure out if really know the stuff I think I know. And it turns out that a lot of times I didn&#8217;t.</p>
<p>And this actually has two advantages: first, you don&#8217;t get frustrated by people not viewing your blog, since this is not your goal anymore and B) because your are not blogging for others,it&#8217;s easy enough to write about anything that pops inside your mind. And it turns out that writing blog-posts get easier and easier over time. I have a lot of subjects in draft which I either need to finish, or cannot yet release, but a lot of posts are just about problems I&#8217;m trying to solve or just solved, or things that annoy me where I blog about. You can write a blog-post in like 10 minutes without problem, which isn&#8217;t much more than writing a (way too long) email.</p>
<p>This actually has led that my blog grew with more and more articles about a very broad range of subjects. And just like a snowball effect, a blog-post got picked up by somebody, and more people got onto the site, and even more, and all of sudden you don&#8217;t even have to subject your blog-posts to the &#8220;major&#8221; outlets, because they are automatically interested in the things you are blogging about.</p>
<p>So the best advice I can give to you: don&#8217;t blog for others, blog for yourself. It&#8217;s a good way to archive the things you know and a great way to find things back you forget in a few months. And after a while, people WILL visit your blog. And funny enough, just this morning I was talking on Freenode with somebody about a subject that was one of my first blog-posts I written on this site.</p>
<p>So,.. in the end, my ideas of march pledge will not be about blogging more,  it will be about blogging just in the same speed I am doing now. This month, and the next months as well.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F15%2Fideas-of-march%2F&amp;title=Ideas%20of%20march" id="wpa2a_18"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/15/ideas-of-march/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why putting SSH on another port than 22 is bad idea</title>
		<link>http://www.adayinthelifeof.nl/2012/03/12/why-putting-ssh-on-another-port-than-22-is-bad-idea/</link>
		<comments>http://www.adayinthelifeof.nl/2012/03/12/why-putting-ssh-on-another-port-than-22-is-bad-idea/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 15:12:05 +0000</pubDate>
		<dc:creator>Joshua Thijssen</dc:creator>
				<category><![CDATA[Server & Admin]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.adayinthelifeof.nl/?p=1677</guid>
		<description><![CDATA[I see a lot of companies and users moving their SSH port to a non-privileged port like 2222 or even 36797. Now, there are few reasons why people would do this, all of them incorrect and dangerous. Some of those reasons I will not even mention, just to protect the people who actually use them [...]]]></description>
			<content:encoded><![CDATA[<p>I see a lot of companies and users moving their SSH port to a non-privileged port like 2222 or even 36797. Now, there are few reasons why people would do this, all of them incorrect and dangerous. Some of those reasons I will not even mention, just to protect the people who actually use them :) But what it comes down to, is that people like to move this port away in order to lower the number of attacks on the SSH port.<span id="more-1677"></span></p>
<p>Now, at first glance, this seems a valid reason: if you don&#8217;t know which port to attack, you can&#8217;t attack it at all :-). But if you re-read that last line, you will notice this is nothing more than security through obscurity, and if you think that that is a good idea, you should not be allowed behind a computer.. ever&#8230;</p>
<p>But there are more reasons why this is a bad idea and one of the most important reason has to do with a bit of the (Linux) way of handling TCP/IP ports. When you are logged onto a system as a non-root user (anyone not being uid 0), you cannot create a listing TCP or UDP port below 1024. This is because port numbers below 1024 are so-called privileged ports and can only be opened by root or processes that are running as root. So for instance, when your webserver (apache, nginx etc) will start, it will do so as the privileged root user in order to open up a listening connection to port 80 (the port that by default will be used for HTTP traffic). Now, as soon as the port is opened and everything that needs to be done as root is done, the webserver will fall back to a non-privileged user (either the www-data, apache, or nobody user). From that point, when something bad is happening, it is only limited to the rights that that user has.</p>
<p>Now, back to SSH: when we start SSH on port 22, we know for a fact that this is done by root or a root-process since no other user could possibly open that port. But what happens when we move SSH to port 2222? This port can be opened without a privileged account, which means I can write a simple script that listens to port 2222 and mimics SSH in order to capture your passwords. And this can easily be done with simple tools commonly available on every linux system/server. So running SSH on a non-privileged port makes it potentially LESS secure, not MORE. You have no way of knowing if you are talking to the real SSH server or not. This reason, and this reason alone makes it that you should NEVER EVER use a non-privileged port for running your SSH server.</p>
<p>On to the next reason not to change ports: A lot of applications actually EXPECT ssh traffic on port 22. Now this might be a debate wether or not those programs are developed properly, but it&#8217;s a fact. Even though you can easily change the port in many applications but not all of them do. Trust me, it WILL be annoying for developers, sysadmins and users to operate on your SSH-port 52241, especially since they are using 20 boxes, each with a different SSH port.</p>
<p>Another issue: many corporations have incoming and outgoing firewalls, meaning you cannot goto any site to any random port and expect it to work. Some secure ports, like port 22 are often exempt from that, while other ports like port 25 or 110 are blocked.</p>
<p>Now, let&#8217;s pretend you STILL want to move the port away because you get so many attacks on your SSH port. First of all: are you able to logon as root? If so, fix that now. Secondly: are you using passwords? If so, fix that now and change into public key authentication. But think about it: is it a problem to have so many people banging at the front of your house? They are just there testing your defenses! :-) I&#8217;d rather have an angry mob outside my house knowing it would mean I would do everything to make sure they can&#8217;t break into my house, than to have an open door, that I do not know about, until some lonely passant just drops in to say hi (and trash the place, or worse).</p>
<p>But if you are REALLY worried about that angry mob, there are better ways to hide the door than to move it. Try port-knocking. Again, security through obscurity, but if you must, this is probably the only viable way to do it. There are several ways to implement port-knocking. There are 3rd party tools, which are A) way to big and complex and B) are implemented in userland so don&#8217;t use them. Instead, Iptables has got a very nifty module called &#8220;recent&#8221;, which allows you to create simple &#8211; yet effective &#8211; port knocking sequences.</p>
<p>Take a look at the following example:</p>
<pre>${IPTABLES} -A INPUT -p tcp --dport 3456 -m recent --set --name portknock
${IPTABLES} -A INPUT -p tcp --syn --dport 22 -m recent --rcheck \
   --seconds 60 --name portknock -j ACCEPT
${IPTABLES} -A INPUT -p tcp --syn --dport 22 -j DENY</pre>
<p>What this does, is that as soon as something tries to connect to port 3456 (yes, a non-privileged port, but no problem, nothing is running on it), it will set a flag called &#8220;portknock&#8221;. Now, when we try to setup a connection to the SSH port, it will check to see if your IP has set a &#8220;portknock&#8221; flag during the last 60 seconds. If not, it will not accept the connection. And the third line will by default deny any access to SSH together as a failsafe. (I&#8217;ve heard before that it would be hard for users to remember that they need to connect to another port first, but somebody who cannot click a link, type a url, or telnet to a port, should he or she really have SSH access to begin with???).</p>
<p>Now, don&#8217;t directly copy/paste this into your firewall, but you can see how easy it is to &#8220;hide&#8221; your ports. You can even create a whole serie of knock sequences if you like but this will only annoy your users and gains you nothing.</p>
<p>Again, this does not change ANYTHING on the fact that it should not matter if your port is out in the open or not. Don&#8217;t fall for security through obscurity, because that will probably be the easiest way to get your box hacked. And please, as a hosting company, a system administrator or even a developer, don&#8217;t ever tell others that moving your SSH port away is a good thing. Because it&#8217;s not, and it never will be.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adayinthelifeof.nl%2F2012%2F03%2F12%2Fwhy-putting-ssh-on-another-port-than-22-is-bad-idea%2F&amp;title=Why%20putting%20SSH%20on%20another%20port%20than%2022%20is%20bad%20idea" id="wpa2a_20"><img src="http://www.adayinthelifeof.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adayinthelifeof.nl/2012/03/12/why-putting-ssh-on-another-port-than-22-is-bad-idea/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

