Sunday, August 28, 2016

Writing Equations on the Web


$$\sigma^{2} = \frac{1}{N-1} \sum_{i=0}^{N-1} (x_i - \mu)^{2}$$
An important requirement arose for me recently which didn't occur for many years of my blogging experience. I wanted to write some complex equations on my blog posts here in my blog. The first idea came in to my mind is preparing the equations on Latex somewhere else and then inserting them inside blog posts as images. But I wanted something better than that so I kept searching. Finally I found the right tool to do it.

MathJax is a JavaScript library which we can include in our web pages. Then we can various syntaxes to write equations in our web pages. The most important point is, I can write my most familiar Latex equation syntax inside HTML documents leaving MathJax to handle how to render them on the client web browsers. Here are the simple steps to do this on a blog article hosted in a blogger.com site such as my blog.

(1)
Put the following lines in the top of the HTML content of the web page first inside the <head> tags. In case you are doing it in a blogger.com article, you have to goto the HTML view first and then add these lines on the top.


<script type="text/javascript" async
  src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>

(2)
Now we are ready to write an equation. We can use Latex math syntax for this but depending on the place we put the equations, we have to use two different tags. In order to write some mathematical text in-line, we have to enclose them inside a \( and \) tags. To put mathematical equations separated from the main texts, we have to enclose them inside a $$ and $$ tags.

Here is an example text line where I will put an in-line equation using \( and \) tags as follows. \( y = ax + b \). Did you see the equation? It was originally written as follows within the text line.

\( y = ax + b \)

OK then here's how we put an equation outside the main text using $$ and $$ tags.

$$\sigma^{2} = \frac{1}{N-1} \sum_{i=0}^{N-1} (x_i - \mu)^{2}$$

It was originally written as follows in the HTML source code.


$$\sigma^{2} = \frac{1}{N-1} \sum_{i=0}^{N-1} (x_i - \mu)^{2}$$

Enjoy writing equations on web pages!

References:
[1] https://www.mathjax.org/
[2] http://docs.mathjax.org/en/latest/start.html

Thursday, August 25, 2016

Reflections of a Busy Semester

my desk at SCoRe lab
Life has become never like before. I've came across so many revolutions throughout my life and at the moment I can't believe how amazing was the journey for the last 29 years. As I always enjoy turning back and looking at the trail behind, I decided to spare some time to think and write about the past 6 months of my life. It's full of new experiences, new faces, new knowledge and all new set of dreams. Even though it is impossible to write about everything, at least this would be a collection of most important things I came across.

The things which took most of my processing power were my academic duties at UCSC. Last semester I involved in 5 courses for undergraduate students in UCSC. In the meantime, I taught an undergraduate course in University of Sri Jayawardenapura as a visiting lecturer. Well, some people find it unbelievable how come I get involved in teaching 5 undergraduate courses in a semester. But this is the thing. An important decision was made in this semester by the administration to assign an assistant lecturer for every course to get involved in teaching along with a senior lecturer. As I'm one of such an assistant lecturer, they assigned me to several courses. Me too was so eager to hold a significant responsibility in teaching and this was the best chance I ever received to work.

course introduction slide of SCS2105
Among all those many courses I got involved in, there were two important courses. The first is SCS2105 - Computer Networks - 1. The two lecturers assigned to teach in this course are Mr. Brian Wijesuriya and me and my role was little compared to Mr. Brian. However, since he had many other courses to teach which made him so busy, I taught about 11 lectures from the total of 15 lectures in this courses. This course was the most well structured and planned course I have ever worked on. Before starting the lecture series, Mr. Brian and me had few meeting and we discussed about the syllabus of the course and about the time line of covering each topic. Most importantly, we decided to prepare a series of practicals and tutorials to make the students busy o work every week. Computer networks is a practical course which should be learned by doing. Many undergraduate students prefer a lifestyle of having a nice time throughout the semester without worrying about course works and jump into learn only at the end of the semester a week before the exams. This is not what we encourage and I think we did our best to give a hard time for such people.

