David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type IPv4 uint32 |
| 10 | |
| 11 | func ParseIP(dot string) (IPv4, error) { |
| 12 | var ip IPv4 = 0 |
| 13 | o := strings.Split(dot, ".") |
| 14 | for i := 0; i < 4; i += 1 { |
| 15 | b, _ := strconv.Atoi(o[i]) |
| 16 | ip = ip | (IPv4(byte(b)) << (uint(3-i) * 8)) |
| 17 | } |
| 18 | return ip, nil |
| 19 | } |
| 20 | |
| 21 | func (ip IPv4) Next() (IPv4, error) { |
| 22 | return ip + 1, nil |
| 23 | } |
| 24 | |
| 25 | func (ip IPv4) String() string { |
| 26 | b := []byte{0, 0, 0, 0} |
| 27 | for i := 0; i < 4; i += 1 { |
| 28 | m := IPv4(255) << uint((3-i)*8) |
| 29 | b[i] = byte(((ip & m) >> uint((3-i)*8))) |
| 30 | } |
| 31 | return fmt.Sprintf("%d.%d.%d.%d", b[0], b[1], b[2], b[3]) |
| 32 | } |