Thursday, 2 February 2017

Classless Inter Domain Routing Made Easy

Introduction :
One day I was working with VPC (Virtual Private Cloud) inside AWS(Amazon Web Services), where I had a need to calculate the CIDR notation of an IP address and subnet combinations.
I had to use online tools to calculate the Subnets and CIDR every time when I was working with VPC, but I found it interesting that how the network get  broken into different small Networks. So, finally I decided why not to learn CIDR Methods, and then calculate it by my own side instead of using tools every time.
说了立法?.jpg
But the questions that striked in my mind were:
  • What is CIDR ?  
  • How CIDR Came into Picture ?  
  • What CIDR do ?

For Understanding CIDR - (Classless Inter-Domain Routing) few thing need to be cleared before :

1. IP Addresses
2. Structure of IP Address
3. Internet Protocol Address Types
4. Classes
5. Network Mask
6. Subnetting

IP Address -

It is the Address of the Computer, Laptop, Printers or even of the Mobile Sets.
Everyone has some Address, so as these devices also have an Internet Protocol Address (IP Address), also called as Logical Address.
In a Network there are many Computers …
Network..??
A Network is a group of two or more Computers Linked Together.

So When there are Many Computers in a Network, We need to uniquely identify each Computer, so there IP ADDRESS works as an Unique Identifier for Computers and Other Devices.

For Example : There are Twin Sisters, How we are going to Identify them differently  
By their Name that are unique for each of them.
Here Name of the Girls are the IP Addresses that will be unique and the two Girls are the two Devices.

Structure of IP Address -  

Now the Question is How do an IP Address looks like??

IP ADDRESS : 192.168.33.10

IP ADDRESS is made up of 32-Bit - 8.8.8.8 = (8+8+8+8=32 Bits)
A bit (short for binary digit) is the smallest unit of data in a computer.

Binary Conversion for 192 :
192 :    128      64       32      16           8      4          2         1
              1        1         0         0           0       0          0         0   
          Bit 1    Bit 2    Bit 3   Bit 4     Bit 5    Bit 6   Bit 7     Bit 8    - Total Bit = 8

128+64 = 192
So, 0’s for Other and 1 for the Number whose sum will be 192

Binary Conversion for 168 :
168 :    128      64       32         16       8           4         2          1
             1          0         1           0        1           0         0          0
           Bit 1    Bit 2    Bit 3   Bit 4     Bit 5    Bit 6   Bit 7     Bit 8 - Total Bit = 8

Binary Conversion for 33 :
  33 :   128        64       32        16       8           4         2          1
             0          0         1           0        0           0         0          1
           Bit 1    Bit 2    Bit 3   Bit 4     Bit 5    Bit 6   Bit 7     Bit 8 - Total Bit = 8

Binary Conversion for 10 :
10 :     128       64       32         16       8          4          2          1
             0          0         0           0        1           0         1          0
           Bit 1    Bit 2    Bit 3   Bit 4     Bit 5    Bit 6   Bit 7     Bit 8 - Total Bit = 8


8.8.8.8 - total of 32 Bit.
Dotted Decimal Notation : In dot form 4 Sections are called as OCTETS - Vendor Neutral Term for Bytes.

Let a IP Be : 200.10.20.30
Inside a Network : 200.10.20 - will remain same and 30 will be unique for each.

Type of IP Address -  

  1. Assignment Method
  2. Classes : 1) Classful
                    2) Classless
  3. Public / Private
  4. Version

Assignment Methods :

Assignment Method is method that defines how to assign an IP address to a Device.

IP Address can be assigned in two ways
1) Static IP Address
Static IP Address is the IP Address in which configuration is done Manually and is used in small networks.

2) Dynamic IP Address
Dynamic IP Address is the IP Address in which the configuration is done by the Computer Interface or by the Host Interface - DHCP (Dynamic Host Configuration Protocol)
-- Configuration is Automatic--

Classes :

classes define that in an IP, How much part will be for Network and How much is for Host.

There are 2 types of classes in IP Addressing :
  1. Classful
  2. Classless

CLASSFUL : IP Address are divided into 5 Classes;

