File tree 1 file changed +7
-21
lines changed
1 file changed +7
-21
lines changed Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
- "bytes"
5
4
"encoding/binary"
6
5
"net"
7
6
)
@@ -11,28 +10,15 @@ func ip2long(ip net.IP) uint32 {
11
10
return binary .BigEndian .Uint32 (ip .To4 ()[0 :4 ])
12
11
}
13
12
14
- func long2ip (ipIntLong uint32 ) net.IP {
15
- ipInt := int64 (ipIntLong )
16
- b0 := byte (ipInt >> 24 & 0xff )
17
- b1 := byte (ipInt >> 16 & 0xff )
18
- b2 := byte (ipInt >> 8 & 0xff )
19
- b3 := byte (ipInt & 0xff )
20
- return net.IP {b0 , b1 , b2 , b3 }
13
+ func long2ip (data uint32 ) net.IP {
14
+ b := long2bytes (data )
15
+ return net.IP {b [0 ], b [1 ], b [2 ], b [3 ]}
21
16
}
22
17
23
- // Quickly ripped from https://gist.github.com/chiro-hiro/2674626cebbcb5a676355b7aaac4972d
24
- func long2bytes (ip uint32 ) []byte {
25
- r := make ([]byte , 4 )
26
- for i := uint32 (0 ); i < 4 ; i ++ {
27
- r [3 - i ] = byte ((ip >> (8 * i )) & 0xff )
28
- }
29
- return r
30
- }
31
-
32
- func bytes2long (ip []byte ) uint32 {
33
- var long uint32
34
- binary .Read (bytes .NewBuffer (ip ), binary .LittleEndian , & long )
35
- return long
18
+ func long2bytes (data uint32 ) []byte {
19
+ b := make ([]byte , 4 )
20
+ binary .BigEndian .PutUint32 (b , data )
21
+ return b
36
22
}
37
23
38
24
func calcBroadcast (network , netmask net.IP ) net.IP {
You can’t perform that action at this time.
0 commit comments