2010-03-23 00:05:56 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* PPP library with GLib integration
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
2010-04-01 20:47:25 +00:00
|
|
|
#include <sys/stat.h>
|
2010-04-02 03:33:03 +00:00
|
|
|
#include <sys/time.h>
|
2010-03-23 00:05:56 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "gatutil.h"
|
2010-04-28 20:31:59 +00:00
|
|
|
#include "gathdlc.h"
|
2010-03-23 00:05:56 +00:00
|
|
|
#include "gatppp.h"
|
2010-04-09 15:08:30 +00:00
|
|
|
#include "crc-ccitt.h"
|
2010-03-23 00:05:56 +00:00
|
|
|
#include "ppp.h"
|
|
|
|
|
2010-04-04 05:32:27 +00:00
|
|
|
#define DEFAULT_MRU 1500
|
2010-04-21 22:26:30 +00:00
|
|
|
#define DEFAULT_MTU 1500
|
2010-04-04 05:32:27 +00:00
|
|
|
|
2010-05-10 21:28:23 +00:00
|
|
|
#define PPP_ADDR_FIELD 0xff
|
|
|
|
#define PPP_CTRL 0x03
|
|
|
|
|
2011-05-20 09:38:14 +00:00
|
|
|
#define GUARD_TIMEOUTS 1500
|
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
enum ppp_phase {
|
|
|
|
PPP_PHASE_DEAD = 0, /* Link dead */
|
|
|
|
PPP_PHASE_ESTABLISHMENT, /* LCP started */
|
|
|
|
PPP_PHASE_AUTHENTICATION, /* Auth started */
|
|
|
|
PPP_PHASE_NETWORK, /* IPCP started */
|
2011-05-24 12:58:30 +00:00
|
|
|
PPP_PHASE_LINK_UP, /* IPCP negotiation ok, link up */
|
2010-04-30 19:37:17 +00:00
|
|
|
PPP_PHASE_TERMINATION, /* LCP Terminate phase */
|
|
|
|
};
|
|
|
|
|
2010-04-04 05:25:09 +00:00
|
|
|
struct _GAtPPP {
|
|
|
|
gint ref_count;
|
|
|
|
enum ppp_phase phase;
|
|
|
|
struct pppcp_data *lcp;
|
|
|
|
struct pppcp_data *ipcp;
|
2010-04-13 19:42:59 +00:00
|
|
|
struct ppp_net *net;
|
2010-04-13 19:20:16 +00:00
|
|
|
struct ppp_chap *chap;
|
2010-04-28 20:31:59 +00:00
|
|
|
GAtHDLC *hdlc;
|
2010-04-04 05:25:09 +00:00
|
|
|
gint mru;
|
2010-04-21 22:26:30 +00:00
|
|
|
gint mtu;
|
2010-04-13 19:14:23 +00:00
|
|
|
char username[256];
|
|
|
|
char password[256];
|
2010-04-04 05:25:09 +00:00
|
|
|
GAtPPPConnectFunc connect_cb;
|
|
|
|
gpointer connect_data;
|
2010-04-30 20:31:26 +00:00
|
|
|
GAtPPPDisconnectFunc disconnect_cb;
|
2010-04-04 05:25:09 +00:00
|
|
|
gpointer disconnect_data;
|
2010-04-30 20:31:26 +00:00
|
|
|
GAtPPPDisconnectReason disconnect_reason;
|
2010-04-04 05:25:09 +00:00
|
|
|
GAtDebugFunc debugf;
|
|
|
|
gpointer debug_data;
|
2011-02-26 06:14:29 +00:00
|
|
|
gboolean sta_pending;
|
2011-02-28 20:26:26 +00:00
|
|
|
guint ppp_dead_source;
|
2011-05-04 15:39:01 +00:00
|
|
|
GAtSuspendFunc suspend_func;
|
|
|
|
gpointer suspend_data;
|
2011-05-19 09:58:28 +00:00
|
|
|
int fd;
|
2011-05-22 13:34:08 +00:00
|
|
|
guint guard_timeout_source;
|
2011-05-20 09:38:14 +00:00
|
|
|
gboolean suspended;
|
2010-04-04 05:25:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void ppp_debug(GAtPPP *ppp, const char *str)
|
|
|
|
{
|
2010-11-27 19:39:00 +00:00
|
|
|
if (ppp == NULL || ppp->debugf == NULL)
|
2010-04-04 05:25:09 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
ppp->debugf(str, ppp->debug_data);
|
|
|
|
}
|
|
|
|
|
2011-02-28 20:26:26 +00:00
|
|
|
static gboolean ppp_dead(gpointer userdata)
|
2011-02-26 06:14:29 +00:00
|
|
|
{
|
2011-02-28 20:26:26 +00:00
|
|
|
GAtPPP *ppp = userdata;
|
|
|
|
|
2011-02-26 06:14:29 +00:00
|
|
|
DBG(ppp, "");
|
|
|
|
|
2011-02-28 20:26:26 +00:00
|
|
|
ppp->ppp_dead_source = 0;
|
|
|
|
|
2011-02-26 06:14:29 +00:00
|
|
|
/* notify interested parties */
|
|
|
|
if (ppp->disconnect_cb)
|
|
|
|
ppp->disconnect_cb(ppp->disconnect_reason,
|
|
|
|
ppp->disconnect_data);
|
2011-02-28 20:26:26 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2011-02-26 06:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sta_sent(gpointer userdata)
|
|
|
|
{
|
|
|
|
GAtPPP *ppp = userdata;
|
|
|
|
|
|
|
|
DBG(ppp, "");
|
|
|
|
|
|
|
|
ppp->sta_pending = FALSE;
|
|
|
|
|
|
|
|
if (ppp->phase == PPP_PHASE_DEAD)
|
|
|
|
ppp_dead(ppp);
|
|
|
|
}
|
|
|
|
|
2010-05-13 17:31:32 +00:00
|
|
|
struct ppp_header *ppp_packet_new(gsize infolen, guint16 protocol)
|
|
|
|
{
|
|
|
|
struct ppp_header *ppp_packet;
|
|
|
|
|
|
|
|
ppp_packet = g_try_malloc0(infolen + sizeof(*ppp_packet));
|
|
|
|
if (ppp_packet == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ppp_packet->proto = htons(protocol);
|
|
|
|
ppp_packet->address = PPP_ADDR_FIELD;
|
|
|
|
ppp_packet->control = PPP_CTRL;
|
|
|
|
|
|
|
|
return ppp_packet;
|
|
|
|
}
|
|
|
|
|
2010-04-29 19:18:29 +00:00
|
|
|
/*
|
|
|
|
* Silently discard packets which are received when they shouldn't be
|
|
|
|
*/
|
|
|
|
static inline gboolean ppp_drop_packet(GAtPPP *ppp, guint16 protocol)
|
|
|
|
{
|
|
|
|
switch (ppp->phase) {
|
|
|
|
case PPP_PHASE_ESTABLISHMENT:
|
|
|
|
case PPP_PHASE_TERMINATION:
|
|
|
|
if (protocol != LCP_PROTOCOL)
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
case PPP_PHASE_AUTHENTICATION:
|
|
|
|
if (protocol != LCP_PROTOCOL && protocol != CHAP_PROTOCOL)
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
case PPP_PHASE_DEAD:
|
|
|
|
return TRUE;
|
|
|
|
case PPP_PHASE_NETWORK:
|
2010-04-30 19:37:17 +00:00
|
|
|
if (protocol != LCP_PROTOCOL && protocol != CHAP_PROTOCOL &&
|
|
|
|
protocol != IPCP_PROTO)
|
2010-04-29 19:18:29 +00:00
|
|
|
return TRUE;
|
|
|
|
break;
|
2010-04-30 19:37:17 +00:00
|
|
|
case PPP_PHASE_LINK_UP:
|
|
|
|
break;
|
2010-04-29 19:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-28 20:31:59 +00:00
|
|
|
static void ppp_receive(const unsigned char *buf, gsize len, void *data)
|
2010-04-02 03:33:03 +00:00
|
|
|
{
|
2010-04-28 20:31:59 +00:00
|
|
|
GAtPPP *ppp = data;
|
|
|
|
guint16 protocol = ppp_proto(buf);
|
|
|
|
const guint8 *packet = ppp_info(buf);
|
2010-04-02 03:33:03 +00:00
|
|
|
|
2010-04-29 19:18:29 +00:00
|
|
|
if (ppp_drop_packet(ppp, protocol))
|
2010-04-26 17:28:41 +00:00
|
|
|
return;
|
|
|
|
|
2010-04-02 20:10:29 +00:00
|
|
|
switch (protocol) {
|
|
|
|
case PPP_IP_PROTO:
|
2010-04-29 19:18:29 +00:00
|
|
|
ppp_net_process_packet(ppp->net, packet);
|
2010-04-02 20:10:29 +00:00
|
|
|
break;
|
|
|
|
case LCP_PROTOCOL:
|
|
|
|
pppcp_process_packet(ppp->lcp, packet);
|
|
|
|
break;
|
|
|
|
case IPCP_PROTO:
|
|
|
|
pppcp_process_packet(ppp->ipcp, packet);
|
|
|
|
break;
|
|
|
|
case CHAP_PROTOCOL:
|
2010-04-13 19:20:16 +00:00
|
|
|
if (ppp->chap) {
|
|
|
|
ppp_chap_process_packet(ppp->chap, packet);
|
2010-04-02 20:10:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-04-02 20:48:44 +00:00
|
|
|
/* fall through */
|
2010-04-02 20:10:29 +00:00
|
|
|
default:
|
2010-04-28 20:31:59 +00:00
|
|
|
pppcp_send_protocol_reject(ppp->lcp, buf, len);
|
2010-04-02 20:48:44 +00:00
|
|
|
break;
|
2010-04-02 20:10:29 +00:00
|
|
|
};
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-28 20:31:59 +00:00
|
|
|
/*
|
|
|
|
* transmit out through the lower layer interface
|
|
|
|
*
|
|
|
|
* infolen - length of the information part of the packet
|
|
|
|
*/
|
|
|
|
void ppp_transmit(GAtPPP *ppp, guint8 *packet, guint infolen)
|
2010-04-02 03:33:03 +00:00
|
|
|
{
|
2010-05-10 20:06:37 +00:00
|
|
|
struct ppp_header *header = (struct ppp_header *) packet;
|
|
|
|
guint16 proto = ppp_proto(packet);
|
2010-04-28 20:31:59 +00:00
|
|
|
guint8 code;
|
|
|
|
gboolean lcp = (proto == LCP_PROTOCOL);
|
|
|
|
guint32 xmit_accm = 0;
|
2011-02-26 06:14:29 +00:00
|
|
|
gboolean sta = FALSE;
|
2010-04-02 03:33:03 +00:00
|
|
|
|
2010-04-28 20:31:59 +00:00
|
|
|
/*
|
|
|
|
* all LCP Link Configuration, Link Termination, and Code-Reject
|
|
|
|
* packets must be sent with the default sending ACCM
|
2010-04-02 03:33:03 +00:00
|
|
|
*/
|
2010-04-28 20:31:59 +00:00
|
|
|
if (lcp) {
|
|
|
|
code = pppcp_get_code(packet);
|
|
|
|
lcp = code > 0 && code < 8;
|
2011-02-26 06:14:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're going down, we try to make sure to send the final
|
|
|
|
* ack before informing the upper layers via the ppp_disconnect
|
|
|
|
* function. Once we enter PPP_DEAD phase, no further packets
|
|
|
|
* will be sent
|
|
|
|
*/
|
|
|
|
if (code == PPPCP_CODE_TYPE_TERMINATE_ACK)
|
|
|
|
sta = TRUE;
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-28 20:31:59 +00:00
|
|
|
if (lcp) {
|
|
|
|
xmit_accm = g_at_hdlc_get_xmit_accm(ppp->hdlc);
|
|
|
|
g_at_hdlc_set_xmit_accm(ppp->hdlc, ~0U);
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-10 20:06:37 +00:00
|
|
|
header->address = PPP_ADDR_FIELD;
|
|
|
|
header->control = PPP_CTRL;
|
|
|
|
|
|
|
|
if (g_at_hdlc_send(ppp->hdlc, packet,
|
2011-02-26 06:14:29 +00:00
|
|
|
infolen + sizeof(*header)) == TRUE) {
|
|
|
|
if (sta) {
|
|
|
|
GAtIO *io = g_at_hdlc_get_io(ppp->hdlc);
|
|
|
|
|
|
|
|
ppp->sta_pending = TRUE;
|
|
|
|
g_at_io_set_write_done(io, sta_sent, ppp);
|
|
|
|
}
|
|
|
|
} else
|
2011-02-26 06:18:27 +00:00
|
|
|
DBG(ppp, "Failed to send a frame\n");
|
2010-04-28 20:31:59 +00:00
|
|
|
|
|
|
|
if (lcp)
|
|
|
|
g_at_hdlc_set_xmit_accm(ppp->hdlc, xmit_accm);
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
static inline void ppp_enter_phase(GAtPPP *ppp, enum ppp_phase phase)
|
2010-04-02 03:33:03 +00:00
|
|
|
{
|
2011-02-26 06:18:27 +00:00
|
|
|
DBG(ppp, "%d", phase);
|
2010-04-30 19:37:17 +00:00
|
|
|
ppp->phase = phase;
|
2010-04-13 19:11:44 +00:00
|
|
|
|
2011-02-26 06:14:29 +00:00
|
|
|
if (phase == PPP_PHASE_DEAD && ppp->sta_pending == FALSE)
|
2011-02-28 20:26:26 +00:00
|
|
|
ppp->ppp_dead_source = g_idle_add(ppp_dead, ppp);
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:40:41 +00:00
|
|
|
void ppp_set_auth(GAtPPP *ppp, const guint8* auth_data)
|
2010-04-02 03:33:03 +00:00
|
|
|
{
|
|
|
|
guint16 proto = get_host_short(auth_data);
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case CHAP_PROTOCOL:
|
2010-04-13 19:20:16 +00:00
|
|
|
if (ppp->chap)
|
|
|
|
ppp_chap_free(ppp->chap);
|
|
|
|
|
|
|
|
ppp->chap = ppp_chap_new(ppp, auth_data[2]);
|
2010-04-02 03:33:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-02-26 06:18:27 +00:00
|
|
|
DBG(ppp, "unknown authentication proto");
|
2010-04-02 03:33:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-13 19:19:04 +00:00
|
|
|
void ppp_auth_notify(GAtPPP *ppp, gboolean success)
|
|
|
|
{
|
2010-04-30 19:37:17 +00:00
|
|
|
if (success == FALSE) {
|
2010-04-30 20:31:26 +00:00
|
|
|
ppp->disconnect_reason = G_AT_PPP_REASON_AUTH_FAIL;
|
2010-04-30 19:37:17 +00:00
|
|
|
pppcp_signal_close(ppp->lcp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_NETWORK);
|
|
|
|
|
|
|
|
/* Send UP & OPEN events to the IPCP layer */
|
|
|
|
pppcp_signal_open(ppp->ipcp);
|
|
|
|
pppcp_signal_up(ppp->ipcp);
|
2010-04-13 19:19:04 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 03:19:05 +00:00
|
|
|
void ppp_ipcp_up_notify(GAtPPP *ppp, const char *local, const char *peer,
|
2010-04-13 19:55:10 +00:00
|
|
|
const char *dns1, const char *dns2)
|
|
|
|
{
|
2011-05-19 09:58:28 +00:00
|
|
|
ppp->net = ppp_net_new(ppp, ppp->fd);
|
2010-04-13 19:55:10 +00:00
|
|
|
|
2011-05-22 11:43:03 +00:00
|
|
|
/*
|
|
|
|
* ppp_net_new took control over the fd, whatever happens is out of
|
|
|
|
* our hands now
|
|
|
|
*/
|
|
|
|
ppp->fd = -1;
|
|
|
|
|
2010-04-30 20:31:26 +00:00
|
|
|
if (ppp->net == NULL) {
|
|
|
|
ppp->disconnect_reason = G_AT_PPP_REASON_NET_FAIL;
|
|
|
|
pppcp_signal_close(ppp->lcp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-22 22:49:15 +00:00
|
|
|
if (ppp_net_set_mtu(ppp->net, ppp->mtu) == FALSE)
|
2011-02-26 06:18:27 +00:00
|
|
|
DBG(ppp, "Unable to set MTU");
|
2010-04-21 22:26:30 +00:00
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_LINK_UP);
|
|
|
|
|
2010-04-30 20:31:26 +00:00
|
|
|
if (ppp->connect_cb)
|
|
|
|
ppp->connect_cb(ppp_net_get_interface(ppp->net),
|
2010-06-25 03:19:05 +00:00
|
|
|
local, peer, dns1, dns2,
|
|
|
|
ppp->connect_data);
|
2010-04-13 19:55:10 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
void ppp_ipcp_down_notify(GAtPPP *ppp)
|
2010-04-13 19:59:13 +00:00
|
|
|
{
|
2010-04-28 22:05:06 +00:00
|
|
|
/* Most likely we failed to create the interface */
|
|
|
|
if (ppp->net == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-04-13 20:14:40 +00:00
|
|
|
ppp_net_free(ppp->net);
|
|
|
|
ppp->net = NULL;
|
2010-04-13 19:59:13 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
void ppp_ipcp_finished_notify(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
if (ppp->phase != PPP_PHASE_NETWORK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Our IPCP parameter negotiation failed */
|
2010-04-30 20:31:26 +00:00
|
|
|
ppp->disconnect_reason = G_AT_PPP_REASON_IPCP_FAIL;
|
2010-04-30 19:37:17 +00:00
|
|
|
pppcp_signal_close(ppp->ipcp);
|
|
|
|
pppcp_signal_close(ppp->lcp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ppp_lcp_up_notify(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
/* Wait for the peer to send us a challenge if we expect auth */
|
|
|
|
if (ppp->chap != NULL) {
|
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_AUTHENTICATION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise proceed as if auth succeeded */
|
|
|
|
ppp_auth_notify(ppp, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ppp_lcp_down_notify(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
if (ppp->phase == PPP_PHASE_NETWORK || ppp->phase == PPP_PHASE_LINK_UP)
|
|
|
|
pppcp_signal_down(ppp->ipcp);
|
|
|
|
|
2010-04-30 20:31:26 +00:00
|
|
|
if (ppp->disconnect_reason == G_AT_PPP_REASON_UNKNOWN)
|
|
|
|
ppp->disconnect_reason = G_AT_PPP_REASON_PEER_CLOSED;
|
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_TERMINATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ppp_lcp_finished_notify(GAtPPP *ppp)
|
|
|
|
{
|
2010-05-10 19:12:45 +00:00
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_DEAD);
|
2010-04-30 19:37:17 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 03:33:03 +00:00
|
|
|
void ppp_set_recv_accm(GAtPPP *ppp, guint32 accm)
|
|
|
|
{
|
2010-04-28 20:31:59 +00:00
|
|
|
g_at_hdlc_set_recv_accm(ppp->hdlc, accm);
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-07 03:45:14 +00:00
|
|
|
void ppp_set_xmit_accm(GAtPPP *ppp, guint32 accm)
|
2010-04-02 03:33:03 +00:00
|
|
|
{
|
2010-04-28 20:31:59 +00:00
|
|
|
g_at_hdlc_set_xmit_accm(ppp->hdlc, accm);
|
2010-04-02 03:33:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 22:26:30 +00:00
|
|
|
/*
|
|
|
|
* The only time we use other than default MTU is when we are in
|
|
|
|
* the network phase.
|
|
|
|
*/
|
|
|
|
void ppp_set_mtu(GAtPPP *ppp, const guint8 *data)
|
|
|
|
{
|
|
|
|
guint16 mtu = get_host_short(data);
|
|
|
|
|
|
|
|
ppp->mtu = mtu;
|
|
|
|
}
|
|
|
|
|
2010-04-28 22:24:36 +00:00
|
|
|
static void io_disconnect(gpointer user_data)
|
|
|
|
{
|
|
|
|
GAtPPP *ppp = user_data;
|
|
|
|
|
2010-05-10 19:12:45 +00:00
|
|
|
if (ppp->phase == PPP_PHASE_DEAD)
|
|
|
|
return;
|
|
|
|
|
2010-04-30 20:31:26 +00:00
|
|
|
ppp->disconnect_reason = G_AT_PPP_REASON_LINK_DEAD;
|
2010-04-28 22:24:36 +00:00
|
|
|
pppcp_signal_down(ppp->lcp);
|
2010-04-30 19:37:17 +00:00
|
|
|
pppcp_signal_close(ppp->lcp);
|
2010-04-28 22:24:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 10:22:58 +00:00
|
|
|
static void ppp_proxy_suspend_net_interface(gpointer user_data)
|
|
|
|
{
|
|
|
|
GAtPPP *ppp = user_data;
|
|
|
|
|
|
|
|
ppp->suspended = TRUE;
|
|
|
|
ppp_net_suspend_interface(ppp->net);
|
|
|
|
|
|
|
|
if (ppp->suspend_func)
|
|
|
|
ppp->suspend_func(ppp->suspend_data);
|
|
|
|
}
|
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
gboolean g_at_ppp_listen(GAtPPP *ppp, GAtIO *io)
|
2010-03-23 00:05:56 +00:00
|
|
|
{
|
2011-05-24 13:08:34 +00:00
|
|
|
ppp->hdlc = g_at_hdlc_new_from_io(io);
|
|
|
|
if (ppp->hdlc == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ppp->suspended = FALSE;
|
|
|
|
g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp);
|
2011-05-25 10:22:58 +00:00
|
|
|
g_at_hdlc_set_suspend_function(ppp->hdlc,
|
|
|
|
ppp_proxy_suspend_net_interface, ppp);
|
2011-05-24 13:08:34 +00:00
|
|
|
g_at_io_set_disconnect_function(io, io_disconnect, ppp);
|
|
|
|
|
2010-04-13 19:11:44 +00:00
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
|
2010-04-30 19:37:17 +00:00
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Administrative Open */
|
|
|
|
gboolean g_at_ppp_open(GAtPPP *ppp, GAtIO *io)
|
|
|
|
{
|
|
|
|
ppp->hdlc = g_at_hdlc_new_from_io(io);
|
|
|
|
if (ppp->hdlc == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ppp->suspended = FALSE;
|
|
|
|
g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp);
|
2011-05-25 10:22:58 +00:00
|
|
|
g_at_hdlc_set_suspend_function(ppp->hdlc,
|
|
|
|
ppp_proxy_suspend_net_interface, ppp);
|
2011-05-24 13:08:34 +00:00
|
|
|
g_at_hdlc_set_no_carrier_detect(ppp->hdlc, TRUE);
|
|
|
|
g_at_io_set_disconnect_function(io, io_disconnect, ppp);
|
|
|
|
|
2010-04-30 19:37:17 +00:00
|
|
|
/* send an UP & OPEN events to the lcp layer */
|
|
|
|
pppcp_signal_up(ppp->lcp);
|
|
|
|
pppcp_signal_open(ppp->lcp);
|
2011-05-24 13:08:34 +00:00
|
|
|
|
|
|
|
ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
|
|
|
|
|
|
|
|
return TRUE;
|
2010-03-23 00:05:56 +00:00
|
|
|
}
|
|
|
|
|
2010-04-13 19:14:23 +00:00
|
|
|
gboolean g_at_ppp_set_credentials(GAtPPP *ppp, const char *username,
|
|
|
|
const char *password)
|
2010-03-23 00:05:59 +00:00
|
|
|
{
|
2010-04-13 19:14:23 +00:00
|
|
|
if (username && strlen(username) > 255)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (password && strlen(password) > 255)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
memset(ppp->username, 0, sizeof(ppp->username));
|
|
|
|
memset(ppp->password, 0, sizeof(ppp->password));
|
|
|
|
|
|
|
|
if (username)
|
|
|
|
strcpy(ppp->username, username);
|
|
|
|
|
|
|
|
if (password)
|
|
|
|
strcpy(ppp->password, password);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *g_at_ppp_get_username(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
return ppp->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *g_at_ppp_get_password(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
return ppp->password;
|
2010-03-23 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 01:25:10 +00:00
|
|
|
void g_at_ppp_set_recording(GAtPPP *ppp, const char *filename)
|
2010-04-28 20:54:58 +00:00
|
|
|
{
|
|
|
|
if (ppp == NULL)
|
2010-04-29 01:25:10 +00:00
|
|
|
return;
|
2010-04-28 20:54:58 +00:00
|
|
|
|
2010-04-29 01:25:10 +00:00
|
|
|
g_at_hdlc_set_recording(ppp->hdlc, filename);
|
2010-04-28 20:54:58 +00:00
|
|
|
}
|
|
|
|
|
2010-03-31 23:45:17 +00:00
|
|
|
void g_at_ppp_set_connect_function(GAtPPP *ppp, GAtPPPConnectFunc func,
|
2010-04-01 20:11:11 +00:00
|
|
|
gpointer user_data)
|
2010-03-23 00:05:56 +00:00
|
|
|
{
|
2010-03-31 23:45:17 +00:00
|
|
|
if (func == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ppp->connect_cb = func;
|
2010-03-23 00:05:56 +00:00
|
|
|
ppp->connect_data = user_data;
|
|
|
|
}
|
|
|
|
|
2010-04-30 20:31:26 +00:00
|
|
|
void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtPPPDisconnectFunc func,
|
2010-04-01 20:11:11 +00:00
|
|
|
gpointer user_data)
|
2010-03-23 00:05:56 +00:00
|
|
|
{
|
2010-03-31 23:47:30 +00:00
|
|
|
if (func == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ppp->disconnect_cb = func;
|
2010-03-23 00:05:56 +00:00
|
|
|
ppp->disconnect_data = user_data;
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:11:11 +00:00
|
|
|
void g_at_ppp_set_debug(GAtPPP *ppp, GAtDebugFunc func, gpointer user_data)
|
|
|
|
{
|
|
|
|
if (ppp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ppp->debugf = func;
|
|
|
|
ppp->debug_data = user_data;
|
|
|
|
}
|
|
|
|
|
2011-05-04 15:39:01 +00:00
|
|
|
void g_at_ppp_set_suspend_function(GAtPPP *ppp, GAtSuspendFunc func,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
if (ppp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ppp->suspend_func = func;
|
|
|
|
ppp->suspend_data = user_data;
|
2011-05-25 10:22:58 +00:00
|
|
|
|
|
|
|
if (ppp->hdlc != NULL)
|
|
|
|
g_at_hdlc_set_suspend_function(ppp->hdlc,
|
2011-05-04 15:39:01 +00:00
|
|
|
ppp_proxy_suspend_net_interface, ppp);
|
|
|
|
}
|
|
|
|
|
2010-03-23 00:05:56 +00:00
|
|
|
void g_at_ppp_shutdown(GAtPPP *ppp)
|
|
|
|
{
|
2010-04-30 19:37:17 +00:00
|
|
|
if (ppp->phase == PPP_PHASE_DEAD || ppp->phase == PPP_PHASE_TERMINATION)
|
|
|
|
return;
|
|
|
|
|
2010-04-30 20:31:26 +00:00
|
|
|
ppp->disconnect_reason = G_AT_PPP_REASON_LOCAL_CLOSE;
|
2010-04-13 19:11:44 +00:00
|
|
|
pppcp_signal_close(ppp->lcp);
|
2010-03-23 00:05:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-20 09:38:14 +00:00
|
|
|
static gboolean call_suspend_cb(gpointer user_data)
|
|
|
|
{
|
|
|
|
GAtPPP *ppp = user_data;
|
|
|
|
|
2011-05-22 13:34:08 +00:00
|
|
|
ppp->guard_timeout_source = 0;
|
2011-05-20 09:38:14 +00:00
|
|
|
|
|
|
|
if (ppp->suspend_func)
|
|
|
|
ppp->suspend_func(ppp->suspend_data);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean send_escape_sequence(gpointer user_data)
|
|
|
|
{
|
|
|
|
GAtPPP *ppp = user_data;
|
|
|
|
GAtIO *io = g_at_hdlc_get_io(ppp->hdlc);
|
|
|
|
|
|
|
|
g_at_io_write(io, "+++", 3);
|
2011-05-22 13:34:08 +00:00
|
|
|
ppp->guard_timeout_source = g_timeout_add(GUARD_TIMEOUTS,
|
2011-05-20 09:38:14 +00:00
|
|
|
call_suspend_cb, ppp);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_at_ppp_suspend(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
if (ppp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ppp->suspended = TRUE;
|
|
|
|
ppp_net_suspend_interface(ppp->net);
|
|
|
|
g_at_hdlc_suspend(ppp->hdlc);
|
2011-05-22 13:34:08 +00:00
|
|
|
ppp->guard_timeout_source = g_timeout_add(GUARD_TIMEOUTS,
|
2011-05-20 09:38:14 +00:00
|
|
|
send_escape_sequence, ppp);
|
|
|
|
}
|
|
|
|
|
2011-05-20 09:38:14 +00:00
|
|
|
void g_at_ppp_resume(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
if (ppp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_at_hdlc_get_io(ppp->hdlc) == NULL) {
|
|
|
|
io_disconnect(ppp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ppp->suspended = FALSE;
|
|
|
|
g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc),
|
|
|
|
io_disconnect, ppp);
|
|
|
|
ppp_net_resume_interface(ppp->net);
|
|
|
|
g_at_hdlc_resume(ppp->hdlc);
|
|
|
|
}
|
|
|
|
|
2010-03-23 00:05:56 +00:00
|
|
|
void g_at_ppp_ref(GAtPPP *ppp)
|
|
|
|
{
|
|
|
|
g_atomic_int_inc(&ppp->ref_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_at_ppp_unref(GAtPPP *ppp)
|
|
|
|
{
|
2010-04-09 21:01:30 +00:00
|
|
|
gboolean is_zero;
|
2010-03-25 04:41:54 +00:00
|
|
|
|
2010-07-06 09:57:37 +00:00
|
|
|
if (ppp == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-04-09 21:01:30 +00:00
|
|
|
is_zero = g_atomic_int_dec_and_test(&ppp->ref_count);
|
|
|
|
|
|
|
|
if (is_zero == FALSE)
|
|
|
|
return;
|
2010-04-01 20:47:25 +00:00
|
|
|
|
2011-05-20 09:38:14 +00:00
|
|
|
if (ppp->suspended == FALSE)
|
|
|
|
g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc),
|
|
|
|
NULL, NULL);
|
2010-07-07 01:46:32 +00:00
|
|
|
|
2010-04-28 20:31:59 +00:00
|
|
|
if (ppp->net)
|
|
|
|
ppp_net_free(ppp->net);
|
2011-05-19 09:58:28 +00:00
|
|
|
else if (ppp->fd >= 0)
|
|
|
|
close(ppp->fd);
|
2010-04-09 21:01:30 +00:00
|
|
|
|
2010-04-28 20:31:59 +00:00
|
|
|
if (ppp->chap)
|
|
|
|
ppp_chap_free(ppp->chap);
|
2010-04-09 21:01:30 +00:00
|
|
|
|
|
|
|
lcp_free(ppp->lcp);
|
|
|
|
ipcp_free(ppp->ipcp);
|
2010-04-13 20:14:40 +00:00
|
|
|
|
2011-02-28 20:26:26 +00:00
|
|
|
if (ppp->ppp_dead_source) {
|
|
|
|
g_source_remove(ppp->ppp_dead_source);
|
|
|
|
ppp->ppp_dead_source = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-22 13:34:21 +00:00
|
|
|
if (ppp->guard_timeout_source) {
|
|
|
|
g_source_remove(ppp->guard_timeout_source);
|
|
|
|
ppp->guard_timeout_source = 0;
|
|
|
|
}
|
|
|
|
|
2010-06-11 14:15:32 +00:00
|
|
|
g_at_hdlc_unref(ppp->hdlc);
|
|
|
|
|
2010-04-09 21:01:30 +00:00
|
|
|
g_free(ppp);
|
2010-03-23 00:05:56 +00:00
|
|
|
}
|
|
|
|
|
2010-06-29 16:49:40 +00:00
|
|
|
void g_at_ppp_set_server_info(GAtPPP *ppp, const char *remote,
|
2010-06-28 22:38:54 +00:00
|
|
|
const char *dns1, const char *dns2)
|
2010-06-25 03:19:05 +00:00
|
|
|
{
|
2010-06-28 22:38:54 +00:00
|
|
|
guint32 r = 0;
|
|
|
|
guint32 d1 = 0;
|
|
|
|
guint32 d2 = 0;
|
|
|
|
|
|
|
|
inet_pton(AF_INET, remote, &r);
|
|
|
|
inet_pton(AF_INET, dns1, &d1);
|
|
|
|
inet_pton(AF_INET, dns2, &d2);
|
|
|
|
|
2010-06-29 16:49:40 +00:00
|
|
|
ipcp_set_server_info(ppp->ipcp, r, d1, d2);
|
2010-06-25 03:19:05 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
static GAtPPP *ppp_init_common(gboolean is_server, guint32 ip)
|
2010-03-23 00:05:56 +00:00
|
|
|
{
|
|
|
|
GAtPPP *ppp;
|
|
|
|
|
|
|
|
ppp = g_try_malloc0(sizeof(GAtPPP));
|
2010-11-27 19:39:00 +00:00
|
|
|
if (ppp == NULL)
|
2010-03-23 00:05:56 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ppp->ref_count = 1;
|
2011-05-24 13:08:34 +00:00
|
|
|
ppp->suspended = TRUE;
|
2011-05-19 09:58:28 +00:00
|
|
|
ppp->fd = -1;
|
|
|
|
|
2010-03-23 00:05:56 +00:00
|
|
|
/* set options to defaults */
|
|
|
|
ppp->mru = DEFAULT_MRU;
|
2010-04-21 22:26:30 +00:00
|
|
|
ppp->mtu = DEFAULT_MTU;
|
2010-04-02 18:13:20 +00:00
|
|
|
|
2010-03-23 00:05:56 +00:00
|
|
|
/* initialize the lcp state */
|
2010-06-29 16:53:11 +00:00
|
|
|
ppp->lcp = lcp_new(ppp, is_server);
|
2010-03-23 00:05:56 +00:00
|
|
|
|
2010-04-02 17:42:04 +00:00
|
|
|
/* initialize IPCP state */
|
2010-06-29 16:53:11 +00:00
|
|
|
ppp->ipcp = ipcp_new(ppp, is_server, ip);
|
2010-04-02 17:42:04 +00:00
|
|
|
|
2010-03-23 00:05:56 +00:00
|
|
|
return ppp;
|
|
|
|
}
|
2010-04-30 15:52:16 +00:00
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
GAtPPP *g_at_ppp_new(void)
|
2010-04-30 15:52:16 +00:00
|
|
|
{
|
2011-05-24 13:08:34 +00:00
|
|
|
return ppp_init_common(FALSE, 0);
|
2010-04-30 15:52:16 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
GAtPPP *g_at_ppp_server_new_full(const char *local, int fd)
|
2010-04-30 15:52:16 +00:00
|
|
|
{
|
2010-06-29 16:53:11 +00:00
|
|
|
GAtPPP *ppp;
|
|
|
|
guint32 ip;
|
|
|
|
|
|
|
|
if (local == NULL)
|
|
|
|
ip = 0;
|
|
|
|
else if (inet_pton(AF_INET, local, &ip) != 1)
|
|
|
|
return NULL;
|
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
ppp = ppp_init_common(TRUE, ip);
|
2010-06-29 16:53:11 +00:00
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
if (ppp != NULL)
|
|
|
|
ppp->fd = fd;
|
2010-04-30 15:52:16 +00:00
|
|
|
|
|
|
|
return ppp;
|
|
|
|
}
|
2011-05-19 09:58:28 +00:00
|
|
|
|
2011-05-24 13:08:34 +00:00
|
|
|
GAtPPP *g_at_ppp_server_new(const char *local)
|
2011-05-19 09:58:28 +00:00
|
|
|
{
|
2011-05-24 13:08:34 +00:00
|
|
|
return g_at_ppp_server_new_full(local, -1);
|
2011-05-19 09:58:28 +00:00
|
|
|
}
|