1 /*
2                                   NETWIB
3                              Network library
4                 Copyright(c) 1999-2010 Laurent Constantin
5                                   -----
6 
7   Main server   : http://www.laurentconstantin.com/
8   Backup server : http://laurentconstantin.free.fr/
9   [my current email address is on the web servers]
10 
11                                   -----
12   This file is part of Netwib.
13 
14   Netwib is free software: you can redistribute it and/or modify
15   it under the terms of the GNU General Public License version 3
16   as published by the Free Software Foundation.
17 
18   Netwib is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details (http://www.gnu.org/).
22 
23 ------------------------------------------------------------------------
24 */
25 
26 #include <netwib/inc/maininc.h>
27 
28 /*-------------------------------------------------------------*/
netwib_show_etherhdr(netwib_constetherhdr * petherhdr,netwib_encodetype encodetype,netwib_buf * pbuf)29 static netwib_err netwib_show_etherhdr(netwib_constetherhdr *petherhdr,
30                                        netwib_encodetype encodetype,
31                                        netwib_buf *pbuf)
32 {
33   switch(encodetype) {
34     case NETWIB_ENCODETYPE_SYNTH :
35       netwib_er(netwib_buf_append_fmt(pbuf, "%{eth}->%{eth}",
36                                       &petherhdr->src, &petherhdr->dst));
37       break;
38     case NETWIB_ENCODETYPE_ARRAY :
39       netwib_er(netwib_show_array_head("Ethernet", pbuf));
40       netwib_er(netwib_show_array_fmt32(pbuf,
41                                         " %{eth}->%{eth} type:%{uint32:#04X}",
42                                         &petherhdr->src, &petherhdr->dst,
43                                         petherhdr->type));
44       netwib_er(netwib_show_array_tail(pbuf));
45       break;
46     default :
47       return(NETWIB_ERR_LOINTERNALERROR);
48       break;
49   }
50 
51   return(NETWIB_ERR_OK);
52 }
53 
54 /*-------------------------------------------------------------*/
netwib_show_nullhdr(netwib_constnullhdr * pnullhdr,netwib_encodetype encodetype,netwib_buf * pbuf)55 static netwib_err netwib_show_nullhdr(netwib_constnullhdr *pnullhdr,
56                                       netwib_encodetype encodetype,
57                                       netwib_buf *pbuf)
58 {
59   switch(encodetype) {
60     case NETWIB_ENCODETYPE_SYNTH :
61       netwib_er(netwib_buf_append_string("null", pbuf));
62       break;
63     case NETWIB_ENCODETYPE_ARRAY :
64       netwib_er(netwib_show_array_head("Null", pbuf));
65       netwib_er(netwib_show_array_fmt32(pbuf, " type:%{uint32:#08X}",
66                                         pnullhdr->type));
67       netwib_er(netwib_show_array_tail(pbuf));
68       break;
69     default :
70       return(NETWIB_ERR_LOINTERNALERROR);
71       break;
72   }
73 
74   return(NETWIB_ERR_OK);
75 }
76 
77 /*-------------------------------------------------------------*/
netwib_show_loophdr(netwib_constloophdr * ploophdr,netwib_encodetype encodetype,netwib_buf * pbuf)78 static netwib_err netwib_show_loophdr(netwib_constloophdr *ploophdr,
79                                       netwib_encodetype encodetype,
80                                       netwib_buf *pbuf)
81 {
82   switch(encodetype) {
83     case NETWIB_ENCODETYPE_SYNTH :
84       netwib_er(netwib_buf_append_string("loop", pbuf));
85       break;
86     case NETWIB_ENCODETYPE_ARRAY :
87       netwib_er(netwib_show_array_head("Loop", pbuf));
88       netwib_er(netwib_show_array_fmt32(pbuf, " type:%{uint32:#08X}",
89                                         ploophdr->type));
90       netwib_er(netwib_show_array_tail(pbuf));
91       break;
92     default :
93       return(NETWIB_ERR_LOINTERNALERROR);
94       break;
95   }
96 
97   return(NETWIB_ERR_OK);
98 }
99 
100 /*-------------------------------------------------------------*/
netwib_show_ppphdr(netwib_constppphdr * pppphdr,netwib_encodetype encodetype,netwib_buf * pbuf)101 static netwib_err netwib_show_ppphdr(netwib_constppphdr *pppphdr,
102                                      netwib_encodetype encodetype,
103                                      netwib_buf *pbuf)
104 {
105   switch(encodetype) {
106     case NETWIB_ENCODETYPE_SYNTH :
107       netwib_er(netwib_buf_append_string("ppp", pbuf));
108       break;
109     case NETWIB_ENCODETYPE_ARRAY :
110       netwib_er(netwib_show_array_head("Ppp", pbuf));
111       netwib_er(netwib_show_array_fmt32(pbuf, " address:%{uint32:#02X} control:%{uint32:#02X} protocol:%{uint32:#04X}", pppphdr->address, pppphdr->control, pppphdr->protocol));
112       netwib_er(netwib_show_array_tail(pbuf));
113       break;
114     default :
115       return(NETWIB_ERR_LOINTERNALERROR);
116       break;
117   }
118 
119   return(NETWIB_ERR_OK);
120 }
121 
122 /*-------------------------------------------------------------*/
netwib_show_linuxsllhdr(netwib_constlinuxsllhdr * plinuxsllhdr,netwib_encodetype encodetype,netwib_buf * pbuf)123 static netwib_err netwib_show_linuxsllhdr(netwib_constlinuxsllhdr *plinuxsllhdr,
124                                           netwib_encodetype encodetype,
125                                           netwib_buf *pbuf)
126 {
127   netwib_uint32 i, halen;
128   netwib_byte array[80];
129   netwib_buf buf;
130 
131   switch(encodetype) {
132     case NETWIB_ENCODETYPE_SYNTH :
133       netwib_er(netwib_buf_append_string("linuxsll", pbuf));
134       break;
135     case NETWIB_ENCODETYPE_ARRAY :
136       netwib_er(netwib_show_array_head("Linux_SLL", pbuf));
137       netwib_er(netwib_buf_init_ext_arrayempty(array, sizeof(array), &buf));
138       netwib_er(netwib_buf_append_fmt(&buf,
139                     "pkttype:%{uint32:#04X} hatype:%{uint32:#04X} srcaddr:",
140                                       plinuxsllhdr->pkttype,
141                                       plinuxsllhdr->hatype));
142       halen = plinuxsllhdr->halen;
143       if (halen > 8) halen = 8;
144       for (i = 0; i < halen; i++) {
145         netwib_er(netwib_buf_append_fmt(&buf, "%{uint32:02X}",
146                                         plinuxsllhdr->srcaddr[i]));
147       }
148       netwib_er(netwib_show_array_fmt32(pbuf, " %{buf}", &buf));
149       netwib_er(netwib_show_array_fmt32(pbuf, " protocol:%{uint32:#04X}",
150                                         plinuxsllhdr->protocol));
151       netwib_er(netwib_show_array_tail(pbuf));
152       break;
153     default :
154       return(NETWIB_ERR_LOINTERNALERROR);
155       break;
156   }
157 
158   return(NETWIB_ERR_OK);
159 }
160 
161 /*-------------------------------------------------------------*/
netwib_linkhdr_show(netwib_constlinkhdr * plinkhdr,netwib_encodetype encodetype,netwib_buf * pbuf)162 netwib_err netwib_linkhdr_show(netwib_constlinkhdr *plinkhdr,
163                                netwib_encodetype encodetype,
164                                netwib_buf *pbuf)
165 {
166   netwib_buf buf;
167 
168   switch(encodetype) {
169     case NETWIB_ENCODETYPE_SYNTH :
170     case NETWIB_ENCODETYPE_ARRAY :
171       break;
172     default:
173       netwib_er(netwib_buf_init_mallocdefault(&buf));
174       netwib_er(netwib_pkt_append_linkhdr(plinkhdr, &buf));
175       netwib_er(netwib_buf_encode(&buf, encodetype, pbuf));
176       netwib_er(netwib_buf_close(&buf));
177       return(NETWIB_ERR_OK);
178   }
179 
180   switch(plinkhdr->type) {
181     case NETWIB_DEVICE_DLTTYPE_EN10MB :
182       netwib_er(netwib_show_etherhdr(&plinkhdr->hdr.ether, encodetype,
183                                            pbuf));
184       break;
185     case NETWIB_DEVICE_DLTTYPE_NULL :
186       netwib_er(netwib_show_nullhdr(&plinkhdr->hdr.null, encodetype,
187                                           pbuf));
188       break;
189     case NETWIB_DEVICE_DLTTYPE_LOOP :
190       netwib_er(netwib_show_loophdr(&plinkhdr->hdr.loop, encodetype,
191                                           pbuf));
192       break;
193     case NETWIB_DEVICE_DLTTYPE_RAW :
194     case NETWIB_DEVICE_DLTTYPE_RAW4 :
195     case NETWIB_DEVICE_DLTTYPE_RAW6 :
196       /* append nothing */
197       break;
198     case NETWIB_DEVICE_DLTTYPE_PPP :
199       netwib_er(netwib_show_ppphdr(&plinkhdr->hdr.ppp, encodetype,
200                                          pbuf));
201       break;
202     case NETWIB_DEVICE_DLTTYPE_LINUX_SLL :
203       netwib_er(netwib_show_linuxsllhdr(&plinkhdr->hdr.linuxsll,
204                                               encodetype, pbuf));
205       break;
206     default :
207       return(NETWIB_ERR_LONOTIMPLEMENTED);
208       break;
209   }
210   return(NETWIB_ERR_OK);
211 }
212