Thursday, 27 December 2012

What is Static Library and Dynamic Library? How to generate the static and dynamic library?



How to Generate Static Library and Dynamic Library(Shared Libaries) Using Makefile:

First of all i would like give some introduction about static library and dynamic library.

The difference between static and dynamic libraries can be viewed as how you get to work. You can drive your car or you can take a train. Driving your own car is kinda like a static library. Taking the train is kinda like a shared library.

When you drive your car all you need is the car. When you take the train there is usually more than one train car by itself. Usually there's a locomotive, and then some passenger cars, and maybe a caboose to boot. As long as all the cars are working in harmony you continue to get to work. If any of the cars has a problem your chances of getting to work diminish.

When you compile a program with static libraries statically linked libraries are linked into the final executable by the linker. This increases the size of the executable. Likewise when a library needs to be updated you'll need to compile the new library and then recompile the application to take advantage of the new library. Ok, so why do we have static libraries then? Well if you're booting your system into maintenance mode static libraries can be beneficial.


What is Static Library?

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``.a'' suffix. This collection is created using the ar (archiver) program.  

In static linking, the size of the executable becomes greater than in dynamic linking, as the library code is stored within the executable rather than in separate files

Static libraries permit users to link to programs without having to recompile its code, saving recompilation time. Note that recompilation time is less important given today's faster compilers, so this reason is not as strong as it once was. Static libraries are often useful for developers if they wish to permit programmers to link to their library, but don't want to give the library source code.

To create a static library, or to add additional object files to an existing static library, use a command like this:

                   ar rcs library.a File1.o File2.o
or 
                   ar -qcs library.a File1.o File2.o 



Makefile to Generate Static Library.

Let's take small example, suppose there are two files and generate the static library and link it to main file.

file1.c

int fun1(int a, int b)
{
  int c;

  /* add the Two Variables */
  c= a+b;
  return(c);
}

file2.c

int fun2(int a, int b)
{
  int c;

  /* add the Two Variables */
  c= a+b;
  return(c);
}
 Above there are two files, here you can find the Makefile to generate the library using file1.o and file2.o

Makefile to generate MyLibrary.a using file1.o and file2.o

CC = gcc

CFLAGS = -g -Wall

# .a extension for static library
# .so extension for dynamic library
# step 1:
# it goes to step2:

all: libMylibrary.a


#step 3:
# Return to step 2
# Declare this OBJS before it is used, Here OBJS is a variable.

OBJS = $(PWD)/file1.o \
       $(PWD)/file2.o

#step 2:

libMylibrary.a : $(OBJS)
        ar -qcs libMylibrary.a $(OBJS)


# step 4:
# Return to step 3

$(PWD)/%.o : %.c
        $(CC) $(CFLAGS) -c $< -o $@

clean:
        rm -f *.o
        rm -f *.a


How to Link this library to the Main function file to get .exe

mainFile.c

#include<stdio.h>
#include<stdlib.h>

extern int fun1(int a, int b);
extern int fun2(int a, int b);

int main()
{

  printf("The value of Function1: %d", fun1(4,5));
  printf("The value of Function2: %d", fun2(6,7));

  return 0;
}

Makefile links library and generate the test.exe

CC = gcc

CFLAGS = -g -Wall

# .a extension for static library
# .so extension for dynamic library
# step 1:
# it goes to step2:

all: test


#step 3:
# Return to step 2

OBJS = $(PWD)/mainFile.o \

#step 2:

test : $(OBJS)
        $(CC) $(CFLAGS) $(OBJS) $(LIBDIR) $(LIBLOG) -o test.exe


#step 4:
# Return to Step 3

$(PWD)/mainFile.o : mainFile.c
        $(CC) $(CFLAGS) -c mainFile.c

# Step 5:
# For static library, always include LIB DIR
# This is Mandatory
LIBDIR =  -L./
LIBLOG = -lMylibrary

clean:
        rm -f *.o
        rm -f *.a

 Important Point:
When ever you are linking static library to mainFile,  it is mandatory to mention the LIBDIR( where the library is present) using -L option. This $(LIBDIR) should be included when generating .exe file. 

Note: How to run your own makefile, suppose i have makefile as Makefile_org, then run using the below command.
make -f Makefile_org
clean :
 make -f Makefile_org clean

