| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | package net | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 17:08:01 +02:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"net" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 10:15:25 +00:00
										 |  |  | // Destination represents a network destination including address and protocol (tcp / udp). | 
					
						
							| 
									
										
										
										
											2016-09-20 11:53:05 +02:00
										 |  |  | type Destination struct { | 
					
						
							|  |  |  | 	Network Network | 
					
						
							|  |  |  | 	Port    Port | 
					
						
							| 
									
										
										
										
											2016-12-21 15:37:16 +01:00
										 |  |  | 	Address Address | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 11:25:05 +01:00
										 |  |  | // DestinationFromAddr generates a Destination from a net address. | 
					
						
							| 
									
										
										
										
											2016-08-14 23:20:23 +02:00
										 |  |  | func DestinationFromAddr(addr net.Addr) Destination { | 
					
						
							|  |  |  | 	switch addr := addr.(type) { | 
					
						
							|  |  |  | 	case *net.TCPAddr: | 
					
						
							|  |  |  | 		return TCPDestination(IPAddress(addr.IP), Port(addr.Port)) | 
					
						
							|  |  |  | 	case *net.UDPAddr: | 
					
						
							|  |  |  | 		return UDPDestination(IPAddress(addr.IP), Port(addr.Port)) | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2017-02-08 10:01:22 +01:00
										 |  |  | 		panic("Net: Unknown address type.") | 
					
						
							| 
									
										
										
										
											2016-08-14 23:20:23 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-14 17:08:01 +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 { | 
					
						
							| 
									
										
										
										
											2016-09-20 11:53:05 +02:00
										 |  |  | 	return Destination{ | 
					
						
							|  |  |  | 		Network: Network_TCP, | 
					
						
							|  |  |  | 		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 { | 
					
						
							| 
									
										
										
										
											2016-09-20 11:53:05 +02:00
										 |  |  | 	return Destination{ | 
					
						
							|  |  |  | 		Network: Network_UDP, | 
					
						
							|  |  |  | 		Address: address, | 
					
						
							|  |  |  | 		Port:    port, | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-20 16:03:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 22:42:16 +01:00
										 |  |  | // NetAddr returns the network address in this Destination in string form. | 
					
						
							| 
									
										
										
										
											2017-08-22 15:15:09 +02:00
										 |  |  | func (d Destination) NetAddr() string { | 
					
						
							|  |  |  | 	return d.Address.String() + ":" + d.Port.String() | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 22:42:16 +01:00
										 |  |  | // String returns the strings form of this Destination. | 
					
						
							| 
									
										
										
										
											2017-08-22 15:15:09 +02:00
										 |  |  | func (d Destination) String() string { | 
					
						
							|  |  |  | 	return d.Network.URLPrefix() + ":" + d.NetAddr() | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 22:42:16 +01:00
										 |  |  | // IsValid returns true if this Destination is valid. | 
					
						
							| 
									
										
										
										
											2017-08-22 15:15:09 +02:00
										 |  |  | func (d Destination) IsValid() bool { | 
					
						
							|  |  |  | 	return d.Network != Network_Unknown | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 11:25:05 +01:00
										 |  |  | // AsDestination converts current Enpoint into Destination. | 
					
						
							| 
									
										
										
										
											2017-08-22 15:15:09 +02:00
										 |  |  | func (p *Endpoint) AsDestination() Destination { | 
					
						
							| 
									
										
										
										
											2016-09-20 16:05:35 +02:00
										 |  |  | 	return Destination{ | 
					
						
							| 
									
										
										
										
											2017-08-22 15:15:09 +02:00
										 |  |  | 		Network: p.Network, | 
					
						
							|  |  |  | 		Address: p.Address.AsAddress(), | 
					
						
							|  |  |  | 		Port:    Port(p.Port), | 
					
						
							| 
									
										
										
										
											2016-09-20 16:05:35 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |