ppp: Transition the phase directly

Use of the generate event function, while more 'pure' with regard to how
the spec views transitions, actually makes code more difficult to read.

Instead use phase transitions directly inside gatppp.  This still bleeds
through a little into lcp code, and probably should be fixed in a better
way eventually.
This commit is contained in:
Denis Kenzior 2010-04-13 14:11:44 -05:00
parent 552db428ef
commit fed99b9265
3 changed files with 33 additions and 76 deletions

View File

@ -48,14 +48,6 @@
#define PPP_ADDR_FIELD 0xff
#define PPP_CTRL 0x03
enum ppp_phase {
PPP_DEAD = 0,
PPP_ESTABLISHMENT,
PPP_AUTHENTICATION,
PPP_NETWORK,
PPP_TERMINATION,
};
struct _GAtPPP {
gint ref_count;
enum ppp_phase phase;
@ -354,7 +346,7 @@ static void ppp_dead(GAtPPP *ppp)
ppp->disconnect_cb(ppp->disconnect_data);
}
static void ppp_transition_phase(GAtPPP *ppp, enum ppp_phase phase)
void ppp_enter_phase(GAtPPP *ppp, enum ppp_phase phase)
{
/* don't do anything if we're already there */
if (ppp->phase == phase)
@ -363,64 +355,33 @@ static void ppp_transition_phase(GAtPPP *ppp, enum ppp_phase phase)
/* set new phase */
ppp->phase = phase;
g_print("Entering new phase: %d\n", phase);
switch (phase) {
case PPP_ESTABLISHMENT:
/* send an UP event to the lcp layer */
case PPP_PHASE_ESTABLISHMENT:
/* send an UP & OPEN events to the lcp layer */
pppcp_signal_up(ppp->lcp);
pppcp_signal_open(ppp->lcp);
break;
case PPP_AUTHENTICATION:
/* we don't do authentication right now, so send NONE */
if (ppp->auth->proto == 0)
ppp_generate_event(ppp, PPP_NONE);
/* otherwise we need to wait for the peer to send us a challenge */
case PPP_PHASE_AUTHENTICATION:
/* If we don't expect auth, move on to network phase */
if (ppp->chap == NULL)
ppp_enter_phase(ppp, PPP_PHASE_NETWORK);
/* otherwise wait for the peer to send us a challenge */
break;
case PPP_TERMINATION:
/* send a CLOSE event to the lcp layer */
pppcp_signal_close(ppp->lcp);
break;
case PPP_DEAD:
ppp_dead(ppp);
break;
case PPP_NETWORK:
/* bring network phase up */
ppp_net_open(ppp->net);
case PPP_PHASE_NETWORK:
/* Send UP & OPEN events to the IPCP layer */
pppcp_signal_open(ppp->ipcp);
pppcp_signal_up(ppp->ipcp);
/* bring network phase up */
ppp_net_open(ppp->net);
break;
}
}
/*
* send the event handler a new event to process
*/
void ppp_generate_event(GAtPPP *ppp, enum ppp_event event)
{
switch (event) {
case PPP_UP:
/* causes transition to ppp establishment */
ppp_transition_phase(ppp, PPP_ESTABLISHMENT);
case PPP_PHASE_TERMINATION:
pppcp_signal_close(ppp->lcp);
break;
case PPP_OPENED:
ppp_transition_phase(ppp, PPP_AUTHENTICATION);
break;
case PPP_CLOSING:
/* causes transition to termination phase */
ppp_transition_phase(ppp, PPP_TERMINATION);
break;
case PPP_DOWN:
/* cases transition to dead phase */
ppp_transition_phase(ppp, PPP_DEAD);
break;
case PPP_NONE:
case PPP_SUCCESS:
/* causes transition to network phase */
ppp_transition_phase(ppp, PPP_NETWORK);
break;
case PPP_FAIL:
if (ppp->phase == PPP_ESTABLISHMENT)
ppp_transition_phase(ppp, PPP_DEAD);
else if (ppp->phase == PPP_AUTHENTICATION)
ppp_transition_phase(ppp, PPP_TERMINATION);
case PPP_PHASE_DEAD:
ppp_dead(ppp);
break;
}
}
@ -429,7 +390,6 @@ static void read_watcher_destroy_notify(GAtPPP *ppp)
{
ppp->read_watch = 0;
pppcp_signal_down(ppp->lcp);
pppcp_signal_close(ppp->lcp);
}
void ppp_set_auth(GAtPPP *ppp, const guint8* auth_data)
@ -480,9 +440,7 @@ gboolean ppp_get_acfc(GAtPPP *ppp)
/* Administrative Open */
void g_at_ppp_open(GAtPPP *ppp)
{
/* send an open event to the lcp layer */
pppcp_signal_open(ppp->lcp);
pppcp_signal_up(ppp->lcp);
ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
}
void g_at_ppp_set_credentials(GAtPPP *ppp, const char *username,
@ -539,7 +497,7 @@ void g_at_ppp_set_recording(GAtPPP *ppp, const char *filename)
void g_at_ppp_shutdown(GAtPPP *ppp)
{
ppp_generate_event(ppp, PPP_CLOSING);
pppcp_signal_close(ppp->lcp);
}
void g_at_ppp_ref(GAtPPP *ppp)
@ -666,7 +624,7 @@ static void ppp_xmit_destroy_notify(gpointer destroy_data)
ppp->write_watch = 0;
if (ppp->phase == PPP_DEAD)
if (ppp->phase == PPP_PHASE_DEAD)
ppp_dead(ppp);
}

