Skip to content

Commit

Permalink
Merge pull request #109 from AkihiroSuda/dev2
Browse files Browse the repository at this point in the history
Add log.h
  • Loading branch information
AkihiroSuda authored Dec 10, 2024
2 parents fac3207 + 58e3862 commit abdbc35
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
32 changes: 14 additions & 18 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <uuid/uuid.h>

#include "cli.h"
#include "log.h"

#ifndef VERSION
#define VERSION "UNKNOWN"
Expand Down Expand Up @@ -77,7 +78,7 @@ enum {
struct cli_options *cli_options_parse(int argc, char *argv[]) {
struct cli_options *res = calloc(1, sizeof(*res));
if (res == NULL) {
perror("calloc");
ERRORN("calloc");
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -109,7 +110,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
} else if (strcmp(optarg, "bridged") == 0) {
res->vmnet_mode = VMNET_BRIDGED_MODE;
} else {
fprintf(stderr, "Unknown vmnet mode \"%s\"\n", optarg);
ERRORF("Unknown vmnet mode \"%s\"", optarg);
goto error;
}
break;
Expand All @@ -127,7 +128,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
break;
case CLI_OPT_VMNET_INTERFACE_ID:
if (uuid_parse(optarg, res->vmnet_interface_id) < 0) {
fprintf(stderr, "Failed to parse UUID \"%s\"\n", optarg);
ERRORF("Failed to parse UUID \"%s\"", optarg);
goto error;
}
break;
Expand Down Expand Up @@ -166,7 +167,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
* is specified) */
struct in_addr sin;
if (!inet_aton(res->vmnet_gateway, &sin)) {
perror("inet_aton(res->vmnet_gateway)");
ERRORN("inet_aton(res->vmnet_gateway)");
goto error;
}
uint32_t h = ntohl(sin.s_addr);
Expand All @@ -175,7 +176,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
sin.s_addr = htonl(h);
const char *end_static = inet_ntoa(sin); /* static storage, do not free */
if (end_static == NULL) {
perror("inet_ntoa");
ERRORN("inet_ntoa");
goto error;
}
res->vmnet_dhcp_end = strdup(end_static);
Expand All @@ -189,36 +190,31 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {

/* validate */
if (res->vmnet_mode == VMNET_BRIDGED_MODE && res->vmnet_interface == NULL) {
fprintf(
stderr,
"vmnet mode \"bridged\" require --vmnet-interface to be specified\n");
ERROR("vmnet mode \"bridged\" require --vmnet-interface to be specified");
goto error;
}
if (res->vmnet_gateway == NULL) {
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
fprintf(stderr,
"WARNING: --vmnet-gateway=IP should be explicitly specified to "
"avoid conflicting with other applications\n");
WARN("--vmnet-gateway=IP should be explicitly specified to "
"avoid conflicting with other applications");
}
if (res->vmnet_dhcp_end != NULL) {
fprintf(stderr, "--vmnet-dhcp-end=IP requires --vmnet-gateway=IP\n");
ERROR("--vmnet-dhcp-end=IP requires --vmnet-gateway=IP");
goto error;
}
if (res->vmnet_mask != NULL) {
fprintf(stderr, "--vmnet-mask=MASK requires --vmnet-gateway=IP\n");
ERROR("--vmnet-mask=MASK requires --vmnet-gateway=IP");
goto error;
}
} else {
if (res->vmnet_mode == VMNET_BRIDGED_MODE) {
fprintf(stderr,
"vmnet mode \"bridged\" conflicts with --vmnet-gateway\n");
ERROR("vmnet mode \"bridged\" conflicts with --vmnet-gateway");
goto error;
}
struct in_addr dummy;
if (!inet_aton(res->vmnet_gateway, &dummy)) {
fprintf(stderr,
"invalid address \"%s\" was specified for --vmnet-gateway\n",
res->vmnet_gateway);
ERRORF("invalid address \"%s\" was specified for --vmnet-gateway",
res->vmnet_gateway);
goto error;
}
}
Expand Down
20 changes: 20 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef SOCKET_VMNET_LOG_H
#define SOCKET_VMNET_LOG_H
#include <errno.h>

extern bool debug;

#define DEBUGF(fmt, ...) \
do { \
if (debug) \
fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \
} while (0)

#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
#define WARN(msg) fprintf(stderr, "WARN | " msg "\n")
#define WARNF(fmt, ...) fprintf(stderr, "WARN | " fmt "\n", __VA_ARGS__)

#endif /* SOCKET_VMNET_LOG_H */
20 changes: 4 additions & 16 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,13 @@
#include <vmnet/vmnet.h>

#include "cli.h"
#include "log.h"

#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101500
#error "Requires macOS 10.15 or later"
#endif

static bool debug = false;

#define DEBUGF(fmt, ...) \
do { \
if (debug) \
fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \
} while (0)

#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
bool debug = false;

static const char *vmnet_strerror(vmnet_return_t v) {
switch (v) {
Expand Down Expand Up @@ -410,12 +400,10 @@ int main(int argc, char *argv[]) {
struct cli_options *cliopt = cli_options_parse(argc, argv);
assert(cliopt != NULL);
if (geteuid() != 0) {
ERROR("WARNING: Running without root. This is very unlikely to "
"work: See README.md");
WARN("Running without root. This is very unlikely to work: See README.md");
}
if (geteuid() != getuid()) {
ERROR("WARNING: Seems running with SETUID. This is insecure and "
"highly discouraged: See README.md");
WARN("Seems running with SETUID. This is insecure and highly discouraged: See README.md");
}

if (sigsetjmp(jmpbuf, 1) != 0) {
Expand Down

0 comments on commit abdbc35

Please sign in to comment.