Skip to content

Commit

Permalink
also specify IP directely
Browse files Browse the repository at this point in the history
  • Loading branch information
velnias75 committed Feb 28, 2020
1 parent f26bbca commit 15eee0a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ AC_CHECK_HEADERS([syslog.h])
AC_CHECK_HEADERS([sys/syscall.h])
AC_CHECK_HEADERS([sys/socket.h])
AC_CHECK_HEADERS([netinet/in.h])
AC_CHECK_HEADERS([arpa/inet.h])

# Checks for typedefs, structures, and compiler characteristics.
AX_CXX_GCC_ABI_DEMANGLE
Expand Down
15 changes: 5 additions & 10 deletions src/common/abstractsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <netdb.h> // for addrinfo, freeaddrinfo, etc
#endif

#if defined(HAVE_ARPA_INET_H) && !defined(NDEBUG)
#include <arpa/inet.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
Expand All @@ -48,8 +50,6 @@
#include <vector> // for vector
#include <stdbool.h>

#include <list>

#include "abstractsocket.h" // for AbstractSocket
#include "abstractsocketimpl.h" // for AbstractSocketImpl
#include "logger.h" // for logWarning
Expand Down Expand Up @@ -157,15 +157,10 @@ void AbstractSocket::connect(bool inetd) throw(Exception::SocketException) {
_pimpl->m_sfd, errno);
}

std::list<struct addrinfo *> al;

for(rp = result; rp != NULL; rp = rp->ai_next) al.push_front(rp);

for(std::list<struct addrinfo *>::iterator i = al.begin();
i != al.end(); ++i) {
for(rp = result; rp != NULL; rp = rp->ai_next) {

rp = *i;

#if defined(HAVE_ARPA_INET_H) && !defined(NDEBUG)
if(rp->ai_family == AF_INET6) {

char dst[INET6_ADDRSTRLEN + 1];
Expand All @@ -185,7 +180,7 @@ void AbstractSocket::connect(bool inetd) throw(Exception::SocketException) {
inet_ntop(AF_INET, addr, dst, INET_ADDRSTRLEN);
logDebug("trying addr: " << dst);
}

#endif
_pimpl->m_sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);

Expand Down
2 changes: 1 addition & 1 deletion src/common/abstractsocketimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,4 @@ void AbstractSocketImpl::logErrSocketOptions(unsigned char what) {
if(what & SOCKOPT_REUSEPORT) logWarning("SOCKOPT_REUSEPORT failed");
}

// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4;
// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4; remove-trailing-space: true;
2 changes: 1 addition & 1 deletion src/common/abstractsocketimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ class AbstractSocketImpl {

#endif /* NETMAUMAU_ABSTRACTSOCKETIMPL_H */

// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4;
// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4; remove-trailing-space: true;
29 changes: 26 additions & 3 deletions src/server/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include <netdb.h> // for getnameinfo, NI_NUMERICHOST
#endif

#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h> // for getuid, setegid, seteuid
#endif
Expand Down Expand Up @@ -92,6 +96,20 @@ void unknownSignal(int sig) {
#endif
}

bool isValidIP(const char *candidate) {

#if HAVE_ARPA_INET_H
char buf[INET6_ADDRSTRLEN];

if(::inet_pton(AF_INET, candidate, buf) ||
::inet_pton(AF_INET6, candidate, buf)) {
return true;
}
#endif

return false;
}

int addr4proto(struct ifaddrs *const ifas, std::set<std::string> &ifaces,
bool listOnly, const char *iface, char *addr, std::size_t len,
int family, std::size_t familyLen, const char *v) {
Expand Down Expand Up @@ -301,14 +319,19 @@ int getGroup(gid_t *gid, const char *group) {

int getIPForIF(bool ipv4, char *addr, size_t len, const char *iface) {

bool listOnly = !addr && !len && !iface;
const bool listOnly = !addr && !len && !iface;

if(!listOnly && isValidIP(iface)) {
::strcpy(addr, iface);
return 0;
}

std::set<std::string> ifaces;

struct ifaddrs *ifas;

if(!::getifaddrs(&ifas)) {

if(addr4proto(ifas, ifaces, listOnly, iface, addr, len,
!ipv4 ? AF_INET6 : AF_INET,
!ipv4 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in),
Expand Down Expand Up @@ -575,4 +598,4 @@ void disarmIdleTimer(timer_t timerid, struct itimerspec &its) {

}

// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4;
// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4; remove-trailing-space: true;
2 changes: 1 addition & 1 deletion src/server/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ void disarmIdleTimer(timer_t timerid, struct itimerspec &its);

#endif /* NETMAUMAU_HELPERS_H */

// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4;
// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4; remove-trailing-space: true;
19 changes: 14 additions & 5 deletions src/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
#include "ci_string.h"
#endif

#if HAVE_ARPA_INET_H
#define IF_IP_OPT "INTERFACE|IP"
#define IF_IP_HLP "INTERFACE/IP"
#else
#define IF_IP_OPT "INTERFACE"
#define IF_IP_HLP IF_IP_OPT
#endif

namespace {

const char *REFUSED = "refused";
Expand Down Expand Up @@ -145,11 +153,12 @@ poptOption poptOptions[] = {
"Put the server in a mode suitable for (x)inetd", NULL
},
#endif
{ "bind", 'b', POPT_ARG_STRING, &NetMauMau::host, 0, "Bind to HOST", "HOST" },
{ "bind", 'b', POPT_ARG_STRING, &NetMauMau::host, 0, "Bind webserver to HOST", "HOST" },
#ifndef _WIN32
{ NULL, '6', POPT_ARG_NONE, NULL, '6', "Bind to IPv6 address (default, must be set before -I)", NULL },
{ NULL, '4', POPT_ARG_NONE, NULL, '4', "Bind to IPv4 address (must be set before -I)", NULL },
{ "iface", 'I', POPT_ARG_STRING, &NetMauMau::interface, 'I', "Bind to INTERFACE, listen on any address of omitted", "INTERFACE" },
{ NULL, '6', POPT_ARG_NONE, NULL, '6', "Bind to IPv6 address (default, must be set before -I, ignored if IP is given)", NULL },
{ NULL, '4', POPT_ARG_NONE, NULL, '4', "Bind to IPv4 address (must be set before -I, ignored if IP is given)", NULL },
{ "iface", 'I', POPT_ARG_STRING, &NetMauMau::interface, 'I',
"Bind to " IF_IP_HLP ", listen on any IPv4/IPv6 address if omitted", IF_IP_OPT },
#endif
{
"port", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &NetMauMau::port, 0,
Expand Down Expand Up @@ -685,4 +694,4 @@ int main(int argc, const char **argv) {
return EXIT_SUCCESS;
}

// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4;
// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4; remove-trailing-space: true;

0 comments on commit 15eee0a

Please sign in to comment.