Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4
Sql.Provider.jdbc 1.0

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.Network.Pcap
Viewing contents of Public_Network_Pcap-1.5/pcap.c

/* Generated from "pcap.cmod" by precompile.pike
 *
 * Do NOT edit this file.
 */

#undef PRECOMPILE_API_VERSION
#define PRECOMPILE_API_VERSION 2


#line 1 "pcap.cmod"
/* 
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: pcap.cmod,v 1.1.1.1 2004-09-01 15:12:44 hww3 Exp $
 */

/*
 * File licensing and authorship information block.
 *
 * Version: MPL 1.1/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *
 * Bill Welliver 
 *
 * Portions created by the Initial Developer are Copyright (C) Bill Welliver
 * All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of the LGPL, and not to allow others to use your version
 * of this file under the terms of the MPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL or the LGPL.
 *
 * Significant Contributors to this file are:
 *
 */

/*! @module Public
 */

/*! @module Network
 */

/*! @module Pcap
 */

#define _GNU_SOURCE

#include "util.h"
#include "config.h"

#ifdef HAVE_LIBWPCAP
#define HAVE_LIBPCAP
#endif

#ifdef HAVE_LIBPCAP

#ifdef HAVE_UNISTD_H
#include 
#endif

#include 
#include 
#include 
#include 

#ifdef HAVE_PCAP_H
#include 
#endif

#ifndef DEFAULT_CMOD_STORAGE
#define DEFAULT_CMOD_STORAGE
#endif


#undef class_Pcap_defined
#define class_Pcap_defined
DEFAULT_CMOD_STORAGE struct program *Pcap_program=NULL;
static int Pcap_program_fun_num=-1;
#line 98 "pcap.cmod"
typedef struct
{
    pcap_t * handle;
} PCAP_OBJECT_DATA;


#undef var_object_data_Pcap_defined
#define var_object_data_Pcap_defined

#undef var_promisc_Pcap_defined
#define var_promisc_Pcap_defined

#undef var_dev_Pcap_defined
#define var_dev_Pcap_defined

#undef var_capture_cb_Pcap_defined
#define var_capture_cb_Pcap_defined

#undef var_capture_length_Pcap_defined
#define var_capture_length_Pcap_defined

#undef var_capture_timeout_Pcap_defined
#define var_capture_timeout_Pcap_defined

#undef THIS
#define THIS ((struct Pcap_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_PCAP
#define THIS_PCAP ((struct Pcap_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_PCAP
#define OBJ2_PCAP(o) ((struct Pcap_struct *)(o->storage+Pcap_storage_offset))

