main.c (8fa6ebe47d24a6621f1be15f3157e2e1a046596e) main.c (7cf368eb42edc6d7738f431289a1e3e2203421e6)
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.143 1998/09/17 00:45:27 brian Exp $
20 * $Id: main.c,v 1.144 1998/10/22 02:32:49 brian Exp $
21 *
22 * TODO:
23 */
24
25#include <sys/types.h>
26#include <netinet/in.h>
27#include <netinet/in_systm.h>
28#include <netinet/ip.h>

--- 145 unchanged lines hidden (view full) ---

174static void
175Usage(void)
176{
177 fprintf(stderr,
178 "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
179#ifndef NOALIAS
180 " [ -alias ]"
181#endif
21 *
22 * TODO:
23 */
24
25#include <sys/types.h>
26#include <netinet/in.h>
27#include <netinet/in_systm.h>
28#include <netinet/ip.h>

--- 145 unchanged lines hidden (view full) ---

174static void
175Usage(void)
176{
177 fprintf(stderr,
178 "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
179#ifndef NOALIAS
180 " [ -alias ]"
181#endif
182 " [system]\n");
182 " [system ...]\n");
183 exit(EX_START);
184}
185
183 exit(EX_START);
184}
185
186static char *
186static int
187ProcessArgs(int argc, char **argv, int *mode, int *alias)
188{
187ProcessArgs(int argc, char **argv, int *mode, int *alias)
188{
189 int optc, labelrequired, newmode;
189 int optc, newmode, arg;
190 char *cp;
191
190 char *cp;
191
192 optc = labelrequired = 0;
192 optc = 0;
193 *mode = PHYS_INTERACTIVE;
194 *alias = 0;
193 *mode = PHYS_INTERACTIVE;
194 *alias = 0;
195 while (argc > 0 && **argv == '-') {
196 cp = *argv + 1;
195 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
196 cp = argv[arg] + 1;
197 newmode = Nam2mode(cp);
198 switch (newmode) {
199 case PHYS_NONE:
200 if (strcmp(cp, "alias") == 0) {
201#ifdef NOALIAS
197 newmode = Nam2mode(cp);
198 switch (newmode) {
199 case PHYS_NONE:
200 if (strcmp(cp, "alias") == 0) {
201#ifdef NOALIAS
202 log_Printf(LogWARN, "Cannot load alias library\n");
202 log_Printf(LogWARN, "Cannot load alias library (compiled out)\n");
203#else
204 *alias = 1;
205#endif
206 optc--; /* this option isn't exclusive */
207 } else
208 Usage();
209 break;
210
211 case PHYS_ALL:
212 Usage();
213 break;
214
203#else
204 *alias = 1;
205#endif
206 optc--; /* this option isn't exclusive */
207 } else
208 Usage();
209 break;
210
211 case PHYS_ALL:
212 Usage();
213 break;
214
215 case PHYS_AUTO:
216 case PHYS_BACKGROUND:
217 case PHYS_DDIAL:
218 labelrequired = 1;
219 /* fall through */
220
221 default:
222 *mode = newmode;
223 }
215 default:
216 *mode = newmode;
217 }
224 optc++;
225 argv++;
226 argc--;
227 }
228
218 }
219
229 if (argc > 1) {
230 fprintf(stderr, "You may specify only one system label.\n");
231 exit(EX_START);
232 }
233
234 if (optc > 1) {
235 fprintf(stderr, "You may specify only one mode.\n");
236 exit(EX_START);
237 }
238
220 if (optc > 1) {
221 fprintf(stderr, "You may specify only one mode.\n");
222 exit(EX_START);
223 }
224
239 if (labelrequired && argc != 1) {
240 fprintf(stderr, "Destination system must be specified in"
241 " auto, background or ddial mode.\n");
225 if (*mode == PHYS_AUTO && arg == argc) {
226 fprintf(stderr, "A system must be specified in auto mode.\n");
242 exit(EX_START);
243 }
244
227 exit(EX_START);
228 }
229
245 return argc == 1 ? *argv : NULL; /* Don't SetLabel yet ! */
230 return arg; /* Don't SetLabel yet ! */
246}
247
231}
232
233static void
234CheckLabel(const char *label, struct prompt *prompt, int mode)
235{
236 const char *err;
237
238 if ((err = system_IsValid(label, prompt, mode)) != NULL) {
239 fprintf(stderr, "You may not use ppp in this mode with this label\n");
240 fprintf(stderr, "%s: %s\n", label, err);
241 if (mode == PHYS_DIRECT)
242 log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
243 label, err);
244 log_Close();
245 exit(1);
246 }
247}
248
249
248int
249main(int argc, char **argv)
250{
250int
251main(int argc, char **argv)
252{
251 char *name, *label;
252 int nfds, mode, alias;
253 char *name;
254 int nfds, mode, alias, label, arg;
253 struct bundle *bundle;
254 struct prompt *prompt;
255
256 nfds = getdtablesize();
257 if (nfds >= FD_SETSIZE)
258 /*
259 * If we've got loads of file descriptors, make sure they're all
260 * closed. If they aren't, we may end up with a seg fault when our
261 * `fd_set's get too big when select()ing !
262 */
263 while (--nfds > 2)
264 close(nfds);
265
266 name = strrchr(argv[0], '/');
267 log_Open(name ? name + 1 : argv[0]);
268
269#ifndef NOALIAS
270 PacketAliasInit();
271#endif
255 struct bundle *bundle;
256 struct prompt *prompt;
257
258 nfds = getdtablesize();
259 if (nfds >= FD_SETSIZE)
260 /*
261 * If we've got loads of file descriptors, make sure they're all
262 * closed. If they aren't, we may end up with a seg fault when our
263 * `fd_set's get too big when select()ing !
264 */
265 while (--nfds > 2)
266 close(nfds);
267
268 name = strrchr(argv[0], '/');
269 log_Open(name ? name + 1 : argv[0]);
270
271#ifndef NOALIAS
272 PacketAliasInit();
273#endif
272 label = ProcessArgs(argc - 1, argv + 1, &mode, &alias);
274 label = ProcessArgs(argc, argv, &mode, &alias);
273
274#ifdef __FreeBSD__
275 /*
276 * A FreeBSD hack to dodge a bug in the tty driver that drops output
277 * occasionally.... I must find the real reason some time. To display
278 * the dodgy behaviour, comment out this bit, make yourself a large
279 * routing table and then run ppp in interactive mode. The `show route'
280 * command will drop chunks of data !!!

--- 27 unchanged lines hidden (view full) ---

308 return -1;
309 }
310 ptr = conf + strlen(conf)-2;
311 while (ptr > conf && *ptr != '/')
312 *ptr-- = '\0';
313 } while (ptr >= conf);
314 }
315
275
276#ifdef __FreeBSD__
277 /*
278 * A FreeBSD hack to dodge a bug in the tty driver that drops output
279 * occasionally.... I must find the real reason some time. To display
280 * the dodgy behaviour, comment out this bit, make yourself a large
281 * routing table and then run ppp in interactive mode. The `show route'
282 * command will drop chunks of data !!!

--- 27 unchanged lines hidden (view full) ---

310 return -1;
311 }
312 ptr = conf + strlen(conf)-2;
313 while (ptr > conf && *ptr != '/')
314 *ptr-- = '\0';
315 } while (ptr >= conf);
316 }
317
316 if (!system_IsValid(label, prompt, mode)) {
317 fprintf(stderr, "You may not use ppp in this mode with this label\n");
318 if (mode == PHYS_DIRECT) {
319 const char *l;
320 l = label ? label : "default";
321 log_Printf(LogWARN, "Label %s rejected -direct connection\n", l);
322 }
323 log_Close();
324 return 1;
325 }
318 if (label < argc)
319 for (arg = label; arg < argc; arg++)
320 CheckLabel(argv[arg], prompt, mode);
321 else
322 CheckLabel("default", prompt, mode);
326
327 if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) {
328 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno));
329 return EX_START;
330 }
331 if (prompt) {
332 prompt->bundle = bundle; /* couldn't do it earlier */
333 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);

--- 13 unchanged lines hidden (view full) ---

347 sig_signal(SIGALRM, SIG_IGN);
348 signal(SIGPIPE, SIG_IGN);
349
350 if (mode == PHYS_INTERACTIVE)
351 sig_signal(SIGTSTP, TerminalStop);
352
353 sig_signal(SIGUSR2, BringDownServer);
354
323
324 if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) {
325 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno));
326 return EX_START;
327 }
328 if (prompt) {
329 prompt->bundle = bundle; /* couldn't do it earlier */
330 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);

