-
Notifications
You must be signed in to change notification settings - Fork 39
/
BeaconMain.cpp
executable file
·148 lines (127 loc) · 3.51 KB
/
BeaconMain.cpp
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
/**************************************************************
* Copyright (c) 2010-2013, Dynamic Network Services, Inc.
* Jake Montgomery (jmontgomery@dyn.com) & Tom Daly (tom@dyn.com)
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#include "common.h"
#include "Beacon.h"
#include "utils.h"
#include <string.h>
#include <list>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[])
{
bool ret;
Beacon app;
bool tee = false;
bool doFork = true;
int argIndex;
list<SockAddr> controlPorts;
list<IpAddr> listenAddrs;
const char *valueString;
#ifdef BFD_DEBUG
tee = true;
#endif
//Parse command line options.
//Be careful what we do here, since we are pre-fork, and we have not initialized
//utils, logging, etc.
for (argIndex = 1; argIndex < argc; argIndex++)
{
if (0 == strcmp("--notee", argv[argIndex]))
{
tee = false;
}
else if (0 == strcmp("--tee", argv[argIndex]))
{
tee = true;
}
else if (0 == strcmp("--nofork", argv[argIndex]))
{
doFork = false;
}
else if (0 == strcmp("--version", argv[argIndex]))
{
fprintf(stdout, "%s version %s\n", BeaconAppName, SofwareVesrion);
exit(0);
}
else if (CheckArg("--control", argv[argIndex], &valueString))
{
SockAddr addrVal;
if (!valueString || *valueString == '\0')
{
fprintf(stderr, "--control must be followed by an '=' and a ip address with a port.\n");
exit(1);
}
if (!addrVal.FromString(valueString))
{
fprintf(stderr, "--control address <%s> is not an IPv4 or IPv6 address.\n", valueString);
exit(1);
}
if (!addrVal.HasPort())
{
fprintf(stderr, "--control address must have a port specified. The address <%s> does not contain a port.\n", valueString);
exit(1);
}
controlPorts.push_back(addrVal);
}
else if (CheckArg("--listen", argv[argIndex], &valueString))
{
IpAddr addrVal;
if (!valueString || *valueString == '\0')
{
fprintf(stderr, "--listen must be followed by an '=' and a ip address (may be 0.0.0.0 or ::).\n");
exit(1);
}
if (!addrVal.FromString(valueString))
{
fprintf(stderr, "--listen address <%s> is not an IPv4 or IPv6 address.\n", valueString);
exit(1);
}
listenAddrs.push_back(addrVal);
}
else
{
fprintf(stderr, "Unrecognized %s command line option %s.\n", BeaconAppName, argv[argIndex]);
exit(1);
}
}
if (doFork)
{
tee = false;
if (0 != daemon(1, 0))
{
fprintf(stderr, "Failed to daemonize. Exiting.\n");
exit(1);
}
}
srand(time(NULL));
if (!UtilsInit() || !UtilsInitThread())
{
fprintf(stderr, "Unable to init thread local storage. Exiting.\n");
exit(1);
}
if (controlPorts.empty())
{
SockAddr addr;
addr.FromString("127.0.0.1", PORTNUM);
controlPorts.push_back(addr);
addr.FromString("127.0.0.1", ALT_PORTNUM);
controlPorts.push_back(addr);
}
if (listenAddrs.empty())
{
IpAddr addr;
addr.FromString("0.0.0.0");
listenAddrs.push_back(addr);
addr.FromString("::");
listenAddrs.push_back(addr);
}
// Setup logging first
// gLog.SetLogLevel(Log::Detail);
gLog.LogToSyslog("bfdd-beacon", tee);
gLog.Message(Log::App, "Started %d", getpid());
ret = app.Run(controlPorts, listenAddrs);
gLog.Message(Log::App, "Shutdown %d", getpid());
return ret ? 0 : 1;
}