course introduction slides of SCS4107
While that course went in a well structured and planned phase, SCS4107 - Operating Systems - 2 was a course which went on a much experimental approach. Since this semester was the first time this course was offered, course contents were not fixed in the beginning and we had to plan on the go. We based this course on the Operating Systems Engineering course in MIT and took many contents from there. Since the senior lecturer worked on this course was Dr. Chamath Keppitiyagama, it was easier for me since it is always enjoyable to work with him. During the first 8 lectures of the course, I conducted sessions about XV6 operating system which is a clone of the famous Unix version 6 operating system. In every lecture, we spent the first half explaining some component of the operating system and then gave a task for the students to spend the next half of the course working on their own. They got marks at the end of the day for the work they do. The next 7 lectures of the course was on some advanced topics of operating systems conducted by Dr. Chamath. I attended to those lectures like a student since all those things were new to me too.

It was not a happy time always. I faced really challenging, stressful and tiresome days during the past semester. Sometimes, due to the large amount of works to switch between, I had a limited time to prepare before conducting a lecture. Because of that, there were moments where I felt that I was not prepared enough in front of the students. Anyway, I think I did my very best. When I received the student feedback forms, it was a little bit disappointing to see how some students responded specially in Computer Networks course for 2nd year CS students. But, there were some good and constructive comments from some students too. I'm thankful to them for helping me to improve as a junior lecturer. I identified many weaknesses of me which I should fix in future semesters.

In addition to teaching duties, I enjoyed my work in SCoRe lab. Our significant work is the contribution to write a new funding proposal for a research project. Dr. Chamath, Prof. Thiemo and Prof. Luca involved in writing this proposal in collaboration and I wrote a significant part of the document. It was a great experience to me to understand how to write grant proposals. Meanwhile, towards the end of this semester, I made a new initiative which seems to be interesting to many other people. That was starting a reading group among the SCoRe lab community. Our objective is to meet every week and do some group work such as reading a book, studying some topic or presenting a latest research paper. I hope this reading group will go a long way from here.


Sunday, August 7, 2016

Preparing Latex Documents in Sinhala (සිංහල) Language

[Note: I wrote a follow up post for this blog post which you can find here.]

Since I'm addicted to Latex for preparing all kinds of documents such as letters, presentation slides and research papers, sometimes I find it hard to work on some document if it is given in a file format for a WYSIWYG editor such as Microsoft word. Finally several months ago, I came across an interest to try and prepare some documents on Sinhala (සිංහල) language using Latex since it is my native language. Actually, I got the interest after working on a Wikipedia page translation in to Sinhala. You can find that wiki page which I translated here. However, I didn't have a chance to spend some time on making Latex documents in Sinhala. Today was the right time for that.

This is how we can prepare a simple Latex document in Sinhala. First of all, we need to have the necessary packages installed. I cannot specifically mention each and every package we need since I used to install the full distribution of texlive package to get everything under a single roof. So, let's go like that.

sudo apt-get install texlive-full

After the installation, open a text editor and enter the following content into it. We can use Google input tools for typing the Sinhala unicode texts and then copy and paste here inside our Latex document.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
\documentclass{article}
\usepackage{fontspec}

%
% This file is a sample tex file to illustrate use of
% XeTeX in typesetting a Sinhala document.
% Asanka Sayakkara, 2016
% Based on the example from Golam Mortuza Hossain, 2008

%
% Define fonts that you want to use
%

\font\serif="FreeSerif:script=sinh"
\font\serifbb="FreeSerif:script=sinh" at 24pt
\font\serifsection="FreeSerif:script=sinh" at 16pt
\font\deffont="FreeSerif:script=sinh" at 14pt
%\font\mukti="Mukti Narrow Bold:script=beng"

%
% "script=sinh" in above lines ensures that appropriate
% complex text rendering engines are used for proper
% rendering of Sinhala.
%

