// makeEndpointID constructs an endpoint ID in the format of <hostname>_<port>.
426541
func makeEndpointID(listenPort int) (string, error) {
	hostname, err := os.Hostname()
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%v_%v", hostname, listenPort), nil
}
288116
func Lookup(id string) (*endpoint.Endpoint, error) {
	mutex.RLock()
	defer mutex.RUnlock()

	prefix, eid, err := endpointid.Parse(id)
	if err != nil {
		return nil, err
	}

	switch prefix {
	case endpointid.CiliumLocalIdPrefix:
		n, err := endpointid.ParseCiliumID(id)
		if err != nil {
			return nil, err
		}
		return lookupCiliumID(uint16(n)), nil

	case endpointid.CiliumGlobalIdPrefix:
		return nil, ErrUnsupportedID

	case endpointid.ContainerIdPrefix:
		return lookupContainerID(eid), nil

	case endpointid.DockerEndpointPrefix:
		return lookupDockerEndpoint(eid), nil

	case endpointid.ContainerNamePrefix:
		return lookupDockerContainerName(eid), nil

	case endpointid.PodNamePrefix:
		return lookupPodNameLocked(eid), nil

	case endpointid.IPv4Prefix:
		return lookupIPv4(eid), nil

	case endpointid.IPv6Prefix:
		return lookupIPv6(eid), nil

	default:
		return nil, ErrInvalidPrefix{InvalidPrefix: prefix.String()}
	}
}
716046
func (a *Client) GetEndpointIDConfig(params *GetEndpointIDConfigParams) (*GetEndpointIDConfigOK, error) {
	// TODO: Validate the params before sending
	if params == nil {
		params = NewGetEndpointIDConfigParams()
	}

	result, err := a.transport.Submit(&runtime.ClientOperation{
		ID:                 "GetEndpointIDConfig",
		Method:             "GET",
		PathPattern:        "/endpoint/{id}/config",
		ProducesMediaTypes: []string{"application/json"},
		ConsumesMediaTypes: []string{"application/json"},
		Schemes:            []string{"http"},
		Params:             params,
		Reader:             &GetEndpointIDConfigReader{formats: a.formats},
		Context:            params.Context,
		Client:             params.HTTPClient,
	})
	if err != nil {
		return nil, err
	}
	return result.(*GetEndpointIDConfigOK), nil

}
1127482
func ParseProxyID(proxyID string) (endpointID uint16, ingress bool, protocol string, port uint16, err error) {
	comps := strings.Split(proxyID, ":")
	if len(comps) != 4 {
		err = fmt.Errorf("invalid proxy ID structure: %s", proxyID)
		return
	}
	epID, err := strconv.ParseUint(comps[0], 10, 16)
	if err != nil {
		return
	}
	endpointID = uint16(epID)
	ingress = comps[1] == "ingress"
	protocol = comps[2]
	l4port, err := strconv.ParseUint(comps[3], 10, 16)
	if err != nil {
		return
	}
	port = uint16(l4port)
	return
}
211187
func (r *ForwarderRequest) CreateEndpoint(queue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) {
	r.mu.Lock()
	defer r.mu.Unlock()

	if r.segment == nil {
		return nil, tcpip.ErrInvalidEndpointState
	}

	f := r.forwarder
	ep, err := f.listen.createEndpointAndPerformHandshake(r.segment, &header.TCPSynOptions{
		MSS:           r.synOptions.MSS,
		WS:            r.synOptions.WS,
		TS:            r.synOptions.TS,
		TSVal:         r.synOptions.TSVal,
		TSEcr:         r.synOptions.TSEcr,
		SACKPermitted: r.synOptions.SACKPermitted,
	})
	if err != nil {
		return nil, err
	}

	// Start the protocol goroutine.
	ep.startAcceptedLoop(queue)

	return ep, nil
}
333850
func MakeSumEndpoint(s addservice.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		req := request.(SumRequest)
		v, err := s.Sum(ctx, req.A, req.B)
		return SumResponse{V: v, Err: err}, nil
	}
}
1361142
func (c *Client) EndpointCreate(ep *models.EndpointChangeRequest) error {
	id := pkgEndpointID.NewCiliumID(ep.ID)
	params := endpoint.NewPutEndpointIDParams().WithID(id).WithEndpoint(ep).WithTimeout(api.ClientTimeout)
	_, err := c.Endpoint.PutEndpointID(params)
	return Hint(err)
}
1174830
func WriteEndpoint(keys []*lxcmap.EndpointKey, pm *policymap.PolicyMap) error {
	return writeEndpoint(keys, pm.GetFd())
}
1095922
func NewGetEndpointID(ctx *middleware.Context, handler GetEndpointIDHandler) *GetEndpointID {
	return &GetEndpointID{Context: ctx, Handler: handler}
}
1341882
func makeEndpoint(hostport, serviceName string) (ep *zipkincore.Endpoint) {
	ep = zipkincore.NewEndpoint()

	// Set the ServiceName
	ep.ServiceName = serviceName

	if strings.IndexByte(hostport, ':') < 0 {
		// "<host>" becomes "<host>:0"
		hostport = hostport + ":0"
	}

	// try to parse provided "<host>:<port>"
	host, port, err := net.SplitHostPort(hostport)
	if err != nil {
		// if unparsable, return as "undefined:0"
		return
	}

	// try to set port number
	p, _ := strconv.ParseUint(port, 10, 16)
	ep.Port = int16(p)

	// if <host> is a domain name, look it up
	addrs, err := net.LookupIP(host)
	if err != nil {
		// return as "undefined:<port>"
		return
	}

	var addr4, addr16 net.IP
	for i := range addrs {
		addr := addrs[i].To4()
		if addr == nil {
			// IPv6
			if addr16 == nil {
				addr16 = addrs[i].To16() // IPv6 - 16 bytes
			}
		} else {
			// IPv4
			if addr4 == nil {
				addr4 = addr // IPv4 - 4 bytes
			}
		}
		if addr16 != nil && addr4 != nil {
			// IPv4 & IPv6 have been set, we can stop looking further
			break
		}
	}
	// default to 0 filled 4 byte array for IPv4 if IPv6 only host was found
	if addr4 == nil {
		addr4 = make([]byte, 4)
	}

	// set IPv4 and IPv6 addresses
	ep.Ipv4 = (int32)(binary.BigEndian.Uint32(addr4))
	ep.Ipv6 = []byte(addr16)
	return
}
1262207
func (s *CreateDevEndpointInput) SetEndpointName(v string) *CreateDevEndpointInput {
	s.EndpointName = &v
	return s
}
1095499
func endpointsToEndpointsMap(endpoints *v1.Endpoints, hostname string, hns HostNetworkService) proxyEndpointsMap {
	if endpoints == nil {
		return nil
	}

	endpointsMap := make(proxyEndpointsMap)
	// We need to build a map of portname -> all ip:ports for that
	// portname.  Explode Endpoints.Subsets[*] into this structure.
	for i := range endpoints.Subsets {
		ss := &endpoints.Subsets[i]
		for i := range ss.Ports {
			port := &ss.Ports[i]
			if port.Port == 0 {
				klog.Warningf("Ignoring invalid endpoint port %s", port.Name)
				continue
			}
			svcPortName := proxy.ServicePortName{
				NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name},
				Port:           port.Name,
			}
			for i := range ss.Addresses {
				addr := &ss.Addresses[i]
				if addr.IP == "" {
					klog.Warningf("Ignoring invalid endpoint port %s with empty host", port.Name)
					continue
				}
				isLocal := addr.NodeName != nil && *addr.NodeName == hostname
				epInfo := newEndpointInfo(addr.IP, uint16(port.Port), isLocal, hns)
				endpointsMap[svcPortName] = append(endpointsMap[svcPortName], epInfo)
			}
			if klog.V(3) {
				newEPList := []*endpointsInfo{}
				for _, ep := range endpointsMap[svcPortName] {
					newEPList = append(newEPList, ep)
				}
				klog.Infof("Setting endpoints for %q to %+v", svcPortName, newEPList)
			}
		}
	}
	return endpointsMap
}
253546
func ProxyID(endpointID uint16, ingress bool, protocol string, port uint16) string {
	direction := "egress"
	if ingress {
		direction = "ingress"
	}
	return fmt.Sprintf("%d:%s:%s:%d", endpointID, direction, protocol, port)
}
699455
func (s *CreateDevEndpointOutput) SetEndpointName(v string) *CreateDevEndpointOutput {
	s.EndpointName = &v
	return s
}
880215
func (e *Endpoint) ProxyID(l4 *policy.L4Filter) string {
	return policy.ProxyID(e.ID, l4.Ingress, string(l4.Protocol), uint16(l4.Port))
}
1154470
func DeleteEndpointID(stateDriver core.StateDriver, epID string) (*mastercfg.CfgEndpointState, error) {
	epCfg := &mastercfg.CfgEndpointState{}
	var epgCfg *mastercfg.EndpointGroupState

	epCfg.StateDriver = stateDriver
	err := epCfg.Read(epID)
	if err != nil {
		return nil, err
	}

	nwCfg := &mastercfg.CfgNetworkState{}
	nwCfg.StateDriver = stateDriver
	err = nwCfg.Read(epCfg.NetID)

	// Network may already be deleted if infra nw
	// If network present, free up nw resources
	if err == nil && epCfg.IPAddress != "" {
		if len(epCfg.ServiceName) > 0 {
			epgCfg = &mastercfg.EndpointGroupState{}
			epgCfg.StateDriver = stateDriver
			if err := epgCfg.Read(epCfg.ServiceName + ":" + nwCfg.Tenant); err != nil {
				log.Errorf("failed to read endpoint group %s, error %s",
					epCfg.ServiceName+":"+epgCfg.TenantName, err)
				return nil, err
			}
		}

		err = networkReleaseAddress(nwCfg, epgCfg, epCfg.IPAddress)
		if err != nil {
			log.Errorf("Error releasing endpoint state for: %s. Err: %v", epCfg.IPAddress, err)
		}

		if epCfg.EndpointGroupKey != "" {
			epgCfg := &mastercfg.EndpointGroupState{}
			epgCfg.StateDriver = stateDriver
			err = epgCfg.Read(epCfg.EndpointGroupKey)
			if err != nil {
				log.Errorf("Error reading EPG for endpoint: %+v", epCfg)
			}

			epgCfg.EpCount--

			// write updated epg state
			err = epgCfg.Write()
			if err != nil {
				log.Errorf("error writing epg config. Error: %s", err)
			}
		}

		// decrement ep count
		nwCfg.EpCount--

		// write modified nw state
		err = nwCfg.Write()
		if err != nil {
			log.Errorf("error writing nw config. Error: %s", err)
		}
	}

	// Even if network not present (already deleted), cleanup ep cfg
	err = epCfg.Clear()
	if err != nil {
		log.Errorf("error writing ep config. Error: %s", err)
		return nil, err
	}

	return epCfg, err
}
1320020
func (o *PatchEndpointIDParams) WithEndpoint(endpoint *models.EndpointChangeRequest) *PatchEndpointIDParams {
	o.SetEndpoint(endpoint)
	return o
}