Class A : 0 - 126                         N.H.H.H              Assigned for Large Organization
127                                               N.H.H.H             Assigned for the Loopback
Class B : 128 - 191                     N.N.H.H              Assigned for Medium Companies
Class C : 192 - 223                     N.N.N.H              Assigned for Small Organizations
Class D : 224 - 239                                                 Assigned for Multicasting
Class E : 240 - 255                                                 Assigned for Experimental Purpose

CLASSLESS : Classless addressing is an  IP address where a subnet mask does not define its class.  Subnet mask can be anywhere between bit 0 and bit 31.

CLASS A IP ADDRESS :

Range of Class A IP Address :  0.0.0.0 - 127.255.255.255
Network ID : 8 Bit
Host ID : 24 Bit (8+8+8)

  • IP Address begins with 0,First Bit will always be Zero
  • 7 Remaining Bits in Network part : Only 128 Possible class A Network
  • 24 Bits in Local Part : Over 16 million hosts per Class A Network
  • All class A network parts are assigned or reserved.

<------ 8 Bit --------> <------------------------------ 24 Bit --------------------------------------->
Network ID(N)
Host ID(H)
Host ID(H)
Host ID(H)
0                     7 8                                                                31   
0NNNNNNN       .      HHHHHHHH     .      HHHHHHHH   .         HHHHHHHH

In Binary :

Class A starts from : 00000000.00000000.00000000.00000000
Class A ends at      : 01111111.11111111.11111111.11111111

In Decimal :

Class A IP Address is from 0.0.0.0 to 127.255.255.255

Number of Networks : 2^7 = 128
Number of Hosts : 2^24

SOME EXCEPTIONS IN CLASS A : Cannot be assigned to host

0.0.0.0 : For Self check - Represent Default Network or M
0.255.255.255 : For Self check - Represent Default Network or My IP
127.0.0.0 : Loop Back Address Range : solve NIC Problem
127.255.255.255 : Loop Back Address Range : solve NIC Problem



CLASS B IP ADDRESS :

Range of Class B IP Address : 128.0.0.0 - 191.255.255.255
Network ID : 16 Bit(8+8)
Host ID : 16 Bit (8+8)

  • First two Bit will always be One and Zero
  • 14 Bits in Network part - Over 16,000 possible Class B Network
  • 16 Bits in Local Part  - Over 65,000 possible Hosts


<---------------- 16 Bit-----------------------><------------------ 24 Bit ------------------------>
Network ID(N)
Network ID(N)
Host ID(H)
Host ID(H)
0                                        15 | 16                                                    31   
10NNNNNN          .     NNNNNNNN     . HHHHHHHH       . HHHHHHHH

In Binary :
Class B starts fr0m : 10000000.00000000.00000000.00000000
Class B ends at        : 10111111.11111111.11111111.11111111

In Decimal :
Class B IP Address is from  128.0.0.0 to 191.255.255.255

Number of Networks : 2^14
Number of Hosts : 2^16

SOME EXCEPTIONS IN CLASS B : Cannot be assigned to host

169.254.X.X : Reserved for APIPA (Automatic Private IP Address) - Host take IP Automatically ifit doesn’t get any DHCP Server in the Network.



CLASS C IP ADDRESS :

Range of Class B IP Address : 192.0.0.0 - 223.255.255.255
Network ID : 24 Bit(8+8+8)
Host ID : 8 Bit (8)

**Most Popular and Commonly Used**

  • First three Bit will always be One,One and Zero
  • 21 Bits in Network part - Over 2 Million  possible Class C Network
  • 8 Bits in Local Part  - Only  256 possible Hosts per class C Network


<---------------- 24 Bit------------------------------------------------><----------- 8 Bit ---------->
Network ID(N)
Network ID(N)
Network ID(N)
Host ID(H)
0                                                                        23 | 24                             31    
110NNNNN            .    NNNNNNNN   .      NNNNNNNN     .      HHHHHHHH

In Binary :

Class C starts from : 1100000.00000000.00000000.00000000
Class C ends at        : 11011111.11111111.11111111.11111111

In Decimal :

Class C IP Address is from  192.0.0.0 to 223.255.255.255

