Simplify code

This commit is contained in:
Julian Kornberger
2018-09-30 22:42:25 +02:00
committed by Alessandro Boch
parent e137ed6e2c
commit aa5b058fc0
5 changed files with 10 additions and 28 deletions

View File

@@ -1524,7 +1524,8 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
base.Protinfo = parseProtinfo(attrs) protinfo := parseProtinfo(attrs)
base.Protinfo = &protinfo
} }
case unix.IFLA_OPERSTATE: case unix.IFLA_OPERSTATE:
base.OperState = LinkOperState(uint8(attr.Value[0])) base.OperState = LinkOperState(uint8(attr.Value[0]))

View File

@@ -371,16 +371,12 @@ func (req *NetlinkRequest) Serialize() []byte {
} }
func (req *NetlinkRequest) AddData(data NetlinkRequestData) { func (req *NetlinkRequest) AddData(data NetlinkRequestData) {
if data != nil { req.Data = append(req.Data, data)
req.Data = append(req.Data, data)
}
} }
// AddRawData adds raw bytes to the end of the NetlinkRequest object during serialization // AddRawData adds raw bytes to the end of the NetlinkRequest object during serialization
func (req *NetlinkRequest) AddRawData(data []byte) { func (req *NetlinkRequest) AddRawData(data []byte) {
if data != nil { req.RawData = append(req.RawData, data...)
req.RawData = append(req.RawData, data...)
}
} }
// Execute the request against a the given sockType. // Execute the request against a the given sockType.

View File

@@ -41,7 +41,7 @@ func (h *Handle) LinkGetProtinfo(link Link) (Protinfo, error) {
if err != nil { if err != nil {
return pi, err return pi, err
} }
pi = *parseProtinfo(infos) pi = parseProtinfo(infos)
return pi, nil return pi, nil
} }
@@ -49,8 +49,7 @@ func (h *Handle) LinkGetProtinfo(link Link) (Protinfo, error) {
return pi, fmt.Errorf("Device with index %d not found", base.Index) return pi, fmt.Errorf("Device with index %d not found", base.Index)
} }
func parseProtinfo(infos []syscall.NetlinkRouteAttr) *Protinfo { func parseProtinfo(infos []syscall.NetlinkRouteAttr) (pi Protinfo) {
var pi Protinfo
for _, info := range infos { for _, info := range infos {
switch info.Attr.Type { switch info.Attr.Type {
case nl.IFLA_BRPORT_MODE: case nl.IFLA_BRPORT_MODE:
@@ -71,5 +70,5 @@ func parseProtinfo(infos []syscall.NetlinkRouteAttr) *Protinfo {
pi.ProxyArpWiFi = byteToBool(info.Value[0]) pi.ProxyArpWiFi = byteToBool(info.Value[0])
} }
} }
return &pi return
} }

View File

@@ -199,12 +199,7 @@ func (h *Handle) xfrmPolicyGetOrDelete(policy *XfrmPolicy, nlProto int) (*XfrmPo
return nil, err return nil, err
} }
p, err := parseXfrmPolicy(msgs[0], FAMILY_ALL) return parseXfrmPolicy(msgs[0], FAMILY_ALL)
if err != nil {
return nil, err
}
return p, nil
} }
func parseXfrmPolicy(m []byte, family int) (*XfrmPolicy, error) { func parseXfrmPolicy(m []byte, family int) (*XfrmPolicy, error) {

View File

@@ -184,12 +184,7 @@ func (h *Handle) xfrmStateAllocSpi(state *XfrmState) (*XfrmState, error) {
return nil, err return nil, err
} }
s, err := parseXfrmState(msgs[0], FAMILY_ALL) return parseXfrmState(msgs[0], FAMILY_ALL)
if err != nil {
return nil, err
}
return s, err
} }
// XfrmStateDel will delete an xfrm state from the system. Note that // XfrmStateDel will delete an xfrm state from the system. Note that
@@ -394,11 +389,7 @@ func (h *Handle) XfrmStateFlush(proto Proto) error {
req.AddData(&nl.XfrmUsersaFlush{Proto: uint8(proto)}) req.AddData(&nl.XfrmUsersaFlush{Proto: uint8(proto)})
_, err := req.Execute(unix.NETLINK_XFRM, 0) _, err := req.Execute(unix.NETLINK_XFRM, 0)
if err != nil { return err
return err
}
return nil
} }
func limitsToLft(lmts XfrmStateLimits, lft *nl.XfrmLifetimeCfg) { func limitsToLft(lmts XfrmStateLimits, lft *nl.XfrmLifetimeCfg) {