Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / rp-l2tp / debug.c
blobdd7a45e833fd9a560628956ebad76be24cefc02b
1 /***********************************************************************
3 * debug.c
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.
12 * LIC: GPL
14 ***********************************************************************/
16 static char const RCSID[] =
17 "$Id: debug.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
19 #include "l2tp.h"
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <sys/time.h>
23 #include <unistd.h>
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
31 to db */
32 static struct timeval big_bang = {0, 0};
34 /**********************************************************************
35 * %FUNCTION: debug_avp_type_to_str
36 * %ARGUMENTS:
37 * type -- an AVP type
38 * %RETURNS:
39 * A string representation of AVP type
40 ***********************************************************************/
41 char const *
42 l2tp_debug_avp_type_to_str(uint16_t type)
44 static char buf[64];
45 switch(type) {
46 AVPCASE(MESSAGE_TYPE);
47 AVPCASE(RESULT_CODE);
48 AVPCASE(PROTOCOL_VERSION);
49 AVPCASE(FRAMING_CAPABILITIES);
50 AVPCASE(BEARER_CAPABILITIES);
51 AVPCASE(TIE_BREAKER);
52 AVPCASE(FIRMWARE_REVISION);
53 AVPCASE(HOST_NAME);
54 AVPCASE(VENDOR_NAME);
55 AVPCASE(ASSIGNED_TUNNEL_ID);
56 AVPCASE(RECEIVE_WINDOW_SIZE);
57 AVPCASE(CHALLENGE);
58 AVPCASE(Q931_CAUSE_CODE);
59 AVPCASE(CHALLENGE_RESPONSE);
60 AVPCASE(ASSIGNED_SESSION_ID);
61 AVPCASE(CALL_SERIAL_NUMBER);
62 AVPCASE(MINIMUM_BPS);
63 AVPCASE(MAXIMUM_BPS);
64 AVPCASE(BEARER_TYPE);
65 AVPCASE(FRAMING_TYPE);
66 AVPCASE(CALLED_NUMBER);
67 AVPCASE(CALLING_NUMBER);
68 AVPCASE(SUB_ADDRESS);
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);
79 AVPCASE(CALL_ERRORS);
80 AVPCASE(ACCM);
81 AVPCASE(RANDOM_VECTOR);
82 AVPCASE(PRIVATE_GROUP_ID);
83 AVPCASE(RX_CONNECT_SPEED);
84 AVPCASE(SEQUENCING_REQUIRED);
86 /* Unknown */
87 sprintf(buf, "Unknown_AVP#%d", (int) type);
88 return buf;
91 /**********************************************************************
92 * %FUNCTION: debug_message_type_to_str
93 * %ARGUMENTS:
94 * type -- an MESSAGE type
95 * %RETURNS:
96 * A string representation of message type
97 ***********************************************************************/
98 char const *
99 l2tp_debug_message_type_to_str(uint16_t type)
101 static char buf[64];
102 switch(type) {
103 MSGCASE(SCCRQ);
104 MSGCASE(SCCRP);
105 MSGCASE(SCCCN);
106 MSGCASE(StopCCN);
107 MSGCASE(HELLO);
108 MSGCASE(OCRQ);
109 MSGCASE(OCRP);
110 MSGCASE(OCCN);
111 MSGCASE(ICRQ);
112 MSGCASE(ICRP);
113 MSGCASE(ICCN);
114 MSGCASE(CDN);
115 MSGCASE(WEN);
116 MSGCASE(SLI);
117 MSGCASE(ZLB);
119 sprintf(buf, "Unknown_Message#%d", (int) type);
120 return buf;
123 /**********************************************************************
124 * %FUNCTION: debug_tunnel_to_str
125 * %ARGUMENTS:
126 * tunnel
127 * %RETURNS:
128 * A string representation of tunnel (my_id/assigned_id)
129 ***********************************************************************/
130 char const *
131 l2tp_debug_tunnel_to_str(l2tp_tunnel *tunnel)
133 static char buf[64];
134 sprintf(buf, "%d/%d", (int) tunnel->my_id, (int) tunnel->assigned_id);
135 return buf;
138 /**********************************************************************
139 * %FUNCTION: debug_session_to_str
140 * %ARGUMENTS:
141 * session
142 * %RETURNS:
143 * A string representation of session (my_id/assigned_id)
144 ***********************************************************************/
145 char const *
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);
153 return buf;
156 /**********************************************************************
157 * %FUNCTION: db
158 * %ARGUMENTS:
159 * what -- which facet we are debugging
160 * fmt -- printf-style format
161 * args -- arguments
162 * %RETURNS:
163 * Nothing
164 * %DESCRIPTION:
165 * If bit in debug mask for "what" is set, print debugging message.
166 ***********************************************************************/
167 void
168 l2tp_db(int what, char const *fmt, ...)
170 va_list ap;
171 struct timeval now;
172 long sec_diff, usec_diff;
174 if (!(debug_mask & what)) return;
176 gettimeofday(&now, NULL);
178 if (!big_bang.tv_sec) {
179 big_bang = now;
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;
185 if (usec_diff < 0) {
186 usec_diff += 1000000;
187 sec_diff--;
190 /* Convert to seconds.milliseconds */
191 usec_diff /= 1000;
193 va_start(ap, fmt);
194 fprintf(stderr, "%4ld.%03ld ", sec_diff, usec_diff);
195 vfprintf(stderr, fmt, ap);
196 va_end(ap);
199 /**********************************************************************
200 * %FUNCTION: debug_set_bitmask
201 * %ARGUMENTS:
202 * mask -- what to set debug bitmask to
203 * %RETURNS:
204 * Nothing
205 * %DESCRIPTION:
206 * Sets debug bitmask
207 ***********************************************************************/
208 void
209 l2tp_debug_set_bitmask(unsigned long mask)
211 debug_mask = mask;
214 /**********************************************************************
215 * %FUNCTION: debug_describe_dgram
216 * %ARGUMENTS:
217 * dgram -- an L2TP datagram
218 * %RETURNS:
219 * A string describing the datagram
220 ***********************************************************************/
221 char const *
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);
232 } else {
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);
237 return buf;