diff options
author | Denis Kenzior <denkenz@gmail.com> | 2010-04-30 15:31:26 -0500 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2010-04-30 15:31:26 -0500 |
commit | 6d20194e7520a2190429b9f4582a7ee3b573d3c9 (patch) | |
tree | 982d847212b6658c7c1835c2cd6f55f6a6fbc430 /gatchat | |
parent | 9ae0dcb47d71803681a205c4708be82d27111443 (diff) |
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.
Diffstat (limited to 'gatchat')
-rw-r--r-- | gatchat/gatppp.c | 32 | ||||
-rw-r--r-- | gatchat/gatppp.h | 20 |
2 files changed, 33 insertions, 19 deletions
diff --git a/gatchat/gatppp.c b/gatchat/gatppp.c index 253b33fe..70669b04 100644 --- a/gatchat/gatppp.c +++ b/gatchat/gatppp.c @@ -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); } diff --git a/gatchat/gatppp.h b/gatchat/gatppp.h index 7631fe58..438b9521 100644 --- a/gatchat/gatppp.h +++ b/gatchat/gatppp.h @@ -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); |