/*
 * $Id: interface.c,v 1.1.1.1 2001/10/15 06:54:53 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.
 * 
 */


#include <Python.h>
#include <libnet.h>
#include <errno.h>
#include "pylibnet.h"

static char ebuf[LIBNET_ERRBUF_SIZE];

interface *new_interface()
{
  interface *self;
  int status;

  self = (interface *)malloc(sizeof(interface));
  if (!self)
    throw_exception(1,"malloc()");
  self->link=NULL;
  self->fd=0;
  self->device=NULL;

  return self;
}

void delete_interface(interface *self)
{
  if (self->link)
    libnet_close_link_interface(self->link);
  if (self->link) 
  /* if libnet_close_link_interface didn't free the link ptr, do it here */
    free(self->link);
  if (self->fd)
    libnet_close_raw_sock(self->fd);
  if (self->device)
    free(self->device);

  free(self);
}

void interface_open_link(interface *self, char *device)
{
  self->link = libnet_open_link_interface(device, ebuf);

  self->device=(char *)malloc(strlen(device));
  strcpy(self->device,device);
  
  if (!self->link)
    throw_exception(1,ebuf);
  
}

void interface_open_raw(interface *self, int protocol)
{
  self->fd = libnet_open_raw_sock(protocol);
  if (self->fd==-1)
    throw_exception(errno,strerror(errno));
}


void interface_write (interface *self, PyObject *pkt)
{
  int status;

  if ((pkt!=Py_None) && !PyString_Check(pkt)) {
    PyErr_SetString(PyExc_TypeError,"expected a string");
  }

  if (self->fd) {
    status=libnet_write_ip(self->fd, 
                           PyString_AsString(pkt),
                           PyString_Size(pkt));
    if ((status<0) && (status!=PyString_Size(pkt))) 
      PyErr_SetString(PyExc_IOError,"libnet_write_ip");
  }
  else if (self->link) {
    status=libnet_write_link_layer(self->link,
                                   self->device,
                                   PyString_AsString(pkt),
                                   PyString_Size(pkt)); 
    if ((status<0) && (status!=PyString_Size(pkt))) 
      PyErr_SetString(PyExc_IOError,"libnet_write_link_layer");
  }

}


u_long interface_get_ipaddr(interface *self)
{
  u_long addr;
 
  if (!self->link) {
    throw_exception(-1,"interface must first be opened with open_link()");
    return -1;
  }

  addr = libnet_get_ipaddr(self->link, self->device, ebuf);

  if (addr < 0) {
    throw_exception(addr, ebuf);
    return -1;
  }

  return addr;
}



PyObject *interface_get_hwaddr(interface *self)
{
  struct ether_addr *addr;

  if (!self->link) {
    throw_exception(-1,"interface must first be opened with open_link()");
    return NULL;
  }

  addr = libnet_get_hwaddr(self->link, self->device, ebuf);

  if (addr == NULL) {
    throw_exception(-1, ebuf);
    return NULL;
  }

  return PyString_FromStringAndSize(addr->ether_addr_octet,6);
}


syntax highlighted by Code2HTML, v. 0.9.1