| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | package net | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 10:15:25 +00:00
										 |  |  | // Destination represents a network destination including address and protocol (tcp / udp). | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | type Destination interface { | 
					
						
							| 
									
										
										
										
											2016-02-06 18:03:42 +01:00
										 |  |  | 	Network() Network // Protocol of communication (tcp / udp) | 
					
						
							| 
									
										
										
										
											2015-09-21 10:15:25 +00:00
										 |  |  | 	Address() Address // Address of destination | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | 	Port() Port | 
					
						
							|  |  |  | 	String() string // String representation of the destination | 
					
						
							|  |  |  | 	NetAddr() string | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | 	Equals(Destination) bool | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 10:15:25 +00:00
										 |  |  | 	IsTCP() bool // True if destination is reachable via TCP | 
					
						
							|  |  |  | 	IsUDP() bool // True if destination is reachable via UDP | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | // TCPDestination creates a TCP destination with given address | 
					
						
							|  |  |  | func TCPDestination(address Address, port Port) Destination { | 
					
						
							|  |  |  | 	return &tcpDestination{address: address, port: port} | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | // UDPDestination creates a UDP destination with given address | 
					
						
							|  |  |  | func UDPDestination(address Address, port Port) Destination { | 
					
						
							|  |  |  | 	return &udpDestination{address: address, port: port} | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | type tcpDestination struct { | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | 	address Address | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | 	port    Port | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 18:03:42 +01:00
										 |  |  | func (dest *tcpDestination) Network() Network { | 
					
						
							|  |  |  | 	return TCPNetwork | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *tcpDestination) Address() Address { | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	return dest.address | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *tcpDestination) NetAddr() string { | 
					
						
							|  |  |  | 	return dest.address.String() + ":" + dest.port.String() | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *tcpDestination) String() string { | 
					
						
							|  |  |  | 	return "tcp:" + dest.NetAddr() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dest *tcpDestination) IsTCP() bool { | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	return true | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *tcpDestination) IsUDP() bool { | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	return false | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *tcpDestination) Port() Port { | 
					
						
							|  |  |  | 	return dest.port | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | func (dest *tcpDestination) Equals(another Destination) bool { | 
					
						
							| 
									
										
										
										
											2016-01-21 12:05:28 +00:00
										 |  |  | 	if dest == nil && another == nil { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if dest == nil || another == nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | 	if !another.IsTCP() { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return dest.Port() == another.Port() && dest.Address().Equals(another.Address()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | type udpDestination struct { | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	address Address | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | 	port    Port | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 18:03:42 +01:00
										 |  |  | func (dest *udpDestination) Network() Network { | 
					
						
							|  |  |  | 	return UDPNetwork | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *udpDestination) Address() Address { | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | 	return dest.address | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *udpDestination) NetAddr() string { | 
					
						
							|  |  |  | 	return dest.address.String() + ":" + dest.port.String() | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *udpDestination) String() string { | 
					
						
							|  |  |  | 	return "udp:" + dest.NetAddr() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dest *udpDestination) IsTCP() bool { | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | func (dest *udpDestination) IsUDP() bool { | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	return true | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (dest *udpDestination) Port() Port { | 
					
						
							|  |  |  | 	return dest.port | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (dest *udpDestination) Equals(another Destination) bool { | 
					
						
							| 
									
										
										
										
											2016-01-21 12:05:28 +00:00
										 |  |  | 	if dest == nil && another == nil { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if dest == nil || another == nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | 	if !another.IsUDP() { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return dest.Port() == another.Port() && dest.Address().Equals(another.Address()) | 
					
						
							|  |  |  | } |