From d6093b819fd31be14becc6dfcaeb30001eb88bc1 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Wed, 31 Mar 2010 18:45:17 -0500 Subject: [PATCH] Refactor: GAtPPP connect callback The connect callback was not giving enough information and the information it was providing was not in a convenient form. - Provide the ppp interface name (e.g. tun0) - Provide ip, dns1 & dns2 as strings - Do not send the ppp structure in the callback, it is most likely present in the user data anyway --- gatchat/gatppp.c | 9 ++++++--- gatchat/gatppp.h | 6 +++--- gatchat/ppp_net.c | 32 +++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/gatchat/gatppp.c b/gatchat/gatppp.c index 8f196364..7afff531 100644 --- a/gatchat/gatppp.c +++ b/gatchat/gatppp.c @@ -49,10 +49,13 @@ void g_at_ppp_set_credentials(GAtPPP *ppp, const char *username, auth_set_credentials(ppp->auth, username, passwd); } -void g_at_ppp_set_connect_function(GAtPPP *ppp, - GAtPPPConnectFunc callback, gpointer user_data) +void g_at_ppp_set_connect_function(GAtPPP *ppp, GAtPPPConnectFunc func, + gpointer user_data) { - ppp->connect_cb = callback; + if (func == NULL) + return; + + ppp->connect_cb = func; ppp->connect_data = user_data; } diff --git a/gatchat/gatppp.h b/gatchat/gatppp.h index 2fba90f7..41aadcad 100644 --- a/gatchat/gatppp.h +++ b/gatchat/gatppp.h @@ -34,9 +34,9 @@ typedef enum _GAtPPPConnectStatus { G_AT_PPP_CONNECT_FAIL } GAtPPPConnectStatus; -typedef void (*GAtPPPConnectFunc)(GAtPPP *ppp, GAtPPPConnectStatus success, - guint32 ip_address, - guint32 dns1, guint32 dns2, +typedef void (*GAtPPPConnectFunc)(GAtPPPConnectStatus success, + const char *iface, const char *ip, + const char *dns1, const char *dns2, gpointer user_data); typedef void (*GAtPPPDisconnectFunc)(GAtPPP *ppp, gpointer user_data); diff --git a/gatchat/ppp_net.c b/gatchat/ppp_net.c index 3a74e0be..12e2d55b 100644 --- a/gatchat/ppp_net.c +++ b/gatchat/ppp_net.c @@ -222,14 +222,32 @@ static void ipcp_up(struct pppcp_data *pppcp) { struct ipcp_data *data = pppcp->priv; GAtPPP *ppp = pppcp->ppp; + char ip[INET_ADDRSTRLEN]; + char dns1[INET_ADDRSTRLEN]; + char dns2[INET_ADDRSTRLEN]; + struct in_addr addr; - /* call the connect function */ - if (ppp->connect_cb) - ppp->connect_cb(ppp, G_AT_PPP_CONNECT_SUCCESS, - __get_unaligned_long(data->ip_address), - __get_unaligned_long(data->primary_dns), - __get_unaligned_long(data->secondary_dns), - ppp->connect_data); + if (ppp->connect_cb == NULL) + return; + + memset(ip, 0, sizeof(ip)); + addr.s_addr = __get_unaligned_long(data->ip_address); + inet_ntop(AF_INET, &addr, ip, INET_ADDRSTRLEN); + + memset(dns1, 0, sizeof(dns1)); + addr.s_addr = __get_unaligned_long(data->primary_dns); + inet_ntop(AF_INET, &addr, dns1, INET_ADDRSTRLEN); + + memset(dns2, 0, sizeof(dns2)); + addr.s_addr = __get_unaligned_long(data->secondary_dns); + inet_ntop(AF_INET, &addr, dns2, INET_ADDRSTRLEN); + + ppp->connect_cb(G_AT_PPP_CONNECT_SUCCESS, + pppcp->ppp->net->if_name, + ip[0] ? ip : NULL, + dns1[0] ? dns1 : NULL, + dns2[0] ? dns2 : NULL, + ppp->connect_data); } static void ipcp_down(struct pppcp_data *data)