align.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package netlink
  2. import "unsafe"
  3. // Functions and values used to properly align netlink messages, headers,
  4. // and attributes. Definitions taken from Linux kernel source.
  5. // #define NLMSG_ALIGNTO 4U
  6. const nlmsgAlignTo = 4
  7. // #define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
  8. func nlmsgAlign(len int) int {
  9. return ((len) + nlmsgAlignTo - 1) & ^(nlmsgAlignTo - 1)
  10. }
  11. // #define NLMSG_LENGTH(len) ((len) + NLMSG_HDRLEN)
  12. func nlmsgLength(len int) int {
  13. return len + nlmsgHeaderLen
  14. }
  15. // #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
  16. var nlmsgHeaderLen = nlmsgAlign(int(unsafe.Sizeof(Header{})))
  17. // #define NLA_ALIGNTO 4
  18. const nlaAlignTo = 4
  19. // #define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
  20. func nlaAlign(len int) int {
  21. return ((len) + nlaAlignTo - 1) & ^(nlaAlignTo - 1)
  22. }
  23. // Because this package's Attribute type contains a byte slice, unsafe.Sizeof
  24. // can't be used to determine the correct length.
  25. const sizeofAttribute = 4
  26. // #define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr)))
  27. var nlaHeaderLen = nlaAlign(sizeofAttribute)