<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>LibBits</title>
	<atom:link href="http://libbits.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://libbits.wordpress.com</link>
	<description>Little bits of information on everything</description>
	<lastBuildDate>Fri, 03 May 2013 17:49:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='libbits.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>LibBits</title>
		<link>http://libbits.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://libbits.wordpress.com/osd.xml" title="LibBits" />
	<atom:link rel='hub' href='http://libbits.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SocketCAN Support in Python</title>
		<link>http://libbits.wordpress.com/2012/05/22/socketcan-support-in-python/</link>
		<comments>http://libbits.wordpress.com/2012/05/22/socketcan-support-in-python/#comments</comments>
		<pubDate>Tue, 22 May 2012 11:02:05 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=231</guid>
		<description><![CDATA[Since version 3.3 Python provides support for SocketCAN. You can specify AF_CAN protocol family as you do in C. Below you&#8217;ll find slightly modified example provided in the original SocketCAN patch. To get started just copy the code into example.py and start it as follows provided your CAN interface is can0: python example.py can0 Use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=231&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Since version 3.3 Python provides support for SocketCAN. You can specify AF_CAN protocol family as you do in C. Below you&#8217;ll find slightly modified example provided in the original <a href="http://bugs.python.org/issue10141" target="_blank">SocketCAN patch</a>.</p>
<p>To get started just copy the code into example.py and start it as follows provided your CAN interface is can0:</p>
<p><code>python example.py can0</code></p>
<p>Use <code>build_can_frame</code>() and <code>dissect_can_frame</code>() to build/dissect CAN frames. Interface management like bitrate settings or getting statistics will be made as usual via iproute2 utility.</p>
<pre class="brush: python; light: false; title: ; wrap-lines: false; notranslate">
import socket
import struct
import sys

# CAN frame packing/unpacking (see `struct can_frame` in &lt;linux/can.h&gt;)
can_frame_fmt = &quot;=IB3x8s&quot;

def build_can_frame(can_id, data):
        can_dlc = len(data)
        data = data.ljust(8, b'\x00')
        return struct.pack(can_frame_fmt, can_id, can_dlc, data)

def dissect_can_frame(frame):
        can_id, can_dlc, data = struct.unpack(can_frame_fmt, frame)
        return (can_id, can_dlc, data[:can_dlc])

if len(sys.argv) != 2:
        print('Provide CAN device name (can0, slcan0 etc.)')
        sys.exit(0)

# create a raw socket and bind it to the given CAN interface
s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
s.bind((sys.argv[1],))

while True:
        cf, addr = s.recvfrom(16)

        print('Received: can_id=%x, can_dlc=%x, data=%s' % dissect_can_frame(cf))

        try:
                s.send(cf)
        except socket.error:
                print('Error sending CAN frame')

        try:
                s.send(build_can_frame(0x01, b'\x01\x02\x03'))
        except socket.error:
                print('Error sending CAN frame')
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/231/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=231&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2012/05/22/socketcan-support-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
		<item>
		<title>Capturing and Analyzing CAN Frames with Wireshark</title>
		<link>http://libbits.wordpress.com/2012/05/07/capturing-and-analyzing-can-frames-with-wireshark/</link>
		<comments>http://libbits.wordpress.com/2012/05/07/capturing-and-analyzing-can-frames-with-wireshark/#comments</comments>
		<pubDate>Mon, 07 May 2012 09:23:14 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[CAN]]></category>
		<category><![CDATA[CANopen]]></category>
		<category><![CDATA[SocketCAN]]></category>
		<category><![CDATA[Wireshark]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=218</guid>
		<description><![CDATA[Wireshark is a well-known network packet sniffer. Since 2009 it is also capable of capturing CAN frames via SocketCAN interface in Linux. Just configure and activate your CAN interface and it will show up as one of the available sniffing interfaces. The image below shows CAN frames captured via USB-CAN adapter (slcan driver). Following information [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=218&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.wireshark.org/" target="_blank">Wireshark</a> is a well-known network packet sniffer. Since 2009 it is also capable of capturing CAN frames via <a href="http://en.wikipedia.org/wiki/Socketcan" target="_blank">SocketCAN</a> interface in Linux. Just configure and activate your CAN interface and it will show up as one of the available sniffing interfaces. The image below shows CAN frames captured via USB-CAN adapter (slcan driver).</p>
<p><a href="https://libbits.files.wordpress.com/2012/05/wireshark-can.png"><img class="aligncenter size-medium wp-image-219" title="wireshark-can" src="https://libbits.files.wordpress.com/2012/05/wireshark-can.png?w=300&#038;h=246" alt="" width="300" height="246" /></a></p>
<p>Following information will be extracted from CAN frame:</p>
<ul>
<li>Identifier</li>
<li>Extended Flag</li>
<li>Remote Transmission Request Flag</li>
<li>Error Flag</li>
</ul>
<p>As of Wireshark version 1.7.1 <a href="https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6651" target="_blank">CANopen dissector</a> was introduced. See image below.</p>
<p><a href="https://libbits.files.wordpress.com/2012/05/wireshark-canopen.jpg"><img class="aligncenter size-medium wp-image-219" title="wireshark-canopen" src="https://libbits.files.wordpress.com/2012/05/wireshark-canopen.jpg?w=300&#038;h=246" alt="" width="300" height="246" /></a></p>
<p>As CAN has no ports or other remarkable protocol options you&#8217;ll have to manually choose, how CAN frames should be interpreted.</p>
<p><a href="https://libbits.files.wordpress.com/2012/05/wireshark-choose-protocol.jpg"><img class="aligncenter size-medium wp-image-219" title="wireshark-choose-protocol" src="https://libbits.files.wordpress.com/2012/05/wireshark-choose-protocol.jpg?w=300&#038;h=246" alt="" width="300" height="246" /></a></p>
<p>And the last note. Though CAN frames can be captured only in Linux, they still can be analyzed on every system Wireshark is running on.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/218/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=218&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2012/05/07/capturing-and-analyzing-can-frames-with-wireshark/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>

		<media:content url="https://libbits.files.wordpress.com/2012/05/wireshark-can.png?w=300" medium="image">
			<media:title type="html">wireshark-can</media:title>
		</media:content>

		<media:content url="https://libbits.files.wordpress.com/2012/05/wireshark-canopen.jpg?w=300" medium="image">
			<media:title type="html">wireshark-canopen</media:title>
		</media:content>

		<media:content url="https://libbits.files.wordpress.com/2012/05/wireshark-choose-protocol.jpg?w=300" medium="image">
			<media:title type="html">wireshark-choose-protocol</media:title>
		</media:content>
	</item>
		<item>
		<title>Controller Area Network (CAN) support in Android</title>
		<link>http://libbits.wordpress.com/2012/03/27/controller-area-network-can-support-in-android/</link>
		<comments>http://libbits.wordpress.com/2012/03/27/controller-area-network-can-support-in-android/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 08:04:55 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[CAN]]></category>
		<category><![CDATA[can-utils]]></category>
		<category><![CDATA[iproute2]]></category>
		<category><![CDATA[SocketCAN]]></category>
		<category><![CDATA[Wireshark]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=200</guid>
		<description><![CDATA[Since version 2.6.25 Linux kernel has a special networking stack for CAN communications &#8211; SocketCAN. There exist many drivers for various CAN devices in the kernel: PCI/USB cards, SoC integrated CAN interfaces etc. Thus writing a CAN software is not a problem on modern Linux systems, because you have a unified interface to talk to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=200&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Since version 2.6.25 Linux kernel has a special networking stack for <a href="http://en.wikipedia.org/wiki/Controller_Area_Network" target="_blank">CAN</a> communications &#8211; <a href="http://en.wikipedia.org/wiki/Socketcan" target="_blank">SocketCAN</a>. There exist many drivers for various CAN devices in the kernel: PCI/USB cards, SoC integrated CAN interfaces etc. Thus writing a CAN software is not a problem on modern Linux systems, because you have a unified interface to talk to different CAN hardware. Due to SocketCAN support in <a href="http://www.wireshark.org/" target="_blank">Wireshark</a> you can sniff and analyze CAN communication without a need for expensive proprietary software.</p>
<p>But what about Android? With versions 3 Android is able to work better with bigger screens, hence it is suitable to run on industrial Panel PCs  (whether x86 or ARM based). CAN support can be divided into two levels:</p>
<ul>
<li>system layer (SocketCAN interface configuration and administration)</li>
<li>application layer (Java API to send/receive CAN frames and getting network statistics)</li>
</ul>
<p>To the first case belong such tools as ip from <a href="http://en.wikipedia.org/wiki/Iproute2" target="_blank">iproute2</a> and <a href="https://gitorious.org/linux-can/can-utils" target="_blank">can-utils</a>. Unfortunately Android&#8217;s ip won&#8217;t function out-of-the-box due to some compiler optimizations that interfere with dynamic routines usage in ip. For more detail refer to this <a href="https://android-review.googlesource.com/#/c/34240/" target="_blank">patch</a>, that fixes this issue. So with working ip CAN interfaces can be configured (bitrate, sample point etc.) and brought up and down.</p>
<p>can-utils package provides tools to send/receive CAN frames and some other stuff like server for serial link based CAN devices. This package won&#8217;t get compiled in Android because of missing kernel headers (linux/can.h etc.). This <a href="https://android-review.googlesource.com/#/c/34510/" target="_blank">patch</a> adds required header files and also adds AF_CAN protocol family to <a href="http://en.wikipedia.org/wiki/Bionic_%28software%29" target="_blank">Bionic library</a>. can-utils already has Android.mk, so it can be added to external packages without any change.</p>
<p>I could find no production ready solution for the application layer. The most desirable solution would be native support for SocketCAN like in Python 3.3 (see this <a href="http://bugs.python.org/issue10141" target="_blank">bug report</a>). As alternative <a href="http://kayak.2codeornot2code.org/index.html" target="_blank">Kayak project</a> provides <a href="https://github.com/dschanoeh/socketcand" target="_blank">socketcand</a> server and Java classes that communicate via TCP with socketcand, hence OS independent. But both Kayak classes as also socketcand will have to be adopted due to some Android API limitations.</p>
<p>Basic issues seem to be solved and the road to bring full CAN support to Android is open. I&#8217;m looking forward to see new projects appearing in this area. Let me know if I&#8217;ve missed something related.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/200/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=200&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2012/03/27/controller-area-network-can-support-in-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing patch series with git</title>
		<link>http://libbits.wordpress.com/2011/12/27/managing-patch-series-with-git/</link>
		<comments>http://libbits.wordpress.com/2011/12/27/managing-patch-series-with-git/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 14:34:21 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[commit]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[quilt]]></category>
		<category><![CDATA[rebase]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=188</guid>
		<description><![CDATA[Since I&#8217;m involved in Buildroot development, I have to send patch series to BR mailings-list frequently. Quilt was my friend for all this time. But the whole procedure has some disadvantages like using e-mail client and defining subject string etc. Git provides very flexible environment with format-patch and send-email commands. With the first one you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=188&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Since I&#8217;m involved in Buildroot development, I have to send patch series to BR mailings-list frequently. Quilt was my friend for all this time. But the whole procedure has some disadvantages like using e-mail client and defining subject string etc.</p>
<p>Git provides very flexible environment with format-patch and send-email commands. With the first one you can specify which commits should exported as patches.</p>
<p><code>git format-patch origin/master</code></p>
<p>will extract all commits since the last push. If you just want to extract only 2 latest commits of your 5 commits execute:</p>
<p><code>git format-patch -2</code></p>
<p>If you&#8217;re sending reworked patches, it is important to show the version of this patch set. This can be done with &#8211;subject-prefix.</p>
<p><code>git format-patch --subject-prefix="PATCH v2" origin</code></p>
<p>will add [PATCH v2 x/y] before your real subject.</p>
<p>The patches will be sent via git send-email command, where you can specify what patches should be sent to whom.</p>
<p>git send-email &#8211;to=dev@list.org &#8211;cc=maintainer@list.org 0001-foo.patch</p>
<p>With &#8211;compose switch you can add a summary to the whole path set.</p>
<p>The above commands let you format and send the finished patches. But how to rework the patch in the middle or the whole patch set? The answer is git rebase. With <code>git rebase -i origin/master</code> you can interactively mark the patches to change or change the order of patches etc. Please refer to this <a title="section" href="http://book.git-scm.com/4_interactive_rebasing.html" target="_blank">section</a> of &#8220;Git Community Book&#8221; for further details about git rebase.</p>
<p>Happy gitting <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/188/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=188&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2011/12/27/managing-patch-series-with-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing LAN test with Python</title>
		<link>http://libbits.wordpress.com/2011/09/28/writing-lan-test-with-python/</link>
		<comments>http://libbits.wordpress.com/2011/09/28/writing-lan-test-with-python/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 15:13:59 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[dpkt]]></category>
		<category><![CDATA[netifaces]]></category>
		<category><![CDATA[pypcap]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[raw_sock]]></category>
		<category><![CDATA[winpcap]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=177</guid>
		<description><![CDATA[I was told to make a LAN test. As I was playing with Python anyway and wanted one test for both Linux and Windows, I decided to give it a try. The test is working as follows. Two LAN interfaces will be connected to each other with one LAN cable. The script gets two MACs [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=177&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was told to make a LAN test. As I was playing with Python anyway and wanted one test for both Linux and Windows, I decided to give it a try.</p>
<p>The test is working as follows. Two LAN interfaces will be connected to each other with one LAN cable. The script gets two MACs as command line parameter. An Ethernet packet will be created and sent in unicast from one interface to another and vice versa.</p>
<p>The first objective was to determine a network interface corresponding to the given MAC address. This could be solved OS independent with <a title="netifaces" href="http://pypi.python.org/pypi/netifaces" target="_blank">netifaces</a> package.  Some hacks were needed to get this packaged installed, namely it had to be compiled. I solved this by using MinGW. Visual Studio 2008 can be used too. So finding interfaces looks like this:</p>
<pre class="brush: python; light: false; title: ; wrap-lines: false; notranslate">
def get_eth_interface(mac):
    for iface in netifaces.interfaces():
        a = netifaces.ifaddresses(iface)
        iface_addr = a[netifaces.AF_LINK][0]['addr']
        if(mac.lower() == iface_addr):
            return iface

return &quot;none&quot;
</pre>
<p>The second and most painful objective was RAW_SOCK and Ethernet packets. In Linux it is not a problem: one can use built-in socket objects:</p>
<pre class="brush: python; light: false; title: ; wrap-lines: false; notranslate">
s = socket.socket(socket.AF_INET, socket.SOCK_RAW)
s.bind((src_iface,ethernet.ETH_TYPE_ARP))
</pre>
<p>But Windows itself has only very limited raw socket support. After long searching I found some posts suggesting using <a title="pypcap" href="http://code.google.com/p/pypcap/" target="_blank">pypcap</a> (an interface to <a title="WinPcap" href="http://www.winpcap.org" target="_blank">WinPcap</a>). The installation was a little bit tricky because there was no installer for Python 2.7 and later, so I had to check out the latest version from project&#8217;s SVN repo and compile it myself. After that is was rather easy to implement packet sending/receiving.</p>
<p>There is one useful package that simplifies Ethernet packet creation:  <a title="dpkt" href="http://code.google.com/p/dpkt/" target="_blank">dpkt</a>. Creating some simple Ethernet packet looks like this:</p>
<pre class="brush: python; light: false; title: ; wrap-lines: false; notranslate">
def build_packet(src_mac, dest_mac):
    packet = ethernet.Ethernet()
    packet.src = eth_aton(src_mac)
    packet.dst = eth_aton(dest_mac)
    packet.data = (TEST_DATA)
    packet.type = ethernet.ETH_TYPE_ARP

return packet
</pre>
<p>And the last but not least was creating an executable for Windows. It was necessary because of the installation problems described above. I found a very actively developed project <a title="PyInstaller" href="http://www.pyinstaller.org/" target="_blank">PyInstaller</a>. With this you can create one executable file containing all needed libraries. Unfortunately it still cannot handle egg folders properly, so I had to tweak netifaces package to be included into the final executable.</p>
<p>And one more hack concerning creating Windows executable: always use sys.exit() and not simply exit(), because the executable will complain not finding such a function.</p>
<p>Finally I ended up with rather simple and OS independent script for LAN testing and some silver hair <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/177/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=177&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2011/09/28/writing-lan-test-with-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
		<item>
		<title>Check if IP is within range specified in CIDR in Java</title>
		<link>http://libbits.wordpress.com/2011/05/17/check-if-ip-is-within-range-specified-in-cidr-in-java/</link>
		<comments>http://libbits.wordpress.com/2011/05/17/check-if-ip-is-within-range-specified-in-cidr-in-java/#comments</comments>
		<pubDate>Tue, 17 May 2011 23:36:35 +0000</pubDate>
		<dc:creator>deepcani</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=156</guid>
		<description><![CDATA[Recently I had to deal with CIDR notation. I simply needed to check if a particular IP is within the range specified by CIDR. Here is an example, given IP range specified as 157.166.224.26/10, check if 157.166.21.21 is within this range. It seems that for .NET and C++/C folks the CIDR and IP range check [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=156&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Recently I had to deal with <a href="http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing" title="CIDR Notation" target="_blank">CIDR notation</a>. I simply needed to check if a particular IP is within the range specified by CIDR. </p>
<p>Here is an example, given IP range specified as 157.166.224.26/10, check if 157.166.21.21 is within this range. It seems that for .NET and C++/C folks the CIDR and IP range check come with the standard libraries. This is not the case for java, as far as I know. So, here is how I&#8217;ve done it in Java, after looking at java source code for IntetAddress, and a <a href="http://bytes.com/topic/c/answers/765104-determining-whether-given-ip-exist-cidr-ip-range" target="_blank">similar C implementation</a>.</p>
<p>Let&#8217;s see what the range 157.166.224.26/10 really specifies. Here is a <a href="http://www.subnet-calculator.com/cidr.php" title="CIDR calculator" target="_blank">handy calculator</a> which I used when playing with CIDR. So, as we can see,  157.166.224.26/10 really means you have to accept IPs from 157.128.0.0 to 157.191.255.255. The range contains 2^(32-10) IP addresses.</p>
<pre class="brush: java; title: ; notranslate">
// Step 1. Convert IPs into ints (32 bits). 
// E.g. 157.166.224.26 becomes 10011101  10100110  11100000 00011010
int addr = (( 157 &lt;&lt; 24 ) &amp; 0xFF000000) 
           | (( 166 &lt;&lt; 16 ) &amp; 0xFF0000) 
           | (( 224 &lt;&lt; 8 ) &amp; 0xFF00) 
           |  ( 26 &amp; 0xFF);

// Step 2. Get CIDR mask
int mask = (-1) &lt;&lt; (32 - 10);

// Step 3. Find lowest IP address
int lowest = addr &amp; mask;

// Step 4. Find highest IP address
int highest = lowest + (~mask);

</pre>
<p>Now, your IP is in the range if lowest &lt;= IP &amp;&amp; IP &lt;= highest. </p>
<p>Hopefully, no bugs here.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/156/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/156/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=156&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2011/05/17/check-if-ip-is-within-range-specified-in-cidr-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5eb3b29e8ec756f8191829647afb35c0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ilya</media:title>
		</media:content>
	</item>
		<item>
		<title>Get total rsync progress using Python</title>
		<link>http://libbits.wordpress.com/2011/04/09/get-total-rsync-progress-using-python/</link>
		<comments>http://libbits.wordpress.com/2011/04/09/get-total-rsync-progress-using-python/#comments</comments>
		<pubDate>Sat, 09 Apr 2011 22:23:07 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Linux@home]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rsync]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=134</guid>
		<description><![CDATA[Doing large backups with rsync can take really long time. So it is very important to know the exact progress. rsync provides such an option as &#8211;progress to view the progress of each file:   1238099 100%  146.38kB/s    0:00:08  (xfer#5, to-check=169/396) The last number shows the whole number of files to proceed. This number is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=134&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Doing large backups with rsync can take really long time. So it is very important to know the exact progress. rsync provides such an option as &#8211;progress to view the progress of each file:<br />
 <br />
1238099 100%  146.38kB/s    0:00:08  (xfer#5, to-check=169/396)</p>
<p>The last number shows the whole number of files to proceed. This number is true for small amount of files. But if you have to transfer a big amount, rsync will manage them in chunks, so the last number will be increased each time rsync takes another chunk of files.</p>
<p>My solution was just to make a dry-run and extract the real total number of files and then calculate the whole progress using it.</p>
<pre class="brush: python; light: false; title: ; wrap-lines: false; notranslate">
import subprocess
import re
import sys

print('Dry run:')
cmd = 'rsync -az --stats --dry-run ' + sys.argv[1] + ' ' + sys.argv[2]
proc = subprocess.Popen(cmd,
                                   shell=True,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   )
remainder = proc.communicate()[0]
mn = re.findall(r'Number of files: (\d+)', remainder)
total_files = int(mn[0])
print('Number of files: ' + str(total_files))

print('Real rsync:')
cmd = 'rsync -avz  --progress ' + sys.argv[1] + ' ' + sys.argv[2]
proc = subprocess.Popen(cmd,
                                   shell=True,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
)
while True:
             output = proc.stdout.readline()
if 'to-check' in output:
             m = re.findall(r'to-check=(\d+)/(\d+)', output)
             progress = (100 * (int(m[0][1]) - int(m[0][0]))) / total_files
             sys.stdout.write('\rDone: ' + str(progress) + '%')
             sys.stdout.flush()
             if int(m[0][0]) == 0:
                      break

print('\rFinished')
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=134&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2011/04/09/get-total-rsync-progress-using-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
		<item>
		<title>Calling PsExec from Ant Script</title>
		<link>http://libbits.wordpress.com/2010/06/02/calling-psexec-from-ant-script/</link>
		<comments>http://libbits.wordpress.com/2010/06/02/calling-psexec-from-ant-script/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 17:33:03 +0000</pubDate>
		<dc:creator>deepcani</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=98</guid>
		<description><![CDATA[Part of the work I&#8217;m doing has to do with launching Amazon instances on EC2. Once the instance is running and is ready to be used we install various software on it remotely, via ant script. And, like many other folks out there we rely on PsExec, a tool formerly from Sysinternals (and now from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=98&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Part of the work I&#8217;m doing has to do with launching Amazon instances on EC2. Once the instance is running and is ready to be used we install various software on it remotely, via ant script. And, like many other folks out there we rely on <a href="http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx">PsExec</a>, a tool formerly from Sysinternals (and now from Microsoft).</p>
<p>If you google for &#8216;psexec from ant&#8217; you&#8217;ll find quite a few unresolved issues and disappointed forum posts. People complain that psExec hangs, and that they cannot see the output of the tool when it is called from ant&#8230; Below I describe a workaround that worked for me. The workaround wraps PsExec into a batch file, and starts the tool in a separate console window. This way the tool is launched from a console window environment, and not really from ant.</p>
<p>Sample batch file (runPsExec.bat):</p>
<pre class="brush: bash; title: ; notranslate">
start &quot;&quot; /wait %pstools.home%/psexec %*
exit %ERRORLEVEL%
</pre>
<p>And here is how to use it in ant:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;exec executable=&quot;cmd.exe&quot; failonerror=&quot;true&quot; &gt;
  &lt;arg value=&quot;/C&quot;/&gt;
  &lt;arg value=&quot;${basedir}/runPsExec.bat&quot;/&gt;
  &lt;arg line=&quot;\\remoteHost -u xxxx -p xxxx ipconfig /all&quot;/&gt;
&lt;/exec&gt;
</pre>
<p>The second line in runPsExec.bat script is needed, because otherwise the error code is not propagated from the shell to your ant script.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/98/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=98&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2010/06/02/calling-psexec-from-ant-script/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5eb3b29e8ec756f8191829647afb35c0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ilya</media:title>
		</media:content>
	</item>
		<item>
		<title>VIM as IDE</title>
		<link>http://libbits.wordpress.com/2010/05/17/vim-as-ide/</link>
		<comments>http://libbits.wordpress.com/2010/05/17/vim-as-ide/#comments</comments>
		<pubDate>Mon, 17 May 2010 12:22:07 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[vim auto-completion ctags vimdiff]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=87</guid>
		<description><![CDATA[I&#8217;m using VIM for some years. It is a great editor, but till now I only used some small subset of its advanced features like splitting windows, using ctags, vimdiff etc. But after having some problems concerning VIM usage via putty, I decided to learn some more features. Below you&#8217;ll find a list of useful [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=87&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m using VIM for some years. It is a great editor, but till now I only used some small subset of its advanced features like splitting windows, using ctags, vimdiff etc. But after having some problems concerning VIM usage via putty, I decided to learn some more features. Below you&#8217;ll find a list of useful plugins I found during my search:</p>
<p><a href="http://vim.sourceforge.net/scripts/script.php?script_id=273" target="_blank">taglist</a></p>
<p>This is very useful plugin showing you an overview of a current source file, delivering macro, variables and function definitions in a special window.</p>
<p><a href="http://www.vim.org/scripts/script.php?script_id=1754" target="_blank">TTrCodeAssistor</a></p>
<p>This plugin enables auto-completion of function prototypes.</p>
<p><a href="http://www.vim.org/scripts/script.php?script_id=1520" target="_blank">OmniCppComplete</a></p>
<p>This plugin enables auto-completion for structures/classes.</p>
<p><span style="color:#ff0000;">Note</span>: all these plugins rely on ctags, I use following alias:</p>
<p><code>alias ctags='ctags -R --exclude=.pc --exclude=patches --exclude=.git --c++-kinds=+p --fields=+iaS --extra=+q'</code></p>
<p>the &#8211;exclude statements are very useful, if using quilt, otherwise you can spring into the backup version of the file instead of original one. Other statements are needed to correctly extract structure/classes information.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=87&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2010/05/17/vim-as-ide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
		<item>
		<title>Automatically Convert Video Using Inotify</title>
		<link>http://libbits.wordpress.com/2010/04/13/automatically-convert-video-using-inotify/</link>
		<comments>http://libbits.wordpress.com/2010/04/13/automatically-convert-video-using-inotify/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 20:52:23 +0000</pubDate>
		<dc:creator>yegorich</dc:creator>
				<category><![CDATA[Linux@home]]></category>
		<category><![CDATA[inotify]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[xvid]]></category>

		<guid isPermaLink="false">http://libbits.wordpress.com/?p=74</guid>
		<description><![CDATA[I just needed to convert some YouTube videos from flv into Xvid format to watch them on my old media player. At first I converted each video manually using ffmpeg. It was not very comfortable. Using WinFF made the stuff easier nevertheless it still required manual intervention. So after some searching I came across an [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=74&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I just needed to convert some YouTube videos from flv into Xvid format to watch them on my old media player. At first I converted each video manually using ffmpeg. It was not very comfortable. Using WinFF made the stuff easier nevertheless it still required manual intervention. So after some searching I came across an article describing Inotify kernel feature. So here is my script:</p>
<p><code>
<pre class="brush: bash; light: false; title: ; wrap-lines: false; notranslate">

#!/bin/sh

SRCDIR=/home/user/Videos/downloaded
DESTDIR=/home/user/Videos/converted/

inotifywait -mq --format=&quot;%w%f&quot; -e close_write $SRCDIR | while read file; do
       ffmpeg -i &quot;$file&quot; -vcodec libxvid -aspect 4:3 -acodec libmp3lame -ab  192k $DESTDIR&quot;`basename &quot;$file&quot; .flv`&quot;.avi
       rm &quot;$file&quot;
done

</pre>
<p></code></p>
<p>Now each downloaded video will be automatically converted and moved to the right folder. The script can be started on startup to make the process fully automatically.</p>
<p>Dependencies: <a href="https://github.com/rvoicilas/inotify-tools/wiki/" title="inotify-tools" target="_blank">inotify-tools</a> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/libbits.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/libbits.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=libbits.wordpress.com&#038;blog=5885356&#038;post=74&#038;subd=libbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://libbits.wordpress.com/2010/04/13/automatically-convert-video-using-inotify/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d875f6bb3d484f227b68a7b2923de948?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yegorich</media:title>
		</media:content>
	</item>
	</channel>
</rss>
