[go: up one dir, main page]

Menu

[r66]: / util / conf.cpp  Maximize  Restore  History

Download this file

267 lines (209 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*****************************************************************************
Copyright © 2006, 2007, The Board of Trustees of the University of Illinois.
All Rights Reserved.
Sector: A Distributed Storage and Computing Infrastructure
National Center for Data Mining (NCDM)
University of Illinois at Chicago
http://www.ncdm.uic.edu/
Sector is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
Sector is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu [gu@lac.uic.edu], last updated 02/23/2007
*****************************************************************************/
#include "conf.h"
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
using namespace cb;
int ConfParser::init(string path)
{
m_ConfFile.open(path.c_str());
if (m_ConfFile.bad())
return -1;
return 0;
}
void ConfParser::close()
{
m_ConfFile.close();
}
int ConfParser::getNextParam(Param& param)
{
//param format
// NAME
// < tab >value1
// < tab >value2
// < tab >...
// < tab >valuen
param.m_strName = "";
param.m_vstrValue.clear();
while (!m_ConfFile.eof())
{
char buf[1024];
string name;
m_ConfFile.getline(buf, 1024);
// skip blank lines
if (0 == strlen(buf))
continue;
// skip comments
if ('#' == buf[0])
continue;
// no blanks or tabs in front of name line
if ((' ' == buf[0]) || ('\t' == buf[0]))
return -1;
char* str = buf;
string token = "";
if (NULL == (str = getToken(str, token)))
continue;
param.m_strName = token;
// scan param values
while (!m_ConfFile.eof())
{
m_ConfFile.getline(buf, 1024);
if ('\t' != buf[0])
break;
str = buf;
if (NULL == (str = getToken(str, token)))
return -1;
param.m_vstrValue.insert(param.m_vstrValue.end(), token);
}
return 0;
}
return -1;
}
char* ConfParser::getToken(char* str, string& token)
{
char* p = str;
// skip blank spaces
while ((' ' == *p) || ('\t' == *p))
++ p;
// nothing here...
if ('\0' == *p)
return NULL;
token = "";
while ((' ' != *p) && ('\t' != *p) && ('\0' != *p))
{
token.append(1, *p);
++ p;
}
return p;
}
int SECTORParam::init(const string& path)
{
m_strDataDir = "../data/";
m_llMaxDataSize = 0;
m_iSECTORPort = 2237; // CBFS
m_iRouterPort = 24673; // CHORD
m_iDataPort = 8386; // UDTM
ConfParser parser;
Param param;
if (0 != parser.init(path))
{
cerr << "couldn't locate SETCOR configuration file. Please check " << path << endl;
return -1;
}
while (0 == parser.getNextParam(param))
{
if (param.m_vstrValue.empty())
continue;
if ("DATADIR" == param.m_strName)
m_strDataDir = param.m_vstrValue[0];
else if ("MAXDATASIZE" == param.m_strName)
m_llMaxDataSize = atoll(param.m_vstrValue[0].c_str());
else if ("SECTOR_PORT" == param.m_strName)
m_iSECTORPort = atoi(param.m_vstrValue[0].c_str());
else if ("ROUTER_PORT" == param.m_strName)
m_iRouterPort = atoi(param.m_vstrValue[0].c_str());
else if ("DATA_PORT" == param.m_strName)
m_iDataPort = atoi(param.m_vstrValue[0].c_str());
else if ("WRITE_ALLOWED" == param.m_strName)
{
for (vector<string>::iterator i = param.m_vstrValue.begin(); i != param.m_vstrValue.end(); ++ i)
m_IPSec.addIP(*i);
}
else if ("MAX_SPE_MEM" == param.m_strName)
m_iMaxSPEMem = atoi(param.m_vstrValue[0].c_str());
else
cerr << "unrecongnized system parameter: " << param.m_strName << endl;
}
parser.close();
return 0;
}
IPSec::IPSec()
{
m_vIPList.clear();
}
int IPSec::addIP(const string& ip)
{
char buf[64];
unsigned int i = 0;
for (unsigned int n = ip.length(); i < n; ++ i)
{
if ('/' == ip.c_str()[i])
break;
buf[i] = ip.c_str()[i];
}
buf[i] = '\0';
in_addr addr;
if (inet_pton(AF_INET, buf, &addr) < 0)
return -1;
IPRange entry;
entry.m_uiIP = addr.s_addr;
entry.m_uiMask = 0xFFFFFFFF;
if (i == ip.length())
{
m_vIPList.insert(m_vIPList.end(), entry);
return 0;
}
if ('/' != ip.c_str()[i])
return -1;
++ i;
bool format = false;
int j = 0;
for (unsigned int n = ip.length(); i < n; ++ i, ++ j)
{
if ('.' == ip.c_str()[i])
format = true;
buf[j] = ip.c_str()[i];
}
buf[j] = '\0';
if (format)
{
//255.255.255.0
if (inet_pton(AF_INET, buf, &addr) < 0)
return -1;
entry.m_uiMask = addr.s_addr;
}
else
{
char* p;
unsigned int bit = strtol(buf, &p, 10);
if ((p == buf) || (bit > 32) || (bit < 0))
return -1;
if (bit < 32)
entry.m_uiMask = ((unsigned int)1 << bit) - 1;
}
m_vIPList.insert(m_vIPList.end(), entry);
return 0;
}
bool IPSec::checkIP(const string& ip)
{
in_addr addr;
if (inet_pton(AF_INET, ip.c_str(), &addr) < 0)
return false;
for (vector<IPRange>::iterator i = m_vIPList.begin(); i != m_vIPList.end(); ++ i)
{
if ((addr.s_addr & i->m_uiMask) == (i->m_uiIP & i->m_uiMask))
return true;
}
return false;
}