Просмотр исходного кода

Simplify default config for tun file descriptor

Rod Hynes 8 лет назад
Родитель
Сommit
3948a32cb1
4 измененных файлов с 26 добавлено и 28 удалено
  1. 17 18
      psiphon/common/tun/tun.go
  2. 0 1
      psiphon/common/tun/tun_test.go
  3. 5 5
      psiphon/config.go
  4. 4 4
      psiphon/controller.go

+ 17 - 18
psiphon/common/tun/tun.go

@@ -1164,20 +1164,19 @@ type ClientConfig struct {
 	// tunnel server.
 	Transport io.ReadWriteCloser
 
-	// TunFD specifies a file descriptor to use to read
-	// and write packets to be relayed to the client. When
-	// TuFD is specified, the Client will use this existing
-	// tun device and not create its own; in this case,
-	// network address and routing configuration is not
-	// performed by the Client. As the packet tunnel server
-	// performs transparent source IP address and DNS rewriting,
-	// the tun device may have any assigned IP address, but
-	// should be configured with the given MTU; and DNS should
-	// be configured to use the transparent DNS target
-	// resolver addresses.
-	// Set TunFD to -1 to ignore this parameter and create
-	// and configure a tun device.
-	TunFD int
+	// TunFileDescriptor specifies a file descriptor to use to
+	// read and write packets to be relayed to the client. When
+	// TunFileDescriptor is specified, the Client will use this
+	// existing tun device and not create its own; in this case,
+	// network address and routing configuration is not performed
+	// by the Client. As the packet tunnel server performs
+	// transparent source IP address and DNS rewriting, the tun
+	// device may have any assigned IP address, but should be
+	// configured with the given MTU; and DNS should be configured
+	// to use the transparent DNS target resolver addresses.
+	// Set TunFileDescriptor to <= 0 to ignore this parameter
+	// and create and configure a tun device.
+	TunFileDescriptor int
 
 	// IPv4AddressCIDR is the IPv4 address and netmask to
 	// assign to a newly created tun device.
@@ -1207,14 +1206,14 @@ type Client struct {
 }
 
 // NewClient initializes a new Client. Unless using the
-// TunFD configuration parameter, a new tun device is
-// created for the client.
+// TunFileDescriptor configuration parameter, a new tun
+// device is created for the client.
 func NewClient(config *ClientConfig) (*Client, error) {
 
 	var device *Device
 	var err error
 
-	if config.TunFD == -1 {
+	if config.TunFileDescriptor <= 0 {
 		device, err = NewClientDevice(config)
 	} else {
 		device, err = NewClientDeviceFromFD(config)
@@ -1990,7 +1989,7 @@ func newDevice(
 // NewClientDeviceFromFD wraps an existing tun device.
 func NewClientDeviceFromFD(config *ClientConfig) (*Device, error) {
 
-	dupFD, err := dupCloseOnExec(config.TunFD)
+	dupFD, err := dupCloseOnExec(config.TunFileDescriptor)
 	if err != nil {
 		return nil, common.ContextError(err)
 	}

+ 0 - 1
psiphon/common/tun/tun_test.go

@@ -395,7 +395,6 @@ func startTestClient(
 		IPv4AddressCIDR:   "172.16.0.1/24",
 		IPv6AddressCIDR:   "fd26:b6a6:4454:310a:0000:0000:0000:0001/64",
 		RouteDestinations: routeDestinations,
-		TunFD:             -1,
 		Transport:         unixConn,
 		MTU:               MTU,
 	}

+ 5 - 5
psiphon/config.go

@@ -469,11 +469,11 @@ type Config struct {
 	EmitSLOKs bool
 
 	// PacketTunnelTunDeviceFileDescriptor specifies a tun device file descriptor
-	// to use for running a packet tunnel. When set, a packet tunnel is established
-	// through the server and packets are relayed via the tun device. The file
-	// descriptor is duped in NewController.
+	// to use for running a packet tunnel. When this value is > 0, a packet tunnel
+	// is established through the server and packets are relayed via the tun device
+	// file descriptor. The file descriptor is duped in NewController.
 	// When PacketTunnelTunDeviceFileDescriptor is set, TunnelPoolSize must be 1.
-	PacketTunnelTunFileDescriptor *int
+	PacketTunnelTunFileDescriptor int
 }
 
 // DownloadURL specifies a URL for downloading resources along with parameters
@@ -657,7 +657,7 @@ func LoadConfig(configJson []byte) (*Config, error) {
 	}
 
 	// This constraint is expected by logic in Controller.runTunnels()
-	if config.PacketTunnelTunFileDescriptor != nil && config.TunnelPoolSize != 1 {
+	if config.PacketTunnelTunFileDescriptor > 0 && config.TunnelPoolSize != 1 {
 		return nil, common.ContextError(errors.New("PacketTunnelTunFileDescriptor requires TunnelPoolSize to be 1"))
 	}
 

+ 4 - 4
psiphon/controller.go

@@ -145,7 +145,7 @@ func NewController(config *Config) (controller *Controller, err error) {
 
 	controller.splitTunnelClassifier = NewSplitTunnelClassifier(config, controller)
 
-	if config.PacketTunnelTunFileDescriptor != nil {
+	if config.PacketTunnelTunFileDescriptor > 0 {
 
 		// Run a packet tunnel client. The lifetime of the tun.Client is the
 		// lifetime of the Controller, so it exists across tunnel establishments
@@ -156,9 +156,9 @@ func NewController(config *Config) (controller *Controller, err error) {
 		packetTunnelTransport := NewPacketTunnelTransport()
 
 		packetTunnelClient, err := tun.NewClient(&tun.ClientConfig{
-			Logger:    NoticeCommonLogger(),
-			TunFD:     *config.PacketTunnelTunFileDescriptor,
-			Transport: packetTunnelTransport,
+			Logger:            NoticeCommonLogger(),
+			TunFileDescriptor: config.PacketTunnelTunFileDescriptor,
+			Transport:         packetTunnelTransport,
 		})
 		if err != nil {
 			return nil, common.ContextError(err)