Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
0b986316
Commit
0b986316
authored
Mar 16, 2010
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net: fix IPMask.String not to crash on all-0xff mask
R=r CC=golang-dev
https://golang.org/cl/438042
parent
7f4c2cae
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
21 deletions
+38
-21
src/pkg/net/ip.go
src/pkg/net/ip.go
+38
-21
No files found.
src/pkg/net/ip.go
View file @
0b986316
...
@@ -51,6 +51,20 @@ func IPv4(a, b, c, d byte) IP {
...
@@ -51,6 +51,20 @@ func IPv4(a, b, c, d byte) IP {
return
p
return
p
}
}
// IPv4Mask returns the IP mask (in 16-byte form) of the
// IPv4 mask a.b.c.d.
func
IPv4Mask
(
a
,
b
,
c
,
d
byte
)
IPMask
{
p
:=
make
(
IPMask
,
IPv6len
)
for
i
:=
0
;
i
<
12
;
i
++
{
p
[
i
]
=
0xff
}
p
[
12
]
=
a
p
[
13
]
=
b
p
[
14
]
=
c
p
[
15
]
=
d
return
p
}
// Well-known IPv4 addresses
// Well-known IPv4 addresses
var
(
var
(
IPv4bcast
=
IPv4
(
255
,
255
,
255
,
255
)
// broadcast
IPv4bcast
=
IPv4
(
255
,
255
,
255
,
255
)
// broadcast
...
@@ -103,9 +117,9 @@ func (ip IP) To16() IP {
...
@@ -103,9 +117,9 @@ func (ip IP) To16() IP {
// Default route masks for IPv4.
// Default route masks for IPv4.
var
(
var
(
classAMask
=
IP
Mask
(
IPv4
(
0xff
,
0
,
0
,
0
)
)
classAMask
=
IP
v4Mask
(
0xff
,
0
,
0
,
0
)
classBMask
=
IP
Mask
(
IPv4
(
0xff
,
0xff
,
0
,
0
)
)
classBMask
=
IP
v4Mask
(
0xff
,
0xff
,
0
,
0
)
classCMask
=
IP
Mask
(
IPv4
(
0xff
,
0xff
,
0xff
,
0
)
)
classCMask
=
IP
v4Mask
(
0xff
,
0xff
,
0xff
,
0
)
)
)
// DefaultMask returns the default IP mask for the IP address ip.
// DefaultMask returns the default IP mask for the IP address ip.
...
@@ -229,18 +243,19 @@ func (ip IP) String() string {
...
@@ -229,18 +243,19 @@ func (ip IP) String() string {
// If mask is a sequence of 1 bits followed by 0 bits,
// If mask is a sequence of 1 bits followed by 0 bits,
// return the number of 1 bits.
// return the number of 1 bits.
func
simpleMaskLength
(
mask
IPMask
)
int
{
func
simpleMaskLength
(
mask
IPMask
)
int
{
var
i
int
var
n
int
for
i
=
0
;
i
<
len
(
mask
);
i
++
{
for
i
,
v
:=
range
mask
{
if
mask
[
i
]
!=
0xFF
{
if
v
==
0xff
{
break
n
+=
8
}
continue
}
}
n
:=
8
*
i
// found non-ff byte
v
:=
mask
[
i
]
// count 1 bits
for
v
&
0x80
!=
0
{
for
v
&
0x80
!=
0
{
n
++
n
++
v
<<=
1
v
<<=
1
}
}
// rest must be 0 bits
if
v
!=
0
{
if
v
!=
0
{
return
-
1
return
-
1
}
}
...
@@ -249,6 +264,8 @@ func simpleMaskLength(mask IPMask) int {
...
@@ -249,6 +264,8 @@ func simpleMaskLength(mask IPMask) int {
return
-
1
return
-
1
}
}
}
}
break
}
return
n
return
n
}
}
...
@@ -266,8 +283,8 @@ func (mask IPMask) String() string {
...
@@ -266,8 +283,8 @@ func (mask IPMask) String() string {
}
}
case
16
:
case
16
:
n
:=
simpleMaskLength
(
mask
)
n
:=
simpleMaskLength
(
mask
)
if
n
>=
0
{
if
n
>=
12
*
8
{
return
itod
(
uint
(
n
))
return
itod
(
uint
(
n
-
12
*
8
))
}
}
}
}
return
IP
(
mask
)
.
String
()
return
IP
(
mask
)
.
String
()
...
...
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