--- 13 unchanged lines hidden (view full) ---

344 sig_signal(SIGALRM, SIG_IGN);
345 signal(SIGPIPE, SIG_IGN);
346
347 if (mode == PHYS_INTERACTIVE)
348 sig_signal(SIGTSTP, TerminalStop);
349
350 sig_signal(SIGUSR2, BringDownServer);
351
355 if (label) {
356 /*
357 * Set label both before and after system_Select !
358 * This way, "set enddisc label" works during system_Select, and we
359 * also end up with the correct label if we have embedded load
360 * commands.
361 */
362 bundle_SetLabel(bundle, label);
363 if (system_Select(bundle, label, CONFFILE, prompt, NULL) < 0) {
364 prompt_Printf(prompt, "Destination system (%s) not found.\n", label);
365 AbortProgram(EX_START);
366 }
367 bundle_SetLabel(bundle, label);
368 if (mode == PHYS_AUTO &&
369 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
370 prompt_Printf(prompt, "You must \"set ifaddr\" with a peer address "
371 "in label %s for auto mode.\n", label);
372 AbortProgram(EX_START);
373 }
352 for (arg = label; arg < argc; arg++) {
353 /* In case we use LABEL or ``set enddisc label'' */
354 bundle_SetLabel(bundle, argv[argc - 1]);
355 system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
374 }
375
356 }
357
358 if (label < argc)
359 /* In case the last label did a ``load'' */
360 bundle_SetLabel(bundle, argv[argc - 1]);
361
362 if (mode == PHYS_AUTO &&
363 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
364 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
365 "in auto mode.\n");
366 AbortProgram(EX_START);
367 }
368
376 if (mode != PHYS_INTERACTIVE) {
377 if (mode != PHYS_DIRECT) {
378 int bgpipe[2];
379 pid_t bgpid;
380
381 if (mode == PHYS_BACKGROUND && pipe(bgpipe)) {
382 log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
383 AbortProgram(EX_SOCK);

--- 179 unchanged lines hidden ---
369 if (mode != PHYS_INTERACTIVE) {
370 if (mode != PHYS_DIRECT) {
371 int bgpipe[2];
372 pid_t bgpid;
373
374 if (mode == PHYS_BACKGROUND && pipe(bgpipe)) {
375 log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
376 AbortProgram(EX_SOCK);

--- 179 unchanged lines hidden ---