// Pack Service to a set of nlattrs. // If full is given, include service settings, otherwise only the identifying fields are given. func (self *Service) attrs(full bool) nlgo.AttrSlice { var attrs nlgo.AttrSlice if self.FwMark != 0 { attrs = append(attrs, nlattr(IPVS_SVC_ATTR_AF, nlgo.U16(self.Af)), nlattr(IPVS_SVC_ATTR_FWMARK, nlgo.U32(self.FwMark)), ) } else if self.Protocol != 0 && self.Addr != nil && self.Port != 0 { attrs = append(attrs, nlattr(IPVS_SVC_ATTR_AF, nlgo.U16(self.Af)), nlattr(IPVS_SVC_ATTR_PROTOCOL, nlgo.U16(self.Protocol)), nlattr(IPVS_SVC_ATTR_ADDR, packAddr(self.Af, self.Addr)), nlattr(IPVS_SVC_ATTR_PORT, packPort(self.Port)), ) } else { panic("Incomplete service id fields") } if full { attrs = append(attrs, nlattr(IPVS_SVC_ATTR_SCHED_NAME, nlgo.NulString(self.SchedName)), nlattr(IPVS_SVC_ATTR_FLAGS, pack(&self.Flags)), nlattr(IPVS_SVC_ATTR_TIMEOUT, nlgo.U32(self.Timeout)), nlattr(IPVS_SVC_ATTR_NETMASK, nlgo.U32(self.Netmask)), ) } return attrs }
func TestDest(t *testing.T) { testService := Service{ Af: syscall.AF_INET6, } testDest := Dest{ Addr: net.ParseIP("2001:db8:6b:6b::0"), Port: 1337, FwdMethod: IP_VS_CONN_F_TUNNEL, Weight: 10, UThresh: 1000, LThresh: 0, } testAttrs := nlgo.AttrSlice{ nlattr(IPVS_DEST_ATTR_ADDR, nlgo.Binary([]byte{0x20, 0x01, 0x0d, 0xb8, 0x00, 0x6b, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), nlattr(IPVS_DEST_ATTR_PORT, nlgo.U16(0x3905)), nlattr(IPVS_DEST_ATTR_FWD_METHOD, nlgo.U32(IP_VS_CONN_F_TUNNEL)), nlattr(IPVS_DEST_ATTR_WEIGHT, nlgo.U32(10)), nlattr(IPVS_DEST_ATTR_U_THRESH, nlgo.U32(1000)), nlattr(IPVS_DEST_ATTR_L_THRESH, nlgo.U32(0)), } // pack packAttrs := testDest.attrs(&testService, true) packBytes := packAttrs.Bytes() if !bytes.Equal(packBytes, testAttrs.Bytes()) { t.Errorf("fail Dest.attrs(): \n%s", hex.Dump(packBytes)) } // unpack if unpackedAttrs, err := ipvs_dest_policy.Parse(packBytes); err != nil { t.Fatalf("error ipvs_dest_policy.Parse: %s", err) } else if unpackedDest, err := unpackDest(testService, unpackedAttrs.(nlgo.AttrMap)); err != nil { t.Fatalf("error unpackDest: %s", err) } else { testDestEquals(t, testDest, unpackedDest) } }
func packPort(port uint16) nlgo.U16 { return nlgo.U16(htons(port)) }