interfaces.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // interfaces.h :
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // https://manpages.debian.org/stretch/ifupdown/interfaces.5.en.html
  6. // https://wiki.debian.org/NetworkConfiguration
  7. // https://wiki.ubuntuusers.de/interfaces/
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10. #if !defined(AGD_INTERFACES_H__0F16A7C6_9054_4BBB_8A10_EAE00ED4EE9C__INCLUDED_)
  11. #define AGD_INTERFACES_H__0F16A7C6_9054_4BBB_8A10_EAE00ED4EE9C__INCLUDED_
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <linux/if_packet.h>
  18. #include <net/if_arp.h>
  19. #include <vector>
  20. #include <list>
  21. #include <string>
  22. #include "inet4s.h"
  23. #include "inet4d.h"
  24. #include "inet4m.h"
  25. /////////////////////////////////////////////////////////////////////////////
  26. // interfaces.h - Declarations:
  27. extern unsigned long g_nItfID;
  28. /////////////////////////////////////////////////////////////////////////////
  29. typedef struct _ITF_CONFIG_GROUP
  30. {
  31. _ITF_CONFIG_GROUP(){_reset();}
  32. void _reset(void)
  33. {
  34. cfgName.clear();
  35. members.clear();
  36. }
  37. std::string cfgName;
  38. std::vector<std::string> members;
  39. }ITF_CONFIG_GROUP, *LPITF_CONFIG_GROUP;
  40. typedef const ITF_CONFIG_GROUP *LPCITF_CONFIG_GROUP;
  41. typedef std::list<ITF_CONFIG_GROUP> ITF_CONFIG_GROUP_LIST;
  42. typedef enum CfgGroup
  43. {
  44. CG_Unknown = -1,
  45. CG_Auto,
  46. CG_AllowAuto,
  47. CG_AllowHotplug,
  48. CG_NoAutoDown,
  49. CG_NoScripts,
  50. CG_User
  51. }CfgGroup;
  52. CfgGroup ParseCfgGroup(const std::vector<std::string> &v, ITF_CONFIG_GROUP &icg);
  53. CfgGroup GetIfaceCfgGroup(const std::string &s);
  54. const char *GetIfaceCfgGroupStr(CfgGroup cg);
  55. typedef void (*PFN_ENUM_IFACE_CFG_GROUPS_CALLBACK)(CfgGroup, void*);
  56. /////////////////////////////////////////////////////////////////////////////
  57. typedef enum IfaceProtos
  58. {
  59. IFP_Unknown = -1,
  60. IFP_Inet,
  61. IFP_Inet6,
  62. IFP_Ipx,
  63. IFP_Can
  64. }IfaceProtos;
  65. IfaceProtos GetIfaceProto(const std::string &s);
  66. const char *GetIfaceProtoStr(IfaceProtos ip);
  67. /////////////////////////////////////////////////////////////////////////////
  68. typedef enum IfaceMethods
  69. {
  70. IFM_Unknown = -1,
  71. IFM_Static,
  72. IFM_Dhcp,
  73. IFM_Manual,
  74. IFM_BootP,
  75. IFM_Tunnel,
  76. IFM_Ppp,
  77. IFM_WvDial,
  78. IFM_IpV4ll,
  79. IFM_Loopback,
  80. IFM_Auto
  81. }IfaceMethods;
  82. IfaceMethods GetIfaceMethod(const std::string &s);
  83. const char *GetIfaceMethodStr(IfaceMethods im);
  84. /////////////////////////////////////////////////////////////////////////////
  85. typedef enum IfaceCommands
  86. {
  87. IFC_Unknown = -1,
  88. IFC_PreUp,
  89. IFC_Up,
  90. IFC_PostUp,
  91. IFC_Down,
  92. IFC_PreDown,
  93. IFC_PostDown
  94. }IfaceCommands;
  95. typedef struct _IFACE_COMMAND
  96. {
  97. IfaceCommands cmd;
  98. std::string args;
  99. }IFACE_COMMAND, *LPIFACE_COMMAND;
  100. typedef const IFACE_COMMAND *LPCIFACE_COMMAND;
  101. IfaceCommands GetIfaceCommand(const std::string &s);
  102. const char *GetIfaceCommandsStr(IfaceCommands ic);
  103. bool ParseIfaceCmd(const std::vector<std::string> &v, std::vector<IFACE_COMMAND> &c);
  104. /////////////////////////////////////////////////////////////////////////////
  105. typedef enum ItfInclude
  106. {
  107. II_None = -1,
  108. II_File,
  109. II_Directory
  110. }ItfInclude;
  111. typedef struct _ITF_INCLUDE
  112. {
  113. ItfInclude inc;
  114. std::string path;
  115. }ITF_INCLUDE, *LPITF_INCLUDE;
  116. typedef const ITF_INCLUDE *LPCITF_INCLUDE;
  117. typedef std::list<ITF_INCLUDE> ITF_INCLUDE_LIST;
  118. bool ParseIncludes(const std::vector<std::string> &v, ITF_INCLUDE_LIST &inc);
  119. const char *GetIncTypeStr(ItfInclude inc);
  120. /////////////////////////////////////////////////////////////////////////////
  121. typedef struct _ITF_IFACE_BLOCK
  122. {
  123. _ITF_IFACE_BLOCK(){_reset();}
  124. void _reset(void)
  125. {
  126. cfgName.clear();
  127. proto = IFP_Unknown;
  128. method = IFM_Unknown;
  129. inherits.clear();
  130. desc.clear();
  131. cmds.clear();
  132. unparsed.clear();
  133. inet4s._reset();
  134. inet4d._reset();
  135. inet4m._reset();
  136. id = ++g_nItfID;
  137. up = running = false;
  138. }
  139. unsigned long id;
  140. std::string cfgName;
  141. IfaceProtos proto;
  142. IfaceMethods method;
  143. std::string inherits;
  144. std::string desc;
  145. std::vector<IFACE_COMMAND> cmds;
  146. std::vector<std::string> unparsed;
  147. IFACE_INET_STATIC inet4s;
  148. IFACE_INET_DHCP inet4d;
  149. IFACE_INET_MANUAL inet4m;
  150. bool up;
  151. bool running;
  152. }ITF_IFACE_BLOCK, *LPITF_IFACE_BLOCK;
  153. typedef const ITF_IFACE_BLOCK *LPCITF_IFACE_BLOCK;
  154. typedef std::list<ITF_IFACE_BLOCK> ITF_IFACE_BLOCK_LIST;
  155. typedef enum IfaceHdrType
  156. {
  157. IHR_NoHdr,
  158. IHR_Invalid,
  159. IHR_Unknown,
  160. IHR_OK
  161. }IfaceHdrType;
  162. IfaceHdrType ParseIfaceHeader(const std::vector<std::string> &v, ITF_IFACE_BLOCK &ib);
  163. bool ParseIfaceParam(const std::vector<std::string> &v, ITF_IFACE_BLOCK &ib);
  164. bool ParseIfaceDesc(const std::vector<std::string> &v, ITF_IFACE_BLOCK &ib);
  165. /////////////////////////////////////////////////////////////////////////////
  166. typedef struct _ITF_MAPPING_BLOCK
  167. {
  168. _ITF_MAPPING_BLOCK(){_reset();}
  169. void _reset(void)
  170. {
  171. unparsed.clear();
  172. }
  173. std::vector<std::string> unparsed;
  174. }ITF_MAPPING_BLOCK, *LPITF_MAPPING_BLOCK;
  175. typedef const ITF_MAPPING_BLOCK *LPCITF_MAPPING_BLOCK;
  176. typedef std::list<ITF_MAPPING_BLOCK> ITF_MAPPING_BLOCK_LIST;
  177. bool ParseMapping(const std::vector<std::string> &v, ITF_MAPPING_BLOCK &mab);
  178. /////////////////////////////////////////////////////////////////////////////
  179. typedef struct _ETC_NETWORK_INTERFACES
  180. {
  181. _ETC_NETWORK_INTERFACES(void){_reset();}
  182. void _reset(void)
  183. {
  184. mbl.clear();
  185. cgl.clear();
  186. ibl.clear();
  187. inc.clear();
  188. unparsed.clear();
  189. }
  190. ITF_MAPPING_BLOCK_LIST mbl;
  191. ITF_CONFIG_GROUP_LIST cgl;
  192. ITF_IFACE_BLOCK_LIST ibl;
  193. ITF_INCLUDE_LIST inc;
  194. std::vector<std::string> unparsed;
  195. }ETC_NETWORK_INTERFACES, *LPETC_NETWORK_INTERFACES;
  196. typedef const ETC_NETWORK_INTERFACES *LPCETC_NETWORK_INTERFACES;
  197. typedef void (*PFN_IF_UPDOWN_COMPLETE)(int, void*);
  198. typedef void (*PFN_IF_UPDOWN_OUTPUT)(const char*, void*);
  199. typedef struct _IF_UPDOWN_ASYNC_PARAMS
  200. {
  201. int fd;
  202. pid_t pid;
  203. PFN_IF_UPDOWN_COMPLETE pfnComplete;
  204. void *ctxComplete;
  205. PFN_IF_UPDOWN_OUTPUT pfnCallback;
  206. void *ctxOutput;
  207. }IF_UPDOWN_ASYNC_PARAMS, *LPIF_UPDOWN_ASYNC_PARAMS;
  208. typedef const IF_UPDOWN_ASYNC_PARAMS *LPCIF_UPDOWN_ASYNC_PARAMS;
  209. LPITF_IFACE_BLOCK GetInterfaceBlockByID(ETC_NETWORK_INTERFACES &eni, unsigned long id);
  210. void ProcessIface(ETC_NETWORK_INTERFACES &eni);
  211. void ProcessGroupMembers(ETC_NETWORK_INTERFACES &eni);
  212. bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath = NULL);
  213. bool WriteEtcNetworkInterfaces(const ETC_NETWORK_INTERFACES &eni, const char *pszPath = NULL);
  214. bool RemoveInterfaceBlock(ETC_NETWORK_INTERFACES &eni, unsigned long id);
  215. bool AddInterfaceToCfgGroup(ETC_NETWORK_INTERFACES &eni, unsigned long id, CfgGroup cg);
  216. bool RemoveInterfaceFromCfgGroup(ETC_NETWORK_INTERFACES &eni, unsigned long id, CfgGroup cg);
  217. CfgGroup EnumInterfaceCfgGroups(ETC_NETWORK_INTERFACES &eni, unsigned long id, PFN_ENUM_IFACE_CFG_GROUPS_CALLBACK pfnClb, void *pCtx);
  218. int IfUp(const char *pszItfName, PFN_IF_UPDOWN_OUTPUT pfnCallback = NULL, void *ctx = NULL);
  219. int IfDown(const char *pszItfName, PFN_IF_UPDOWN_OUTPUT pfnCallback = NULL, void *ctx = NULL);
  220. int IfUpAsync(const char *pszItfName, pid_t &pid, PFN_IF_UPDOWN_COMPLETE pfnComplete, void *ctxComplete = NULL, PFN_IF_UPDOWN_OUTPUT pfnCallback = NULL, void *ctxOutput = NULL);
  221. int IfDownAsync(const char *pszItfName, pid_t &pid, PFN_IF_UPDOWN_COMPLETE pfnComplete, void *ctxComplete = NULL, PFN_IF_UPDOWN_OUTPUT pfnCallback = NULL, void *ctxOutput = NULL);
  222. /////////////////////////////////////////////////////////////////////////////
  223. #endif // !defined(AGD_INTERFACES_H__0F16A7C6_9054_4BBB_8A10_EAE00ED4EE9C__INCLUDED_)