/* * $Id: libnet.c,v 1.2 2001/10/15 07:00:17 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 #include #include #include "pylibnet.h" static char ebuf[LIBNET_ERRBUF_SIZE]; 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 *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_ARP_H); status=libnet_build_arp(hrd, pro, hln, pln, op, sha, spa, tha, tpa, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_arp"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_ARP_H); free(buf); return pkt; } 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 *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_DNS_H); status=libnet_build_dns(id, flags, num_q, num_anws_rr, num_auth_rr, num_addi_rr, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_dns"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_DNS_H); free(buf); return pkt; } PyObject *build_ethernet(u_char *dst, u_char *src, u_short type) { PyObject *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_ETH_H); status=libnet_build_ethernet(dst, src, type, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_ethernet"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_ETH_H); free(buf); return pkt; } PyObject *build_icmp_echo(u_char type, u_char code, u_short id, u_short seq) { PyObject *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_ICMP_H); status=libnet_build_icmp_echo(type, code, id, seq, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_icmp_echo"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_ICMP_H); free(buf); return pkt; } PyObject *build_icmp_mask(u_char type, u_char code, u_short id, u_short seq, u_long mask) { PyObject *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_ICMP_MASK_H); status=libnet_build_icmp_mask(type, code, id, seq, mask, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_icmp_mask"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_ICMP_MASK_H); free(buf); return pkt; } 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 *pkt; unsigned char *buf; int status; unsigned int payloadlen; unsigned int buflen; if ((payload!=Py_None) && !PyString_Check(payload)) { PyErr_SetString(PyExc_TypeError,"expected a string"); return NULL; } if (payload==Py_None) payloadlen=0; else payloadlen=PyString_Size(payload); buflen=LIBNET_ICMP_UNREACH_H+4*orig_len+payloadlen; buf = (unsigned char *)malloc(buflen); status=libnet_build_icmp_unreach(type, code, orig_len, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_src, orig_dst, payloadlen ? PyString_AsString(payload):NULL, payloadlen, buf); if (status==-1) { throw_exception(status, "libnet_build_icmp_unreach"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, buflen); free(buf); return pkt; } 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 *pkt; unsigned char *buf; int status; unsigned int payloadlen; unsigned int buflen; if ((payload!=Py_None) && !PyString_Check(payload)) { PyErr_SetString(PyExc_TypeError,"expected a string"); return NULL; } if (payload==Py_None) payloadlen=0; else payloadlen=PyString_Size(payload); buflen=LIBNET_ICMP_TIMXCEED_H+4*orig_len+payloadlen; buf = (unsigned char *)malloc(buflen); status=libnet_build_icmp_timeexceed(type, code, orig_len, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_src, orig_dst, payloadlen ? PyString_AsString(payload):NULL, payloadlen, buf); if (status==-1) { throw_exception(status, "libnet_build_icmp_echo"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, buflen); free(buf); return pkt; } 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 *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_ICMP_TS_H); status=libnet_build_icmp_timestamp(type, code, id, seq, otime, rtime, ttime, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_icmp_timestamp"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_ICMP_TS_H); free(buf); return pkt; } 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 *pkt; unsigned char *buf; int status; unsigned int payloadlen; unsigned int buflen; if ((payload!=Py_None) && !PyString_Check(payload)) { PyErr_SetString(PyExc_TypeError,"expected a string"); return NULL; } if (payload==Py_None) payloadlen=0; else payloadlen=PyString_Size(payload); buflen=LIBNET_ICMP_REDIRECT_H+4*orig_len+payloadlen; buf = (unsigned char *)malloc(buflen); status=libnet_build_icmp_redirect(type, code, gateway, orig_len, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_src, orig_dst , payloadlen ? PyString_AsString(payload):NULL, payloadlen, buf); if (status==-1) { throw_exception(status, "libnet_build_icmp_redirect"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, buflen); free(buf); return pkt; } 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) { PyObject *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_IP_H); status=libnet_build_ip(len, tos, id, frag, ttl, prot, src, dst, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_ip"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_IP_H); free(buf); return pkt; } 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 *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_TCP_H); status=libnet_build_tcp(sp, dp, seq, ack, control, win, urg, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_tcp"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_TCP_H); free(buf); return pkt; } PyObject *build_udp(u_short sp, u_short dp) { PyObject *pkt; unsigned char *buf; int status; buf = (unsigned char *)malloc(LIBNET_UDP_H); status=libnet_build_udp(sp,dp, NULL, 0, buf); if (status==-1) { throw_exception(status, "libnet_build_udp"); free(buf); return NULL; } pkt=PyString_FromStringAndSize(buf, LIBNET_UDP_H); free(buf); return pkt; } void do_checksum(PyObject *pkt, int protocol, int packet_size) { int status; if (!PyString_Check(pkt)) { PyErr_SetString(PyExc_TypeError,"expected a string"); return; } status = libnet_do_checksum(PyString_AsString(pkt), protocol, packet_size); if (status==-1) { throw_exception(status, "libnet_do_checksum"); return; } } u_char *select_device(PyObject *device) { int status; char *dev; struct sockaddr_in sin; if ((device!=Py_None) && !PyString_Check(device)) { PyErr_SetString(PyExc_TypeError,"expected a string"); return NULL; } if (device==Py_None) dev=NULL; else dev=PyString_AsString(device); status = libnet_select_device(&sin, &dev, ebuf); if (status<0) { throw_exception(status, ebuf); return NULL; } return dev; } u_char *host_lookup(u_long in, u_short use_name) { return libnet_host_lookup(in, use_name); } u_long name_resolve(u_char *hostname, u_short use_name) { return libnet_name_resolve(hostname, 1); }