1998-02-16 00:01:12 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-13 00:44:51 +00:00
|
|
|
* $Id: datalink.c,v 1.1.2.19 1998/03/13 00:44:33 brian Exp $
|
1998-02-16 00:01:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <alias.h>
|
1998-02-17 19:28:35 +00:00
|
|
|
#include <stdio.h>
|
1998-02-16 00:01:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "loadalias.h"
|
|
|
|
#include "vars.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "descriptor.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "async.h"
|
|
|
|
#include "throughput.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "physical.h"
|
|
|
|
#include "bundle.h"
|
|
|
|
#include "chat.h"
|
|
|
|
#include "ccp.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "auth.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "modem.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "ipcp.h"
|
1998-02-17 19:28:35 +00:00
|
|
|
#include "prompt.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "lcpproto.h"
|
|
|
|
#include "pap.h"
|
|
|
|
#include "chap.h"
|
|
|
|
#include "datalink.h"
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
static const char *datalink_State(struct datalink *);
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
datalink_OpenTimeout(void *v)
|
|
|
|
{
|
|
|
|
struct datalink *dl = (struct datalink *)v;
|
|
|
|
|
|
|
|
StopTimer(&dl->dial_timer);
|
|
|
|
if (dl->state == DATALINK_OPENING)
|
|
|
|
LogPrintf(LogPHASE, "%s: Redial timer expired.\n", dl->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
datalink_StartDialTimer(struct datalink *dl, int Timeout)
|
|
|
|
{
|
|
|
|
StopTimer(&dl->dial_timer);
|
|
|
|
|
|
|
|
if (Timeout) {
|
|
|
|
dl->dial_timer.state = TIMER_STOPPED;
|
|
|
|
if (Timeout > 0)
|
|
|
|
dl->dial_timer.load = Timeout * SECTICKS;
|
|
|
|
else
|
1998-02-16 19:11:10 +00:00
|
|
|
dl->dial_timer.load = (random() % DIAL_TIMEOUT) * SECTICKS;
|
1998-02-16 00:01:12 +00:00
|
|
|
dl->dial_timer.func = datalink_OpenTimeout;
|
|
|
|
dl->dial_timer.arg = dl;
|
|
|
|
StartTimer(&dl->dial_timer);
|
|
|
|
if (dl->state == DATALINK_OPENING)
|
|
|
|
LogPrintf(LogPHASE, "%s: Enter pause (%d) for redialing.\n",
|
|
|
|
dl->name, Timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
datalink_HangupDone(struct datalink *dl)
|
|
|
|
{
|
|
|
|
modem_Close(dl->physical);
|
1998-03-09 19:24:58 +00:00
|
|
|
dl->phone.chosen = "[N/A]";
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
if (!dl->dial_tries || (dl->dial_tries < 0 && !dl->reconnect_tries)) {
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering CLOSED state\n", dl->name);
|
|
|
|
dl->state = DATALINK_CLOSED;
|
|
|
|
dl->dial_tries = -1;
|
|
|
|
dl->reconnect_tries = 0;
|
|
|
|
bundle_LinkClosed(dl->bundle, dl);
|
1998-02-17 19:28:49 +00:00
|
|
|
datalink_StartDialTimer(dl, dl->cfg.dial_timeout);
|
1998-02-16 00:01:12 +00:00
|
|
|
} else {
|
1998-02-17 19:28:01 +00:00
|
|
|
LogPrintf(LogPHASE, "%s: Re-entering OPENING state\n", dl->name);
|
1998-02-16 00:01:12 +00:00
|
|
|
dl->state = DATALINK_OPENING;
|
1998-02-16 19:10:44 +00:00
|
|
|
if (dl->dial_tries < 0) {
|
1998-02-17 19:28:49 +00:00
|
|
|
datalink_StartDialTimer(dl, dl->cfg.reconnect_timeout);
|
|
|
|
dl->dial_tries = dl->cfg.max_dial;
|
1998-02-16 19:10:44 +00:00
|
|
|
dl->reconnect_tries--;
|
|
|
|
} else {
|
|
|
|
dl->dial_tries--;
|
1998-03-09 19:24:58 +00:00
|
|
|
if (dl->phone.next == NULL)
|
1998-02-17 19:28:49 +00:00
|
|
|
datalink_StartDialTimer(dl, dl->cfg.dial_timeout);
|
1998-02-16 19:10:44 +00:00
|
|
|
else
|
1998-02-17 19:28:49 +00:00
|
|
|
datalink_StartDialTimer(dl, dl->cfg.dial_next_timeout);
|
1998-02-16 19:10:44 +00:00
|
|
|
}
|
1998-02-16 00:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-09 19:24:58 +00:00
|
|
|
static const char *
|
|
|
|
datalink_ChoosePhoneNumber(struct datalink *dl)
|
|
|
|
{
|
|
|
|
char *phone;
|
|
|
|
|
|
|
|
if (dl->phone.alt == NULL) {
|
|
|
|
if (dl->phone.next == NULL) {
|
|
|
|
strncpy(dl->phone.list, dl->cfg.phone.list, sizeof dl->phone.list - 1);
|
|
|
|
dl->phone.list[sizeof dl->phone.list - 1] = '\0';
|
|
|
|
dl->phone.next = dl->phone.list;
|
|
|
|
}
|
|
|
|
dl->phone.alt = strsep(&dl->phone.next, ":");
|
|
|
|
}
|
|
|
|
phone = strsep(&dl->phone.alt, "|");
|
|
|
|
dl->phone.chosen = *phone ? phone : "[NONE]";
|
|
|
|
if (*phone)
|
|
|
|
LogPrintf(LogPHASE, "Phone: %s\n", phone);
|
|
|
|
return phone;
|
|
|
|
}
|
|
|
|
|
1998-02-17 01:05:47 +00:00
|
|
|
static void
|
|
|
|
datalink_LoginDone(struct datalink *dl)
|
|
|
|
{
|
1998-02-18 00:27:49 +00:00
|
|
|
if (!dl->script.packetmode) {
|
|
|
|
dl->dial_tries = -1;
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering READY state\n", dl->name);
|
|
|
|
dl->state = DATALINK_READY;
|
|
|
|
} else if (modem_Raw(dl->physical, dl->bundle) < 0) {
|
|
|
|
dl->dial_tries = 0;
|
1998-02-17 01:05:47 +00:00
|
|
|
LogPrintf(LogWARN, "datalink_LoginDone: Not connected.\n");
|
1998-02-18 00:27:49 +00:00
|
|
|
if (dl->script.run) {
|
1998-02-17 01:05:47 +00:00
|
|
|
LogPrintf(LogPHASE, "%s: Entering HANGUP state\n", dl->name);
|
|
|
|
dl->state = DATALINK_HANGUP;
|
|
|
|
modem_Offline(dl->physical);
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL);
|
1998-02-17 01:05:47 +00:00
|
|
|
} else
|
|
|
|
datalink_HangupDone(dl);
|
|
|
|
} else {
|
|
|
|
dl->dial_tries = -1;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
1998-02-21 01:45:26 +00:00
|
|
|
lcp_Setup(&LcpInfo, dl->state == DATALINK_READY ? 0 : VarOpenMode);
|
1998-02-23 00:38:44 +00:00
|
|
|
ccp_Setup(&dl->ccp);
|
1998-02-21 01:45:26 +00:00
|
|
|
|
1998-03-01 01:07:49 +00:00
|
|
|
LogPrintf(LogPHASE, "%s: Entering LCP state\n", dl->name);
|
|
|
|
dl->state = DATALINK_LCP;
|
1998-02-18 00:27:49 +00:00
|
|
|
FsmUp(&LcpInfo.fsm);
|
|
|
|
FsmOpen(&LcpInfo.fsm);
|
1998-02-17 01:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
static int
|
|
|
|
datalink_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
|
|
|
|
int *n)
|
|
|
|
{
|
|
|
|
struct datalink *dl = descriptor2datalink(d);
|
|
|
|
int result;
|
|
|
|
|
1998-02-16 00:18:52 +00:00
|
|
|
result = 0;
|
1998-02-16 00:01:12 +00:00
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_CLOSED:
|
1998-02-16 00:18:52 +00:00
|
|
|
break;
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
case DATALINK_OPENING:
|
|
|
|
if (dl->dial_timer.state != TIMER_RUNNING) {
|
|
|
|
if (--dl->dial_tries < 0)
|
|
|
|
dl->dial_tries = 0;
|
|
|
|
if (modem_Open(dl->physical, dl->bundle) >= 0) {
|
1998-02-17 01:05:47 +00:00
|
|
|
if (dl->script.run) {
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering DIAL state\n", dl->name);
|
|
|
|
dl->state = DATALINK_DIAL;
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(&dl->chat, dl->physical, dl->cfg.script.dial, 1,
|
|
|
|
datalink_ChoosePhoneNumber(dl));
|
1998-02-17 19:28:49 +00:00
|
|
|
if (!(mode & MODE_DDIAL) && dl->cfg.max_dial)
|
1998-02-17 01:05:47 +00:00
|
|
|
LogPrintf(LogCHAT, "%s: Dial attempt %u of %d\n",
|
1998-02-17 19:28:49 +00:00
|
|
|
dl->name, dl->cfg.max_dial - dl->dial_tries,
|
|
|
|
dl->cfg.max_dial);
|
1998-02-17 01:05:47 +00:00
|
|
|
} else
|
|
|
|
datalink_LoginDone(dl);
|
1998-02-16 00:01:12 +00:00
|
|
|
} else {
|
1998-02-17 19:28:49 +00:00
|
|
|
if (!(mode & MODE_DDIAL) && dl->cfg.max_dial)
|
1998-02-16 00:01:12 +00:00
|
|
|
LogPrintf(LogCHAT, "Failed to open modem (attempt %u of %d)\n",
|
1998-02-17 19:28:49 +00:00
|
|
|
dl->cfg.max_dial - dl->dial_tries, dl->cfg.max_dial);
|
1998-02-16 00:01:12 +00:00
|
|
|
else
|
|
|
|
LogPrintf(LogCHAT, "Failed to open modem\n");
|
|
|
|
|
1998-02-17 19:28:49 +00:00
|
|
|
if (!(mode & MODE_DDIAL) && dl->cfg.max_dial && dl->dial_tries == 0) {
|
1998-02-16 00:01:12 +00:00
|
|
|
LogPrintf(LogPHASE, "%s: Entering CLOSED state\n", dl->name);
|
|
|
|
dl->state = DATALINK_CLOSED;
|
|
|
|
dl->reconnect_tries = 0;
|
|
|
|
dl->dial_tries = -1;
|
1998-02-16 19:10:03 +00:00
|
|
|
bundle_LinkClosed(dl->bundle, dl);
|
1998-02-17 01:05:47 +00:00
|
|
|
}
|
1998-02-17 19:28:49 +00:00
|
|
|
datalink_StartDialTimer(dl, dl->cfg.dial_timeout);
|
1998-02-16 00:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
1998-02-16 00:18:52 +00:00
|
|
|
break;
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
case DATALINK_HANGUP:
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
case DATALINK_LOGIN:
|
|
|
|
result = descriptor_UpdateSet(&dl->chat.desc, r, w, e, n);
|
|
|
|
switch (dl->chat.state) {
|
|
|
|
case CHAT_DONE:
|
|
|
|
/* script succeeded */
|
|
|
|
switch(dl->state) {
|
|
|
|
case DATALINK_HANGUP:
|
|
|
|
datalink_HangupDone(dl);
|
|
|
|
break;
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering LOGIN state\n", dl->name);
|
|
|
|
dl->state = DATALINK_LOGIN;
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(&dl->chat, dl->physical, dl->cfg.script.login, 0, NULL);
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
|
|
|
case DATALINK_LOGIN:
|
1998-02-17 01:05:47 +00:00
|
|
|
datalink_LoginDone(dl);
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CHAT_FAILED:
|
|
|
|
/* Going down - script failed */
|
|
|
|
LogPrintf(LogWARN, "Chat script failed\n");
|
|
|
|
switch(dl->state) {
|
|
|
|
case DATALINK_HANGUP:
|
|
|
|
datalink_HangupDone(dl);
|
|
|
|
break;
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
case DATALINK_LOGIN:
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering HANGUP state\n", dl->name);
|
|
|
|
dl->state = DATALINK_HANGUP;
|
|
|
|
modem_Offline(dl->physical);
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL);
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
case DATALINK_READY:
|
1998-03-01 01:07:49 +00:00
|
|
|
case DATALINK_LCP:
|
|
|
|
case DATALINK_AUTH:
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_OPEN:
|
|
|
|
result = descriptor_UpdateSet(&dl->physical->desc, r, w, e, n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
datalink_IsSet(struct descriptor *d, fd_set *fdset)
|
|
|
|
{
|
|
|
|
struct datalink *dl = descriptor2datalink(d);
|
|
|
|
|
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_CLOSED:
|
1998-02-17 19:28:35 +00:00
|
|
|
case DATALINK_OPENING:
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_HANGUP:
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
case DATALINK_LOGIN:
|
|
|
|
return descriptor_IsSet(&dl->chat.desc, fdset);
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
case DATALINK_READY:
|
1998-03-01 01:07:49 +00:00
|
|
|
case DATALINK_LCP:
|
|
|
|
case DATALINK_AUTH:
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_OPEN:
|
|
|
|
return descriptor_IsSet(&dl->physical->desc, fdset);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
datalink_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
|
|
|
{
|
|
|
|
struct datalink *dl = descriptor2datalink(d);
|
|
|
|
|
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_CLOSED:
|
1998-02-17 19:28:35 +00:00
|
|
|
case DATALINK_OPENING:
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_HANGUP:
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
case DATALINK_LOGIN:
|
|
|
|
descriptor_Read(&dl->chat.desc, bundle, fdset);
|
|
|
|
break;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
case DATALINK_READY:
|
1998-03-01 01:07:49 +00:00
|
|
|
case DATALINK_LCP:
|
|
|
|
case DATALINK_AUTH:
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_OPEN:
|
|
|
|
descriptor_Read(&dl->physical->desc, bundle, fdset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-23 00:38:44 +00:00
|
|
|
datalink_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
1998-02-16 00:01:12 +00:00
|
|
|
{
|
|
|
|
struct datalink *dl = descriptor2datalink(d);
|
|
|
|
|
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_CLOSED:
|
1998-02-17 19:28:35 +00:00
|
|
|
case DATALINK_OPENING:
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_HANGUP:
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
case DATALINK_LOGIN:
|
1998-02-23 00:38:44 +00:00
|
|
|
descriptor_Write(&dl->chat.desc, bundle, fdset);
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
case DATALINK_READY:
|
1998-03-01 01:07:49 +00:00
|
|
|
case DATALINK_LCP:
|
|
|
|
case DATALINK_AUTH:
|
1998-02-16 00:01:12 +00:00
|
|
|
case DATALINK_OPEN:
|
1998-02-23 00:38:44 +00:00
|
|
|
descriptor_Write(&dl->physical->desc, bundle, fdset);
|
1998-02-16 00:01:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-01 01:07:49 +00:00
|
|
|
static void
|
|
|
|
datalink_ComeDown(struct datalink *dl, int stay)
|
|
|
|
{
|
|
|
|
if (stay) {
|
|
|
|
dl->dial_tries = -1;
|
|
|
|
dl->reconnect_tries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) {
|
|
|
|
modem_Offline(dl->physical);
|
|
|
|
if (dl->script.run && dl->state != DATALINK_OPENING) {
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering HANGUP state\n", dl->name);
|
|
|
|
dl->state = DATALINK_HANGUP;
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL);
|
1998-03-01 01:07:49 +00:00
|
|
|
} else
|
|
|
|
datalink_HangupDone(dl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-27 01:22:39 +00:00
|
|
|
static void
|
|
|
|
datalink_LayerStart(void *v, struct fsm *fp)
|
|
|
|
{
|
|
|
|
/* The given FSM is about to start up ! */
|
1998-03-01 01:07:49 +00:00
|
|
|
if (fp == &LcpInfo.fsm) {
|
|
|
|
struct datalink *dl = (struct datalink *)v;
|
|
|
|
(*dl->parent->LayerStart)(dl->parent->object, fp);
|
|
|
|
}
|
1998-02-27 01:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
datalink_LayerUp(void *v, struct fsm *fp)
|
|
|
|
{
|
|
|
|
/* The given fsm is now up */
|
|
|
|
struct datalink *dl = (struct datalink *)v;
|
|
|
|
|
|
|
|
if (fp == &LcpInfo.fsm) {
|
1998-03-01 01:07:49 +00:00
|
|
|
LcpInfo.auth_ineed = LcpInfo.want_auth;
|
|
|
|
LcpInfo.auth_iwait = LcpInfo.his_auth;
|
|
|
|
if (LcpInfo.his_auth || LcpInfo.want_auth) {
|
|
|
|
if (dl->bundle->phase == PHASE_DEAD ||
|
|
|
|
dl->bundle->phase == PHASE_ESTABLISH)
|
1998-03-13 00:44:33 +00:00
|
|
|
bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE);
|
1998-03-01 01:07:49 +00:00
|
|
|
LogPrintf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name,
|
|
|
|
Auth2Nam(LcpInfo.his_auth), Auth2Nam(LcpInfo.want_auth));
|
|
|
|
if (LcpInfo.his_auth == PROTO_PAP)
|
|
|
|
StartAuthChallenge(&dl->pap, dl->physical, SendPapChallenge);
|
|
|
|
if (LcpInfo.want_auth == PROTO_CHAP)
|
|
|
|
StartAuthChallenge(&dl->chap.auth, dl->physical, SendChapChallenge);
|
|
|
|
} else
|
|
|
|
datalink_AuthOk(dl);
|
1998-02-27 01:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-01 01:07:49 +00:00
|
|
|
void
|
|
|
|
datalink_AuthOk(struct datalink *dl)
|
|
|
|
{
|
|
|
|
FsmUp(&dl->ccp.fsm);
|
|
|
|
FsmOpen(&dl->ccp.fsm);
|
|
|
|
dl->state = DATALINK_OPEN;
|
|
|
|
(*dl->parent->LayerUp)(dl->parent->object, &LcpInfo.fsm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
datalink_AuthNotOk(struct datalink *dl)
|
|
|
|
{
|
|
|
|
dl->state = DATALINK_LCP;
|
|
|
|
FsmClose(&LcpInfo.fsm);
|
|
|
|
}
|
|
|
|
|
1998-02-27 01:22:39 +00:00
|
|
|
static void
|
|
|
|
datalink_LayerDown(void *v, struct fsm *fp)
|
|
|
|
{
|
|
|
|
/* The given FSM has been told to come down */
|
|
|
|
struct datalink *dl = (struct datalink *)v;
|
|
|
|
if (fp == &LcpInfo.fsm) {
|
1998-03-01 01:07:49 +00:00
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_OPEN:
|
|
|
|
FsmDown(&dl->ccp.fsm);
|
|
|
|
FsmClose(&dl->ccp.fsm);
|
|
|
|
(*dl->parent->LayerDown)(dl->parent->object, fp);
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case DATALINK_AUTH:
|
|
|
|
StopTimer(&dl->pap.authtimer);
|
|
|
|
StopTimer(&dl->chap.auth.authtimer);
|
|
|
|
}
|
|
|
|
dl->state = DATALINK_LCP;
|
1998-02-27 01:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
datalink_LayerFinish(void *v, struct fsm *fp)
|
|
|
|
{
|
|
|
|
/* The given fsm is now down */
|
|
|
|
struct datalink *dl = (struct datalink *)v;
|
|
|
|
|
|
|
|
if (fp == &LcpInfo.fsm) {
|
|
|
|
(*dl->parent->LayerFinish)(dl->parent->object, fp);
|
1998-03-01 01:07:49 +00:00
|
|
|
datalink_ComeDown(dl, 0);
|
1998-02-27 01:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
struct datalink *
|
1998-02-27 01:22:39 +00:00
|
|
|
datalink_Create(const char *name, struct bundle *bundle,
|
|
|
|
const struct fsm_parent *parent)
|
1998-02-16 00:01:12 +00:00
|
|
|
{
|
|
|
|
struct datalink *dl;
|
|
|
|
|
|
|
|
dl = (struct datalink *)malloc(sizeof(struct datalink));
|
|
|
|
if (dl == NULL)
|
|
|
|
return dl;
|
|
|
|
|
|
|
|
dl->desc.type = DATALINK_DESCRIPTOR;
|
|
|
|
dl->desc.next = NULL;
|
|
|
|
dl->desc.UpdateSet = datalink_UpdateSet;
|
|
|
|
dl->desc.IsSet = datalink_IsSet;
|
|
|
|
dl->desc.Read = datalink_Read;
|
|
|
|
dl->desc.Write = datalink_Write;
|
1998-02-16 19:10:03 +00:00
|
|
|
|
1998-02-16 19:11:10 +00:00
|
|
|
dl->state = DATALINK_CLOSED;
|
|
|
|
|
1998-02-17 19:28:49 +00:00
|
|
|
*dl->cfg.script.dial = '\0';
|
|
|
|
*dl->cfg.script.login = '\0';
|
|
|
|
*dl->cfg.script.hangup = '\0';
|
1998-03-09 19:24:58 +00:00
|
|
|
*dl->cfg.phone.list = '\0';
|
|
|
|
*dl->phone.list = '\0';
|
|
|
|
dl->phone.next = NULL;
|
|
|
|
dl->phone.alt = NULL;
|
|
|
|
dl->phone.chosen = "N/A";
|
1998-02-17 19:28:35 +00:00
|
|
|
dl->script.run = 1;
|
|
|
|
dl->script.packetmode = 1;
|
1998-02-16 19:10:03 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
dl->bundle = bundle;
|
|
|
|
dl->next = NULL;
|
1998-02-16 19:11:10 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
memset(&dl->dial_timer, '\0', sizeof dl->dial_timer);
|
1998-02-16 19:11:10 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
dl->dial_tries = 0;
|
1998-02-17 19:28:49 +00:00
|
|
|
dl->cfg.max_dial = 1;
|
|
|
|
dl->cfg.dial_timeout = DIAL_TIMEOUT;
|
|
|
|
dl->cfg.dial_next_timeout = DIAL_NEXT_TIMEOUT;
|
1998-02-16 19:11:10 +00:00
|
|
|
|
1998-02-16 19:10:44 +00:00
|
|
|
dl->reconnect_tries = 0;
|
1998-02-17 19:28:49 +00:00
|
|
|
dl->cfg.max_reconnect = 0;
|
|
|
|
dl->cfg.reconnect_timeout = RECONNECT_TIMEOUT;
|
1998-02-16 19:10:44 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
dl->name = strdup(name);
|
1998-03-13 00:44:51 +00:00
|
|
|
if ((dl->physical = modem_Create(dl->name, dl)) == NULL) {
|
1998-02-16 00:01:12 +00:00
|
|
|
free(dl->name);
|
|
|
|
free(dl);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(&dl->chat, dl->physical, NULL, 1, NULL);
|
1998-02-16 00:01:12 +00:00
|
|
|
|
1998-02-27 01:22:39 +00:00
|
|
|
dl->parent = parent;
|
|
|
|
dl->fsm.LayerStart = datalink_LayerStart;
|
|
|
|
dl->fsm.LayerUp = datalink_LayerUp;
|
|
|
|
dl->fsm.LayerDown = datalink_LayerDown;
|
|
|
|
dl->fsm.LayerFinish = datalink_LayerFinish;
|
|
|
|
dl->fsm.object = dl;
|
|
|
|
|
|
|
|
lcp_Init(&LcpInfo, dl->bundle, dl->physical, &dl->fsm);
|
|
|
|
ccp_Init(&dl->ccp, dl->bundle, &dl->physical->link, &dl->fsm);
|
1998-02-16 00:01:12 +00:00
|
|
|
|
1998-03-01 01:07:49 +00:00
|
|
|
authinfo_Init(&dl->pap);
|
|
|
|
authinfo_Init(&dl->chap.auth);
|
|
|
|
|
1998-02-17 19:28:35 +00:00
|
|
|
LogPrintf(LogPHASE, "%s: Created in CLOSED state\n", dl->name);
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct datalink *
|
|
|
|
datalink_Destroy(struct datalink *dl)
|
|
|
|
{
|
|
|
|
struct datalink *result;
|
|
|
|
|
|
|
|
if (dl->state != DATALINK_CLOSED)
|
1998-02-17 19:28:35 +00:00
|
|
|
LogPrintf(LogERROR, "Oops, destroying a datalink in state %s\n",
|
|
|
|
datalink_State(dl));
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
result = dl->next;
|
|
|
|
chat_Destroy(&dl->chat);
|
|
|
|
link_Destroy(&dl->physical->link);
|
|
|
|
free(dl->name);
|
|
|
|
free(dl);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-17 01:05:47 +00:00
|
|
|
datalink_Up(struct datalink *dl, int runscripts, int packetmode)
|
1998-02-16 00:01:12 +00:00
|
|
|
{
|
1998-02-17 19:28:35 +00:00
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_CLOSED:
|
|
|
|
LogPrintf(LogPHASE, "%s: Entering OPENING state\n", dl->name);
|
|
|
|
dl->state = DATALINK_OPENING;
|
1998-02-17 19:28:49 +00:00
|
|
|
dl->reconnect_tries = dl->cfg.max_reconnect;
|
|
|
|
dl->dial_tries = dl->cfg.max_dial;
|
1998-02-17 19:28:35 +00:00
|
|
|
dl->script.run = runscripts;
|
|
|
|
dl->script.packetmode = packetmode;
|
|
|
|
break;
|
1998-02-16 00:01:12 +00:00
|
|
|
|
1998-02-17 19:28:35 +00:00
|
|
|
case DATALINK_OPENING:
|
|
|
|
if (!dl->script.run && runscripts)
|
|
|
|
dl->script.run = 1;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case DATALINK_DIAL:
|
|
|
|
case DATALINK_LOGIN:
|
|
|
|
case DATALINK_READY:
|
|
|
|
if (!dl->script.packetmode && packetmode) {
|
|
|
|
dl->script.packetmode = 1;
|
|
|
|
if (dl->state == DATALINK_READY)
|
|
|
|
datalink_LoginDone(dl);
|
|
|
|
}
|
|
|
|
break;
|
1998-02-16 00:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-17 19:28:35 +00:00
|
|
|
void
|
|
|
|
datalink_Close(struct datalink *dl, int stay)
|
|
|
|
{
|
|
|
|
/* Please close */
|
1998-03-01 01:07:49 +00:00
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_OPEN:
|
|
|
|
FsmDown(&dl->ccp.fsm);
|
|
|
|
FsmClose(&dl->ccp.fsm);
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case DATALINK_AUTH:
|
|
|
|
case DATALINK_LCP:
|
|
|
|
FsmClose(&LcpInfo.fsm);
|
|
|
|
if (stay) {
|
|
|
|
dl->dial_tries = -1;
|
|
|
|
dl->reconnect_tries = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
datalink_ComeDown(dl, stay);
|
|
|
|
}
|
1998-02-17 19:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
datalink_Down(struct datalink *dl, int stay)
|
|
|
|
{
|
|
|
|
/* Carrier is lost */
|
1998-03-01 01:07:49 +00:00
|
|
|
switch (dl->state) {
|
|
|
|
case DATALINK_OPEN:
|
|
|
|
FsmDown(&dl->ccp.fsm);
|
|
|
|
FsmClose(&dl->ccp.fsm);
|
|
|
|
/* fall through */
|
1998-02-17 19:28:35 +00:00
|
|
|
|
1998-03-01 01:07:49 +00:00
|
|
|
case DATALINK_AUTH:
|
|
|
|
case DATALINK_LCP:
|
|
|
|
FsmDown(&LcpInfo.fsm);
|
|
|
|
if (stay)
|
|
|
|
FsmClose(&LcpInfo.fsm);
|
|
|
|
else
|
|
|
|
FsmOpen(&dl->ccp.fsm);
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
datalink_ComeDown(dl, stay);
|
|
|
|
}
|
1998-02-17 19:28:35 +00:00
|
|
|
}
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
void
|
|
|
|
datalink_StayDown(struct datalink *dl)
|
|
|
|
{
|
|
|
|
dl->reconnect_tries = 0;
|
|
|
|
}
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
datalink_Show(struct datalink *dl)
|
|
|
|
{
|
|
|
|
prompt_Printf(&prompt, "Link %s: State %s\n", dl->name, datalink_State(dl));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *states[] = {
|
|
|
|
"CLOSED",
|
|
|
|
"OPENING",
|
|
|
|
"HANGUP",
|
|
|
|
"DIAL",
|
|
|
|
"LOGIN",
|
|
|
|
"READY",
|
1998-03-01 01:07:49 +00:00
|
|
|
"LCP"
|
|
|
|
"AUTH"
|
1998-02-17 19:28:35 +00:00
|
|
|
"OPEN"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
datalink_State(struct datalink *dl)
|
|
|
|
{
|
|
|
|
if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0])
|
|
|
|
return "unknown";
|
|
|
|
return states[dl->state];
|
|
|
|
}
|