summaryrefslogtreecommitdiff
path: root/src/sig_handler.c
blob: 98ae50d94a76b3125fbf741f3dbaa53f1d138f9e (plain) (blame)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 *  uAnytun
 *
 *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
 *  featured implementation uAnytun has no support for multiple connections
 *  or synchronisation. It is a small single threaded implementation intended
 *  to act as a client on small platforms.
 *  The secure anycast tunneling protocol (satp) defines a protocol used
 *  for communication between any combination of unicast and anycast
 *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
 *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
 *  ethernet, ip, arp ...). satp directly includes cryptography and
 *  message authentication based on the methodes used by SRTP.  It is
 *  intended to deliver a generic, scaleable and secure solution for
 *  tunneling and relaying of packets of any protocol.
 *  
 *
 *  Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
 *
 *  This file is part of uAnytun.
 *
 *  uAnytun 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 3 of the License, or
 *  any later version.
 *
 *  uAnytun 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 uAnytun. If not, see <http://www.gnu.org/licenses/>.
 */

#include "datatypes.h"

#include "log.h"

#include "sig_handler.h"
#include <errno.h>


#ifndef WINVER

#include <signal.h>
#include <unistd.h>
#include <fcntl.h>

static int sig_pipe_fds[2];

static void sig_handler(int sig)
{
  sigset_t set;
  int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
  if(ret != sizeof(sigset_t))
    sigemptyset(&set);

  sigaddset(&set, sig);
  ret = write(sig_pipe_fds[1], &set, sizeof(sigset_t));
}


int signal_init()
{
  if(pipe(sig_pipe_fds)) {
    log_printf(ERROR, "signal handling init failed (pipe error: %s)", strerror(errno));
    return -1;
  }

  int i;
  for(i=0; i<2; ++i) {
    int fd_flags = fcntl(sig_pipe_fds[i], F_GETFL);
    if(fd_flags == -1) {
      log_printf(ERROR, "signal handling init failed (pipe fd[%d] read flags error: %s)", i, strerror(errno));
      return -1;
    }
    if(fcntl(sig_pipe_fds[i], F_SETFL, fd_flags | O_NONBLOCK) == -1){
      log_printf(ERROR, "signal handling init failed (pipe fd[%d] write flags error: %s)", i, strerror(errno));
      return -1;
    }
  }

  struct sigaction act, act_ign;
  act.sa_handler = sig_handler;
  sigfillset(&act.sa_mask);
  act.sa_flags = 0;
  act_ign.sa_handler = SIG_DFL;
  sigfillset(&act_ign.sa_mask);
  act_ign.sa_flags = 0;

  if((sigaction(SIGINT, &act, NULL) < 0) ||
     (sigaction(SIGQUIT, &act, NULL) < 0) ||
     (sigaction(SIGTERM, &act, NULL) < 0) ||
     (sigaction(SIGHUP, &act, NULL) < 0) ||
     (sigaction(SIGUSR1, &act, NULL) < 0) ||
     (sigaction(SIGUSR2, &act, NULL) < 0) ||
     (sigaction(SIGPIPE, &act_ign, NULL) < 0)) {

    log_printf(ERROR, "signal handling init failed (sigaction error: %s)", strerror(errno));
    close(sig_pipe_fds[0]);
    close(sig_pipe_fds[1]);
  }

  return sig_pipe_fds[0];
}

int signal_handle()
{
  sigset_t set, oldset, tmpset;

  sigemptyset(&tmpset);
  sigaddset(&tmpset, SIGINT);
  sigaddset(&tmpset, SIGQUIT);
  sigaddset(&tmpset, SIGTERM);
  sigaddset(&tmpset, SIGHUP);
  sigaddset(&tmpset, SIGUSR1);
  sigaddset(&tmpset, SIGUSR2);
  sigprocmask(SIG_BLOCK, &tmpset, &oldset);

  int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
  if(ret != sizeof(sigset_t))
    sigemptyset(&set);

  int return_value = 0;
  int sig;
  for(sig=1; sig < NSIG; ++sig) {
    if(sigismember(&set, sig)) {
      switch(sig) {
      case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); return_value = 1; break;
      case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); return_value = 1; break;
      case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); return_value = 1; break;
      case SIGHUP: log_printf(NOTICE, "SIG-Hup caught"); break;
      case SIGUSR1: log_printf(NOTICE, "SIG-Usr1 caught"); break;
      case SIGUSR2: log_printf(NOTICE, "SIG-Usr2 caught"); break;
      default: log_printf(WARNING, "unknown signal %d caught, ignoring", sig); break;
      }
      sigdelset(&set, sig);
    }
  }

  sigprocmask(SIG_SETMASK, &oldset, NULL);
  return return_value;
}

void signal_stop()
{
  struct sigaction act;
  act.sa_handler = SIG_DFL;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;

  sigaction(SIGINT, &act, NULL);
  sigaction(SIGQUIT, &act, NULL);
  sigaction(SIGTERM, &act, NULL);
  sigaction(SIGHUP, &act, NULL);
  sigaction(SIGUSR1, &act, NULL);
  sigaction(SIGUSR2, &act, NULL);
  sigaction(SIGPIPE, &act, NULL);

  close(sig_pipe_fds[0]);
  close(sig_pipe_fds[1]);
}


