Monday, August 25, 2014

ARP Protocol and Detection of Change in Gateway (router/BHR) through ARP

ARP (Address resolution protocol) is used by the Internet protocol (IP) to map IP address to MAC address. In other words, ARP is used for the resolution of network layer addresses to the link layer addresses and hence it falls as a layer 2 protocol of the OSI model.

Suppose a device (PC/laptop/set top/Mobile) connected via a gateway (router/BHR - Broadband home router) needs to communicate to the outside world over ethernet it needs to determine the MAC address of the gateway (henceforth gateway, router and BHR are used interchangeably) given it's IP address. These IP-to-MAC address mappings are derived from the the ARP cache maintained on every device. 

An ARP cache or an ARP table is a record that contains IP-MAC address mappings of the gateways in the subnet to which a given device is connected. If the IP address does not appear in the device's cache, that device cannot target messages to the target until IP-MAC is obtained. In this case, the device broadcasts an ARP request asking what is the MAC address of the gateway. The gateway receives this ARP request and sends an ARP response to the device that contains it's MAC address. This information is updated in the ARP cache of the device. Then on, whenever the device wishes to communicate over ethernet the MAC address of the gateway is obtained from the ARP cache unless the ARP cache is expired. 
This is all about the ARP protocol, arp cache, arp request and arp responses. Now let's see about detecting a gateway change in the network. 

You may be thinking why gateway change has to be detected and what happens if the gateway change goes undetected ?
Generally all the devices connected to the router obtains a DHCP IP address. Here the device not really owns a DHCP IP address but it leases for a certain period of time after the expiration of which the DHCP IP lease has to be renewed. 
Consider an use case scenario in which a client device has leased a DHCP IP from the gateway/router (let the router be R1 having the ip IP1). Let the router R1 be replaced with the router R2 having the same IP address IP1. In such a scenario if the gateway change is not detected the client device would not renew it's lease until the lease expires. The arp cache would still be containing the MAC address of router R1 and all communications from the client devices fail as the arp cache is not updated with the new router's MAC address.

Let's see how do we detect a change in the gateway and thereby renew DHCP IP address ?
When the client device wishes to communicate with the other device or to the public internet, it broadcasts ARP request asking the MAC address of the gateway. The gateway responds with a message (ARP response) which includes it's MAC address. If the gateway is replaced with a different gateway, the ARP response would contain a different MAC address and the change in the gateway could be detected. Once the gateway change is detected, all the client devices de-configures and binds to DHCP IP interface again to obtain a new DHCP IP that helps in maintaining the connectivity which otherwise results in a outage.

Thus the detection of a change in the gateway and thereby renewing DHCP IP address puts the device active in the network and keeps the device connected.  

Wednesday, July 9, 2014

"Learning to Code" and "Coding to Learn"

I recently came across Mitchell Resnick's Interview. Mitchell Resnick, PhD, is a Papert Professor of Learning Research and the director of Lifelong kinderGarten group at MIT Media Lab. He speaks about the importance of creative learning through Computer Science and in turn Coding. He clearly illustrates the differences between the two paradigms: "Learning to code" and "Coding to learn". He brings out the importance of coding like identifying the problems, creative thinking, organizing and reformulating the ideas to meet the needs apart from the jobs and careers it would help in. I have always believed that coding teaches us to think systematically.

The below is one of the excerpt from Mitchell's interview where he speaks about the importance of Coding as a subject in school:

Should every child learn to code? Should coding be a school subject like algebra or chemistry?
RESNICK: I do think every child should learn to code, and I would approach it similarly to writing—the same way we teach children to write and then let them use their writing in all other courses. You learn to write and then use your writing in writing book reports and writing science reports—you use it in all other subjects. And I think similarly, it would be great for all kids to code and then use that knowledge in many other classes.
Events like Hour of Code have raised the visibility of coding and given people an opportunity to get some sense of what coding is all about. But it only will be meaningful if there’s a follow-up and follow-through. If people take that initial spark and turn an hour of code into a day of code or a week of code or a month of code where they continue to explore the possibilities of coding.

So it’s the same thing. If you just spend an hour learning to write, it wouldn’t be so useful. On the other hand, if that sparks your imagination and then you continue to do more things with it, then it becomes meaningful

Source/Complete Mitchell's interview is available here

