Saturday, 21 March 2015

An attempt to answer the frequent question "What happens when you type google.com into your browser

What happens when...

This blogpost is an attempt to answer the age old interview question "What happens when you type google.com into your browser and press enter?"
Except instead of the usual story, we're going to try to answer this question in as much detail as possible.

The "enter" key bottoms out

        To pick a zero point, let's choose the enter key on the keyboard hitting the bottom of its range. At this point, an electrical circuit specific to the enter key is closed (either directly or capacitively). This allows a small amount of current to flow into the logic circuitry of the keyboard, which scans the state of each key switch, debounces the electrical noise of the rapid intermittent closure of the switch, and converts it to a keycode integer, in this case 13. The keyboard controller then encodes the keycode for transport to the computer. This is now almost universally over a Universal Serial Bus (USB) or Bluetooth connection, but historically has been over PS/2 or ADB connections.
         In the case of the the USB example: the USB circuitry of the keyboard is powered by the 5V supply provided over pin 1 from the computer's USB host controller. 17.78 mA of this current is returned on either the D+ or D- pin (the middle 2) of the keyboard's USB connector. Which pin carries the current is rapidly toggled between the two creating a high speed bitstream (the rate depending on USB 1, 2, or 3) serially encoding the digital value of the enter key. This serial signal is then decoded at the computer's host USB controller, and interpreted by the computer's Human Interface Device (HID) universal keyboard device driver. The value of the key is then passed into the operating system's hardware abstraction layer.

Interrupt fires...

The keyboard sends signals on its interrupt request line (IRQ), which is mapped to an interrupt vector (integer) by the interrupt controller. The CPU uses the Interrupt Descriptor Table (IDT) to map the interrupt vectors to functions (interrupt handlers) which are supplied by the kernel. When an interrupt arrvies, the CPU indexes the IDT with the interrupt vector and runs the appropriate handler. Thus, the kernel is entered.

