summaryrefslogtreecommitdiff
path: root/ppp/dahdi.c
blob: 4e623556ffb3bd041b85c09ce52969607c639d78 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* dahdi.c - pppd plugin to implement PPP over DAHDI HDLC channel.
 *
 * Copyright 2002  Digium, Inc. 
 *		   Mark Spencer <markster@digium.inc>
 *
 * Borrows from PPPoE by Michal Ostrowski <mostrows@styx.uwaterloo.ca>,
 *		  Jamal Hadi Salim <hadi@cyberus.ca>
 *
 * which in turn...
 *
 * Borrows heavily from the PPPoATM plugin by Mitchell Blank Jr.,
 * which is based in part on work from Jens Axboe and Paul Mackerras.
 *
 */

/*
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2 as published by the
 * Free Software Foundation. See the LICENSE file included with
 * this program for more details.
 */

#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <pppd/pppd.h>
#include <pppd/fsm.h>
#include <pppd/lcp.h>
#include <pppd/ipcp.h>
#include <pppd/ccp.h>
#include <pppd/pathnames.h>

#include <dahdi/user.h>

extern int new_style_driver;

const char pppd_version[] = VERSION;

#define _PATH_DAHDI_OPT         _ROOT_PATH "/etc/ppp/options."

#define DAHDI_MTU	(DAHDI_DEFAULT_MTU_MRU - 16)
extern int kill_link;
int     retries = 0;

int setdevname_dahdi(const char *cp);

static option_t dahdi_options[] = {
	{ "device name", o_wild, (void *) &setdevname_dahdi,
	  "Serial port device name",
	  OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
	  devnam},
	{ NULL }
};

static int dahdi_fd = -1;
static int dahdi_chan = 0;

static int connect_dahdi(void)
{
    
    struct dahdi_params dahdi_params;
    int res;
    int x;

    info("DAHDI device is '%s'\n", devnam);

    strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));

    if (strlen(devnam) && strcmp(devnam, "stdin")) {
	/* Get the channel number */
	dahdi_chan = atoi(devnam);
	if (dahdi_chan < 1) {
		fatal("'%s' is not a valid device name\n", devnam);
		return -1;
	}

	/* Open /dev/dahdi/channel interface */
	dahdi_fd = open("/dev/dahdi/channel", O_RDWR);
	if (dahdi_fd < 0) {
		fatal("Unable to open DAHDI channel interface: '%s'\n", strerror(errno));
		return dahdi_fd;
	}

	/* Specify which channel we really want */
	x = dahdi_chan;
	res = ioctl(dahdi_fd, DAHDI_SPECIFY, &x);
	if (res) {
		fatal("Unable to specify channel %d: %s\n", dahdi_chan, strerror(errno));
		close(dahdi_fd);
		dahdi_fd = -1;
		return -1;
	}
    } else
        dahdi_fd = STDIN_FILENO;


    /* Get channel parameters */
    memset(&dahdi_params, 0, sizeof(dahdi_params));
    dahdi_params.channo = -1;

    res = ioctl(dahdi_fd, DAHDI_GET_PARAMS, &dahdi_params);

    if (res) {
	fatal("Device '%s' does not appear to be a DAHDI device\n", devnam ? devnam : "<stdin>");
    }

    x = 1;

    /* Throw into HDLC/PPP mode */
    res = ioctl(dahdi_fd, DAHDI_HDLCPPP, &x);

    if (res) {
	fatal("Unable to put device '%s' into HDLC mode\n", devnam);
	close(dahdi_fd);
	dahdi_fd = -1;
	return -1;
    }

    /* Once the logging is fixed, print a message here indicating
       connection parameters */
    dahdi_chan = dahdi_params.channo;
    info("Connected to DAHDI device '%s' (%d)\n", dahdi_params.name, dahdi_params.channo);

    return dahdi_fd;
}

