IP Address Prefixes ACM programming practice problem. Ron Smith.

9
IP Address Prefixes ACM programming practice problem. Ron Smith

Transcript of IP Address Prefixes ACM programming practice problem. Ron Smith.

Page 1: IP Address Prefixes ACM programming practice problem. Ron Smith.

IP Address Prefixes

ACM programming practice problem.

Ron Smith

Page 2: IP Address Prefixes ACM programming practice problem. Ron Smith.

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.

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.

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.

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.

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.

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.

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)*'.'