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 bool NRMapParser::ReadFile(const std::string & fileName)
40 std::ifstream source(fileName.c_str());
44 errorStr = "Error opening file ";
46 printfd(__FILE__, "NRMapParser::ReadFile(): %s\n", errorStr.c_str());
52 std::vector<NET_ROUTER> _nrmap;
54 while (getline(source, line))
64 if (ParseLine(line, nr))
66 printfd(__FILE__, "NRMapParser::ReadFile(): Error parsing line %d: '%s'\n", lineNumber, errorStr.c_str());
78 bool NRMapParser::ParseLine(const std::string & line, NET_ROUTER & nr) const
80 // xxx.xxx.xxx.xxx/yy zzz.zzz.zzz.zzz
81 size_t pos = line.find_first_of(" \t");
83 if (pos == std::string::npos)
85 errorStr = "No space between subnet and router";
89 std::string subnet(line.substr(0, pos)); // xxx.xxx.xxx.xxx/yy
94 if (ParseNet(subnet, ip, mask))
100 nr.subnetMask = mask;
102 pos = line.find_first_not_of(" \t", pos);
104 if (pos == std::string::npos)
106 errorStr = "No router address found";
110 size_t pos2 = line.find_first_of(" \t", pos);
112 std::string router(line.substr(pos, pos2 == std::string::npos ? line.length() - pos2 - 1 : pos2 - pos)); //zzz.zzz.zzz.zzz
116 if (ParseRouter(router, routerIP))
121 std::vector<uint32_t>::iterator it;
123 it = std::lower_bound(
128 nr.routers.insert(it, routerIP);
130 //nr.routers.push_back(routerIP);
132 while (pos2 != std::string::npos)
134 pos = line.find_first_not_of(" \t", pos2);
136 if (pos == std::string::npos)
141 pos2 = line.find_first_of(" \t", pos);
143 if (ParseRouter(line.substr(
145 pos2 == std::string::npos ? line.length() - pos2 - 1 : pos2 - pos),
151 it = std::lower_bound(
156 nr.routers.insert(it, routerIP);
158 //nr.routers.push_back(routerIP);
165 bool NRMapParser::ParseNet(const std::string & line, uint32_t & ip, uint32_t & mask) const
167 // xxx.xxx.xxx.xxx/yy
169 size_t pos = line.find_first_of('/');
171 if (pos == std::string::npos)
173 errorStr = "Subnet is not in CIDR notation";
177 int res = inet_pton(AF_INET, line.substr(0, pos).c_str(), &ip); //xxx.xxx.xxx.xxx
181 errorStr = strerror(errno);
186 errorStr = "Invalid subnet address";
190 if (str2x(line.substr(pos + 1, line.length() - pos - 1), mask)) //yy
192 errorStr = "Invalid subnet mask";
197 errorStr = "Subnet mask is out of range [0..32]";
200 mask = htonl(0xffFFffFF << (32 - mask)); //bitmask
205 bool NRMapParser::ParseRouter(const std::string & line, uint32_t & ip) const
207 int res = inet_pton(AF_INET, line.c_str(), &ip); //zzz.zzz.zzz.zzz
211 errorStr = strerror(errno);
216 printfd(__FILE__, "NRMapParser::ParseRouter(): IP '%s' is invalid\n", line.c_str());
217 errorStr = "Invalid router address";