Real Time Example.
Suppose there are three modules BMODULE, CMODULE , DMODULE. BMODULE  is having dependency upon other two modules i.e. CMODULE , DMODULE
Hence, first compile CMODULE  and DMODULE and link their objects to BMODULE to generate the .exe.
Here  DMODULE generates the static library and CMODULE generates the object files(o's)

Step1:  Create Directories of BModule, DModule, CModule

-bash-3.2#  mkdir mreddya
-bash-3.2#  mkdir MAKEFILE_EXERCISE  

create directories under /root/mreddya/MAKEFILE_EXERCISE/as shown below.
-bash-3.2#  mkdir BMODULE
-bash-3.2#  mkdir CMODULE 
-bash-3.2#  mkdir DMODULE
 
Step2:  Create sub-directories of BModule, DModule, CModule
create sub directories under each module as shown below.
-bash-3.2# cd DMODULE/
-bash-3.2#  mkdir src
-bash-3.2#  mkdir inc
-bash-3.2#  mkdir obj 

-bash-3.2# cd CMODULE/
-bash-3.2#  mkdir src
-bash-3.2#  mkdir inc
-bash-3.2#  mkdir obj 

-bash-3.2# cd BMODULE/
-bash-3.2#  mkdir src
-bash-3.2#  mkdir inc
-bash-3.2#  mkdir obj 

Step 3:  Generate Library for DModule for the below files

-bash-3.2# cd DMODULE/
-bash-3.2# cd
Create four files under source directory, dModulefile1.c  dModulefile2.c  dModulefile3.c, Makefile
-bash-3.2# cd DMODULE/
-bash-3.2# cd inc
Create three files under inc,dModulefile1.h  dModulefile2.h  dModulefile3.h


dModulefile1.c

#include "dModulefile1.h"

int DModuleFunction1(int a)
{
   printf("I am in DModuleFunction1 and its Value:%d", a);
   a = a + FUNC_VALUE4;
   return(a);
}

dModulefile2.c

#include "dModulefile2.h"

int DModuleFunction2(int b)
{
   printf("I am in DModuleFunction2 and its Value:%d", b);
   b = b + FUNC_VALUE5;
   return(b);
}

dModulefile3.c

#include "dModulefile3.h"

int DModuleFunction3(int c)
{
   printf("I am in DModuleFunction3 and its Value:%d", c);
   c = c + FUNC_VALUE6;
   return(c);
}

dModulefile1.h
#include<stdio.h>
#define FUNC_VALUE4 14

dModulefile2.h
#include<stdio.h>
#define FUNC_VALUE5 11

dModulefile3.h
#include<stdio.h>
#define FUNC_VALUE6 13

Step 4:  Makefile for DMODULE (Makefile place in src directory)

# define the Compiler as gcc
CC = gcc

# define compiler flags "CFLAGS"  if you -g option it is for GDB, -wall to display all
# warnings

CFLAGS= -g -Wall

# TO include Directories, To include any directories give -I/Path Directory
# e.g: INCLUDEDIR = -I/VOBS/CMODULE/INC/, to include one more directory
# then Syntax will be INCLUDEDIR += -I/VOBS/CMODULE/INC/
#                     INCLUDEDIR  = -I/VOBS/BMODULE/INC/

INCLUDEDIR = -I/root/mreddya/MAKEFILE_EXERCISE/DMODULE/inc

# define MACRO for OBJECT DIRETORY
OBJ-DIR = /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj

# copy all .o( Objects) in to object directory
# All Objects are stored in OBJ-DIR
#
OBJS = $(OBJ-DIR)/dModulefile1.o \
        $(OBJ-DIR)/dModulefile2.o \
        $(OBJ-DIR)/dModulefile3.o \


# First whenever we give Make, it comes here, and Check OBJS
# Then It will check what and all .o are required from 
# OBJS directory
# then it will go Command to generate the .o's.

all:    libDmodule.a

# Generate the Library

libDmodule.a: $(OBJS)
        ar -qcs libDmodule.a $(OBJS)

#To Generate %.o means same directory.
# target ( It tells $(OBJ-DIR)/%.o : %.c, generate the %.c files # to .o files using command.)
# To get the Target the below command should be executed, 
# otherwise Linker error will be there.

$(OBJ-DIR)/%.o : %.c
        $(CC) $(CFLAGS) $(INCLUDEDIR) -c $< -o $@


clean:
        rm -f $(OBJ-DIR)/*.o
        rm -f *.a

Step 5:  Run the Makefile and check the results

-bash-3.2# pwd
/root/mreddya/MAKEFILE_EXERCISE/DMODULE/src
-bash-3.2# make
gcc -g -Wall -I/root/mreddya/MAKEFILE_EXERCISE/DMODULE/inc -c dModulefile1.c -o /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj/dModulefile1.o
gcc -g -Wall -I/root/mreddya/MAKEFILE_EXERCISE/DMODULE/inc -c dModulefile2.c -o /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj/dModulefile2.o
gcc -g -Wall -I/root/mreddya/MAKEFILE_EXERCISE/DMODULE/inc -c dModulefile3.c -o /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj/dModulefile3.o
ar -qcs libDmodule.a /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj/dModulefile1.o /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj/dModulefile2.o /root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj/dModulefile3.o

-bash-3.2# ls
dModulefile1.c  dModulefile2.c  dModulefile3.c  libDmodule.a  Makefile

-bash-3.2# pwd
/root/mreddya/MAKEFILE_EXERCISE/DMODULE/obj
-bash-3.2# ls
dModulefile1.o  dModulefile2.o  dModulefile3.o
Step 6:  Generate objects for CMODULE

-bash-3.2# cd CMODULE/
-bash-3.2# cd
Create four files under source directory, cModulefile1.c  cModulefile2.c  cModulefile3.c, Makefile
-bash-3.2# cd CMODULE/
-bash-3.2# cd inc
Create three files under inc,cModulefile1.h  cModulefile2.h  cModulefile3.h

cModulefile1.c

#include "cModulefile1.h"

int CModuleFunction1(int a)
{
   printf("I am in CModuleFunction1 and its Value:%d", a);
   a = a + FUNC_VALUE1;
   return(a);
}
cModulefile2.c

#include "cModulefile2.h"

int CModuleFunction2(int b)
{
   printf("I am in CModuleFunction2 and its Value:%d", b);
   b = b + FUNC_VALUE2;
   return(b);
}

dModulefile3.c

#include "cModulefile3.h"

int CModuleFunction3(int c)
{
   printf("I am in CModuleFunction3 and its Value:%d", c);
   c = c + FUNC_VALUE3;
   return(c);
}

cModulefile1.h
#include<stdio.h>
#define FUNC_VALUE1 10

cModulefile2.h
#include<stdio.h>
#define FUNC_VALUE2 11

cModulefile2.h
#include<stdio.h>
#define FUNC_VALUE3 11


Step 7:  Makefile for CMODULE (Makefile place in src directory)

# define the Compiler as gcc
CC = gcc

# define compiler flags "CFLAGS"  if you -g option it is for GDB, -wall to display all
# warnings

CFLAGS= -g -Wall

# TO include Directories, To include any directories give -I/Path Directory
# e.g: INCLUDEDIR = -I/VOBS/CMODULE/INC/, to include one more directory
# then Syntax will be INCLUDEDIR += -I/VOBS/CMODULE/INC/
#                     INCLUDEDIR  = -I/VOBS/BMODULE/INC/

INCLUDEDIR = -I/root/mreddya/MAKEFILE_EXERCISE/CMODULE/inc

# define MACRO for OBJECT DIRETORY
OBJ-DIR = /root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj

# copy all .o( Objects) in to object directory
# All Objects are stored in OBJ-DIR
#
OBJS = $(OBJ-DIR)/cModulefile1.o \
        $(OBJ-DIR)/cModulefile2.o \
        $(OBJ-DIR)/cModulefile3.o \


# First whenever we give Make, it comes here, and Check OBJS
# Then It will check what and all .o are required from 
# OBJS directory
# then it will go Command to generate the .o's.

all: $(OBJS)

#To Generate %.o means same directory.
# target ( It tells $(OBJ-DIR)/%.o : %.c, generate the %.c files to .o files using command.)
#    command.
#To Generate %.o means same directory.
# target ( It tells $(OBJ-DIR)/%.o : %.c, generate the %.c files to .o files using command.)
#    command.
# To get the Target the below command should be executed, otherwise
# Linker error will be there.

$(OBJ-DIR)/%.o : %.c
        $(CC) $(CFLAGS) $(INCLUDEDIR) -c $< -o $@


clean:
        rm -f $(OBJ-DIR)/*.o


Step 8:  Run the Makefile and check the results

-bash-3.2# pwd
/root/mreddya/MAKEFILE_EXERCISE/CMODULE/src

-bash-3.2# make clean
rm -f /root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj/*.o

-bash-3.2# make
gcc -g -Wall -I/root/mreddya/MAKEFILE_EXERCISE/CMODULE/inc -c cModulefile1.c -o /root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj/cModulefile1.o
gcc -g -Wall -I/root/mreddya/MAKEFILE_EXERCISE/CMODULE/inc -c cModulefile2.c -o /root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj/cModulefile2.o
gcc -g -Wall -I/root/mreddya/MAKEFILE_EXERCISE/CMODULE/inc -c cModulefile3.c -o /root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj/cModulefile3.o
-bash-3.2# pwd
/root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj

-bash-3.2# ls
cModulefile1.o  cModulefile2.o  cModulefile3.o

Step 9: Get .exe by Linking the CMODULE and DMODULE to BMODULE 

-bash-3.2# cd BMODULE/
-bash-3.2# cd
Create two files under source directory, bModulefile.c,  Makefile
-bash-3.2# cd BMODULE/
-bash-3.2# cd inc
Create one file under inc,bModulefile.h

bModulefile.c

#include<stdio.h>

#include "bModulefile.h"

/* extern the Functions */
extern int CModuleFunction1(int );
extern int CModuleFunction2(int );
extern int CModuleFunction3(int );
extern int DModuleFunction1(int );
extern int DModuleFunction2(int );
extern int DModuleFunction3(int );


void getFunValue(void)
{
  int result = 0;

  result = CModuleFunction1(3)* FUNC_VALUE8;
  printf("The Result value with CModuleFunction1:%d\n", result);
  result = CModuleFunction2(4)* FUNC_VALUE8;
  printf("The Result value with CModuleFunction2:%d\n", result);
  result = CModuleFunction3(5)* FUNC_VALUE8;
  printf("The Result value with CModuleFunction3:%d\n", result);
  result = DModuleFunction1(3)* FUNC_VALUE8;
  printf("The Result value with DModuleFunction1:%d\n", result);
  result = DModuleFunction2(4)* FUNC_VALUE8;
  printf("The Result value with DModuleFunction2:%d\n", result);
  result = DModuleFunction3(5)* FUNC_VALUE8;
  printf("The Result value with DModuleFunction3:%d\n", result);
}
int main()
{
   /* call the getFunValue Function */
   getFunValue();
   return 0;
}

bModulefile.h


#define FUNC_VALUE8 1

Step 10:  Makefile for BMODULE (Makefile place in src directory)
This Makefile links both CMODULE and DMODULE and generates the test.exe


Some points to be Noted here:
Before compiling BMODULE, first it is mandate to compile CMODULE and DMODULE.
Once CMODULE and DMODULE compiled and run the Makefile of BMODULE.

Step 11:  Makefile compiles first CMODULE and DMODULE then BMODULE
Run this Makefile using make all command


# This is the Main Make file which incorporates all the files and
# generates the .exe file.

# define the Compiler Macro as gcc
CC = gcc

# define any Flags whether to compile as 64 bit(-m64) or with -g option
# to debug in gdb and also to show file name with warnings.
CFLAGS = -g -Wall

# Include the Directories which you want to link here.
# Include the .h directories.
# Whenever multiple directories are included then use +=  with -I option
INCL_DIR += -I/root/mreddya/MAKEFILE_EXERCISE/BMODULE/inc

# define MACRO for OBJECT DIRETORY
OBJ-DIR = /root/mreddya/MAKEFILE_EXERCISE/BMODULE/obj

EXE-DIR = /root/mreddya/MAKEFILE_EXERCISE/BMODULE/src

COBJ-DIR = /root/mreddya/MAKEFILE_EXERCISE/CMODULE/obj

DLIB_DIR = /root/mreddya/MAKEFILE_EXERCISE/DMODULE/src


OBJS = $(OBJ-DIR)/bModulefile.o

COBJS = $(COBJ-DIR)/cModulefile1.o \
        $(COBJ-DIR)/cModulefile2.o \
       $(COBJ-DIR)/cModulefile3.o \


EXTERNALLIBS = -L$(DLIB_DIR)
LIBLOG       = -lDmodule



CMODULE_COMPILE:
        cd /root/mreddya/MAKEFILE_EXERCISE/CMODULE/src && make
DMODULE_COMPILE:
        cd /root/mreddya/MAKEFILE_EXERCISE/DMODULE/src && make
BMODULE_COMPILE:
        $(CC) $(CFLAGS) $(INCL_DIR) -c *.c -o $(OBJS)

all:
        make all_modules

all_modules:
        make CMODULE_COMPILE
        make DMODULE_COMPILE
        make BMODULE_COMPILE
        $(CC) -o test.exe $(OBJS) $(EXTERNALLIBS) $(LIBLOG) $(COBJS)


clean:
        rm -f $(OBJ-DIR)/*.o
        rm -f $(EXE-DIR)/*.exe
        rm -f $(COBJ-DIR)/*.o
        rm -f $(DOBJ-DIR)/*.o

There are several ways to write make files, this is one way.
write small script and compile the Makefiles like show below.

build.sh script

cd /root/mreddya/MAKEFILE_EXERCISE/CMODULE/src
make
cd /root/mreddya/MAKEFILE_EXERCISE/DMODULE/src
make

cd /root/mreddya/MAKEFILE_EXERCISE/BMODULE/src
make

What is Dynamic Library or Shared Library (.so)?

Dynamic library is also called as Shared Library. 
Shared Library is a binary file(.so file) contains object (.o) files. All object files form as shared library  and this library can be loaded or unloaded dynamically.

By using shared library concept, one can make the code is position-independent, meaning that the code may be loaded anywhere in memory. Applications linked to shared objects at run time.
 
How to generate a shared library:

Step 1: Create object code, it means generate object files (.o files) 

 gcc -fPIC -c <filename.c>

The -fPIC option tells gcc to create position independent code which is necessary for shared libraries.
  e.g: gcc -fPIC -c myfilename.c

Step 2: Create shared library 

 gcc -g -Wall -shared -o  <library Name>  <objectfile1.o,  objectfile2.o>

 It will create the library with .so extension.
e.g: gcc -g -Wall -shared -o  libMyfuncs.so file1.o  file2.o


How to use the shared library to your application

Step 1:
Set the environment for Library using 
 export LD_LIBRARY_PATH=.

The above indicates the path directory where the library is present. ./ or  .  indicates the present directory

Step 3:
Compile your Main file and generate the object code.
e.g: gcc -c mainFile

And then Link the shared library  to the main file as mentioned below.


 e.g: gcc  -L./ -lMyfuncs mainFile.o -o test.exe

 Notes:
suppose if  "-L/opt/exp", means library is present in this directory and link the library using -llibName. The name of the library is Myfuncs.so.
The libraries will NOT be included in the executable but will be dynamically linked during run time execution.
That's reason size of .exe is small when we compare with static library.

How to check the list of shared libraries using by the application.

Use simple command.
ldd <.exe>

 -bash-3.2# ldd test.exe
        libMyFun.so => ./libMyFun.so (0x00002ab25d70f000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003ef7c00000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003ef6c00000)



Simple Makefile to generate shared library

file1.c

int func1(int a, int b)
{
    int c;
    /* Add */
    c= a+b;
    return(c);
}

