Showing posts with label IP. Show all posts
Showing posts with label IP. Show all posts

Thursday, 16 February 2023

DNS (Domain Name System)

 

  
 

DNS (Domain Name System)

  • a protocol, part of the Internet Protocol (IP) Suite
  • hierarchical and distributed naming system for computers, services, and other resources in the Internet
  • naming database in which internet domain names are located and translated into Internet Protocol (IP) addresses 
  • translates domain names to IP addresses so browsers can load Internet resources
  • helps Internet users and network devices discover websites using human-readable host names, instead of numeric IP addresses; For humans, domain names are a lot easier to remember than a sequence of numbers.
  • DNS configuration settings of some website are what allows visitors to still access that website even after it gets moved to a new hosting provider (its IP address will change but domain name will not)
 
For a hosted web site we need to specify (usually 2) DNS servers. These could be provided by hosting provider but we can specify custom ones e.g. Cloudflare DNS servers. These DNS servers will be nodes in DNS distributed database system which will be providing DNS records about our domains for whoever queries about them. Let's see which DNS records we can set.

Common DNS Records

  • A (A record, Address record,  IPv4 address record)
    • maps from an IPv4 address to a domain name
    • used to point the domain name at one or multiple IP addresses
    • also referred to as a host or hostname
  • AAAA (IPv6 address record) maps domain name to IPv6 address
  • CNAME (Canonical Name record)
    • used to create an alias from one hostname to another
    • maps one domain name (an alias) to another (the canonical name)
    • Example: example.com has an A record which points to the IP address. If we say "www.example.com is a CNAME to example.com" and "ftp.example.com is a CNAME to example.com" that means that someone accessing www.example.com or ftp.example.com will be pointed to the same IP address that example.com points to. This is useful so that when your IP address changes, you only have to update example.com’s entry (DNS A record for example.com), and www.example.com and ftp.example.com automatically point to the right place.
    • If you already have an A record, you will not use a CNAME
    • CNAME record tells anyone visiting a subdomain to also use the same DNS records as another domain or subdomain. 
    • This sort of thing is convenient when running multiple services from a single IP address (e.g. FTP server and web server share the same IP address but different port)
    • CNAME records only work for subdomains and must always point to another domain or subdomain and never directly to an IP address.
    • When a DNS resolver encounters a CNAME record while looking for a regular resource record, it will restart the query using the canonical name instead of the original name.
  • MX (Mail eXchanger)
    • allows you to control the delivery of mail for a given domain or subdomain. In our context, MX records can be set on a host-by-host basis to point to other hosts on the Internet (usually with permanent connections) that are set up to accept and/or route mail for your hostname(s). Setting a backup MX makes the entry you specify a secondary mail exchanger. This means that delivery will be attempted to your host first, and then to the backup host you specify if that fails.
  • TXT (TXT records) 
    • used to store information. Common uses include SPF, DKIM, etc.



CNAME vs A name

An A record points a hostname directly to an IP address, while a CNAME record points a hostname to another hostname.

Think of an A record like a physical street address, and a CNAME record like a nickname or a forwarding address. They are both parts of the Domain Name System (DNS), which acts like the internet's phonebook to help computers find websites.

Quick Comparison


Feature                 A Record (Address)                  CNAME Record (Canonical Name)
What it points to   An IP address (e.g., 192.0.2.1)    Another domain name (e.g., ://shopify.com)
Best used for        The main root domain                  Subdomains and third-party services 
                             (e.g., yourdomain.com)                (e.g., ://yourdomain.com)
Speed                   Faster (resolves in one step)         Slower (requires multiple lookup steps)
Coexistence         Can live with MX, TXT, and       Cannot share a name with any other records
                             other records

The A Record: The Direct Map


The "A" stands for Address. This is the most basic type of DNS record. It connects a name you can type (like a website) straight to the unique number of the computer hosting that site.
  • The Analogy: You look up "The Central Library" in a phone book, and it gives you the exact physical location: 123 Main Street.
  • Real-world Example: You want your root domain mywebsite.com to open your web server. You create an A record pointing to your server's IP address.
    • Name: mywebsite.com
    • Type: A
    • Value: 192.0.2.23

The CNAME Record: The Alias


"CNAME" stands for Canonical Name. Instead of pointing to a number, it points to another name. It tells the computer, "Hey, I don't have the IP address, go look at this other website name to find it!"
  • The Analogy: You look up "The City Book Place" in the phone book. The book says, "This is just another name for The Central Library. Go look up The Central Library instead."
  • Real-world Example: You build an online shop using a service like Shopify. Shopify gives you a unique store address like ://myshopify.com. You want customers to see ://mywebsite.com instead. You create a CNAME record:
    • Name: ://mywebsite.com
    • Type: CNAME
    • Value: ://myshopify.com


Example Scenario: What Happens Behind the Scenes?

Imagine a user types ://mywebsite.com into their browser. Here is how the internet figures out where to go using both records:
  1. The CNAME Step: The computer asks the DNS server for the location of ://mywebsite.com. The server replies: "That name is just an alias for ://myshopify.com."
  2. The A Record Step: The computer then asks the DNS server: "Okay, what is the address for ://myshopify.com?"
  3. The Final Destination: Shopify’s DNS server looks at its own A record and replies: "The IP address is 23.227.38.65."
  4. The website opens!

Why use a CNAME instead of an A Record?


If Shopify ever changes its server IP addresses, they only have to update their own A record. Because your CNAME points to their name and not their number, your shop keeps working perfectly without you changing a thing.


Can CNAME coexist with A name?


In the Domain Name System (DNS), a CNAME (Canonical Name) record cannot coexist with other DNS records that share the exact same hostname (Name).

Because a CNAME acts as a pointer or alias for another domain, it overrides all other records for that specific name. Standard DNS specifications explicitly prohibit mixing a CNAME with A, AAAA, MX, TXT, or NS records for the same host.

The Exceptions to the Rule


There are only two scenarios where a CNAME can be used alongside other data at the same name:
  • DNSSEC: If your domain uses DNS security, cryptographic records (like RRSIG and NSEC) must exist alongside the CNAME.
  • "CNAME-like" proprietary records: Many DNS providers (like DNSimple, Cloudflare, and others) offer custom ALIAS or ANAME records. These synthetic records allow you to alias a domain while still hosting other records like MX records for email alongside them.

Common DNS Conflicts


If you try to add a CNAME, you will usually get an error from your DNS provider if you already have these records for the same name:
A or AAAA records: CNAMEs cannot map to an IP address directly, so they conflict with address records.
MX records: If you need a CNAME for a web service, but also need MX records for email, you cannot put both on the same subdomain (e.g., you cannot have ://yourdomain.com be both a CNAME and an MX record).
TXT records: You cannot verify a domain with a TXT record if the hostname already has a CNAME.


How to Fix Coexistence Issues


If you need both types of records for the same purpose, consider these common workarounds:
  • Use subdomains: You can create a CNAME on ://yourdomain.com and use your main yourdomain.com for your MX/TXT records.
  • Use ALIAS/ANAME records: If your host allows it, switch to an ALIAS/ANAME record for the zone apex (the root domain) so that you can simultaneously maintain MX records for email.
  • Use A/AAAA records: Instead of creating a CNAME, simply resolve the target domain to its IP address and create standard A/AAAA records, which can coexist with TXT or MX records.


DNS records for bojankomazec.com, my domain registered with GoDaddy:

========================================================= 
Type   Name  Data               TTL    Delete     Edit
========================================================= 
A @ 216.58.213.115         600 seconds
NS @ ns31.domaincontrol.com. 1 Hour Can't delete Can't edit
NS @ ns32.domaincontrol.com. 1 Hour Can't delete Can't edit
CNAME email email.secureserver.net. 1 Hour
CNAME ftp bojankomazec.com. 1 Hour
CNAME ihXXXXXXXX6i gv-7sXXXXXXXXv3xr.dv.googlehosted.com. 1 Hour
CNAME www ghs.google.com.                                 1 Hour
CNAME _domainconnect _domainconnect.gd.domaincontrol.com. 1 Hour
SOA @ Primary nameserver: ns31.domaincontrol.com.    600 secs
MX @ smtp.secureserver.net. (Priority: 0)         1 Hour
MX @ mailstore1.secureserver.net. (Priority: 10) 1 Hour
========================================================= 

CNAME records in blue are those that I had manually to set so that my bojan-komazec.blogspot.com gets redirected to bojankomazec.com. See Set up a custom domain - Blogger Help. Also, look at https://www.nslookup.io/domains/bojankomazec.com/webservers/.

Linux offers a DNS lookup tool which can be used to find out the nameservers and e.g. IP address of the domain:

$ host -t ns bojankomazec.com
bojankomazec.com name server ns31.domaincontrol.com.
bojankomazec.com name server ns32.domaincontrol.com.

$ host bojankomazec.com

bojankomazec.com has address 216.58.213.115
bojankomazec.com mail is handled by 10 mailstore1.secureserver.net.
bojankomazec.com mail is handled by 0 smtp.secureserver.net.



It can take up to 72 hours for setting new DNS records to take effect - while change is replicated across all DNS servers on the internet. (see DNS Propagation)
 
The network of DNS servers is hierarchical. Types of DNS servers are:
  • Recursive resolvers (DNS recursors)
    • clients first send to them DNS queries
    • they are assigned by ISP but can be set manually:
      • Cloudflare 1.1.1.1
      • Google (8.8.8.8 and 8.8.4.4)
    • they respond either with cached data or send the request to root, TDL and finally to Authoritative nameserver from which they receive IP address
    • every recursive resolver knows about 13 (types of) DNS root nameservers
  • Root nameservers
    • when receive query about some domain name e.g. example.com they return the address of the TLD nameserver which contains information about the domain extension e.g. .com 
    • there are over 600 root nameservers which sync among themselves (anycast routing) and all contain the same data
  • TLD (Top-Level Domain) nameservers
    • they are domain extension-specific - each of them contains the list of authoritative servers for only a single domain e.g. .com or .ai. 
    • they return the address of authoritative servers
    • Larger TLDs and registrars (like GoDaddy, Namcheap etc...) use an API call to notify the TLD operator of any new registrations and changes
  • Authoritative nameserver
    • resolver’s last step in the journey for an IP address
    • they are domain-specific - each of them contains the list of IP addresses for a particular doman e.g. ftp.example.com or www.example.com
    • they return the IP address for a given hostname or, if domain has a CNAME (alias domain name), resolver needs to repeat the whole process in order to get the IP address for that alias host name.
    • when you register your web site, name servers you set for it are authoritative nameservers
 

To manage DNS records of domain e.g. example.com means setting DNS records for its root and subdomains. For each record we set:
  • Type: A, CNAME, MX, ...
  • Name: e.g. ftp (for ftp.example.com)
  • Content: this is the value which depends on the type e.g. IPv4 address if A record, alias if CNAME etc....
  • Proxy status: DNS only (proxy disabled) or Proxied (proxy enabled)
  • TTL (Time to Live) - in minutes


NS Records


NS records (short for Name Server records) are a fundamental part of the Domain Name System (DNS). They tell the rest of the internet which DNS servers are the authoritative bosses for your domain.

In plain English: when someone types your website address into a browser, the NS records point them to the exact servers that hold the map (the IP addresses) for that domain.

How They Work (The Mailroom Analogy)


Think of the DNS process like looking up a physical business address:

  1. The Request: A user types example.com.
  2. The Registrar: The internet checks the global registry (like .com) to see where example.com is registered.
  3. The NS Record: The registry looks at your domain's NS records and says, "Go talk to ns1.nameserver.com—they have the official records for this site."
  4. The Resolution: The browser asks that specific name server for the actual website IP address (via an A record), and the site loads.

Crucial Rule: Without NS records, your domain is effectively lost. The internet will know you own the name, but it won't know which server to ask for directions to get there.

Why Do You Usually See Two or More?


You will almost never see just one NS record for a domain. They always come in clusters—usually at least two, sometimes four:

  • ns1.provider.com
  • ns2.provider.com

This is entirely for redundancy. If one name server goes down due to a hardware failure or a cyberattack, the other servers are there to instantly pick up the slack so your website, email, and cloud services don't go offline.

Changing NS Records


You typically edit NS records at your domain registrar (the place where you bought the domain).

You usually only change them when you are switching your DNS management to a new provider. For example, if you buy a domain at one company but want to route and protect your web traffic through a cloud platform like Cloudflare, you will replace the original registrar's NS records with Cloudflare's name servers.

Once you update them, it can take anywhere from a few minutes up to 48 hours for routers across the globe to learn the new directions—a period known as DNS propagation.



Resources:


Thursday, 9 June 2022

Internet Protocol (IP) Addresses and Ranges

Internet Protocol (IP) is one of protocols from Network Layer.





Each node in IP network has IP address assigned so IP packets can be routed between them. 


IPv4 Addresses


image source: juniper.net

  • IPv4 - Internet Protocol v4 (1981)
  • each node has its identifier - IP address  
  • 32-bit number divided in octets
    • example: 56.122.17.4
  • dot-decimal notation
  • four octets, four sections of 8 bits
  • each octet (each section) is 8-bit number so in total 32-bits required to represent the full addres; there are 2^32 combinations in total
  • 0.0.0.0 to 255.255.255.255 
  • some addresses and ranges are reserved for e.g. private networks 


IPv6 Addresses

 
image source: juniper.net

  • Introduced in order to expand the range of IP addresses.
  • 128-bit address scheme 
    • 2^128 (undecillion) address combinations 
  • IPv6 as hexadecimal: 8 segments of 16 bits separated by colons
    • Example: 1e03:b32f:042d:0000:0000:0000:0436:4aef
  • How to shorten IPv6 address:
    • Leading zeros in each segment can be omitted.
    • Segments with all zeros can be replaced with single zero:
      • 1e03:b32f:42d:0:0:0:436:4aef
    • Furthermore, a single subset of consecutive zeros can be replaced with two colons: 
      • 1e03:b32f:42d::436:4aef
      • This can be done only once as if we had two occurrences of double-colons we wouldn't know how many zeros each represent. 

Classless Inter-Domain Routing (CIDR) Notation


  • A way of specifying a range of IP addresses, including the case of a single IP address. 
    • Example: 192.10.0.0/16
  • Number after slash denotes leading bits in a 32-bit number that get frozen. In this example, 192.10 (first two octets) stays the same and the rest two octets change so we get the range from 192.10.0.0 to 192.10.255.255. Two last octets are free to change which gives us 2^16 addresses in this range.
  • Examples:
    • 192.10.0.0/24 gives the range 192.10.0.0 to 192.10.0.255
    • 192.10.0.0/32 gives the single IP address: 192.10.0.0  
    • 192.10.0.0/31 gives two IP addresses: 192.10.0.0 and 192.10.0.1
    • 192.10.0.0/30 gives four IP addresses: 192.10.0.0 to 192.10.0.4 
    • 192.10.0.0/17 freezes first 17 bits which is first 2 octets and first bit of 3rd octet so we get the range 192.10.0.0 to 192.10.127.255 (0111111 = 127)
    • 192.10.0.0/28 freezes first 3 octets and first half (first 4 bits) of the 4th octet giving the range of 16 IP addresses: 192.10.0.0 to 192.10.0.15 (00001111 = 15)
  • AWS VPC allows leading bits between 16 (/16) and 28 (/28).
 

Private Network Ranges


  • Public IP range is routable to the Internet. These IP addresses can directly communicate to Internet. 
  • Private network ranges solve the problem of not having enough of IPv4 addresses for all devices connected to Internet.
  • Private Network Ranges (IETF specification RFC1918):
    • 10.0.0.0/8 => 10.0.0.0 to 10.255.255.255 (~16 million addresses)
    • 172.16.0.0/12 => 172.16.0.0 to 172.31.255.255 (~1 million addresses)
    • 192.168.0.0/16 => 192.168.0.0 to 192.168.255.255 (~65k addresses)
  • Private IP range is not routable to the Internet, they are not publicly available, can be used only in private networks. Devices with private network IPs can reach Internet via Network Address Translation or proxy service - something that translates private IP address to public IP address. 

 

ipcalc Tool

 
 
ipcalc is a useful tool which visualizes subnet calculations.
 
To install it on Ubuntu Linux:
 
$ sudo apt install ipcalc
 
To visualize CIDR:

$ ipcalc 192.168.0.0/24
Address:   192.168.0.0          11000000.10101000.00000000. 00000000
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.0.0/24       11000000.10101000.00000000. 00000000
HostMin:   192.168.0.1          11000000.10101000.00000000. 00000001
HostMax:   192.168.0.254        11000000.10101000.00000000. 11111110
Broadcast: 192.168.0.255        11000000.10101000.00000000. 11111111
Hosts/Net: 254                   Class C, Private Internet

---

Monday, 2 May 2016

How to host PHP web application on IIS web server

Although I work as a desktop application developer I talked the other day about PHP language with one of my colleagues who is a web developer. Having at home a Windows machine which comes with IIS web server (IIS10 on Win10), I became curious about what would it take to create a simple PHP web application and host it on IIS. I decided to make a single PHP page which would display the public IP address of the host which sends request.

And here we go. I googled for a simple PHP code which echoes the IP address of the client so all credits for the code below go to the guy who answered the following question on StackOverflow: How to get the client IP address in PHP?

main.php:


Index page in PHP world might have a different standard name and I am sure this code should be rearranged and optimized but I am leaving it as it is as my goal is to make fast proof of concept.

It has been a while since I touched IIS web server last time but I remember I could add a web application under the Default Web Site. Each web application has its own directory at path c:\inetpub\wwwroot so we can save main.php at a new directory: c:\inetpub\wwwroot\WhatIsMyIp. We can then add a new application:



When IIS receives request for PHP page, it has to forward it to PHP engine which would process it. This engine is actually PHP language binding for FastCGI protocol which comes as an executable (php-cgi.exe) within a PHP pack for Windows which can be downloaded from http://windows.php.net/download/. Side note on that page says "If you are using PHP as FastCGI with IIS you should use the Non-Thread Safe (NTS) versions of PHP." so I downloaded the latest version (php-7.0.6-nts-Win32-VC14-x64.zip at the moment...) and unpacked it into arbitrary location e.g. c:\PHP\php-7.0.6-nts-Win32-VC14-x64\.

CGI has to be enabled on IIS and this can be done in Windows Features. If we click on Windows start button and type Turn Windows features on and off, Windows Features window opens. We have to go to Internet Information Services, World Wide Web Services, Application Development Features and select (tick) CGI item:



We can now add a PHP CGI handler to our website in IIS:


We also have to make sure that World Wide Web Publishing Service (W3SVC) is running:


If IIS server and Default Web Site are started, we can test our web application from the local browser:


In order to allow accessing this web site from other devices on the same LAN we have to set Inbound rule on the firewall running on the IIS host. In case of Windows Firewall we can simply enable predefined rule World Wide Web Services (HTTP Traffic-In):


I am using default port for HTTP traffic (TCP port 80) so this rule will allow all incoming packets destined for IIS host and port 80 to reach IIS process - the one which listens to HTTP requests on that port.

If we want to access website hosted on another device within the same LAN we have to know what's its IP address (or hostname if we would set up some DNS resolution within this LAN or on the client device). ipconfig command run on IIS host outputs 192.168.0.3 as its IP.

If we open browser on another device which is on the same LAN and type http://192.168.0.3/WhatIsMyIp we'll get the following:



Obviously, all IP addresses we got are their IP addresses within the same LAN because so far both server and clients were on the same network. All devices behind router have the same public IP address which is the WAN (public) address of the router. This address is usually assigned by the ISP.

The true test would be if we try to load this page from a browser on a device which is not on this network. In order to allow accessing this web site from devices which are not on the same LAN, we have to enable port forwarding for HTTP traffic on the router behind which is host with our IIS server. That could look like this:


If WAN address of this router is e.g. 74.90.112.208 we can call our web app from e.g. mobile device connected to Internet via mobile 3G or 4G interface by typing in the browser: http://74.90.112.208/WhatIsMyIp.