⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diald.c

📁 PPP协议C语言源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
            i2 = getDialInt(&netp->hw.sp);

           /* Test:  (Value1 Condition Value2) */
            switch (c1) {
                default:
                    goto term;
                case 1:
                    if (i1 < i2)
                        goto good_goto;
                    goto bad_goto;
                case 2:
                    if (i1 <= i2)
                        goto good_goto;
                    goto bad_goto;
                case 3:
                case 4:
                    if (i1 == i2)
                        goto good_goto;
                    goto bad_goto;
                case 5:
                    if (i1 > i2)
                        goto good_goto;
                    goto bad_goto;
                case 6:
                    if (i1 >= i2)
                        goto good_goto;
                    goto bad_goto;
            }
        case ussDialXcmdLogE:   /* type int-index */
            i1 = *netp->hw.sp++;
            _sv[netno].logp = ussDialLogTable[i1];
            _sv[netno].logid = i1;
            break;
        case ussDialXcmdNologE: /* type */
            _sv[netno].logp = 0;
            _sv[netno].logid = -1;
            break;
        case ussDialXcmdUpE:    /* type */
        case USS_DIAL_SFLAG:    /* unofficial-type */
#if NTRACE >= 3
            Nprintf("DIALD: Ending script (good) %s\n", _sv[netp->netno].name);
#endif
            netp->hw.st = ussDialStateDoneE;
            dialDown(netno);
            return 1;           /* Finished with success */
        default:                /* Unknown command, see if next byte is OK */
            break;
        }
        break;
    case ussDialStateSendE:     /* Timeout on Send (shouldn't happen) */
        netp->hw.opt1 = 0;
        netp->hw.st = ussDialStateNextE;
        break;
    case ussDialStatePausE:     /* Timeout on pause (not a bad thing) */
        netp->hw.st = ussDialStateNextE;
        break;
    case ussDialStateDoneE:     /* Already finished (shouldn't happen) */
        return 1;
    case ussDialStateRecvE:     /* Timeout on Expect */
    case ussDialStateRecvE|ussDialStateBChckE:  /* Check for data received */
        cp = (char *)_sv[netno].mp+MESSH_SZ+LHDRSZ; /* Expected data */
        cp2 = netp->hw.buflim;                      /* Received data */

#if NTRACE >= 9
        {
            char *a;
            Nprintf("DIALD: Compare '");
            for (a=cp; *a; a++)
                if (*a == '\r')
                    Nputchr('r');
                else if (*a == '\n')
                    Nputchr('n');
                else
                    Nputchr(*a);
            Nprintf("' with '");
            for (a=cp2; *a; a++)
                if (*a == '\r')
                    Nputchr('r');
                else if (*a == '\n')
                    Nputchr('n');
                else
                    Nputchr(*a);
            Nprintf("'\n");
        }
#endif

       /* If the data is found */
        if (isSubString(cp2, cp)) {
            netp->hw.st = ussDialStateNextE;
           /*
            ** In many cases, a send must be performed immediately after
            ** an expect.  Many modems are single duplexed when off-line so
            ** this fails as the modem transmits a few more characters than
            ** the ones the user script was expecting.  The following code
            ** adds a delay based on the baud rate and the assumption that
            ** three characters (that is, "\r\n\r") may be transmitted.
            */
            if (*netp->hw.sp != ussDialXcmdExpectE) {
                netp->hw.st |= ussDialStateBWaitE;
                netp->hw.idle_time = 24000 / (unsigned int)netp->bps + 1;
                netp->hw.idle_time += TimeMS() + 1000 / clocks_per_sec;
            }
        }
       /* Data not found and time expired.  Turn status bad and proceed. */
        else if (netp->hw.st == ussDialStateRecvE) {
            netp->hw.opt1 = 0;
            netp->hw.st = ussDialStateNextE;
        }
       /* Data not found.  Keep waiting until timeout */
        else
            netp->hw.st = ussDialStateRecvE|ussDialStateBWaitE;
        break;
    }
    if (netp->hw.st == ussDialStateNextE)
        goto next_command;                          /* Do another command */

    return 0;                   /* Still working */
}


/*
** * * * * * *
** ussScriptInit()  Set a script up to execute
**
** int ussScriptTimeout(int netno);
**
** PARAMETERS:
**  (in) netno                  a yfnet network number
**  (in) *fs                    a pointer to a formatted script
**
** DESCRIPTION:
**  This function will set up the script passed to it as the one
**  to execute.
*/
void ussScriptInit(int netno, const unsigned char *fs)
{
    struct NET *netp;
    int i1;

    netp = &nets[netno];

   /* If the current script is not fs */
    if (netp->hw.script != fs) {
        if (netp->hw.st > ussDialStateNoneE && netp->hw.st != ussDialStateDoneE)
            dialDown(netno);
        netp->hw.st = ussDialStateNoneE;    /* Stop current script */
    }

   /* If current script is stopped... */
    if (netp->hw.st == ussDialStateNoneE) {
       /* Get a buffer for send/expect */
        WAITFOR((_sv[netno].mp = Ngetbuf()) != 0, SIG_WN(netno), netp->tout, i1);
        if (!i1) {
            netp->hw.script = (unsigned char *)fs;  /* Set script */
            netp->hw.sp = netp->hw.script;          /* Set pointer */
            netp->hw.st = ussDialStateNextE;        /* Start script */
            dialUp(netno);
#if NTRACE >= 5
            Nprintf("DIALD: Starting new dial script\n");
#endif
        }
#if NTRACE
        else {
            Nprintf("DIALD: Could not get a buffer for dialer!\n");
            Nprintf("DIALD: Terminating script\n");
        }
#endif
    }
}




