MAC, IP and Gateway address

I was thinking of writing all about it by myself but this answer on Quora was so well written and knowledgeable that I thought of pasting it instead. This is the answer by anonymous:


MAC Address

A MAC address is a 6-byte hexadecimal address that allows a NIC to be uniquely identified on the network. The MAC address forms the basis of network communication, regardless of the protocol used to achieve network connection.A Media Access Control address (MAC address) is a  unique identifier assigned to a network interface card (NIC) by its  manufacturer. The network interface card is used to connect to the  Ethernet network. Each NIC has its own unique MAC address. The MAC  addresses are assigned permanently to adapters and cannot be changed as  they are a unique identification of the hardware interface of network.

The MAC addresses are stored in the NIC’s hardware, its read-only  memory, or some other firmware mechanism. The address encodes the  manufacturer’s registered identification number. A MAC address can also  be referred to as the burned-in address, Ethernet hardware address  (EHA), hardware address or physical address.

Packets that are sent on the ethernet are always coming from a MAC  address and sent to a MAC address. If a network adapter is receiving a  packet, it is comparing the packet’s destination MAC address to the  adapter’s own MAC address. If the addresses match, the packet is  processed, otherwise it is discarded.

The MAC addresses are formed and assigned according to the rules of one  of three numbering name spaces managed by the Institute of Electrical  and Electronics Engineers (IEEE): MAC-48, EUI-48, and EUI-64. The first  half of the MAC address tells what brand/model the card is, and the  second half is a unique identifier specific to that card.

For example, consider the MAC address of the computer on which this book is being written: 00:D0:59:09:07:51. The first three bytes (00:D0:59) identify the manufacturer of the card; because only this manufacturer can use this address, it is known as the Organizational Unique Identifier(OUI). The last three bytes (09:07:51) are then referred to as the Universal LAN MAC address: They make this interface unique.

How to find the MAC address of your computer(any version of Windows):-

  1. Open the command prompt. Press the Win+R keys and type cmd into the Run field. Press Enter(↵) to start the Command Prompt.
  2. Run GetMAC. At the command prompt, type getmac /v /fo list and press Enter(↵). This will display configuration information for all of your network connections.
  3. Look for the physical address which would be your MAC address


IP address
In order to transfer data from one location to another, it must first  know where the users are located. IP distinguishes these as IP  addresses. The IP address is a number that is assigned to each device,  such as a computer, a printer, etc. It describes where on the Internet  your computer is located. An IP address is required by any device that  participates in a computer network that uses the Internet Protocol for  communication.

There are two standards for IP addresses: IP Version 4 (IPv4) and IP  Version 6 (IPv6). All computers with IP addresses have an IPv4 address,  and many are starting to use the new IPv6 address system as well. Here’s  what these two address types mean:

  • IPv4 uses 32 binary bits  to create a single unique address on the network. An IPv4 address is  expressed by four numbers separated by dots. Each number is the decimal  (base-10) representation for an eight-digit binary (base-2) number, also  called an octet. For example: 216.27.61.137
  • IPv6 uses 128  binary bits to create a single unique address on the network. An IPv6  address is expressed by eight groups of hexadecimal (base-16) numbers  separated by colons, as in 2001:cdba:0000:0000:0000:0000:3257:9652.  Groups of numbers that contain all zeros are often omitted to save  space, leaving a colon separator to mark the gap (as in  2001:cdba::3257:9652).

How does your computer get its IP address? An IP address can be either  dynamic or static. A static address is one that you configure yourself  by editing your computer’s network settings. This type of address is  rare, and it can create network issues if you use it without a good  understanding of TCP/IP. Dynamic addresses are the most common. They’re  assigned by the Dynamic Host Configuration Protocol (DHCP), a service  running on the network. DHCP typically runs on network hardware such as routers or dedicated DHCP servers.

IP Classes
  • 0.0.0.0 — This represents the default network, which is the abstract concept of just being connected to a TCP/IP network.
  • 255.255.255.255 — This address is reserved for network broadcasts, or messages that should go to all computers on the network.
  • 127.0.0.1 — This is called the loopback address, meaning your computer’s way of  identifying itself, whether or not it has an assigned IP address.
  • 169.254.0.1 to 169.254.255.254 — This is the Automatic Private IP Addressing (APIPA) range of  addresses assigned automatically when a computer’s unsuccessful getting  an address from a DHCP server.

