2 * cbcp - Call Back Configuration Protocol.
4 * Copyright (c) 1995 Pedro Roque Marques
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Pedro Roque Marques. The name of the author may not be used to
13 * endorse or promote products derived from this software without
14 * specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: cbcp.c,v 1.7 1999/05/12 06:19:46 paulus Exp $";
27 #include <sys/types.h>
39 static int setcbcp __P((char **));
41 static option_t cbcp_option_list[] = {
42 { "callback", o_special, setcbcp,
48 * Protocol entry points.
50 static void cbcp_init __P((int unit));
51 static void cbcp_open __P((int unit));
52 static void cbcp_lowerup __P((int unit));
53 static void cbcp_input __P((int unit, u_char *pkt, int len));
54 static void cbcp_protrej __P((int unit));
55 static int cbcp_printpkt __P((u_char *pkt, int len,
56 void (*printer) __P((void *, char *, ...)),
59 struct protent cbcp_protent = {
79 cbcp_state cbcp[NUM_PPP];
81 /* internal prototypes */
83 static void cbcp_recvreq __P((cbcp_state *us, char *pckt, int len));
84 static void cbcp_resp __P((cbcp_state *us));
85 static void cbcp_up __P((cbcp_state *us));
86 static void cbcp_recvack __P((cbcp_state *us, char *pckt, int len));
87 static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
89 /* option processing */
94 lcp_wantoptions[0].neg_cbcp = 1;
95 cbcp_protent.enabled_flag = 1;
96 cbcp[0].us_number = strdup(*argv);
97 if (cbcp[0].us_number == 0)
98 novm("callback number");
99 cbcp[0].us_type |= (1 << CB_CONF_USER);
100 cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
112 memset(us, 0, sizeof(cbcp_state));
114 us->us_type |= (1 << CB_CONF_NO);
117 /* lower layer is up */
122 cbcp_state *us = &cbcp[iface];
124 dbglog("cbcp_lowerup");
125 dbglog("want: %d", us->us_type);
127 if (us->us_type == CB_CONF_USER)
128 dbglog("phone no: %s", us->us_number);
138 /* process an incomming packet */
140 cbcp_input(unit, inpacket, pktlen)
149 cbcp_state *us = &cbcp[unit];
153 if (pktlen < CBCP_MINLEN) {
154 error("CBCP packet is too small");
164 error("CBCP packet: invalid length");
174 cbcp_recvreq(us, inp, len);
178 dbglog("CBCP_RESP received");
183 dbglog("id doesn't match: expected %d recv %d",
186 cbcp_recvack(us, inp, len);
194 /* protocol was rejected by foe */
195 void cbcp_protrej(int iface)
199 char *cbcp_codenames[] = {
200 "Request", "Response", "Ack"
203 char *cbcp_optionnames[] = {
210 /* pretty print a packet */
212 cbcp_printpkt(p, plen, printer, arg)
215 void (*printer) __P((void *, char *, ...));
218 int code, opt, id, len, olen, delay;
221 if (plen < HEADERLEN)
227 if (len < HEADERLEN || len > plen)
230 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
231 printer(arg, " %s", cbcp_codenames[code-1]);
233 printer(arg, " code=0x%x", code);
235 printer(arg, " id=0x%x", id);
246 if (olen < 2 || olen > len) {
253 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
254 printer(arg, " %s", cbcp_optionnames[opt-1]);
256 printer(arg, " option=0x%x", opt);
260 printer(arg, " delay = %d", delay);
268 memcpy(str, p, olen - 4);
270 printer(arg, " number = %s", str);
280 for (; len > 0; --len) {
282 printer(arg, " %.2x", code);
288 /* received CBCP request */
290 cbcp_recvreq(us, pckt, pcktlen)
295 u_char type, opt_len, delay, addr_type;
302 dbglog("length: %d", len);
305 GETCHAR(opt_len, pckt);
308 GETCHAR(delay, pckt);
310 us->us_allowed |= (1 << type);
314 dbglog("no callback allowed");
318 dbglog("user callback allowed");
320 GETCHAR(addr_type, pckt);
321 memcpy(address, pckt, opt_len - 4);
322 address[opt_len - 4] = 0;
324 dbglog("address: %s", address);
329 dbglog("user admin defined allowed");
350 cb_type = us->us_allowed & us->us_type;
351 dbglog("cbcp_resp cb_type=%d", cb_type);
355 lcp_down(us->us_unit);
358 if (cb_type & ( 1 << CB_CONF_USER ) ) {
359 dbglog("cbcp_resp CONF_USER");
360 PUTCHAR(CB_CONF_USER, bufp);
361 len = 3 + 1 + strlen(us->us_number) + 1;
363 PUTCHAR(5, bufp); /* delay */
365 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
366 cbcp_send(us, CBCP_RESP, buf, len);
370 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
371 dbglog("cbcp_resp CONF_ADMIN");
372 PUTCHAR(CB_CONF_ADMIN, bufp);
375 PUTCHAR(5, bufp); /* delay */
376 cbcp_send(us, CBCP_RESP, buf, len);
380 if (cb_type & ( 1 << CB_CONF_NO ) ) {
381 dbglog("cbcp_resp CONF_NO");
382 PUTCHAR(CB_CONF_NO, bufp);
386 cbcp_send(us, CBCP_RESP, buf, len);
387 (*ipcp_protent.open)(us->us_unit);
393 cbcp_send(us, code, buf, len)
402 outp = outpacket_buf;
406 MAKEHEADER(outp, PPP_CBCP);
409 PUTCHAR(us->us_id, outp);
410 PUTSHORT(outlen, outp);
413 BCOPY(buf, outp, len);
415 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
419 cbcp_recvack(us, pckt, len)
424 u_char type, delay, addr_type;
430 GETCHAR(opt_len, pckt);
433 GETCHAR(delay, pckt);
436 GETCHAR(addr_type, pckt);
437 memcpy(address, pckt, opt_len - 4);
438 address[opt_len - 4] = 0;
440 dbglog("peer will call: %s", address);
442 if (type == CB_CONF_NO)
449 /* ok peer will do callback */
455 lcp_close(0, "Call me back, please");
456 status = EXIT_CALLBACK;