#else


#include <winsock2.h>
#include <windows.h>

static SOCKET sig_sock_fds[2];
static SOCKET server_sock;

static DWORD WINAPI signal_connect_thread(void* p)
{
  uint16_t port = *((uint16_t*)p);
  
  SOCKET client;
  client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(client == INVALID_SOCKET) {
    log_printf(ERROR, "Error at socket(): 0x%04X\n", WSAGetLastError());
    return -1;
  }

  struct sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  server.sin_port = port;

  int cnt = 0;
  int ret = SOCKET_ERROR;
  do {
    Sleep(100);
    ret = connect(client, (SOCKADDR*) &server, sizeof(server));
    cnt++;
  }
  while(ret != 0 && cnt < 10);

  if(ret != 0) {
    log_printf(ERROR, "connect() failed: 0x%04X", WSAGetLastError());
    closesocket(client);
    return -1;
  }

  log_printf(DEBUG, "sig_handler connected to port %d", port);  

  sig_sock_fds[1] = client;

  return 0;
}

static BOOL WINAPI signal_ctrl_handler(DWORD ctrlType)
{
  char c = (char)ctrlType;
  send(sig_sock_fds[1], &c, 1, 0);
  return 1;
}

int signal_init()
{
  WSADATA wsaData;
  int ret = WSAStartup(MAKEWORD(2,2), &wsaData);
  if(ret != NO_ERROR) {
    log_printf(ERROR, "Error at WSAStartup(): 0x%04X", WSAGetLastError());
    return -1;
  }

  server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(server_sock == INVALID_SOCKET) {
    log_printf(ERROR, "Error at socket(): 0x%04X\n", WSAGetLastError());
    WSACleanup();
    return -1;
  }

  struct sockaddr_in service;
  service.sin_family = AF_INET;
  service.sin_addr.s_addr = inet_addr("127.0.0.1");
  service.sin_port = 0;

  if(bind(server_sock, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) {
    log_printf(ERROR, "bind() failed: 0x%04X", WSAGetLastError());
    closesocket(server_sock);
    WSACleanup();
    return -1;
  }
  
  struct sockaddr_in hugo;
  int hugo_len = sizeof(hugo);
  if(getsockname(server_sock, (SOCKADDR*) &hugo, &hugo_len) == SOCKET_ERROR) {
    log_printf(ERROR, "getsockname() failed: 0x%04X", WSAGetLastError());
    closesocket(server_sock);
    WSACleanup();
    return -1;
  }
  
  if(listen(server_sock, 1) == SOCKET_ERROR) {
    log_printf(ERROR, "listen() failed: 0x%04X", WSAGetLastError());
    closesocket(server_sock);
    WSACleanup();
    return -1;
  }

  log_printf(DEBUG, "sig_handler listening on port %d", hugo.sin_port);

  HANDLE t = CreateThread(NULL, 0, signal_connect_thread, &(hugo.sin_port), 0, NULL);
  if(t == INVALID_HANDLE_VALUE) {
    log_printf(ERROR, "CreateThread() failed: 0x%04X", GetLastError());
    closesocket(server_sock);
    WSACleanup();
    return -1;
  }

  sig_sock_fds[0] = accept(server_sock, NULL, NULL);
  if(sig_sock_fds[0] == INVALID_SOCKET) {
    log_printf(ERROR, "accept() failed: 0x%04X", WSAGetLastError());
    closesocket(server_sock);
    WSACleanup();
    WaitForSingleObject(t, INFINITE);
    return -1;
  }

  WaitForSingleObject(t, INFINITE);
  
  if(!SetConsoleCtrlHandler(signal_ctrl_handler, 1)) {
    log_printf(ERROR, "adding console ctrl handler failed: 0x%04X" , GetLastError());
  }

  return sig_sock_fds[0];
}

int signal_handle()
{
  char c;
  int ret = recv(sig_sock_fds[0], &c, 1, 0);
  if(ret == 1) {
    switch(c) {
    case CTRL_C_EVENT: log_printf(NOTICE, "CTRL-C Event received, exitting"); return 1;
    case CTRL_BREAK_EVENT: log_printf(NOTICE, "CTRL-Break Event received, ignoring"); return 1;
    case CTRL_CLOSE_EVENT: log_printf(NOTICE, "Close Event received, exitting"); return 1;
    case CTRL_LOGOFF_EVENT: log_printf(NOTICE, "LogOff Event received, exitting"); return 1;
    case CTRL_SHUTDOWN_EVENT: log_printf(NOTICE, "Shutdown Event received, exitting"); return 1;
    default: log_printf(NOTICE, "unknown event received 0x%02X, ignoring", c); return 0;
    }
  }

  if(ret == 0) {
    log_printf(ERROR, "signal socket closed normally, exitting");
    return 1;
  }

  log_printf(ERROR, "signal socket closed with error 0x%04X, exitting", GetLastError());
  return 1;
}

void signal_stop()
{
  if(!SetConsoleCtrlHandler(signal_ctrl_handler, 0)) {
    log_printf(ERROR, "removeing console ctrl handler failed: 0x%04X" , GetLastError());
  }
  closesocket(sig_sock_fds[0]);
  closesocket(sig_sock_fds[1]);
  closesocket(server_sock);
  WSACleanup();
  return;
}
#endif