]>
git.saurik.com Git - apple/network_cmds.git/blob - startslip.tproj/startslip.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1990, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
63 #include <sys/termios.h>
64 #include <sys/ioctl.h>
68 #include <sys/socket.h>
69 #include <sys/syslog.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in.h>
73 #include <net/if_slvar.h>
80 #define DEFAULT_BAUD B9600
81 int speed
= DEFAULT_BAUD
;
82 #define FC_NONE 0 /* flow control: none */
83 #define FC_SW 1 /* flow control: software (XON/XOFF) */
84 #define FC_HW 2 /* flow control: hardware (RTS/CTS) */
85 int flowcontrol
= FC_NONE
;
89 int wait_time
= 60; /* then back off */
90 #define MAXTRIES 6 /* w/60 sec and doubling, takes an hour */
91 #define PIDFILE "/var/run/startslip.pid"
97 #define syslog fprintf
98 #define LOG_ERR stderr
99 #define LOG_INFO stderr
103 #define printd if (debug) printf
115 FILE *wfd
= NULL
, *pfd
;
116 char *dialerstring
= 0, buf
[BUFSIZ
];
117 int first
= 1, tries
= 0;
126 while ((ch
= getopt(argc
, argv
, "db:s:p:A:F:")) != EOF
)
133 speed
= atoi(optarg
);
137 pausefirst
= atoi(optarg
);
140 dialerstring
= optarg
;
147 if (strcmp(optarg
, "none") == 0)
148 flowcontrol
= FC_NONE
;
149 else if (strcmp(optarg
, "sw") == 0)
151 else if (strcmp(optarg
, "hw") == 0)
154 (void)fprintf(stderr
,
155 "flow control: none, sw, hw\n");
160 (void)fprintf(stderr
, "flow control not supported\n");
173 openlog("startslip", LOG_PID
, LOG_DAEMON
);
176 if (debug
== 0 && (fd
= open("/dev/tty", 0)) >= 0) {
177 ioctl(fd
, TIOCNOTTY
, 0);
184 setbuf(stdout
, NULL
);
186 if (pfd
= fopen(PIDFILE
, "r")) {
188 fscanf(pfd
, "%d", &pid
);
195 if (++tries
> MAXTRIES
) {
196 syslog(LOG_ERR
, "exiting after %d tries\n", tries
);
204 * We may get a HUP below, when the parent (session leader/
205 * controlling process) exits; ignore HUP until into new session.
207 signal(SIGHUP
, SIG_IGN
);
213 printd("parent exit\n");
222 printd("restart: pid %d: ", pid
);
223 if (pfd
= fopen(PIDFILE
, "w")) {
224 fprintf(pfd
, "%d\n", pid
);
238 if ((fd
= open(argv
[0], O_RDWR
)) < 0) {
240 syslog(LOG_ERR
, "open %s: %m\n", argv
[0]);
244 sleep(wait_time
* tries
);
250 if (ioctl(fd
, TIOCSCTTY
, 0) < 0)
251 perror("ioctl (TIOCSCTTY)");
253 signal(SIGHUP
, sighup
);
255 if (ioctl(fd
, TIOCGETD
, &disc
) < 0)
256 perror("ioctl(TIOCSETD)");
257 printf(" (disc was %d)", disc
);
260 if (ioctl(fd
, TIOCSETD
, &disc
) < 0) {
261 perror("ioctl(TIOCSETD)");
262 syslog(LOG_ERR
, "%s: ioctl (TIOCSETD 0): %m\n",
267 if (tcgetattr(fd
, &t
) < 0) {
269 syslog(LOG_ERR
, "%s: tcgetattr: %m\n", argv
[0]);
273 t
.c_iflag
&= ~IMAXBEL
;
274 switch (flowcontrol
) {
276 t
.c_cflag
|= (CRTS_IFLOW
|CCTS_OFLOW
);
279 t
.c_iflag
|= (IXON
|IXOFF
);
282 t
.c_cflag
&= ~(CRTS_IFLOW
|CCTS_OFLOW
);
283 t
.c_iflag
&= ~(IXON
|IXOFF
);
286 cfsetspeed(&t
, speed
);
287 if (tcsetattr(fd
, TCSAFLUSH
, &t
) < 0) {
289 syslog(LOG_ERR
, "%s: tcsetattr: %m\n", argv
[0]);
293 sleep(wait_time
* tries
);
298 if (ioctl(fd
, TIOCGETP
, &sgtty
) < 0) {
299 perror("ioctl (TIOCGETP)");
300 syslog(LOG_ERR
, "%s: ioctl (TIOCGETP): %m\n",
304 sgtty
.sg_flags
= RAW
| ANYP
;
305 sgtty
.sg_erase
= sgtty
.sg_kill
= 0377;
306 sgtty
.sg_ispeed
= sgtty
.sg_ospeed
= speed
;
307 if (ioctl(fd
, TIOCSETP
, &sgtty
) < 0) {
308 perror("ioctl (TIOCSETP)");
309 syslog(LOG_ERR
, "%s: ioctl (TIOCSETP): %m\n",
314 sleep(wait_time
* tries
);
319 sleep(2); /* wait for flakey line to settle */
323 wfd
= fdopen(fd
, "w+");
325 syslog(LOG_ERR
, "can't fdopen slip line\n");
328 setbuf(wfd
, (char *)0);
330 printd(", send dialstring");
331 fprintf(wfd
, "%s\r", dialerstring
);
339 printd("look for login: ");
341 if (getline(buf
, BUFSIZ
, fd
) == 0 || hup
) {
342 sleep(wait_time
* tries
);
346 if (bcmp(buf
, annex
, strlen(annex
)) == 0) {
347 fprintf(wfd
, "slip\r");
348 printd("Sent \"slip\"\n");
351 if (bcmp(&buf
[1], "sername:", 8) == 0) {
352 fprintf(wfd
, "%s\r", argv
[1]);
353 printd("Sent login: %s\n", argv
[1]);
356 if (bcmp(&buf
[1], "assword:", 8) == 0) {
357 fprintf(wfd
, "%s\r", argv
[2]);
358 printd("Sent password: %s\n", argv
[2]);
362 if (bcmp(&buf
[1], "ogin:", 5) == 0) {
363 fprintf(wfd
, "%s\r", argv
[1]);
364 printd("Sent login: %s\n", argv
[1]);
367 if (bcmp(&buf
[1], "assword:", 8) == 0) {
368 fprintf(wfd
, "%s\r", argv
[2]);
369 printd("Sent password: %s\n", argv
[2]);
376 * Security hack. Do not want private information such as the
377 * password and possible phone number to be left around.
378 * So we clobber the arguments.
380 for (ap
= argv
- optind
+ 1; ap
< argv
+ 3; ap
++)
381 for (cp
= *ap
; *cp
!= 0; cp
++)
389 if (ioctl(fd
, TIOCSETD
, &disc
) < 0) {
390 perror("ioctl(TIOCSETD)");
391 syslog(LOG_ERR
, "%s: ioctl (TIOCSETD SLIP): %m\n",
395 if (first
&& debug
== 0) {
399 (void) open("/dev/null", O_RDWR
);
403 (void) system("ifconfig sl0 up");
406 syslog(LOG_INFO
, "reconnected (%d tries).\n", tries
);
412 printd("sigpause return\n");
422 if (hup
== 0 && logged_in
)
423 syslog(LOG_INFO
, "hangup signal\n");
427 getline(buf
, size
, fd
)
435 for (i
= 0; i
< size
; i
++) {
438 if ((ret
= read(fd
, &buf
[i
], 1)) == 1) {
440 if (buf
[i
] == '\r' || buf
[i
] == '\0')
442 if (buf
[i
] != '\n' && buf
[i
] != ':')
445 printd("Got %d: \"%s\"\n", i
+ 1, buf
);
450 perror("getline: read");
452 fprintf(stderr
, "read returned 0\n");
454 printd("returning 0 after %d: \"%s\"\n", i
, buf
);
463 (void)fprintf(stderr
,
464 "usage: startslip [-d] [-b speed] [-s string] [-A annexname] [-F flowcontrol] dev user passwd\n");