jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 2 | * OpenGGSN - Gateway GPRS Support Node |
| 3 | * Copyright (C) 2002, 2003 Mondru AB. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 4 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 5 | * 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. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 9 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 10 | * The initial developer of the original code is |
| 11 | * Jens Jakobsen <jj@openggsn.org> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 12 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 13 | * Contributor(s): |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 14 | * |
| 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> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include <sys/socket.h> |
| 42 | #include <sys/ioctl.h> |
| 43 | #include <net/if.h> |
| 44 | #include <features.h> |
| 45 | |
| 46 | #include <errno.h> |
| 47 | |
| 48 | #include <asm/types.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <linux/netlink.h> |
| 51 | |
| 52 | #include <time.h> |
| 53 | |
| 54 | #include "tun.h" |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 55 | #include "ippool.h" |
| 56 | #include "syserr.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 57 | #include "../gtp/pdp.h" |
| 58 | #include "../gtp/gtp.h" |
| 59 | #include "cmdline.h" |
| 60 | |
| 61 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 62 | int maxfd = 0; /* For select() */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 63 | struct tun_t *tun; /* TUN instance */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 64 | |
| 65 | struct in_addr listen_; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 66 | struct in_addr net, mask; /* Network interface */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 67 | struct in_addr dns1, dns2; /* PCO DNS address */ |
| 68 | char *ipup, *ipdown; /* Filename of scripts */ |
| 69 | int debug; /* Print debug output */ |
| 70 | struct ul255_t pco; |
| 71 | struct ul255_t qos; |
| 72 | struct ul255_t apn; |
| 73 | |
| 74 | struct tun_t *tun; /* TUN instance */ |
| 75 | struct ippool_t *ippool; /* Pool of IP addresses */ |
| 76 | struct gsn_t *gsn; /* GSN instance */ |
| 77 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | /* Used to write process ID to file. Assume someone else will delete */ |
| 81 | void log_pid(char *pidfile) { |
| 82 | FILE *file; |
| 83 | mode_t oldmask; |
| 84 | |
| 85 | oldmask = umask(022); |
| 86 | file = fopen(pidfile, "w"); |
| 87 | umask(oldmask); |
| 88 | if(!file) |
| 89 | return; |
| 90 | fprintf(file, "%d\n", getpid()); |
| 91 | fclose(file); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | int encaps_printf(void *p, void *packet, unsigned len) |
| 96 | { |
| 97 | int i; |
| 98 | if (debug) { |
| 99 | printf("The packet looks like this:\n"); |
| 100 | for( i=0; i<len; i++) { |
| 101 | printf("%02x ", (unsigned char)*(char *)(packet+i)); |
| 102 | if (!((i+1)%16)) printf("\n"); |
| 103 | }; |
| 104 | printf("\n"); |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 109 | int delete_context(struct pdp_t *pdp) { |
jjako | 4901471 | 2003-01-05 17:59:49 +0000 | [diff] [blame] | 110 | if (debug) printf("Deleting PDP context\n"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 111 | ippool_freeip((struct ippoolm_t *) pdp->peer); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 116 | int create_context(struct pdp_t *pdp) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 117 | struct in_addr addr; |
| 118 | struct ippoolm_t *member; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 119 | |
| 120 | if (debug) printf("Received create PDP context request\n"); |
| 121 | |
| 122 | pdp->eua.l=0; /* TODO: Indicates dynamic IP */ |
| 123 | |
| 124 | /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */ |
| 125 | memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 126 | memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 127 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 128 | if (pdp_euaton(&pdp->eua, &addr)) { |
| 129 | addr.s_addr = 0; /* Request dynamic */ |
| 130 | } |
| 131 | |
| 132 | if (ippool_newip(ippool, &member, &addr)) { |
| 133 | return EOF; /* Allready in use, or no more available */ |
| 134 | } |
| 135 | |
| 136 | pdp_ntoeua(&member->addr, &pdp->eua); |
| 137 | pdp->peer = &member; |
| 138 | pdp->ipif = tun; /* TODO */ |
| 139 | member->peer = pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 140 | |
| 141 | return 0; /* Success */ |
| 142 | } |
| 143 | |
| 144 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 145 | /* Callback for receiving messages from tun */ |
| 146 | int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) { |
| 147 | struct ippoolm_t *ipm; |
| 148 | struct in_addr dst; |
| 149 | struct tun_packet_t *iph = (struct tun_packet_t*) pack; |
| 150 | |
| 151 | dst.s_addr = iph->dst; |
| 152 | |
| 153 | if (ippool_getip(ippool, &ipm, &dst)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 154 | if (debug) printf("Received packet with no destination!!!\n"); |
| 155 | return 0; |
| 156 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 157 | |
| 158 | if (ipm->peer) /* Check if a peer protocol is defined */ |
| 159 | gtp_gpdu(gsn, (struct pdp_t*) ipm->peer, pack, len); |
| 160 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 161 | } |
| 162 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 163 | int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) { |
| 164 | /* printf("encaps_tun. Packet received: forwarding to tun\n");*/ |
| 165 | return tun_encaps((struct tun_t*) pdp->ipif, pack, len); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | int main(int argc, char **argv) |
| 170 | { |
| 171 | /* gengeopt declarations */ |
| 172 | struct gengetopt_args_info args_info; |
| 173 | |
| 174 | struct hostent *host; |
| 175 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 176 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 177 | fd_set fds; /* For select() */ |
| 178 | struct timeval idleTime; /* How long to select() */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 179 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 180 | |
| 181 | int timelimit; /* Number of seconds to be connected */ |
| 182 | int starttime; /* Time program was started */ |
| 183 | |
| 184 | /* open a connection to the syslog daemon */ |
| 185 | /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/ |
| 186 | openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON); |
| 187 | |
| 188 | if (cmdline_parser (argc, argv, &args_info) != 0) |
| 189 | exit(1); |
| 190 | if (args_info.debug_flag) { |
| 191 | printf("listen: %s\n", args_info.listen_arg); |
| 192 | printf("conf: %s\n", args_info.conf_arg); |
| 193 | printf("fg: %d\n", args_info.fg_flag); |
| 194 | printf("debug: %d\n", args_info.debug_flag); |
| 195 | printf("qos: %#08x\n", args_info.qos_arg); |
| 196 | printf("apn: %s\n", args_info.apn_arg); |
| 197 | printf("net: %s\n", args_info.net_arg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 198 | printf("dynip: %s\n", args_info.dynip_arg); |
| 199 | printf("statip: %s\n", args_info.statip_arg); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 200 | printf("ipup: %s\n", args_info.ipup_arg); |
| 201 | printf("ipdown: %s\n", args_info.ipdown_arg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 202 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 203 | printf("statedir: %s\n", args_info.statedir_arg); |
| 204 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 205 | } |
| 206 | |
| 207 | /* Try out our new parser */ |
| 208 | |
| 209 | if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0) |
| 210 | exit(1); |
| 211 | if (args_info.debug_flag) { |
| 212 | printf("cmdline_parser_configfile\n"); |
| 213 | printf("listen: %s\n", args_info.listen_arg); |
| 214 | printf("conf: %s\n", args_info.conf_arg); |
| 215 | printf("fg: %d\n", args_info.fg_flag); |
| 216 | printf("debug: %d\n", args_info.debug_flag); |
| 217 | printf("qos: %#08x\n", args_info.qos_arg); |
| 218 | printf("apn: %s\n", args_info.apn_arg); |
| 219 | printf("net: %s\n", args_info.net_arg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 220 | printf("dynip: %s\n", args_info.dynip_arg); |
| 221 | printf("statip: %s\n", args_info.statip_arg); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 222 | printf("ipup: %s\n", args_info.ipup_arg); |
| 223 | printf("ipdown: %s\n", args_info.ipdown_arg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 224 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 225 | printf("statedir: %s\n", args_info.statedir_arg); |
| 226 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 227 | } |
| 228 | |
| 229 | /* Handle each option */ |
| 230 | |
| 231 | /* foreground */ |
| 232 | /* If flag not given run as a daemon */ |
| 233 | if (!args_info.fg_flag) |
| 234 | { |
| 235 | closelog(); |
| 236 | /* Close the standard file descriptors. */ |
| 237 | /* Is this really needed ? */ |
| 238 | freopen("/dev/null", "w", stdout); |
| 239 | freopen("/dev/null", "w", stderr); |
| 240 | freopen("/dev/null", "r", stdin); |
| 241 | daemon(0, 0); |
| 242 | /* Open log again. This time with new pid */ |
| 243 | openlog(PACKAGE, LOG_PID, LOG_DAEMON); |
| 244 | } |
| 245 | |
| 246 | /* debug */ |
| 247 | debug = args_info.debug_flag; |
| 248 | |
| 249 | /* pidfile */ |
| 250 | /* This has to be done after we have our final pid */ |
| 251 | if (args_info.pidfile_arg) { |
| 252 | log_pid(args_info.pidfile_arg); |
| 253 | } |
| 254 | |
| 255 | /* listen */ |
| 256 | /* If no listen option is specified listen to any local port */ |
| 257 | /* Do hostname lookup to translate hostname to IP address */ |
| 258 | if (args_info.listen_arg) { |
| 259 | if (!(host = gethostbyname(args_info.listen_arg))) { |
| 260 | fprintf(stderr, "%s: Invalid listening address: %s!\n", |
| 261 | PACKAGE, args_info.listen_arg); |
| 262 | syslog(LOG_ERR, "Invalid listening address: %s!", |
| 263 | args_info.listen_arg); |
| 264 | return 1; |
| 265 | } |
| 266 | else { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 267 | memcpy(&listen_.s_addr, host->h_addr, host->h_length); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | else { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 271 | listen_.s_addr = htonl(INADDR_ANY); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* net */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 275 | /* Store net as in_addr net and mask */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 276 | if (args_info.net_arg) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 277 | if(ippool_aton(&net, &mask, args_info.net_arg, 0)) { |
| 278 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 279 | "Invalid network address: %s!", args_info.net_arg); |
| 280 | return -1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 284 | /* dynip */ |
| 285 | if (!args_info.dynip_arg) { |
| 286 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 287 | "No dynamic address pool given!"); |
| 288 | return -1; |
| 289 | } |
| 290 | else { |
| 291 | if (ippool_new(&ippool, args_info.dynip_arg, |
| 292 | IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) { |
| 293 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 294 | "Failed to allocate IP pool!"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 298 | /* DNS1 and DNS2 */ |
| 299 | dns1.s_addr = 0; |
| 300 | if (args_info.pcodns1_arg) |
| 301 | inet_aton(args_info.pcodns1_arg, &dns1); |
| 302 | |
| 303 | dns2.s_addr = 0; |
| 304 | if (args_info.pcodns2_arg) |
| 305 | inet_aton(args_info.pcodns2_arg, &dns2); |
| 306 | |
| 307 | pco.l = 20; |
| 308 | pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */ |
| 309 | pco.v[1] = 0x80; /* IPCP */ |
| 310 | pco.v[2] = 0x21; |
| 311 | pco.v[3] = 0x10; /* Length of contents */ |
| 312 | pco.v[4] = 0x02; /* ACK */ |
| 313 | pco.v[5] = 0x00; /* ID: Need to match request */ |
| 314 | pco.v[6] = 0x00; /* Length */ |
| 315 | pco.v[7] = 0x10; |
| 316 | pco.v[8] = 0x81; /* DNS 1 */ |
| 317 | pco.v[9] = 0x06; |
| 318 | memcpy(&pco.v[10], &dns1, sizeof(dns1)); |
| 319 | pco.v[14] = 0x83; |
| 320 | pco.v[15] = 0x06; /* DNS 2 */ |
| 321 | memcpy(&pco.v[16], &dns2, sizeof(dns2)); |
| 322 | |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 323 | /* ipup */ |
| 324 | ipup = args_info.ipup_arg; |
| 325 | |
| 326 | /* ipdown */ |
| 327 | ipdown = args_info.ipdown_arg; |
| 328 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 329 | /* Timelimit */ |
| 330 | timelimit = args_info.timelimit_arg; |
| 331 | starttime = time(NULL); |
| 332 | |
| 333 | /* qos */ |
| 334 | qos.l = 3; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 335 | qos.v[2] = (args_info.qos_arg) & 0xff; |
| 336 | qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff; |
| 337 | qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 338 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 339 | /* apn */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 340 | if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) { |
| 341 | printf("Invalid APN\n"); |
| 342 | return -1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 343 | } |
| 344 | apn.l = strlen(args_info.apn_arg) + 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 345 | apn.v[0] = (char) strlen(args_info.apn_arg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 346 | strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1); |
| 347 | |
| 348 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 349 | |
| 350 | if (debug) printf("gtpclient: Initialising GTP tunnel\n"); |
| 351 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 352 | if (gtp_new(&gsn, args_info.statedir_arg, &listen_)) { |
| 353 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 354 | "Failed to create gtp"); |
| 355 | exit(1); |
| 356 | } |
| 357 | if (gsn->fd > maxfd) maxfd = gsn->fd; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | gtp_set_cb_gpdu(gsn, encaps_tun); |
| 361 | gtp_set_cb_delete_context(gsn, delete_context); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 362 | gtp_set_cb_create_context(gsn, create_context); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 363 | |
| 364 | |
| 365 | /* Create a tunnel interface */ |
| 366 | if (tun_new((struct tun_t**) &tun)) { |
| 367 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 368 | "Failed to create tun"); |
| 369 | exit(1); |
| 370 | } |
| 371 | |
| 372 | tun_setaddr(tun, &net, &net, &mask); |
| 373 | tun_set_cb_ind(tun, cb_tun_ind); |
| 374 | if (tun->fd > maxfd) maxfd = tun->fd; |
| 375 | |
| 376 | if (ipup) { |
| 377 | char buf[1024]; |
| 378 | char snet[100]; |
| 379 | char smask[100]; |
| 380 | |
| 381 | strncpy(snet, inet_ntoa(net), sizeof(snet)); |
| 382 | snet[sizeof(snet)-1] = 0; |
| 383 | strncpy(smask, inet_ntoa(mask), sizeof(smask)); |
| 384 | smask[sizeof(smask)-1] = 0; |
| 385 | |
| 386 | /* system("ipup /dev/tun0 192.168.0.10"); */ |
| 387 | snprintf(buf, sizeof(buf), "%s %s %s %s", |
| 388 | ipup, tun->devname, snet, smask); |
| 389 | buf[sizeof(buf)-1] = 0; |
| 390 | if (debug) printf("%s\n", buf); |
| 391 | system(buf); |
| 392 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 393 | |
| 394 | /******************************************************************/ |
| 395 | /* Main select loop */ |
| 396 | /******************************************************************/ |
| 397 | |
| 398 | while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) { |
| 399 | |
| 400 | FD_ZERO(&fds); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 401 | if (tun) FD_SET(tun->fd, &fds); |
| 402 | FD_SET(gsn->fd, &fds); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 403 | |
| 404 | gtp_retranstimeout(gsn, &idleTime); |
| 405 | switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) { |
| 406 | case -1: /* Error with select() * |
| 407 | if (errno != EINTR) |
| 408 | syslog(LOG_ERR, "CTRL: Error with select(), quitting"); |
| 409 | *goto leave_clear_call;*/ |
| 410 | syslog(LOG_ERR, "GGSN: select = -1"); |
| 411 | break; |
| 412 | case 0: |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 413 | /* printf("Select returned 0\n"); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 414 | gtp_retrans(gsn); /* Only retransmit if nothing else */ |
| 415 | break; |
| 416 | default: |
| 417 | break; |
| 418 | } |
| 419 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 420 | if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) && |
| 421 | tun_decaps(tun) < 0) { |
| 422 | syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun->fd); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 423 | } |
| 424 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 425 | if (FD_ISSET(gsn->fd, &fds)) |
| 426 | gtp_decaps(gsn); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 427 | |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 428 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 429 | |
| 430 | gtp_free(gsn); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame^] | 431 | tun_free(tun); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 432 | |
| 433 | return 1; |
| 434 | |
| 435 | } |
| 436 | |