2015-09-13 14:01:50 -04:00
|
|
|
package net
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2015-09-20 10:55:45 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-10 18:24:18 -04:00
|
|
|
)
|
|
|
|
|
2015-09-21 06:15:25 -04:00
|
|
|
// Address represents a network address to be communicated with. It may be an IP address or domain
|
|
|
|
// address, not both. This interface doesn't resolve IP address for a given domain.
|
2015-09-20 10:55:45 -04:00
|
|
|
type Address interface {
|
2015-12-02 06:47:54 -05:00
|
|
|
IP() net.IP // IP of this Address
|
|
|
|
Domain() string // Domain of this Address
|
|
|
|
Port() Port // Port of this Address
|
2015-09-20 10:55:45 -04:00
|
|
|
|
2015-09-21 06:15:25 -04:00
|
|
|
IsIPv4() bool // True if this Address is an IPv4 address
|
|
|
|
IsIPv6() bool // True if this Address is an IPv6 address
|
2015-09-21 05:51:18 -04:00
|
|
|
IsDomain() bool // True if this Address is an domain address
|
2015-09-20 10:55:45 -04:00
|
|
|
|
2015-09-21 05:51:18 -04:00
|
|
|
String() string // String representation of this Address
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-14 02:43:04 -04:00
|
|
|
func allZeros(data []byte) bool {
|
|
|
|
for _, v := range data {
|
|
|
|
if v != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-21 05:51:18 -04:00
|
|
|
// IPAddress creates an Address with given IP and port.
|
2015-09-12 16:11:54 -04:00
|
|
|
func IPAddress(ip []byte, port uint16) Address {
|
2015-09-20 10:55:45 -04:00
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv4len:
|
2015-12-02 06:47:54 -05:00
|
|
|
return &IPv4Address{
|
|
|
|
port: Port(port),
|
|
|
|
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
case net.IPv6len:
|
2015-10-14 02:43:04 -04:00
|
|
|
if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
|
|
|
|
return IPAddress(ip[12:16], port)
|
|
|
|
}
|
2015-12-02 06:47:54 -05:00
|
|
|
return &IPv6Address{
|
|
|
|
port: Port(port),
|
2015-09-21 06:15:25 -04:00
|
|
|
ip: [16]byte{
|
|
|
|
ip[0], ip[1], ip[2], ip[3],
|
|
|
|
ip[4], ip[5], ip[6], ip[7],
|
|
|
|
ip[8], ip[9], ip[10], ip[11],
|
|
|
|
ip[12], ip[13], ip[14], ip[15],
|
|
|
|
},
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
default:
|
2015-10-13 07:55:06 -04:00
|
|
|
log.Error("Invalid IP format: %v", ip)
|
2015-09-24 06:54:10 -04:00
|
|
|
return nil
|
2015-09-16 11:07:05 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-21 05:51:18 -04:00
|
|
|
// DomainAddress creates an Address with given domain and port.
|
2015-09-12 16:11:54 -04:00
|
|
|
func DomainAddress(domain string, port uint16) Address {
|
2015-12-02 06:47:54 -05:00
|
|
|
return &DomainAddressImpl{
|
|
|
|
domain: domain,
|
|
|
|
port: Port(port),
|
2015-09-16 11:07:05 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 10:55:45 -04:00
|
|
|
type IPv4Address struct {
|
2015-12-02 06:47:54 -05:00
|
|
|
port Port
|
|
|
|
ip [4]byte
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
2015-09-11 08:12:26 -04:00
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv4Address) IP() net.IP {
|
2015-09-20 10:55:45 -04:00
|
|
|
return net.IP(addr.ip[:])
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this *IPv4Address) Port() Port {
|
|
|
|
return this.port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (addr *IPv4Address) Domain() string {
|
2015-09-20 10:55:45 -04:00
|
|
|
panic("Calling Domain() on an IPv4Address.")
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv4Address) IsIPv4() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv4Address) IsIPv6() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv4Address) IsDomain() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this *IPv4Address) String() string {
|
|
|
|
return this.IP().String() + ":" + this.port.String()
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type IPv6Address struct {
|
2015-12-02 06:47:54 -05:00
|
|
|
port Port
|
|
|
|
ip [16]byte
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv6Address) IP() net.IP {
|
2015-09-20 10:55:45 -04:00
|
|
|
return net.IP(addr.ip[:])
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this *IPv6Address) Port() Port {
|
|
|
|
return this.port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (addr *IPv6Address) Domain() string {
|
2015-09-20 10:55:45 -04:00
|
|
|
panic("Calling Domain() on an IPv6Address.")
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv6Address) IsIPv4() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv6Address) IsIPv6() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *IPv6Address) IsDomain() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this *IPv6Address) String() string {
|
|
|
|
return "[" + this.IP().String() + "]:" + this.port.String()
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type DomainAddressImpl struct {
|
2015-12-02 06:47:54 -05:00
|
|
|
port Port
|
2015-09-20 10:55:45 -04:00
|
|
|
domain string
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *DomainAddressImpl) IP() net.IP {
|
2015-09-20 10:55:45 -04:00
|
|
|
panic("Calling IP() on a DomainAddress.")
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this *DomainAddressImpl) Port() Port {
|
|
|
|
return this.port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (addr *DomainAddressImpl) Domain() string {
|
2015-09-20 10:55:45 -04:00
|
|
|
return addr.domain
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *DomainAddressImpl) IsIPv4() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *DomainAddressImpl) IsIPv6() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (addr *DomainAddressImpl) IsDomain() bool {
|
2015-09-20 10:55:45 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this *DomainAddressImpl) String() string {
|
|
|
|
return this.domain + ":" + this.port.String()
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|