internet basics

Webserver: Any website that you access, goes to a computer on internet. On that computer is a program running (for linux it's apache) which allows that computer to return the response back to the client. This computer is called a webserver. Before we get into webserver program, let's get some basics.

IP addr:

Each device connected to internet is assigned a unique 32 bit IP addr. These are written as 4 octets(each octet is 8 bits) - e.g. 18.251.48.77. IP addresses can go from 0.0.0.0 to 255.255.255.255. However, IP adr are divided into 5 classes based on addr range:

class A: 1.0.0.0 - 127.255.255.255 => Top 8 bits are Net ID. Bottom 24 bits are Node ID.

class B: 128.0.0.0 - 191.255.255.255 => Top 16 bits are Net ID. Bottom 16 bits are Node ID.

class C: 192.0.0.0 - 223.255.255.255 => Top 24 bits are Net ID. Bottom 8 bits are Node ID.

class D: 224.0.0.0 - 239.255.255.255 => multicast. Top 4 MSB are "1110". Remaining 28 bits are Multicast ID.

class E: 240.0.0.0 - 255.255.255.255 => experimental, reserved for future use

Public vs Private: IP addr can be public or private.

The following ranges are reserved by the Internet Assigned Numbers Authority (IANA) for use as private IP addresses:

  • 10.0.0.0 to 10.255.255.255 => from class A
  • 172.16.0.0 to 172.31.255.255 => from class B
  • 192.168.0.0 to 192.168.255.255 => from class C. All of the 192.x addr are not registered publically, so they can only be used behind a router as private IP addresses. This range is where most private IP addresses fall, which is why the default IP address for most Linksys, D-Link, Cisco, and NETGEAR routers is an IP within this set.

Any other addr besides the private ones listed above are available for use as public IP addr. Public IP addr are the ones that can be accessed by any device connected to internet. Each device connected to internet has 1 uniue public IP addr. A device finds some other device on internet by knowing their public IP addr, and using routers to route to that device. private addresses are non-routable — hardware devices on the internet are programmed to prevent devices with a private IP address from communicating directly with any other IP beyond the router that they're connected to. Private IP addr are not unique. Devices in my home and neighbours home can have same private IP addr.

Home Router:

Home routers have 2 or more n/w i/f (or port) each with it's own IP addr. One i/f connects to Wired internet lines coming to your house. There is usually a modem inbuilt in the router which provides this i/f. This i/f has public IP addr that can be seen by the whole world. This public ip addr is assigned to your router by your ISP provider (AT&T, Spectrum, etc). The ISP provider usually keeps on changing this public addr assigned to your router. That is why you may see diff IP addr of your router on diff days. All other i/f of router are the wired or wireless i/f with their private ip addr. All computers in the house also have their own private IP addr. Thus, behind the router, all devices talk to each other using their private IP addr. These same private ip addr can also be present behind your neighbour's routers. But since when trying to connect to private ip addr, the router never allows the traffic for private ip addr to leave the router, there is no source of confusion b/w your home's private n/w traffic and your neighbor's private n/w traffic, since none of this traffic ever leave their respective router, if it's meant for a device on same n/w.

If you try to find IP addr of your computer, use the "ifconfig" cmd on a terminal in Linux.

ifconfig: interface configuration. Used to view and change the configuration of the network interfaces on your system. It is run at startup to configure network interfaces of your computer. Usually, there are 3 n/w i/f for your computer = wired ethernet, wireless and loopback. Ethernet i/f has name "eth", loopback has "lo" while wireless has "wlan". Names may differ for diff Linux distro. All i/f show 32 bit IP v4 address, and 48 bit IP v6 address / 48 bit mac physical address. If Ethernet i/f is connected, we'll see ip addr assigned as 192.168.1.57.  Loopback i/f shows ip addr as 127.0.0.1. If we have a web server running on our computer, and if we type 127.0.0.1 in our browser, it doesn't goto any other computer, but loops back on same computer and shows main index page (this is useful in debug). Wireless i/f shows IP addr as "inet: 192.168.1.70". NOTE: ethernet i/ and wireless i/f have their own IP addr, which are not same. So, depending on which IP addr you use, you connect via ethernet or wireless to that device.

NOTE: ethernet and wireless i/f show that the computer is assigned a private ip addr from class C, while loopback i/f shows private ip addr from class A. Both addr are private addr. So, if we have webserver running on our computer, we could type 192.168.1.70 in our browser, and it will behave same as loopback, i.e it will show http main index page on our own computer,

On CentOS, you may see more than 1 wireless n/w i/f even though the computer has 1 n/w card. These are virtual n/w switch/bridge with names as virbr0 (virtual bridge 0), etc. These are used by certain applications on your m/c. These virtual i/f can be disabled or removed w/o any effect (except for the pgm using it).

Note: What we got above is the private ip addr of our computer. We don't know the ip addr of other devices talking to our computer. Next, let's try to find IP addr of our modem/router:

route: route cmd shows the kernel routing table entries, and also allows us to edit the table. This is the table that is used by OS for static routing. It's primary use is to set up static routes to specific hosts or networks via an interface after it was configured with the ifconfig program.

> route => route cmd shows below o/p. The flag U indicates that route is up and G indicates that it is gateway. 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         homeportal                 0.0.0.0         UG    600    0        0 wlp3s0
192.168.1.0     0.0.0.0         255.255.255.0       U     600     0         0 wlp3s0
192.168.122.0   0.0.0.0         255.255.255.0     U     0         0         0 virbr0

route -n => this shows gateway IP addr number instead of names.
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0                UG    600    0        0 wlp3s0
192.168.1.0     0.0.0.0         255.255.255.0      U     600    0        0 wlp3s0
192.168.122.0   0.0.0.0         255.255.255.0   U        0      0        0 virbr0
 
From above o/p, we see that if a packet comes into the system (i.e this laptop) and has a destination in the range 192.168.1.0 through 192.168.1.255, then it is forwarded to the gateway 0.0.0.0 (also represented as * on some systems) — a special address which represents an invalid or non-existant destination. So, in this case, our system will not route these packets. Same is the case with ip addr 192.168.122.0 to 192.168.122.255.

If the destination is not in this IP address range, it is forwarded to the default gateway (in this case, 192.168.1.254 which is the router), and that system will determine how to forward the traffic on to the next step towards its destination. 192.168.1.254 is the private IP addr of the router (router has public ip addr also, which is on the other i/f). If we type this addr in browser, 192.168.1.254, we'll see out login menu for the router show up (this page is the http index page of router that it's shipped with).