/*
** * * * * * *
** format()    Put send/expect data in a buffer
**
** static void format(struct NET *netp, MESS *mess);
**
** PARAMETERS:
**  (in/out) struct NET *netp   link-layer structure pointer
**  (in/out) MESS *mess         Message buffer for data received
**
** DESCRIPTION:
**  Formats send/expect data field into a buffer.
*/
static void format(struct NET *netp, MESS *mess)
{
    char *cp;

   /* Assign buflim, bufout and bufin to buffer for send/expect data */
    netp->hw.bufout = netp->hw.bufin = (char *)mess + MESSH_SZ + LHDRSZ;

   /* Put data and variables in buffer */
    while (*netp->hw.sp) {
       /* If the data is a variable */
        if (*netp->hw.sp == USS_DIAL_RCMD_VAR) {
            cp = (char *)*ussDialVarTable[*++netp->hw.sp];
            strcpy(netp->hw.bufin, cp);
            netp->hw.bufin += strlen(cp);
            netp->hw.sp++;
        }
       /* If the data is an integer */
        else if (*netp->hw.sp == USS_DIAL_RCMD_INT) {
            Nsscanf(netp->hw.bufin, "%u", ussDialIntTable[*++netp->hw.sp]);
            netp->hw.bufin += strlen(netp->hw.bufin);
            netp->hw.sp++;
        }
       /* If the data is just data */
        else
            *netp->hw.bufin++ = *netp->hw.sp++;
    }

    *netp->hw.bufin++ = 0;              /* Null terminate data */
    netp->hw.buflim = netp->hw.bufin;   /* Save location of free space */
}


/*
** * * * * * *
** getDialInt()     Get an integer for a dial command
**
** static int getDialInt(unsigned char **sp)
**
** PARAMETERS:
**  (in) char **sp              A value/result pointer to a char array
**
** RETURNS:
**  #                           The number given by the array
**
** DESCRIPTION:
**  This function will examine the contents of the array at the pointer
**  value to see if it contains a variable integer.  If so, the value
**  is extracted from the integer table and the value is returned.  If
**  the value is direct, the value in the array is returned.
**  In either case, the pointer to the array is incremented.
*/
static int getDialInt(unsigned char **sp)
{
    int i1;
    unsigned char *cp = *sp;

    if (*cp == USS_DIAL_RCMD_INT)
        i1 = ussDialIntTable[*++cp];
    else {
        i1 = (*cp++ << 8) & 0xff00; /* High byte of integer */
        i1 += *cp & 0xff;           /* Low byte of integer */
    }
    *sp = ++cp;
    return i1;
}


/*
** * * * * * *
** expect()  Start expecting data received
**
** static void expect(struct NET *netp, MESS *mess);
**
** PARAMETERS:
**  (in/out) struct NET *netp   link-layer structure pointer
**  (in/out) MESS *mess         Message buffer for data received
**
** DESCRIPTION:
**  Sets up the software to expect data received.
*/
static void expect(struct NET *netp, MESS *mess)
{
    unsigned long ul1;
    char *cp;

    mess->netno = netp->netno;

   /* Parse the expect time */
    ul1 = getDialInt(&netp->hw.sp);

   /* Parse the expect data */
    format(netp, mess);

   /* Set the time to wait (seconds -> milliseconds) */
    netp->hw.idle_time = 1000 * ul1 + TimeMS();

   /* Set the wait state */
    netp->hw.st = ussDialStateRecvE|ussDialStateBWaitE;
}


/*
** * * * * * *
** isIgnore()  Test a character to see if it is in the ignore array
**
** static int isIgnore(char c);
**
** PARAMETERS:
**  (in) char c                 A character to compare
**
** RETURNS:
**  0                           Not an ignore character
**  1                           Is an ignore character
*/
static const char ignore[] = {' ','\r','\n'};   /* Characters to ignore */
static int isIgnore(char c)
{
    int i=0;
    while (i < sizeof(ignore))
        if (ignore[i++] == c)
            return 1;
    return 0;
}


/*
** * * * * * *
** isSubString()  See if the second string is in the first
**
** static int isSubString(char *a, char *b);
**
** PARAMETERS:
**  (in) char *a                The first string
**  (in) char *b                The second string
**
** RETURNS:
**  0                           String b is in string a
**  #                           String b is not in string a
**
** DESCRIPTION:
**  This function will test if string b is a sub-string of string a.  It
**  will ignore the characters in the ignore array above.
**
**      "xxx"      is a sub-string of       "abcxxxdef"
**      "abcdef"   is not a sub-string of   "abcxxxdef"
**      ""         is a sub-string of       <anything>
**      <anything> is not a sub-string of   ""
*/
static int isSubString(char *a, char *b)
{
    char c1, *a2, *b2;

    c1 = 0;
    do {
        while (*b && isIgnore(*b))
            b++;
        while (*a && *a != *b)
            a++;
        for (a2 = a, b2 = b; *a2 && *b2 && !(c1 = (*a2 ^ *b2));) {
            a2++, b2++;
            while (*a && isIgnore(*a))
                a++;
            while (*b && isIgnore(*b))
                b++;
        }
        if (!c1 && !*b2)
            return 1;
    } while (*a++);

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -