blob: 29e35000ce57f158765a5c5ccdb922148630e633 [file] [log] [blame]
David K. Bainbridge8bc905c2016-05-31 14:07:10 -07001package main
2
3import (
4 "fmt"
5 "strconv"
6 "strings"
7)
8
9type IPv4 uint32
10
11func 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
21func (ip IPv4) Next() (IPv4, error) {
22 return ip + 1, nil
23}
24
25func (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}