Home | History | Annotate | Download | only in tcpdump
      1 /*
      2  * Copyright (c) 1988-1997
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Copyright (c) 1998-2012  Michael Richardson <mcr (at) tcpdump.org>
      6  *      The TCPDUMP project
      7  *
      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
     19  * written permission.
     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.
     23  */
     24 
     25 #ifdef HAVE_CONFIG_H
     26 #include "config.h"
     27 #endif
     28 
     29 #include <netdissect-stdinc.h>
     30 #include "netdissect.h"
     31 #include <string.h>
     32 #include <stdio.h>
     33 
     34 #ifdef USE_LIBSMI
     35 #include <smi.h>
     36 #endif
     37 
     38 /*
     39  * Initialize anything that must be initialized before dissecting
     40  * packets.
     41  *
     42  * This should be called at the beginning of the program; it does
     43  * not need to be called, and should not be called, for every
     44  * netdissect_options structure.
     45  */
     46 int
     47 nd_init(char *errbuf, size_t errbuf_size)
     48 {
     49 #ifdef _WIN32
     50 	WORD wVersionRequested;
     51 	WSADATA wsaData;
     52 	int err;
     53 
     54 	/*
     55 	 * Request Winsock 2.2; we expect Winsock 2.
     56 	 */
     57 	wVersionRequested = MAKEWORD(2, 2);
     58 	err = WSAStartup(wVersionRequested, &wsaData);
     59 	if (err != 0) {
     60 		strlcpy(errbuf, "Attempting to initialize Winsock failed",
     61 		    errbuf_size);
     62 		return (-1);
     63 	}
     64 #endif /* _WIN32 */
     65 
     66 #ifdef USE_LIBSMI
     67 	/*
     68 	 * XXX - should we just fail if this fails?  Some of the
     69 	 * libsmi calls may fail.
     70 	 */
     71 	smiInit("tcpdump");
     72 #endif
     73 
     74 	/*
     75 	 * Clears the error buffer, and uses it so we don't get
     76 	 * "unused argument" warnings at compile time.
     77 	 */
     78 	strlcpy(errbuf, "", errbuf_size);
     79 	return (0);
     80 }
     81 
     82 /*
     83  * Clean up anything that ndo_init() did.
     84  */
     85 void
     86 nd_cleanup(void)
     87 {
     88 #ifdef USE_LIBSMI
     89 	/*
     90 	 * This appears, in libsmi 0.4.8, to do nothing if smiInit()
     91 	 * wasn't done or failed, so we call it unconditionally.
     92 	 */
     93 	smiExit();
     94 #endif
     95 
     96 #ifdef _WIN32
     97 	/*
     98 	 * Undo the WSAStartup() call above.
     99 	 */
    100 	WSACleanup();
    101 #endif
    102 }
    103 
    104 int
    105 nd_have_smi_support(void)
    106 {
    107 #ifdef USE_LIBSMI
    108 	return (1);
    109 #else
    110 	return (0);
    111 #endif
    112 }
    113 
    114 /*
    115  * Indicates whether an SMI module has been loaded, so that we can use
    116  * libsmi to translate OIDs.
    117  */
    118 int nd_smi_module_loaded;
    119 
    120 int
    121 nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size)
    122 {
    123 #ifdef USE_LIBSMI
    124 	if (smiLoadModule(module) == 0) {
    125 		snprintf(errbuf, errbuf_size, "could not load MIB module %s",
    126 		    module);
    127 		return (-1);
    128 	}
    129 	nd_smi_module_loaded = 1;
    130 	return (0);
    131 #else
    132 	snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support",
    133 	    module);
    134 	return (-1);
    135 #endif
    136 }
    137 
    138 const char *
    139 nd_smi_version_string(void)
    140 {
    141 #ifdef USE_LIBSMI
    142 	return (smi_version_string);
    143 #else
    144 	return (NULL);
    145 #endif
    146 }
    147