// StripPort strips port from the host.
// https://golang.org/pkg/net/url/#URL.Hostname
484578
func StripPort(host string) string {
	colon := strings.IndexByte(host, ':')
	if colon == -1 {
		return host
	}
	if i := strings.IndexByte(host, ']'); i != -1 {
		return strings.TrimPrefix(host[:i], "[")
	}
	return host[:colon]
}
686638
func (d *VppDriver) CreateHostAccPort(id, a string, nw int) (string, error) {
	log.Infof("Not implemented")
	return "", nil
}
617471
func (s *PortManager) ReleasePort(networks []tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber, addr tcpip.Address, port uint16) {
	s.mu.Lock()
	defer s.mu.Unlock()

	for _, network := range networks {
		desc := portDescriptor{network, transport, port}
		if m, ok := s.allocatedPorts[desc]; ok {
			n, ok := m[addr]
			if !ok {
				continue
			}
			n.refs--
			if n.refs == 0 {
				delete(m, addr)
			} else {
				m[addr] = n
			}
			if len(m) == 0 {
				delete(s.allocatedPorts, desc)
			}
		}
	}
}
428470
func (hp *hostPort) networkHostPort() network.HostPort {
	return network.HostPort{
		Address: network.Address{
			Value:     hp.Value,
			Type:      network.AddressType(hp.AddressType),
			Scope:     network.Scope(hp.Scope),
			SpaceName: network.SpaceName(hp.SpaceName),
		},
		Port: hp.Port,
	}
}
1153289
func HostPort(urlStr string) (string, error) {
	// TODO: rename this function to URLHostPort instead, like
	// ListenHostPort below.
	u, err := url.Parse(urlStr)
	if err != nil {
		return "", fmt.Errorf("could not parse %q as a url: %v", urlStr, err)
	}
	if u.Scheme == "" {
		return "", fmt.Errorf("url %q has no scheme", urlStr)
	}
	hostPort := u.Host
	if hostPort == "" || strings.HasPrefix(hostPort, ":") {
		return "", fmt.Errorf("url %q has no host", urlStr)
	}
	idx := strings.Index(hostPort, "]")
	if idx == -1 {
		idx = 0
	}
	if !strings.Contains(hostPort[idx:], ":") {
		if u.Scheme == "https" {
			hostPort += ":443"
		} else {
			hostPort += ":80"
		}
	}
	return hostPort, nil
}
475618
func (p Port) NetworkPort() corenetwork.Port {
	return corenetwork.Port{
		Protocol: p.Protocol,
		Number:   p.Number,
	}
}
83732
func (sw *OvsSwitch) AddHostPort(intfName string, intfNum, network int, isHostNS bool) (string, error) {
	var err error

	// some error checking
	if sw.netType != "vxlan" {
		log.Fatalf("Can not add host port to OVS type %s.", sw.netType)
	}

	ovsPortType := ""
	ovsPortName := getOvsPortName(intfName, isHostNS)
	if isHostNS {
		ovsPortType = "internal"
	} else {
		log.Infof("Host port in container name space -- ignore")
		return "", nil
	}

	portID := "host" + intfName

	// If the port already exists in OVS, remove it first
	if sw.ovsdbDriver.IsPortNamePresent(ovsPortName) {
		log.Infof("Removing existing interface entry %s from OVS", ovsPortName)

		// Delete it from ovsdb
		err := sw.ovsdbDriver.DeletePort(ovsPortName)
		if err != nil {
			log.Errorf("Error deleting port %s from OVS. Err: %v", ovsPortName, err)
		}
	}

	// Ask OVSDB driver to add the port as an access port
	err = sw.ovsdbDriver.CreatePort(ovsPortName, ovsPortType, portID, hostVLAN, 0, 0)
	if err != nil {
		log.Errorf("Error adding hostport %s to OVS. Err: %v", intfName, err)
		return "", err
	}

	// Get the openflow port number for the interface
	ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(ovsPortName)
	if err != nil {
		log.Errorf("Could not find the OVS port %s. Err: %v", intfName, err)
		return "", err
	}

	// Assign an IP based on the intfnumber
	ipStr, macStr := netutils.PortToHostIPMAC(intfNum, network)
	mac, _ := net.ParseMAC(macStr)

	portInfo := ofnet.HostPortInfo{
		PortNo:  ofpPort,
		MacAddr: mac,
		IpAddr:  ipStr,
		Kind:    "NAT",
	}
	// Add to ofnet if this is the hostNS port.
	netutils.SetInterfaceMac(intfName, macStr)
	netutils.SetInterfaceIP(intfName, ipStr)
	err = setLinkUp(intfName)

	if sw.ofnetAgent != nil {
		err = sw.ofnetAgent.AddHostPort(portInfo)
		if err != nil {
			log.Errorf("Error adding host port %s. Err: %v", intfName, err)
			return "", err
		}
	}

	log.Infof("Added host port %s to OVS switch %s.", intfName, sw.bridgeName)

	defer func() {
		if err != nil {
			sw.ovsdbDriver.DeletePort(intfName)
		}
	}()

	return ipStr, nil
}
631453
func (n *Network) PeerPort(p *Peer, portName PortName) uint16 {
	peerPorts := n.PortsByPeerID[p.ID()]
	Expect(peerPorts).NotTo(BeNil())
	return peerPorts[portName]
}
208810
func (spec *ConnectionSpec) Port(port int) *ConnectionSpec {
	spec.port = port
	return spec
}
146539
func (c *Container) Port(ctx context.Context, port string) (string, error) {
	if bindings, ok := c.container.NetworkSettings.Ports[nat.Port(port)]; ok {
		result := []string{}
		for _, binding := range bindings {
			result = append(result, binding.HostIP+":"+binding.HostPort)
		}

		return strings.Join(result, "\n"), nil
	}
	return "", nil
}
1018526
func (d *FakeNetEpDriver) CreateHostAccPort(id, a string, nw int) (string, error) {
	return "", core.Errorf("Not implemented")
}
668701
func (c *xContext) Port() int {
	host := c.Request().Host()
	delim := `:`
	if len(host) > 0 && host[0] == '[' {
		delim = `]:`
	}
	parts := strings.SplitN(host, delim, 2)
	if len(parts) > 1 {
		port, _ := strconv.Atoi(parts[1])
		return port
	}
	return 80
}
419528
func (r *Request) Port() string {
	_, port, err := net.SplitHostPort(r.W.RemoteAddr().String())
	if err != nil {
		return "0"
	}
	return port
}
515424
func (sw *OvsSwitch) CreatePort(intfName string, cfgEp *mastercfg.CfgEndpointState, pktTag, nwPktTag, burst, dscp int, skipVethPair bool, bandwidth int64) error {
	var ovsIntfType string
	var err error
	vethCreated := false
	dbUpdated := false

	// Get OVS port name
	ovsPortName := getOvsPortName(intfName, skipVethPair)
	defer func() {
		if err != nil {
			if vethCreated {
				deleteVethPair(intfName, ovsPortName)
			}
			if dbUpdated {
				sw.ovsdbDriver.DeletePort(intfName)
			}
		}
	}()

	// Create Veth pairs if required
	if useVethPair && !skipVethPair {
		ovsIntfType = ""

		// Create a Veth pair
		err = createVethPair(intfName, ovsPortName)
		if err != nil {
			log.Errorf("Error creating veth pairs. Err: %v", err)
			return err
		}
		vethCreated = true

		// Set the OVS side of the port as up
		err = setLinkUp(ovsPortName)
		if err != nil {
			log.Errorf("Error setting link %s up. Err: %v", ovsPortName, err)
			return err
		}
	} else {
		ovsPortName = intfName
		ovsIntfType = "internal"
	}

	// If the port already exists in OVS, remove it first
	if sw.ovsdbDriver.IsPortNamePresent(ovsPortName) {
		log.Debugf("Removing existing interface entry %s from OVS", ovsPortName)

		// Delete it from ovsdb
		err = sw.ovsdbDriver.DeletePort(ovsPortName)
		if err != nil {
			log.Errorf("Error deleting port %s from OVS. Err: %v", ovsPortName, err)
		}
	}
	// Ask OVSDB driver to add the port
	err = sw.ovsdbDriver.CreatePort(ovsPortName, ovsIntfType, cfgEp.ID, pktTag, burst, bandwidth)
	if err != nil {
		return err
	}
	dbUpdated = true

	// Wait a little for OVS to create the interface
	time.Sleep(300 * time.Millisecond)

	// Set the link mtu to 1450 to allow for 50 bytes vxlan encap
	// (inner eth header(14) + outer IP(20) outer UDP(8) + vxlan header(8))
	if sw.netType == "vxlan" {
		correctMtu := sw.vxlanEncapMtu - 50 //Include Vxlan header size
		err = setLinkMtu(intfName, correctMtu)
	} else {
		err = setLinkMtu(intfName, sw.vxlanEncapMtu)
	}
	if err != nil {
		log.Errorf("Error setting link %s mtu. Err: %v", intfName, err)
		return err
	}

	// Set the interface mac address
	err = netutils.SetInterfaceMac(intfName, cfgEp.MacAddress)
	if err != nil {
		log.Errorf("Error setting interface Mac %s on port %s", cfgEp.MacAddress, intfName)
		return err
	}

	// Add the endpoint to ofnet
	// Get the openflow port number for the interface
	ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(ovsPortName)
	if err != nil {
		log.Errorf("Could not find the OVS port %s. Err: %v", ovsPortName, err)
		return err
	}

	macAddr, _ := net.ParseMAC(cfgEp.MacAddress)

	// Assign an IP based on the intfnumber
	pvtIP := sw.getPvtIP(ovsPortName)

	// Build the endpoint info
	endpoint := ofnet.EndpointInfo{
		PortNo:            ofpPort,
		MacAddr:           macAddr,
		Vlan:              uint16(nwPktTag),
		IpAddr:            net.ParseIP(cfgEp.IPAddress),
		Ipv6Addr:          net.ParseIP(cfgEp.IPv6Address),
		EndpointGroup:     cfgEp.EndpointGroupID,
		EndpointGroupVlan: uint16(pktTag),
		Dscp:              dscp,
		HostPvtIP:         pvtIP,
	}

	log.Infof("Adding local endpoint: {%+v}", endpoint)

	// Add the local port to ofnet
	err = sw.ofnetAgent.AddLocalEndpoint(endpoint)

	if err != nil {
		log.Errorf("Error adding local port %s to ofnet. Err: %v", ovsPortName, err)
		return err
	}
	return nil
}
509636
func (u *DockerRegistryURL) HostPort() string {
	if u.Port == "" {
		return u.Host
	}
	return net.JoinHostPort(u.Host, u.Port)

}
688794
func (b *Bogus) HostPort() (string, string) {
	h, p, _ := net.SplitHostPort(b.server.URL[7:])
	return h, p
}
832108
func (p *NetPlugin) DeleteHostAccPort(portName string) error {
	p.Lock()
	defer p.Unlock()
	return p.NetworkDriver.DeleteHostAccPort(portName)
}