Number of Networks : 2^21
Number of Hosts : 2^8



 
CLASS D IP ADDRESS :

Range : 224.0.0.0 - 239.255.255.255
IP Address begins with 1110

Used for Multicasting, Not defining networks.
  • Sending messages to group of hosts
  • just to one (Unicasting)
  • ALL HOSTS (Broadcasting)
  • Say to send a videoconference stream to a group of receivers

In Binary :

Class D starts from : 11100000.00000000.00000000.00000000
Class D end at        : 11101111.11111111.11111111.11111111

In Decimal :

Class D IP Address is from  224.0.0.0 to 239.255.255.255

224.0.0.5 - OSPF
All OSPF Routers address is used to send HELLO PACKETS

224.0.0.6 - OSPF
All the routers address is used to send OSPF routing information to designated routers on a network segment.

224.0.0.9 - The Routing Information Protocol (RIP) version 2 group address is used to send routing information to all RIP2-aware routers on a network segment.

224.0.0.10 - EIGRP
used to send routing information to all EIGRP routers on a network segment.

224.0.0.18 - Virtual Router Redundancy Protocol.


Private/Public:


PUBLIC :
A public also called as  External IP address is the one that your ISP (Internet Service Provider) provides to identify your home network to the outside world. It is an IP address that is unique throughout the entire Internet.
When you're setting up your router, if your ISP issued you a static IP address, you enter it into your router's settings. For a dynamic IP address, you specify DHCP in your router's network settings. DHCP is Dynamic Host Control Protocol. It tells your router to accept whatever public IP address your ISP issues.
Those who wanted not to connect through internet but they wanted to run their network on TCP/IP Protocol
Here came the concept of PRIVATE  IP

PRIVATE :
Just as your network's public IP address is issued by your ISP, your router issues private (or internal) IP addresses to each network device inside your network. This provides unique identification for devices that are within your home network, such as your computer, your Slingbox, and so on.
THEY ARE NOT ROUTABLE

CLASS A PRIVATE ADDRESS   10.0.0.0 - 10.255.255.255
CLASS B PRIVATE ADDRESS   172.16.0.0 - 172.31.255.255
CLASS C PRIVATE ADDRESS   192.168.0.0 - 192.168.255.255

Internet Protocol Address :
           Reserved IP Address :

  1. Addresses beginning with 127 are reserved for loopback and internal testing - Used for Self Testing that TCP/IP is properly working or not.
  2. XXX.0.0.0 reserved for Network Address   
  3. XXX.255.255.255 reserved for Broadcast
  4. 0.0.0.0 - First Address - Represent Local Network / Used for Default Routing
  5. 255.255.255.255 - Broadcast

Example : Let a Class A IP Address be - 101.101.101.101
               Network Address - 101.0.0.0
               BroadCast Address - 101.255.255.255


 : Let a Class B IP Address be - 150.150.150.150
               Network Address - 150.150.0.0
               BroadCast Address - 150.150.255.255

I hope that gives you a good knowledge of IP Addresses and their classes.
Now, We can move on to what sub-netting is, in my next blog.
Please Follow this link to get on to sub-netting -
Classless Inter Domain Routing Made Easy (Cont..)

Gitolite

Requirement

We need private git repositories for internally use in our project so we use Gitolite for this requirement. Our client has a lot of consultants, partners and short term employees working with their code so they needed a good way of controlling access to the repos and preferably without giving each of them a unix user on the server where the repo is hosted.

What is Gitolite?

Gitolite is basically an access layer on top of Git. Users are granted access to repos via a simple config file and we as an admin only needs the users public SSH key and a username from the user. Gitolite uses this to grant or deny access to our Git repositories. And it does this via a git repository named gitolite-admin.


Installation


We need a public key and a Gitolite user through which we will setup the Gitolite.

In this case I have used my base machine(Ubuntu) public key so that only my machine can manage Gitolite.

Now we will copy this public key to a virtual machine

 
$ scp ~/.ssh/gitolite.pub git@192.168.0.20:/home/git


where vagrant is the user of my virtual machine & its IP is 192.168.0.20

Now we will install & create a gitolite user on remote machine which will be hosting gitolite.

