ppp: Refactor connect / disconnect callbacks

Right now it is very hard to figure out whether we should be calling the
connect callback or the disconnect callback.  So refactor as follows:
	- Connect callback is only called once the net is actually up
	- Disconnect callback is called once ppp is down, with a reason
	  for why it is so.
This commit is contained in:
Denis Kenzior 2010-04-30 15:31:26 -05:00
parent 9ae0dcb47d
commit 6d20194e75
2 changed files with 33 additions and 19 deletions

View File

@ -66,8 +66,9 @@ struct _GAtPPP {
char password[256];
GAtPPPConnectFunc connect_cb;
gpointer connect_data;
GAtDisconnectFunc disconnect_cb;
GAtPPPDisconnectFunc disconnect_cb;
gpointer disconnect_data;
GAtPPPDisconnectReason disconnect_reason;
GAtDebugFunc debugf;
gpointer debug_data;
};
@ -177,7 +178,8 @@ static void ppp_dead(GAtPPP *ppp)
{
/* notify interested parties */
if (ppp->disconnect_cb)
ppp->disconnect_cb(ppp->disconnect_data);
ppp->disconnect_cb(ppp->disconnect_reason,
ppp->disconnect_data);
}
static inline void ppp_enter_phase(GAtPPP *ppp, enum ppp_phase phase)
@ -209,6 +211,7 @@ void ppp_set_auth(GAtPPP *ppp, const guint8* auth_data)
void ppp_auth_notify(GAtPPP *ppp, gboolean success)
{
if (success == FALSE) {
ppp->disconnect_reason = G_AT_PPP_REASON_AUTH_FAIL;
pppcp_signal_close(ppp->lcp);
return;
}
@ -225,20 +228,19 @@ void ppp_ipcp_up_notify(GAtPPP *ppp, const char *ip,
{
ppp->net = ppp_net_new(ppp);
if (ppp->net == NULL) {
ppp->disconnect_reason = G_AT_PPP_REASON_NET_FAIL;
pppcp_signal_close(ppp->lcp);
return;
}
if (ppp_net_set_mtu(ppp->net, ppp->mtu) == FALSE)
g_printerr("Unable to set MTU\n");
ppp_enter_phase(ppp, PPP_PHASE_LINK_UP);
if (ppp->connect_cb == NULL)
return;
if (ppp->net == NULL)
ppp->connect_cb(G_AT_PPP_CONNECT_FAIL, NULL,
NULL, NULL, NULL, ppp->connect_data);
else
ppp->connect_cb(G_AT_PPP_CONNECT_SUCCESS,
ppp_net_get_interface(ppp->net),
if (ppp->connect_cb)
ppp->connect_cb(ppp_net_get_interface(ppp->net),
ip, dns1, dns2, ppp->connect_data);
}
@ -258,6 +260,7 @@ void ppp_ipcp_finished_notify(GAtPPP *ppp)
return;
/* Our IPCP parameter negotiation failed */
ppp->disconnect_reason = G_AT_PPP_REASON_IPCP_FAIL;
pppcp_signal_close(ppp->ipcp);
pppcp_signal_close(ppp->lcp);
}
@ -279,6 +282,9 @@ void ppp_lcp_down_notify(GAtPPP *ppp)
if (ppp->phase == PPP_PHASE_NETWORK || ppp->phase == PPP_PHASE_LINK_UP)
pppcp_signal_down(ppp->ipcp);
if (ppp->disconnect_reason == G_AT_PPP_REASON_UNKNOWN)
ppp->disconnect_reason = G_AT_PPP_REASON_PEER_CLOSED;
ppp_enter_phase(ppp, PPP_PHASE_TERMINATION);
}
@ -312,6 +318,7 @@ static void io_disconnect(gpointer user_data)
{
GAtPPP *ppp = user_data;
ppp->disconnect_reason = G_AT_PPP_REASON_LINK_DEAD;
pppcp_signal_down(ppp->lcp);
pppcp_signal_close(ppp->lcp);
}
@ -375,7 +382,7 @@ void g_at_ppp_set_connect_function(GAtPPP *ppp, GAtPPPConnectFunc func,
ppp->connect_data = user_data;
}
void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtDisconnectFunc func,
void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtPPPDisconnectFunc func,
gpointer user_data)
{
if (func == NULL)
@ -399,6 +406,7 @@ void g_at_ppp_shutdown(GAtPPP *ppp)
if (ppp->phase == PPP_PHASE_DEAD || ppp->phase == PPP_PHASE_TERMINATION)
return;
ppp->disconnect_reason = G_AT_PPP_REASON_LOCAL_CLOSE;
pppcp_signal_close(ppp->lcp);
}

View File

@ -33,22 +33,28 @@ struct _GAtPPP;
typedef struct _GAtPPP GAtPPP;
typedef enum _GAtPPPConnectStatus {
G_AT_PPP_CONNECT_SUCCESS,
G_AT_PPP_CONNECT_FAIL
} GAtPPPConnectStatus;
typedef enum _GAtPPPDisconnectReason {
G_AT_PPP_REASON_UNKNOWN,
G_AT_PPP_REASON_AUTH_FAIL, /* Failed to authenticate */
G_AT_PPP_REASON_IPCP_FAIL, /* Failed to negotiate IPCP */
G_AT_PPP_REASON_NET_FAIL, /* Failed to create tun */
G_AT_PPP_REASON_PEER_CLOSED, /* Peer initiated a close */
G_AT_PPP_REASON_LINK_DEAD, /* Link to the peer died */
G_AT_PPP_REASON_LOCAL_CLOSE, /* Normal user close */
} GAtPPPDisconnectReason;
typedef void (*GAtPPPConnectFunc)(GAtPPPConnectStatus success,
const char *iface, const char *ip,
typedef void (*GAtPPPConnectFunc)(const char *iface, const char *ip,
const char *dns1, const char *dns2,
gpointer user_data);
typedef void (*GAtPPPDisconnectFunc)(GAtPPPDisconnectReason reason,
gpointer user_data);
GAtPPP *g_at_ppp_new(GIOChannel *modem);
GAtPPP *g_at_ppp_new_from_io(GAtIO *io);
void g_at_ppp_open(GAtPPP *ppp);
void g_at_ppp_set_connect_function(GAtPPP *ppp, GAtPPPConnectFunc callback,
gpointer user_data);
void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtDisconnectFunc func,
void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtPPPDisconnectFunc func,
gpointer user_data);
void g_at_ppp_set_debug(GAtPPP *ppp, GAtDebugFunc func, gpointer user_data);
void g_at_ppp_shutdown(GAtPPP *ppp);