source: rtems/c/src/libnetworking/pppd/chat.c @ cedf9296

4.104.114.84.95
Last change on this file since cedf9296 was aee474b, checked in by Joel Sherrill <joel.sherrill@…>, on 10/12/01 at 13:43:05

2001-10-12 Mike Siers <mikes@…>

  • Update to stable working state. Congratulations Mike! :)
  • modem_example: Directory removed.
  • modem_example/16550.h, modem_example/README, modem_example/modem.c, modem_example/modem.h, modem_example/ppp.c, modem_example/ppp.h, modem_example/pppcompress.c: Files removed.
  • pppd/example/pppd.options: New file.
  • pppd/README, pppd/STATUS, pppd/cbcp.c, pppd/cbcp.h, pppd/chat.c, pppd/pppd.h, pppd/rtemsmain.c: Updated.
  • Property mode set to 100644
File size: 15.3 KB
Line 
1/*
2 *      Chat -- a program for automatic session establishment (i.e. dial
3 *              the phone and log in).
4 *
5 * Standard termination codes:
6 *  0 - successful completion of the script
7 *  1 - invalid argument, expect string too large, etc.
8 *  2 - error on an I/O operation or fatal error condition.
9 *  3 - timeout waiting for a simple string.
10 *  4 - the first string declared as "ABORT"
11 *  5 - the second string declared as "ABORT"
12 *  6 - ... and so on for successive ABORT strings.
13 *
14 *      This software is in the public domain.
15 *
16 * -----------------
17 *      22-May-99 added environment substitutuion, enabled with -E switch.
18 *      Andreas Arens <andras@cityweb.de>.
19 *
20 *      12-May-99 added a feature to read data to be sent from a file,
21 *      if the send string starts with @.  Idea from gpk <gpk@onramp.net>.
22 *
23 *      added -T and -U option and \T and \U substitution to pass a phone
24 *      number into chat script. Two are needed for some ISDN TA applications.
25 *      Keith Dart <kdart@cisco.com>
26 *     
27 *
28 *      Added SAY keyword to send output to stderr.
29 *      This allows to turn ECHO OFF and to output specific, user selected,
30 *      text to give progress messages. This best works when stderr
31 *      exists (i.e.: pppd in nodetach mode).
32 *
33 *      Added HANGUP directives to allow for us to be called
34 *      back. When HANGUP is set to NO, chat will not hangup at HUP signal.
35 *      We rely on timeouts in that case.
36 *
37 *      Added CLR_ABORT to clear previously set ABORT string. This has been
38 *      dictated by the HANGUP above as "NO CARRIER" (for example) must be
39 *      an ABORT condition until we know the other host is going to close
40 *      the connection for call back. As soon as we have completed the
41 *      first stage of the call back sequence, "NO CARRIER" is a valid, non
42 *      fatal string. As soon as we got called back (probably get "CONNECT"),
43 *      we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
44 *      Note that CLR_ABORT packs the abort_strings[] array so that we do not
45 *      have unused entries not being reclaimed.
46 *
47 *      In the same vein as above, added CLR_REPORT keyword.
48 *
49 *      Allow for comments. Line starting with '#' are comments and are
50 *      ignored. If a '#' is to be expected as the first character, the
51 *      expect string must be quoted.
52 *
53 *
54 *              Francis Demierre <Francis@SwissMail.Com>
55 *              Thu May 15 17:15:40 MET DST 1997
56 *
57 *
58 *      Added -r "report file" switch & REPORT keyword.
59 *              Robert Geer <bgeer@xmission.com>
60 *
61 *      Added -s "use stderr" and -S "don't use syslog" switches.
62 *              June 18, 1997
63 *              Karl O. Pinc <kop@meme.com>
64 *
65 *
66 *      Added -e "echo" switch & ECHO keyword
67 *              Dick Streefland <dicks@tasking.nl>
68 *
69 *
70 *      Considerable updates and modifications by
71 *              Al Longyear <longyear@pobox.com>
72 *              Paul Mackerras <paulus@cs.anu.edu.au>
73 *
74 *
75 *      The original author is:
76 *
77 *              Karl Fox <karl@MorningStar.Com>
78 *              Morning Star Technologies, Inc.
79 *              1760 Zollinger Road
80 *              Columbus, OH  43221
81 *              (614)451-1883
82 *
83 */
84
85#include <stdio.h>
86#include <ctype.h>
87#include <time.h>
88#include <fcntl.h>
89#include <errno.h>
90#include <string.h>
91#include <stdlib.h>
92#include <unistd.h>
93#include <sys/types.h>
94#include <sys/stat.h>
95#include <syslog.h>
96#include <termios.h>
97#include "pppd.h"
98
99#undef  TERMIOS
100#define TERMIOS
101
102
103#define STR_LEN 1024
104char temp2[STR_LEN];
105
106#ifndef SIGTYPE
107#define SIGTYPE void
108#endif
109
110#undef __P
111#undef __V
112
113#ifdef __STDC__
114#include <stdarg.h>
115#define __V(x)  x
116#define __P(x)  x
117#else
118#include <varargs.h>
119#define __V(x)  (va_alist) va_dcl
120#define __P(x)  ()
121#define const
122#endif
123
124#ifndef O_NONBLOCK
125#define O_NONBLOCK      O_NDELAY
126#endif
127
128
129/*************** Micro getopt() *********************************************/
130#define OPTION(c,v)     (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
131                                (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
132                                &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
133#define OPTARG(c,v)     (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
134                                (_O=4,(char*)0):(char*)0)
135#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
136#define ARG(c,v)        (c?(--c,*v++):(char*)0)
137
138#if 0
139static int _O = 0;              /* Internal state */
140#endif
141/*************** Micro getopt() *********************************************/
142
143char *program_name;
144
145#define MAX_ABORTS              16
146#define MAX_REPORTS             16
147#define DEFAULT_CHAT_TIMEOUT    45
148#define MAX_TIMEOUTS            10
149
150int echo             = 0;
151int quiet            = 0;
152int report           = 0;
153int use_env          = 0;
154int exit_code        = 0;
155char *report_file    = (char *) 0;
156char *chat_file      = (char *) 0;
157char *phone_num      = (char *) 0;
158char *phone_num2     = (char *) 0;
159static int timeout   = DEFAULT_CHAT_TIMEOUT;
160
161#ifdef TERMIOS
162#define term_parms struct termios
163#define get_term_param(param) tcgetattr(0, param)
164#define set_term_param(param) tcsetattr(0, TCSANOW, param)
165struct termios saved_tty_parameters;
166#endif
167
168char *fail_reason = (char *)0;
169char  fail_buffer[50];
170char *abort_string[MAX_ABORTS]={"BUSY","NO DIALTONE","NO CARRIER","NO ANSWER","RING\r\nRING"};
171int n_aborts = 5;
172int abort_next = 0, timeout_next = 0, echo_next = 0;
173int clear_abort_next = 0;
174
175char *report_string[MAX_REPORTS] ;
176char  report_buffer[50] ;
177int n_reports = 0, report_next = 0, report_gathering = 0 ;
178int clear_report_next = 0;
179
180int say_next = 0, hup_next = 0;
181
182void *dup_mem __P((void *b, size_t c));
183void *copy_of __P((char *s));
184void break_sequence __P((void));
185int  get_string __P((register char *string));
186int  put_string __P((register char *s));
187int  write_char __P((int c));
188int  put_char __P((int c));
189int  get_char __P((void));
190void chat_send __P((register char *s));
191char *character __P((int c));
192void chat_expect __P((register char *s));
193char *clean __P((register char *s, int sending));
194char *expect_strtok __P((char *, char *));
195int chatmain __P((char *));
196
197
198void *dup_mem(b, c)
199void *b;
200size_t c;
201{
202    void *ans = malloc (c);
203    if (!ans)
204        return NULL;
205
206    memcpy(ans, b, c);
207    return ans;
208}
209
210void *copy_of (s)
211char *s;
212{
213    return dup_mem(s, strlen (s) + 1);
214}
215
216char *getnextcommand(char **string)
217{
218        char *buf=*string,*res;
219        res=strchr(buf,'|');
220        if (res==NULL)
221                return NULL;
222        *res='\0';
223        *string=res+1;
224        return buf;
225}
226
227int chatmain(argv)
228char *argv;
229{
230  char    *arg;
231
232  /* initialize exit code */
233  exit_code = 0;
234
235  if ( debug ) {
236    dbglog("chat_main: %s\n", argv);
237  }
238
239  /* get first expect string */
240  arg = getnextcommand(&argv);
241  while (( arg != NULL ) && ( exit_code == 0 )) {
242    /* process the expect string */
243    chat_expect(arg);
244    if ( exit_code == 0 ) {
245      /* get the next send string */
246      arg = getnextcommand(&argv);
247      if ( arg != NULL ) {
248        /* process the send string */
249        chat_send(arg);
250
251        /* get the next expect string */
252        arg = getnextcommand(&argv);
253      }
254    }
255  }
256
257  if ( exit_code ) {
258   exit_code = -exit_code;
259  }
260
261  return ( exit_code );
262}
263
264void break_sequence()
265{
266  tcsendbreak(ttyfd, 0);
267}
268
269/*
270 *      'Clean up' this string.
271 */
272char *clean(s, sending)
273register char *s;
274int sending;  /* set to 1 when sending (putting) this string. */
275{
276    char temp[STR_LEN], env_str[STR_LEN], cur_chr;
277    register char *s1, *phchar;
278    int add_return = sending;
279#define isoctal(chr)    (((chr) >= '0') && ((chr) <= '7'))
280#define isalnumx(chr)   ((((chr) >= '0') && ((chr) <= '9')) \
281                         || (((chr) >= 'a') && ((chr) <= 'z')) \
282                         || (((chr) >= 'A') && ((chr) <= 'Z')) \
283                         || (chr) == '_')
284
285    s1 = temp;
286    while (*s) {
287        cur_chr = *s++;
288        if (cur_chr == '^') {
289            cur_chr = *s++;
290            if (cur_chr == '\0') {
291                *s1++ = '^';
292                break;
293            }
294            cur_chr &= 0x1F;
295            if (cur_chr != 0) {
296                *s1++ = cur_chr;
297            }
298            continue;
299        }
300       
301        if (use_env && cur_chr == '$') {                /* ARI */
302            phchar = env_str;
303            while (isalnumx(*s))
304                *phchar++ = *s++;
305            *phchar = '\0';
306            phchar = getenv(env_str);
307            if (phchar)
308                while (*phchar)
309                    *s1++ = *phchar++;
310            continue;
311        }
312
313        if (cur_chr != '\\') {
314            *s1++ = cur_chr;
315            continue;
316        }
317
318        cur_chr = *s++;
319        if (cur_chr == '\0') {
320            if (sending) {
321                *s1++ = '\\';
322                *s1++ = '\\';
323            }
324            break;
325        }
326
327        switch (cur_chr) {
328        case 'b':
329            *s1++ = '\b';
330            break;
331
332        case 'c':
333            if (sending && *s == '\0')
334                add_return = 0;
335            else
336                *s1++ = cur_chr;
337            break;
338
339        case '\\':
340        case 'K':
341        case 'p':
342        case 'd':
343            if (sending)
344                *s1++ = '\\';
345            *s1++ = cur_chr;
346            break;
347
348        case 'T':
349            if (sending && phone_num) {
350                for (phchar = phone_num; *phchar != '\0'; phchar++)
351                    *s1++ = *phchar;
352            }
353            else {
354                *s1++ = '\\';
355                *s1++ = 'T';
356            }
357            break;
358
359        case 'U':
360            if (sending && phone_num2) {
361                for (phchar = phone_num2; *phchar != '\0'; phchar++)
362                    *s1++ = *phchar;
363            }
364            else {
365                *s1++ = '\\';
366                *s1++ = 'U';
367            }
368            break;
369
370        case 'q':
371            quiet = 1;
372            break;
373
374        case 'r':
375            *s1++ = '\r';
376            break;
377
378        case 'n':
379            *s1++ = '\n';
380            break;
381
382        case 's':
383            *s1++ = ' ';
384            break;
385
386        case 't':
387            *s1++ = '\t';
388            break;
389
390        case 'N':
391            if (sending) {
392                *s1++ = '\\';
393                *s1++ = '\0';
394            }
395            else
396                *s1++ = 'N';
397            break;
398           
399        case '$':                       /* ARI */
400            if (use_env) {
401                *s1++ = cur_chr;
402                break;
403            }
404            /* FALL THROUGH */
405
406        default:
407            if (isoctal (cur_chr)) {
408                cur_chr &= 0x07;
409                if (isoctal (*s)) {
410                    cur_chr <<= 3;
411                    cur_chr |= *s++ - '0';
412                    if (isoctal (*s)) {
413                        cur_chr <<= 3;
414                        cur_chr |= *s++ - '0';
415                    }
416                }
417
418                if (cur_chr != 0 || sending) {
419                    if (sending && (cur_chr == '\\' || cur_chr == 0))
420                        *s1++ = '\\';
421                    *s1++ = cur_chr;
422                }
423                break;
424            }
425
426            if (sending)
427                *s1++ = '\\';
428            *s1++ = cur_chr;
429            break;
430        }
431    }
432
433    if (add_return)
434        *s1++ = '\r';
435
436    *s1++ = '\0'; /* guarantee closure */
437    *s1++ = '\0'; /* terminate the string */
438    return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
439}
440
441/*
442 * A modified version of 'strtok'. This version skips \ sequences.
443 */
444char *expect_strtok (s, term)
445     char *s, *term;
446{
447    static  char *str   = "";
448    int     escape_flag = 0;
449    char   *result;
450
451/*
452 * If a string was specified then do initial processing.
453 */
454    if (s)
455        str = s;
456
457/*
458 * If this is the escape flag then reset it and ignore the character.
459 */
460    if (*str)
461        result = str;
462    else
463        result = (char *) 0;
464
465    while (*str) {
466        if (escape_flag) {
467            escape_flag = 0;
468            ++str;
469            continue;
470        }
471
472        if (*str == '\\') {
473            ++str;
474            escape_flag = 1;
475            continue;
476        }
477
478/*
479 * If this is not in the termination string, continue.
480 */
481        if (strchr (term, *str) == (char *) 0) {
482            ++str;
483            continue;
484        }
485
486/*
487 * This is the terminator. Mark the end of the string and stop.
488 */
489        *str++ = '\0';
490        break;
491    }
492    return (result);
493}
494
495/*
496 * Process the expect string
497 */
498void chat_expect (s)
499char *s;
500{
501    char *expect;
502    char *reply;
503
504    if (strcmp(s, "HANGUP") == 0) {
505        ++hup_next;
506        return;
507    }
508 
509    if (strcmp(s, "ABORT") == 0) {
510        ++abort_next;
511        return;
512    }
513
514    if (strcmp(s, "CLR_ABORT") == 0) {
515        ++clear_abort_next;
516        return;
517    }
518
519    if (strcmp(s, "REPORT") == 0) {
520        ++report_next;
521        return;
522    }
523
524    if (strcmp(s, "CLR_REPORT") == 0) {
525        ++clear_report_next;
526        return;
527    }
528
529    if (strcmp(s, "TIMEOUT") == 0) {
530        ++timeout_next;
531        return;
532    }
533
534    if (strcmp(s, "ECHO") == 0) {
535        ++echo_next;
536        return;
537    }
538
539    if (strcmp(s, "SAY") == 0) {
540        ++say_next;
541        return;
542    }
543
544/*
545 * Fetch the expect and reply string.
546 */
547    for (;;) {
548        expect = expect_strtok (s, "-");
549        s      = (char *) 0;
550
551        if (expect == (char *) 0)
552            return;
553
554        reply = expect_strtok (s, "-");
555
556/*
557 * Handle the expect string. If successful then exit.
558 */
559        if (get_string (expect))
560            return;
561
562/*
563 * If there is a sub-reply string then send it. Otherwise any condition
564 * is terminal.
565 */
566        if (reply == (char *) 0 || exit_code != 3)
567            break;
568
569        chat_send (reply);
570    }
571}
572
573/*
574 * Translate the input character to the appropriate string for printing
575 * the data.
576 */
577
578char *character(c)
579int c;
580{
581    static char string[10];
582    char *meta;
583
584    meta = (c & 0x80) ? "M-" : "";
585    c &= 0x7F;
586
587    if (c < 32)
588        sprintf(string, "%s^%c", meta, (int)c + '@');
589    else if (c == 127)
590        sprintf(string, "%s^?", meta);
591    else
592        sprintf(string, "%s%c", meta, c);
593
594    return (string);
595}
596
597/*
598 *  process the reply string
599 */
600void chat_send (s)
601register char *s;
602{
603    char file_data[STR_LEN];
604
605    if (say_next) {
606        say_next = 0;
607        s = clean(s, 1);
608        write(2, s, strlen(s));
609        free(s);
610        return;
611    }
612
613    if (hup_next) {
614        hup_next = 0;
615        return;
616    }
617
618    if (echo_next) {
619        echo_next = 0;
620        echo = (strcmp(s, "ON") == 0);
621        return;
622    }
623
624    if (abort_next) {
625        char *s1;
626       
627        abort_next = 0;
628        if ( n_aborts < MAX_ABORTS ) {
629          s1 = clean(s, 0);
630          if (( strlen(s1) <= strlen(s) ) &&
631              ( strlen(s1) <  sizeof(fail_buffer))) {
632
633            abort_string[n_aborts++] = s1;
634          }
635        }
636        return;
637    }
638
639    if (clear_abort_next) {
640        clear_abort_next = 0;
641        return;
642    }
643
644    if (report_next) {
645        report_next = 0;
646        return;
647    }
648
649    if (clear_report_next) {
650        clear_report_next = 0;
651        return;
652    }
653
654    if (timeout_next) {
655        timeout_next = 0;
656        timeout = atoi(s);
657       
658        if (timeout <= 0)
659            timeout = DEFAULT_CHAT_TIMEOUT;
660
661        return;
662    }
663
664    if (strcmp(s, "EOT") == 0)
665        s = "^D\\c";
666    else if (strcmp(s, "BREAK") == 0)
667        s = "\\K\\c";
668
669    if (!put_string(s)) {
670      exit_code = 2;
671    }
672}
673
674int get_char()
675{
676    int status;
677    char c;
678    int tries=MAX_TIMEOUTS;
679
680        while(tries)
681        {
682            status = read(ttyfd, &c, 1);
683            switch (status) {
684                   case 1:
685                                return ((int)c & 0x7F);
686                    default:
687                                tries--;                       
688            }
689    }
690        return -1;                                             
691}
692
693int put_char(c)
694int c;
695{
696  char ch = c;
697
698  write(ttyfd, &ch, 1);
699
700  return 0;
701}
702
703int write_char (c)
704int c;
705{
706    if (put_char(c) < 0) {
707        return (0);
708    }
709    return (1);
710}
711
712int put_string (s)
713register char *s;
714{
715    quiet = 0;
716    s = clean(s, 1);
717    while (*s) {
718        register char c = *s++;
719
720        if (c != '\\') {
721            if (!write_char (c))
722                return 0;
723            continue;
724        }
725
726        c = *s++;
727        switch (c) {
728        case 'd':
729            sleep(1);
730            break;
731
732        case 'K':
733            break_sequence();
734            break;
735
736        case 'p':
737#if 0 /* FIXME!!! */
738            usleep(10000);      /* 1/100th of a second (arg is microseconds) */
739#else
740            sleep(1);
741#endif
742            break;
743
744        default:
745            if (!write_char (c))
746                return 0;
747            break;
748        }
749    }
750
751    return (1);
752}
753
754/*
755 *      'Wait for' this string to appear on this file descriptor.
756 */
757int get_string(string)
758register char *string;
759{
760    int c, len, minlen;
761    register char *s = temp2, *end = s + STR_LEN;
762    char *logged = temp2;
763    struct termios tios;
764
765    memset(temp2, 0, sizeof(temp2));
766
767    tcgetattr(ttyfd, &tios);
768    tios.c_cc[VMIN] = 0;
769    tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS;
770    tcsetattr(ttyfd, TCSANOW, &tios);
771               
772    string = clean(string, 0);
773    len = strlen(string);
774    minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
775       
776    if (len > STR_LEN) {
777        exit_code = 1;
778        return 0;
779    }
780
781    if (len == 0) {
782                        return (1);
783    }
784
785   while ( (c = get_char()) >= 0) {
786                int n, abort_len;
787
788        *s++ = c;
789        *s=0;
790
791        if (s - temp2 >= len &&
792            c == string[len - 1] &&
793            strncmp(s - len, string, len) == 0) {
794            return (1);
795        }
796
797        for (n = 0; n < n_aborts; ++n) {
798            if (s - temp2 >= (abort_len = strlen(abort_string[n])) &&
799                strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
800
801                exit_code = n + 4;
802                strcpy(fail_reason = fail_buffer, abort_string[n]);
803                return (0);
804            }
805        }
806
807        if (s >= end) {
808            if (logged < s - minlen) {
809                logged = s;
810            }
811            s -= minlen;
812            memmove(temp2, s, minlen);
813            logged = temp2 + (logged - s);
814            s = temp2 + minlen;
815        }
816    }
817
818    exit_code = 3;
819    return (0);
820}
Note: See TracBrowser for help on using the repository browser.