#undef GET_PCAP_STORAGE
#define GET_PCAP_STORAGE ((struct Pcap_struct *)(o->storage+Pcap_storage_offset)
static ptrdiff_t Pcap_storage_offset;
struct Pcap_struct {

#ifdef var_object_data_Pcap_defined
#line 103 "pcap.cmod"
PCAP_OBJECT_DATA   *object_data;
#endif /* var_object_data_Pcap_defined */

#ifdef var_promisc_Pcap_defined
  INT_TYPE promisc;

#endif /* var_promisc_Pcap_defined */

#ifdef var_dev_Pcap_defined
  struct pike_string * dev;

#endif /* var_dev_Pcap_defined */

#ifdef var_capture_cb_Pcap_defined
  struct svalue capture_cb;

#endif /* var_capture_cb_Pcap_defined */

#ifdef var_capture_length_Pcap_defined
  INT_TYPE capture_length;

#endif /* var_capture_length_Pcap_defined */

#ifdef var_capture_timeout_Pcap_defined
  INT_TYPE capture_timeout;

#endif /* var_capture_timeout_Pcap_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct Pcap_struct *Pcap_gdb_dummy_ptr;
#endif
#line 110 "pcap.cmod"
/****
 *
 * Low-level Pcap interface
 *
 ****/
void dispatch_pike_method(u_char *user_data, const struct pcap_pkthdr *header,
  const u_char *packet);

void got_packet_cb(u_char *user_data, const struct pcap_pkthdr *header, 
  const u_char *packet)
{

  struct thread_state *state;

  if((state = thread_state_for_id(th_self()))!=NULL)
  {
    /* This is a pike thread.  Do we have the interpreter lock? */
    if(!state->swapped)
    {
      /* Yes.  Go for it... */
      dispatch_pike_method(user_data, header, packet);
    }
    else
    {
      /* Nope, let's get it... */
      mt_lock_interpreter();
      SWAP_IN_THREAD(state);

      dispatch_pike_method(user_data, header, packet);

      /* Restore */
      SWAP_OUT_THREAD(state);
      mt_unlock_interpreter();
     }
   }
}


void dispatch_pike_method(u_char *user_data, const struct pcap_pkthdr *header, 
  const u_char *packet)
{
  struct svalue *sv;
  int num_args;
 
  sv = &(THIS->capture_cb);

  if(UNSAFE_IS_ZERO(sv)); /* have we set a callback? */
  else
  {
    /* push the callback arguments onto the stack. */
    push_svalue(&THIS->capture_cb);
  
    num_args=1;

    push_text("time_sec");
    push_int(header->ts.tv_sec);
    push_text("time_msec");
    push_int(header->ts.tv_usec);
    push_text("caplen");
    push_int(header->caplen);
    push_text("len");
    push_int(header->len);
    push_text("data");
    push_string(make_shared_binary_string(packet, header->caplen));
    f_aggregate_mapping(10);

    if(user_data)
    {
       sv = (struct svalue *)user_data;
       push_svalue(sv);
       num_args = 2;
    }
  
    /* call the callback function */
    apply_svalue(Pike_sp-(num_args+1), num_args);

    /* pop the callback and returned value from the stack */
    pop_n_elems(num_args);  
  }
}

/*! @decl void create()
 *!   Creates a new Pcap object
 *!
 */
#define f_Pcap_create_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_create_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_create(INT32 args) {
#line 195 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("create",args,0);
{
  struct svalue *sv;

  sv = malloc(sizeof(struct svalue));

  sv->type = T_INT;
  sv->subtype = NUMBER_UNDEFINED;

  THIS->capture_length = 56;
  THIS->capture_timeout = 10;
  assign_svalue(&THIS->capture_cb, sv);
}

}
/*! @decl string lookup_dev()
 *!   Returns a likely device name for capturing data.
 *!
 */
#define f_Pcap_lookup_dev_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_lookup_dev_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_lookup_dev(INT32 args) {
#line 213 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("lookup_dev",args,0);
{
  char * dev;
  char errbuf[PCAP_ERRBUF_SIZE];

  dev = pcap_lookupdev(errbuf);

  if(strlen(errbuf))
    Pike_error("Public.Network.Pcap()->lookup_dev(): %s\n", errbuf);
  push_text(dev);
}

}
/*! @decl void set_promisc(int promisc)
 *!   Toggles the promiscuous capture flag.
 *!
 *!    Only takes effect when a capture is opened.
 *!
 */
#define f_Pcap_set_promisc_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_set_promisc_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_set_promisc(INT32 args) {
#line 231 "pcap.cmod"
INT_TYPE promisc;
#line 231 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("set_promisc",args,1);
#line 231 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("set_promisc",1,"int");
#line 231 "pcap.cmod"
promisc=Pike_sp[0-1].u.integer;
{
  THIS->promisc = promisc;
  pop_n_elems(args);
}

}
/*! @decl void set_promisc(int msec)
 *!   Sets the capture timeout. Not available or effective on all systems.
 *!
 *!   Only takes effect when a capture is opened.
 *!
 */
#define f_Pcap_set_capture_timeout_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_set_capture_timeout_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_set_capture_timeout(INT32 args) {
#line 243 "pcap.cmod"
INT_TYPE msec;
#line 243 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("set_capture_timeout",args,1);
#line 243 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("set_capture_timeout",1,"int");
#line 243 "pcap.cmod"
msec=Pike_sp[0-1].u.integer;
{
  THIS->capture_timeout = msec;
  pop_n_elems(args);
}

}
/*! @decl void set_capture_callback(function cb)
 *!   Sets the capture callback function for use with @[loop] and @[dispatch].
 *!   The callback function takes 2 arguments: a mapping containing the data, and an
 *!   optional user data argument supplied to the capture function.
 *!
 */
