Skip to content

Commit b7eadf9

Browse files
aryanA101akelson42
authored andcommitted
handle ip modes & add compilation flags for windows build
1 parent a6cf161 commit b7eadf9

File tree

8 files changed

+44
-57
lines changed

8 files changed

+44
-57
lines changed

include/common.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace kiwix {
1818

19+
enum class IpMode { ipv4, ipv6, all };
1920
typedef zim::size_type size_type;
2021
typedef zim::offset_type offset_type;
2122

include/server.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <string>
2424
#include <memory>
25+
#include "common.h"
2526

2627
namespace kiwix
2728
{
@@ -62,10 +63,10 @@ namespace kiwix
6263
{ m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; }
6364
void setBlockExternalLinks(bool blockExternalLinks)
6465
{ m_blockExternalLinks = blockExternalLinks; }
65-
void setIPv6(bool ipv6) { m_ipv6 = ipv6; }
66+
void setIpMode(IpMode mode) { m_ipMode = mode; }
6667
int getPort();
6768
std::string getAddress();
68-
bool isAddressIPv6();
69+
IpMode getIpMode() const;
6970

7071
protected:
7172
std::shared_ptr<Library> mp_library;
@@ -80,7 +81,7 @@ namespace kiwix
8081
bool m_withTaskbar = true;
8182
bool m_withLibraryButton = true;
8283
bool m_blockExternalLinks = false;
83-
bool m_ipv6 = false;
84+
IpMode m_ipMode = IpMode::ipv4;
8485
int m_ipConnectionLimit = 0;
8586
std::unique_ptr<InternalServer> mp_server;
8687
};

include/tools.h

+3-15
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
#include <map>
2626
#include <cstdint>
2727

28-
struct ip_addr{
28+
namespace kiwix {
29+
struct IpAddress{
2930
std::string addr;
3031
std::string addr6;
3132
};
3233

33-
namespace kiwix {
3434
typedef std::pair<std::string, std::string> LangNameCodePair;
3535
typedef std::vector<LangNameCodePair> FeedLanguages;
3636
typedef std::vector<std::string> FeedCategories;
@@ -220,23 +220,11 @@ bool fileReadable(const std::string& path);
220220
*/
221221
std::string getMimeTypeForFile(const std::string& filename);
222222

223-
/** Provides all available network interfaces on Windows
224-
*
225-
* This function provides the available IPv4 and IPv6 network interfaces
226-
*/
227-
std::map<std::string,ip_addr> getNetworkInterfacesWin();
228-
229-
/** Provides all available network interfaces on Posix
230-
*
231-
* This function provides the available IPv4 and IPv6 network interfaces
232-
*/
233-
std::map<std::string,ip_addr> getNetworkInterfacesPosix();
234-
235223
/** Provides all available network interfaces
236224
*
237225
* This function provides the available IPv4 and IPv6 network interfaces
238226
*/
239-
std::map<std::string,ip_addr> getNetworkInterfaces();
227+
std::map<std::string,IpAddress> getNetworkInterfaces();
240228

241229
/** Provides the best IP address
242230
* This function provides the best IP address from the list given by getNetworkInterfaces

meson.build

+4-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ if host_machine.system() == 'windows' and static_deps
4848
endif
4949

5050
if host_machine.system() == 'windows'
51-
add_project_arguments('-DNOMINMAX', language: 'cpp')
51+
add_project_arguments('-DNOMINMAX', language: 'cpp')
52+
extra_libs += ['-liphlpapi']
53+
else
54+
extra_link_args = []
5255
endif
5356

5457
all_deps = [thread_dep, libicu_dep, libzim_dep, pugixml_dep, libcurl_dep, microhttpd_dep, zlib_dep, xapian_dep]
@@ -58,12 +61,6 @@ inc = include_directories('include', extra_include)
5861
conf = configuration_data()
5962
conf.set('LIBKIWIX_VERSION', '"@0@"'.format(meson.project_version()))
6063

61-
if build_machine.system() == 'windows'
62-
extra_link_args = ['-lshlwapi', '-lwinmm']
63-
else
64-
extra_link_args = []
65-
endif
66-
6764
subdir('include')
6865
subdir('scripts')
6966
subdir('static')

src/server.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool Server::start() {
5151
m_withTaskbar,
5252
m_withLibraryButton,
5353
m_blockExternalLinks,
54-
m_ipv6,
54+
m_ipMode,
5555
m_indexTemplateString,
5656
m_ipConnectionLimit));
5757
return mp_server->start();
@@ -85,9 +85,9 @@ std::string Server::getAddress()
8585
return mp_server->getAddress();
8686
}
8787

88-
bool Server::isAddressIPv6()
88+
IpMode Server::getIpMode() const
8989
{
90-
return mp_server->isAddressIPv6();
90+
return mp_server->getIpMode();
9191
}
9292

9393
}

src/server/internalServer.cpp

+19-14
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ InternalServer::InternalServer(LibraryPtr library,
416416
bool withTaskbar,
417417
bool withLibraryButton,
418418
bool blockExternalLinks,
419-
bool ipv6,
419+
IpMode ipMode,
420420
std::string indexTemplateString,
421421
int ipConnectionLimit) :
422422
m_addr(addr),
@@ -429,7 +429,7 @@ InternalServer::InternalServer(LibraryPtr library,
429429
m_withTaskbar(withTaskbar),
430430
m_withLibraryButton(withLibraryButton),
431431
m_blockExternalLinks(blockExternalLinks),
432-
m_ipv6(ipv6),
432+
m_ipMode(ipMode),
433433
m_indexTemplateString(indexTemplateString.empty() ? RESOURCE::templates::index_html : indexTemplateString),
434434
m_ipConnectionLimit(ipConnectionLimit),
435435
mp_daemon(nullptr),
@@ -466,30 +466,35 @@ bool InternalServer::start() {
466466
sockAddr6.sin6_addr = in6addr_any;
467467
sockAddr4.sin_addr.s_addr = htonl(INADDR_ANY);
468468
}
469-
m_addr = kiwix::getBestPublicIp(m_ipv6);
470-
std::cout<<m_addr<<std::endl;
471-
} else if (inet_pton(AF_INET6, m_addr.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1 ) {
472-
m_ipv6 = true;
473-
} else if (inet_pton(AF_INET, m_addr.c_str(), &(sockAddr4.sin_addr.s_addr)) == 1) {
474-
if (m_ipv6) {
475-
std::cerr << "Ip address " << m_addr << " is not a valid ipv6 address" << std::endl;
469+
m_addr = kiwix::getBestPublicIp(m_ipMode == IpMode::ipv6 || m_ipMode == IpMode::all);
470+
} else {
471+
bool ipv6 = inet_pton(AF_INET6, m_addr.c_str(), &(sockAddr6.sin6_addr.s6_addr)) == 1;
472+
bool ipv4 = inet_pton(AF_INET, m_addr.c_str(), &(sockAddr4.sin_addr.s_addr)) == 1;
473+
if (ipv6){
474+
m_ipMode = IpMode::all;
475+
} else if (!ipv4) {
476+
std::cerr << "Ip address " << m_addr << " is not a valid ip address" << std::endl;
476477
return false;
477478
}
478-
} else {
479-
std::cerr << "Ip address " << m_addr << " is not a valid ip address" << std::endl;
480-
return false;
481479
}
482480

483-
if (m_ipv6)
481+
if (m_ipMode == IpMode::all) {
484482
flags|=MHD_USE_DUAL_STACK;
483+
} else if (m_ipMode == IpMode::ipv6) {
484+
flags|=MHD_USE_IPv6;
485+
}
486+
487+
struct sockaddr* sockaddr = (m_ipMode==IpMode::all || m_ipMode==IpMode::ipv6)
488+
? (struct sockaddr*)&sockAddr6
489+
: (struct sockaddr*)&sockAddr4;
485490

486491
mp_daemon = MHD_start_daemon(flags,
487492
m_port,
488493
NULL,
489494
NULL,
490495
&staticHandlerCallback,
491496
this,
492-
MHD_OPTION_SOCK_ADDR, m_ipv6?(struct sockaddr*)&sockAddr6:(struct sockaddr*)&sockAddr4,
497+
MHD_OPTION_SOCK_ADDR, sockaddr,
493498
MHD_OPTION_THREAD_POOL_SIZE, m_nbThreads,
494499
MHD_OPTION_PER_IP_CONNECTION_LIMIT, m_ipConnectionLimit,
495500
MHD_OPTION_END);

src/server/internalServer.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class InternalServer {
103103
bool withTaskbar,
104104
bool withLibraryButton,
105105
bool blockExternalLinks,
106-
bool ipv6,
106+
IpMode ipMode,
107107
std::string indexTemplateString,
108108
int ipConnectionLimit);
109109
virtual ~InternalServer();
@@ -119,7 +119,7 @@ class InternalServer {
119119
void stop();
120120
std::string getAddress() { return m_addr; }
121121
int getPort() { return m_port; }
122-
bool isAddressIPv6() { return m_ipv6; }
122+
IpMode getIpMode() const { return m_ipMode; }
123123

124124
private: // functions
125125
std::unique_ptr<Response> handle_request(const RequestContext& request);
@@ -176,7 +176,7 @@ class InternalServer {
176176
bool m_withTaskbar;
177177
bool m_withLibraryButton;
178178
bool m_blockExternalLinks;
179-
bool m_ipv6;
179+
IpMode m_ipMode;
180180
std::string m_indexTemplateString;
181181
int m_ipConnectionLimit;
182182
struct MHD_Daemon* mp_daemon;

src/tools/networkTools.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ std::string kiwix::download(const std::string& url) {
7979

8080
#ifdef _WIN32
8181

82-
std::map<std::string,ip_addr> kiwix::getNetworkInterfacesWin() {
83-
std::map<std::string,ip_addr> interfaces;
82+
std::map<std::string,kiwix::IpAddress> getNetworkInterfacesWin() {
83+
std::map<std::string,kiwix::IpAddress> interfaces;
8484

8585
const int working_buffer_size = 15000;
8686
const int max_tries = 3;
@@ -98,15 +98,13 @@ std::map<std::string,ip_addr> kiwix::getNetworkInterfacesWin() {
9898
// Successively allocate the required memory until GetAdaptersAddresses does not
9999
// results in ERROR_BUFFER_OVERFLOW for a maximum of max_tries
100100
do{
101-
102101
interfacesHead = (IP_ADAPTER_ADDRESSES *) malloc(outBufLen);
103102
if (interfacesHead == NULL) {
104103
std::cerr << "Memory allocation failed for IP_ADAPTER_ADDRESSES struct" << std::endl;
105104
return interfaces;
106105
}
107106

108107
dwRetVal = GetAdaptersAddresses(family, flags, NULL, interfacesHead, &outBufLen);
109-
110108
} while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < max_tries));
111109

112110
if (dwRetVal == NO_ERROR) {
@@ -147,8 +145,8 @@ std::map<std::string,ip_addr> kiwix::getNetworkInterfacesWin() {
147145

148146
#else
149147

150-
std::map<std::string,ip_addr> kiwix::getNetworkInterfacesPosix() {
151-
std::map<std::string,ip_addr> interfaces;
148+
std::map<std::string,kiwix::IpAddress> getNetworkInterfacesPosix() {
149+
std::map<std::string,kiwix::IpAddress> interfaces;
152150

153151
struct ifaddrs *interfacesHead;
154152
if (getifaddrs(&interfacesHead) == -1) {
@@ -179,20 +177,17 @@ std::map<std::string,ip_addr> kiwix::getNetworkInterfacesPosix() {
179177

180178
#endif
181179

182-
std::map<std::string,ip_addr> kiwix::getNetworkInterfaces() {
183-
std::map<std::string,ip_addr> interfaces;
184-
180+
std::map<std::string,kiwix::IpAddress> kiwix::getNetworkInterfaces() {
185181
#ifdef _WIN32
186182
return getNetworkInterfacesWin();
187183
#else
188184
return getNetworkInterfacesPosix();
189185
#endif
190-
191186
}
192187

193188
std::string kiwix::getBestPublicIp(bool ipv6) {
194-
ip_addr bestPublicIp = ip_addr{"127.0.0.1","::1"};
195-
std::map<std::string,ip_addr> interfaces = getNetworkInterfaces();
189+
kiwix::IpAddress bestPublicIp = kiwix::IpAddress{"127.0.0.1","::1"};
190+
std::map<std::string,kiwix::IpAddress> interfaces = getNetworkInterfaces();
196191

197192
#ifndef _WIN32
198193
const char* const prioritizedNames[] =

0 commit comments

Comments
 (0)