2 * Copyright (c) 1988-1997
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1998-2012 Michael Richardson <mcr@tcpdump.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that: (1) source code distributions
10 * retain the above copyright notice and this paragraph in its entirety, (2)
11 * distributions including binary code include the above copyright notice and
12 * this paragraph in its entirety in the documentation or other materials
13 * provided with the distribution, and (3) all advertising materials mentioning
14 * features or use of this software display the following acknowledgement:
15 * ``This product includes software developed by the University of California,
16 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17 * the University nor the names of its contributors may be used to endorse
18 * or promote products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 #include "netdissect-stdinc.h"
30 #include "netdissect.h"
40 * Initialize anything that must be initialized before dissecting
43 * This should be called at the beginning of the program; it does
44 * not need to be called, and should not be called, for every
45 * netdissect_options structure.
48 nd_init(char *errbuf
, size_t errbuf_size
)
51 WORD wVersionRequested
;
56 * Request Winsock 2.2; we expect Winsock 2.
58 wVersionRequested
= MAKEWORD(2, 2);
59 err
= WSAStartup(wVersionRequested
, &wsaData
);
61 strlcpy(errbuf
, "Attempting to initialize Winsock failed",
69 * XXX - should we just fail if this fails? Some of the
70 * libsmi calls may fail.
76 * Clears the error buffer, and uses it so we don't get
77 * "unused argument" warnings at compile time.
79 strlcpy(errbuf
, "", errbuf_size
);
84 * Clean up anything that ndo_init() did.
91 * This appears, in libsmi 0.4.8, to do nothing if smiInit()
92 * wasn't done or failed, so we call it unconditionally.
99 * Undo the WSAStartup() call above.
106 nd_have_smi_support(void)
116 * Indicates whether an SMI module has been loaded, so that we can use
117 * libsmi to translate OIDs.
119 int nd_smi_module_loaded
;
122 nd_load_smi_module(const char *module
, char *errbuf
, size_t errbuf_size
)
125 if (smiLoadModule(module
) == 0) {
126 snprintf(errbuf
, errbuf_size
, "could not load MIB module %s",
130 nd_smi_module_loaded
= 1;
133 snprintf(errbuf
, errbuf_size
, "MIB module %s not loaded: no libsmi support",
140 nd_smi_version_string(void)
143 return (smi_version_string
);
151 nd_push_buffer(netdissect_options
*ndo
, u_char
*new_buffer
,
152 const u_char
*new_packetp
, const u_char
*new_snapend
)
154 struct netdissect_saved_packet_info
*ndspi
;
156 ndspi
= (struct netdissect_saved_packet_info
*)malloc(sizeof(struct netdissect_saved_packet_info
));
158 return (0); /* fail */
159 ndspi
->ndspi_buffer
= new_buffer
;
160 ndspi
->ndspi_packetp
= ndo
->ndo_packetp
;
161 ndspi
->ndspi_snapend
= ndo
->ndo_snapend
;
162 ndspi
->ndspi_prev
= ndo
->ndo_packet_info_stack
;
164 ndo
->ndo_packetp
= new_packetp
;
165 ndo
->ndo_snapend
= new_snapend
;
166 ndo
->ndo_packet_info_stack
= ndspi
;
168 return (1); /* success */
172 * Set a new snapshot end to the minimum of the existing snapshot end
173 * and the new snapshot end.
176 nd_push_snapend(netdissect_options
*ndo
, const u_char
*new_snapend
)
178 struct netdissect_saved_packet_info
*ndspi
;
180 ndspi
= (struct netdissect_saved_packet_info
*)malloc(sizeof(struct netdissect_saved_packet_info
));
182 return (0); /* fail */
183 ndspi
->ndspi_buffer
= NULL
; /* no new buffer */
184 ndspi
->ndspi_packetp
= ndo
->ndo_packetp
;
185 ndspi
->ndspi_snapend
= ndo
->ndo_snapend
;
186 ndspi
->ndspi_prev
= ndo
->ndo_packet_info_stack
;
189 * Make sure the new snapend is sane.
191 * If it's after the current snapend, it's not valid. We
192 * silently ignore the new setting; that means that our callers
193 * don't have to do this check themselves, and also means that
194 * if the new length is used when dissecting, we'll go past the
195 * snapend and report an error.
197 * If it's before the beginning of the packet, it's not valid.
198 * That "should not happen", but might happen with a *very*
199 * large adjustment to the snapend; our callers *should* check
200 * for that, so we fail if they haven't done so.
202 if (new_snapend
<= ndo
->ndo_snapend
) {
203 /* Snapend isn't past the previous snapend */
204 if (new_snapend
>= ndo
->ndo_packetp
) {
205 /* And it isn't before the beginning of the packet */
206 ndo
->ndo_snapend
= new_snapend
;
208 /* But it's before the beginning of the packet */
209 ND_PRINT(" [new snapend before beginning of packet in nd_push_snapend]");
213 ndo
->ndo_packet_info_stack
= ndspi
;
215 return (1); /* success */
219 * Change an already-pushed snapshot end. This may increase the
220 * snapshot end, as it may be used, for example, for a Jumbo Payload
221 * option in IPv6. It must not increase it past the snapshot length
222 * atop which the current one was pushed, however.
225 nd_change_snapend(netdissect_options
*ndo
, const u_char
*new_snapend
)
227 struct netdissect_saved_packet_info
*ndspi
;
228 const u_char
*previous_snapend
;
230 ndspi
= ndo
->ndo_packet_info_stack
;
231 if (ndspi
->ndspi_prev
!= NULL
)
232 previous_snapend
= ndspi
->ndspi_prev
->ndspi_snapend
;
234 previous_snapend
= ndo
->ndo_snapend
;
236 * Make sure the new snapend is sane.
238 * If it's after the current snapend, it's not valid. We
239 * silently ignore the new setting; that means that our callers
240 * don't have to do this check themselves, and also means that
241 * if the new length is used when dissecting, we'll go past the
242 * snapend and report an error.
244 * If it's before the beginning of the packet, it's not valid.
245 * That "should not happen", but might happen with a *very*
246 * large adjustment to the snapend; our callers *should* check
247 * for that, so we fail if they haven't done so.
249 if (new_snapend
<= previous_snapend
) {
250 /* Snapend isn't past the previous snapend */
251 if (new_snapend
>= ndo
->ndo_packetp
) {
252 /* And it isn't before the beginning of the packet */
253 ndo
->ndo_snapend
= new_snapend
;
255 /* But it's before the beginning of the packet */
256 ND_PRINT(" [new snapend before beginning of packet in nd_push_snapend]");
263 nd_pop_packet_info(netdissect_options
*ndo
)
265 struct netdissect_saved_packet_info
*ndspi
;
267 ndspi
= ndo
->ndo_packet_info_stack
;
268 ndo
->ndo_packetp
= ndspi
->ndspi_packetp
;
269 ndo
->ndo_snapend
= ndspi
->ndspi_snapend
;
270 ndo
->ndo_packet_info_stack
= ndspi
->ndspi_prev
;
272 free(ndspi
->ndspi_buffer
);
277 nd_pop_all_packet_info(netdissect_options
*ndo
)
279 while (ndo
->ndo_packet_info_stack
!= NULL
)
280 nd_pop_packet_info(ndo
);