github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/mixnet/conn.go (about)

     1  // Copyright (c) 2015, Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mixnet
    16  
    17  import (
    18  	"net"
    19  	"sync"
    20  	"time"
    21  )
    22  
    23  // Conn implements the net.Conn interface. The read and write operations are
    24  // overloaded to check that only cells are sent between entities in the mixnet
    25  // protocol.
    26  type Conn struct {
    27  	net.Conn
    28  	id        uint32
    29  	timeout   time.Duration // timeout on read/write.
    30  	circuits  map[uint64]*Circuit
    31  	cLock     *sync.RWMutex
    32  	withProxy bool
    33  }
    34  
    35  // Read a cell from the channel. If len(msg) != CellBytes, return an error.
    36  func (c *Conn) Read(msg []byte) (n int, err error) {
    37  	c.Conn.SetDeadline(time.Now().Add(c.timeout))
    38  	n, err = c.Conn.Read(msg)
    39  	if err != nil {
    40  		return n, err
    41  	}
    42  	if n != CellBytes {
    43  		return n, errCellLength
    44  	}
    45  	return n, nil
    46  }
    47  
    48  // Write a cell to the channel. If the len(cell) != CellBytes, return an error.
    49  func (c *Conn) Write(msg []byte) (n int, err error) {
    50  	c.Conn.SetDeadline(time.Now().Add(c.timeout))
    51  	if len(msg) != CellBytes {
    52  		return 0, errCellLength
    53  	}
    54  	n, err = c.Conn.Write(msg)
    55  	if err != nil {
    56  		return n, err
    57  	}
    58  	return n, nil
    59  }
    60  
    61  func (c *Conn) Member(id uint64) bool {
    62  	c.cLock.RLock()
    63  	_, ok := c.circuits[id]
    64  	c.cLock.RUnlock()
    65  	return ok
    66  }
    67  
    68  func (c *Conn) GetCircuit(id uint64) *Circuit {
    69  	c.cLock.RLock()
    70  	defer c.cLock.RUnlock()
    71  	return c.circuits[id]
    72  }
    73  
    74  func (c *Conn) AddCircuit(circuit *Circuit) {
    75  	c.cLock.Lock()
    76  	defer c.cLock.Unlock()
    77  	c.circuits[circuit.id] = circuit
    78  }
    79  
    80  func (c *Conn) DeleteCircuit(circuit *Circuit) bool {
    81  	c.cLock.Lock()
    82  	defer c.cLock.Unlock()
    83  	close(circuit.cells)
    84  	delete(c.circuits, circuit.id)
    85  	return len(c.circuits) == 0
    86  }
    87  
    88  func (c *Conn) Empty() bool {
    89  	c.cLock.RLock()
    90  	defer c.cLock.RUnlock()
    91  	return len(c.circuits) == 0
    92  }