file2.c

 int func1(int a, int b)
{
    int c;
    /* Add */
    c= a+b;
    return(c);
}

Makefile to generate shared library as lib

CC = gcc

CFLAGS = -g -Wall

OBJS = $(PWD)/file1.o \
        $(PWD)/file2.o

all: libMyFun.so

libMyFun.so : $(OBJS)
        $(CC) $(CFLAGS) -shared -o libMyFun.so $(OBJS)

$(PWD)/%.o : %.c
        $(CC) $(CFLAGS) -fPIC -c *.c
clean:
        rm -f *.o
        rm -f *.so


 How to Link this shared library to the mainFile.c

mainFile.c


#include<stdio.h>

extern int func1(int, int);
extern int func2(int, int);

int main()
{

  printf("The result of Function 1 is: %d\n", func1(4,5));
  printf("The result of Function 1 is: %d\n", func2(10,20));
  return 0;
}


Build.sh  <Generate the test.exe using small script>

#step 1: clean the Library
make clean

#step 2: Generated the Library
make

#step 3: Generates the mainFile.o

gcc -c mainFile.c

#step 4: clean the .exe
rm -f *.exe

#step 5: Link the Shared library to mainFile.o and
#generates the test.exe file.

gcc -L./ -lMyFun mainFile.o -o test.exe