#define f_Pcap_set_capture_callback_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_set_capture_callback_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_set_capture_callback(INT32 args) {
#line 255 "pcap.cmod"
struct svalue * cb;
#line 255 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("set_capture_callback",args,1);
#line 255 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_FUNCTION) SIMPLE_ARG_TYPE_ERROR("set_capture_callback",1,"function");
#line 255 "pcap.cmod"
cb=Pike_sp+0-1; dmalloc_touch_svalue(Pike_sp+0-1);
{
  assign_svalue(&(THIS->capture_cb), cb);
  pop_n_elems(args);
}

}
/*! @decl void set_capture_length(int len)
 *!   Sets the maximum amount of data that will be captured. 
 *!   Should be set to at least the largest amount of data expected on a given 
 *!   network. By default, the length is set to 56, which is a minimum amount for 
 *!   useful data capture.
 *!
 *!   Only takes effect when a capture is opened.
 *!
 */
#define f_Pcap_set_capture_length_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_set_capture_length_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_set_capture_length(INT32 args) {
#line 270 "pcap.cmod"
INT_TYPE len;
#line 270 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("set_capture_length",args,1);
#line 270 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("set_capture_length",1,"int");
#line 270 "pcap.cmod"
len=Pike_sp[0-1].u.integer;
{
  THIS->capture_length = len;
  pop_n_elems(args);
}

}
/*! @decl string version()
 *!   Returns the version if the pcap library in use.
 *!
 *!   Not available with all versions of the pcap library. If this function is not available,
 *!   this function will return zero (0).
 *!
 */
#define f_Pcap_version_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_version_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_version(INT32 args) {
#line 283 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("version",args,0);
{
  char * ver;

#ifdef HAVE_PCAP_LIB_VERSION
  ver = pcap_lib_version();
  push_text(ver);
#else
  push_int(0);  
#endif /* HAVE_PCAP_LIB_VERSION */

}

}
/*! @decl string file_version()
 *!   Returns the version of the capture file being read.
 */
#define f_Pcap_file_version_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_file_version_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_file_version(INT32 args) {
#line 299 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("file_version",args,0);
{
  int res;
  int major_ver;
  int minor_ver;
  char * v;

  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->version(): not open.\n");

  major_ver = pcap_major_version(THIS->object_data->handle);
  minor_ver = pcap_minor_version(THIS->object_data->handle);
 
  res = sprintf(v, "%d.%d", major_ver, minor_ver);

  push_text(v);
}


}
/*! @decl int close()
 *!   Closes the capture device.
 */
#define f_Pcap_close_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_close_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_close(INT32 args) {
#line 321 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("close",args,0);
{
  if(THIS->object_data->handle)
  {
    pcap_close(THIS->object_data->handle);
    THIS->object_data->handle = 0;
    push_int(1);    
  }
  else
  {
    Pike_error("Public.Network.Pcap()->close(): not open.\n");
  }
}

}
/*! @decl string open_live(string dev)
 *!   opens a capture session on device dev.
 *!
 *!   throws an error if the open was unsuccessful.
 *! 
 *!  @seealso 
 *!    @[set_capture_length] @[set_promisc]
 */
#define f_Pcap_open_live_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_open_live_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_open_live(INT32 args) {
#line 343 "pcap.cmod"
struct pike_string * dev;
#line 343 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("open_live",args,1);
#line 343 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("open_live",1,"string");
#line 343 "pcap.cmod"
debug_malloc_pass(dev=Pike_sp[0-1].u.string);
{
  pcap_t * h;
  char errbuf[PCAP_ERRBUF_SIZE];
  errbuf[0]=0;

  if(THIS->object_data->handle)
  {
	pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->open_live(): already open.\n");
  }

  h = pcap_open_live(dev->str, THIS->capture_length, THIS->promisc, 0, errbuf);

  if(!h) 
  { 
    pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->open_live(): %s\n", errbuf);
  }

  if(strlen(errbuf))
  {
  pop_n_elems(args);
  Pike_error("Public.Network.Pcap()->open_live(): %s\n", errbuf);
  }

  THIS->object_data->handle = h;

  add_ref(dev);
  THIS->dev = dev;

  pop_n_elems(args);
  push_int(1);
  return;
}

}
/*! @decl string open_offline(string file)
 *!   opens a the capture file located at path file.
 *!
 *!   throws an error if the open was unsuccessful.
 *! 
 */
