// ReleaseIP adds the provided ip back into the pool of
// available ips to be returned for use.
774408
func (a *IPAllocator) ReleaseIP(network *net.IPNet, ip net.IP) error {
	if network == nil {
		return nil
	}

	a.mutex.Lock()
	defer a.mutex.Unlock()

	if allocated, exists := a.allocatedIPs[network.String()]; exists {
		delete(allocated.p, ip.String())
	}
	return nil
}
380012
func (t *Transport) ReleasePeer(pid peer.Identifier, sub peer.Subscriber) error {
	t.lock.Lock()
	defer t.lock.Unlock()

	p, ok := t.peers[pid.Identifier()]
	if !ok {
		return peer.ErrTransportHasNoReferenceToPeer{
			TransportName:  "tchannel.Transport",
			PeerIdentifier: pid.Identifier(),
		}
	}

	if err := p.Unsubscribe(sub); err != nil {
		return err
	}

	if p.NumSubscribers() == 0 {
		// Release the peer so that the connection retention loop stops.
		p.Release()
		delete(t.peers, pid.Identifier())
	}

	return nil
}
443968
func (c *connector) RemoteIP() string {
	addr := c.RemoteAddr()
	if addr == "" {
		return ""
	}

	return strings.Split(addr, ":")[0]
}
919538
func (c *CIDR) FirstIP() net.IP {
	var ip net.IP
	for ip = IncIP(c.ip.Mask(c.ipnet.Mask)); c.ipnet.Contains(ip); IncIP(ip) {
		break
	}
	return ip
}
1217632
func ReleaseAddressHandler(w http.ResponseWriter, r *http.Request, vars map[string]string) (interface{}, error) {
	var relReq AddressReleaseRequest
	var networkID string
	var epgName string
	var epgCfg *mastercfg.EndpointGroupState

	// Get object from the request
	err := json.NewDecoder(r.Body).Decode(&relReq)
	if err != nil {
		log.Errorf("Error decoding ReleaseAddressHandler. Err %v", err)
		return nil, err
	}

	log.Infof("Received AddressReleaseRequest: %+v", relReq)

	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return nil, err
	}

	networkID, epgName = getNwAndEpgFromAddrReq(relReq.NetworkID)
	if len(epgName) > 0 {
		epgCfg = &mastercfg.EndpointGroupState{}
		epgCfg.StateDriver = stateDriver
		if err = epgCfg.Read(epgName); err != nil {
			log.Errorf("failed to read epg %s, %s", epgName, err)
			return nil, err
		}
		log.Infof("AddressReleaseRequest for network: %s epg: %s", networkID, epgName)
	}

	// find the network from network id
	nwCfg := &mastercfg.CfgNetworkState{}
	nwCfg.StateDriver = stateDriver
	err = nwCfg.Read(networkID)
	if err != nil {
		log.Errorf("network %s is not operational", relReq.NetworkID)
		return nil, err
	}

	// release addresses
	err = networkReleaseAddress(nwCfg, epgCfg, relReq.IPv4Address)
	if err != nil {
		log.Errorf("Failed to release address. Err: %v", err)
		return nil, err
	}

	return "success", nil
}
659102
func (r *kvReferenceCounter) release(ctx context.Context, key string) (err error) {
	r.Lock()
	defer r.Unlock()

	refcnt, ok := r.keys[key] // 0 if not found
	// avoid underflow and report bug
	if !ok || refcnt == 0 {
		log.WithField("key", key).Error("BUG: attempt to release ipcache entry while refcnt == 0")
		return nil
	}

	refcnt--
	if refcnt == 0 {
		delete(r.keys, key)
		delete(r.marshaledIPIDPairs, key)
		err = r.store.release(ctx, key)
	} else {
		r.keys[key] = refcnt
	}
	return err
}
1012804
func (s *Server) Release() {
	// Block all new incomming socket connections.
	s.Block(true)

	// Wait for a little moment, so new incomming sockets are added
	// to the sockets active list.
	time.Sleep(200 * time.Millisecond)

	// Close all current connected sockets.
	sockets := s.Sockets()
	for _, s := range sockets {
		s.Close()
	}
}
166284
func (c *Client) CreateRelease(appID string, release *ct.Release) error {
	release.AppID = appID
	return c.Post("/releases", release, release)
}
439619
func (s *IpAddressResponse) SetIp(v string) *IpAddressResponse {
	s.Ip = &v
	return s
}
1313203
func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP {
	p := new(net.IP)
	f.IPVarP(p, name, "", value, usage)
	return p
}
784377
func GetIP(conn client.Connection, req IPRequest) (*IP, error) {
	logger := plog.WithFields(log.Fields{
		"hostid":    req.HostID,
		"ipaddress": req.IPAddress,
	})

	basepth := "/"
	if req.PoolID != "" {
		basepth = path.Join("/pools", req.PoolID)
	}

	// Get the current pool ip
	ppth := path.Join(basepth, "/ips", req.IPID())
	pdat := &PoolIP{}
	if err := conn.Get(ppth, pdat); err != nil {
		logger.WithError(err).Debug("Could not look up virtual ip on resource pool")
		// TODO: error
		return nil, err
	}
	logger.Debug("Found pool ip")

	// Get the current host ip
	hpth := path.Join(basepth, "/hosts", req.HostID, "ips", req.IPID())
	hdat := &HostIP{}
	if err := conn.Get(hpth, hdat); err != nil {
		logger.WithError(err).Debug("Could not look up virtual ip on host")
		// TODO: error
		return nil, err
	}
	logger.Debug("Found host ip")

	return &IP{
		PoolIP:    *pdat,
		HostIP:    *hdat,
		HostID:    req.HostID,
		IPAddress: req.IPAddress,
	}, nil
}
22350
func (p *ConnPool) releaseConn(conn *Conn) {
	refCount := atomic.AddInt32(&conn.refCount, -1)
	if refCount == 0 && atomic.LoadInt32(&conn.shouldClose) == 1 {
		conn.Close()
	}
}
135278
func getOutboundIP() (string, error) {
	var outboundIP string
	var err error
	timeout := time.After(outboundIPMaxWait * time.Second)
	for {
		if outboundIP, err = utils.GetIPAddress(); err == nil {
			// Success
			return outboundIP, nil
		}
		select {
		case <-timeout:
			// Give up
			return "", fmt.Errorf("Gave up waiting for network (to determine our outbound IP address)")
		default:
			// Retry
			log.Debug("Waiting for network initialization")
			time.Sleep(outboundIPRetryDelay * time.Second)
		}
	}
}
620168
func CreateIP(conn client.Connection, req IPRequest, netmask, iface string) error {
	logger := plog.WithFields(log.Fields{
		"hostid":    req.HostID,
		"ipaddress": req.IPAddress,
	})

	basepth := "/"
	if req.PoolID != "" {
		basepth = path.Join("/pools", req.PoolID)
	}

	t := conn.NewTransaction()

	// Prepare the pool ip
	ppth := path.Join(basepth, "/ips")
	err := conn.CreateIfExists(ppth, &client.Dir{})
	if err != nil && err != client.ErrNodeExists {
		logger.WithError(err).Debug("Could not initialize pool ip path")
		// TODO: error
		return err
	}

	ppth = path.Join(ppth, req.IPID())
	pdat := &PoolIP{}
	t.Create(ppth, pdat)

	// Prepare the host ip
	hpth := path.Join(basepth, "/hosts", req.HostID, "/ips")
	err = conn.CreateIfExists(hpth, &client.Dir{})
	if err != nil && err != client.ErrNodeExists {
		logger.WithError(err).Debug("Could not initialize host ip path")
		// TODO: error
		return err
	}

	hpth = path.Join(hpth, req.IPID())
	hdat := &HostIP{
		Netmask:       netmask,
		BindInterface: iface,
	}
	t.Create(hpth, hdat)

	if err := t.Commit(); err != nil {
		logger.WithError(err).Debug("Could not commit transaction")
		// TODO: err
		return err
	}
	logger.Debug("Created ip")
	return nil
}
1041540
func lastIP(n *net.IPNet) net.IP {
	mask := n.Mask
	ip := n.IP
	lastip := make(net.IP, len(ip))
	// set bits zero in the mask to ones in ip
	for i, m := range mask {
		lastip[i] = (^m) | ip[i]
	}
	return lastip
}
1197715
func (a *allocator) ReleaseAddress(poolID string, address net.IP) error {
	var relAddress string
	if address != nil {
		relAddress = address.String()
	}
	req := &api.ReleaseAddressRequest{PoolID: poolID, Address: relAddress}
	res := &api.ReleaseAddressResponse{}
	return a.call("ReleaseAddress", req, res)
}
528093
func (l *HostIPListener) release(req IPRequest) {
	logger := plog.WithField("ipaddress", req.IPAddress)
	logger.Info("Releasing ip binding")
	if err := l.handler.ReleaseIP(req.IPAddress); err != nil {
		logger.WithError(err).Error("Could not release ip binding")
	}

	if err := DeleteIP(l.conn, req); err != nil {
		logger.WithError(err).Error("Could not delete ip node from zookeeper")
	}
}