Run the Build.sh

chmod +x Build.sh
./Build.sh



Friday, 24 August 2012

IPv6


IPv6:
IPv6 stands for  Internet protocol version 6.
IPv6 utilizes 128-bit Internet addresses. Therefore, it can support 2^128 Internet addresses — 340,282,366,920,938,000,000,000,000,000,000,000,000 of them to be exact. 

That’s a lot of addresses, so many that it requires a hexadecimal system to display the addresses.
In other words, there are more than enough IPv6 addresses to keep the Internet operational for a very, very long time.
IPv6 address generally represents in Hexa Decimal Format( Since it has bigger (128-bit) address space).

IPv6 address represented as XXXX: XXXX: XXXX : XXXX : XXXX: XXXX: XXXX: XXXX  ( XX -> 1 Byte XXXX->2 bytes , total 16 bytes = 128 bits and all the values are represented in the Hexa Decimal format).

IPv6 Address Representation:
 Like IPv4, IPv6 address also has network address and Interface Id. 64 bits represents Network address and 64 bits represents the Interface Id.
  
For example :   
 21DA:00D3:0000:2F3B: 02AA:00FF:FE28:9C5A  ( In this example first portion of 64 bits represents the network address and  remaining portion of 64 bits represents the host address.)

There are many ways of representing the IPv6 address:
Zero compression:   If there are zeros in the IPv6 address, then it can be compressed.

Way 1: Leading zeros in the address field are optional and can be compressed as mentioned below.
Example 1:     
2031:0000:130F:0000:0000:09C0:876A:130B  = 2031:0:130F:0:0:9C0:876A:130B
                                                                                                (compressed form)
Example 2:
            0000 = 0 (compressed form)

A pair of colons (::) represents successive fields of 0. However, the pair of colons is allowed only once in a valid IPv6 address.

Example 1:
            2031:0:130F:0:0:9C0:876A:130B = 2031:0:130F :: 9C0:876A:130B (compressed form)

Example 2:
FF01:0:0:0:0:0:1 = FF01 :: 1

Example 3:
2031:0000:130F:0000:0000:09C0:876A:130B 
2031:0:130F:0:0:9C0:876A:130B
            
              2031:0:130F::9C0:876A:130B 

IPv6 Address Prefix:

The prefix can defined as "Part of address indicates the bits have Fixed values or are the Bits of the   Network Identifier.
An IPv6 Address Prefix is represented in "address/prefix-length"
    
Note: Prefix Length indicates the No.of Bits represents the Network Address.
    
For Example: FE80:2233:4445:2244:1133:1122:0:1/64

In the above example prefix length is 64, first 64 bits represents the Network Address ( FE80:2233:4445:2244 )

IPv6 Sub-netting:


IPv6  sub netting is little different than IPv4 sub netting.Let’s take small example to understand about IPv6 sub netting.

2000: 4567 :7896/48 , The first 48 bits represents the NETWORK address and Next 16 bits used for Subnetting and Last 64 bits are Host bits. 

2000:   4567:   7896:             XXXX :                 3456:   7895:   1233:  9876
<Network Address>            <Subnet Id>             < HOST Bits(64)  address >

The Number of sub nets are (2^16) =65536 and each sub-net will have 2^64 hosts can be connected.
The Sub-net addresses are shown below.

2000:   4567:       7896:          0000:
2000:   4567:       7896:          0001:
2000:   4567:       7896:          0002:
|                                                |
|                                                |                      
2000:   4567:       7896:          FFFF:                 
 

What if Prefix length is not Multiply of 4?
     To properly express a subnet with a prefix where its prefix length is not a multiple of 4, we must complete hexadecimal to binary conversions to determine the appropriate subnet identifier.

For example:
    - To express the subnet of the address and prefix of

         21DA: D3: 0: 2F3B: 2AA: FF: FE28: 9C5A/5959 bits represents the Network Address, remaining 5 bits represents the subnet, so total of subnets are 2^5 = 32  and 64 Bits represents the Host Address.

         21DA:   D3:    0:    2F3 B:     2AA:FF:FE28:9C5A/59

         - we must convert the “3B” in “2F3B” to binary (0011 1011),

         21DA:   D3:    0:    2F  0011 1011:       
        
        <------59 bits-------------->                     (  MASK  with AND & operator)
       
         21DA:   D3:    0:    2F  1110  0000:

        ============================        
         21DA:   D3:    0:    2F  0010 0000 =>    21DA:   D3:    0:    2F20 is the Subnet Identifier