root@git:~# apt-get install gitolite3

root@git:~# adduser gitolite
Now we need to remove password of gitolite user from below command

root@git:~# passwd -d gitolite

Let's move & change the ownership of this public key.

root@git:~# mv gitolite.pub /home/gitolite/
root@git:~# chown gitolite:gitolite /home/gitolite/gitolite.pub

Become the gitolite user

root@git:~# su - gitolite

Now setup the gitolite with the public key

gitolite@git:~# gitolite setup -pk gitolite.pub

Now to manage the repositories, users and access-rights we will download the gitolite-admin(git repository) to our base machine.

$ git clone gitolite@192.168.0.20:gitolite-admin
$ cd gitolite-admin
$ ls -l
total
8
drwxr-xr-x
2 nitin nitin 4096 Jan 10 17:52 conf/
drwxr-xr-x
2 nitin nitin 4096 Jan  9 13:43 keydir/

where “keydir” is the directory where we store our user’s keys and that key name must be same as existing username on the system.
In conf directory there is a “gitolite.conf” file which controls which repositories are available on the system and who has which rights to those repositories.
We just need to add new repository name & users who will access it and this file will create the repo & grant the permission on it accordingly.

Let us explore my gitolite.conf file in which I have added a new repository called “opstreeblog

$ cat conf/gitolite.conf

# Group name & members

@admin = nitin
@staff    = jatin james

# Gitolite admin repository

repo gitolite-admin
   RW+   = gitolite @admin

# Read-Write permission to all the users on testing repo

repo testing
   RW+    = @all

# Read-Write permission to user sandy & the admin group. And Read-Only access to staff group

repo opstreeblog
   RW+   = sandy @admin
   R         = @staff





where '@' denotes the user group i.e @staff is a group & jatin, james are the users of this group and these names must be similar to the key name stored in keydir directory.
For example "jatin" user must have the public key named "jatin.pub"

Let's have a quick test of our setup

$ git commit conf/gitolite.conf -m “added opstreeblog repo”

[master 357bbc8] added "opstreeblog" repo

1 files changed, 9 insertions(+), 1 deletions(-)

nitin@Latitude-3460:~/gitolite-admin$ git push origin master

Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 428 bytes, done.
Total
4 (delta 0), reused 0 (delta 0)
remote: Initialized empty Git repository in /home/gitolite/repositories/opstreeblog.git/
To gitbox:gitolite-admin d595439..357bbc8
master -> master

I hope that gives you a good overview of how to install and manage Gitolite.

Monday, 8 August 2016

Stunnel a Proxy to ship the log on SSL

Introduction 

Few days ago I got a task to create the SSL connection with logstash redis plug-in with Azure Redis. As we are shipping the logs form the several data center to the Azure Redis. So logs must be shipped on SSL connection. There is no provision to create SSL connection through logstash redis plug-in thats why logstash redis plug-in is not able to make SSL connection with Azure redis.

To resolve this problem we have to setup the stunnel as proxy front of the logstash redis plug-in. Stunnel can create SSL connection with Azure redis and Stunnel provide non-ssl connection for the logstash redis plug-in.
Azure redis provide two type of connections, SSL on 6380 port and non SSL on 6379 port and also provide primary and secondary key(password).

Installation

Install Stunnel on the ubuntu. 
$ sudo apt-get install stunnel  

Configuration

Create a configuration for stunnel /etc/stunnel/stunnel.conf and put the following lines into the configure file.
setuid = root
setgid = root
pid = /var/run/stunnel-azureredis.pid
debug = 7
output = /var/log/stunnel4/azureredis.log
options = NO_SSLv2
options = NO_SSLv3
[azureredis]
accept=127.0.0.1:6379
  connect=<AZURE_REDIS_URL>:6380
  client=yes
  TIMEOUTidle = 30


Restart the stunnel4
$ service stunnel4 restart

Install redis-cli to test the connection try to ping the redis azure it should reply as PONG
$ redis-cli -a <AZURE_REDIS_PASSWORD>
127.0.0.1:6379> ping
PONG

Now you can make SSL connection with azure redis on SSL.