2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
24 $Date: 2009/10/22 09:58:53 $
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
35 #include "stg/common.h"
36 #include "nrmap_parser.h"
38 NRMapParser::NRMapParser()
42 NRMapParser::~NRMapParser()
46 bool NRMapParser::ReadFile(const std::string & fileName)
48 std::ifstream source(fileName.c_str());
52 errorStr = "Error opening file ";
54 printfd(__FILE__, "NRMapParser::ReadFile(): %s\n", errorStr.c_str());
60 std::vector<NET_ROUTER> _nrmap;
62 while (getline(source, line))
72 if (ParseLine(line, nr))
74 printfd(__FILE__, "NRMapParser::ReadFile(): Error parsing line %d: '%s'\n", lineNumber, errorStr.c_str());
86 bool NRMapParser::ParseLine(const std::string & line, NET_ROUTER & nr) const
88 // xxx.xxx.xxx.xxx/yy zzz.zzz.zzz.zzz
89 size_t pos = line.find_first_of(" \t");
91 if (pos == std::string::npos)
93 errorStr = "No space between subnet and router";
97 std::string subnet(line.substr(0, pos)); // xxx.xxx.xxx.xxx/yy
102 if (ParseNet(subnet, ip, mask))
108 nr.subnetMask = mask;
110 pos = line.find_first_not_of(" \t", pos);
112 if (pos == std::string::npos)
114 errorStr = "No router address found";
118 size_t pos2 = line.find_first_of(" \t", pos);
120 std::string router(line.substr(pos, pos2 == std::string::npos ? line.length() - pos2 - 1 : pos2 - pos)); //zzz.zzz.zzz.zzz
124 if (ParseRouter(router, routerIP))
129 std::vector<uint32_t>::iterator it;
131 it = std::lower_bound(
136 nr.routers.insert(it, routerIP);
138 //nr.routers.push_back(routerIP);
140 while (pos2 != std::string::npos)
142 pos = line.find_first_not_of(" \t", pos2);
144 if (pos == std::string::npos)
149 pos2 = line.find_first_of(" \t", pos);
151 if (ParseRouter(line.substr(
153 pos2 == std::string::npos ? line.length() - pos2 - 1 : pos2 - pos),
159 it = std::lower_bound(
164 nr.routers.insert(it, routerIP);
166 //nr.routers.push_back(routerIP);
173 bool NRMapParser::ParseNet(const std::string & line, uint32_t & ip, uint32_t & mask) const
175 // xxx.xxx.xxx.xxx/yy
177 size_t pos = line.find_first_of('/');
179 if (pos == std::string::npos)
181 errorStr = "Subnet is not in CIDR notation";
185 int res = inet_pton(AF_INET, line.substr(0, pos).c_str(), &ip); //xxx.xxx.xxx.xxx
189 errorStr = strerror(errno);
194 errorStr = "Invalid subnet address";
198 if (str2x(line.substr(pos + 1, line.length() - pos - 1), mask)) //yy
200 errorStr = "Invalid subnet mask";
205 errorStr = "Subnet mask is out of range [0..32]";
208 mask = htonl(0xffFFffFF << (32 - mask)); //bitmask
213 bool NRMapParser::ParseRouter(const std::string & line, uint32_t & ip) const
215 int res = inet_pton(AF_INET, line.c_str(), &ip); //zzz.zzz.zzz.zzz
219 errorStr = strerror(errno);
224 printfd(__FILE__, "NRMapParser::ParseRouter(): IP '%s' is invalid\n", line.c_str());
225 errorStr = "Invalid router address";