1 /***********************************************************************
5 * Debugging routines for L2TP
7 * Copyright (C) 2002 by Roaring Penguin Software Inc.
9 * This software may be distributed under the terms of the GNU General
10 * Public License, Version 2, or (at your option) any later version.
14 ***********************************************************************/
16 static char const RCSID
[] =
17 "$Id: debug.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
25 #define AVPCASE(x) case AVP_ ## x: return #x
26 #define MSGCASE(x) case MESSAGE_ ## x: return #x
28 static unsigned long debug_mask
= 0;
30 /* Big bang is when the universe started... set by first call
32 static struct timeval big_bang
= {0, 0};
34 /**********************************************************************
35 * %FUNCTION: debug_avp_type_to_str
39 * A string representation of AVP type
40 ***********************************************************************/
42 l2tp_debug_avp_type_to_str(uint16_t type
)
46 AVPCASE(MESSAGE_TYPE
);
48 AVPCASE(PROTOCOL_VERSION
);
49 AVPCASE(FRAMING_CAPABILITIES
);
50 AVPCASE(BEARER_CAPABILITIES
);
52 AVPCASE(FIRMWARE_REVISION
);
55 AVPCASE(ASSIGNED_TUNNEL_ID
);
56 AVPCASE(RECEIVE_WINDOW_SIZE
);
58 AVPCASE(Q931_CAUSE_CODE
);
59 AVPCASE(CHALLENGE_RESPONSE
);
60 AVPCASE(ASSIGNED_SESSION_ID
);
61 AVPCASE(CALL_SERIAL_NUMBER
);
65 AVPCASE(FRAMING_TYPE
);
66 AVPCASE(CALLED_NUMBER
);
67 AVPCASE(CALLING_NUMBER
);
69 AVPCASE(TX_CONNECT_SPEED
);
70 AVPCASE(PHYSICAL_CHANNEL_ID
);
71 AVPCASE(INITIAL_RECEIVED_CONFREQ
);
72 AVPCASE(LAST_SENT_CONFREQ
);
73 AVPCASE(LAST_RECEIVED_CONFREQ
);
74 AVPCASE(PROXY_AUTHEN_TYPE
);
75 AVPCASE(PROXY_AUTHEN_NAME
);
76 AVPCASE(PROXY_AUTHEN_CHALLENGE
);
77 AVPCASE(PROXY_AUTHEN_ID
);
78 AVPCASE(PROXY_AUTHEN_RESPONSE
);
81 AVPCASE(RANDOM_VECTOR
);
82 AVPCASE(PRIVATE_GROUP_ID
);
83 AVPCASE(RX_CONNECT_SPEED
);
84 AVPCASE(SEQUENCING_REQUIRED
);
87 sprintf(buf
, "Unknown_AVP#%d", (int) type
);
91 /**********************************************************************
92 * %FUNCTION: debug_message_type_to_str
94 * type -- an MESSAGE type
96 * A string representation of message type
97 ***********************************************************************/
99 l2tp_debug_message_type_to_str(uint16_t type
)
119 sprintf(buf
, "Unknown_Message#%d", (int) type
);
123 /**********************************************************************
124 * %FUNCTION: debug_tunnel_to_str
128 * A string representation of tunnel (my_id/assigned_id)
129 ***********************************************************************/
131 l2tp_debug_tunnel_to_str(l2tp_tunnel
*tunnel
)
134 sprintf(buf
, "%d/%d", (int) tunnel
->my_id
, (int) tunnel
->assigned_id
);
138 /**********************************************************************
139 * %FUNCTION: debug_session_to_str
143 * A string representation of session (my_id/assigned_id)
144 ***********************************************************************/
146 l2tp_debug_session_to_str(l2tp_session
*session
)
148 static char buf
[128];
149 sprintf(buf
, "(%d/%d, %d/%d)",
150 (int) session
->tunnel
->my_id
,
151 (int) session
->tunnel
->assigned_id
,
152 (int) session
->my_id
, (int) session
->assigned_id
);
156 /**********************************************************************
159 * what -- which facet we are debugging
160 * fmt -- printf-style format
165 * If bit in debug mask for "what" is set, print debugging message.
166 ***********************************************************************/
168 l2tp_db(int what
, char const *fmt
, ...)
172 long sec_diff
, usec_diff
;
174 if (!(debug_mask
& what
)) return;
176 gettimeofday(&now
, NULL
);
178 if (!big_bang
.tv_sec
) {
182 /* Compute difference between now and big_bang */
183 sec_diff
= now
.tv_sec
- big_bang
.tv_sec
;
184 usec_diff
= now
.tv_usec
- big_bang
.tv_usec
;
186 usec_diff
+= 1000000;
190 /* Convert to seconds.milliseconds */
194 fprintf(stderr
, "%4ld.%03ld ", sec_diff
, usec_diff
);
195 vfprintf(stderr
, fmt
, ap
);
199 /**********************************************************************
200 * %FUNCTION: debug_set_bitmask
202 * mask -- what to set debug bitmask to
207 ***********************************************************************/
209 l2tp_debug_set_bitmask(unsigned long mask
)
214 /**********************************************************************
215 * %FUNCTION: debug_describe_dgram
217 * dgram -- an L2TP datagram
219 * A string describing the datagram
220 ***********************************************************************/
222 l2tp_debug_describe_dgram(l2tp_dgram
const *dgram
)
224 static char buf
[256];
226 if (dgram
->bits
& TYPE_BIT
) {
227 /* Control datagram */
228 snprintf(buf
, sizeof(buf
), "type=%s, tid=%d, sid=%d, Nr=%d, Ns=%d",
229 l2tp_debug_message_type_to_str(dgram
->msg_type
),
230 (int) dgram
->tid
, (int) dgram
->sid
,
231 (int) dgram
->Nr
, (int) dgram
->Ns
);
233 snprintf(buf
, sizeof(buf
), "data message tid=%d sid=%d payload_len=%d",
234 (int) dgram
->tid
, (int) dgram
->sid
,
235 (int) dgram
->payload_len
);