# Path: shapy/framework/netlink/message.py
# class Attr(object):
#     """Represents a single attribute."""
#     #struct rtattr {
#     #   unsigned short rta_len;    /* Length of option */
#     #   unsigned short rta_type;   /* Type of option */
#     #   /* Data follows */
#     #};
#     
#     rtattr = Struct('HH')
#     
#     @classmethod
#     def unpack(cls, data):
#         length, type = cls.rtattr.unpack(data[:cls.rtattr.size])
#         data_len = length-cls.rtattr.size
#         attr_data = struct.unpack('{0}s'.format(data_len),
#                              data[cls.rtattr.size:cls.rtattr.size+data_len])[0]
#         return cls(type, attr_data), data[align(length):]
#     
#     def __init__(self, rta_type, payload):
#         self.rta_type = rta_type
#         self.payload = payload
#         self.data = self.unpack_data()
#         
#     def pack(self):
#         data = struct.pack('{0}s'.format(len(self.payload)), self.payload)
#         rta_len = self.rtattr.size+len(data)
#         return struct.pack('{0}{1}s'.format(self.rtattr.format, align(len(data))),
#                            rta_len, self.rta_type, data)
#     
#     def unpack_data(self):
#         if hasattr(self, 'data_format') and hasattr(self, 'data_struct'):
#             return self.data_struct._make(self.data_format.unpack(self.payload))
#         return None
#     
#     def __repr__(self):
#         return """<rtattr datalen=%d rta_type=%d data="%s">""" % (
#             align(len(self.payload)), self.rta_type,
#             self.payload.encode('string_escape'))


from shapy.framework.netlink.message import Attr
from .constants import *
from struct import Struct
        key = self.tc_u32_key.pack(mask, val, offset, offmask)
        data = sel+key
        super(u32_selector, self).__init__(TCA_U32_SEL, data)


class u32_mirred_action(Attr):
    """
    The basic action redirecting all matched traffic to the interface specified
    by ifindex using mirred strategy.
    """
    # include/linux/tc_act/tc_mirred.h
    ##define tc_gen \
    #    __u32                 index; \
    #    __u32                 capab; \
    #    int                   action; \
    #    int                   refcnt; \
    #    int                   bindcnt
    #struct tc_mirred {
    #    tc_gen;
    #    int                     eaction;   /* one of IN/EGRESS_MIRROR/REDIR */
    #    __u32                   ifindex;  /* ifindex of egress port */
    #};
    
    data_format = Struct("IIiiiiI")
    
    def __init__(self, ifindex):
        tc_mirred_parms = Attr(TCA_MIRRED_PARMS,
                               self.data_format.pack(0, 0, TCA_INGRESS_MIRROR, 0, 0,
                                                     TCA_EGRESS_REDIR, ifindex))
        kind = Attr(TCA_ACT_KIND, "mirred\0")

# Path: shapy/framework/netlink/message.py
# class Attr(object):
#     """Represents a single attribute."""
#     #struct rtattr {
#     #   unsigned short rta_len;    /* Length of option */
#     #   unsigned short rta_type;   /* Type of option */
#     #   /* Data follows */
#     #};
#     
#     rtattr = Struct('HH')
#     
#     @classmethod
#     def unpack(cls, data):
#         length, type = cls.rtattr.unpack(data[:cls.rtattr.size])
#         data_len = length-cls.rtattr.size
#         attr_data = struct.unpack('{0}s'.format(data_len),
#                              data[cls.rtattr.size:cls.rtattr.size+data_len])[0]
#         return cls(type, attr_data), data[align(length):]
#     
#     def __init__(self, rta_type, payload):
#         self.rta_type = rta_type
#         self.payload = payload
#         self.data = self.unpack_data()
#         
#     def pack(self):
#         data = struct.pack('{0}s'.format(len(self.payload)), self.payload)
#         rta_len = self.rtattr.size+len(data)
#         return struct.pack('{0}{1}s'.format(self.rtattr.format, align(len(data))),
#                            rta_len, self.rta_type, data)
#     
#     def unpack_data(self):
#         if hasattr(self, 'data_format') and hasattr(self, 'data_struct'):
#             return self.data_struct._make(self.data_format.unpack(self.payload))
#         return None
#     
#     def __repr__(self):
#         return """<rtattr datalen=%d rta_type=%d data="%s">""" % (
#             align(len(self.payload)), self.rta_type,
#             self.payload.encode('string_escape'))


# Path: shapy/framework/netlink/filter.py
from shapy.framework.netlink.message import Attr
from .constants import *
from struct import Struct

        key = self.tc_u32_key.pack(mask, val, offset, offmask)
        data = sel+key
        super(u32_selector, self).__init__(TCA_U32_SEL, data)


class u32_mirred_action(Attr):
    """
    The basic action redirecting all matched traffic to the interface specified
    by ifindex using mirred strategy.
    """
    # include/linux/tc_act/tc_mirred.h
    ##define tc_gen \
    #    __u32                 index; \
    #    __u32                 capab; \
    #    int                   action; \
    #    int                   refcnt; \
    #    int                   bindcnt
    #struct tc_mirred {
    #    tc_gen;
    #    int                     eaction;   /* one of IN/EGRESS_MIRROR/REDIR */
    #    __u32                   ifindex;  /* ifindex of egress port */
    #};
    
    data_format = Struct("IIiiiiI")
    
    def __init__(self, ifindex):
        tc_mirred_parms = Attr(TCA_MIRRED_PARMS,
                               self.data_format.pack(0, 0, TCA_INGRESS_MIRROR, 0, 0,
                                                     TCA_EGRESS_REDIR, ifindex))
        kind = Attr(TCA_ACT_KIND, "mirred\0")

        options = Attr(TCA_ACT_OPTIONS, tc_mirred_parms.pack())