Minor code cleanup

Fixed compilation issue on OSX
This commit is contained in:
Luca Deri 2018-06-06 09:26:08 +02:00
parent 21055550f3
commit 415f6b0a72
6 changed files with 1272 additions and 1235 deletions

80
edge.c
View File

@ -476,13 +476,13 @@ static void help() {
"-c <community> " "-c <community> "
"[-k <encrypt key> | -K <key file>] " "[-k <encrypt key> | -K <key file>] "
"[-s <netmask>] " "[-s <netmask>] "
#if defined(N2N_HAVE_SETUID) #ifndef WIN32
"[-u <uid> -g <gid>]" "[-u <uid> -g <gid>]"
#endif /* #ifndef N2N_HAVE_SETUID */ #endif /* #ifndef WIN32 */
#if defined(N2N_HAVE_DAEMON) #ifndef WIN32
"[-f]" "[-f]"
#endif /* #if defined(N2N_HAVE_DAEMON) */ #endif /* #ifndef WIN32 */
"[-m <MAC address>]" "[-m <MAC address>]"
"\n" "\n"
"-l <supernode host:port> " "-l <supernode host:port> "
@ -506,9 +506,9 @@ static void help() {
printf("-u <UID> | User ID (numeric) to use when privileges are dropped.\n"); printf("-u <UID> | User ID (numeric) to use when privileges are dropped.\n");
printf("-g <GID> | Group ID (numeric) to use when privileges are dropped.\n"); printf("-g <GID> | Group ID (numeric) to use when privileges are dropped.\n");
#endif /* ifndef WIN32 */ #endif /* ifndef WIN32 */
#ifdef N2N_HAVE_DAEMON #ifndef WIN32
printf("-f | Do not fork and run as a daemon; rather run in foreground.\n"); printf("-f | Do not fork and run as a daemon; rather run in foreground.\n");
#endif /* #ifdef N2N_HAVE_DAEMON */ #endif /* #ifndef WIN32 */
printf("-m <MAC address> | Fix MAC address for the TAP interface (otherwise it may be random)\n" printf("-m <MAC address> | Fix MAC address for the TAP interface (otherwise it may be random)\n"
" : eg. -m 01:02:03:04:05:06\n"); " : eg. -m 01:02:03:04:05:06\n");
printf("-M <mtu> | Specify n2n MTU of edge interface (default %d).\n", DEFAULT_MTU); printf("-M <mtu> | Specify n2n MTU of edge interface (default %d).\n", DEFAULT_MTU);
@ -1383,7 +1383,7 @@ static int handle_PACKET( n2n_edge_t * eee,
{ {
uint8_t decodebuf[N2N_PKT_BUF_SIZE]; uint8_t decodebuf[N2N_PKT_BUF_SIZE];
size_t eth_size; size_t eth_size;
size_t rx_transop_idx=0; int rx_transop_idx;
rx_transop_idx = transop_enum_to_index(pkt->transform); rx_transop_idx = transop_enum_to_index(pkt->transform);
@ -1925,6 +1925,55 @@ static int run_loop(n2n_edge_t * eee );
#define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/ #define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/
#define N2N_IF_MODE_SIZE 16 /* static | dhcp */ #define N2N_IF_MODE_SIZE 16 /* static | dhcp */
/* *************************************************** */
void daemonize() {
#ifndef WIN32
int childpid;
traceEvent(TRACE_NORMAL, "Parent process is exiting (this is normal)");
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
if((childpid = fork()) < 0)
traceEvent(TRACE_ERROR, "Occurred while daemonizing (errno=%d)",
errno);
else {
if(!childpid) { /* child */
int rc;
//traceEvent(TRACE_NORMAL, "Bye bye: I'm becoming a daemon...");
rc = chdir("/");
if(rc != 0)
traceEvent(TRACE_ERROR, "Error while moving to / directory");
setsid(); /* detach from the terminal */
fclose(stdin);
fclose(stdout);
/* fclose(stderr); */
/*
* clear any inherited file mode creation mask
*/
//umask(0);
/*
* Use line buffered stdout
*/
/* setlinebuf (stdout); */
setvbuf(stdout, (char *)NULL, _IOLBF, 0);
} else /* father */
exit(0);
}
#endif
}
/* *************************************************** */
/** Entry point to program from kernel. */ /** Entry point to program from kernel. */
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -2017,8 +2066,7 @@ int main(int argc, char* argv[])
optarg = NULL; optarg = NULL;
while((opt = getopt_long(effectiveargc, while((opt = getopt_long(effectiveargc,
effectiveargv, effectiveargv,
"K:k:a:bc:Eu:g:m:M:s:d:l:p:fvhrt:", long_options, NULL)) != EOF) "K:k:a:bc:Eu:g:m:M:s:d:l:p:fvhrt:", long_options, NULL)) != EOF) {
{
switch (opt) switch (opt)
{ {
case'K': case'K':
@ -2069,13 +2117,13 @@ int main(int argc, char* argv[])
break; break;
} }
#endif #endif
#ifdef N2N_HAVE_DAEMON #ifndef WIN32
case 'f' : /* do not fork as daemon */ case 'f' : /* do not fork as daemon */
{ {
eee.daemon=0; eee.daemon=0;
break; break;
} }
#endif /* #ifdef N2N_HAVE_DAEMON */ #endif /* #ifndef WIN32 */
case 'm' : /* TUNTAP MAC address */ case 'm' : /* TUNTAP MAC address */
{ {
@ -2176,17 +2224,13 @@ int main(int argc, char* argv[])
} }
#ifdef N2N_HAVE_DAEMON #ifndef WIN32
if(eee.daemon) if(eee.daemon)
{ {
useSyslog = 1; /* traceEvent output now goes to syslog. */ useSyslog = 1; /* traceEvent output now goes to syslog. */
if ( -1 == daemon( 0, 0 ) ) daemonize();
{
traceEvent( TRACE_ERROR, "Failed to become daemon." );
exit(-5);
} }
} #endif /* #ifndef WIN32 */
#endif /* #ifdef N2N_HAVE_DAEMON */
traceEvent(TRACE_NORMAL, "Starting n2n edge %s %s", n2n_sw_version, n2n_sw_buildDate); traceEvent(TRACE_NORMAL, "Starting n2n edge %s %s", n2n_sw_version, n2n_sw_buildDate);

6
n2n.c
View File

@ -429,7 +429,11 @@ extern char * sock_to_cstr( n2n_sock_str_t out,
{ {
const uint8_t * a = sock->addr.v4; const uint8_t * a = sock->addr.v4;
r = snprintf(out, N2N_SOCKBUF_SIZE, "%hu.%hu.%hu.%hu:%hu", r = snprintf(out, N2N_SOCKBUF_SIZE, "%hu.%hu.%hu.%hu:%hu",
(a[0] & 0xff), (a[1] & 0xff), (a[2] & 0xff), (a[3] & 0xff), sock->port ); (unsigned short)(a[0] & 0xff),
(unsigned short)(a[1] & 0xff),
(unsigned short)(a[2] & 0xff),
(unsigned short)(a[3] & 0xff),
(unsigned short)sock->port );
return out; return out;
} }
} }

12
n2n.h
View File

@ -36,14 +36,7 @@
tunctl -u UID -t tunX tunctl -u UID -t tunX
*/ */
#if defined(__APPLE__) && defined(__MACH__)
#define _DARWIN_
#endif
/* Some capability defaults which can be reset for particular platforms. */
#define N2N_HAVE_DAEMON 1
#define N2N_HAVE_SETUID 1
/* #define N2N_CAN_NAME_IFACE */ /* #define N2N_CAN_NAME_IFACE */
/* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */ /* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */
@ -99,11 +92,6 @@ struct ether_hdr
typedef struct ether_hdr ether_hdr_t; typedef struct ether_hdr ether_hdr_t;
#ifdef __sun__
#include <sys/sysmacros.h> /* MIN() and MAX() declared here */
#undef N2N_HAVE_DAEMON
#endif /* #ifdef __sun__ */
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/udp.h> #include <netinet/udp.h>

View File

@ -30,8 +30,10 @@ static void read_mac(char *ifname, n2n_mac_t mac_addr) {
_sock=socket(PF_INET, SOCK_DGRAM, 0); _sock=socket(PF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, ifname); strcpy(ifr.ifr_name, ifname);
res = ioctl(_sock,SIOCGIFHWADDR,&ifr); res = ioctl(_sock,SIOCGIFHWADDR,&ifr);
if(res < 0) { if(res < 0) {
perror ("Get hw addr"); perror ("Get hw addr");
traceEvent(TRACE_ERROR, "Unable to read interfce %s MAC", ifname);
} else } else
memcpy(mac_addr, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6); memcpy(mac_addr, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);

View File

@ -62,46 +62,45 @@ int tuntap_open(tuntap_dev *device /* ignored */,
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno)); traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno));
return(-1); return(-1);
} else { } else {
char buf[256]; char cmd[256];
FILE *fd; FILE *fd;
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
device->ip_addr = inet_addr(device_ip); device->ip_addr = inet_addr(device_ip);
if ( device_mac && device_mac[0] != '\0' ) if( device_mac && device_mac[0] != '\0') {
{
/* Set the hw address before bringing the if up. */ /* Set the hw address before bringing the if up. */
snprintf(buf, sizeof(buf), "ifconfig %s link %s active", snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active",
tap_device, device_mac); tap_device, device_mac);
system(buf); system(cmd);
} }
snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s mtu %d up", snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up",
tap_device, device_ip, device_mask, mtu); tap_device, device_ip, device_mask, mtu);
system(buf); system(cmd);
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)",
tap_device, device_ip, device_mask); tap_device, device_ip, device_mask);
/* Read MAC address */ /* Read MAC address */
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_device);
/* traceEvent(TRACE_INFO, "%s", cmd); */
snprintf(buf, sizeof(buf), "ifconfig %s |grep address|cut -c 11-28", tap_device); fd = popen(cmd, "r");
/* traceEvent(TRACE_INFO, "%s", buf); */
fd = popen(buf, "r");
if(fd < 0) { if(fd < 0) {
tun_close(device); tun_close(device);
return(-1); return(-1);
} else { } else {
int a, b, c, d, e, f; int a, b, c, d, e, f;
char buf[256];
buf[0] = 0; buf[0] = 0;
fgets(buf, sizeof(buf), fd); fgets(buf, sizeof(buf), fd);
pclose(fd); pclose(fd);
if(buf[0] == '\0') { if(buf[0] == '\0') {
traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address", tap_device); traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address [%s]", tap_device, cmd);
exit(0); exit(0);
} }

View File

@ -17,7 +17,7 @@
#include "n2n.h" #include "n2n.h"
#ifdef _DARWIN_ #ifdef __APPLE__
void tun_close(tuntap_dev *device); void tun_close(tuntap_dev *device);
@ -77,7 +77,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
fd = popen(buf, "r"); fd = popen(buf, "r");
if(fd < 0) { if(fd < 0) {
tun_close(device); tuntap_close(device);
return(-1); return(-1);
} else { } else {
int a, b, c, d, e, f; int a, b, c, d, e, f;
@ -129,4 +129,4 @@ void tuntap_get_address(struct tuntap_dev *tuntap)
{ {
} }
#endif /* _DARWIN_ */ #endif /* __APPLE__ */