need an algorithm for collapsing netblock ranges into lists of superset ranges

后端 未结 4 880
名媛妹妹
名媛妹妹 2020-12-01 16:28

My math-fu is failing me! I need an efficient way of reducing network ranges to supersets, e.g. if I input list of IP ranges:

  • 1.1.1.1 to 2.2.2.5
  • 1.1.1
4条回答
  •  醉梦人生
    2020-12-01 17:35

    Alright, my coworker came up with this answer, which I think is pretty excellent. Let me know if you see any issues:

    • Order the IP ranges by StartingIP
    • For each row "x" to insert:
      • If there is a previous row "y" in the list, fetch:
        • If x and y are contiguous, extend y to x's EndingIP
        • Else if x.StartingIP <= y.StartingIP and x.EndingIP > y.EndingIP, extend y to x.EndingIP
        • Else if x is a subset of y, do nothing
        • Else, create a new range
      • Else, create a new range and insert into the list

提交回复
热议问题