View File

@ -26,14 +26,12 @@
#define IPCP_PROTO 0x8021
#define PPP_IP_PROTO 0x0021
enum ppp_event {
PPP_UP = 1,
PPP_OPENED,
PPP_SUCCESS,
PPP_NONE,
PPP_CLOSING,
PPP_FAIL,
PPP_DOWN
enum ppp_phase {
PPP_PHASE_DEAD = 0, /* Link dead */
PPP_PHASE_ESTABLISHMENT, /* LCP started */
PPP_PHASE_AUTHENTICATION, /* Auth started */
PPP_PHASE_NETWORK, /* IPCP started */
PPP_PHASE_TERMINATION, /* LCP Terminate phase */
};
struct ppp_header {
@ -90,7 +88,7 @@ struct ppp_net_data {
};
void ppp_debug(GAtPPP *ppp, const char *str);
void ppp_generate_event(GAtPPP *ppp, enum ppp_event event);
void ppp_enter_phase(GAtPPP *ppp, enum ppp_phase phase);
void ppp_transmit(GAtPPP *ppp, guint8 *packet, guint infolen);
void ppp_set_auth(GAtPPP *ppp, const guint8 *auth_data);
void ppp_set_recv_accm(GAtPPP *ppp, guint32 accm);

View File

@ -103,7 +103,7 @@ static void lcp_reset_config_options(struct lcp_data *lcp)
*/
static void lcp_up(struct pppcp_data *pppcp)
{
ppp_generate_event(pppcp_get_ppp(pppcp), PPP_OPENED);
ppp_enter_phase(pppcp_get_ppp(pppcp), PPP_PHASE_AUTHENTICATION);
}
/*
@ -115,6 +115,7 @@ static void lcp_down(struct pppcp_data *pppcp)
lcp_reset_config_options(lcp);
pppcp_set_local_options(pppcp, lcp->options, lcp->options_len);
ppp_enter_phase(pppcp_get_ppp(pppcp), PPP_PHASE_TERMINATION);
}
/*
@ -123,7 +124,7 @@ static void lcp_down(struct pppcp_data *pppcp)
*/
static void lcp_finished(struct pppcp_data *pppcp)
{
ppp_generate_event(pppcp_get_ppp(pppcp), PPP_DOWN);
ppp_enter_phase(pppcp_get_ppp(pppcp), PPP_PHASE_DEAD);
}
static void lcp_rca(struct pppcp_data *pppcp, const struct pppcp_packet *packet)