netlink-musl.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef NETLINK_MUSL_H
  2. #define NETLINK_MUSL_H
  3. #if !__linux__
  4. #error netlink-musl.h only works with a linux kernel
  5. #endif
  6. #if __ANDROID__
  7. #error netlink-musl.h does not work with Android
  8. #endif
  9. #include <stdint.h>
  10. /* linux/netlink.h */
  11. #define NETLINK_ROUTE 0
  12. struct nlmsghdr {
  13. uint32_t nlmsg_len;
  14. uint16_t nlmsg_type;
  15. uint16_t nlmsg_flags;
  16. uint32_t nlmsg_seq;
  17. uint32_t nlmsg_pid;
  18. };
  19. #define NLM_F_REQUEST 1
  20. #define NLM_F_MULTI 2
  21. #define NLM_F_ACK 4
  22. #define NLM_F_ROOT 0x100
  23. #define NLM_F_MATCH 0x200
  24. #define NLM_F_ATOMIC 0x400
  25. #define NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH)
  26. #define NLMSG_NOOP 0x1
  27. #define NLMSG_ERROR 0x2
  28. #define NLMSG_DONE 0x3
  29. #define NLMSG_OVERRUN 0x4
  30. /* linux/rtnetlink.h */
  31. #define RTM_NEWLINK 16
  32. #define RTM_GETLINK 18
  33. #define RTM_NEWADDR 20
  34. #define RTM_GETADDR 22
  35. struct rtattr {
  36. unsigned short rta_len;
  37. unsigned short rta_type;
  38. };
  39. struct rtgenmsg {
  40. unsigned char rtgen_family;
  41. };
  42. struct ifinfomsg {
  43. unsigned char ifi_family;
  44. unsigned char __ifi_pad;
  45. unsigned short ifi_type;
  46. int ifi_index;
  47. unsigned ifi_flags;
  48. unsigned ifi_change;
  49. };
  50. /* linux/if_link.h */
  51. #define IFLA_ADDRESS 1
  52. #define IFLA_BROADCAST 2
  53. #define IFLA_IFNAME 3
  54. #define IFLA_STATS 7
  55. /* linux/if_addr.h */
  56. struct ifaddrmsg {
  57. uint8_t ifa_family;
  58. uint8_t ifa_prefixlen;
  59. uint8_t ifa_flags;
  60. uint8_t ifa_scope;
  61. uint32_t ifa_index;
  62. };
  63. #define IFA_ADDRESS 1
  64. #define IFA_LOCAL 2
  65. #define IFA_LABEL 3
  66. #define IFA_BROADCAST 4
  67. /* musl */
  68. #define NETLINK_ALIGN(len) (((len)+3) & ~3)
  69. #define NLMSG_DATA(nlh) ((void*)((char*)(nlh)+sizeof(struct nlmsghdr)))
  70. #define NLMSG_DATALEN(nlh) ((nlh)->nlmsg_len-sizeof(struct nlmsghdr))
  71. #define NLMSG_DATAEND(nlh) ((char*)(nlh)+(nlh)->nlmsg_len)
  72. #define NLMSG_NEXT(nlh) (struct nlmsghdr*)((char*)(nlh)+NETLINK_ALIGN((nlh)->nlmsg_len))
  73. #define NLMSG_OK(nlh,end) ((char*)(end)-(char*)(nlh) >= sizeof(struct nlmsghdr))
  74. #define RTA_DATA(rta) ((void*)((char*)(rta)+sizeof(struct rtattr)))
  75. #define RTA_DATALEN(rta) ((rta)->rta_len-sizeof(struct rtattr))
  76. #define RTA_DATAEND(rta) ((char*)(rta)+(rta)->rta_len)
  77. #define RTA_NEXT(rta) (struct rtattr*)((char*)(rta)+NETLINK_ALIGN((rta)->rta_len))
  78. #define RTA_OK(nlh,end) ((char*)(end)-(char*)(rta) >= sizeof(struct rtattr))
  79. #define NLMSG_RTA(nlh,len) ((void*)((char*)(nlh)+sizeof(struct nlmsghdr)+NETLINK_ALIGN(len)))
  80. #define NLMSG_RTAOK(rta,nlh) RTA_OK(rta,NLMSG_DATAEND(nlh))
  81. int __rtnetlink_enumerate(int link_af, int addr_af, int (*cb)(void *ctx, struct nlmsghdr *h), void *ctx);
  82. #endif // NETLINK_MUSL_H