IPv4 Address
An IPv4 address identifies a device on the Internet uniquely(although there are some technique to make it non-unique)
IPv4 address : 32 bits long, a.b.c.d
Each octet is represented in decimal
Netmask
Some devices may be in the same network
For example,network 255.255.255.0 means if the 24 bits are the same, these IPs are in the same network
1 | $ ifconfig |
1 | def same_network_with_me(netmask:int,other_device_IP:int,my_IP:int): |
My solution :
1 | source = ['128.34.1.15' , '10.0.1.4' , '10.0.1.4' , '171.64.15.33' , '171.64.15.33'] |
Classless Inter-Domain Routing (CIDR)
Lots of IPs are wasted in this allocation policy
Today’s Address Structure
- Still assign contiguous ranges of address to nearby networks
- Assign addresses by
block
- Address block is a pair :
(address , count)
count
is power of 2, specifing the netmask length171.64.0.0/16
means any address in range171.64.0.0
to171.64.255.255
xxx.xxx.xxx.xxx/24
describe a block of 256 addresses
How IPv4 addresses are assigned?
Managed by IANA(International Assign Number Authority)
Longest Prefix Match
The algorithm used by routers to forward packages to different directions of link
- The router resort to a Forwarding table to forword the package
- It search the forwarding table for a best match and forward the package
- If no match is found, any package will fall back to the default match whose pattern is
x.x.x.x
, matching any IP
Quiz
My solution :
1 | # maybe some bit level searching tree will have better performance |
1 | Package with IP : 63.19.5.3 goes to link 3 |
My implementation
Of course, there can be other implementation
Address Resolution Protocal(ARP)
ARP provides a mechanism to translate IP addresses to link-layer addresses.
- Each layer has its own address
port
for application layerIP address
for network layerlink layer address(Network card address, MAC)
for link layer
- An IP address specify a host in the network which can have multiple network card
- A link layer address can specify a unique device(network card) which can receive and send link frames
- A ethernet card is a typical network card whose address is in form of
xx:xx:xx:xx:xx:xx
, represented by hexadecimal, 48-bit long.
1 | A ------------------------------> B |
So we can see that we need a way to decide a MAC address from a given IP address, which means we need a Mapping(ARP)
ARP
Request and Relay mode
- The NIC first search local ARP cache for destination host’s MAC address
1 | $ arp -a |
- If not found, the NIC send a arp request package via broadcast(
ff:ff:ff:ff:ff:ff
)
- All nodes in the network receive the package, if someone see that the requested IP is correspond to his own IP, he will then send a relay package
- The NIC receive the relay package and update the ARP cache
ARP spoof
The attack can only be used on networks that use ARP, and requires attacker have direct access to the local network segment to be attacked
(Since ARP only work in a LAN !)