-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
183 lines (155 loc) · 4 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2009-2010 Michał "MasterM" Siejak
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include "ipx.h"
#include "ipxserver.h"
static FILE *log = NULL;
static void quit_handler(int signal)
{
IPX_StopServer();
LOG_MSG("MAIN: Server shutdown.");
#ifdef LOG_TO_FILE
fclose(log);
#endif
exit(0);
}
static void daemon_handler(int signum)
{
switch(signum) {
case SIGALRM: exit(1); break;
case SIGCHLD: exit(1); break;
case SIGUSR1: exit(0); break;
}
}
int log_stdout(const char *fmt, ...)
{
va_list args;
int ret;
time_t timestamp;
struct tm *t;
char buffer[50];
time(×tamp);
t = localtime(×tamp);
sprintf(buffer, "%02d-%02d-%d %02d:%02d:%02d", t->tm_mon+1, t->tm_mday, t->tm_year+1900, t->tm_hour, t->tm_min, t->tm_sec);
va_start(args, fmt);
printf("[%s] ", buffer);
ret = vprintf(fmt, args);
printf("\n");
va_end(args);
fflush(stdout);
return ret;
}
int log_file(const char *fmt, ...)
{
va_list args;
int ret;
time_t timestamp;
struct tm *t;
char buffer[50];
time(×tamp);
t = localtime(×tamp);
sprintf(buffer, "%02d-%02d-%d %02d:%02d:%02d", t->tm_mon+1, t->tm_mday, t->tm_year+1900, t->tm_hour, t->tm_min, t->tm_sec);
va_start(args, fmt);
fprintf(log, "[%s] ", buffer);
ret = vfprintf(log, fmt, args);
fprintf(log, "\n");
va_end(args);
fflush(log);
return ret;
}
void daemonize()
{
pid_t pid, sid, parent;
// If we're already a daemon, return.
if(getppid() == 1) return;
signal(SIGCHLD, daemon_handler);
signal(SIGALRM, daemon_handler);
signal(SIGUSR1, daemon_handler);
// Fork off the parent process.
pid = fork();
if(pid < 0)
exit(1);
else if(pid > 0) {
// Wait for SIGUSR1 or exit with failure.
alarm(2);
pause();
exit(1);
}
parent = getppid();
// Cancel out signals
signal(SIGCHLD, SIG_DFL); // Child dies
signal(SIGTERM, SIG_DFL); // Default on SIGTERM
signal(SIGHUP, SIG_IGN); // Ignore SIGHUP
signal(SIGTSTP, SIG_IGN); // TTY signals below:
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
// Change the file mode mask
umask(0);
// Create new session (sid)
sid = setsid();
if(sid < 0) exit(1);
// Redirect standard files to /dev/null
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
kill(parent, SIGUSR1);
}
int main(int argc, char **argv)
{
int port;
if(argc < 2) {
fprintf(stderr, "Usage: %s <port>\n", PROGNAME);
return 1;
}
port = atoi(argv[1]);
if(port <= 0) {
fprintf(stderr, "%s: Invalid port number!\n", PROGNAME);
return 2;
}
#ifdef LOG_TO_FILE
log = fopen(LOGNAME, "a");
if(log == NULL) {
fprintf(stderr, "%s: Cannot open logfile!\n", PROGNAME);
return 2;
}
// Go daemon if logging to file
daemonize();
#endif
// Start tunneling service
signal(SIGTERM, quit_handler);
signal(SIGINT, quit_handler);
if(!IPX_StartServer(port)) {
fprintf(stderr, "%s: Could not start IPX server!\n", PROGNAME);
return 1;
}
// Main loop
LOG_MSG("MAIN: Server started on UDP port %d", port);
while(1) {
IPX_ServerLoop();
usleep(TICK_TIME);
}
return 0;
}