# Path: shapy/framework/utils.py
# def align(l, alignto=4):
#     """Aligned length to nearest multiple of 4."""
#     return (l + alignto - 1) & ~(alignto - 1)
# 
# class InterpreterMixin(object):
#     interpreters = {}
#     
#     @classmethod
#     def register(cls, interpreter, content_types):
#         for type in content_types:
#             cls.interpreters.update({type: interpreter})
#     
#     @classmethod
#     def select(cls, selector):
#         """Selects interpreter object based on the selector."""
#         return cls.interpreters[selector]


import quopri, binascii
import struct
from struct import Struct
from shapy.framework.utils import align, InterpreterMixin
from .constants import *
    #   __u32 nlmsg_len;    /* Length of message including header. */
    #   __u16 nlmsg_type;   /* Type of message content. */
    #   __u16 nlmsg_flags;  /* Additional flags. */
    #   __u32 nlmsg_seq;    /* Sequence number. */
    #   __u32 nlmsg_pid;    /* PID of the sending process. */
    #};
    
    nlmsghdr = Struct("IHHII")
    
    @classmethod
    def unpack(cls, msg):
        """Unpack raw bytes into a Netlink message."""
        mlength, type, flags, seq, pid = cls.nlmsghdr.unpack(msg[:cls.nlmsghdr.size])
        st = None
        if msg[cls.nlmsghdr.size:] > 0:
            st_cls = ServiceTemplate.select(type)
            st = st_cls.unpack(msg[cls.nlmsghdr.size:])
        return Message(type, flags, seq, service_template=st)
    
    def __init__(self, type, flags=0, seq=-1, service_template=None):
        """Used for creating Netlink messages."""
        self.type = type
        self.flags = flags
        self.seq = seq
        self.pid = 0
        self.service_template = service_template
        self.service_template.message = self

    def __len__(self):
        """Aligned length of service template message + attributes."""

# Path: shapy/framework/utils.py
# def align(l, alignto=4):
#     """Aligned length to nearest multiple of 4."""
#     return (l + alignto - 1) & ~(alignto - 1)
# 
# class InterpreterMixin(object):
#     interpreters = {}
#     
#     @classmethod
#     def register(cls, interpreter, content_types):
#         for type in content_types:
#             cls.interpreters.update({type: interpreter})
#     
#     @classmethod
#     def select(cls, selector):
#         """Selects interpreter object based on the selector."""
#         return cls.interpreters[selector]


# Path: shapy/framework/netlink/message.py
import quopri, binascii
import struct
from struct import Struct
from shapy.framework.utils import align, InterpreterMixin
from .constants import *

    #   __u32 nlmsg_len;    /* Length of message including header. */
    #   __u16 nlmsg_type;   /* Type of message content. */
    #   __u16 nlmsg_flags;  /* Additional flags. */
    #   __u32 nlmsg_seq;    /* Sequence number. */
    #   __u32 nlmsg_pid;    /* PID of the sending process. */
    #};
    
    nlmsghdr = Struct("IHHII")
    
    @classmethod
    def unpack(cls, msg):
        """Unpack raw bytes into a Netlink message."""
        mlength, type, flags, seq, pid = cls.nlmsghdr.unpack(msg[:cls.nlmsghdr.size])
        st = None
        if msg[cls.nlmsghdr.size:] > 0:
            st_cls = ServiceTemplate.select(type)
            st = st_cls.unpack(msg[cls.nlmsghdr.size:])
        return Message(type, flags, seq, service_template=st)
    
    def __init__(self, type, flags=0, seq=-1, service_template=None):
        """Used for creating Netlink messages."""
        self.type = type
        self.flags = flags
        self.seq = seq
        self.pid = 0
        self.service_template = service_template
        self.service_template.message = self

    def __len__(self):
        """Aligned length of service template message + attributes."""

        return align(self.nlmsghdr.size + len(self.payload))