(On Windows) A WM_KEYDOWN message is sent to the app

         The HID transport passes the key down event to the KBDHID.sys driver which converts the HID usage into a scancode. In this case the scan code is VK_RETURN (0x0D). The KBDHID.sys driver interfaces with the KBDCLASS.sys (keyboard class driver). This driver is responsible for handling all keyboard and keypad input in a secure manner. It then calls into Win32K.sys (after potentially passing the message through an 3rd party keyboard filters that are installed). This all happens in kernel mode.
     Win32K.sys figures out what window is the active window through the GetForegroundWindow() API. This API provides the window handle of the browser's address box. The main Windows "message pump" then calls SendMessage(hwnd, WM_KEYDOWN, VK_RETURN, lParam). lParam is a bitmask that indicates further information about the keypress: repeat count (0 in this case), the actual scan code (can be OEM dependent, but generally wouldn't be for VK_RETURN), whether extended keys (e.g. alt, shift, ctrl) were also pressed (they weren't), and some other state.
          The Windows SendMessage API is a relatively straightforward function that simply calls the main message processing function (called a WindowProc) assigned to the window handle (hWnd).
The window (hWnd) that is active is actually an edit control and the WindowProc in this case has a message handler for WM_KEYDOWN messages. This code looks within the 3rd parameter that was passed to SendMessage (wParam) and, because it is VK_RETURN knows the user has hit the ENTER key.

Is it a URL or a search term?

Parse URL...

Check HSTS list...

Convert non-ASCII Unicode characters in hostname

  • The browser checks the hostname for characters that are not in a-z, A-Z, 0-9, -, or ..
  • Since the hostname is google.com there won't be any, but if there were the browser would apply Punycode encoding to the hostname portion of the URL.

DNS lookup...

  • Browser checks if the domain is in its cache.
  • If not found, calls gethostbyname library function (varies by OS) to do the lookup.
  • If gethostbyname does not have it cached then a request is made to the known DNS server that was given to the network stack. This is typically the local router or the ISP's caching DNS server.
  • The local DNS server (or local gateway's) MAC address is looked up in the ARP cache. If the MAC address is missing, an ARP request packet is sent.
  • Port 53 is opened to send a UDP request to DNS server (if the response size is too large, TCP will be used instead).
  • If the local/ISP DNS server does not have it, then a recursive search is requested and that flows up the list of DNS servers until the SOA is reached, and if found an answer is returned.

Opening of a socket

1.Once the browser receives the IP address of the destination server it takes that and the given port number from the URL (the http protocol defaults to port 80, and https to port 443) and makes a call to the system library function named socket and requests a TCP socket stream - AF_INET and SOCK_STREAM.
2.This request is passed to the Transport Layer where the extra love that TCP/IP requires for ensuring packet delivery and ordering is added and then a IP packet is fashioned.
3.The IP packet is then handed off to the physical network layer which inspects the target IP address, looks up the subnet in it's route tables and wrapped in an ethernet frame with the proper gateway address as the recipient.
4.This address lookup and wrapping of datagrams continues until one of two things happen, the time-to-live value for a datagram reaches zero at which point the packet is dropped or it reaches the destination.
5.This send and receive happens multiple times following the TCP connection flow:
  • Client chooses an initial sequence number (ISN) and sends the packet to the server with the SYN bit set to indicate it is setting the ISN
  • Server receives SYN and if it's in an agreeable mood:
    • Server chooses its own initial sequence number
    • Server sets SYN to indicate it is choosing its ISN
    • Server copies the (client ISN +1) to its ACK field and adds the ACK flag to indicate it is acknowledging receipt of the first packet
  • Client acknowledges the connection by sending a packet:
    • Increases its own sequence number
    • Increases the receiver acknowledgement number
    • Sets ACK field
  • Data is transferred as follows:
    • As one side sends N data bytes, it increases its SEQ by that number
    • When the other side acknowledges receipt of that packet (or a string of packets), it sends an ACK packet with the ACK value equal to the last received sequence from the other
  • To close the connection:
    • The closer sends a FIN packet
    • The other sides ACKs the FIN packet and sends its own FIN
    • The closer acknowledges the other side's FIN with an ACK

UDP packets

TLS handshake...

TCP packets

HTTP protocol...

HTML parsing...

  • Fetch contents of requested document from network layer in 8kb chunks
  • Parse HTML document
  • Convert elements to DOM nodes in the content tree
  • Fetch/prefetch external resources linked to the page (CSS, Images, JavaScript files, etc.)
  • Execute synchronous JavaScript code

CSS interpretation...

  • Parse CSS files and

Friday, 6 February 2015

What u can do wid linux....not much..think again

   They say Canonical founder Mark Shuttleworth is aiming for world domination. Well, can you blame him...?All of  this recent years have been a hurricane for Linux and open source. Riding on the back of the Android Wave, Linus Torvald's kernel is going to systems many would never have even imagined. That said, let's look at some of the many things people say you can do with Linux. Try your hand at them too if you can.

  1. Inventory your collection in a database. Linux comes packed with many different types of database tools. For example: You could run a database server with MySQL then use Calligra to connect to it and get a visual overview of your databases.
  2. Fully customize your desktop. I know this seems like a lame thing to throw in the middle of a list of cool things to do, but we all love making Linux our own and Linux offers more options for desktop customization than most OS's. You can even change your window manager.
  3. Learn to program in python, C, C++, Assembler, Scratch, or another language with the wealth of compilers, debuggers and visual editors in Linux.
  4. Test hardware with software like memtest86 or one of the hundreds of tools in the Phoronix Test Suite.
  5. Face off against your friends in a friendly 3D game of Alien Arena.
  6. Host you own cloud server with OwnCloud.
  7. Virtualize other operating systems with virtualization software like Xen, VirtualBox or VMWare.
  8. Go Wardriving for access points: wardriving.com has a lot of links to Linux wireless software.
  9. DJ a party or just remix music you like with Mixxx.
  10. Security and surveillance. You can set up a simple web camera streaming to a website with Camstream or opt for a more professional solution using Zoneminder.
  11. Check out the stars with astronomy and space simulation software (Celestia).
  12. Build a MAME cabinet and run retro games all from one box. One guy's blog about building a mame cabinet.
  13. Hunt hurricanes - older Linux Journal article.
  14. Build a cluster with PelicanHPC - we've actually tried this and it takes less than 20 minutes to set up.
  15. Make movies. Linux has lots of great software that can be used to edit and cut movies including: OpenShot, Kdenlive, LIVES, Toonloop, Avidemux, Kino (old) and Cinelerra.
  16. Write a blog. If movies aren't your thing, but you can write, Linux has lots of software that can connect to blogs (Blogilo), check spelling (spell), or just give you a distraction-free slate (pyroom) to write on.
  17. Edit, organize and publish photographs to social networking sites with software like F-Spot, Shotwell, Fotoxx, Digikam or Rawtherapee.
  18. Create PDFs without expensive software (LibreOffice).
  19. Boot Linux from a Live USB key for troubleshooting or just keeping your surfing secure on a different computer.
  20. Record a podcast with Audacity, and create the music with different midi software (Rosegarden).
  21. Conduct biological sequence analysis. Link to what looks spammish but is actually a large collection of Linux biological sequence analysis software at Bioinformatics.org.
  22. Create 3D models and renderings for your movies or games with Blender.
  23. Build a custom Personal Video Recorder (PVR) with MythTV. Want to never worry about drive space on your PVR? Put a few 4 terabyte drives in a system and run MythTV.
  24. Build a robot using an embedded board and Linux. Linuxpcrobot.
- If you’re a programmer, then Linux has a number of compilers, visual editors and debuggers to offer. These will aid you in your endeavours in programming languages like C++, Scratch, Assembler, Python and others.

Wednesday, 21 January 2015

Microsoft is all set to show off Windows 10 tonight.....

        At an event at its Redmond headquarters, the company will finally unveil the Consumer Preview of Windows 10, the next version of Windows, to the world. To be fair, some of us have already been playing with the Technical Preview, an early, unpolished version of Windows 10 that Microsoft released in September last year. But the Consumer Preview marks a special milestone: for the first time, Microsoft will show off brand new features that it hopes will set right everything that Windows 8 did wrong. Here are some of them listed...

1. Continuum
Sure, Windows 10 runs great on traditional laptops and desktops. But what if you have one of those newer, hybrid devices that combine a tablet with a detachable keyboard? Microsoft's solution to fix this problem was something called 'Continuum,' a feature that dynamically changes the user interface as soon as you snap a keyboard on or off. Check out the video below to get an idea of what it will look like.

2. Cortana
 Microsoft's intellligent voice-controlled assistants on its phones will finally be seen in Windows on your laptop and desktop. Talking to your computer isn't as natural as talking to your phone, so we'll have to wait and see if Cortana in Windows is really useful or just a gimmick. But at any rate, it will give Microsoft a one-up over Apple, which doesn't have Siri integrated into its own OS X operating system yet.



3. Support for more media formats
Are you an audiophile that prefers uncompressed FLAC audio over crummy little MP3 files? A serial downloader that gets their movies in open-source MKV? Windows 10 will support these file formats out of the box. Bye bye VLC.

4. Better XBOX integration
While it's unclear how exactly will Microsoft will bring Windows, the world's dominant desktop operating sytstem, and XBOX, one of the two biggest gaming platforms in the world together, rest assured that this will happen. Last year, CEO Satya Nadella made clear that XBOX was an integral part of Microsoft's Windows 10 strategy. Will we be able to stream XBOX One games right down to your Windows PC as some gamers are claiming? We'll find out tomorrow.


5. Windows Phone

http://www.hindustantimes.com/Images/popup/2015/1/WindowsPhone.jpg
Microsoft's mobile platform is just about hanging in there with a 3 percent market share around the world but the company isn't going to give up just yet. Expect Microsoft to unify the underlying code behind Windows and Windows Phone so make it easy for developers to write apps that will run on both desktop and mobile devices seamlessly.