Skip to content

Commit

Permalink
Optimization: structure the code of converting unix.Sockaddr to net.IP
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 11, 2019
1 parent 68117d3 commit ec7bd36
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
17 changes: 1 addition & 16 deletions eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,10 @@ func (lp *loop) loopUDPIn(fd int) error {
if err != nil || n == 0 {
return nil
}
var sa6 unix.SockaddrInet6
switch sa := sa.(type) {
case *unix.SockaddrInet4:
sa6.ZoneId = 0
sa6.Port = sa.Port
for i := 0; i < 12; i++ {
sa6.Addr[i] = 0
}
sa6.Addr[12] = sa.Addr[0]
sa6.Addr[13] = sa.Addr[1]
sa6.Addr[14] = sa.Addr[2]
sa6.Addr[15] = sa.Addr[3]
case *unix.SockaddrInet6:
sa6 = *sa
}
c := &conn{
fd: fd,
localAddr: lp.svr.ln.lnaddr,
remoteAddr: netpoll.SockaddrToUDPAddr(&sa6),
remoteAddr: netpoll.SockaddrToUDPAddr(sa),
inboundBuffer: lp.svr.bytesPool.Get().(*ringbuffer.RingBuffer),
}
c.cache = lp.packet[:n]
Expand Down
2 changes: 2 additions & 0 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func (s *testServer) OnOpened(c Conn) (out []byte, action Action) {
if c.RemoteAddr() == nil {
panic("nil local addr")
}
//fmt.Printf("TCP from remote addr:%s to local addr: %s\n", c.RemoteAddr().String(), c.LocalAddr().String())
return
}
func (s *testServer) OnClosed(c Conn, err error) (action Action) {
Expand Down Expand Up @@ -405,6 +406,7 @@ func (s *testServer) React(c Conn) (out []byte, action Action) {
}
return
} else if s.network == "udp" {
//fmt.Printf("UDP from remote addr:%s to local addr: %s\n", c.RemoteAddr().String(), c.LocalAddr().String())
out = c.Read()
c.ResetBuffer()
return
Expand Down
49 changes: 25 additions & 24 deletions netpoll/socktoaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
// SockaddrToTCPOrUnixAddr converts a Sockaddr to a net.TCPAddr or net.UnixAddr.
// Returns nil if conversion fails.
func SockaddrToTCPOrUnixAddr(sa unix.Sockaddr) net.Addr {
ip, zone := SockaddrToIPAndZone(sa)
switch sa := sa.(type) {
case *unix.SockaddrInet4:
ip := sockaddrInet4ToIP(sa)
return &net.TCPAddr{IP: ip, Port: sa.Port}
case *unix.SockaddrInet6:
ip, zone := sockaddrInet6ToIPAndZone(sa)
return &net.TCPAddr{IP: ip, Port: sa.Port, Zone: zone}
case *unix.SockaddrUnix:
return &net.UnixAddr{Name: sa.Name, Net: "unix"}
Expand All @@ -28,19 +29,39 @@ func SockaddrToTCPOrUnixAddr(sa unix.Sockaddr) net.Addr {
// SockaddrToUDPAddr converts a Sockaddr to a net.UDPAddr
// Returns nil if conversion fails.
func SockaddrToUDPAddr(sa unix.Sockaddr) *net.UDPAddr {
ip, zone := SockaddrToIPAndZone(sa)
switch sa := sa.(type) {
case *unix.SockaddrInet4:
ip := sockaddrInet4ToIP(sa)
return &net.UDPAddr{IP: ip, Port: sa.Port}
case *unix.SockaddrInet6:
ip, zone := sockaddrInet6ToIPAndZone(sa)
return &net.UDPAddr{IP: ip, Port: sa.Port, Zone: zone}
}
return nil
}

// IP6ZoneToString converts an IP6 Zone unix int to a net string
// sockaddrInet4ToIPAndZone converts a SockaddrInet4 to a net.IP.
// It returns nil if conversion fails.
func sockaddrInet4ToIP(sa *unix.SockaddrInet4) net.IP {
ip := make([]byte, 16)
// V4InV6Prefix
ip[10] = 0xff
ip[11] = 0xff
copy(ip[12:16], sa.Addr[:])
return ip
}

// sockaddrInet6ToIPAndZone converts a SockaddrInet6 to a net.IP with IPv6 Zone.
// It returns nil if conversion fails.
func sockaddrInet6ToIPAndZone(sa *unix.SockaddrInet6) (net.IP, string) {
ip := make([]byte, 16)
copy(ip, sa.Addr[:])
return ip, ip6ZoneToString(int(sa.ZoneId))
}

// ip6ZoneToString converts an IP6 Zone unix int to a net string
// returns "" if zone is 0
func IP6ZoneToString(zone int) string {
func ip6ZoneToString(zone int) string {
if zone == 0 {
return ""
}
Expand All @@ -50,26 +71,6 @@ func IP6ZoneToString(zone int) string {
return int2decimal(uint(zone))
}

// SockaddrToIPAndZone converts a Sockaddr to a net.IP (with optional IPv6 Zone)
// Returns nil if conversion fails.
func SockaddrToIPAndZone(sa unix.Sockaddr) (net.IP, string) {
switch sa := sa.(type) {
case *unix.SockaddrInet4:
ip := make([]byte, 16)
// V4InV6Prefix
ip[10] = 0xff
ip[11] = 0xff
copy(ip[12:16], sa.Addr[:])
return ip, ""

case *unix.SockaddrInet6:
ip := make([]byte, 16)
copy(ip, sa.Addr[:])
return ip, IP6ZoneToString(int(sa.ZoneId))
}
return nil, ""
}

// Convert int to decimal string.
func int2decimal(i uint) string {
if i == 0 {
Expand Down

0 comments on commit ec7bd36

Please sign in to comment.