Sunday, 25 March 2012

Basic of Network Protocol

ARP

When a device needs to send an IP packet to another device on the local network, the IP software will first check to see if it knows the hardware address associated with the destination IP address. If it founds then the sender simply transmit the data to the destination. However, if the destination system's hardware address is not known then the IP software has to locate  it before any data can be sent. at this point, IP will call on ARP to locate the hardware address of the destination system.
ARP achieves this task by issuing a low level broadcast onto the network, equesting that the system that is using the specified IP address respond with its hardware address. if the destination system is powered up and on the network, it will see this broadcast (As will all  of the other devices on the local network), and it will return an ARP response back to the original system. not that the response is not broadcast back of the network. but is instead sent directly to the requesting device.

ARP structure

ARP packets work at the data-link layer, the same as IP packets. As such, ARP packets are completely separate from IP packets; they even have a different protocol  ID of 0806, instead of 0800 as used with IP.ARP packets contain several fields, although only five of them are actually used to proved ARP's functionality.
Field Usage
Source Hardware address The hardware address of the sender's device
Source IP address The network address of the senders device
Destination Hardware address The hardware address of the receiver's device (when this field is unknown ARP set it to all-zero)
Destination IP address The network address of the reciver's device
Message-Type Indicates whether the current ARP packets is a request or a response to a request

ARP Request

When a device is sending ARP request, it fills in three of the four address-related fields, providing its own hardware and IP address, as well as the IP address of the target. the destination hardware address is yet unknown so that field is filled with zeros. In addition it will set the message type to indicate that the current  packet is an ARP request, and then broadcast the request onto the local network for all devices to see

ARP Reply

All of the local devices should monitor the network for ARP broadcasts, and when ever they see a request for themselves they should generate a response packet and send it back to the requesting system. The response packet will consist of the local device's IP address of the original sender. The response will also be marked as as such, with the message-type field indicating that the current packet is an ARP response. the new ARP packet is then unicast directly to the original requester, where it is received and processed.

Friday, 2 March 2012

Mysql Restore and Backup

1. Getting backup of a MySQL database using mysqldump.

Use following command line for taking backup of your MySQL database using mysqldump utility.

mysqldump –-user [user name] –-password=[password] [database name] > [dump file]
 
or
mysqldump –u[user name] –p[password] [database name] > [dump file]

Example:





mysqldump –-user root –-password=myrootpassword db_test > db_test.sql
 
or
 
mysqldump –uroot –pmyrootpassword db_test > db_test.sql

2. Backup multiple databases in MySQL.


mysqldump –u[user name] –p[password] [database name 1] [database name 2] .. > [dump file]
Example:

mysqldump –-user root –-password=myrootpassword db_test db_second db_third > db_test.sql

3. Backup all databases in MySQL.


shell> mysqldump –u[user name] –p[password] –all-databases > [dump file]

4. Backup a specific table in MySQL.



shell> mysqldump --user [username] --password=[password] [database name] [table name] \
> /tmp/sugarcrm_accounts_contacts.sql
Example:


shell> mysqldump --user root --password=myrootpassword db_test customers \
> db_test_customers.sql

5. Restoring MySQL database.

The mysqldump utility is used only to take the MySQL dump. To restore the database from the dump file that you created in previous step, use mysql command.


shell> mysql --u [username] --password=[password] [database name] < [dump file]
Example:

shell> mysql --user root --password=myrootpassword new_db < db_test.sql

Tuesday, 28 February 2012

C Program to show Dynamic Memory Allocation using Malloc Function

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    int no, *pt,i;
    clrscr();
    printf("Enter no of Students :");
    scanf("%d",&no);
    pt=(int *)malloc(no*2);
    if(pt== NULL)
    {
        printf("\n\nMemory allocation failed!");
        getch();
        exit(0);
    }
    printf("* * * * Enter roll no of students. * * * *\n");
    for (i=0;i<no;i++)
    {
        printf("-->");
        scanf("%d",(pt+i));
    }
    printf("\n* * * * Entered roll no. * * * *\n");
    for (i=0;i<no;i++)
    {
        printf("%d, ",*(pt+i));
    }
    getch();
}
 

Code Explanation:

Let’s dig out the above program; this is a simple c program example which uses integer pointer to dynamically allocate memory according to user’s input at runtime. As I told you earlier that we can do this either with malloc() or calloc(). In this example I am using malloc() and calloc() is explained below. To use malloc() or calloc() function we must include stdlib.h or alloc.h header file otherwise you will get malloc prototype error at compilation. Line no. 3 includes the stdlib.h header file. In line no. 7 an integer pointer pt and some other integer is declared which we will need later in input and loop. Line no. 11 stores integer number in no variable which is used as number of students whose roll number we are going to input. Next line (line no. 12) is most important one in this program; it does all allocation jobs. If malloc function is able to allocate memory then it returns address of memory chunk that was allocated else it returns NULL.
pt=(int *)malloc(no*2);
As you already know that malloc is a function, it requires one parameter or argument i.e. number of bytes to reserve. In our program we provided no*2 as parameter. The variable no holds the number of students whose roll no we are going to enter. But why we are multiplying no with 2? Here is answer, suppose we want to store roll number of 10 students. We stored 10 in no variable and passed this no variable in malloc function. But do you think it will work? Because integer occupies 2 bytes and we wanted to store 10 students information. And if we pass 10 to malloc then compiler will reserve 10 bytes only which can store only 5 students’ information. To solve this issue I have multiplied no with 2 so if you enter 10 then it will reserve 20 bytes of memory. If you don’t remember which data type occupies how much memory then you can use sizeof() which return data type size.
pt=(int *)malloc(no*sizeof(int));
Just before malloc() I have used (int *), this is called type casting (means converting from one data type to other compatible data type). If memory allocation is successful then malloc() by default returns pointer to a void and we type cast that to integer pointer and stores the address in pt integer pointer variable. So pt pointer contains starting address of memory allocated using malloc().
If, due any reason, malloc() fails to allocate memory it returns NULL. Line no. 13 – 18 does the same job, if malloc() fails to allocate memory the it will show error and exit from program.
Line no 21 – 25 is used to take roll no of student and store it in memory. Look at line no. 24 and particularly at (pt + i) statement. Integer pointer pt contains starting address of memory which is allocated by malloc() and by adding i variable value we are instructing it to move further to store value in that memory chunk. For example, if pointer starting address is 1002 then (pt+1) would mean 1004, (pt+2) = 1006 and so on.
Line no. 28 – 31 prints the value stored in the memory and thing is simple.