blob: fb85defea6e9cca5541fa9da64df3dc3ac1c3a94 [file] [log] [blame]
David K. Bainbridgedf9df632016-07-07 18:47:46 -07001// Copyright 2016 Open Networking Laboratory
2//
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}