IP Address Prefixes ACM programming practice problem. Ron Smith.
-
Upload
hector-george -
Category
Documents
-
view
214 -
download
1
Transcript of IP Address Prefixes ACM programming practice problem. Ron Smith.
![Page 1: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/1.jpg)
IP Address Prefixes
ACM programming practice problem.
Ron Smith
![Page 2: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/2.jpg)
CIDR NotationBlock: 190.101.118.99/22IP Address: 190.101.118.99Network Prefix: 22Represents ip addresses with prefixes:
190.101.116.190.101.117.190.101.118.190.101.119.
![Page 3: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/3.jpg)
Our Notationip = 190.101.118.99 n = 22
190.101 . 116 .
190.101 . 117 .
190.101 . 118 .
190.101 . 119 .
Common bytes [0:3]
Criticalbyte [3]
Start = 116
preDot postDot
Total = 4
![Page 4: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/4.jpg)
Critical Byte• Given:
ip = IP network (type list)
n = network prefix (type int)
• Compute:
cByte = n/8 #integer division
common = ip[0:cByte]
crit = int(ip[cByte])
![Page 5: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/5.jpg)
Splitting the critical byte• Given:
Network prefix: n = 22
Critical byte: crit = 118 = (01110110)2
011101 10
Fixed Bits Variable Bits
# fixed bits = n % 8 # variable bits = 8 - n % 8
![Page 6: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/6.jpg)
Finding total, start• Given:
Critical bit: crit = 118
#Variable bits: vbits = 2
• Find:
total = 2vbits
start = crit – crit % total
![Page 7: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/7.jpg)
How to treat ip as an arraydef getitem(s,n,delim='.'):
s = s.split(delim)
s = s[n]
if type(s) == list:
s = '.'.join(s)
return s
![Page 8: IP Address Prefixes ACM programming practice problem. Ron Smith.](https://reader036.fdocuments.net/reader036/viewer/2022072011/56649e245503460f94b126fa/html5/thumbnails/8.jpg)
How to handle dots.• Dot after common:
preDot = (cByte > 0 and cBtye < 4)*'.'
• If cByte == 0, common is empty: No '.'
• If cByte == 4, everything common: No '.'
• Final dot:
postDot = (cByte < 4)*'.'