summaryrefslogtreecommitdiff
path: root/sbin/photurisd/server.c
blob: 13ac367262d30e7bab0c3da09b6270de1c5a6c7b (plain)
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
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright 1997,1998 Niels Provos <provos@physnet.uni-hamburg.de>
 * All rights reserved.
 *
 * Parts derived from code by Angelos D. Keromytis, kermit@forthnet.gr
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Niels Provos.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * server.c:
 * SERVER handling functions
 */

#ifndef lint
static char rcsid[] = "$Id: server.c,v 1.6 2000/12/14 23:28:59 provos Exp $";
#endif

#define _SERVER_C_
#include <stdio.h>
#include <stdlib.h> 
#include <fcntl.h>
#include <sys/types.h>  
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <netdb.h> 
#include <sys/ioctl.h> 
#include <net/if.h> 
#include <string.h>
#include <unistd.h>
#ifdef _AIX 
#include <sys/select.h> 
#endif 
#include <errno.h>
#include "config.h"
#include "photuris.h"
#include "server.h"
#include "api.h"
#include "packet.h"
#include "schedule.h"
#include "log.h"
#include "buffer.h"
#ifdef IPSEC
#include "spi.h"
#include "attributes.h"
#include "kernel.h"
#endif

int
init_server(void)
{
     struct sockaddr_in sin, *sin2; 
     struct protoent *proto; 
     struct stat sb;
     int sock, d, i, ip, on = 1; 
     struct ifconf ifconf; 
     void *newbuf;
     char buf[4096];

     readfds = normfds = NULL;

     if (global_port == 0) {
#ifndef PHOTURIS_PORT  
	  struct servent *ser;

	  if ((ser = getservbyname("photuris", "udp")) == (struct servent *) NULL)
	       log_fatal("getservbyname(\"photuris\") in init_server()");

	  global_port = ser->s_port;
#else  
	  global_port = PHOTURIS_PORT;
#endif  
     }

     if ((proto = getprotobyname("udp")) == (struct protoent *) NULL)
          log_fatal("getprotobyname() in init_server()"); 
 
     if ((global_socket = socket(PF_INET, SOCK_DGRAM, proto->p_proto)) < 0)
          log_fatal("socket() in init_server()"); 
     
     setsockopt(global_socket, SOL_SOCKET, SO_REUSEADDR, (void *)&on, 
		sizeof(on));
#ifdef IPSEC
     kernel_set_socket_policy(global_socket);
#endif	  

     /* get the local addresses */ 
 
     ifconf.ifc_len = sizeof(buf); 
     ifconf.ifc_buf = buf; 
     bzero(buf, 1024); 
 
     if (ioctl(global_socket, SIOCGIFCONF, &ifconf) == -1) 
          log_fatal("ioctl() in init_server()"); 

     sin.sin_port = htons(global_port);
     sin.sin_addr.s_addr = INADDR_ANY; 
     sin.sin_family = AF_INET; 
     
     if (bind(global_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0)
	  log_fatal("bind() in init_server()"); 
 
     /* Save interfaces addresses here */
     addresses = (char **) calloc(1+1, sizeof(char *)); 
     if (addresses == (char **) NULL) 
	  log_fatal("calloc() in init_server()"); 
     addresses[1] = (char *) NULL; 
 
     sockets = (int *) calloc(1+1, sizeof(int)); 
     if (sockets == (int *) NULL) 
	  log_fatal("calloc() in init_server()"); 
     sockets[1] = -1;
 
     if (lstat(PHOTURIS_FIFO, &sb) == -1) {
	  if (errno != ENOENT)
	       log_fatal("stat() in init_server()");
	  if (mkfifo(PHOTURIS_FIFO, 0660) == -1)
	       log_fatal("mkfifo() in init_server()");
     } else if (!(sb.st_mode & S_IFIFO))
	  log_print("%s is not a FIFO in init_server()", PHOTURIS_FIFO);

     /* We listen on a named pipe */
#if defined(linux) || defined(_AIX)
     if ((sockets[0] = open(PHOTURIS_FIFO, O_RDWR| O_NONBLOCK, 0)) == -1)
#else
     if ((sockets[0] = open(PHOTURIS_FIFO, O_RDONLY | O_NONBLOCK, 0)) == -1)
#endif
	  log_fatal("open() in init_server()");
     i = 1;                  /* One interface already */

#ifdef IPSEC
     /* We also listen on pfkeyv2 for notify messages */
     newbuf = realloc(addresses, (i + 2) * sizeof(char *)); 
     if (newbuf == NULL) {
	  if (addresses != NULL)
	       free (addresses);
	  log_fatal("realloc() in init_server()"); 
     }
     addresses = (char **) newbuf;
     
     addresses[i + 1] = (char *) NULL; 

     newbuf = realloc(sockets, (i + 2)* sizeof(int));
     if (newbuf == NULL) {
	  if (sockets != NULL)
	       free (sockets);
	  log_fatal("realloc() in init_server()");
     }
     sockets = (int *) newbuf;

     sockets[i] = kernel_get_socket();
     sockets[i+1] = -1;

     i++;                    /* Next interface */
#endif

     for (ip = 0, d = 0; d < ifconf.ifc_len; d += IFNAMSIZ + 
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(_AIX)
	       buf[IFNAMSIZ + d] 
#else 
	       sizeof(struct sockaddr) 
#endif 
	       , i++, ip++) {
	  sin2 = (struct sockaddr_in *) &buf[IFNAMSIZ + d]; 
 
	  if (sin2->sin_family != AF_INET) { 
	       i--; ip--;
	       continue; 
	  } 
 
	  newbuf =  realloc(addresses, (i + 2) * sizeof(char *)); 
	  if (newbuf == NULL) {
	       if (addresses != NULL)
		    free (addresses);
	       log_fatal("realloc() in init_server()"); 
	  }
	  addresses = (char **) newbuf;
	     
	  addresses[i] = strdup(inet_ntoa(sin2->sin_addr)); 
	  if (addresses[i] == (char *) NULL) 
	       log_fatal("strdup() in init_server()"); 
	  addresses[i + 1] = (char *) NULL; 

	  newbuf = realloc(sockets, (i + 2)* sizeof(int));
	  if (newbuf == NULL) {
	       if (sockets != NULL)
		    free (sockets);
	       log_fatal("realloc() in init_server()");
	  }
	  sockets = (int *) newbuf;

	  sockets[i+1] = -1;

	  if ((sock = socket(PF_INET, SOCK_DGRAM, proto->p_proto)) < 0)
	       log_fatal("socket() in init_server()"); 
	  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, 
		     sizeof(on)); 
#ifdef IPSEC
	  kernel_set_socket_policy(sock);
#endif	  
	  sockets[i] = sock;

#ifdef DEBUG 
	  printf("Local interface %s, address %s.\n", buf + d, 
		 addresses[i]); 
#endif 

	  bzero((void *)&sin, sizeof(sin));
	  sin.sin_port = htons(global_port);
	  sin.sin_addr.s_addr = inet_addr(addresses[i]);
	  sin.sin_family = AF_INET; 
     
	  if (bind(sockets[i], (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0)
	       log_fatal("bind() in init_server()"); 
  
     } 

     num_ifs = i;
 
#ifdef DEBUG 
     printf("%d local interfaces supporting IP found.\n", ip); 
#endif 

     return 1;
}

int 
server(void)
{
     struct sockaddr_in sin; 
     struct timeval timeout; 
     int i, d, size;

     setvbuf(stdout, (char *)NULL, _IOLBF, 0);

     size = howmany(sockets[num_ifs-1], NFDBITS) * sizeof(fd_mask);
     normfds = (fd_set *)malloc(size);
     if (normfds == NULL)
	  log_fatal("malloc(%d) for fd_set", size);

     readfds = (fd_set *)malloc(size);
     if (readfds == NULL)
	  log_fatal("malloc(%d) for fd_set", size);

     memset((void *)normfds, 0, size); 

     for (i = 0; i < num_ifs; i++)
	  FD_SET(sockets[i], normfds);

     while (1) {
	  bcopy(normfds, readfds, size);

	  /* Timeout till next job */
	  timeout.tv_usec = 0;
	  timeout.tv_sec = schedule_next();

#ifdef DEBUG2
	  printf("Sleeping for %ld seconds\n", timeout.tv_sec);
#endif

	  if (select(sockets[num_ifs-1]+1, 
		     readfds, (fd_set *) NULL, (fd_set *) NULL, 
		     (timeout.tv_sec == -1 ? NULL : &timeout)) < 0) {
	       if (errno == EINTR) 
                    continue; 
	       else
                    log_fatal("select() in server()"); 
	  }

	  for (i=0; i<num_ifs; i++) {
	       if (FD_ISSET(sockets[i], readfds)) {
#ifdef IPSEC
		    if (i == 1)       /* PF_ENCAP NOTIFIES */
			 kernel_handle_notify(sockets[i]);
		    else
#endif
			 if (addresses[i] == NULL)
				 process_api(sockets[i], global_socket); 
			 else {
				 d = sizeof(struct sockaddr_in);
				 if (recvfrom(sockets[i], 
#ifdef BROKEN_RECVFROM
					      (char *) buffer, 1,
#else
					      (char *) NULL, 0, 
#endif
					      MSG_PEEK,
					      (struct sockaddr *)&sin,
					      &d) == -1) {
					 log_error("recvfrom() in server()"); 
					 return -1;
				 }
				 handle_packet(sockets[i], addresses[i]);
 		    }
	       } 
	  }

#ifdef IPSEC
	  /* 
	   * Deal with queue acquire and expire message, since we
	   * dont have proper timeout code, it needs to go here.
	   */
	  kernel_handle_queue();
#endif

	  schedule_process(global_socket);
	  fflush(stdout);
	  fflush(stderr);
     }

     /* We will never reach this place - it's called limbo */

}