| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /*
- * Copyright (c) 2016, Psiphon Inc.
- * All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package common
- import (
- "net"
- "sync"
- )
- // Conns is a synchronized list of Conns that is used to coordinate
- // interrupting a set of goroutines establishing connections, or
- // close a set of open connections, etc.
- // Once the list is closed, no more items may be added to the
- // list (unless it is reset).
- type Conns struct {
- mutex sync.Mutex
- isClosed bool
- conns map[net.Conn]bool
- }
- func (conns *Conns) Reset() {
- conns.mutex.Lock()
- defer conns.mutex.Unlock()
- conns.isClosed = false
- conns.conns = make(map[net.Conn]bool)
- }
- func (conns *Conns) Add(conn net.Conn) bool {
- conns.mutex.Lock()
- defer conns.mutex.Unlock()
- if conns.isClosed {
- return false
- }
- if conns.conns == nil {
- conns.conns = make(map[net.Conn]bool)
- }
- conns.conns[conn] = true
- return true
- }
- func (conns *Conns) Remove(conn net.Conn) {
- conns.mutex.Lock()
- defer conns.mutex.Unlock()
- delete(conns.conns, conn)
- }
- func (conns *Conns) CloseAll() {
- conns.mutex.Lock()
- defer conns.mutex.Unlock()
- conns.isClosed = true
- for conn, _ := range conns.conns {
- conn.Close()
- }
- conns.conns = make(map[net.Conn]bool)
- }
- // IPAddressFromAddr is a helper which extracts an IP address
- // from a net.Addr or returns "" if there is no IP address.
- func IPAddressFromAddr(addr net.Addr) string {
- ipAddress := ""
- if addr != nil {
- host, _, err := net.SplitHostPort(addr.String())
- if err == nil {
- ipAddress = host
- }
- }
- return ipAddress
- }
|