github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/aclprovider/ipsetprovider_windows.go (about)

     1  // +build windows
     2  
     3  package provider
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"go.uber.org/zap"
     9  
    10  	"github.com/aporeto-inc/go-ipset/ipset"
    11  	"go.aporeto.io/trireme-lib/utils/frontman"
    12  )
    13  
    14  // IpsetProvider returns a fabric for Ipset.
    15  type IpsetProvider interface {
    16  	NewIpset(name string, ipsetType string, p *ipset.Params) (Ipset, error)
    17  	GetIpset(name string) Ipset
    18  	DestroyAll(prefix string) error
    19  	ListIPSets() ([]string, error)
    20  }
    21  
    22  // Ipset is an abstraction of all the methods an implementation of userspace
    23  // ipsets need to provide.
    24  type Ipset interface {
    25  	Add(entry string, timeout int) error
    26  	AddOption(entry string, option string, timeout int) error
    27  	Del(entry string) error
    28  	Destroy() error
    29  	Flush() error
    30  	Test(entry string) (bool, error)
    31  }
    32  
    33  type ipsetProvider struct{}
    34  
    35  type winIPSet struct {
    36  	handle uintptr
    37  	name   string // for debugging
    38  }
    39  
    40  // NewIpset returns an IpsetProvider interface based on the go-ipset
    41  // external package.
    42  func (i *ipsetProvider) NewIpset(name string, ipsetType string, p *ipset.Params) (Ipset, error) {
    43  	ipsetHandle, err := frontman.Wrapper.NewIpset(name, ipsetType)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return &winIPSet{ipsetHandle, name}, nil
    48  }
    49  
    50  // GetIpset gets the ipset object from the name.
    51  // Note that the interface can't return error here, but since it's possible to fail in Windows,
    52  // we log error and return incomplete object, and expect a failure from Frontman on a later call.
    53  func (i *ipsetProvider) GetIpset(name string) Ipset {
    54  	ipsetHandle, err := frontman.Wrapper.GetIpset(name)
    55  	if err != nil {
    56  		zap.L().Error(fmt.Sprintf("failed to get ipset %s", name), zap.Error(err))
    57  		return &winIPSet{0, name}
    58  	}
    59  	return &winIPSet{ipsetHandle, name}
    60  }
    61  
    62  // DestroyAll destroys all the ipsets - it will fail if there are existing references
    63  func (i *ipsetProvider) DestroyAll(prefix string) error {
    64  	return frontman.Wrapper.DestroyAllIpsets(prefix)
    65  }
    66  
    67  func (i *ipsetProvider) ListIPSets() ([]string, error) {
    68  	return frontman.Wrapper.ListIpsets()
    69  }
    70  
    71  // NewGoIPsetProvider Return a Go IPSet Provider
    72  func NewGoIPsetProvider() IpsetProvider {
    73  	return &ipsetProvider{}
    74  }
    75  
    76  func (w *winIPSet) Add(entry string, timeout int) error {
    77  	return frontman.Wrapper.IpsetAdd(w.handle, entry, timeout)
    78  }
    79  
    80  func (w *winIPSet) AddOption(entry string, option string, timeout int) error {
    81  	return frontman.Wrapper.IpsetAddOption(w.handle, entry, option, timeout)
    82  }
    83  
    84  func (w *winIPSet) Del(entry string) error {
    85  	return frontman.Wrapper.IpsetDelete(w.handle, entry)
    86  }
    87  
    88  func (w *winIPSet) Destroy() error {
    89  	return frontman.Wrapper.IpsetDestroy(w.handle)
    90  }
    91  
    92  func (w *winIPSet) Flush() error {
    93  	return frontman.Wrapper.IpsetFlush(w.handle)
    94  }
    95  
    96  func (w *winIPSet) Test(entry string) (bool, error) {
    97  	return frontman.Wrapper.IpsetTest(w.handle, entry)
    98  }