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.1.1.4 2003/10/14 08:09:53 sparq 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");
167 cbcp_recvreq(us
, inp
, len
);
171 dbglog("CBCP_RESP received");
176 dbglog("id doesn't match: expected %d recv %d",
179 cbcp_recvack(us
, inp
, len
);
187 /* protocol was rejected by foe */
188 void cbcp_protrej(int iface
)
192 char *cbcp_codenames
[] = {
193 "Request", "Response", "Ack"
196 char *cbcp_optionnames
[] = {
203 /* pretty print a packet */
205 cbcp_printpkt(p
, plen
, printer
, arg
)
208 void (*printer
) __P((void *, char *, ...));
211 int code
, opt
, id
, len
, olen
, delay
;
214 if (plen
< HEADERLEN
)
220 if (len
< HEADERLEN
|| len
> plen
)
223 if (code
>= 1 && code
<= sizeof(cbcp_codenames
) / sizeof(char *))
224 printer(arg
, " %s", cbcp_codenames
[code
-1]);
226 printer(arg
, " code=0x%x", code
);
228 printer(arg
, " id=0x%x", id
);
239 if (olen
< 2 || olen
> len
) {
246 if (opt
>= 1 && opt
<= sizeof(cbcp_optionnames
) / sizeof(char *))
247 printer(arg
, " %s", cbcp_optionnames
[opt
-1]);
249 printer(arg
, " option=0x%x", opt
);
253 printer(arg
, " delay = %d", delay
);
261 memcpy(str
, p
, olen
- 4);
263 printer(arg
, " number = %s", str
);
273 for (; len
> 0; --len
) {
275 printer(arg
, " %.2x", code
);
281 /* received CBCP request */
283 cbcp_recvreq(us
, pckt
, pcktlen
)
288 u_char type
, opt_len
, delay
, addr_type
;
295 dbglog("length: %d", len
);
298 GETCHAR(opt_len
, pckt
);
301 GETCHAR(delay
, pckt
);
303 us
->us_allowed
|= (1 << type
);
307 dbglog("no callback allowed");
311 dbglog("user callback allowed");
313 GETCHAR(addr_type
, pckt
);
314 memcpy(address
, pckt
, opt_len
- 4);
315 address
[opt_len
- 4] = 0;
317 dbglog("address: %s", address
);
322 dbglog("user admin defined allowed");
343 cb_type
= us
->us_allowed
& us
->us_type
;
344 dbglog("cbcp_resp cb_type=%d", cb_type
);
347 if (cb_type
& ( 1 << CB_CONF_USER
) ) {
348 dbglog("cbcp_resp CONF_USER");
349 PUTCHAR(CB_CONF_USER
, bufp
);
350 len
= 3 + 1 + strlen(us
->us_number
) + 1;
352 PUTCHAR(5, bufp
); /* delay */
354 BCOPY(us
->us_number
, bufp
, strlen(us
->us_number
) + 1);
355 cbcp_send(us
, CBCP_RESP
, buf
, len
);
359 if (cb_type
& ( 1 << CB_CONF_ADMIN
) ) {
360 dbglog("cbcp_resp CONF_ADMIN");
361 PUTCHAR(CB_CONF_ADMIN
, bufp
);
364 PUTCHAR(5, bufp
); /* delay */
365 cbcp_send(us
, CBCP_RESP
, buf
, len
);
369 if (cb_type
& ( 1 << CB_CONF_NO
) ) {
370 dbglog("cbcp_resp CONF_NO");
371 PUTCHAR(CB_CONF_NO
, bufp
);
375 cbcp_send(us
, CBCP_RESP
, buf
, len
);
382 cbcp_send(us
, code
, buf
, len
)
391 outp
= outpacket_buf
;
395 MAKEHEADER(outp
, PPP_CBCP
);
398 PUTCHAR(us
->us_id
, outp
);
399 PUTSHORT(outlen
, outp
);
402 BCOPY(buf
, outp
, len
);
404 output(us
->us_unit
, outpacket_buf
, outlen
+ PPP_HDRLEN
);
408 cbcp_recvack(us
, pckt
, len
)
413 u_char type
, delay
, addr_type
;
419 GETCHAR(opt_len
, pckt
);
422 GETCHAR(delay
, pckt
);
425 GETCHAR(addr_type
, pckt
);
426 memcpy(address
, pckt
, opt_len
- 4);
427 address
[opt_len
- 4] = 0;
429 dbglog("peer will call: %s", address
);
431 if (type
== CB_CONF_NO
)
438 /* ok peer will do callback */
444 lcp_close(0, "Call me back, please");
445 status
= EXIT_CALLBACK
;