Wednesday, July 2, 2014

Writing a running code without main()..!


We all know that main() is the starting point in any C/C++/Java program, But in linux, when a program is executed, always _start() will be called which does cleanup of resources like memory, etc and then calls exit() with main() as it’s argument. Suppose the main() is missing in the program, compiler throws up a undefined reference error to main() function

_start() function can be overriden.

_start function (Inside C)

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crt1.o(.text+0x18): In
function `_start':: undefined reference to `main'
collect2: ld returned 1 exit status

Above error definitely means that there is no main() definition in any of the source files of the project. To dig it further, why _start and why collect2?

_start function is the entry point of a C program which makes a call to main()
collect2 is GCC utility which is used to call various utility function during start time. like ld etc.

Going further into it, main() is the starting point of a C program from the programmer's perspective. Before calling main, a process executes a bulk of code to "clean up the room for execution".
_start is the function which gets called first, which then allocates necessary resources and then calls main() which has to be defined by the programmer.

You can override _start and tell the compiler to not to look for main by using "-nostartfiles" option.

#include<stdio.h>
_start()
{
printf("Hello world!!\n");
_exit(0);
}

To Compile : gcc -nostartfiles sampleTest.c -o a.out
Without _exit(0); the program will compile, but encounters a segmentation fault when it's run.


The other ways to write a running program without using main() is to use
  • token pasting operator (Eg: #define fun  m##a##i##n)
  • main being defined in a macro (Eg: #define fun main; int fun() { })

Citations:


Tuesday, July 1, 2014

Understanding 'Extern' keyword

Let me begin my first post with a topic on 'Extern' keyword in C

'Extern' keyword in C is used in two contexts: variable and functions.

Before delving into the usage of extern let's touch upon 'function declaration/definition' in C. I'm sure almost (everyone) of you will be aware of declaration/definition of a variable/function but for the sake of completeness of this post, I would like to clarify them.

Declaration of a variable/function declares that a variable/function exists somewhere in the program but memory is not allocated. And it also gives details about type of a variable and about the arguments, their order and the return type in case of a function. This is about the declaration of a variable/function. Coming to the definition of a variable/function: it allocates the memory for that variable/function. Therefore, we can think of definition as a super set of declaration.

Coming back to the main definition of EXTERN:

Extern keyword associated with a variable/function extends the visibility of a variable/function throughout the program. By default all C function declarations and definition have "extern" keyword prepended with them. This means that even though we don't use "extern" with function declaration/definition it is present there.

For example, When we write int foo(int arg1, int arg2);
the compiler internally treats this as extern int foo(int arg1, int arg2);

Same is the case with the definition of a function in C. Memory will be allocated to the function only during the definition of the function and this indicates the a function can be declared any number of times in header or source files (.h/.c files) but it can be defined only once. As "extern" extends the visibility of the functions, they can be called anywhere form the program provided the declaration is known. So this is about the "extern" keyword associated with functions.


Now let's delve into the usage of "extern" keyword with the variables.

Any variable declared with "extern" keyword could be used anywhere else in the program. Unlike the previous case with functions variables are not declared/defined as "extern" by default. A variable in 'C' without the "extern" keyword will be declared and also memory will be allocated whereas the variable with "extern" declaration is just a declaration and no memory will be allocated and it can be used anywhere else in the program.

Let us consider the following examples:

example 1:

int var;
int main()
{
     var = 25;
     return 0;
}

the above snippet is perfectly fine as 'var' is declared and defined.


example 2:

extern int var;
int main()
{
    return 0;
}
The above is also fine as the extern variable var is never used (it is just declared)

example 3:

extern int var;

int main()
{
     var = 10;
     return 0;
}

Here, as the extern variable 'var' is not defined no memory is allocated. Compiler throws a undefined reference err in this case.

exmaple 4:

#include "SomeFile.h"

extern int var;
int main()
{
     var = 5;
     return 0;
}

This compiles successfully provided the var is defined in "SomeFile.h"

example 6:

extern int var = 50;

The above statement both declares and defines in a single statement and hence the future referral or updation to the 'var' will not throw any error.

So this is about some insight to "Extern keyword in C'
I hope it is helpful in some way and I have not disappointed :)

To summarize:


1. Declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.


Citations:
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
http://en.wikipedia.org/wiki/External_variable