Tuesday, August 26, 2014

Simple JNI program tutorial in Mac OS X Maverics

Hi, I was digging into JNI for some work, and I saw several examples on the internet. But I had problems successfully running a simple Hello World program in Mac OS X mavericks, with the renovated clang compiler.

This tutorial is based on the following two online tutorials, the first one is the easier one but only for windows based compilers, the second one actually lists all the required linker options for Mac.

http://www.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
https://thenewcircle.com/static/bookshelf/java_fundamentals_tutorial/_java_native_interface_jni.html

As I have come out successful after some digging, here I am presenting the complete code I used. The code listing works with default clang, or gcc compiler installed through mac port.

I intentionally presented screenshot instead of writing the code here, so that viewers write the code themselves, instead of copying!!

First we write a Hello World Program in Java,



Then compile it

Now create the required header file, C/C++

Now, implement HelloJNI.c


Now, this is important, the hello library needs to be with the jnilib extension in Mac, not the linux traditional .so extension. So, it will be libhello.jnilib
The whole command will be,

Now, just run it like,
That's it!

-->

Thursday, August 29, 2013

Cutting a string from shell script

Today i am showing a way to cut specific word from a string in linux shell script,

lets u have  a file such as

Alex Farguson manchester united coach 1987-2013

now we want to get only the year when he started his job, that is 1987, say the file name is coach.txt

we can now simply use,
cat coach.txt | awk '{ print $6 }' | cut -f1 -d"-"
  • cat coach.txt will print the contents of the file to stdout.  
  • awk '{ print $6 }' will print the sixth column, i.e. the one containing 1987-2013
  • cut -f2 -d"-" will cut the output of previous step using - as the delimiter and get the first column (-f1)
-->

Floating point calculation in linux shell script

BC is a useful utility/language to do floating point calculation in linux shell script.

it has so many formats, here are two examples,
bc <<< 20+5/2
bc <<< 'scale=4;20+5/2'
 
here scale=4 means we will consider decimal up to four digits.

another example may be,
 
echo "scale=4;$a/$b" | bc
 
details here.
 
  
-->

Tuesday, August 27, 2013

How to upload files or folders to dropbox from the command line in any linux machine (An engineer's thought, :P)

I was in a complex situation. I needed to transfer a folder of size 5 GB from server A to server C. The problem was that i can only access server A from server B, and server B from server C.

I could transfer the folder from A to B by scp, and do the same thing again for server B and C. But the problem was that I was limited in disk space in server B. So i tried several methods such as pipes, tunneling, port forwarding. But I failed and i did not have enough time to debug. Then suddenly an idea came to my mind, afterall I am an engineer, i should have found out simple work arounds, as Bill gates said, I prefer lazy persons as they will find an way to do things easily, lol.

Here is what I did, I used a bash script that allowed me to upload files from A to drop box directly over command line. Then i just downloaded that folder to my local server C. Here is the link of the dropbox uploader script, https://github.com/andreafabrizi/Dropbox-Uploader. The only limitation is individual files should be under 300 MB. The process is simple and described in the above page.

I thought I should share this with you guys. :)

-->

Wednesday, June 8, 2011

[Micro Blog] How to see which process is using a specific port in Linux

I had a big issue in recent past while dealing with JBoss. I could not run it and it gave the exception port 8083 is already opened. So I needed to know the procedure to identify the process which was holding that port.

After googling a while i found this useful command,

lsof -w -n -i tcp:8083

It gave me the required process id which i denote pid, in my case it was 8380.
After that I used the following command to kill the process actually,


kill 8380


That is how I solved my problem. :)


Sunday, November 21, 2010

SS7 Protocol, Basic Overview

SS7 which stands for Signaling System No. 7 is a protocol for telecommunication systems. This is used as a standard for call initiation and controlling in any network like GSM or PSTN.

SS7 is implemented to establish a voice connections among the calling and called party entities. SS7 is also the medium for advanced capabilities and applications including mobile networking and services as well as wire line applications such as toll-free calling and automatic calling card identification. SS7 is treated as a host for facilitating mobile networking systems, calling card identifications etc.
SS7 is actually can be treated as packet switching. Routes are dynamically assigned on the basis of availability and minimum cost. Unlike TCP/IP SS& networks are private which ensures security and liability.
Drawing1

The SS7 protocol stack  draws similarity with the popular OSI Model . OSI layers 1 to 3 are provided by The Message Transfer Part (MTP) and the Signaling Connection Control Part (SCCP) does the job for OSI layer 1 to 3. Telephone User Part (TUP) or the ISDN User Part (ISUP), provides layer 7. OSI layer 4 to 6 are not implemented so far.[1] The Transaction Capabilities Application Part (TCAP) is the primary SCCP User .It works in the Core Network. TCAP facilitates the Mobile Application Part, the Intelligent Network Application Part and the CAMEL Application Part. The Message Transfer Part (MTP) provides network interface, information transfer, message handling and routing functionalities. Signaling Connection Control Part (SCCP) and MTP Level 3 constitute Network Service Part (NSP). Telephone User Part (TUP) used to connect calls. ISDN User Part (ISUP) actually establishes, maintains, and ends the connections. Transaction Capabilities Application Part (TCAP) makes database queries. and connects to Intelligent Network Application Part (INAP) for intelligent networks, or Mobile Application Part (MAP) for mobile.

SS7 actually two types of signaling: a. connection oriented , b. connectionless oriented signaling. Connection oriented signaling means switch-to-switch that uses inter-office trunks. These trunks are the carrier of voice data. The ISDN User Part (ISUP) part is responsible to establish trunks . Transaction Capability Application Part (TCAP) is responsible for connectionless signaling. This contains the functionality of switch-to-database or database-to-database communications.
I worked on an Interactive Voice Response System which runs on the MAP and ISDN layer of SS7 stack. Here I implemented following call control functions,

 Set-up to initiate a call
 Call Accepted to acknowledge an incoming call and set it to the Alerting state
 Call Connected to answer an incoming call
 Clear to drop a call
 Channel Free to indicate that a call control channel has been idled.

In my Research & Development  team I used the ISDN call Control Protocol based on a telephony card. Our system worked on a PSTN network. The system was developed so that the IVR server works as a switching center to our PSTN controlling center. The server was given a dedicated routing path with separate trunk ID. The system supports multiple channel, and 230 users can use the server simultaneously. I have developed the C program which constantly poll the data line and manages free and busy channels through the implementation of various service handlers which are in fact asynchronous Linux routines written in C.

Sunday, November 7, 2010

Using OpenSSL to generate local private keys for CSR (Certificate Signing Request)


Sometimes for certifying our service with a Certificate Authority we need to generate Certificate Signing Request (CSR) associated with our private keys. This CSR contains our product or service as well as our company information. We call this Distinguished Names. We can take help of commercial PKI(Public Key Infrastructure) products to generate these CSRs. But we do have a nice tool in almost all Linux Systems for this very work. This tool is simple but powerful enough to working as a certificate authority system. This is OpenSSL

In this blog post I am guiding you to necessary 3 commands to generate a CSR based on md5-RSA based key and CSR generation in OpenSSL.
To get a CSR you need to build your private key file first. This is totally yours and should not be compromised with a third party.

To generate this rsa based private key enter the command in the terminal,
openssl genrsa 1024 > your_key.key

Here 1024 is the length of the bits you need to encrypt with. You can use 2048, 3072, 4096 also.

Now you need to regenerate it with the specific private key format. for this enter the command,

openssl rsa -in your_key.key -out your_key.key

Now create the csr based on this key,

openssl req -utf8 -new –key  your_key.key -out your_csr.csr

Now keep your private key in a safe location and send this CSR to a CA for signing with. If you want to work as your own CA, you can generate the certificate by yourself. Though this is not recommended as you are not a trusted root CA and most popular browsers and clients will generate security warnings when users uses this certificates. The self-signing command is,

openssl x509 -in your_csr.csr -out your_crt.crt -req -signkey your_key.key -days 3650

That’s it now you can use this three files your_key.key, your_crt.crt and the root CA certificate (you don’t need if it is self signed) to maintain a certified product line whether it is SSL, TLS, digital sign or HSM (Hardware security modules).

To check the congruity of the signed certificate and your private key, you can use the following command,

a. openssl x509 -noout -text -in your_crt.crt


and take a look on the Modulus and Exponent section. Now use the following command,

b. openssl rsa -noout -text -in your_key.key

and take a look on the modulus and public exponent field.

If those from the certificate (a) and private key (b) matches, you have successfuylly completed the certificate generation process. If they does not match make sure you used the right hashing and encryption algorithm. For example if the private key is generated using md5 hashing with RSA, the CA is required to sign it using the same method.