Squelch an MSVC++ complaint.
[obnox/wireshark/wip.git] / packet-eap.c
1 /* packet-eap.c
2  * Routines for EAP Extensible Authentication Protocol header disassembly
3  *
4  * $Id: packet-eap.c,v 1.1 2001/11/06 20:30:39 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36
37 #include <glib.h>
38 #include "packet.h"
39 #include "packet-ieee8023.h"
40 #include "packet-ipx.h"
41 #include "packet-llc.h"
42 #include "etypes.h"
43 #include "ppptypes.h"
44
45 static int proto_eap = -1;
46 static int hf_eap_code = -1;
47 static int hf_eap_identifier = -1;
48 static int hf_eap_len = -1;
49 static int hf_eap_type = -1;
50
51 static gint ett_eap = -1;
52
53 typedef struct _e_eap {
54     guint8 eap_code;
55     guint8 eap_id;
56     guint16 eap_len;
57 } e_eap;
58
59 static const char *eap_code_name[] = { 
60     "Undefined",
61     "Request",
62     "Response",
63     "Success",
64     "Failure",
65 };
66 #define EAP_CODE_COUNT (sizeof(eap_code_name)/sizeof(eap_code_name[0]))
67
68 static const char *eap_type_name[] = { 
69     "Undefined",
70     "Identity",
71     "Nak (Response only)",
72     "MD5-Challenge",
73     "One-Time Password",
74     "Generic Token Card",
75 };
76 #define EAP_TYPE_COUNT (sizeof(eap_type_name)/sizeof(eap_type_name[0]))
77
78
79 void
80 dissect_eap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
81 {
82   e_eap       eaph;
83   guint       len;
84   proto_tree *ti;
85   proto_tree *volatile eap_tree;
86
87   if (check_col(pinfo->fd, COL_PROTOCOL))
88     col_set_str(pinfo->fd, COL_PROTOCOL, "EAP");
89   if (check_col(pinfo->fd, COL_INFO))
90     col_clear(pinfo->fd, COL_INFO);
91
92   tvb_memcpy(tvb, (guint8 *)&eaph, 0, sizeof(eaph));
93   eaph.eap_len = ntohs(eaph.eap_len);
94
95   len = eaph.eap_len;
96
97   set_actual_length(tvb, pinfo, len);
98
99   eap_tree = NULL;
100
101   if (tree) {
102     ti = proto_tree_add_item(tree, proto_eap, tvb, 0, len, FALSE);
103     eap_tree = proto_item_add_subtree(ti, ett_eap);
104
105     proto_tree_add_text(eap_tree, tvb, 0, 0, "Code: %s (%u) ", 
106                         eaph.eap_code > EAP_CODE_COUNT?
107                         "Unknown": eap_code_name[eaph.eap_code],
108                         eaph.eap_code);
109
110     proto_tree_add_uint(eap_tree, hf_eap_identifier, tvb, 1, 1, eaph.eap_id);
111     proto_tree_add_uint(eap_tree, hf_eap_len,    tvb, 2, 2, eaph.eap_len);
112
113     if (len > 4) {
114         guint8 eap_type = tvb_get_guint8(tvb, 4);
115         proto_tree_add_text(eap_tree, tvb, 4, 1, "Type: %s (%u)", 
116                             eap_type > EAP_TYPE_COUNT?
117                             "Unknown" : eap_type_name[eap_type],
118                             eap_type);
119     }
120     if (len > 5)
121       dissect_data(tvb, 5, pinfo, tree);
122   }
123 }
124
125 void
126 proto_register_eap(void)
127 {
128   static hf_register_info hf[] = {
129         { &hf_eap_code, { 
130                 "Code", "eap.code", FT_UINT8, BASE_DEC, 
131                 NULL, 0x0, "", HFILL }},
132         { &hf_eap_identifier, {
133                 "Id", "eap.id", FT_UINT8, BASE_DEC,
134                 NULL, 0x0, "", HFILL }},
135         { &hf_eap_len, {
136                 "Length", "eap.len", FT_UINT16, BASE_DEC,
137                 NULL, 0x0, "", HFILL }},
138         { &hf_eap_type, { 
139                 "Type", "eap.type", FT_UINT8, BASE_DEC, 
140                 NULL, 0x0, "", HFILL }},
141   };
142   static gint *ett[] = {
143         &ett_eap,
144   };
145
146   proto_eap = proto_register_protocol("Extensible Authentication Protocol", 
147                                       "EAP", "eap");
148   proto_register_field_array(proto_eap, hf, array_length(hf));
149   proto_register_subtree_array(ett, array_length(ett));
150 }
151
152 void
153 proto_reg_handoff_eap(void)
154 {
155   dissector_add("ppp.protocol", PPP_EAP, dissect_eap, proto_eap);
156 }