Archive for August, 2012

The Magic of Binary

In my endeavors as a software developer I gain deeper understanding of Computer Science each year. One thing that has always intrigued me has been binary numbering. As far as I know, everything wonderful and awesome we can do on our computers eventually boils down to a machine interpreting ones and zeroes. For example, the very keyboard on which I type has circuitry that associates each key or combination of keys with a certain numerical code. The computer receives this number and, because of the context in which it was received, knows to interpret this number as a certain letter. The letter ‘A’, for example, has decimal value of 65. It’s hexadecimal representation would be 0×41 and it’s binary representation 01000001. Not to deviate too much from my theme, but even the numbering for the ASCII codes has been extremely well thought out. For upper-case letters, there is always a 010 prefix and for lower-case the prefix is always 011. After the prefix, we are left with 5 bits to represent the letter. We can calculate the number of possible arrangements for a number of bits using 2^n where n is the number of bits. This leaves 32 possible arrangements for 5 bits, which is more than the number of letters in our alphabet. To translate from an ASCII letter to its numerical counterpart and vice versa then becomes easy. Let’s take the letter ‘P’ for example. ‘P’ is the 16th letter of our alphabet. It is a capital letter so it’s prefix would be 010. The number sixteen in binary is 10000. We add that to our prefix and get 01010000, which has a decimal value of 80. Therefore, the key code for the letter ‘P’ is 80. I digress. This is just one of the many amazing ways binary is used in Computer Science. As I said, it’s amazing to me that everything eventually boils down to these ones and zeroes and even electrical high and low signals. Another example would be the screen drawn in front of you. Each pixel (picture element) on your screen has a value associated with it stored in video memory that the video card interprets as a color. The video card knows what signals to send to the monitor to draw this color. The resolution on my screen right now is 1440×900. This means that the screen draws 1440 pixels wide and 900 pixels high. That’s 1,296,000 values floating around in my video memory. Of course, you may have double or triple buffering and this number would be 2 or 3 times as high and as I’m driving three monitors with my new Radeon HD 7770 I’d have to add that multiplication. Again, I digress. The real magic of binary for me is the simple fact that everything we do on our computers is just an interpretation of these high/low, 1/0 values. It all comes down to context and interpretation. I might have a hex value of c0a80032 which in decimal is 3232235570. You might never know what this number means unless you know the context from which it came. If I told you however, that this number was my local IP address right now, you might sooner work it out by breaking it down in octets. 0xc0 = 192, 0xa8 = 168, 0×00 = 0, 0×32 = 50. 192.168.0.50. Also, anyone who’s done a bit of network coding might pick out 0xc0a8 as 192.168. :P Binary is magical to me because it represents possibilities. It is the basis on which our layers of abstraction in the computing world are built and there are so many layers of abstraction that after years of studying I feel I’ve only scratched the surface!

46:6f:72:20:42:72:69:65

Convergence: Ubiety plug-in and libwebsock

Recently, I put out the beginning of another chat plug-in for WordPress called ‘Ubiety‘ which favors WebSockets over the polling system of my AjaxChat plug-in. I don’t like polling over and over again for data. It’s ugly and inefficient. I’d much rather use WebSockets for at-will communication between the server and client. I implemented the simple chat plug-in using the Pusher service. While I prefer the method of communications, this method forces the user to sign-up for the Pusher service. I don’t like this either! I’d like for it be super simple to get going, efficient, and to scale well. Once I get these bases covered, I’ll start looking at the subtler aspects of the chat system and focus on features. In order to eschew Pusher, I began development on libwebsock. Can you tell I love to code? :) Libwebsock is a C library that handles framing and control messages for the WebSocket protocol providing simple callbacks to the C developer for easy production of WebSockets servers. Libwebsock is pretty much fully functional at the moment. It’s still very early on and needs a lot of work. Some aspects of libwebsock that still need work are:

 
  • Initial Handshake
  • Secure WebSockets support via OpenSSL
  • Simpler control frame callback
  • More error checking!
 

I’ve also started development on an Ubiety plug-in server using libwebsock. I’m going to host a server on my personal VPS to try and use up some of that bandwidth I’m wasting. :) I think the cool thing about having this server running somewhere outside of the WordPress host is going to be the ability to chat with people not only on the blog you’re currently viewing but also anyone else who happens to be connected to the Ubiety server. Come to think of it, with this 3rd party nature of the server, this chat will not be limited to WordPress users only. I very well could package this up in a neat JavaScript that could be included on any site and provide presence and chat to ANY website. Wow, the horizons continue to expand on this project. :P Of course, there will be a need to have fine grain control over you’re particular installation including permissions and privacy. This has really given me a lot to think about and I’m excited about the future of this project. Hopefully I can come up with something worthwhile that people will use. Wish me luck!

Rogue DHCP Server Detector

Several times in my experience, I’ve ran into some rather troublesome network issues caused by multiple devices serving DHCP leases on the same physical network segment. For example, on the medium-sized network of which I am an administrator, we have a Linux box doing Network Address Translation and routing for the network; it serves DHCP as well. It also acts as a firewall and caching HTTP proxy. We run into issues when occasionally someone decides to plug in their own wireless router on our network. I won’t get into the specifics of how they manage to do this, but it happens. The end result is we have two devices giving out DHCP leases. The client computers end up getting confused after receiving multiple DHCPOFFERS and we usually have many people complaining that they can’t access the internet. Anyways, after this last time, I decided to write a quick and dirty C program to craft a DHCPDISCOVER packet, pop it out on the network, and listen for any DHCPOFFERS related to that transaction. As I said, the program is quick and dirty, so forgive any obvious blunders. :P You can find the source here. Compile it with gcc: `gcc -o dhcp-check dhcp-check.c`. Then run it as root. You have to be root to play with raw sockets and privileged ports. You can inspect the source if you suspect anything nefarious :P . Also, below is a screenshot.

 
Ubiety v0.0.1
Chat ()