static void disconnect_dahdi(void)
{
    int res;
    int x = 0;
    /* Throw out of HDLC mode */
    res = ioctl(dahdi_fd, DAHDI_HDLCPPP, &x);

    if (res) {
	warn("Unable to take device '%s' out of HDLC mode\n", devnam);
    }

    /* Close if it's not stdin */
    if (strlen(devnam))
	close(dahdi_fd);
    warn("Disconnect from DAHDI");

}


static int setspeed_dahdi(const char *cp)
{
    return 0;
}

static void dahdi_extra_options()
{
    int ret;
    char buf[256];
    snprintf(buf, 256, _PATH_DAHDI_OPT "%s",devnam);
    if(!options_from_file(buf, 0, 0, 1))
	exit(EXIT_OPTION_ERROR);

}



static void send_config_dahdi(int mtu,
			      u_int32_t asyncmap,
			      int pcomp,
			      int accomp)
{
    int sock;

    if (mtu > DAHDI_MTU) {
	warn("Couldn't increase MTU to %d.", mtu);
	mtu = DAHDI_MTU;
    }
}


static void recv_config_dahdi(int mru,
			      u_int32_t asyncmap,
			      int pcomp,
			      int accomp)
{
    if (mru > DAHDI_MTU)
	error("Couldn't increase MRU to %d", mru);
}

static void set_xaccm_pppoe(int unit, ext_accm accm)
{
    /* NOTHING */
}



struct channel dahdi_channel;

/* Check is cp is a valid DAHDI device
 * return either 1 if "cp" is a reasonable thing to name a device
 * or die.
 * Note that we don't actually open the device at this point
 * We do need to fill in:
 *   devnam: a string representation of the device
 */

int (*old_setdevname_hook)(const char* cp) = NULL;
int setdevname_dahdi(const char *cp)
{
    int ret;
    int chan;

    /* If already set, forgoe */
    if (strlen(devnam))
	return 1;


    if (strcmp(cp, "stdin")) {
	ret = sscanf(cp, "%d", &chan);
	if (ret != 1) {
		fatal("DAHDI: Invalid channel: '%s'\n", cp);
		return -1;
	}
    }

    dahdi_copy_string(devnam, cp, sizeof(devnam));

    info("Using DAHDI device '%s'\n", devnam);

    ret = 1;

    if( ret == 1 && the_channel != &dahdi_channel ){

	the_channel = &dahdi_channel;

	modem = 0;

	lcp_allowoptions[0].neg_accompression = 0;
	lcp_wantoptions[0].neg_accompression = 0;

	lcp_allowoptions[0].neg_pcompression = 0;
	lcp_wantoptions[0].neg_pcompression = 0;

	ccp_allowoptions[0].deflate = 0 ;
	ccp_wantoptions[0].deflate = 0 ;

	ipcp_allowoptions[0].neg_vj=0;
	ipcp_wantoptions[0].neg_vj=0;

	ccp_allowoptions[0].bsd_compress = 0;
	ccp_wantoptions[0].bsd_compress = 0;

	lcp_allowoptions[0].neg_asyncmap = 0;
	lcp_wantoptions[0].neg_asyncmap = 0;

    }
    return ret;
}



void plugin_init(void)
{
    if (!ppp_available() && !new_style_driver)
	fatal("Kernel doesn't support ppp_generic needed for DAHDI PPP");
    add_options(dahdi_options);

    info("DAHDI Plugin Initialized");
}

struct channel dahdi_channel = {
    options: dahdi_options,
    process_extra_options: &dahdi_extra_options,
    check_options: NULL,
    connect: &connect_dahdi,
    disconnect: &disconnect_dahdi,
    establish_ppp: &generic_establish_ppp,
    disestablish_ppp: &generic_disestablish_ppp,
    send_config: &send_config_dahdi,
    recv_config: &recv_config_dahdi,
    close: NULL,
    cleanup: NULL
};