Ethereal-dev: [Ethereal-dev] Where I am up to in generating a dissector

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Richard Sharpe <sharpe@xxxxxxxxxx>
Date: Wed, 18 Oct 2000 23:39:46 +0900
Hi,

Attached is some code that represents where I am up to in generating a
dissector from an XML-based description.

Things like SMB will be very easy, compared to the identd protocol.

/* packet-identd.c
 * Routines for identd (Identification Protocol (RFC1413)) packet disassembly
 *
 * $Id: $
 *
 * Copyright (c) 2000 by YOUR NAME HERE <YOUR EMAIL HERE>
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs
 * Copyright 1999 Gerald Combs
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <glib.h>
#include <string.h>
#include "packet.h"
#include "resolv.h"
#include "prefs.h"

#define TCP_PORT_IDENTD 113
void proto_reg_handoff_identd(void);

static int proto_identd = -1;

static int ett_identd = -1;
static int ett_identd_request = -1;
static int ett_identd_reply = -1;

int
dissect_identd_request(tvbuff_t *tvb, int offset, proto_tree *tree)
{
  proto_tree          *ti = NULL, request_tree = NULL;
  int                 cc;

  if (tree) {
    ti = proto_tree_add_text(tree, offset, 0, "request");
    request_tree = proto_tree_add_subtree(ti, ett_identd_request);
  }

  /* Handle the port-on-server field */
  if ((cc = ptvcursor_skip_item(tvb, offset, "1+", "UINT16", "", &var_port-on-server)) < 0){
  /* Error handling code ... */
  }
  else {
    if (tree) {
      ti = proto_tree_add_text(tree, tvb, offset, 1+, "port-on-server: %s", tvb_format_text(tvb, offset, 1+));
    }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "*", "", " ", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "1", "", ",", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "*", "", " ", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  /* Handle the port-on-client field */
  if ((cc = ptvcursor_skip_item(tvb, offset, "1+", "UINT16", "", &var_port-on-client)) < 0){
  /* Error handling code ... */
  }
  else {
    if (tree) {
      ti = proto_tree_add_text(tree, tvb, offset, 1+, "port-on-client: %s", tvb_format_text(tvb, offset, 1+));
    }
  offset += cc;


}


int
dissect_identd_reply(tvbuff_t *tvb, int offset, proto_tree *tree)
{
  proto_tree          *ti = NULL, reply_tree = NULL;
  int                 cc;

  if (tree) {
    ti = proto_tree_add_text(tree, offset, 0, "reply");
    reply_tree = proto_tree_add_subtree(ti, ett_identd_reply);
  }

  /* Handle the port-on-client field */
  if ((cc = ptvcursor_skip_item(tvb, offset, "1+", "UINT16", "", &var_port-on-client)) < 0){
  /* Error handling code ... */
  }
  else {
    if (tree) {
      ti = proto_tree_add_text(tree, tvb, offset, 1+, "port-on-client: %s", tvb_format_text(tvb, offset, 1+));
    }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "*", "", " ", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "1", "", ",", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "*", "", " ", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  /* Handle the port-on-server field */
  if ((cc = ptvcursor_skip_item(tvb, offset, "1+", "UINT16", "", &var_port-on-server)) < 0){
  /* Error handling code ... */
  }
  else {
    if (tree) {
      ti = proto_tree_add_text(tree, tvb, offset, 1+, "port-on-server: %s", tvb_format_text(tvb, offset, 1+));
    }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "*", "", " ", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "1", "", ":", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;

  if ((cc = ptvcursor_skip_item(tvb, offset, "*", "", " ", NULL)) < 0){
  /* Error handling code ... */
  }
  offset += cc;


}



static void
dissect_identd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
  proto_tree     *identd_tree, *ti;
}


/* Register all the bits needed with the filtering engine */

void
proto_register_identd(void)
{
  static hf_register_info hf[] = {
    { &hf_identd_req,
      /* Change the following to the type you need */
      { "Request", "identd.req", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_identd_rsp,
      { "Response", "identd.rsp", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    /* Add more fields here */
  };
  static gint *ett[] = {
    &ett_identd
  };
  module_t *identd_module;

  /* Register our configuration options for , particularly our port */

  identd_module = prefs_register_module("identd", "IDENTD", proto_reg_handoff_identd);

  prefs_register_uint_preference(identd_module, "tcp.port", " TCP Port",
				 "Set the port for  messages (if other"
				 " than the default of 113)",
				 10, &global_identd_tcp_port);

  proto_identd = proto_register_protocol("Identification Protocol (RFC1413)",
				       "identd");

  proto_register_field_array(proto_identd, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));

}

/* The registration hand-off routine */
void
proto_reg_handoff_identd(void)
{
  static int identd_prefs_initialized = FALSE;

  if (identd_prefs_initialized) {

    dissector_delete("tcp.port", tcp_port, dissect_identd);

  }
  else {

    identd_prefs_initialized = TRUE;

  }

  /* Set our port number for future use */

  tcp_port = global_identd_tcp_port;

  dissector_add("tcp.port", global_identd_tcp_port, dissect_identd);

}


Regards
-------
Richard Sharpe, sharpe@xxxxxxxxxx
Samba (Team member, www.samba.org), Ethereal (Team member, www.zing.org)
Contributing author, SAMS Teach Yourself Samba in 24 Hours
Author, Special Edition, Using Samba