IPv6 Address Types ( Classes):

1. Unicast Addressing ( ONE- to - ONE Communication )
A unicast address identifies a single network interface.
The Internet Protocol delivers packets sent to a unicast address to that specific interface.

In Other words:
An address for a single interface. A packet that is sent to a unicast address is delivered to the interface identified by that address.

2. Multicast Addressing ( ONE- to - MANY Communication )
            A multicast address is also used by multiple hosts, which acquire the multicast address destination by participating in the multicast distribution protocol among the network routers. 
A packet that is sent to a multicast address is delivered to all interfaces that have joined the corresponding multicast group.
3. Anycast Addressing ( ONE- to - ONE -of MANY communication)

An anycast address is assigned to a group of interfaces, usually belonging to different nodes. A packet sent to an anycast address is delivered to just one of the member interfaces, typically the nearest host, according to the routing protocol’s definition of distance. 
Anycast addresses cannot be identified easily, they have the same format as unicast addresses, and differ only by their presence in the network at multiple points. Almost any unicast address can be employed as an anycast address.

Any cast addressing is used for one-to-one-of-many communication, with delivery to a single interface.

What about Broadcast in IPv6?  
There is no broadcast in IPv6.
This functionality is taken over by multicast.
A consequence of this is that the all 0’s and all 1’s addresses are legal.

Unicast Address Types:
IPv6 has several major unicast address types.
  • Unicast global addresses  (2000::1/3)
  • Unicast site-local addresses  (FEC0::/10)
  • Unicast link-local addresses  (FE80::/64)
  • Loop back Addresses ( the loopback routing prefix ::1/128 consists of only one address ::1 (0:0:0:0:0:0:0:1 in full notation,)
  • Unspecified Addresses(00::00/128) all are zeros)

Types of IPv6 Addresses in represented in Tabular format
Address type Binary prefix IPv6 notation
Unspecified 00 . . . 0 (128 bits) ::/128
Loopback 00 . . . 1 (128 bits) ::1/128
Multicast 11111111 FF00::/8
Link-local unicast 1111111010 FE80::/10
Site-local unicast 1111111011 FEC0::/10
Global unicast (everything else) starts from (2000::/3 - E000::/3)
  
 What Is an IPv6 Link-Local Unicast Address?

   A link-local unicast address is an IPv6 unicast address that is automatically configured on
an IPv6 node interface by using the link-local prefix FE80::/10 (1111 1110 11) and the interface ID in the EUI-64 format.
 

It is used to communicate with other nodes on the same link. The below figure shows two nodes on a single subnet using Link local IP addresses.  Two nodes on a same sub-net communicate using the Link local IP address ( No need of Routers)



Nodes on single subnet using link-local addresses 

Routers will not forward any packets with link-local source or destination addresses to other links.

How to Use of EUI-64 Format in IPv6 Addresses in link local Address or Global?

EUI- Extended Universal Identifier

To create the IPv6 interface identifier from the 48-bit (6-byte) Ethernet MAC address:

The hexadecimal digits 0xFF-FE are inserted between the third and fourth bytes of the MAC address.

The Universal/Local bit (the second low-order bit of the first byte of the MAC address) is complemented. If it is a 1, it is set to 0; and if it is a 0, it is set to 1.

For example, for the MAC address of 00-60-08-52-F9-D8:
 
The hexadecimal digits 0xFF-FE are inserted between 0x08 (the third byte) and 0x52 (the fourth byte) of the MAC address, forming the 64-bit address of 00-60-08-FF-FE-52-F9-D8.
 
The Universal/Local bit, the second low-order bit of 0x00 (the first byte) of the MAC address, is complemented. The second low-order bit of 0x00 is 0 which, when complemented, becomes 1. The result is that for the first byte, 0x00 becomes 0x02.

As a result, the IPv6 interface identifier that corresponds to the Ethernet MAC address of 00-60-08-52-F9-D8 is 02-60-08-FF-FE-52-F9-D8. 

The link-local address of a node is the combination of the prefix FE80::/64 and the 64-bit interface identifier expressed in colon-hexadecimal notation. 
As a result, the link-local address of this example node, with the prefix of FE80::/64 and the interface identifier 02-60-08-FF-FE-52-F9-D8, is FE80::260:8FF:FE52:F9D8.
 
