blob: 869d2664df6377bd66048531765c1125a5c6e92a [file] [log] [blame]
Brian O'Connor6a37ea92017-08-03 22:45:59 -07001// Copyright 2016 Open Networking Foundation
David K. Bainbridgedf9df632016-07-07 18:47:46 -07002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070014package main
15
16import (
17 "fmt"
18 "strconv"
19 "strings"
20)
21
22type IPv4 uint32
23
24func ParseIP(dot string) (IPv4, error) {
25 var ip IPv4 = 0
26 o := strings.Split(dot, ".")
27 for i := 0; i < 4; i += 1 {
28 b, _ := strconv.Atoi(o[i])
29 ip = ip | (IPv4(byte(b)) << (uint(3-i) * 8))
30 }
31 return ip, nil
32}
33
34func (ip IPv4) Next() (IPv4, error) {
35 return ip + 1, nil
36}
37
38func (ip IPv4) String() string {
39 b := []byte{0, 0, 0, 0}
40 for i := 0; i < 4; i += 1 {
41 m := IPv4(255) << uint((3-i)*8)
42 b[i] = byte(((ip & m) >> uint((3-i)*8)))
43 }
44 return fmt.Sprintf("%d.%d.%d.%d", b[0], b[1], b[2], b[3])
45}