\title{\bf\serifbb මාතෘකාව මෙතන ලියන්න}
\author{\bf\serif අසංක සායක්කාර}
\date{2016-08-06}
\newcommand{\edition}{Second Edition} % Book edition

\begin{document}

\deffont %Default font used for the document
\maketitle

\section{\textbf{\serifsection සිංහල}}

මේ මම සිංහලෙන් සකස් කල ලිපියක්. මෙය සකස් කිරිඉමට මම ලේටෙක් මෘදුකාංගය පාවිච්චි කලා. එය ඉතාම ප්‍රයෝජනවත් මෙවලමක් මට.

\section{English}

Some English here and there.

\end{document}

Let's say you saved this document with the file name sample.tex somewhere in your file system. Now, it's time to compile it and generate our PDF file using it. We should use xelatex tool to do that since latex and pdflatex are not going to work for these kinds of documents with unicode texts.

xelatex sample.tex

Now, there should be a PDF file generated in the same directory and it will have a content like the figure shown in the beginning of this article. Since copying and pasting Latex contents from the above sample content can be hard for people, I added my files to a github repository. You can find it here.

Enjoy preparing documents in Sinhala!

Friday, August 5, 2016

Sniffing GSM Packets using HackRF or RTL-SDR

These are the steps I followed to capture GSM packets using a HackRF SDR device or an RTL-SDR dongle and view it on Wireshark protocol analyzer.

(1) Download the following Ubuntu live iso image which contains GNURadio pre-configured. Then, prepare a live USB stick using this image. I used Ubuntu startup disk creator for this purpose. This is where we start.

s3-dist.gnuradio.org/ubuntu-14.04.3-desktop-amd64-gnuradio-3.7.9.iso

(2) When your live USB stick is ready, boot the laptop with it. Then let's install some packags as follows.

sudo apt-get install git cmake libboost-all-dev libcppunit-dev swig doxygen liblog4cpp5-dev python-scipy

(3) Now we should download the source code, build locally and install one important module called gr-gsm which has all the necessary tools to capture the GSM packets using HackRF and then directing it to Wireshark with a proper format.

cd Downloads
git clone https://github.com/ptrkrysik/gr-gsm.git
cd gr-gsm
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig



(4) Open the file ~/.gnuradio/config.conf and append following two lines to the end of it. In order to edit and save the file, I didn't need root permission.

[grc]
local_blocks_path=/usr/local/share/gnuradio/grc/blocks


(5)  Now, let's install another important package. If you are going to use HackRF for this purpose, you need to install kalibrate-hackrf while if you are going to use RTL-SDR dongle, you need to install kalibrate-rtl.

Installing kalibrate-hackrf:

cd Downloads
git clone https://github.com/scateu/kalibrate-hackrf.git
cd kalibrate-hackrf
./bootstrap
./configure
make
sudo make install


Installing kalibrate-rtl:

sudo apt-get install librtlsdr-dev

git clone https://github.com/steve-m/kalibrate-rtl
cd kalibrate-rtl
./boostrap
./configure

make

(6) Now let's run calibrate tool and scan the frequency range for GSM networks.

For HackRF device:

cd kalibrate-hackrf/src
./kal -s GSM900 -g 40 -l 40


For RTL-SDR device:

cd kalibrate-rtl/src
./kal -s GSM900 -g 40

Now we have to wait a while until this tool discovers and lists down all the frequency channels used by GSM networks in the area. We will refer to the data we found here later.

(7) Install Wireshark if you haven't done yet.

sudo apt-get install wireshark


(8) Go to the place where we downloaded gr-gsm. Then open the given GRC script using Gnuradio Companion tool. Update the gain value to 40. Now run the GRC script. We will get a new window where we can select the GSM channel frequency we need to sniff.

cd Downloads/gr-gsm/apps
sudo gnuradio-companion gr-gsmlivemon.grc



(9) While the above tool is running, open a new terminal and enter the following command to run Wireshark with the appropriate filters.

sudo wireshark -k -Y 'gsmtap && !icmp' -i lo

Now we should see a subset of GSM packets from GSM networks are appearing in Wireshark window.

References:

[1] http://dilsdomain.blogspot.sg/2016/01/install-requirements-for-gsm-band.html
[2]
http://dilsdomain.blogspot.com/2016/01/sniffing-gsm-traffic-with-hackrf.html
[3] https://z4ziggy.wordpress.com/2015/05/17/sniffing-gsm-traffic-with-hackrf/

Sunday, July 31, 2016

Python and SQLite: A beautiful combination

When building small systems which do nice and interesting things, I prefer keeping the implementation as simple as possible. I don't like the philosophy of many people who think too much ahead of time and try to make things work in future requirements too. I like look at problems in a computer scientists perspective than a software engineers perspective. Today I had to build a simple prototype of a system which needs a small back-end database and some simple interactions with it from a front-end. So, without thinking twice, I went for Python as the programming language and SQLite as the database. Simple and easy.

This is a brief note about using Python to interact with SQLite database.

Let's install the sqlite client program first. I'm working on a Ubuntu 16.04 LTS machine.
sudo apt-get install sqlite3 libsqlite3-dev

We can start sqlite3 tool with a name for our database file as follows. The file name is test.db in this case.

sqlite3 test.db

Let's create a simple table and insert some data to it as follows in the prompt.

CREATE TABLE Cars(Id INT, Name TEXT, Price INT);
INSERT INTO Cars VALUES(1,'Audi',52642);
INSERT INTO Cars VALUES(2,'Volvo',12345);


We can query the data we inserted as follows.

SELECT * FROM Cars;

Additionally, we can check the currently created tables and their scheme using following commands.

.tables

.schema

Once we are ready to exit, give the following command on the prompt to exit from the sqlite3 tool.

.exit

Now open a text editor and insert the following python program into it. Save it as sqlite-program.py for the program name.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')

print "Opened database successfully";

cursor = conn.execute("SELECT * FROM Cars")

for row in cursor:
   print "id = ", row[0]
   print "name = ", row[1]
   print "price = ", row[2], "\n"

print "Operation done successfully";

conn.close()

Now we can run this python program to see it taking data from the sqlite database.

python sqlite-program.py

That's all forlks!

References:
[1] http://zetcode.com/db/sqlitepythontutorial/

Thursday, July 7, 2016

IP over TCP

In a previous post, I wrote about how to create virtual network interfaces on Linux by using TUN interface facility. We created a simple use program which opened the file descriptors of the TUN interface from the back-end so that any packet directed to the TUN interface will be read by the back-end program through the file descriptor. Similarly, we can write to the file descriptor from the back-end program so that any application listening to the TUN network interface will receive it.

In this article, I'm demonstrating a simple setup based on TUN network interfaces which we can use to deliver our IP packets from one host to another inside a TCP socket connection. This is a simple but an interesting system to demonstrate that we can capture IP packets and then deliver them through any medium we want including TCP sockets. In this particular example, since we are using TCP sockets, we need two programs; a TCP server and a TCP client to run on the two hosts. As IP packets what we are actually sending are ICMP payloads so we should have named this post as ICMP over TCP to be more precise. Let's start the description.

High-level overview of the setup

1. First of all, obtain the required source files from the following git repository.

git clone https://github.com/asanka-code/tun-tcp-socket.git

2. In the first computer, run following commands to setup a TUN interface called asa0,

sudo ip tuntap add dev asa0 mode tun
sudo ip addr add 10.0.1.1/24 dev asa0
sudo ip link set dev asa0 up
sudo ip addr show

3. In the second computer, run following commands to setup a TUN interface called asa0,

sudo ip tuntap add dev asa0 mode tun
sudo ip addr add 10.0.1.2/24 dev asa0
sudo ip link set dev asa0 up
sudo ip addr show

4. Compile and run TUN controller program which is also a TCP server on host 2,

