Active TCP Socket Polling in Ruby

| Comments

For people starting out with TCP socket programming, official docs and most tutorials suggest the following code pattern:

require 'socket'

socket = TCPSocket.new 'localhost', 2000

while line = socket.gets # Read lines from socket
  puts line              # and print them

socket.close             # close socket when done

However, there’s an interesting problem I found using this pattern while working with SMTP protocol.

The above pattern works well when you work with the socket atomically - i.e. one sends a request, get a response back and the socket is closed after. In this case, socket.gets will eventually return nil after the buffer is read out, however, if one needs to read the socket buffer before it is closed, the pattern stops working, since TCP is an “idle” protocol, happy to assume that the connection is active until proven otherwise.

Flashing NetGear WNDR Routers via TFTP

| Comments

OpenWRT is a great linux distribution for embedded devices, it’s highly customiseable and, in my experience, quite stable, even on most trunk builds.

Since all my routers are running it, I do reflash them once in a while and every time it takes me a bit of time to remember how it’s done, so I decided to make a quick post about it.

This post should be applicable for most Netgear routers of WNDR3XXX and WNDR4XXX family and has been successfully tested by me on WNDR3700,WNDR3800 and WNDR4300.

Airport Pinger

| Comments

I have to do WiFi debugging from time to time and I recently found a nifty way to use Mac standard 80211 framework to aid in that.

First, if you don’t have airport cli tool in your path, I suggest adding it, it is a handy thing to have:

alias 'airport'='/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'

Then, just execute this one-liner:

ping google.com | while read pong; do channel=$(airport --getinfo | grep channel | awk '{print "channel:"$2}'); rssi=$(airport --getinfo | grep agrCtlRSSI | awk '{print "RSSI:"$2}'); bssid=$(airport --getinfo | grep "BSSID" | awk '{print "BSSID:"$2}'); echo "$(date): $pong, $bssid, $channel, $rssi"; done

Nloc - Network Location Management for Alfred

| Comments

I use OS X network location functionality a lot, but switching them from the preferences or in CLI can be a bit tedious, so I made an Alfred workflow to make it fast and efficient.

This workflow allows to quickly change between network locations.


nloc network_location_name

Script supports autocompletion of known network locations:

Up-to-date workflow bundles can be downloaded here.

NOTE: Due to changes in security policy on OS X 10.8, system will ask you for your password after executing the workflow.
OS X 10.7 system shouldn’t ask for your password since it doesn’t require the networksetup command to be executed with super user privileges.

Alfred V2 Octopress Plugin Update

| Comments

Alfred has recently updated to version 2.0. Workflow functionality has been nicely updated so I made adjustments to my extension as well. In this version a blog generate option has been added to the workflow to launch static pages generation from Alfred and notify you on completion.

Set the OCTOPATH variable in the scripts to your octopress installation directory and EDITORAPP to your favourite editor.


blog Awesome post
#Create a new blog post, named "Awesome article"

blog generate
#Launch static page generation via Jekyll.

You can get the updated extension here.

UPDATE 2013-04-21
Fixed the issue where rake could fail on non-standard characters.

UPDATE 2013-05-01
Added native Alfred notification support. terminal-notifier gem no longer required. Extended debugging added. Minor fixes.

MacOS X Server Push Notification Issues

| Comments

Today, one of our MacOS X servers running Snow Leopard stopped sending push notifications. The problem was a bit unusual, so I decided to share my notes on the issue.

I noticed that devices stopped receiving push notifications from this particular server and /var/log/system.log was stating connection errors:

3/9/13 5:20:52 PM   push_notify[76] notification server connect failed, will retry in 300 seconds

First, we need to check the physical connection to APNS servers:

telnet gateway.sandbox.push.apple.com 2195
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.

If that is correct, we need to see if the certificates used by the server are correct. Let’s look at what the server is using for push notifications:

$ sudo serveradmin settings notification:sslKeyFile
notification:sslKeyFile = "/etc/certificates/server.example.com.PMESMFPWKKB2K09I6I8BTKJERP5K152ADN239GDX8.concat.pem"
$ sudo serveradmin settings notification:sslCAFile
notification:sslCAFile = "/etc/certificates/server.example.com.PMESMFPWKKB2K09I6I8BTKJERP5K152ADN239GDX8.chain.pem"

Now, let’s look at the server’s certificates:

$ ls -l /etc/certificates/server.example.com.*
-rw-r--r--  1 root  wheel      1245 Mar  9 15:26 /etc/certificates/server.example.com.N3ZKL8CPVO96759Y38K22D682J3PNB3HA4DL9800J.cert.pem
-rw-r--r--  1 root  wheel      1245 Mar  9 15:26 /etc/certificates/server.example.com.N3ZKL8CPVO96759Y38K22D682J3PNB3HA4DL9800J.chain.pem
-rw-r-----  1 root  certusers  2988 Mar  9 15:26 /etc/certificates/server.example.com.N3ZKL8CPVO96759Y38K22D682J3PNB3HA4DL9800J.concat.pem
-rw-r-----  1 root  certusers  1743 Mar  9 15:26 /etc/certificates/server.example.com.N3ZKL8CPVO96759Y38K22D682J3PNB3HA4DL9800J.key.pem

As you can clearly see from the output - there is no such certificate in the storage. All we have to do now is change the certificate to the correct one:

$ sudo serveradmin settings notification:sslKeyFile = "/etc/certificates/server.example.com.N3ZKL8CPVO96759Y38K22D682J3PNB3HA4DL9800J.concat.pem"
$ sudo serveradmin settings notification:sslCAFile = "/etc/certificates/server.example.com.N3ZKL8CPVO96759Y38K22D682J3PNB3HA4DL9800J.chain.pem"

Then reboot, et voila! Push notification service works again!

P.S. If you have multiple certificates and don’t know which to select for APN service, you can find out the info about them using openssl:

find /etc/certificates -type f -name '*.cert.pem' | while read certificate; do openssl x509 -in $certificate -noout -text; done

Octopress Publishing With Alfred

| Comments

Creating new posts in octopress can be somewhat tedious, so I decided to automate it via Alfred.

This Alfred extension automatically creates a post and opens the markdown file for it in your editor of choice (it uses Mou by default).

Just open Alfred, add a new shell script extension and paste the following script in, setting the octopath variable to your octopress installation directory and ‘editorapp’ to your favourite editor.

Note: Make sure you uncheck the box next to Spaces under the Advanced tab in Alfred. Otherwise, spaces will be escaped and filenames will contain -slash- blocks.

Post to octopress via Alfredlink
#Path for your octopress installation
#Editor to open the post in. (Exact name of the .app bundle in your Applications folder)

if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then

      source "$HOME/.rvm/scripts/rvm"

cd $octopath
rvm use 1.9.3@octopress
octopost=$(rake new_post["{query}"]| grep -o 'source/_posts/.*')
open -a "$editorapp" $octopath/$octopost

You can download the extension bundle here.

New SAMBA Daemon in MacOS X Lion

| Comments

Not that long ago I ran into a strange issue with one old MacOS X application. The issue is - it couldn’t get the NETBIOS hostname of the machine right. I checked the settings - they were all correct. Then, I looked in /var/db/smb.conf and noticed, that the hostname there was outdated and didn’t respond to settings.

Looking into Apple’s documentation on MacOS X Lion I found a note:

“smbd version has been updated to 3.0.28a-apple”

When I looked into manpages I found that the config files have been changed as well:

          The primary configuration for the SMB stack. This file is
          updated by various system services and should not be edited by
          The smbd service's property list file for launchd(8).

The issue was that Apple rolled out a proprietary replacement for the old SAMBA daemon, which is now using PLIST configuration files. The issue is, that some old applications are still looking in /var/db/smb.conf to get the NETBIOS name of the machine.

Here’s a step-by-step workaround to solve the issue:

1) Enter terminal as a super-user:

sudo -s

2) Create a backup of the legacy smb.conf:

mv /var/db/smb.conf /var/db/smb.conf.bak

3) Add a stub smb.conf file to be parsed by the app, getting the NetBIOS name from PLIST file:

netbiosname=$(/usr/libexec/PlistBuddy -c "Print NetBIOSName" /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist) && printf "[global]\nnetbios name = $netbiosname" > smb.conf

4) Give it the necessary permissions: chmod 644 /var/db/smb.conf

5) Reboot the machine.

NetBIOS name of the machine should be picked up by the application shortly after.

Welcome to Temikus' Blog 2.0!

| Comments

Welcome, dear reader.

I decided to move from my existing hosted solution on Squarespace to a static blog, generated using Octopress. This will hopefully allow me to write more often, as it’s far easier to import my notes in this environment.

I hope you will find my notes interesting and/or helpful.

Kind regards, Artem Yakimenko