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.
21 #define RCSID "$Id: cbcp.c,v 1.11 2001/03/08 05:11:10 paulus Exp $"
25 #include <sys/types.h>
33 static const char rcsid[] = RCSID;
38 static int setcbcp __P((char **));
40 static option_t cbcp_option_list[] = {
41 { "callback", o_special, setcbcp,
42 "Ask for callback", OPT_PRIO | OPT_A2STRVAL, &cbcp[0].us_number },
47 * Protocol entry points.
49 static void cbcp_init __P((int unit));
50 static void cbcp_open __P((int unit));
51 static void cbcp_lowerup __P((int unit));
52 static void cbcp_input __P((int unit, u_char *pkt, int len));
53 static void cbcp_protrej __P((int unit));
54 static int cbcp_printpkt __P((u_char *pkt, int len,
55 void (*printer) __P((void *, char *, ...)),
58 struct protent cbcp_protent = {
78 cbcp_state cbcp[NUM_PPP];
80 /* internal prototypes */
82 static void cbcp_recvreq __P((cbcp_state *us, char *pckt, int len));
83 static void cbcp_resp __P((cbcp_state *us));
84 static void cbcp_up __P((cbcp_state *us));
85 static void cbcp_recvack __P((cbcp_state *us, char *pckt, int len));
86 static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
88 /* option processing */
93 lcp_wantoptions[0].neg_cbcp = 1;
94 cbcp_protent.enabled_flag = 1;
95 cbcp[0].us_number = strdup(*argv);
96 if (cbcp[0].us_number == 0)
97 novm("callback number");
98 cbcp[0].us_type |= (1 << CB_CONF_USER);
99 cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
111 memset(us, 0, sizeof(cbcp_state));
113 us->us_type |= (1 << CB_CONF_NO);
116 /* lower layer is up */
121 cbcp_state *us = &cbcp[iface];
123 dbglog("cbcp_lowerup");
124 dbglog("want: %d", us->us_type);
126 if (us->us_type == CB_CONF_USER)
127 dbglog("phone no: %s", us->us_number);
137 /* process an incomming packet */
139 cbcp_input(unit, inpacket, pktlen)
148 cbcp_state *us = &cbcp[unit];
152 if (pktlen < CBCP_MINLEN) {
153 error("CBCP packet is too small");
163 error("CBCP packet: invalid length");
173 cbcp_recvreq(us, inp, len);
177 dbglog("CBCP_RESP received");
182 dbglog("id doesn't match: expected %d recv %d",
185 cbcp_recvack(us, inp, len);
193 /* protocol was rejected by foe */
194 void cbcp_protrej(int iface)
198 char *cbcp_codenames[] = {
199 "Request", "Response", "Ack"
202 char *cbcp_optionnames[] = {
209 /* pretty print a packet */
211 cbcp_printpkt(p, plen, printer, arg)
214 void (*printer) __P((void *, char *, ...));
217 int code, opt, id, len, olen, delay;
220 if (plen < HEADERLEN)
226 if (len < HEADERLEN || len > plen)
229 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
230 printer(arg, " %s", cbcp_codenames[code-1]);
232 printer(arg, " code=0x%x", code);
234 printer(arg, " id=0x%x", id);
245 if (olen < 2 || olen > len) {
252 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
253 printer(arg, " %s", cbcp_optionnames[opt-1]);
255 printer(arg, " option=0x%x", opt);
259 printer(arg, " delay = %d", delay);
267 memcpy(str, p, olen - 4);
269 printer(arg, " number = %s", str);
279 for (; len > 0; --len) {
281 printer(arg, " %.2x", code);
287 /* received CBCP request */
289 cbcp_recvreq(us, pckt, pcktlen)
294 u_char type, opt_len, delay, addr_type;
301 dbglog("length: %d", len);
304 GETCHAR(opt_len, pckt);
307 GETCHAR(delay, pckt);
309 us->us_allowed |= (1 << type);
313 dbglog("no callback allowed");
317 dbglog("user callback allowed");
319 GETCHAR(addr_type, pckt);
320 memcpy(address, pckt, opt_len - 4);
321 address[opt_len - 4] = 0;
323 dbglog("address: %s", address);
328 dbglog("user admin defined allowed");
349 cb_type = us->us_allowed & us->us_type;
350 dbglog("cbcp_resp cb_type=%d", cb_type);
354 lcp_down(us->us_unit);
357 if (cb_type & ( 1 << CB_CONF_USER ) ) {
358 dbglog("cbcp_resp CONF_USER");
359 PUTCHAR(CB_CONF_USER, bufp);
360 len = 3 + 1 + strlen(us->us_number) + 1;
362 PUTCHAR(5, bufp); /* delay */
364 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
365 cbcp_send(us, CBCP_RESP, buf, len);
369 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
370 dbglog("cbcp_resp CONF_ADMIN");
371 PUTCHAR(CB_CONF_ADMIN, bufp);
374 PUTCHAR(5, bufp); /* delay */
375 cbcp_send(us, CBCP_RESP, buf, len);
379 if (cb_type & ( 1 << CB_CONF_NO ) ) {
380 dbglog("cbcp_resp CONF_NO");
381 PUTCHAR(CB_CONF_NO, bufp);
385 cbcp_send(us, CBCP_RESP, buf, len);
392 cbcp_send(us, code, buf, len)
401 outp = outpacket_buf;
405 MAKEHEADER(outp, PPP_CBCP);
408 PUTCHAR(us->us_id, outp);
409 PUTSHORT(outlen, outp);
412 BCOPY(buf, outp, len);
414 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
418 cbcp_recvack(us, pckt, len)
423 u_char type, delay, addr_type;
429 GETCHAR(opt_len, pckt);
432 GETCHAR(delay, pckt);
435 GETCHAR(addr_type, pckt);
436 memcpy(address, pckt, opt_len - 4);
437 address[opt_len - 4] = 0;
439 dbglog("peer will call: %s", address);
441 if (type == CB_CONF_NO)
448 /* ok peer will do callback */
454 lcp_close(0, "Call me back, please");
455 status = EXIT_CALLBACK;