Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-sockaddr
Commits
09ae6064
Unverified
Commit
09ae6064
authored
Jan 28, 2018
by
Steven Allen
Committed by
GitHub
Jan 28, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #10 from libp2p/fix/4byte-ip
don't panic on 4byte IP addresses
parents
7f35106c
aabd5dc7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
9 deletions
+50
-9
net/net.go
net/net.go
+16
-9
net/net_test.go
net/net_test.go
+34
-0
No files found.
net/net.go
View file @
09ae6064
...
...
@@ -115,22 +115,29 @@ func NetAddrToSockaddr(addr net.Addr) Sockaddr {
// IPAndZoneToSockaddr converts a net.IP (with optional IPv6 Zone) to a Sockaddr
// Returns nil if conversion fails.
func
IPAndZoneToSockaddr
(
ip
net
.
IP
,
zone
string
)
Sockaddr
{
switch
{
case
len
(
ip
)
<
net
.
IPv4len
:
// default to IPv4
buf
:=
[
4
]
byte
{
0
,
0
,
0
,
0
}
return
&
SockaddrInet4
{
Addr
:
buf
}
// Unspecified?
if
ip
==
nil
{
if
zone
!=
""
{
return
&
SockaddrInet6
{
ZoneId
:
uint32
(
IP6ZoneToInt
(
zone
))}
}
return
new
(
SockaddrInet4
)
}
case
ip
.
To4
()
!=
nil
:
// Valid IPv4?
if
ip4
:=
ip
.
To4
();
ip4
!=
nil
&&
zone
==
""
{
var
buf
[
4
]
byte
copy
(
buf
[
:
],
ip
[
12
:
16
]
)
// last 4 bytes
copy
(
buf
[
:
],
ip
4
)
// last 4 bytes
return
&
SockaddrInet4
{
Addr
:
buf
}
}
case
ip
.
To16
()
!=
nil
:
// Valid IPv6 address?
if
ip6
:=
ip
.
To16
();
ip6
!=
nil
{
var
buf
[
16
]
byte
copy
(
buf
[
:
],
ip
)
copy
(
buf
[
:
],
ip
6
)
return
&
SockaddrInet6
{
Addr
:
buf
,
ZoneId
:
uint32
(
IP6ZoneToInt
(
zone
))}
}
panic
(
"should be unreachable"
)
return
nil
}
// IPAddrToSockaddr converts a net.IPAddr to a Sockaddr.
...
...
net/net_test.go
0 → 100644
View file @
09ae6064
package
sockaddrnet
import
(
"bytes"
"net"
"testing"
)
func
assertIPEq
(
t
*
testing
.
T
,
ip
net
.
IP
,
sa
Sockaddr
)
{
switch
s
:=
sa
.
(
type
)
{
case
*
SockaddrInet4
:
if
!
bytes
.
Equal
(
s
.
Addr
[
:
],
ip
.
To4
())
{
t
.
Error
(
"IPs not equal"
)
}
case
*
SockaddrInet6
:
if
!
bytes
.
Equal
(
s
.
Addr
[
:
],
ip
.
To16
())
{
t
.
Error
(
"IPs not equal"
)
}
default
:
t
.
Error
(
"not a known sockaddr"
)
}
}
func
subtestIPSockaddr
(
t
*
testing
.
T
,
ip
net
.
IP
)
{
assertIPEq
(
t
,
ip
,
IPAndZoneToSockaddr
(
ip
,
""
))
}
func
TestIPAndZoneToSockaddr
(
t
*
testing
.
T
)
{
subtestIPSockaddr
(
t
,
net
.
ParseIP
(
"127.0.0.1"
))
subtestIPSockaddr
(
t
,
net
.
IPv4zero
)
subtestIPSockaddr
(
t
,
net
.
IP
(
net
.
IPv4zero
.
To4
()))
subtestIPSockaddr
(
t
,
net
.
IPv6unspecified
)
assertIPEq
(
t
,
net
.
IPv4zero
,
IPAndZoneToSockaddr
(
nil
,
""
))
}
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