|
@@ -3,8 +3,8 @@
|
|
|
#include "util.h"
|
|
|
#include "interfaces.h"
|
|
|
|
|
|
-//#define _ETC_NETWORK_INTERFACES "/etc/network/interfaces"
|
|
|
-#define _ETC_NETWORK_INTERFACES "/home/wrk/share/gfanet/libgfanet/res/interfaces"
|
|
|
+#define _ETC_NETWORK_INTERFACES "/etc/network/interfaces"
|
|
|
+//#define _ETC_NETWORK_INTERFACES "/home/wrk/share/gfanet/libgfanet/res/interfaces"
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
// iface block
|
|
@@ -58,27 +58,34 @@ static const char *g_pszIncludes[] =
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
-static const char *_GetIfaceProtoStr(IfaceProtos ip)
|
|
|
+const char *GetIfaceProtoStr(IfaceProtos ip)
|
|
|
{
|
|
|
if(ip >= IFP_Inet && ip <= IFP_Can)
|
|
|
return g_pszProtos[ip];
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
-static const char *_GetIfaceMethodStr(IfaceMethods im)
|
|
|
+const char *GetIfaceMethodStr(IfaceMethods im)
|
|
|
{
|
|
|
if(im >= IFM_Static && im <= IFM_Auto)
|
|
|
return g_pszMethods[im];
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
-static const char *_GetIfaceCommandsStr(IfaceCommands ic)
|
|
|
+const char *GetIfaceCommandsStr(IfaceCommands ic)
|
|
|
{
|
|
|
if(ic >= IFC_PreUp && ic <= IFC_PostDown)
|
|
|
return g_pszCommands[ic];
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
+const char *GetIncTypeStr(ItfInclude inc)
|
|
|
+{
|
|
|
+ if(inc >= II_File && inc <= II_Directory)
|
|
|
+ return g_pszIncludes[inc];
|
|
|
+ return "";
|
|
|
+}
|
|
|
+
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
CfgGroup ParseCfgGroup(const std::vector<std::string> &v, ITF_CONFIG_GROUP &icg)
|
|
@@ -359,7 +366,7 @@ void ProcessGroupMembers(ETC_NETWORK_INTERFACES &eni)
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
-int ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni)
|
|
|
+bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath)
|
|
|
{
|
|
|
typedef enum ParseStates
|
|
|
{
|
|
@@ -373,10 +380,13 @@ int ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni)
|
|
|
static const std::regex regc(strRegExComt, std::regex_constants::ECMAScript | std::regex_constants::optimize);
|
|
|
static const std::regex regl(strRegExLine, std::regex_constants::ECMAScript | std::regex_constants::optimize);
|
|
|
std::string buf;
|
|
|
+
|
|
|
+ if(!pszPath)
|
|
|
+ pszPath = _ETC_NETWORK_INTERFACES;
|
|
|
|
|
|
eni._reset();
|
|
|
|
|
|
- if(ReadFile(_ETC_NETWORK_INTERFACES, buf) > 0)
|
|
|
+ if(ReadFile(pszPath, buf) > 0)
|
|
|
{
|
|
|
ParseStates ps = PS_Root;
|
|
|
std::string line;
|
|
@@ -446,21 +456,22 @@ int ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni)
|
|
|
|
|
|
ProcessIface(eni);
|
|
|
ProcessGroupMembers(eni);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
-bool WriteEtcNetworkInterfaces(const ETC_NETWORK_INTERFACES &eni, const char *pszFile)
|
|
|
+bool WriteEtcNetworkInterfaces(const ETC_NETWORK_INTERFACES &eni, const char *pszPath)
|
|
|
{
|
|
|
- if(!pszFile)
|
|
|
- pszFile = _ETC_NETWORK_INTERFACES;
|
|
|
+ if(!pszPath)
|
|
|
+ pszPath = _ETC_NETWORK_INTERFACES;
|
|
|
|
|
|
int line;
|
|
|
- FILE *pf = fopen(pszFile, "wb");
|
|
|
+ FILE *pf = fopen(pszPath, "wb");
|
|
|
|
|
|
if(pf)
|
|
|
{
|
|
@@ -545,18 +556,18 @@ bool WriteEtcNetworkInterfaces(const ETC_NETWORK_INTERFACES &eni, const char *ps
|
|
|
if(ib.proto != IFP_Unknown)
|
|
|
{
|
|
|
fputc(' ', pf);
|
|
|
- fputs(_GetIfaceProtoStr(ib.proto), pf);
|
|
|
+ fputs(GetIfaceProtoStr(ib.proto), pf);
|
|
|
}
|
|
|
if(ib.method != IFM_Unknown)
|
|
|
{
|
|
|
fputc(' ', pf);
|
|
|
- fputs(_GetIfaceMethodStr(ib.method), pf);
|
|
|
+ fputs(GetIfaceMethodStr(ib.method), pf);
|
|
|
}
|
|
|
fprintf(pf, " inherits %s\n", ib.inheritedFrom.c_str());
|
|
|
|
|
|
}
|
|
|
else
|
|
|
- fprintf(pf, "iface %s %s %s\n", ib.cfgName.c_str(), _GetIfaceProtoStr(ib.proto), _GetIfaceMethodStr(ib.method));
|
|
|
+ fprintf(pf, "iface %s %s %s\n", ib.cfgName.c_str(), GetIfaceProtoStr(ib.proto), GetIfaceMethodStr(ib.method));
|
|
|
|
|
|
if(ib.proto == IFP_Inet)
|
|
|
{
|
|
@@ -583,7 +594,7 @@ bool WriteEtcNetworkInterfaces(const ETC_NETWORK_INTERFACES &eni, const char *ps
|
|
|
{
|
|
|
const IFACE_COMMAND &cmd = *itu;
|
|
|
fputc('\t', pf);
|
|
|
- fputs(_GetIfaceCommandsStr(cmd.cmd), pf);
|
|
|
+ fputs(GetIfaceCommandsStr(cmd.cmd), pf);
|
|
|
fputc(' ', pf);
|
|
|
fputs(cmd.args.c_str(), pf);
|
|
|
fputc('\n', pf);
|
|
@@ -605,9 +616,33 @@ bool WriteEtcNetworkInterfaces(const ETC_NETWORK_INTERFACES &eni, const char *ps
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /////////////////////////////////////////////////////////////////////
|
|
|
+ // write includes
|
|
|
+
|
|
|
+ if(eni.inc.size() > 0)
|
|
|
+ {
|
|
|
+ fputs("##############################################################\n", pf);
|
|
|
+ fputs("# includes\n\n", pf);
|
|
|
+
|
|
|
+ for(auto iti = eni.inc.begin(); iti != eni.inc.end(); iti++)
|
|
|
+ {
|
|
|
+ const ITF_INCLUDE &inc = *iti;
|
|
|
+
|
|
|
+ if(inc.inc == II_File || inc.inc == II_Directory)
|
|
|
+ {
|
|
|
+ fputs(GetIncTypeStr(inc.inc), pf);
|
|
|
+ fputc(' ', pf);
|
|
|
+ fputs(inc.path.c_str(), pf);
|
|
|
+ fputc('\n', pf);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fputc('\n', pf);
|
|
|
+ }
|
|
|
+
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
fclose(pf);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
return false;
|