PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/libfwbuilder-4.1.2/src/fwbuilder/HostsFile.cpp

#
C++ | 179 lines | 131 code | 14 blank | 34 comment | 24 complexity | 9127383b16642f39f8470306020b6331 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Firewall Builder
  3. Copyright (C) 2001 NetCitadel, LLC
  4. Author: Vadim Zaliva lord@crocodile.org
  5. $Id: HostsFile.cpp 170 2008-08-13 06:27:20Z vadim $
  6. This program is free software which we release under the GNU General Public
  7. License. You may redistribute and/or modify this program under the terms
  8. of that license as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. To get a copy of the GNU General Public License, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <fwbuilder/libfwbuilder-config.h>
  18. #include <fwbuilder/HostsFile.h>
  19. #include <fwbuilder/Tools.h>
  20. #include <iostream>
  21. #include <fstream>
  22. #include <sstream>
  23. using namespace std;
  24. using namespace libfwbuilder;
  25. void HostsFile::parse(const string &filename) throw(FWException)
  26. {
  27. ifstream f(filename.c_str(), ios::in);
  28. if(!f)
  29. throw FWException("Can't open file '"+filename+"'");
  30. else
  31. parse(f);
  32. }
  33. /**
  34. * Does actuall parsing.
  35. */
  36. void HostsFile::parse(istream &from) throw(FWException)
  37. {
  38. enum
  39. {
  40. s_ip,
  41. s_iws,
  42. s_ws,
  43. s_name,
  44. s_rest
  45. } state;
  46. state = s_iws;
  47. InetAddr ip ;
  48. string name ;
  49. vector<string> names ;
  50. char c ;
  51. int ln = 1;
  52. while(from.get(c))
  53. {
  54. switch(state)
  55. {
  56. case s_iws:
  57. if(c=='#')
  58. {
  59. state=s_rest;
  60. break;
  61. } else if(c!='\n')
  62. {
  63. state=s_ip;
  64. } else
  65. break;
  66. case s_ip:
  67. if(c=='#')
  68. {
  69. ostringstream err;
  70. err << "Comment started in IP address field at line " << ln;
  71. throw FWException(err.str());
  72. } else if(c==' ' || c=='\t')
  73. {
  74. // if IP address is invalid, it will
  75. // throw exception here.
  76. try
  77. {
  78. ip = InetAddr(name);
  79. names.clear();
  80. name="";
  81. state=s_ws;
  82. } catch(FWNotSupportedException &v6)
  83. {
  84. // IPv6 addresses are not supported
  85. // thus, we just skip it.
  86. state=s_rest;
  87. }
  88. } else
  89. {
  90. name+=c;
  91. }
  92. break;
  93. case s_ws:
  94. if(c=='#')
  95. {
  96. // IP ends with comment without name
  97. if(names.empty())
  98. {
  99. ostringstream err;
  100. err << "Address: '" << name
  101. << "' without host name at line " << ln;
  102. throw FWException(err.str());
  103. } else
  104. {
  105. state=s_rest;
  106. break;
  107. }
  108. } else if(c==' ' || c=='\t')
  109. {
  110. break;
  111. } else if(c=='\n')
  112. {
  113. if(names.empty())
  114. {
  115. ostringstream err;
  116. err << "Address: '" << name
  117. << "' without host name at line " << ln;
  118. throw FWException(err.str());
  119. }
  120. } else
  121. {
  122. state=s_name;
  123. name = "";
  124. }
  125. //Fallthrough
  126. case s_name:
  127. if(c==' ' || c=='\t' || c=='#' || c=='\n')
  128. {
  129. names.push_back(name);
  130. name="";
  131. if(c=='#')
  132. {
  133. data[ip] = names;
  134. state=s_rest;
  135. } else if(c=='\n')
  136. {
  137. data[ip] = names;
  138. state=s_iws;
  139. }
  140. break;
  141. } else
  142. {
  143. name+=c;
  144. break;
  145. }
  146. case s_rest:
  147. if(c=='\n')
  148. {
  149. ln++;
  150. name = "";
  151. state = s_iws;
  152. }
  153. break;
  154. }
  155. }
  156. }