blob: b75b01dbe86522b754dbae78ed19a5c2da3dcd30 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * OpenGGSN - Gateway GPRS Support Node
3 * Copyright (C) 2002, 2003 Mondru AB.
jjako52c24142002-12-16 13:33:51 +00004 *
jjakoa7cd2492003-04-11 09:40:12 +00005 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
jjako52c24142002-12-16 13:33:51 +00009 *
jjakoa7cd2492003-04-11 09:40:12 +000010 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
jjako52c24142002-12-16 13:33:51 +000012 *
jjakoa7cd2492003-04-11 09:40:12 +000013 * Contributor(s):
jjako52c24142002-12-16 13:33:51 +000014 *
15 */
16
17/* ggsn.c
18 *
19 */
20
21#ifdef __linux__
22#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
23#endif
24
25
26#include <syslog.h>
27#include <ctype.h>
28#include <netdb.h>
29#include <signal.h>
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <sys/wait.h>
38#include <sys/stat.h>
jjako0141d202004-01-09 15:19:20 +000039#include <fcntl.h>
jjako52c24142002-12-16 13:33:51 +000040#include <unistd.h>
41
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <net/if.h>
jjako52c24142002-12-16 13:33:51 +000045
46#include <errno.h>
47
jjako52c24142002-12-16 13:33:51 +000048#include <time.h>
49
jjakoff9985c2004-01-16 11:05:22 +000050#include "config.h"
51
jjako52c24142002-12-16 13:33:51 +000052#include "tun.h"
jjakoa7cd2492003-04-11 09:40:12 +000053#include "ippool.h"
54#include "syserr.h"
jjako52c24142002-12-16 13:33:51 +000055#include "../gtp/pdp.h"
56#include "../gtp/gtp.h"
57#include "cmdline.h"
58
59
jjakoa7cd2492003-04-11 09:40:12 +000060int maxfd = 0; /* For select() */
jjakoa7cd2492003-04-11 09:40:12 +000061
62struct in_addr listen_;
jjako52c24142002-12-16 13:33:51 +000063struct in_addr net, mask; /* Network interface */
jjakoa7cd2492003-04-11 09:40:12 +000064struct in_addr dns1, dns2; /* PCO DNS address */
65char *ipup, *ipdown; /* Filename of scripts */
66int debug; /* Print debug output */
67struct ul255_t pco;
68struct ul255_t qos;
69struct ul255_t apn;
70
jjako9c7ff082003-04-11 10:01:41 +000071struct gsn_t *gsn; /* GSN instance */
jjakoa7cd2492003-04-11 09:40:12 +000072struct tun_t *tun; /* TUN instance */
73struct ippool_t *ippool; /* Pool of IP addresses */
jjako52c24142002-12-16 13:33:51 +000074
75
76/* Used to write process ID to file. Assume someone else will delete */
77void log_pid(char *pidfile) {
78 FILE *file;
79 mode_t oldmask;
80
81 oldmask = umask(022);
82 file = fopen(pidfile, "w");
83 umask(oldmask);
jjakoe0149782003-07-06 17:07:04 +000084 if(!file) {
85 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
86 "Failed to create process ID file: %s!", pidfile);
jjako52c24142002-12-16 13:33:51 +000087 return;
jjakoe0149782003-07-06 17:07:04 +000088 }
jjako0141d202004-01-09 15:19:20 +000089 fprintf(file, "%d\n", (int) getpid());
jjako52c24142002-12-16 13:33:51 +000090 fclose(file);
91}
92
jjako0141d202004-01-09 15:19:20 +000093#ifdef __sun__
94int daemon(int nochdir, int noclose) {
95 int fd;
96
97 switch (fork()) {
98 case -1:
99 return (-1);
100 case 0:
101 break;
102 default:
103 _exit(0);
104 }
105
106 if (setsid() == -1)
107 return (-1);
108
109 if (!nochdir) chdir("/");
110
111 if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
112 dup2(fd, STDIN_FILENO);
113 dup2(fd, STDOUT_FILENO);
114 dup2(fd, STDERR_FILENO);
115 if (fd > 2) close (fd);
116 }
117 return (0);
118}
119#endif
120
jjako52c24142002-12-16 13:33:51 +0000121
122int encaps_printf(void *p, void *packet, unsigned len)
123{
124 int i;
125 if (debug) {
126 printf("The packet looks like this:\n");
127 for( i=0; i<len; i++) {
128 printf("%02x ", (unsigned char)*(char *)(packet+i));
129 if (!((i+1)%16)) printf("\n");
130 };
131 printf("\n");
132 }
133 return 0;
134}
135
jjako52c24142002-12-16 13:33:51 +0000136int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000137 if (debug) printf("Deleting PDP context\n");
jjako08d331d2003-10-13 20:33:30 +0000138 if (pdp->peer)
139 ippool_freeip(ippool, (struct ippoolm_t *) pdp->peer);
140 else
141 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
jjako52c24142002-12-16 13:33:51 +0000142 return 0;
143}
144
145
jjako08d331d2003-10-13 20:33:30 +0000146int create_context_ind(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000147 struct in_addr addr;
148 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000149
150 if (debug) printf("Received create PDP context request\n");
151
152 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
153
154 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
155 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
jjakoa7cd2492003-04-11 09:40:12 +0000156 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000157
jjako08d331d2003-10-13 20:33:30 +0000158 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
159 pdp->qos_neg.l = pdp->qos_req.l;
160
jjakoa7cd2492003-04-11 09:40:12 +0000161 if (pdp_euaton(&pdp->eua, &addr)) {
162 addr.s_addr = 0; /* Request dynamic */
163 }
164
165 if (ippool_newip(ippool, &member, &addr)) {
jjako08d331d2003-10-13 20:33:30 +0000166 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
167 return 0; /* Allready in use, or no more available */
jjakoa7cd2492003-04-11 09:40:12 +0000168 }
169
170 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000171 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000172 pdp->ipif = tun; /* TODO */
173 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000174
jjako08d331d2003-10-13 20:33:30 +0000175 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +0000176 return 0; /* Success */
177}
178
179
jjakoa7cd2492003-04-11 09:40:12 +0000180/* Callback for receiving messages from tun */
181int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
182 struct ippoolm_t *ipm;
183 struct in_addr dst;
184 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
185
186 dst.s_addr = iph->dst;
187
188 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000189 if (debug) printf("Received packet with no destination!!!\n");
190 return 0;
191 }
jjakoa7cd2492003-04-11 09:40:12 +0000192
193 if (ipm->peer) /* Check if a peer protocol is defined */
jjako08d331d2003-10-13 20:33:30 +0000194 gtp_data_req(gsn, (struct pdp_t*) ipm->peer, pack, len);
jjakoa7cd2492003-04-11 09:40:12 +0000195 return 0;
jjako52c24142002-12-16 13:33:51 +0000196}
197
jjako52c24142002-12-16 13:33:51 +0000198int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
199 /* printf("encaps_tun. Packet received: forwarding to tun\n");*/
200 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
201}
202
203
204int main(int argc, char **argv)
205{
206 /* gengeopt declarations */
207 struct gengetopt_args_info args_info;
208
209 struct hostent *host;
210
jjako52c24142002-12-16 13:33:51 +0000211
jjako52c24142002-12-16 13:33:51 +0000212 fd_set fds; /* For select() */
213 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000214
jjako52c24142002-12-16 13:33:51 +0000215
216 int timelimit; /* Number of seconds to be connected */
217 int starttime; /* Time program was started */
218
219 /* open a connection to the syslog daemon */
220 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
jjako0141d202004-01-09 15:19:20 +0000221
222 /* TODO: Only use LOG__PERROR for linux */
223#ifdef __linux__
jjako52c24142002-12-16 13:33:51 +0000224 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
jjako0141d202004-01-09 15:19:20 +0000225#else
226 openlog(PACKAGE, (LOG_PID), LOG_DAEMON);
227#endif
228
jjako52c24142002-12-16 13:33:51 +0000229
230 if (cmdline_parser (argc, argv, &args_info) != 0)
231 exit(1);
232 if (args_info.debug_flag) {
233 printf("listen: %s\n", args_info.listen_arg);
234 printf("conf: %s\n", args_info.conf_arg);
235 printf("fg: %d\n", args_info.fg_flag);
236 printf("debug: %d\n", args_info.debug_flag);
237 printf("qos: %#08x\n", args_info.qos_arg);
238 printf("apn: %s\n", args_info.apn_arg);
239 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000240 printf("dynip: %s\n", args_info.dynip_arg);
241 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000242 printf("ipup: %s\n", args_info.ipup_arg);
243 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000244 printf("pidfile: %s\n", args_info.pidfile_arg);
245 printf("statedir: %s\n", args_info.statedir_arg);
246 printf("timelimit: %d\n", args_info.timelimit_arg);
247 }
248
249 /* Try out our new parser */
250
251 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
252 exit(1);
253 if (args_info.debug_flag) {
254 printf("cmdline_parser_configfile\n");
255 printf("listen: %s\n", args_info.listen_arg);
256 printf("conf: %s\n", args_info.conf_arg);
257 printf("fg: %d\n", args_info.fg_flag);
258 printf("debug: %d\n", args_info.debug_flag);
259 printf("qos: %#08x\n", args_info.qos_arg);
260 printf("apn: %s\n", args_info.apn_arg);
261 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000262 printf("dynip: %s\n", args_info.dynip_arg);
263 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000264 printf("ipup: %s\n", args_info.ipup_arg);
265 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000266 printf("pidfile: %s\n", args_info.pidfile_arg);
267 printf("statedir: %s\n", args_info.statedir_arg);
268 printf("timelimit: %d\n", args_info.timelimit_arg);
269 }
270
271 /* Handle each option */
272
jjako52c24142002-12-16 13:33:51 +0000273 /* debug */
274 debug = args_info.debug_flag;
275
jjako52c24142002-12-16 13:33:51 +0000276 /* listen */
jjako52c24142002-12-16 13:33:51 +0000277 /* Do hostname lookup to translate hostname to IP address */
jjakoe0149782003-07-06 17:07:04 +0000278 /* Any port listening is not possible as a valid address is */
279 /* required for create_pdp_context_response messages */
jjako52c24142002-12-16 13:33:51 +0000280 if (args_info.listen_arg) {
281 if (!(host = gethostbyname(args_info.listen_arg))) {
jjakoe0149782003-07-06 17:07:04 +0000282 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
283 "Invalid listening address: %s!", args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000284 exit(1);
jjako52c24142002-12-16 13:33:51 +0000285 }
286 else {
jjakoa7cd2492003-04-11 09:40:12 +0000287 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000288 }
289 }
290 else {
jjakoe0149782003-07-06 17:07:04 +0000291 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
292 "Listening address must be specified! "
293 "Please use command line option --listen or "
294 "edit %s configuration file\n", args_info.conf_arg);
295 exit(1);
jjako52c24142002-12-16 13:33:51 +0000296 }
297
jjako88c22162003-07-06 19:33:18 +0000298
jjako52c24142002-12-16 13:33:51 +0000299 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000300 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000301 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000302 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
303 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
304 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000305 exit(1);
jjako52c24142002-12-16 13:33:51 +0000306 }
307 }
jjakoa7c33812003-04-11 11:51:39 +0000308 else {
309 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
310 "Network address must be specified: %s!", args_info.net_arg);
311 exit(1);
312 }
jjako52c24142002-12-16 13:33:51 +0000313
jjakoa7cd2492003-04-11 09:40:12 +0000314 /* dynip */
315 if (!args_info.dynip_arg) {
jjako88c22162003-07-06 19:33:18 +0000316 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
jjakoa7c33812003-04-11 11:51:39 +0000317 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
318 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
319 "Failed to allocate IP pool!");
320 exit(1);
321 }
jjakoa7cd2492003-04-11 09:40:12 +0000322 }
323 else {
jjako88c22162003-07-06 19:33:18 +0000324 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1 ,0,
jjakoa7cd2492003-04-11 09:40:12 +0000325 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
326 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
327 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000328 exit(1);
jjako52c24142002-12-16 13:33:51 +0000329 }
330 }
331
jjakoa7cd2492003-04-11 09:40:12 +0000332 /* DNS1 and DNS2 */
jjakoff9985c2004-01-16 11:05:22 +0000333#ifdef HAVE_INET_ATON
jjakoa7cd2492003-04-11 09:40:12 +0000334 dns1.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000335 if (args_info.pcodns1_arg) {
jjako1d3db972004-01-16 09:56:56 +0000336 if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) {
jjako76032b92004-01-14 06:22:08 +0000337 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
338 "Failed to convert pcodns1!");
339 exit(1);
340 }
341 }
jjakoa7cd2492003-04-11 09:40:12 +0000342 dns2.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000343 if (args_info.pcodns2_arg) {
jjako1d3db972004-01-16 09:56:56 +0000344 if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) {
jjako76032b92004-01-14 06:22:08 +0000345 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
346 "Failed to convert pcodns2!");
347 exit(1);
348 }
349 }
jjako1d3db972004-01-16 09:56:56 +0000350#else
jjako1d3db972004-01-16 09:56:56 +0000351 dns1.s_addr = 0;
352 if (args_info.pcodns1_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000353 dns1.s_addr = inet_addr(args_info.pcodns1_arg);
354 if (dns1.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000355 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
356 "Failed to convert pcodns1!");
357 exit(1);
358 }
359 }
360 dns2.s_addr = 0;
361 if (args_info.pcodns2_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000362 dns2.s_addr = inet_addr(args_info.pcodns2_arg);
363 if (dns2.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000364 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
365 "Failed to convert pcodns2!");
366 exit(1);
367 }
368 }
jjako1d3db972004-01-16 09:56:56 +0000369#endif
370
jjakoa7cd2492003-04-11 09:40:12 +0000371
372 pco.l = 20;
373 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
374 pco.v[1] = 0x80; /* IPCP */
375 pco.v[2] = 0x21;
376 pco.v[3] = 0x10; /* Length of contents */
377 pco.v[4] = 0x02; /* ACK */
378 pco.v[5] = 0x00; /* ID: Need to match request */
379 pco.v[6] = 0x00; /* Length */
380 pco.v[7] = 0x10;
381 pco.v[8] = 0x81; /* DNS 1 */
382 pco.v[9] = 0x06;
383 memcpy(&pco.v[10], &dns1, sizeof(dns1));
384 pco.v[14] = 0x83;
385 pco.v[15] = 0x06; /* DNS 2 */
386 memcpy(&pco.v[16], &dns2, sizeof(dns2));
387
jjako4b26b512003-01-28 16:13:57 +0000388 /* ipup */
389 ipup = args_info.ipup_arg;
390
391 /* ipdown */
392 ipdown = args_info.ipdown_arg;
393
jjako52c24142002-12-16 13:33:51 +0000394 /* Timelimit */
395 timelimit = args_info.timelimit_arg;
396 starttime = time(NULL);
397
398 /* qos */
399 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000400 qos.v[2] = (args_info.qos_arg) & 0xff;
401 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
402 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000403
jjako52c24142002-12-16 13:33:51 +0000404 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000405 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
406 printf("Invalid APN\n");
407 return -1;
jjako52c24142002-12-16 13:33:51 +0000408 }
409 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000410 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000411 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
jjakoe0149782003-07-06 17:07:04 +0000412
413
414 /* foreground */
415 /* If flag not given run as a daemon */
416 if (!args_info.fg_flag)
417 {
418 closelog();
419 /* Close the standard file descriptors. */
420 /* Is this really needed ? */
421 freopen("/dev/null", "w", stdout);
422 freopen("/dev/null", "w", stderr);
423 freopen("/dev/null", "r", stdin);
424 daemon(0, 0);
425 /* Open log again. This time with new pid */
426 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
427 }
428
429 /* pidfile */
430 /* This has to be done after we have our final pid */
431 if (args_info.pidfile_arg) {
432 log_pid(args_info.pidfile_arg);
433 }
jjakoa7cd2492003-04-11 09:40:12 +0000434
jjako52c24142002-12-16 13:33:51 +0000435
436 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
437
jjako1db1c812003-07-06 20:53:57 +0000438 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
jjakoa7cd2492003-04-11 09:40:12 +0000439 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
440 "Failed to create gtp");
441 exit(1);
442 }
jjako08d331d2003-10-13 20:33:30 +0000443 if (gsn->fd0 > maxfd) maxfd = gsn->fd0;
444 if (gsn->fd1c > maxfd) maxfd = gsn->fd1c;
445 if (gsn->fd1u > maxfd) maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000446
jjako08d331d2003-10-13 20:33:30 +0000447 gtp_set_cb_data_ind(gsn, encaps_tun);
jjako52c24142002-12-16 13:33:51 +0000448 gtp_set_cb_delete_context(gsn, delete_context);
jjako08d331d2003-10-13 20:33:30 +0000449 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000450
451
452 /* Create a tunnel interface */
453 if (tun_new((struct tun_t**) &tun)) {
454 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
455 "Failed to create tun");
456 exit(1);
457 }
458
459 tun_setaddr(tun, &net, &net, &mask);
460 tun_set_cb_ind(tun, cb_tun_ind);
461 if (tun->fd > maxfd) maxfd = tun->fd;
462
jjako9c7ff082003-04-11 10:01:41 +0000463 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000464
465 /******************************************************************/
466 /* Main select loop */
467 /******************************************************************/
468
469 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
470
471 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000472 if (tun) FD_SET(tun->fd, &fds);
jjako08d331d2003-10-13 20:33:30 +0000473 FD_SET(gsn->fd0, &fds);
474 FD_SET(gsn->fd1c, &fds);
475 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000476
477 gtp_retranstimeout(gsn, &idleTime);
478 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
jjakoe0149782003-07-06 17:07:04 +0000479 case -1: /* errno == EINTR : unblocked signal */
480 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
481 "select() returned -1");
jjako52c24142002-12-16 13:33:51 +0000482 break;
483 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000484 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000485 gtp_retrans(gsn); /* Only retransmit if nothing else */
486 break;
487 default:
488 break;
489 }
490
jjakoa7cd2492003-04-11 09:40:12 +0000491 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
492 tun_decaps(tun) < 0) {
jjakoe0149782003-07-06 17:07:04 +0000493 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
494 "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000495 }
jjako08d331d2003-10-13 20:33:30 +0000496
497 if (FD_ISSET(gsn->fd0, &fds))
498 gtp_decaps0(gsn);
499
500 if (FD_ISSET(gsn->fd1c, &fds))
501 gtp_decaps1c(gsn);
502
503 if (FD_ISSET(gsn->fd1u, &fds))
504 gtp_decaps1u(gsn);
jjako52c24142002-12-16 13:33:51 +0000505
jjako4b26b512003-01-28 16:13:57 +0000506 }
jjako52c24142002-12-16 13:33:51 +0000507
508 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000509 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000510
511 return 1;
512
513}
514