gcc tun-server.c -o tun-server
./tun-server

5. Compile and run TUN controller program which is also a TCP client on host 1,

gcc tun-client.c -o tun-client
./tun-client

6. Ping from host 1 to host 2 where our ping packets will be delivered through the TCP client and TCP server in the TCP socket connection (connection x),

ping -I 10.0.1.1 10.0.1.2

If everything is properly setup. We should be able to see the ping responses from the remote host.

Wednesday, June 29, 2016

Installing Oracle Express 11g using Docker on Ubuntu

Installation of Oracle database on a Linux system for learning purposes is a huge hassel since there are so many steps to follow to get Oracle database is up and running. Furthermore, since I'm running on Ubuntu Linux which is a Debian based system, things are much more complicated than working on a RPM based system. Due to these reasons, I was strugging to get Oracle databse up and running on my system for a while. Finally I found a nice way to use it. Without going through the overhead of installing it, I found a docker image which I can to directly run on my Ubuntu Linux platform. Following are the steps I followed to get it done on my Ubuntu 16.04 LTS system.

Preparing the system for Docker

These steps are needed if you didn't have docker configured in your system like me initially. I found these steps from here [1].

(1) Run following commands on the terminal.

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D


(2) Open the following file and remove any existing contents from it. In my case, I didn't have such a file anyway.

sudo vim /etc/apt/sources.list.d/docker.list

Add the following line to that file.

deb https://apt.dockerproject.org/repo ubuntu-xenial main

(3) Then run following commands.

sudo apt-get update
sudo apt-get purge lxc-docker
apt-cache policy docker-engine
sudo apt-get update
sudo apt-get install linux-image-extra-$(uname -r)


(4) Now let's install docker and start it. The last command is just to check whether docker works fine.

sudo apt-get update
sudo apt-get install docker-engine
sudo service docker start
sudo docker run hello-world


Setting up Oracle XE 11g in Docker

Now we are ready to get our oracle database running on docker. I found these instructions mainly from this [2] article.

(1) Let's pull the docker image from repository to our machine.

sudo docker pull alexeiled/docker-oracle-xe-11g

(2) We can start it with the following command.

sudo docker run -d -p 49160:22 -p 49161:1521 -p 49162:8080 alexeiled/docker-oracle-xe-11g

(3) We can see the status of the running docker container using the following commands.

# to see the running docker processes
sudo docker ps

# to see the things running inside a particular docker image, we can use the following command. The value we passed to the '-f' parameter is the contained ID which we can findout using the above command.

sudo docker logs -f 6ab14881419c

(4) We can get a bash terminal for our docker running Oracle XE using the following command. There also we have used the container ID as a parameter.

sudo docker exec -it 6ab14881419c /bin/bash

Preparing sqlplus tool for using Oracle XE 11g

Now that we have started running oracle xe as a docker image and got a root shell for this docker container, we can start interacting with the database server using the 'sqlplus' client program. Let's do some preliminary things to enable the sample database schema called HR and a user account to use it called HR account.

(1) Login to the database using 'sqlplus' tool with username 'system' and password 'oracle'.

# take the root shell of docker image
sudo docker exec -it 6ab14881419c /bin/bash

# start sqlplus tool
sqlplus

(2) Run the following query on the SQL prompt.

ALTER USER HR ACCOUNT UNLOCK IDENTIFIED BY password;

(3) We should see the output as 'User altered.' Now, 'exit' from the 'sqlplus' and then login again but this time with the unlocked user account where the username is 'system' and password is 'password'.

(4) Now in the SQL prompt, type the following query and see whether we get a proper output. If so, we have unlocked our HR account and the HR schema is now available to us.

select count(*) from employees;

Now we are good to go with learning Oracle database.

References:

[1] https://docs.docker.com/engine/installation/linux/ubuntulinux/

[2] http://tuhrig.de/3-ways-of-installing-oracle-xe-11g-on-ubuntu/