For example

 
Uniqueness mask 000000X0 where X=1 is unique and X=0 in not unique. So if X=1 then the EUI-64 Address is 02 90 27 FF FE 17 FC 0F

Characteristics
  • Mandatory addresses that are used exclusively for communication between two IPv6 devices on the same link
  • Automatically assigned by device as soon as IPv6 is enabled
  • Not routable addresses (Their scope is link-specific only.)
  • Identified by the first 10 bits (FE80)
  • Typically created using the EUI-64 format
Addressing
  • Link Local Identifier (10 bits): Always begins with FE80::/10 (i.e. 1111 1110 10)
  • Remainder (54 bits): Could be all zeros or manually configured to another value.
  • Example: FE80:0000:0000:0000:0987:65FF:FE01:2345 or FE80::987:65FF:FE01:2345 (shorthand format)
What Is an IPv6 Global Unicast Address? 
A global unicast address is simply what we call a public IP address in IPv4—that is, an IP address that is routed across the whole Internet. 
You can make out a global unicast address easily: The first three bits are set to 001. Thus, the address prefix of a global IPv6 address is 2000::/3 because 0010000000000000 is 2000 in hex. 
However, in the future, the IANA (Internet Assigned Numbers Authority) might delegate currently unassigned portions of the IPv6 address space. Hence, 2000::/3 won’t always be the prefix for global unicast addresses.
 
IPv6 - tutorial - Global unicast address 


Characteristics
  • Routable and reachable across the Internet
  • IPv6 addresses for widespread generic use
  • Structured as a hierarchy to allow address aggregation
  • Identified by their three high-level bits set to 001 (2000::/3)
Addressing
  • Global Routing Prefix (32 bits): 001 + 29 bit global routing prefix. Assigned to a service provider by IANA.
  • Site Level Aggregator (16 bits): Assigned to a customer by a service provider.
  • LAN (16 bits): Assigned to an individual network by the customer.
3a9284ffe6c72fe5139010f28b07e7ba png


 Small Example with Routers:

 In this example, the routers R1, R2 and R3 are connected via serial interface and have the IPv6 addresses configured as mentioned in the network diagram. Loopback addresses are configured on the routers R1 and R3, and the routers use OSPFv3 to communicate with each other. This example uses the ping command to demonstrate the connectivity between the routers using link-local addresses. The routers R1 and R3 can ping each other with the IPv6 global unicast address, but not with their link-local address. However, router R2 being directly connected to R1 and R3 can communicate with both the routers using their link-local address, because link-local addresses are used only within that local network specific to the physical interface.ipv6-lla-01.gif 

What Is an IPv6  Multicast Address? 

A multicast address identifies not one device but a set of devices a multicast group. A packet being sent to a multicast group is originated by a single device; therefore a multicast packet normally has a unicast address as its source address and a multicast address as its destination address. A multicast address never appears in a packet as a source address.

The members of a multicast group might include only a single device, or even all devices in a network.
Characteristics
  • Contain an 8 bit prefix identifier – FF00::/8 (i.e 1111 1111)
  • The second octet defines the lifetime and scope of the multicast address
  • Multicast addresses are always destination addresses. Multicast addresses are used for router solicitations (RS), router advertisements (RA), DHCPv6, multicast applications, and so forth.
  • Important Note: A default gateway configuration is not required by IPv6 clients because routers are discovered using RSs and RAs.
Common Addresses
  • FF01::1 – Node local, within the same node
  • FF02::1 – Link-local, all nodes on a link
  • FF01::2 – Node-local, same router
  • FF02::2 – Link-local, all routers on a link
  • FF05::2 – Site-local, all routers on the Internet
  • FF02::1:FFxx:xxxx – Link-local, solicited node
59177c45262af7726b0f3fc5dac35ae1
 
 
Table  Examples of well-known IPv6 multicast addresses.
Address
Multicast Group
FF02::1
All Nodes
FF02::2
All Routers
FF02::5
OSPFv3 Routers
FF02::6
OSPFv3 Designated Routers
FF02::9
RIPng Routers
FF02::A
EIGRP Routers
FF02::B
Mobile Agents
FF02::C
DHCP Servers/Relay Agents
FF02::D
All PIM Routers