forked from Tencent/phxsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_config.cpp
More file actions
187 lines (148 loc) · 5.37 KB
/
Copy pathbase_config.cpp
File metadata and controls
187 lines (148 loc) · 5.37 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
/*
Tencent is pleased to support the open source community by making PhxSQL available.
Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the GNU General Public License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/GPL-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#include "phxcomm/base_config.h"
#include "phxcomm/phx_log.h"
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
using std::string;
namespace phxsql {
char *PhxBaseConfig::InitInnerIP() {
int sock_fd;
static char addr[32] = { "" };
char tmp_addr[32] = { "" };
struct in_addr my_addr[2];
struct ifreq stIfreq[10];
const char *pInnerList[] = { "10.0.0.0", "10.255.255.255", "172.16.0.0", "172.31.255.255", "192.168.0.0",
"192.168.255.255", "169.254.0.0", "169.254.255.255" };
if (-1 == (sock_fd = socket(PF_INET, SOCK_DGRAM, 0))) {
return addr;
}
struct ifconf ifc;
memset(&ifc, 0, sizeof(ifc));
ifc.ifc_len = sizeof(stIfreq);
ifc.ifc_req = stIfreq;
ioctl(sock_fd, SIOCGIFCONF, &ifc);
bool is_eth1 = false;
bool is_get_innerip = false;
for (size_t i = 0; i < ifc.ifc_len / sizeof(ifreq); i++) {
sockaddr_in staddr;
memcpy(&staddr, &ifc.ifc_req[i].ifr_addr, sizeof(staddr));
if (0 == strncasecmp(ifc.ifc_req[i].ifr_name, "eth1", strlen("eth1"))) {
is_eth1 = true;
}
if (!is_eth1 && is_get_innerip) {
continue;
}
for (size_t j = 0; j < sizeof(pInnerList) / sizeof(pInnerList[0]) / 2; j++) {
inet_aton(pInnerList[j * 2], &my_addr[0]);
my_addr[0].s_addr = htonl(my_addr[0].s_addr);
inet_aton(pInnerList[j * 2 + 1], &my_addr[1]);
my_addr[1].s_addr = htonl(my_addr[1].s_addr);
unsigned long lTmp = htonl(staddr.sin_addr.s_addr);
if (lTmp >= my_addr[0].s_addr && lTmp <= my_addr[1].s_addr) {
if (is_eth1) {
inet_ntop(AF_INET, &staddr.sin_addr, addr, sizeof(addr));
close(sock_fd);
return addr;
}
is_get_innerip = true;
memset(tmp_addr, 0x0, sizeof(tmp_addr));
inet_ntop(AF_INET, &staddr.sin_addr, tmp_addr, sizeof(tmp_addr));
}
}
is_eth1 = false;
}
memcpy(addr, tmp_addr, sizeof(tmp_addr));
close(sock_fd);
return addr;
}
PhxBaseConfig::PhxBaseConfig() {
}
int PhxBaseConfig::ReadFileWithConfigDirPath(const string &filename) {
return RealReadFile(filename);
}
int PhxBaseConfig::ReadFile(const string &filename) {
return RealReadFile(GetDefaultPath() + filename);
}
int PhxBaseConfig::RealReadFile(const string &filename) {
if (INIReader::ReadFile(filename.c_str()) == 0) {
LogWarning("%s read path %s done", __func__, filename.c_str());
ReadConfig();
return 0;
}
LogWarning("%s read path fail %s, ret %d, error %s", __func__, filename.c_str(), INIReader::ParseError(),
strerror(errno));
return 1;
}
string PhxBaseConfig::Get(const string §ion, const string &name, const string &default_value) {
return INIReader::Get(section, name, default_value);
}
int PhxBaseConfig::GetInteger(const string §ion, const string &name, const int &default_value) {
return INIReader::GetInteger(section, name, default_value);
}
string PhxBaseConfig::inner_ip_;
//get local ip
string PhxBaseConfig::GetInnerIP() {
if (inner_ip_ == "") {
inner_ip_ = InitInnerIP();
}
return inner_ip_;
}
string PhxBaseConfig::s_default_path;
string PhxBaseConfig::GetBasePath() {
static char szTemp[512] = { 0 };
char pszExeName[512] = { 0 };
int iFD = open("/proc/self/cmdline", O_RDONLY, 0);
if (iFD < 0)
return pszExeName;
if (read(iFD, szTemp, sizeof(szTemp) - 1) < 0) {
close(iFD);
return "";
}
szTemp[511] = 0;
close(iFD);
string path = szTemp;
char real_path[128];
realpath(path.c_str(), real_path);
path = real_path;
size_t pos = path.rfind("sbin/");
if (pos == string::npos) {
size_t pos = path.rfind("phxsql/");
if (pos == string::npos) {
return "../";
} else {
return path.substr(0, pos + 7);
}
} else {
return path.substr(0, pos);
}
}
//set the default path, where config files are located
void PhxBaseConfig::SetDefaultPath(const char *config_path) {
s_default_path = config_path;
}
//get the default path, where config files are located
string PhxBaseConfig::GetDefaultPath() {
if (s_default_path == "") {
s_default_path = PhxBaseConfig::GetBasePath() + "etc/";
LogWarning("%s get debuf path %s", __func__, s_default_path.c_str());
}
return s_default_path;
}
}