// Returns the ip:port of the MinIO process
// running in the container
9876
func findEndpoint() (string, error) {
	cmd := exec.Command("netstat", "-ntlp")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		// error getting stdout pipe
		return "", err
	}
	if err = cmd.Start(); err != nil {
		// error executing the command.
		return "", err
	}
	// split netstat output in rows
	scanner := bufio.NewScanner(stdout)
	scanner.Split(bufio.ScanLines)
	// loop over the rows to find MinIO process
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), minioProcess) {
			line := scanner.Text()
			newLine := strings.Replace(line, ":::", "127.0.0.1:", 1)
			fields := strings.Fields(newLine)
			// index 3 in the row has the Local address
			// find the last index of ":" - address will
			// have port number after this index
			i := strings.LastIndex(fields[3], ":")
			// split address and port
			addr := fields[3][:i]
			port := fields[3][i+1:]
			// add surrounding [] for ip6 address
			if strings.Count(addr, ":") > 0 {
				addr = strings.Join([]string{"[", addr, "]"}, "")
			}
			// wait for cmd to complete before return
			if err = cmd.Wait(); err != nil {
				return "", err
			}
			// return joint address and port
			return strings.Join([]string{addr, port}, ":"), nil
		}
	}
	if err = scanner.Err(); err != nil {
		return "", err
	}
	if err = cmd.Wait(); err != nil {
		// command failed to run
		return "", err
	}
	// minio process not found, exit with error
	return "", errors.New("no minio process found")
}
1127722
func (c *Context) Port() int {
	parts := strings.Split(c.Request().Host(), ":")
	if len(parts) == 2 {
		port, _ := strconv.Atoi(parts[1])
		return port
	}
	return 80
}
771585
func HostPortToIP(hostport string, ctx *net.TCPAddr) (hostaddr *net.TCPAddr, err error) {
	host, port, err := net.SplitHostPort(hostport)
	if err != nil {
		return nil, err
	}
	iport, err := strconv.Atoi(port)
	if err != nil || iport < 0 || iport > 0xFFFF {
		return nil, fmt.Errorf("invalid port %d", iport)
	}
	var addr net.IP
	if ctx != nil && host == "localhost" {
		if ctx.IP.To4() != nil {
			addr = net.IPv4(127, 0, 0, 1)
		} else {
			addr = net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
		}
	} else if addr = net.ParseIP(host); addr == nil {
		return nil, fmt.Errorf("could not parse IP %s", host)
	}

	return &net.TCPAddr{IP: addr, Port: iport}, nil
}
1198993
func (c *Client) GetMyIP(ctx context.Context) (net.IP, error) {
	var ip bytes.Buffer

	req, err := c.NewRequest("GET", ipPath, nil, nil)
	if err != nil {
		return nil, err
	}

	if err := c.Do(ctx, req, &ip); err != nil {
		return nil, err
	}

	return net.ParseIP(strings.Trim(ip.String(), "\"")), nil
}
999384
func (r *Request) IP() string {
	ip, _, err := net.SplitHostPort(r.W.RemoteAddr().String())
	if err != nil {
		return r.W.RemoteAddr().String()
	}
	return ip
}
2396
func getFreePort(host string, ports ...int) (int, error) {
	for _, port := range ports {
		c, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
		if err == nil {
			c.Close()
			return port, nil
		}
	}

	c, err := net.Listen("tcp", host+":0")
	if err != nil {
		return 0, err
	}
	addr := c.Addr().(*net.TCPAddr)
	c.Close()
	return addr.Port, nil
}
604473
func (c *NetworkManagerContext) Port(p uint32) NetworkPort {
	for _, port := range c.Ports {
		if port.Port == p {
			return port
		}
	}

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

	return strings.Split(addr, ":")[0]
}
1206242
func (d *Driver) detectIP(c *docker.Container, driverConfig *TaskConfig) (string, bool) {
	if c.NetworkSettings == nil {
		// This should only happen if there's been a coding error (such
		// as not calling InspectContainer after CreateContainer). Code
		// defensively in case the Docker API changes subtly.
		d.logger.Error("no network settings for container", "container_id", c.ID)
		return "", false
	}

	ip, ipName := "", ""
	auto := false
	for name, net := range c.NetworkSettings.Networks {
		if net.IPAddress == "" {
			// Ignore networks without an IP address
			continue
		}

		ip = net.IPAddress
		if driverConfig.AdvertiseIPv6Addr {
			ip = net.GlobalIPv6Address
			auto = true
		}
		ipName = name

		// Don't auto-advertise IPs for default networks (bridge on
		// Linux, nat on Windows)
		if name != "bridge" && name != "nat" {
			auto = true
		}

		break
	}

	if n := len(c.NetworkSettings.Networks); n > 1 {
		d.logger.Warn("multiple Docker networks for container found but Nomad only supports 1",
			"total_networks", n,
			"container_id", c.ID,
			"container_network", ipName)
	}

	return ip, auto
}
132949
func (f *SockaddrFuncs) GetInterfaceIP(namedIfRE string) (string, error) {
	return sockaddr.GetInterfaceIP(namedIfRE)
}
1298688
func (docker *Docker) ServicePortIndex(port int) (int, error) {
	if docker.PortMappings == nil || len(*docker.PortMappings) == 0 {
		return 0, errors.New("The docker does not contain any port mappings to search")
	}

	// step: iterate and find the port
	for index, containerPort := range *docker.PortMappings {
		if containerPort.ContainerPort == port {
			return index, nil
		}
	}

	// step: we didn't find the port in the mappings
	return 0, fmt.Errorf("The docker port %d was not found in the container port mappings", port)
}
803120
func (s *ServiceCache) Find(ip net.IP, port int, host string, local bool) interface{} {
	s.RLock()
	defer s.RUnlock()

	if host != "" {
		if data := s.findHost(host, port, local); data != nil {
			return data
		}
	}

	return s.findIP(ip, port, local)
}
1113189
func getRemoteIP() (ip string, err error) {
	client := &http.Client{
		Timeout: time.Second * 30,
	}

	req, err := http.NewRequest("GET", "http://ifconfig.me", nil)
	if err != nil {
		return
	}

	req.Header.Set("User-Agent", "curl")
	res, err := client.Do(req)
	if err != nil {
		return
	}

	defer res.Body.Close()

	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}
	ip = string(data)

	return
}
792110
func GetHostDockerEnv(api libmachine.API) (map[string]string, error) {
	host, err := CheckIfHostExistsAndLoad(api, cfg.GetMachineName())
	if err != nil {
		return nil, errors.Wrap(err, "Error checking that api exists and loading it")
	}
	ip, err := host.Driver.GetIP()
	if err != nil {
		return nil, errors.Wrap(err, "Error getting ip from host")
	}

	tcpPrefix := "tcp://"
	port := "2376"

	envMap := map[string]string{
		"DOCKER_TLS_VERIFY": "1",
		"DOCKER_HOST":       tcpPrefix + net.JoinHostPort(ip, port),
		"DOCKER_CERT_PATH":  constants.MakeMiniPath("certs"),
	}
	return envMap, nil
}
966226
func (mg *MailgunImpl) GetIP(ctx context.Context, ip string) (IPAddress, error) {
	r := newHTTPRequest(generatePublicApiUrl(mg, ipsEndpoint) + "/" + ip)
	r.setClient(mg.Client())
	r.setBasicAuth(basicAuthUser, mg.APIKey())
	var resp IPAddress
	err := getResponseFromJSON(ctx, r, &resp)
	return resp, err
}
146017
func GetHostPartIP(ip net.IP, mask net.IPMask) (net.IP, error) {
	// Find the effective starting of address and mask
	is, ms, err := compareIPMask(ip, mask)
	if err != nil {
		return nil, fmt.Errorf("cannot compute host portion ip address because %s", err)
	}

	// Compute host portion
	out := GetIPCopy(ip)
	for i := 0; i < len(mask[ms:]); i++ {
		out[is+i] &= ^mask[ms+i]
	}

	return out, nil
}
762669
func (p Port) Proto() string {
	parts := strings.Split(string(p), "/")
	if len(parts) == 1 {
		return "tcp"
	}
	return parts[1]
}