main.c (49581) | main.c (50059) |
---|---|
1/* 2 * User Process PPP 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * | 1/* 2 * User Process PPP 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * |
20 * $Id: main.c,v 1.155 1999/05/13 16:34:57 brian Exp $ | 20 * $Id: main.c,v 1.156 1999/08/09 22:54:51 brian Exp $ |
21 * 22 * TODO: 23 */ 24 25#include <sys/param.h> 26#include <netinet/in.h> 27#include <netinet/in_systm.h> 28#include <netinet/ip.h> --- 5 unchanged lines hidden (view full) --- 34#include <signal.h> 35#include <stdio.h> 36#include <string.h> 37#include <sys/time.h> 38#include <termios.h> 39#include <unistd.h> 40#include <sys/stat.h> 41 | 21 * 22 * TODO: 23 */ 24 25#include <sys/param.h> 26#include <netinet/in.h> 27#include <netinet/in_systm.h> 28#include <netinet/ip.h> --- 5 unchanged lines hidden (view full) --- 34#include <signal.h> 35#include <stdio.h> 36#include <string.h> 37#include <sys/time.h> 38#include <termios.h> 39#include <unistd.h> 40#include <sys/stat.h> 41 |
42#ifndef NOALIAS | 42#ifndef NONAT |
43#ifdef __FreeBSD__ 44#include <alias.h> 45#else 46#include "alias.h" 47#endif 48#endif 49#include "layer.h" 50#include "probe.h" --- 125 unchanged lines hidden (view full) --- 176 snprintf(num, sizeof num, "%d", ex); 177 return num; 178} 179 180static void 181Usage(void) 182{ 183 fprintf(stderr, | 43#ifdef __FreeBSD__ 44#include <alias.h> 45#else 46#include "alias.h" 47#endif 48#endif 49#include "layer.h" 50#include "probe.h" --- 125 unchanged lines hidden (view full) --- 176 snprintf(num, sizeof num, "%d", ex); 177 return num; 178} 179 180static void 181Usage(void) 182{ 183 fprintf(stderr, |
184 "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]" | 184 "Usage: ppp [-auto | -foreground | -background | -direct | -dedicated | -ddial | -interactive]" |
185#ifndef NOALIAS | 185#ifndef NOALIAS |
186 " [ -alias ]" | 186 " [-nat]" |
187#endif 188 " [system ...]\n"); 189 exit(EX_START); 190} 191 192static int | 187#endif 188 " [system ...]\n"); 189 exit(EX_START); 190} 191 192static int |
193ProcessArgs(int argc, char **argv, int *mode, int *alias) | 193ProcessArgs(int argc, char **argv, int *mode, int *nat, int *fg, int *quiet) |
194{ 195 int optc, newmode, arg; 196 char *cp; 197 198 optc = 0; 199 *mode = PHYS_INTERACTIVE; | 194{ 195 int optc, newmode, arg; 196 char *cp; 197 198 optc = 0; 199 *mode = PHYS_INTERACTIVE; |
200 *alias = 0; | 200 *nat = 0; 201 *fg = 0; 202 *quiet = 0; |
201 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) { 202 cp = argv[arg] + 1; 203 newmode = Nam2mode(cp); 204 switch (newmode) { 205 case PHYS_NONE: | 203 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) { 204 cp = argv[arg] + 1; 205 newmode = Nam2mode(cp); 206 switch (newmode) { 207 case PHYS_NONE: |
206 if (strcmp(cp, "alias") == 0) { 207#ifdef NOALIAS 208 log_Printf(LogWARN, "Cannot load alias library (compiled out)\n"); | 208 if (strcmp(cp, "nat") == 0 || strcmp(cp, "alias") == 0) { 209#ifdef NONAT 210 log_Printf(LogWARN, "Cannot load libalias (compiled out)\n"); |
209#else | 211#else |
210 *alias = 1; | 212 *nat = 1; |
211#endif 212 optc--; /* this option isn't exclusive */ | 213#endif 214 optc--; /* this option isn't exclusive */ |
215 } else if (strcmp(cp, "quiet") == 0) { 216 *quiet = 1; 217 optc--; /* this option isn't exclusive */ 218 } else if (strcmp(cp, "foreground") == 0) { 219 *mode = PHYS_BACKGROUND; /* Kinda like background mode */ 220 *fg = 1; |
|
213 } else 214 Usage(); 215 break; 216 217 case PHYS_ALL: 218 Usage(); 219 break; 220 --- 31 unchanged lines hidden (view full) --- 252} 253 254 255int 256main(int argc, char **argv) 257{ 258 char *name; 259 const char *lastlabel; | 221 } else 222 Usage(); 223 break; 224 225 case PHYS_ALL: 226 Usage(); 227 break; 228 --- 31 unchanged lines hidden (view full) --- 260} 261 262 263int 264main(int argc, char **argv) 265{ 266 char *name; 267 const char *lastlabel; |
260 int nfds, mode, alias, label, arg; | 268 int nfds, mode, nat, fg, quiet, label, arg; |
261 struct bundle *bundle; 262 struct prompt *prompt; 263 264 nfds = getdtablesize(); 265 if (nfds >= FD_SETSIZE) 266 /* 267 * If we've got loads of file descriptors, make sure they're all 268 * closed. If they aren't, we may end up with a seg fault when our 269 * `fd_set's get too big when select()ing ! 270 */ 271 while (--nfds > 2) 272 close(nfds); 273 274 name = strrchr(argv[0], '/'); 275 log_Open(name ? name + 1 : argv[0]); 276 | 269 struct bundle *bundle; 270 struct prompt *prompt; 271 272 nfds = getdtablesize(); 273 if (nfds >= FD_SETSIZE) 274 /* 275 * If we've got loads of file descriptors, make sure they're all 276 * closed. If they aren't, we may end up with a seg fault when our 277 * `fd_set's get too big when select()ing ! 278 */ 279 while (--nfds > 2) 280 close(nfds); 281 282 name = strrchr(argv[0], '/'); 283 log_Open(name ? name + 1 : argv[0]); 284 |
277#ifndef NOALIAS | 285#ifndef NONAT |
278 PacketAliasInit(); 279#endif | 286 PacketAliasInit(); 287#endif |
280 label = ProcessArgs(argc, argv, &mode, &alias); | 288 label = ProcessArgs(argc, argv, &mode, &nat, &fg, &quiet); |
281 282 /* 283 * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops 284 * output occasionally.... I must find the real reason some time. To 285 * display the dodgy behaviour, comment out this bit, make yourself a large 286 * routing table and then run ppp in interactive mode. The `show route' 287 * command will drop chunks of data !!! 288 */ --- 31 unchanged lines hidden (view full) --- 320 } 321 322 if (label < argc) 323 for (arg = label; arg < argc; arg++) 324 CheckLabel(argv[arg], prompt, mode); 325 else 326 CheckLabel("default", prompt, mode); 327 | 289 290 /* 291 * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops 292 * output occasionally.... I must find the real reason some time. To 293 * display the dodgy behaviour, comment out this bit, make yourself a large 294 * routing table and then run ppp in interactive mode. The `show route' 295 * command will drop chunks of data !!! 296 */ --- 31 unchanged lines hidden (view full) --- 328 } 329 330 if (label < argc) 331 for (arg = label; arg < argc; arg++) 332 CheckLabel(argv[arg], prompt, mode); 333 else 334 CheckLabel("default", prompt, mode); 335 |
328 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode)); | 336 if (!quiet) 337 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode)); |
329 330 if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) { 331 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno)); 332 return EX_START; 333 } 334 335 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ 336 337 if (prompt) { 338 prompt->bundle = bundle; /* couldn't do it earlier */ | 338 339 if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) { 340 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno)); 341 return EX_START; 342 } 343 344 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ 345 346 if (prompt) { 347 prompt->bundle = bundle; /* couldn't do it earlier */ |
339 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); | 348 if (!quiet) 349 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); |
340 } 341 SignalBundle = bundle; | 350 } 351 SignalBundle = bundle; |
342 bundle->AliasEnabled = alias; 343 if (alias) | 352 bundle->NatEnabled = nat; 353 if (nat) |
344 bundle->cfg.opt |= OPT_IFACEALIAS; 345 346 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 347 prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 348 349 sig_signal(SIGHUP, CloseSession); 350 sig_signal(SIGTERM, CloseSession); 351 sig_signal(SIGINT, CloseConnection); --- 22 unchanged lines hidden (view full) --- 374 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 375 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 376 "in auto mode.\n"); 377 AbortProgram(EX_START); 378 } 379 380 if (mode != PHYS_INTERACTIVE) { 381 if (mode != PHYS_DIRECT) { | 354 bundle->cfg.opt |= OPT_IFACEALIAS; 355 356 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 357 prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 358 359 sig_signal(SIGHUP, CloseSession); 360 sig_signal(SIGTERM, CloseSession); 361 sig_signal(SIGINT, CloseConnection); --- 22 unchanged lines hidden (view full) --- 384 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 385 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 386 "in auto mode.\n"); 387 AbortProgram(EX_START); 388 } 389 390 if (mode != PHYS_INTERACTIVE) { 391 if (mode != PHYS_DIRECT) { |
382 int bgpipe[2]; 383 pid_t bgpid; | 392 if (!fg) { 393 int bgpipe[2]; 394 pid_t bgpid; |
384 | 395 |
385 if (mode == PHYS_BACKGROUND && pipe(bgpipe)) { 386 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 387 AbortProgram(EX_SOCK); 388 } | 396 if (mode == PHYS_BACKGROUND && pipe(bgpipe)) { 397 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 398 AbortProgram(EX_SOCK); 399 } |
389 | 400 |
390 bgpid = fork(); 391 if (bgpid == -1) { 392 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 393 AbortProgram(EX_SOCK); 394 } | 401 bgpid = fork(); 402 if (bgpid == -1) { 403 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 404 AbortProgram(EX_SOCK); 405 } |
395 | 406 |
396 if (bgpid) { 397 char c = EX_NORMAL; | 407 if (bgpid) { 408 char c = EX_NORMAL; |
398 | 409 |
399 if (mode == PHYS_BACKGROUND) { 400 close(bgpipe[1]); 401 BGPid = bgpid; 402 /* If we get a signal, kill the child */ 403 signal(SIGHUP, KillChild); 404 signal(SIGTERM, KillChild); 405 signal(SIGINT, KillChild); 406 signal(SIGQUIT, KillChild); | 410 if (mode == PHYS_BACKGROUND) { 411 close(bgpipe[1]); 412 BGPid = bgpid; 413 /* If we get a signal, kill the child */ 414 signal(SIGHUP, KillChild); 415 signal(SIGTERM, KillChild); 416 signal(SIGINT, KillChild); 417 signal(SIGQUIT, KillChild); |
407 | 418 |
408 /* Wait for our child to close its pipe before we exit */ 409 if (read(bgpipe[0], &c, 1) != 1) { 410 prompt_Printf(prompt, "Child exit, no status.\n"); 411 log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 412 } else if (c == EX_NORMAL) { 413 prompt_Printf(prompt, "PPP enabled.\n"); 414 log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 415 } else { 416 prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 417 log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 418 ex_desc((int) c)); | 419 /* Wait for our child to close its pipe before we exit */ 420 if (read(bgpipe[0], &c, 1) != 1) { 421 prompt_Printf(prompt, "Child exit, no status.\n"); 422 log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 423 } else if (c == EX_NORMAL) { 424 prompt_Printf(prompt, "PPP enabled.\n"); 425 log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 426 } else { 427 prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 428 log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 429 ex_desc((int) c)); 430 } 431 close(bgpipe[0]); |
419 } | 432 } |
433 return c; 434 } else if (mode == PHYS_BACKGROUND) { |
|
420 close(bgpipe[0]); | 435 close(bgpipe[0]); |
421 } 422 return c; 423 } else if (mode == PHYS_BACKGROUND) { 424 close(bgpipe[0]); 425 bundle->notify.fd = bgpipe[1]; | 436 bundle->notify.fd = bgpipe[1]; 437 } 438 439 bundle_LockTun(bundle); /* we have a new pid */ |
426 } 427 | 440 } 441 |
428 bundle_LockTun(bundle); /* we have a new pid */ 429 430 /* -auto, -dedicated, -ddial & -background */ | 442 /* -auto, -dedicated, -ddial, -foreground & -background */ |
431 prompt_Destroy(prompt, 0); 432 close(STDOUT_FILENO); 433 close(STDERR_FILENO); 434 close(STDIN_FILENO); | 443 prompt_Destroy(prompt, 0); 444 close(STDOUT_FILENO); 445 close(STDERR_FILENO); 446 close(STDIN_FILENO); |
435 setsid(); | 447 if (!fg) 448 setsid(); |
436 } else { | 449 } else { |
437 /* -direct: STDIN_FILENO gets used by modem_Open */ | 450 /* -direct - STDIN_FILENO gets used by physical_Open */ |
438 prompt_TtyInit(NULL); 439 close(STDOUT_FILENO); 440 close(STDERR_FILENO); 441 } 442 } else { | 451 prompt_TtyInit(NULL); 452 close(STDOUT_FILENO); 453 close(STDERR_FILENO); 454 } 455 } else { |
443 /* Interactive mode */ | 456 /* -interactive */ |
444 close(STDERR_FILENO); 445 prompt_TtyInit(prompt); 446 prompt_TtyCommandMode(prompt); 447 prompt_Required(prompt); 448 } 449 450 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode)); 451 DoLoop(bundle); --- 131 unchanged lines hidden --- | 457 close(STDERR_FILENO); 458 prompt_TtyInit(prompt); 459 prompt_TtyCommandMode(prompt); 460 prompt_Required(prompt); 461 } 462 463 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode)); 464 DoLoop(bundle); --- 131 unchanged lines hidden --- |