2021-11-04 09:54:04 +08:00
|
|
|
package udp
|
2021-03-19 09:37:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
type m map[string]interface{}
|
|
|
|
|
|
|
|
type Addr struct {
|
2021-03-19 09:37:24 +08:00
|
|
|
IP net.IP
|
|
|
|
Port uint16
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func NewAddr(ip net.IP, port uint16) *Addr {
|
|
|
|
addr := Addr{IP: make([]byte, net.IPv6len), Port: port}
|
2021-04-15 02:50:09 +08:00
|
|
|
copy(addr.IP, ip.To16())
|
2021-03-19 09:37:24 +08:00
|
|
|
return &addr
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func NewAddrFromString(s string) *Addr {
|
|
|
|
ip, port, err := ParseIPAndPort(s)
|
2021-03-19 09:37:24 +08:00
|
|
|
//TODO: handle err
|
|
|
|
_ = err
|
2021-11-04 09:54:04 +08:00
|
|
|
return &Addr{IP: ip.To16(), Port: port}
|
2021-03-19 09:37:24 +08:00
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func (ua *Addr) Equals(t *Addr) bool {
|
2021-03-19 09:37:24 +08:00
|
|
|
if t == nil || ua == nil {
|
|
|
|
return t == nil && ua == nil
|
|
|
|
}
|
|
|
|
return ua.IP.Equal(t.IP) && ua.Port == t.Port
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func (ua *Addr) String() string {
|
2021-04-27 09:21:47 +08:00
|
|
|
if ua == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:37:24 +08:00
|
|
|
return net.JoinHostPort(ua.IP.String(), fmt.Sprintf("%v", ua.Port))
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func (ua *Addr) MarshalJSON() ([]byte, error) {
|
2021-04-27 09:21:47 +08:00
|
|
|
if ua == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:37:24 +08:00
|
|
|
return json.Marshal(m{"ip": ua.IP, "port": ua.Port})
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func (ua *Addr) Copy() *Addr {
|
2021-04-27 09:21:47 +08:00
|
|
|
if ua == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
nu := Addr{
|
2021-03-19 09:37:24 +08:00
|
|
|
Port: ua.Port,
|
|
|
|
IP: make(net.IP, len(ua.IP)),
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(nu.IP, ua.IP)
|
|
|
|
return &nu
|
|
|
|
}
|
|
|
|
|
2023-03-14 03:09:08 +08:00
|
|
|
type AddrSlice []*Addr
|
|
|
|
|
|
|
|
func (a AddrSlice) Equal(b AddrSlice) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range a {
|
|
|
|
if !a[i].Equals(b[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func ParseIPAndPort(s string) (net.IP, uint16, error) {
|
2021-03-19 09:37:24 +08:00
|
|
|
rIp, sPort, err := net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:48:41 +08:00
|
|
|
addr, err := net.ResolveIPAddr("ip", rIp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:37:24 +08:00
|
|
|
iPort, err := strconv.Atoi(sPort)
|
2021-04-02 10:48:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.IP, uint16(iPort), nil
|
2021-03-19 09:37:24 +08:00
|
|
|
}
|