forked from 42wim/mm-go-irckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.go
46 lines (36 loc) · 775 Bytes
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package irckit
import (
"net"
"strings"
"github.com/sorcix/irc"
)
// Conn abstracts the encoding/decoding and sending/receiving when speaking IRC.
type Conn interface {
Close() error
Encode(*irc.Message) error
Decode() (*irc.Message, error)
// ResolveHost returns the resolved host of the RemoteAddr
ResolveHost() string
}
type conn struct {
net.Conn
*irc.Encoder
*irc.Decoder
}
// resolveHost will convert an IP to a Hostname, but fall back to IP on error.
func (c *conn) ResolveHost() string {
addr := c.RemoteAddr()
s := addr.String()
ip, _, err := net.SplitHostPort(s)
if err != nil {
return s
}
names, err := net.LookupAddr(ip)
if err != nil {
return ip
}
if len(names) == 0 {
return ip
}
return strings.TrimSuffix(names[0], ".")
}