Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
78963f4f
Commit
78963f4f
authored
Aug 25, 2011
by
Mikio Hara
Committed by
Russ Cox
Aug 25, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net: make use of IPv4len, IPv6len
R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/4957043
parent
0462f77e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
18 deletions
+18
-18
ip.go
src/pkg/net/ip.go
+18
-18
No files found.
src/pkg/net/ip.go
View file @
78963f4f
...
@@ -214,10 +214,10 @@ func allFF(b []byte) bool {
...
@@ -214,10 +214,10 @@ func allFF(b []byte) bool {
// Mask returns the result of masking the IP address ip with mask.
// Mask returns the result of masking the IP address ip with mask.
func
(
ip
IP
)
Mask
(
mask
IPMask
)
IP
{
func
(
ip
IP
)
Mask
(
mask
IPMask
)
IP
{
n
:=
len
(
ip
)
n
:=
len
(
ip
)
if
len
(
mask
)
==
16
&&
len
(
ip
)
==
4
&&
allFF
(
mask
[
:
12
])
{
if
len
(
mask
)
==
IPv6len
&&
len
(
ip
)
==
IPv4len
&&
allFF
(
mask
[
:
12
])
{
mask
=
mask
[
12
:
]
mask
=
mask
[
12
:
]
}
}
if
len
(
mask
)
==
4
&&
len
(
ip
)
==
16
&&
bytesEqual
(
ip
[
:
12
],
v4InV6Prefix
)
{
if
len
(
mask
)
==
IPv4len
&&
len
(
ip
)
==
IPv6len
&&
bytesEqual
(
ip
[
:
12
],
v4InV6Prefix
)
{
ip
=
ip
[
12
:
]
ip
=
ip
[
12
:
]
}
}
if
n
!=
len
(
mask
)
{
if
n
!=
len
(
mask
)
{
...
@@ -276,7 +276,7 @@ func (ip IP) String() string {
...
@@ -276,7 +276,7 @@ func (ip IP) String() string {
}
}
// If IPv4, use dotted notation.
// If IPv4, use dotted notation.
if
p4
:=
p
.
To4
();
len
(
p4
)
==
4
{
if
p4
:=
p
.
To4
();
len
(
p4
)
==
IPv4len
{
return
itod
(
uint
(
p4
[
0
]))
+
"."
+
return
itod
(
uint
(
p4
[
0
]))
+
"."
+
itod
(
uint
(
p4
[
1
]))
+
"."
+
itod
(
uint
(
p4
[
1
]))
+
"."
+
itod
(
uint
(
p4
[
2
]))
+
"."
+
itod
(
uint
(
p4
[
2
]))
+
"."
+
...
@@ -289,9 +289,9 @@ func (ip IP) String() string {
...
@@ -289,9 +289,9 @@ func (ip IP) String() string {
// Find longest run of zeros.
// Find longest run of zeros.
e0
:=
-
1
e0
:=
-
1
e1
:=
-
1
e1
:=
-
1
for
i
:=
0
;
i
<
16
;
i
+=
2
{
for
i
:=
0
;
i
<
IPv6len
;
i
+=
2
{
j
:=
i
j
:=
i
for
j
<
16
&&
p
[
j
]
==
0
&&
p
[
j
+
1
]
==
0
{
for
j
<
IPv6len
&&
p
[
j
]
==
0
&&
p
[
j
+
1
]
==
0
{
j
+=
2
j
+=
2
}
}
if
j
>
i
&&
j
-
i
>
e1
-
e0
{
if
j
>
i
&&
j
-
i
>
e1
-
e0
{
...
@@ -307,11 +307,11 @@ func (ip IP) String() string {
...
@@ -307,11 +307,11 @@ func (ip IP) String() string {
// Print with possible :: in place of run of zeros
// Print with possible :: in place of run of zeros
var
s
string
var
s
string
for
i
:=
0
;
i
<
16
;
i
+=
2
{
for
i
:=
0
;
i
<
IPv6len
;
i
+=
2
{
if
i
==
e0
{
if
i
==
e0
{
s
+=
"::"
s
+=
"::"
i
=
e1
i
=
e1
if
i
>=
16
{
if
i
>=
IPv6len
{
break
break
}
}
}
else
if
i
>
0
{
}
else
if
i
>
0
{
...
@@ -329,10 +329,10 @@ func (ip IP) Equal(x IP) bool {
...
@@ -329,10 +329,10 @@ func (ip IP) Equal(x IP) bool {
if
len
(
ip
)
==
len
(
x
)
{
if
len
(
ip
)
==
len
(
x
)
{
return
bytesEqual
(
ip
,
x
)
return
bytesEqual
(
ip
,
x
)
}
}
if
len
(
ip
)
==
4
&&
len
(
x
)
==
16
{
if
len
(
ip
)
==
IPv4len
&&
len
(
x
)
==
IPv6len
{
return
bytesEqual
(
x
[
0
:
12
],
v4InV6Prefix
)
&&
bytesEqual
(
ip
,
x
[
12
:
])
return
bytesEqual
(
x
[
0
:
12
],
v4InV6Prefix
)
&&
bytesEqual
(
ip
,
x
[
12
:
])
}
}
if
len
(
ip
)
==
16
&&
len
(
x
)
==
4
{
if
len
(
ip
)
==
IPv6len
&&
len
(
x
)
==
IPv4len
{
return
bytesEqual
(
ip
[
0
:
12
],
v4InV6Prefix
)
&&
bytesEqual
(
ip
[
12
:
],
x
)
return
bytesEqual
(
ip
[
0
:
12
],
v4InV6Prefix
)
&&
bytesEqual
(
ip
[
12
:
],
x
)
}
}
return
false
return
false
...
@@ -386,12 +386,12 @@ func simpleMaskLength(mask IPMask) int {
...
@@ -386,12 +386,12 @@ func simpleMaskLength(mask IPMask) int {
// as an IP address.
// as an IP address.
func
(
mask
IPMask
)
String
()
string
{
func
(
mask
IPMask
)
String
()
string
{
switch
len
(
mask
)
{
switch
len
(
mask
)
{
case
4
:
case
IPv4len
:
n
:=
simpleMaskLength
(
mask
)
n
:=
simpleMaskLength
(
mask
)
if
n
>=
0
{
if
n
>=
0
{
return
itod
(
uint
(
n
+
(
IPv6len
-
IPv4len
)
*
8
))
return
itod
(
uint
(
n
+
(
IPv6len
-
IPv4len
)
*
8
))
}
}
case
16
:
case
IPv6len
:
n
:=
simpleMaskLength
(
mask
)
n
:=
simpleMaskLength
(
mask
)
if
n
>=
12
*
8
{
if
n
>=
12
*
8
{
return
itod
(
uint
(
n
-
12
*
8
))
return
itod
(
uint
(
n
-
12
*
8
))
...
@@ -440,7 +440,7 @@ func parseIPv4(s string) IP {
...
@@ -440,7 +440,7 @@ func parseIPv4(s string) IP {
// * The last 32 bits can be in IPv4 form.
// * The last 32 bits can be in IPv4 form.
// Thus, ::ffff:1.2.3.4 is the IPv4 address 1.2.3.4.
// Thus, ::ffff:1.2.3.4 is the IPv4 address 1.2.3.4.
func
parseIPv6
(
s
string
)
IP
{
func
parseIPv6
(
s
string
)
IP
{
p
:=
make
(
IP
,
16
)
p
:=
make
(
IP
,
IPv6len
)
ellipsis
:=
-
1
// position of ellipsis in p
ellipsis
:=
-
1
// position of ellipsis in p
i
:=
0
// index in string s
i
:=
0
// index in string s
...
@@ -482,7 +482,7 @@ func parseIPv6(s string) IP {
...
@@ -482,7 +482,7 @@ func parseIPv6(s string) IP {
p
[
j
+
2
]
=
p4
[
14
]
p
[
j
+
2
]
=
p4
[
14
]
p
[
j
+
3
]
=
p4
[
15
]
p
[
j
+
3
]
=
p4
[
15
]
i
=
len
(
s
)
i
=
len
(
s
)
j
+=
4
j
+=
IPv4len
break
break
}
}
...
@@ -577,10 +577,10 @@ func ParseCIDR(s string) (ip IP, mask IPMask, err os.Error) {
...
@@ -577,10 +577,10 @@ func ParseCIDR(s string) (ip IP, mask IPMask, err os.Error) {
return
nil
,
nil
,
&
ParseError
{
"CIDR address"
,
s
}
return
nil
,
nil
,
&
ParseError
{
"CIDR address"
,
s
}
}
}
ipstr
,
maskstr
:=
s
[
:
i
],
s
[
i
+
1
:
]
ipstr
,
maskstr
:=
s
[
:
i
],
s
[
i
+
1
:
]
iplen
:=
4
iplen
:=
IPv4len
ip
=
parseIPv4
(
ipstr
)
ip
=
parseIPv4
(
ipstr
)
if
ip
==
nil
{
if
ip
==
nil
{
iplen
=
16
iplen
=
IPv6len
ip
=
parseIPv6
(
ipstr
)
ip
=
parseIPv6
(
ipstr
)
}
}
nn
,
i
,
ok
:=
dtoi
(
maskstr
,
0
)
nn
,
i
,
ok
:=
dtoi
(
maskstr
,
0
)
...
@@ -588,12 +588,12 @@ func ParseCIDR(s string) (ip IP, mask IPMask, err os.Error) {
...
@@ -588,12 +588,12 @@ func ParseCIDR(s string) (ip IP, mask IPMask, err os.Error) {
return
nil
,
nil
,
&
ParseError
{
"CIDR address"
,
s
}
return
nil
,
nil
,
&
ParseError
{
"CIDR address"
,
s
}
}
}
n
:=
uint
(
nn
)
n
:=
uint
(
nn
)
if
iplen
==
4
{
if
iplen
==
IPv4len
{
v4mask
:=
^
uint32
(
0xffffffff
>>
n
)
v4mask
:=
^
uint32
(
0xffffffff
>>
n
)
mask
=
IPv4Mask
(
byte
(
v4mask
>>
24
),
byte
(
v4mask
>>
16
),
byte
(
v4mask
>>
8
),
byte
(
v4mask
))
mask
=
IPv4Mask
(
byte
(
v4mask
>>
24
),
byte
(
v4mask
>>
16
),
byte
(
v4mask
>>
8
),
byte
(
v4mask
))
}
else
{
}
else
{
mask
=
make
(
IPMask
,
16
)
mask
=
make
(
IPMask
,
IPv6len
)
for
i
:=
0
;
i
<
16
;
i
++
{
for
i
:=
0
;
i
<
IPv6len
;
i
++
{
if
n
>=
8
{
if
n
>=
8
{
mask
[
i
]
=
0xff
mask
[
i
]
=
0xff
n
-=
8
n
-=
8
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment