Beispiel #1
0
// parseMulticastGroups parses an array of multicast group nested attributes
// into a slice of MulticastGroups.
func parseMulticastGroups(b []byte) ([]MulticastGroup, error) {
	attrs, err := netlink.UnmarshalAttributes(b)
	if err != nil {
		return nil, err
	}

	groups := make([]MulticastGroup, 0, len(attrs))
	for i, a := range attrs {
		// The type attribute is essentially an array index here; it starts
		// at 1 and should increment for each new array element
		if int(a.Type) != i+1 {
			return nil, errInvalidMulticastGroupArray
		}

		nattrs, err := netlink.UnmarshalAttributes(a.Data)
		if err != nil {
			return nil, err
		}

		var g MulticastGroup
		for _, na := range nattrs {
			switch na.Type {
			case attrMGName:
				g.Name = nlenc.String(na.Data)
			case attrMGID:
				g.ID = nlenc.Uint32(na.Data)
			}
		}

		groups = append(groups, g)
	}

	return groups, nil
}
Beispiel #2
0
// parseAttributes parses netlink attributes into a Family's fields.
func (f *Family) parseAttributes(attrs []netlink.Attribute) error {
	for _, a := range attrs {
		switch a.Type {
		case attrFamilyID:
			f.ID = nlenc.Uint16(a.Data)
		case attrFamilyName:
			f.Name = nlenc.String(a.Data)
		case attrVersion:
			v := nlenc.Uint32(a.Data)
			if v > math.MaxUint8 {
				return errInvalidFamilyVersion
			}

			f.Version = uint8(v)
		case attrMulticastGroups:
			groups, err := parseMulticastGroups(a.Data)
			if err != nil {
				return err
			}

			f.Groups = groups
		}
	}

	return nil
}
// parseAttributes parses netlink attributes into an Interface's fields.
func (ifi *Interface) parseAttributes(attrs []netlink.Attribute) error {
	for _, a := range attrs {
		switch a.Type {
		case nl80211.AttrIfindex:
			ifi.Index = int(nlenc.Uint32(a.Data))
		case nl80211.AttrIfname:
			ifi.Name = nlenc.String(a.Data)
		case nl80211.AttrMac:
			ifi.HardwareAddr = net.HardwareAddr(a.Data)
		case nl80211.AttrWiphy:
			ifi.PHY = int(nlenc.Uint32(a.Data))
		case nl80211.AttrIftype:
			// NOTE: InterfaceType copies the ordering of nl80211's interface type
			// constants.  This may not be the case on other operating systems.
			ifi.Type = InterfaceType(nlenc.Uint32(a.Data))
		case nl80211.AttrWdev:
			ifi.Device = int(nlenc.Uint64(a.Data))
		case nl80211.AttrWiphyFreq:
			ifi.Frequency = int(nlenc.Uint32(a.Data))
		}
	}

	return nil
}