summaryrefslogtreecommitdiffstats
path: root/gatchat/ppp.h
blob: ba17f41cc02ec8b8475fa8311fd05afd1b14851c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 *
 *  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
 *
 */

#include "ppp_cp.h"

#define DEFAULT_MRU	1500
#define BUFFERSZ	DEFAULT_MRU*2
#define DEFAULT_ACCM	0x00000000
#define PPP_ESC		0x7d
#define PPP_FLAG_SEQ 	0x7e
#define PPP_ADDR_FIELD	0xff
#define PPP_CTRL	0x03
#define LCP_PROTOCOL	0xc021
#define CHAP_PROTOCOL	0xc223
#define IPCP_PROTO	0x8021
#define PPP_HEADROOM	2
#define HDLC_HEADROOM	3
#define HDLC_TAIL	3
#define MD5		5

enum ppp_phase {
	PPP_DEAD = 0,
	PPP_ESTABLISHMENT,
	PPP_AUTHENTICATION,
	PPP_NETWORK,
	PPP_TERMINATION,
};

enum ppp_event {
	PPP_UP = 1,
	PPP_OPENED,
	PPP_SUCCESS,
	PPP_NONE,
	PPP_CLOSING,
	PPP_FAIL,
	PPP_DOWN
};

struct ppp_packet_handler {
	guint16 proto;
	void (*handler)(gpointer priv, guint8 *packet);
	gpointer priv;
};

struct ppp_header {
	guint16 proto;
	guint8 info[0];
} __attribute__((packed));

struct packed_short {
	guint16 s;
} __attribute__((packed));

struct packed_long {
	guint32 l;
} __attribute__((packed));

static inline guint32 __get_unaligned_long(const gpointer p)
{
	const struct packed_long *ptr = p;
	return ptr->l;
}

static inline guint16 __get_unaligned_short(const gpointer p)
{
	const struct packed_short *ptr = p;
	return ptr->s;
}

#define get_host_long(p) \
	(ntohl(__get_unaligned_long(p)))

#define get_host_short(p) \
	(ntohs(__get_unaligned_short(p)))

#define ppp_info(packet) \
	(packet + 4)

#define ppp_proto(packet) \
	(get_host_short(packet + 2))

struct auth_data {
	guint16 proto;
	gpointer proto_data;
	void (*process_packet)(struct auth_data *data, guint8 *packet);
	char *username;
	char *password;
	GAtPPP *ppp;
};

struct ppp_net_data {
	GAtPPP *ppp;
	char *if_name;
	GIOChannel *channel;
	struct pppcp_data *ipcp;
};

struct _GAtPPP {
	gint ref_count;
	enum ppp_phase phase;
	struct pppcp_data *lcp;
	struct auth_data *auth;
	struct ppp_net_data *net;
	guint8 buffer[BUFFERSZ];
	int index;
	gint mru;
	guint16 auth_proto;
	char user_name[256];
	char passwd[256];
	gboolean pfc;
	gboolean acfc;
	guint32 xmit_accm[8];
	guint32 recv_accm;
	GIOChannel *modem;
	GAtPPPConnectFunc connect_cb;
	gpointer connect_data;
	GAtDisconnectFunc disconnect_cb;
	gpointer disconnect_data;
	gint modem_watch;
	GAtDebugFunc debugf;
	gpointer debug_data;
	int record_fd;
};

void ppp_generate_event(GAtPPP *ppp, enum ppp_event event);
void ppp_register_packet_handler(struct ppp_packet_handler *handler);
void ppp_transmit(GAtPPP *ppp, guint8 *packet, guint infolen);
void ppp_set_auth(GAtPPP *ppp, guint8 *auth_data);
void ppp_set_recv_accm(GAtPPP *ppp, guint32 accm);
guint32 ppp_get_xmit_accm(GAtPPP *ppp);
void ppp_set_pfc(GAtPPP *ppp, gboolean pfc);
gboolean ppp_get_pfc(GAtPPP *ppp);
void ppp_set_acfc(GAtPPP *ppp, gboolean acfc);
gboolean ppp_get_acfc(GAtPPP *ppp);
struct pppcp_data * lcp_new(GAtPPP *ppp);
void lcp_free(struct pppcp_data *lcp);
void lcp_open(struct pppcp_data *data);
void lcp_close(struct pppcp_data *data);
void lcp_establish(struct pppcp_data *data);
void lcp_terminate(struct pppcp_data *data);
void lcp_protocol_reject(struct pppcp_data *lcp, guint8 *packet, gsize len);
void auth_set_credentials(struct auth_data *data, const char *username,
				const char *passwd);
void auth_set_proto(struct auth_data *data, guint16 proto, guint8 method);
struct auth_data *auth_new(GAtPPP *ppp);
void auth_free(struct auth_data *auth);
struct ppp_net_data *ppp_net_new(GAtPPP *ppp);
void ppp_net_open(struct ppp_net_data *data);
void ppp_net_free(struct ppp_net_data *data);
void ppp_net_close(struct ppp_net_data *data);