// GetPort asks the kernel for a free open port that is ready to use
494970
func GetPort() (int, error) {
	addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
	if err != nil {
		panic(err)
	}

	l, err := net.ListenTCP("tcp", addr)
	if err != nil {
		return -1, errors.Errorf("Error accessing port %d", addr.Port)
	}
	defer l.Close()
	return l.Addr().(*net.TCPAddr).Port, nil
}
469572
func (c *LinkManagerContext) Port(p uint32) LinkPort {
	for _, port := range c.Ports {
		if port.Port == p {
			return port
		}
	}

	return LinkPort{}
}
454540
func (i ImportBinding) GetPortNumber(instanceID int) (uint16, error) {
	t := template.Must(template.New(i.Application).Funcs(funcs).Parse(i.PortTemplate))

	var buffer bytes.Buffer
	if err := t.Execute(&buffer, struct{ InstanceID int }{instanceID}); err != nil {
		return 0, &TemplateError{
			Application: i.Application,
			Field:       i.PortTemplate,
			Message:     "could not interpret value",
		}
	}

	if port := buffer.String(); port != "" {
		portNumber, err := strconv.Atoi(port)
		if err != nil {
			return 0, &TemplateError{
				Application: i.Application,
				Field:       i.PortTemplate,
				Message:     "port value is not an integer",
			}
		} else if portNumber < 0 {
			return 0, &TemplateError{
				Application: i.Application,
				Field:       i.PortTemplate,
				Message:     "port value must be gte zero",
			}
		}
		return uint16(portNumber), nil
	}

	return i.PortNumber, nil
}
320048
func (_class SDNControllerClass) GetPort(sessionID SessionRef, self SDNControllerRef) (_retval int, _err error) {
	_method := "SDN_controller.get_port"
	_sessionIDArg, _err := convertSessionRefToXen(fmt.Sprintf("%s(%s)", _method, "session_id"), sessionID)
	if _err != nil {
		return
	}
	_selfArg, _err := convertSDNControllerRefToXen(fmt.Sprintf("%s(%s)", _method, "self"), self)
	if _err != nil {
		return
	}
	_result, _err := _class.client.APICall(_method, _sessionIDArg, _selfArg)
	if _err != nil {
		return
	}
	_retval, _err = convertIntToGo(_method + " -> ", _result.Value)
	return
}
251996
func OpenPort(c *Config) (*Port, error) {
	size, par, stop := c.Size, c.Parity, c.StopBits
	if size == 0 {
		size = DefaultSize
	}
	if par == 0 {
		par = ParityNone
	}
	if stop == 0 {
		stop = Stop1
	}
	return openPort(c.Name, c.Baud, size, par, stop, c.ReadTimeout)
}
1091368
func (a *dbPortAllocator) Get() (int64, error) {
	sql := `UPDATE ports SET taken = true WHERE port = (SELECT port FROM ports WHERE taken IS NULL ORDER BY port ASC LIMIT 1) RETURNING port`
	var port int64
	err := a.db.QueryRow(sql).Scan(&port)
	return port, err
}
629035
func getDataPathPort(portNum uint32) (uint32, error) {
	// if the value comes as 0 by any reason we set it to default value 4789
	if portNum == 0 {
		portNum = 4789
		return portNum, nil
	}
	// IANA procedures for each range in detail
	// The Well Known Ports, aka the System Ports, from 0-1023
	// The Registered Ports, aka the User Ports, from 1024-49151
	// The Dynamic Ports, aka the Private Ports, from 49152-65535
	// So we can allow range between 1024 to 49151
	if portNum < 1024 || portNum > 49151 {
		return 0, fmt.Errorf("Datapath port number is not in valid range (1024-49151) : %d", portNum)
	}
	return portNum, nil
}
1246930
func (o *oracleInstance) OpenPorts(ctx context.ProviderCallContext, machineId string, rules []network.IngressRule) error {
	if o.env.Config().FirewallMode() != config.FwInstance {
		return errors.Errorf(
			"invalid firewall mode %q for opening ports on instance",
			o.env.Config().FirewallMode(),
		)
	}

	return o.env.OpenPortsOnInstance(ctx, machineId, rules)
}
553340
func GetPortFromKubeConfig(filename, machineName string) (int, error) {
	con, err := ReadConfigOrNew(filename)
	if err != nil {
		return 0, errors.Wrap(err, "Error getting kubeconfig status")
	}
	cluster, ok := con.Clusters[machineName]
	if !ok {
		return 0, errors.Errorf("Kubeconfig does not have a record of the machine cluster")
	}
	kurl, err := url.Parse(cluster.Server)
	if err != nil {
		return APIServerPort, nil
	}
	_, kport, err := net.SplitHostPort(kurl.Host)
	if err != nil {
		return APIServerPort, nil
	}
	port, err := strconv.Atoi(kport)
	return port, err
}
337638
func (s *Server) GetPortHTTP() string {
	hostPort := s.HostPortHTTP
	hostPortSplit := strings.Split(hostPort, ":")
	port := hostPortSplit[len(hostPortSplit)-1]
	return port
}
604473
func (c *NetworkManagerContext) Port(p uint32) NetworkPort {
	for _, port := range c.Ports {
		if port.Port == p {
			return port
		}
	}

	return NetworkPort{}
}
838947
func (r RandomPortListenerSource) GetListener() (net.Listener, string, error) {
	localhost := "127.0.0.1"
	listener, err := net.Listen("tcp", fmt.Sprintf("%s:0", localhost))
	if err != nil {
		return nil, "", err
	}
	port := listener.Addr().(*net.TCPAddr).Port
	address := fmt.Sprintf("%s:%d", localhost, port)
	return listener, address, nil
}
20617
func (c *Transport) getConnection(ctx context.Context) (*Connection, error) {
	conn := c.getAvailableConnection()
	if conn != nil {
		return conn, nil
	}

	// No connections available, make a new one
	conn, err := c.createConnection()
	if err != nil {
		if conn != nil {
			conn.Close()
		}
		return nil, driver.WithStack(err)
	}

	// Invoke callback
	if cb := c.onConnectionCreated; cb != nil {
		if err := cb(ctx, conn); err != nil {
			conn.Close()
			return nil, driver.WithStack(err)
		}
	}

	// Mark the connection as ready
	atomic.StoreInt32(&conn.configured, 1)

	return conn, nil
}
29920
func (kl *Kubelet) GetPortForward(podName, podNamespace string, podUID types.UID, portForwardOpts portforward.V4Options) (*url.URL, error) {
	pods, err := kl.containerRuntime.GetPods(false)
	if err != nil {
		return nil, err
	}
	// Resolve and type convert back again.
	// We need the static pod UID but the kubecontainer API works with types.UID.
	podUID = types.UID(kl.podManager.TranslatePodUID(podUID))
	podFullName := kubecontainer.BuildPodFullName(podName, podNamespace)
	pod := kubecontainer.Pods(pods).FindPod(podFullName, podUID)
	if pod.IsEmpty() {
		return nil, fmt.Errorf("pod not found (%q)", podFullName)
	}

	return kl.streamingRuntime.GetPortForward(podName, podNamespace, podUID, portForwardOpts.Ports)
}
565800
func GetPort(addr net.Addr) (int, error) {
	_, lport, err := net.SplitHostPort(addr.String())
	if err != nil {
		return -1, err
	}
	lportInt, err := strconv.Atoi(lport)
	if err != nil {
		return -1, err
	}
	return lportInt, nil
}
1145689
func (d *OvsdbDriver) GetInterfacesInPort(portName string) []string {
	var intfList []string
	d.cacheLock.RLock()
	defer d.cacheLock.RUnlock()

	for _, row := range d.cache["Port"] {
		name := row.Fields["name"].(string)
		if name == portName {
			// Port found
			// Iterate over the list of interfaces
			switch (row.Fields["interfaces"]).(type) {
			case libovsdb.UUID: // Individual interface case
				intfUUID := row.Fields["interfaces"].(libovsdb.UUID)
				intfInfo := d.GetIntfInfo(intfUUID)
				if reflect.DeepEqual(intfInfo, libovsdb.Row{}) {
					log.Errorf("could not find interface with UUID: %+v", intfUUID)
					break
				}
				intfList = append(intfList, intfInfo.Fields["name"].(string))
			case libovsdb.OvsSet: // Port bond case
				intfUUIDList := row.Fields["interfaces"].(libovsdb.OvsSet)
				for _, intfUUID := range intfUUIDList.GoSet {
					intfInfo := d.GetIntfInfo(intfUUID.(libovsdb.UUID))
					if reflect.DeepEqual(intfInfo, libovsdb.Row{}) {
						continue
					}
					intfList = append(intfList, intfInfo.Fields["name"].(string))
				}
			}
			sort.Strings(intfList)
			break
		}
	}
	return intfList
}
509636
func (u *DockerRegistryURL) HostPort() string {
	if u.Port == "" {
		return u.Host
	}
	return net.JoinHostPort(u.Host, u.Port)

}