#define f_Pcap_open_offline_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_open_offline_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_open_offline(INT32 args) {
#line 385 "pcap.cmod"
struct pike_string * file;
#line 385 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("open_offline",args,1);
#line 385 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("open_offline",1,"string");
#line 385 "pcap.cmod"
debug_malloc_pass(file=Pike_sp[0-1].u.string);
{
  pcap_t * h;
  char errbuf[PCAP_ERRBUF_SIZE];
  errbuf[0]=0;

  if(THIS->object_data->handle)
  {
	pop_n_elems(args);
      Pike_error("Public.Network.Pcap()->open_offline(): already open.\n");
  }

  h = pcap_open_offline(file->str, errbuf);

  if(!h) 
  { 
	pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->open_offline(): %s\n", errbuf);
  }

  THIS->object_data->handle = h;

  pop_n_elems(args);
  push_int(1);
  return;
}

}
/*! @decl string set_filter(string filter)
 *!   Compiles and sets a capture filter for the current filter session.
 *! 
 *!   @param filter
 *!     A string representing the desired filter, in bpf filter format.
 *!   @example
 *!     set_filter("host pelix.ida.liu.se");
 *!
 *!   throws an error if the filter set was unsuccessful.
 *! 
 */
#define f_Pcap_set_filter_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_set_filter_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_set_filter(INT32 args) {
#line 423 "pcap.cmod"
struct pike_string * filter;
#line 423 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("set_filter",args,1);
#line 423 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("set_filter",1,"string");
#line 423 "pcap.cmod"
debug_malloc_pass(filter=Pike_sp[0-1].u.string);
{
  char errbuf[PCAP_ERRBUF_SIZE];
  struct bpf_program *filt;
  bpf_u_int32 mask;
  bpf_u_int32 net;
  char * f;
  char * d;

  filt = malloc(sizeof(struct bpf_program));
  errbuf[0] = 0;

  f = strdup(filter->str);
  if(!THIS->dev)
  {
	pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->set_filter(): no device opened.\n");
  }
  d = strdup(THIS->dev->str);

  pcap_lookupnet(d, &net, &mask, &errbuf);

  if(errbuf && strlen(errbuf))
  {
	pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->set_filter(): %s\n", errbuf);
  }
  pcap_compile(THIS->object_data->handle, filt, f, 0, net);
  if(errbuf && strlen(errbuf))
  {
	pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->set_filter(): %s\n", errbuf);
  }

  pcap_setfilter(THIS->object_data->handle, filt);
  if(errbuf && strlen(errbuf))
  {
	pop_n_elems(args);
    Pike_error("Public.Network.Pcap()->set_filter(): %s\n", errbuf);
  }

  pcap_freecode(filt);

  pop_n_elems(args);

  push_int(1);
}

}
/*! @decl void breakloop()
 *!  break free from a capture loop.
 *!
 *! @note
 *!  not available with all versions of the pcap library. if not present, this function will
 *!  perform no actions.
 */
#define f_Pcap_breakloop_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_breakloop_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_breakloop(INT32 args) {
#line 478 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("breakloop",args,0);
{

#ifdef HAVE_PCAP_BREAKLOOP
  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->breakloop(): not open.\n");

  pcap_breakloop(THIS->object_data->handle);
#else

  /* we don't have pcap_breakloop(), so do nothing */

#endif /* HAVE_PCAP_BREAKLOOP */

  return;
}

}
/*! @decl int loop(int cnt, mixed|void data)
 *!  start a capture loop.
 *!  for each packet captured, the capture callback function will be called.
 *! 
 *! @param cnt
 *!   the number of packets to collect before returning. 
 *!
 *! @param data
 *!   an optional parameter that will be passed as the last argument to the capture callback function.
 *!
 *!  @returns
 *!    0 if all packets were successfully captured, -1 if an error occurred, or -2 if @[breakloop] was
 *!    used to terminate packet processing.
 *!
 *! @seealso
 *!   @[set_capture_callback]
 */
#define f_Pcap_loop_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_loop_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_loop(INT32 args) {
#line 512 "pcap.cmod"
INT_TYPE cnt;
#line 512 "pcap.cmod"
struct svalue * data;
#line 512 "pcap.cmod"
if(args < 1) wrong_number_of_args_error("loop",args,1);
#line 512 "pcap.cmod"
if(args > 2) wrong_number_of_args_error("loop",args,2);
#line 512 "pcap.cmod"
if(Pike_sp[0-args].type != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("loop",1,"int");
#line 512 "pcap.cmod"
cnt=Pike_sp[0-args].u.integer;
#line 512 "pcap.cmod"
if (args > 1) {
#line 512 "pcap.cmod"
data=Pike_sp+1-args; dmalloc_touch_svalue(Pike_sp+1-args);
#line 512 "pcap.cmod"
} else data = NULL;
{
  u_char * user_data;
  int res;

  user_data = (u_char *)data;
  
  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->loop(): not open.\n");

  THREADS_ALLOW();
  res = pcap_loop(THIS->object_data->handle, cnt, 
    got_packet_cb, user_data);  
  THREADS_DISALLOW();

  push_int(res);

  return;
}

}
/*! @decl int dispatch(int cnt, mixed|void data)
 *!  capture a number of packets.
 *!  for each packet captured, the capture callback function will be called.
 *! 
 *! @param cnt
 *!   the maximum number of packets to collect before returning. may return before any or all of the packets are 
 *!   captured. 
 *!
 *! @param data
 *!   an optional parameter that will be passed as the last argument to the capture callback function.
 *!
 *!  @returns 
 *!    the number of packets captured.
 *!
 *! @seealso
 *!   @[set_capture_callback]
 */
#define f_Pcap_dispatch_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_dispatch_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_dispatch(INT32 args) {
#line 549 "pcap.cmod"
INT_TYPE cnt;
#line 549 "pcap.cmod"
struct svalue * data;
#line 549 "pcap.cmod"
if(args < 1) wrong_number_of_args_error("dispatch",args,1);
#line 549 "pcap.cmod"
if(args > 2) wrong_number_of_args_error("dispatch",args,2);
#line 549 "pcap.cmod"
if(Pike_sp[0-args].type != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("dispatch",1,"int");
#line 549 "pcap.cmod"
cnt=Pike_sp[0-args].u.integer;
#line 549 "pcap.cmod"
if (args > 1) {
#line 549 "pcap.cmod"
data=Pike_sp+1-args; dmalloc_touch_svalue(Pike_sp+1-args);
#line 549 "pcap.cmod"
} else data = NULL;
{
  u_char * user_data;
  int res;

  user_data = (u_char *)data;
  
  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->dispatch(): not open.\n");

  THREADS_ALLOW();
  res = pcap_dispatch(THIS->object_data->handle, cnt, 
    got_packet_cb, user_data);  
  THREADS_DISALLOW();
  push_int(res);

  return;
}

}
/*! @decl mapping next()
 *!  capture a packet.
 *!
 *! @returns
 *!   a mapping containing packet data, or zero if no packet was captured in time.
 *! @seealso
 *!   @[set_capture_timeout]
 */
#define f_Pcap_next_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_next_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_next(INT32 args) {
#line 576 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("next",args,0);
{
  struct pcap_pkthdr header;          /* The header that pcap gives us */
  const u_char *packet;                 /* The actual packet */

  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->next(): not open.\n");

  THREADS_ALLOW();
  packet = pcap_next(THIS->object_data->handle, &header);
  THREADS_DISALLOW();

  if(packet == NULL)
  {
     push_int(0);
     return;
  } 
  push_text("len");
  push_int(header.len);
  push_text("caplen");
  push_int(header.caplen);
  push_text("time");
  push_int(header.ts.tv_sec);
  push_text("data");
  push_string(make_shared_binary_string(packet, header.caplen));
  f_aggregate_mapping(8);
}


}
/*! @decl int datalink()
 *!  returns the type of datalink encoding in use with the current devices
 *!
 *! @returns
 *!   DLT_NULL, DLT_EN10MB, DLT_IEEE802, DLT_ARCNET or DLT_SLIP.
 */
#define f_Pcap_datalink_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_datalink_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_datalink(INT32 args) {
#line 611 "pcap.cmod"
if(args != 0) wrong_number_of_args_error("datalink",args,0);
{
  int datalink;

  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->datalink(): not open.\n");

  datalink = pcap_datalink(THIS->object_data->handle);

  do { INT_TYPE ret_=((datalink));  push_int(ret_); return; }while(0);
#line 621 "pcap.cmod"
}

}
/*! @decl string datalink_val_to_name(int datalink)
 *!
 *!  converts a datalink val as returned by @[datalink] to a string
 */
#define f_Pcap_datalink_val_to_name_defined
DEFAULT_CMOD_STORAGE ptrdiff_t f_Pcap_datalink_val_to_name_fun_num = 0;
DEFAULT_CMOD_STORAGE void f_Pcap_datalink_val_to_name(INT32 args) {
#line 627 "pcap.cmod"
INT_TYPE datalink;
#line 627 "pcap.cmod"
if(args != 1) wrong_number_of_args_error("datalink_val_to_name",args,1);
#line 627 "pcap.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("datalink_val_to_name",1,"int");
#line 627 "pcap.cmod"
datalink=Pike_sp[0-1].u.integer;
{
  const char * datalink_name;

  if(!THIS->object_data->handle)
    Pike_error("Public.Network.Pcap()->datalink_val_to_name(): not open.\n");

  datalink_name = pcap_datalink_val_to_name(datalink);

  pop_stack();
  push_text(datalink_name);
  free(datalink_name);
}

}

#undef extra_Pcap_defined
#define extra_Pcap_defined

#undef internal_init_Pcap_defined
#define internal_init_Pcap_defined

#undef Pcap_event_handler_defined
#define Pcap_event_handler_defined
static void init_Pcap_struct(void)
#line 663 "pcap.cmod"
{
    PCAP_OBJECT_DATA * dta = 
	(PCAP_OBJECT_DATA*)malloc(sizeof(PCAP_OBJECT_DATA));
    if (!dta)
        Pike_error("init_pcap: Out of memory!\n");

    dta->handle=0;

    THIS->object_data = dta;

}


#undef Pcap_gc_live_obj
#define Pcap_gc_live_obj

#undef internal_exit_Pcap_defined
#define internal_exit_Pcap_defined

#undef Pcap_event_handler_defined
#define Pcap_event_handler_defined
static void exit_Pcap_struct(void)
#line 676 "pcap.cmod"
{
  if(THIS->object_data)
  {

    if(THIS->object_data->handle != NULL)
      free(THIS->object_data->handle);

    free(THIS->object_data);
  }
}


#ifdef Pcap_event_handler_defined
static void Pcap_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_Pcap_defined
  case PROG_EVENT_INIT: init_Pcap_struct(); break;

#endif /* internal_init_Pcap_defined */

#ifdef internal_exit_Pcap_defined
  case PROG_EVENT_EXIT: exit_Pcap_struct(); break;

#endif /* internal_exit_Pcap_defined */
  default: break; 
  }
}

#endif /* Pcap_event_handler_defined */
#line 689 "pcap.cmod"
#endif /* HAVE_LIBPCAP */

/*! @endmodule
 */

/*! @endmodule
 */

/*! @endmodule
 */



PIKE_MODULE_INIT {

#ifdef class_Pcap_defined

#ifdef PROG_PCAP_ID
#line 95 "pcap.cmod"
  START_NEW_PROGRAM_ID(PCAP);
#else
#line 95 "pcap.cmod"
  start_new_program();

#endif /* PROG_PCAP_ID */

#ifndef tObjImpl_PCAP

#undef tObjImpl_PCAP
#define tObjImpl_PCAP tObj

#endif /* tObjImpl_PCAP */

#ifdef THIS_PCAP
  Pcap_storage_offset = ADD_STORAGE(struct Pcap_struct);
#endif /* THIS_PCAP */

#ifdef var_promisc_Pcap_defined
  PIKE_MAP_VARIABLE("promisc", Pcap_storage_offset + OFFSETOF(Pcap_struct, promisc),
                    "\10\200\0\0\0\177\377\377\377", PIKE_T_INT, 0);
#endif /* var_promisc_Pcap_defined */

#ifdef var_dev_Pcap_defined
  PIKE_MAP_VARIABLE("dev", Pcap_storage_offset + OFFSETOF(Pcap_struct, dev),
                    tStr, PIKE_T_STRING, 0);
#endif /* var_dev_Pcap_defined */

#ifdef var_capture_cb_Pcap_defined
  PIKE_MAP_VARIABLE("capture_cb", Pcap_storage_offset + OFFSETOF(Pcap_struct, capture_cb),
                    tFuncV(tNone,tMix,tAny), PIKE_T_MIXED, 0);
#endif /* var_capture_cb_Pcap_defined */

#ifdef var_capture_length_Pcap_defined
  PIKE_MAP_VARIABLE("capture_length", Pcap_storage_offset + OFFSETOF(Pcap_struct, capture_length),
                    "\10\200\0\0\0\177\377\377\377", PIKE_T_INT, 0);
#endif /* var_capture_length_Pcap_defined */

#ifdef var_capture_timeout_Pcap_defined
  PIKE_MAP_VARIABLE("capture_timeout", Pcap_storage_offset + OFFSETOF(Pcap_struct, capture_timeout),
                    "\10\200\0\0\0\177\377\377\377", PIKE_T_INT, 0);
#endif /* var_capture_timeout_Pcap_defined */

#ifdef extra_Pcap_defined
#line 642 "pcap.cmod"
{
  int o;

  o = DLT_NULL;
  add_integer_constant("DLT_NULL", o, 0);

  o = DLT_EN10MB;
  add_integer_constant("DLT_EN10MB", o, 0);

  o = DLT_IEEE802;
  add_integer_constant("DLT_IEEE802", o, 0);

  o = DLT_ARCNET;
  add_integer_constant("DLT_ARCNET", o, 0);

  o = DLT_SLIP;
  add_integer_constant("DLT_SLIP", o, 0);

}


#endif /* extra_Pcap_defined */

#ifdef Pcap_event_handler_defined
  pike_set_prog_event_callback(Pcap_event_handler);

#ifndef Pcap_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* Pcap_gc_live_obj */

#endif /* Pcap_event_handler_defined */

#ifdef f_Pcap_create_defined
  f_Pcap_create_fun_num =
#line 195 "pcap.cmod"
    ADD_FUNCTION2("create", f_Pcap_create, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_create_defined */

#ifdef f_Pcap_lookup_dev_defined
  f_Pcap_lookup_dev_fun_num =
#line 213 "pcap.cmod"
    ADD_FUNCTION2("lookup_dev", f_Pcap_lookup_dev, tFunc(tNone,tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_lookup_dev_defined */

#ifdef f_Pcap_set_promisc_defined
  f_Pcap_set_promisc_fun_num =
#line 231 "pcap.cmod"
    ADD_FUNCTION2("set_promisc", f_Pcap_set_promisc, tFunc("\10\200\0\0\0\177\377\377\377",tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_set_promisc_defined */

#ifdef f_Pcap_set_capture_timeout_defined
  f_Pcap_set_capture_timeout_fun_num =
#line 243 "pcap.cmod"
    ADD_FUNCTION2("set_capture_timeout", f_Pcap_set_capture_timeout, tFunc("\10\200\0\0\0\177\377\377\377",tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_set_capture_timeout_defined */

#ifdef f_Pcap_set_capture_callback_defined
  f_Pcap_set_capture_callback_fun_num =
#line 255 "pcap.cmod"
    ADD_FUNCTION2("set_capture_callback", f_Pcap_set_capture_callback, tFunc(tFuncV(tNone,tMix,tAny),tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_set_capture_callback_defined */

#ifdef f_Pcap_set_capture_length_defined
  f_Pcap_set_capture_length_fun_num =
#line 270 "pcap.cmod"
    ADD_FUNCTION2("set_capture_length", f_Pcap_set_capture_length, tFunc("\10\200\0\0\0\177\377\377\377",tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_set_capture_length_defined */

#ifdef f_Pcap_version_defined
  f_Pcap_version_fun_num =
#line 283 "pcap.cmod"
    ADD_FUNCTION2("version", f_Pcap_version, tFunc(tNone,tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_version_defined */

#ifdef f_Pcap_file_version_defined
  f_Pcap_file_version_fun_num =
#line 299 "pcap.cmod"
    ADD_FUNCTION2("file_version", f_Pcap_file_version, tFunc(tNone,tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_file_version_defined */

#ifdef f_Pcap_close_defined
  f_Pcap_close_fun_num =
#line 321 "pcap.cmod"
    ADD_FUNCTION2("close", f_Pcap_close, tFunc(tNone,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_close_defined */

#ifdef f_Pcap_open_live_defined
  f_Pcap_open_live_fun_num =
#line 343 "pcap.cmod"
    ADD_FUNCTION2("open_live", f_Pcap_open_live, tFunc(tStr,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_open_live_defined */

#ifdef f_Pcap_open_offline_defined
  f_Pcap_open_offline_fun_num =
#line 385 "pcap.cmod"
    ADD_FUNCTION2("open_offline", f_Pcap_open_offline, tFunc(tStr,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_open_offline_defined */

#ifdef f_Pcap_set_filter_defined
  f_Pcap_set_filter_fun_num =
#line 423 "pcap.cmod"
    ADD_FUNCTION2("set_filter", f_Pcap_set_filter, tFunc(tStr,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_set_filter_defined */

#ifdef f_Pcap_breakloop_defined
  f_Pcap_breakloop_fun_num =
#line 478 "pcap.cmod"
    ADD_FUNCTION2("breakloop", f_Pcap_breakloop, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_breakloop_defined */

#ifdef f_Pcap_loop_defined
  f_Pcap_loop_fun_num =
#line 512 "pcap.cmod"
    ADD_FUNCTION2("loop", f_Pcap_loop, tFunc("\10\200\0\0\0\177\377\377\377" tOr(tMix,tVoid),"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_loop_defined */

#ifdef f_Pcap_dispatch_defined
  f_Pcap_dispatch_fun_num =
#line 549 "pcap.cmod"
    ADD_FUNCTION2("dispatch", f_Pcap_dispatch, tFunc("\10\200\0\0\0\177\377\377\377" tOr(tMix,tVoid),"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_dispatch_defined */

#ifdef f_Pcap_next_defined
  f_Pcap_next_fun_num =
#line 576 "pcap.cmod"
    ADD_FUNCTION2("next", f_Pcap_next, tFunc(tNone,tMapping), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_next_defined */

#ifdef f_Pcap_datalink_defined
  f_Pcap_datalink_fun_num =
#line 611 "pcap.cmod"
    ADD_FUNCTION2("datalink", f_Pcap_datalink, tFunc(tNone,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_datalink_defined */

#ifdef f_Pcap_datalink_val_to_name_defined
  f_Pcap_datalink_val_to_name_fun_num =
#line 627 "pcap.cmod"
    ADD_FUNCTION2("datalink_val_to_name", f_Pcap_datalink_val_to_name, tFunc("\10\200\0\0\0\177\377\377\377",tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Pcap_datalink_val_to_name_defined */
#line 95 "pcap.cmod"
  Pcap_program=end_program();
#line 95 "pcap.cmod"
  Pcap_program_fun_num=add_program_constant("Pcap",Pcap_program,0);

#endif /* class_Pcap_defined */
}
PIKE_MODULE_EXIT {

#ifdef class_Pcap_defined
  if(Pcap_program) {
#line 95 "pcap.cmod"
    free_program(Pcap_program);
    Pcap_program=0;
  }

#endif /* class_Pcap_defined */
}


gotpike.org | Copyright © 2004 - 2019 | Pike is a trademark of Department of Computer and Information Science, Linköping University