/*
 * $Id: libnet.i,v 1.1.1.1 2001/10/15 06:54:54 davidma Exp $
 *
 * Python libnet
 * Copyright (C) 2001, David Margrave (davidma@eskimo.com)
 * 
 * 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.
 * 
 */

%module libnetc

%{
#include <libnet.h>
#include "pylibnet.h"
%}

%include constants.i

typedef unsigned short u_short;
typedef unsigned long u_long;
typedef unsigned char u_char;


%init %{
  /* d is the dictionary for the current module */
  init_errors(d);

%}

%except (python) {
  int err;
  clear_exception();
  PyErr_Clear();
  $function
  if ((err = check_exception())) {
    set_error(err, get_exception_message());
    return NULL;
  }
  else if(PyErr_Occurred()) {
    return NULL;
  }
}


/* typemaps */

%typemap(python, in) u_char *{
  if (!($target=PyString_AsString($source))) {
    PyErr_SetString(PyExc_TypeError,"expected a string");
    return NULL;
  }
}

%typemap(python, out) u_char *{
  $target=PyString_FromString($source);
}

%typemap(python, in) u_long {
  $target=PyLong_AsUnsignedLong($source);
}

%typemap(python, out) u_long {
  $target=PyLong_FromUnsignedLong($source);
}


/* let functions return raw python objects */
%typemap(python, out) PyObject * {
  $target = $source;
}

/* let functions take raw python objects */
%typemap(python, in) PyObject * {
  $target = $source;
}

typedef struct {
  struct libnet_link_int *link;
  int fd;
  char *device;
  %addmethods {
    interface();
    ~interface();
    void open_link(char *device);
    void open_raw(int protocol);
    void write (PyObject *pkt);
    u_long get_ipaddr();
    PyObject *get_hwaddr();
  }
} interface;


/* packet builder routines */
PyObject *build_arp(u_short hrd, u_short pro, u_char hln, u_char pln,
                    u_short op, u_char *sha, u_char *spa, u_char *tha,
                    u_char *tpa);
PyObject *build_dns(u_short id, u_short flags, u_short num_q,
                    u_short num_anws_rr, u_short num_auth_rr,
                    u_short num_addi_rr);
PyObject *build_ethernet(u_char *dst, u_char *src, u_short type);

PyObject *build_icmp_echo(u_char type, u_char code, u_short id, u_short seq);
PyObject *build_icmp_mask(u_char type, u_char code, u_short id, u_short seq,
                         u_long mask);
PyObject *build_icmp_unreach(u_char type, u_char code, u_short orig_len,
        u_char orig_tos, u_short orig_id, u_short orig_frag, u_char orig_ttl,
        u_char orig_prot, u_long orig_src, u_long orig_dst, PyObject *payload);
PyObject *build_icmp_timeexceed(u_char type, u_char code, u_short orig_len,
        u_char orig_tos, u_short orig_id, u_short orig_frag, u_char orig_ttl,
        u_char orig_prot, u_long orig_src, u_long orig_dst, PyObject *payload);
PyObject *build_icmp_timestamp(u_char type,u_char code,u_short id,u_short seq,
        n_time otime, n_time rtime, n_time ttime);
PyObject *build_icmp_redirect(u_char type, u_char code, u_long gateway,
            u_short orig_len, u_char orig_tos, u_short orig_id,
            u_short orig_frag, u_char orig_ttl, u_char orig_prot,
            u_long orig_src, u_long orig_dst, PyObject *payload);





PyObject *build_ip(u_short len, u_char tos, u_short id, u_short frag,
                          u_char ttl, u_char prot, u_long src, u_long dst);
/* ospf etc. will be added later */
PyObject *build_tcp(u_short sp, u_short dp, u_long seq, u_long ack,
                           u_char control, u_short win, u_short urg);
PyObject *build_udp(u_short sp, u_short dp);


void do_checksum(PyObject *pkt, int protocol, int packet_size);
u_char *select_device(PyObject *device);
u_char *host_lookup(u_long in, u_short use_name);
u_long name_resolve(u_char *hostname, u_short use_name);


syntax highlighted by Code2HTML, v. 0.9.1