The other IP address  reservations are for subnet classes. A subnet is a smaller network of  computers connected to a larger network through a router. The subnet can  have its own address system so computers on the same subnet can  communicate quickly without sending data across the larger network. A  router on a TCP/IP network, including the Internet, is configured to  recognize one or more subnets and route network traffic appropriately.


The following are the IP addresses reserved for subnets:

10.0.0.0 to 10.255.255.255 — This falls within the Class A address range of 1.0.0.0 to 127.0.0.0, in which the first bit is 0.

  • 172.16.0.0  to 172.31.255.255 — This falls within the Class B address range of  128.0.0.0 to 191.255.0.0, in which the first two bits are 10.
  • 192.168.0.0  to 192.168.255.255 — This falls within the Class C range of 192.0.0.0  through 223.255.255.0, in which the first three bits are 110.
  • Multi-cast  (formerly called Class D) — The first four bits in the address are  1110, with addresses ranging from 224.0.0.0 to 239.255.255.255.
  • Reserved for future/experimental use (formerly called Class E) — addresses 240.0.0.0 to 254.255.255.254.

Check this link to find the IP address:- How to Find the IP Address of Your PC


Gateway address
The gateway address (or default gateway) is a router interface connected to the local network that sends packets out of the local network. The gateway has a physical and a logical address.

Logical Gateway Address

The gateway operates at the network layer (Layer 3) of the OSI Model. The gateway is used when transmitting packets. When packets are sent over a network, the destination IP address is examined. If the destination IP is outside of the network, then the  packet goes to the gateway for transmission outside of the network. The  gateway is on the same network as end devices. The gateway address must  have the same subnet mask as host devices. Each host on the network uses the same gateway.
The gateway should have a static address, as changing the address  would cause packets not to be delivered. The gateway is typically  assigned either the highest or lowest network address. This is not a  requirement, but many organizations use a consistent addressing scheme  to facilitate network planning.

Physical Gateway Address

The gateway also operates at the data link layer (Layer 2) of the OSI network model. The physical gateway address is called the media access control(MAC) address or burned in address (BIA). The physical address is assigned when the device is manufactured, and cannot be changed. When a frame is sent to a device not on the local network, the gateway’s MAC address is used in the frame header.

You will need your default gateway to allow you to access your admin settings on your router.

After you find out what your default gateway is, you will  be able to make changes to your routers settings. You will use the  default gateway address to access your admin settings of your router. To  do this, type the default gateway address into the address bar of your  browser and hit enter. You will then be prompted for a username and  password. This information can sometimes be found on your router or  direction pamphlet. If you can’t find it on either, you can search the  internet by typing your routers make and model and “default  administrator password”. This will only work if you have not changed the  password on your router. If you have changed the default password and  forget it, you will need to reset your router.
http://10.1.1.1  or  http://192.168.1.1 are both examples of default gateways.


How to find your Gateway address(for Windows users):-

1) Open the command prompt:- Press the Win+R keys and type cmd into the Run field. Press to start the Command Prompt.

2) Type “ipconfig”:- Next type ‘ipconfig’ and press enter Enter(↵) .

3)Look for the “default gateway” which would be the gateway address you are looking for.

#define and typedef

#define is a macro in C++ used as:
#define MACRO_NAME VALUE

For example, `#define SIZE 100`,
Macro makes the replacements at the time of preprocessing, before compilation. All the values are put in expanded code before usual compilation starts.

typedef gives another name to a type and it is used as:
typedef TYPE NEW_TYPE;

Unlike #define it is interpreted by compiler and works as a new name for `TYPE`

See they are looking similiar, if I write #define TYPE int or typedef int INT what’s the difference here.

Difference: #define CHAR_PTR char* and typedef char* CHAR_PTR, lets take them one by one:

#define CHAR_PTR char*
...
CHAR_PTR a, b; //typeid(a).name = char* and typeid(b).name = char
               //because it is simply a replacement
_________________________________________________________________

typedef char* CHAR_PTR;
...
CHAR_PTR a,c; //typeid(a).name() = typeid(b).name = char*

Sources: StackOverflow, Let Us C, The C Programming Language -K&R

Toy C++ program for custom string conversion

I am working on lexer part of compilers right now, the task I was doing is to putting up keyword definitions in cool.flex file. Keywords were given in another file in this format:

//for example keyword is class
#define CLASS 234

and I am supposed to be writing it in cool.flex like this:

//keywords are case-insensitive
[cC] + [lL] + [aA] + [sS] + [sS]        {return (CLASS);}

Instead of doing all this by hand I wrote this C++ program and it worked fine.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
 string str, str1;
 fstream myfile("test.file");
 ofstream file("temp.file");
 while(getline(myfile, str))
 {
 str1="";
 for(int i=0;i<str.length();i++)
 if(isupper(str[i]) && str[i+1]!=' ')
 {
 char ch=putchar(tolower(str[i]));
 file<<"["<<ch<<str[i]<<"]+";
 str1.push_back(str[i]);
 }
 else if(isupper(str[i]) && str[i+1]==' ')
 {
 char ch=putchar(tolower(str[i]));
 file<<"["<<ch<<str[i]<<"]";
 str1.push_back(str[i]);
 }
 file<<" { return("<<str1<<");} \n";
 }
 return 0;
}

Compiler Designing W10L1807

Other Topics

  • Java allows classes to be loaded at the run-time, type checking happens at the compile time and bytecode is verified at the time of run.
  • Classes can be loaded using ClassLoader and also can be unloaded.
  • Initialization is Java is complex and as COOL is a subset of Java, it also has a complex initialization.
  • A class is initialized when it is first used not when it is loaded, as it will be non-deterministic to locate the error source, because classes are loaded in different sections many times.
  • Initializing a class object in Java:
1. Lock the class object for the class
       wait for the lock if another thread is using it
2. If class is being initialized then release the lock and return
3. If it has been initialized the return normally
4. Otherwise mark the object as initialization in progress and release the lock on class
5. Initialize the super class in static order, but initialize static final fields first and before initialization give every field a default value.
6. If error, then mark the class as erroneous
7. If no error, lock class and mark i as initialized and then notify thread that object is ready to use and unlock class
  • If N features are there in the systems then there are potentially N^2 interactions.

Compiler Designing W10L1806

Java Threads

  • Threads bring concurrency to Java programs, In Java each has a Program Counter and stack
  • A program can have multiple threads.
  • Scheduler: A scheduler picks one of the threads and execute one instruction in it. The picking up job is completely random.
  • Thread is class in Java having start and stop method.
  • Two threads can use the same object in the heap.
  • A synchronized method can acquire a lock on the object to be processed, and then can free it.
  • How synchronization works:
class A
{
     int a=0, b=9;
     void f1() {a=8;b=7;}
     void f2() {print(a,b);}
};

if two threads are there and one can implement f1 andd other can f2, then results would be random depending upon the function chosen and instruction executed.

Using keyword synchronized only on write operation won’t work either because only a synchronized function checks for lock.

  • The next point is that even without synchronization, a variable would hold a value given by some thread. A value is written atomically.
    • Although double violates the atomic property, because it actually stored as a two word number. So instructions are divided into two, one is for one and other for the second word. So the instructions can interleave. So doubles are declared to be volatile to avoid the situation of out of the thin air value.

Compiler Designing W10L1805

Java Coercions

  • Conversion from one type to another.
  • Widening, (example int to float) can be implicitly and narrowing, (example float to int)is to be done explicitly.
  • bool has no coercion or cast.
  • Many times coercion can result in surprising behavior. Example of PL/I here:
A = '123';
B = '123'
C = A + B    //C would have three blanks in it as a result, + will force A and B to convert in             //integers, and because C should have a length of 6, and the resulting sum would              //be placed at 
            //the last, the initial three letters will be chosen having blanks.

Compiler designing W10L1804

Java Interfaces

  • It specifiy relationship among classes without using inheritance.
interface XY {void function(int x, int y);}
class X implements XY {void function(int x, int y) {...}}

it is same as multiple inheritance in C++. class X implements A,B,C {...} but an interface need not to be at fixed offset.

Because of no fixed offsets, implementation of interfaces are different. One approach is to use a mapping function that will map method strings to method code. Hashing is used to improve the performance.

Compiler Designing W10L1803

Java Exceptions

  • Handling something unexpectable gracefully.
class Foo {
     public static void main(String[] args){
          try{X();} catch(Exception e){System.out.println("Error");} 
     }
     public void X() throws MyException {
           throw new MyException();
     }
}

Exception expressions:

gif

gif (1)

When try block is executing, it goes all in the stack and when throw is to be executed, all the contents of the try block has to be popped.

  • An uncaught exception during object finalization(cleans object) can not be handled.