0

I have this gap in my understanding of networking that I've always had a hard time crossing.

Over the years I've written various little TCP/UDP sender/receiver projects -- mostly simple hello-world-level code, in order to learn the mechanics of sending/receiving data over TCP or UDP.

But almost all the examples I've referenced tend to use 127.0.0.x as the receiver's address. I.e. a TCP listener does a bind() to a 127.0.0.x address.

Similarly, in my Udemy Docker & Kubernetes course, the instructor creates a Node.js web server that listens at 127.0.0.1 port 3000.

I have a shaky understanding that these "loopback" addresses are some OS feature that makes these "virtual addresses" and allows them to act as if they are real network addresses. (That explanation was probably really bad, revealing my shaky understanding)


My questions (I know multiple questions per post are frowned upon, but I hope these questions are logically grouped enough that it's justified asking these questions together):

  • What is the difference between these loopback addresses (I assume 127.0.0.1 - 127.0.0.255?) and "real" (maybe non-loopback is the better word?) IP addresses?
  • On a given PC, what governs whether an IP address is available to the C bind() function?
    E.g in the following code snippet (adapted from here):
    struct sockaddr_in sin;
    int s = socket(AF_INET, SOCK_STREAM, 0);
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(1221908521); // 72.212.216.41 according to http://www.aboutmyip.com/AboutMyXApp/IP2Integer.jsp?ipAddress=72.212.216.41
    sin.sin_port = htons(3000);
    bind(s, (struct sockaddr *) &sin, sizeof sin);
    
    ...what governs whether the IP address 72.212.216.41 is available on that particular PC for the C code to function correctly?
  • Who gave my network card the address 72.212.216.41?
  • Is 72.212.216.41 something that's tightly-coupled to my network card and immutable? Or can some software process change it?
  • Can one network card only have one IP address?
  • Why do so many networking tutorials use of 127.0.0.1 instead of "non-loopback" addresses? (I don't mean to generalize, but I have found this to be largely true)

I think the gist of my questions is I'm trying to understand how IP address assignment/availability works in the context of a single PC.
Like what software/hardware says, "StoneThrow's PC, you're assigned 72.212.216.41, so your C programs are allowed to call bind() with that address"?

2
  • 1
    From my point of view, your questions are too different to be grouped in a single question. To answer your main question : the purpose to use 127.0.0.1 instead of a non localhost address is to make a service available only locally and to prevent other computers to access this service.
    – S. Brottes
    Commented Aug 18, 2022 at 20:36
  • If you connect your computer to the Internet through an ISP-supplied router (sometimes labelled a modem, but the router part matters here) it usually does NAT, and some ISPs do CGNAT as well, thus the address of your computer is NOT the same as the public address seen and reported by whatsmyip-type hosts. Do ip addr on most newish Linux, ifconfig on other Unix, or ipconfig on Windows to see the (non-loopback on Windows) address(es) you can bind. Commented Aug 19, 2022 at 4:35

2 Answers 2

4
  • IP addresses are assigned to interfaces, not computers, etc.

  • Interfaces do not have to be backed by physical hardware, an operating system can provide a driver that looks like a network interface to an application, but is really something else.

  • So, operating systems can present a "loopback" interface to applications, which internally will take incoming traffic and basically copy it to the interface's output buffer without handing it to a device driver that talks to a physical NIC - hence "loopback".

  • Some operating systems have the loopback interface as an interface you can see and manipulate like any other interface (Linux and the common lo interface versus ones like eth0, etc.), and some make the loopback interface invisible (Windows).

What is the difference between these loopback addresses (I assume 127.0.0.1 - 127.0.0.255?) and "real" (maybe non-loopback is the better word?) IP addresses?

All IP addresses are "real". The overarching rules in networking are:

You can do anything you want as long as both sides agree, but usually you don't control the other side, so standards and conventions determine what you can do unless you create conditions where those no longer apply.

127.0.0.0/16 is reserved by the IANA for loopback adapter purposes, and that's it. It's a convention, but a convention all ISPs and operating systems follow.

Technically you could assign 127.0.0.1 to a physical NIC, but I think only Linux would actually let you. However, no ISP router is going to do anything with 127.0.0.0/16 except drop it, so as soon as 127.0.0.1 escapes your router (assuming your router is properly configured to route it to your ISP) it'll be dropped. You can always do whatever you want on your own LAN.

On a given PC, what governs whether an IP address is available to the C bind() function? E.g in the following code snippet (adapted from here):

I think all that bind() cares about is that nothing else is using the same socket. A socket is an IP+port.

what governs whether the IP address 72.212.216.41 is available on that particular PC for the C code to function correctly?

Nothing is stopping you from assigning that IP to anything you want on your network. Whether it will do anything productive depends on how other NICs on your network are configured (are they all on the same subnet?), whether each system can reach a router (get router IP from DHCP or manually set), whether that router works, and your ISP.

Who gave my network card the address 72.212.216.41?

Probably DHCP, and probably your ISPs DHCP server (Cox). If not DHCP, then most operating system's APIPA implementation will kick in. This is assuming you haven't statically set an IP + subnet mask.

Is 72.212.216.41 something that's tightly-coupled to my network card and immutable? Or can some software process change it?

Nope. That's an IP address, and that's layer 3, while your NIC is concerned with layer 2 (Ethernet frames) and layer 1 (physically modulating/listening in on the cable)

While many NICs have "offload engines" that handle portions of TCP/IP, they can all be disabled and TCP/IP done entirely in software.

Can one network card only have one IP address?

Under most operating systems:

  • Most of the time 1 NIC port = 1 interface, but sometimes you can do bonding where 2 or 3 ports work together and look like 1 interface. This is common on servers and switches.

  • An interface can have 0, 1, or more than 1 IP address+subnet mask combination. In IPv6 each interface will usually have at least 2 (one will be link-local).

Why do so many networking tutorials use of 127.0.0.1 instead of "non-loopback" addresses? (I don't mean to generalize, but I have found this to be largely true)

So you don't bother public systems on the Internet that aren't expecting you to talk to them, and also don't bother other systems on your own LAN that aren't expecting you to talk to them. Loopback stays entirely on your machine.

Like what software/hardware says, "StoneThrow's PC, you're assigned 72.212.216.41

Often this is DHCP. There are DHCP servers, and DHCP clients. DHCP clients use Ethernet broadcasts, which send traffic to all systems on a network and does not require an IP, in order to find the DHCP server and get an IP from it.

6
  • Can you clarify the term "127.0.0.0/16"? I don't think I've seen that convention, i.e. IP address, forward-slash, integer. Is that the same as "127.0.0.0:16" (which I think means IP address 127.0.0.0 port 16)?
    – StoneThrow
    Commented Aug 18, 2022 at 21:57
  • "1 NIC port - 1 interface"...is a "NIC port" the little slot where you plug in an Ethernet cable in the back of your PC?
    – StoneThrow
    Commented Aug 18, 2022 at 22:09
  • "An interface can have 0, 1, or more than 1 IP address+subnet mask combination." -- I was mostly grooving along to your answer until this statement, which I had a hard time grokking. How does this play out in terms of the 72.212.216.41 that my NIC's interface was assigned by DHCP?
    – StoneThrow
    Commented Aug 18, 2022 at 22:30
  • 1
    To make that more understandable: in a /24 network the first 24 bits (three numbers) are fixed. So 192.168.0.0/24 would refer to the network 192.168.0.0 with valid IP addresses being 192.168.0.1-254 and 192.168.0.255 being the broadcast address that reaches all of them ( .0 is reserved for the network ). In the case of the giant reserved block (16 million+ addresses) for loopbacks since it's a /8 the broadcast would be 127.255.255.255 if there was ever a use for it.
    – davolfman
    Commented Aug 19, 2022 at 19:58
  • 1
    NIC stands for Network Interface Card. So yes. That's what you plug an ethernet cable into. Technically a WiFi card is a NIC too. It's a general term. You've unwittingly asked a LOT of basic questions so you may want to find some good tutorials or even classes.
    – davolfman
    Commented Aug 19, 2022 at 20:36
1

IP may be assigned manually by you or automatically by different configuration mechanism or protocols. Your address is public and assigned to network interface by your provider by DHCP or by PPTP.
Just read this in a whole: https://en.m.wikipedia.org/wiki/IP_address

IP adresses mainly differs in thier global reachabilty:

https://en.m.wikipedia.org/wiki/Private_network

https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml

IP is assigned to a network interface (card} on your computer and can be changed at any time.

Computer may have several network interfaces and each interface may have several IPs assigned.

127.0.0.1 is used for convience and security. It's always present by default even if there is no network interfaces on computer and software can safely binds to it for communication between local processes only.

https://en.m.wikipedia.org/wiki/Localhost

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .