github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/common/service.go (about)

     1  package common
     2  
     3  import (
     4  	"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
     5  	"go.aporeto.io/enforcerd/trireme-lib/utils/portspec"
     6  )
     7  
     8  // Service is a protocol/port service of interest - used to pass user requests
     9  type Service struct {
    10  	// Ports are the corresponding ports
    11  	Ports *portspec.PortSpec `json:"ports,omitempty"`
    12  
    13  	// Port is the service port. This has been deprecated and will be removed in later releases 01/13/2018
    14  	Port uint16
    15  
    16  	// Protocol is the protocol number
    17  	Protocol uint8 `json:"protocol,omitempty"`
    18  
    19  	// Addresses are the IP addresses. An empty list means 0.0.0.0/0
    20  	Addresses map[string]struct{} `json:"addresses,omitempty"`
    21  
    22  	// FQDNs is the list of FQDNs for the service.
    23  	FQDNs []string `json:"fqdns,omitempty"`
    24  }
    25  
    26  // ConvertServicesToPortList converts an array of services to a port list
    27  func ConvertServicesToPortList(services []Service) string {
    28  
    29  	portlist := ""
    30  	for _, s := range services {
    31  		portlist = portlist + s.Ports.String() + ","
    32  	}
    33  
    34  	if len(portlist) == 0 {
    35  		portlist = "0"
    36  	} else {
    37  		portlist = portlist[:len(portlist)-1]
    38  	}
    39  
    40  	return portlist
    41  }
    42  
    43  // ConvertServicesToProtocolPortList converts an array of services to tcp/udp port list
    44  func ConvertServicesToProtocolPortList(services []Service) (string, string) {
    45  
    46  	tcpPortlist := ""
    47  	udpPortlist := ""
    48  	for _, s := range services {
    49  		if s.Protocol == packet.IPProtocolTCP {
    50  			tcpPortlist = tcpPortlist + s.Ports.String() + ","
    51  		} else {
    52  			udpPortlist = udpPortlist + s.Ports.String() + ","
    53  		}
    54  	}
    55  
    56  	if len(tcpPortlist) == 0 {
    57  		tcpPortlist = "0"
    58  	} else {
    59  		tcpPortlist = tcpPortlist[:len(tcpPortlist)-1]
    60  	}
    61  
    62  	if len(udpPortlist) == 0 {
    63  		udpPortlist = "0"
    64  	} else {
    65  		udpPortlist = udpPortlist[:len(udpPortlist)-1]
    66  	}
    67  
    68  	return tcpPortlist, udpPortlist
    69  }