David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // 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. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "strconv" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
| 22 | type IPv4 uint32 |
| 23 | |
| 24 | func 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 | |
| 34 | func (ip IPv4) Next() (IPv4, error) { |
| 35 | return ip + 1, nil |
| 36 | } |
| 37 | |
| 38 | func (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 | } |