SHARE
TWEET

Untitled

a guest Jun 26th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     PyLorcon2 - Python bindings for Lorcon2 library
  3.     Copyright (C) 2010  Core Security Technologies
  4.  
  5.     This file is part of PyLorcon2.
  6.  
  7.     This program is free software: you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation, either version 3 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20.     Author: Andres Blanco (6e726d)     <6e726d@gmail.com>
  21.     Author: Ezequiel Gutesman (gutes)  <egutesman@gmail.com>
  22. */
  23.  
  24.  
  25. #include <python3.5/Python.h>
  26. #include <lorcon2/lorcon.h>
  27. #include <lorcon2/lorcon_packet.h>
  28. #include <lorcon2/lorcon_multi.h>
  29. #include "PyLorcon2.h"
  30.  
  31.  
  32. /*
  33.     ###########################################################################
  34.    
  35.     Module functions
  36.    
  37.     ###########################################################################
  38. */
  39.  
  40. PyDoc_STRVAR(PyLorcon2_get_version__doc__,
  41.     "get_version() -> integer\n\n"
  42.     "Return the lorcon2-version in the format YYYYMMRR (year-month-release #)");
  43.  
  44. static PyObject*
  45. PyLorcon2_get_version(PyObject *self, PyObject *args);
  46.  
  47.  
  48. PyDoc_STRVAR(PyLorcon2_list_drivers__doc__,
  49.     "list_drivers() -> list\n\n"
  50.     "Return a list of tuples describing the supported drivers");
  51. static PyObject*
  52. PyLorcon2_list_drivers(PyObject *self, PyObject *args);
  53.  
  54. PyDoc_STRVAR(PyLorcon2_find_driver__doc__,
  55.     "find_driver(string) -> tuple\n\n"
  56.     "Return a tuple with driver name and description");
  57.  
  58. static PyObject*
  59. PyLorcon2_find_driver(PyObject *self, PyObject *args);
  60.  
  61. PyDoc_STRVAR(PyLorcon2_auto_driver__doc__,
  62.     "auto_driver(string) -> tuple\n\n"
  63.     "Return a tuple with the driver name and description");
  64.  
  65. static PyObject*
  66. PyLorcon2_auto_driver(PyObject *self, PyObject *args);
  67.  
  68.  
  69. /*
  70.     ###########################################################################
  71.    
  72.     Class PyLorcon2
  73.    
  74.     ###########################################################################
  75. */
  76.  
  77. static void
  78. PyLorcon2_Context_dealloc(PyLorcon2_Context *self);
  79.  
  80. static int
  81. PyLorcon2_Context_init(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  82.  
  83. PyDoc_STRVAR(PyLorcon2_Context_open_inject__doc__,
  84.     "open_inject() -> None\n\n"
  85.     "Set context to injection-mode");
  86. static PyObject*
  87. PyLorcon2_Context_open_inject(PyLorcon2_Context *self);
  88.  
  89. PyDoc_STRVAR(PyLorcon2_Context_open_monitor__doc__,
  90.     "open_monitor() -> None\n\n"
  91.     "Set context to monitor-mode");
  92. static PyObject*
  93. PyLorcon2_Context_open_monitor(PyLorcon2_Context *self);
  94.  
  95.  
  96. PyDoc_STRVAR(PyLorcon2_Context_open_injmon__doc__,
  97.     "open_injmon() -> None\n\n"
  98.     "Set context to injection- and monitor-mode");
  99. static PyObject*
  100. PyLorcon2_Context_open_injmon(PyLorcon2_Context *self);
  101.  
  102. PyDoc_STRVAR(PyLorcon2_Context_close__doc__,
  103.     "close() -> None\n\n"
  104.     "Close context");
  105. static PyObject*
  106. PyLorcon2_Context_close(PyLorcon2_Context *self);
  107.  
  108. PyDoc_STRVAR(PyLorcon2_Context_get_error__doc__,
  109.     "get_error() -> string\n\n"
  110.     "Return last error message generated for this context");
  111. static PyObject*
  112. PyLorcon2_Context_get_error(PyLorcon2_Context *self);
  113.  
  114. PyDoc_STRVAR(PyLorcon2_Context_get_capiface__doc__,
  115.     "get_capiface() -> string\n\n"
  116.     "Return the interface for this context");
  117. static PyObject*
  118. PyLorcon2_Context_get_capiface(PyLorcon2_Context *self);
  119.  
  120. PyDoc_STRVAR(PyLorcon2_Context_send_bytes__doc__,
  121.     "send_bytes(object) -> integer\n\n"
  122.     "Send the string-representation of the given object");
  123. static PyObject*
  124. PyLorcon2_Context_send_bytes(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  125.  
  126. PyDoc_STRVAR(PyLorcon2_Context_set_timeout__doc__,
  127.     "set_timeout(integer) -> None\n\n"
  128.     "Set the timeout for this context");
  129. static PyObject*
  130. PyLorcon2_Context_set_timeout(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  131.  
  132. PyDoc_STRVAR(PyLorcon2_Context_get_timeout__doc__,
  133.     "get_timeout() -> integer\n\n"
  134.     "Get the timeout for this context");
  135. static PyObject*
  136. PyLorcon2_Context_get_timeout(PyLorcon2_Context *self);
  137.  
  138. PyDoc_STRVAR(PyLorcon2_Context_set_vap__doc__,
  139.     "set_vap() -> string\n\n"
  140.     "Set the vap for this context");
  141. static PyObject*
  142. PyLorcon2_Context_set_vap(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  143.  
  144. PyDoc_STRVAR(PyLorcon2_Context_get_vap__doc__,
  145.     "get_vap() -> string\n\n"
  146.     "Get the vap for this context");
  147. static PyObject*
  148. PyLorcon2_Context_get_vap(PyLorcon2_Context *self);
  149.  
  150. PyDoc_STRVAR(PyLorcon2_Context_get_driver_name__doc__,
  151.     "get_driver_name() -> string\n\n"
  152.     "Get the driver-name for this context");
  153. static PyObject*
  154. PyLorcon2_Context_get_driver_name(PyLorcon2_Context *self);
  155.  
  156. PyDoc_STRVAR(PyLorcon2_Context_set_channel__doc__,
  157.     "set_channel(integer) -> None\n\n"
  158.     "Set the channel for this context");
  159. static PyObject*
  160. PyLorcon2_Context_set_channel(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  161.  
  162. PyDoc_STRVAR(PyLorcon2_Context_get_channel__doc__,
  163.     "get_channel() -> integer\n\n"
  164.     "Get the channel for this context");
  165. static PyObject*
  166. PyLorcon2_Context_get_channel(PyLorcon2_Context *self);
  167.  
  168. PyDoc_STRVAR(PyLorcon2_Context_get_hwmac__doc__,
  169.     "get_hwmac() -> tuple\n\n"
  170.     "Get the hardware MAC for this context");
  171. static PyObject*
  172. PyLorcon2_Context_get_hwmac(PyLorcon2_Context *self);
  173.  
  174. PyDoc_STRVAR(PyLorcon2_Context_set_hwmac__doc__,
  175.     "set_hwmac(tuple) -> None\n\n"
  176.     "Set the hardware MAC for this context");
  177. static PyObject*
  178. PyLorcon2_Context_set_hwmac(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  179.  
  180. PyDoc_STRVAR(PyLorcon2_Context_get_next__doc__,
  181.     "set_packet_callback() -> packet\n\n"
  182.     "Fetch the next packet (blocking)");
  183. static PyObject *
  184. PyLorcon2_Context_get_next(PyLorcon2_Context *self);
  185.  
  186. PyDoc_STRVAR(PyLorcon2_Context_set_filter__doc__,
  187.     "set_filter(object) -> integer\n\n"
  188.     "Set a pcap BPF filter");
  189. static PyObject*
  190. PyLorcon2_Context_set_filter(PyLorcon2_Context *self, PyObject *args, PyObject *kwds);
  191.  
  192. /*
  193.     ###########################################################################
  194.    
  195.     Packet access
  196.    
  197.     ###########################################################################
  198. */
  199.  
  200. PyDoc_STRVAR(PyLorcon2Packet_get_time_sec__doc__,
  201.     "get_time_sec() -> integer\n\n"
  202.     "Return the packet timestamp, seconds");
  203. static PyObject*
  204. PyLorcon2_Packet_get_time_sec(PyLorcon2_Packet *self);
  205.  
  206. PyDoc_STRVAR(PyLorcon2Packet_get_time_usec__doc__,
  207.     "get_time_usec() -> integer\n\n"
  208.     "Return the packet timestamp, useconds");
  209. static PyObject*
  210. PyLorcon2_Packet_get_time_usec(PyLorcon2_Packet *self);
  211.  
  212. PyDoc_STRVAR(PyLorcon2Packet_get_length__doc__,
  213.     "get_length() -> integer\n\n"
  214.     "Return the packet record total length including per-packet headers");
  215. static PyObject*
  216. PyLorcon2_Packet_get_length(PyLorcon2_Packet *self);
  217.  
  218. PyDoc_STRVAR(PyLorcon2Packet_get_dot11_length__doc__,
  219.     "get_header_length() -> integer\n\n"
  220.     "Return the packet record length after removing per-packet headers");
  221. static PyObject*
  222. PyLorcon2_Packet_get_dot11_length(PyLorcon2_Packet *self);
  223.  
  224. PyDoc_STRVAR(PyLorcon2Packet_get_payload_length__doc__,
  225.     "get_payload_length() -> integer\n\n"
  226.     "Return the packet record data component length");
  227. static PyObject*
  228. PyLorcon2_Packet_get_payload_length(PyLorcon2_Packet *self);
  229.  
  230. PyDoc_STRVAR(PyLorcon2Packet_get_packet__doc__,
  231.     "get_packet() -> array\n\n"
  232.     "Return an array of the total packet as captured");
  233. static PyObject*
  234. PyLorcon2_Packet_get_packet(PyLorcon2_Packet *self);
  235.  
  236. PyDoc_STRVAR(PyLorcon2Packet_get_dot11__doc__,
  237.     "get_packet_dot11() -> array\n\n"
  238.     "Return an array of the packet starting at the dot11 header");
  239. static PyObject*
  240. PyLorcon2_Packet_get_dot11(PyLorcon2_Packet *self);
  241.  
  242. PyDoc_STRVAR(PyLorcon2Packet_get_data_payload__doc__,
  243.     "get_packet_data_payload() -> array\n\n"
  244.     "Return an array of the packet starting at the data payload");
  245. static PyObject*
  246. PyLorcon2_Packet_get_data_payload(PyLorcon2_Packet *self);
  247.  
  248. PyDoc_STRVAR(PyLorcon2Packet_get_interface__doc__,
  249.     "get_interface() -> PyLorcon2.Context\n\n"
  250.     "Return the context which saw this packet");
  251. static PyObject*
  252. PyLorcon2_Packet_get_interface(PyLorcon2_Packet *self);
  253.  
  254. /* Multi-cap */
  255. static int PyLorcon2_Multi_init(PyLorcon2_Multi *self, PyObject *args, PyObject *kwds);
  256. static void PyLorcon2_Multi_dealloc(PyLorcon2_Multi *self);
  257.  
  258. PyDoc_STRVAR(PyLorcon2Multi_get_error__doc__,
  259.     "get_error() -> String\n\n"
  260.     "Return last error");
  261. static PyObject*
  262. PyLorcon2_Multi_get_error(PyLorcon2_Multi *self);
  263.  
  264. PyDoc_STRVAR(PyLorcon2Multi_add_interface__doc__,
  265.     "add_interface(Lorcon2.Context) -> None\n\n"
  266.     "Add an (opened!) lorcon2 interface to the multicapture");
  267. static PyObject*
  268. PyLorcon2_Multi_add_interface(PyLorcon2_Multi *self, PyObject *args);
  269.  
  270. PyDoc_STRVAR(PyLorcon2Multi_del_interface__doc__,
  271.     "del_interface(Lorcon2.Context) -> None\n\n"
  272.     "Remove an interface from the multicapture");
  273. static PyObject*
  274. PyLorcon2_Multi_del_interface(PyLorcon2_Multi *self, PyObject *args);
  275.  
  276. PyDoc_STRVAR(PyLorcon2Multi_get_interfaces__doc__,
  277.     "get_interfaces() -> vector<Lorcon2.Context>\n\n"
  278.     "Return a list of interfaces in the multicapture");
  279. static PyObject*
  280. PyLorcon2_Multi_get_interfaces(PyLorcon2_Multi *self);
  281.  
  282. PyDoc_STRVAR(PyLorcon2Multi_loop__doc__,
  283.     "loop(count, callback) -> Integer\n\n"
  284.     "Loop on all interfaces, calling the callback function on each "
  285.     "packet.  Callbacks should be:\n"
  286.     "def MultiCallback(packet)\n"
  287.     "Processes `count' packets (0 for infinite)");
  288. static PyObject*
  289. PyLorcon2_Multi_loop(PyLorcon2_Multi *self, PyObject *args);
  290.  
  291. PyDoc_STRVAR(PyLorcon2Multi_get_multi_ptr__doc__,
  292.     "get_multi_ptr() -> Capsule Object\n\n"
  293.     "Return a C pointer to the multicapture internal object.  This exists "
  294.     "solely to integrate with other native libraries via python native "
  295.     "glue and should not be called from Python code.");
  296. static PyObject*
  297. PyLorcon2_Multi_get_multi_ptr(PyLorcon2_Multi *self);
  298.  
  299. PyDoc_STRVAR(PyLorcon2Multi_set_interface_error_cb__doc__,
  300.     "set_interface_error_cb(callback) -> None\n\n"
  301.     "Set a callback function to be called if an interface encounters an "
  302.     "error.  Callbacks should be:\n"
  303.     "def ErrorCallback(Lorcon2.Multi, Lorcon2.Context)");
  304. static PyObject *
  305. PyLorcon2_Multi_set_interface_error_cb(PyLorcon2_Multi *self, PyObject *args);
  306.  
  307. PyDoc_STRVAR(PyLorcon2Multi_remove_interface_error_cb__doc__,
  308.     "remove_interface_error_cb() -> None\n\n"
  309.     "Remove error callback");
  310. static PyObject *
  311. PyLorcon2_Multi_remove_interface_error_cb(PyLorcon2_Multi *self, PyObject *args);
  312.  
  313. /*
  314.     ###########################################################################
  315.    
  316.     Class PyLorcon2Packet
  317.    
  318.     ###########################################################################
  319. */
  320.  
  321. static void
  322. PyLorcon2_Packet_dealloc(PyLorcon2_Packet *self);
  323.  
  324. static int
  325. PyLorcon2_Packet_init(PyLorcon2_Packet *self, PyObject *args, PyObject *kwds);
  326.  
  327. /*
  328.     ###########################################################################
  329.    
  330.     Packet Definitions
  331.    
  332.     ###########################################################################
  333. */
  334. static PyMethodDef PyLorcon2_Packet_Methods[] =
  335. {
  336.     {"get_time_sec", (PyCFunction) PyLorcon2_Packet_get_time_sec,
  337.         METH_NOARGS, PyLorcon2Packet_get_time_sec__doc__},
  338.     {"get_time_usec", (PyCFunction) PyLorcon2_Packet_get_time_usec,
  339.         METH_NOARGS, PyLorcon2Packet_get_time_usec__doc__},
  340.     {"get_length", (PyCFunction) PyLorcon2_Packet_get_length,
  341.         METH_NOARGS, PyLorcon2Packet_get_length__doc__},
  342.     {"get_dot11_length", (PyCFunction) PyLorcon2_Packet_get_dot11_length,
  343.         METH_NOARGS, PyLorcon2Packet_get_dot11_length__doc__},
  344.     {"get_payload_length", (PyCFunction) PyLorcon2_Packet_get_payload_length,
  345.         METH_NOARGS, PyLorcon2Packet_get_payload_length__doc__},
  346.     {"get_packet", (PyCFunction) PyLorcon2_Packet_get_packet,
  347.         METH_NOARGS, PyLorcon2Packet_get_packet__doc__},
  348.     {"get_dot11", (PyCFunction) PyLorcon2_Packet_get_dot11,
  349.         METH_NOARGS, PyLorcon2Packet_get_dot11__doc__},
  350.     {"get_data_payload", (PyCFunction) PyLorcon2_Packet_get_data_payload,
  351.         METH_NOARGS, PyLorcon2Packet_get_data_payload__doc__},
  352.     {"get_interface", (PyCFunction) PyLorcon2_Packet_get_interface,
  353.         METH_NOARGS, PyLorcon2Packet_get_interface__doc__},
  354.     { NULL, NULL, 0, NULL }
  355. };
  356.  
  357. static PyTypeObject PyLorcon2_PacketType = {
  358.     PyVarObject_HEAD_INIT(NULL, 0)
  359.     0,                                        /* ob_size */
  360.     "PyLorcon2.Packet",                       /* tp_name */
  361.     sizeof(PyLorcon2_Packet),                 /* tp_basic_size */
  362.     0,                                        /* tp_itemsize */
  363.     (destructor)PyLorcon2_Packet_dealloc,     /* tp_dealloc */
  364.     0,                                        /* tp_print */
  365.     0,                                        /* tp_getattr */
  366.     0,                                        /* tp_setattr */
  367.     0,                                        /* tp_compare */
  368.     0,                                        /* tp_repr */
  369.     0,                                        /* tp_as_number */
  370.     0,                                        /* tp_as_sequence */
  371.     0,                                        /* tp_as_mapping */
  372.     0,                                        /* tp_hash */
  373.     0,                                        /* tp_call */
  374.     0,                                        /* tp_str */
  375.     0,                                        /* tp_getattro */
  376.     0,                                        /* tp_setattro */
  377.     0,                                        /* tp_as_buffer */
  378.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  379.     "PyLorcon2 Packet Object",                /* tp_doc */
  380.     0,                                        /* tp_traverse */
  381.     0,                                        /* tp_clear */
  382.     0,                                        /* tp_richcompare */
  383.     0,                                        /* tp_weaklistoffset */
  384.     0,                                        /* tp_iter */
  385.     0,                                        /* tp_iternext */
  386.     PyLorcon2_Packet_Methods,                 /* tp_methods */
  387.     0,                                        /* tp_members */
  388.     0,                                        /* tp_getset */
  389.     0,                                        /* tp_base */
  390.     0,                                        /* tp_dict */
  391.     0,                                        /* tp_descr_get */
  392.     0,                                        /* tp_descr_set */
  393.     0,                                        /* tp_dictoffset */
  394.     (initproc)PyLorcon2_Packet_init,          /* tp_init */
  395.     0,                                        /* tp_alloc */
  396.     0,                                        /* tp_new */
  397. };
  398.  
  399. /*
  400.     ###########################################################################
  401.    
  402.     Packet Definitions
  403.    
  404.     ###########################################################################
  405. */
  406. static PyMethodDef PyLorcon2_Multi_Methods[] =
  407. {
  408.     {"get_error", (PyCFunction) PyLorcon2_Multi_get_error,
  409.         METH_NOARGS, PyLorcon2Multi_get_error__doc__},
  410.     {"add_interface", (PyCFunction) PyLorcon2_Multi_add_interface,
  411.         METH_VARARGS, PyLorcon2Multi_add_interface__doc__ },
  412.     {"del_interface", (PyCFunction) PyLorcon2_Multi_del_interface,
  413.         METH_VARARGS, PyLorcon2Multi_del_interface__doc__ },
  414.     {"get_interfaces", (PyCFunction) PyLorcon2_Multi_get_interfaces,
  415.         METH_NOARGS, PyLorcon2Multi_get_interfaces__doc__ },
  416.     {"loop", (PyCFunction) PyLorcon2_Multi_loop,
  417.         METH_VARARGS, PyLorcon2Multi_loop__doc__ },
  418.     {"get_multi_ptr", (PyCFunction) PyLorcon2_Multi_get_multi_ptr,
  419.         METH_NOARGS, PyLorcon2Multi_get_multi_ptr__doc__ },
  420.     {"set_interface_error_cb", (PyCFunction) PyLorcon2_Multi_set_interface_error_cb,
  421.         METH_VARARGS, PyLorcon2Multi_set_interface_error_cb__doc__ },
  422.     {"remove_interface_error_cb",
  423.         (PyCFunction) PyLorcon2_Multi_remove_interface_error_cb,
  424.         METH_VARARGS, PyLorcon2Multi_remove_interface_error_cb__doc__ },
  425.     { NULL, NULL, 0, NULL }
  426. };
  427.  
  428. static PyTypeObject PyLorcon2_MultiType = {
  429.     PyVarObject_HEAD_INIT(NULL, 0)
  430.     0,                                        /* ob_size */
  431.     "PyLorcon2.Multi",                        /* tp_name */
  432.     sizeof(PyLorcon2_Multi),                  /* tp_basic_size */
  433.     0,                                        /* tp_itemsize */
  434.     (destructor)PyLorcon2_Multi_dealloc,      /* tp_dealloc */
  435.     0,                                        /* tp_print */
  436.     0,                                        /* tp_getattr */
  437.     0,                                        /* tp_setattr */
  438.     0,                                        /* tp_compare */
  439.     0,                                        /* tp_repr */
  440.     0,                                        /* tp_as_number */
  441.     0,                                        /* tp_as_sequence */
  442.     0,                                        /* tp_as_mapping */
  443.     0,                                        /* tp_hash */
  444.     0,                                        /* tp_call */
  445.     0,                                        /* tp_str */
  446.     0,                                        /* tp_getattro */
  447.     0,                                        /* tp_setattro */
  448.     0,                                        /* tp_as_buffer */
  449.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  450.     "PyLorcon2 Multicap",                     /* tp_doc */
  451.     0,                                        /* tp_traverse */
  452.     0,                                        /* tp_clear */
  453.     0,                                        /* tp_richcompare */
  454.     0,                                        /* tp_weaklistoffset */
  455.     0,                                        /* tp_iter */
  456.     0,                                        /* tp_iternext */
  457.     PyLorcon2_Multi_Methods,                  /* tp_methods */
  458.     0,                                        /* tp_members */
  459.     0,                                        /* tp_getset */
  460.     0,                                        /* tp_base */
  461.     0,                                        /* tp_dict */
  462.     0,                                        /* tp_descr_get */
  463.     0,                                        /* tp_descr_set */
  464.     0,                                        /* tp_dictoffset */
  465.     (initproc)PyLorcon2_Multi_init,           /* tp_init */
  466.     0,                                        /* tp_alloc */
  467.     0,                                        /* tp_new */
  468. };
  469.  
  470. /*
  471.     ###########################################################################
  472.    
  473.     Definitions
  474.    
  475.     ###########################################################################
  476. */
  477.  
  478. static PyMethodDef PyLorcon2Methods[] =
  479. {
  480.     {"get_version",  PyLorcon2_get_version,  
  481.         METH_NOARGS,  PyLorcon2_get_version__doc__},
  482.     {"list_drivers", PyLorcon2_list_drivers,
  483.         METH_NOARGS,  PyLorcon2_list_drivers__doc__},
  484.     {"find_driver",  PyLorcon2_find_driver,  
  485.         METH_VARARGS, PyLorcon2_find_driver__doc__},
  486.     {"auto_driver",  PyLorcon2_auto_driver,  
  487.         METH_VARARGS, PyLorcon2_auto_driver__doc__},
  488.     {NULL, NULL, 0, NULL}
  489. };
  490.  
  491. static PyMethodDef PyLorcon2_Context_Methods[] =
  492. {
  493.     {"open_inject",     (PyCFunction)PyLorcon2_Context_open_inject,     METH_NOARGS,  PyLorcon2_Context_open_inject__doc__},
  494.     {"open_monitor",    (PyCFunction)PyLorcon2_Context_open_monitor,    METH_NOARGS,  PyLorcon2_Context_open_monitor__doc__},
  495.     {"open_injmon",     (PyCFunction)PyLorcon2_Context_open_injmon,     METH_NOARGS,  PyLorcon2_Context_open_injmon__doc__},
  496.     {"close",           (PyCFunction)PyLorcon2_Context_close,           METH_NOARGS,  PyLorcon2_Context_close__doc__},
  497.     {"get_error",       (PyCFunction)PyLorcon2_Context_get_error,       METH_NOARGS,  PyLorcon2_Context_get_error__doc__},
  498.     {"get_capiface",    (PyCFunction)PyLorcon2_Context_get_capiface,    METH_NOARGS,  PyLorcon2_Context_get_capiface__doc__},
  499.     {"send_bytes",      (PyCFunction)PyLorcon2_Context_send_bytes,      METH_VARARGS, PyLorcon2_Context_send_bytes__doc__},
  500.     {"set_filter",      (PyCFunction)PyLorcon2_Context_set_filter,      METH_VARARGS, PyLorcon2_Context_set_filter__doc__},
  501.     {"set_timeout",     (PyCFunction)PyLorcon2_Context_set_timeout,
  502.                         METH_VARARGS | METH_KEYWORDS, PyLorcon2_Context_set_timeout__doc__},
  503.     {"get_timeout",     (PyCFunction)PyLorcon2_Context_get_timeout,     METH_NOARGS,  PyLorcon2_Context_get_timeout__doc__},
  504.     {"set_vap",         (PyCFunction)PyLorcon2_Context_set_vap,
  505.                         METH_VARARGS | METH_KEYWORDS, PyLorcon2_Context_set_vap__doc__},
  506.     {"get_vap",         (PyCFunction)PyLorcon2_Context_get_vap,         METH_NOARGS,  PyLorcon2_Context_get_vap__doc__},
  507.     {"get_driver_name", (PyCFunction)PyLorcon2_Context_get_driver_name, METH_NOARGS,  PyLorcon2_Context_get_driver_name__doc__},
  508.     {"set_channel",     (PyCFunction)PyLorcon2_Context_set_channel,     METH_VARARGS, PyLorcon2_Context_set_channel__doc__},
  509.     {"get_channel",     (PyCFunction)PyLorcon2_Context_get_channel,     METH_NOARGS,  PyLorcon2_Context_get_channel__doc__},
  510.     {"set_hwmac",       (PyCFunction)PyLorcon2_Context_set_hwmac,       METH_VARARGS, PyLorcon2_Context_set_hwmac__doc__},
  511.     {"get_hwmac",       (PyCFunction)PyLorcon2_Context_get_hwmac,       METH_NOARGS,  PyLorcon2_Context_get_hwmac__doc__},
  512.     {"get_next",        (PyCFunction)PyLorcon2_Context_get_next,        METH_NOARGS,  PyLorcon2_Context_get_next__doc__},
  513.     {NULL, NULL, 0, NULL}
  514. };
  515.  
  516. static PyTypeObject PyLorcon2_ContextType = {
  517.     PyVarObject_HEAD_INIT(NULL, 0)
  518.     0,                                        /* ob_size */
  519.     "PyLorcon2.Context",                      /* tp_name */
  520.     sizeof(PyLorcon2_Context),                /* tp_basic_size */
  521.     0,                                        /* tp_itemsize */
  522.     (destructor)PyLorcon2_Context_dealloc,    /* tp_dealloc */
  523.     0,                                        /* tp_print */
  524.     0,                                        /* tp_getattr */
  525.     0,                                        /* tp_setattr */
  526.     0,                                        /* tp_compare */
  527.     0,                                        /* tp_repr */
  528.     0,                                        /* tp_as_number */
  529.     0,                                        /* tp_as_sequence */
  530.     0,                                        /* tp_as_mapping */
  531.     0,                                        /* tp_hash */
  532.     0,                                        /* tp_call */
  533.     0,                                        /* tp_str */
  534.     0,                                        /* tp_getattro */
  535.     0,                                        /* tp_setattro */
  536.     0,                                        /* tp_as_buffer */
  537.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  538.     "PyLorcon2 Context Object",               /* tp_doc */
  539.     0,                                        /* tp_traverse */
  540.     0,                                        /* tp_clear */
  541.     0,                                        /* tp_richcompare */
  542.     0,                                        /* tp_weaklistoffset */
  543.     0,                                        /* tp_iter */
  544.     0,                                        /* tp_iternext */
  545.     PyLorcon2_Context_Methods,                /* tp_methods */
  546.     0,                                        /* tp_members */
  547.     0,                                        /* tp_getset */
  548.     0,                                        /* tp_base */
  549.     0,                                        /* tp_dict */
  550.     0,                                        /* tp_descr_get */
  551.     0,                                        /* tp_descr_set */
  552.     0,                                        /* tp_dictoffset */
  553.     (initproc)PyLorcon2_Context_init,         /* tp_init */
  554.     0,                                        /* tp_alloc */
  555.     0,                                        /* tp_new */
  556. };
  557.  
  558.  
  559. /*
  560.     ###########################################################################
  561.    
  562.     Module initialization
  563.    
  564.     ###########################################################################
  565. */
  566.  
  567. PyMODINIT_FUNC
  568. PyInit_PyLorcon2(void)
  569. {
  570.     PyObject *m;
  571.  
  572.     if(PyType_Ready(&PyLorcon2_ContextType) < 0)
  573.         return NULL;
  574.  
  575.     if(PyType_Ready(&PyLorcon2_PacketType) < 0)
  576.         return NULL;
  577.  
  578.     if(PyType_Ready(&PyLorcon2_MultiType) < 0)
  579.         return NULL;
  580.    
  581.     static struct PyModuleDef moduledef = {
  582.         PyModuleDef_HEAD_INIT,
  583.         "PyLorcon2",
  584.         "Wrapper for the Lorcon2 library",
  585.         -1,
  586.         PyLorcon2Methods,
  587.         NULL,
  588.         NULL,
  589.         NULL,
  590.         NULL
  591.     };
  592.     m = PyModule_Create(&moduledef);
  593.  
  594.     if(m == NULL)
  595.         return NULL;
  596.  
  597.     /* Lorcon2 Exception */
  598.     Lorcon2Exception = PyErr_NewException("PyLorcon2.Lorcon2Exception", NULL, NULL);
  599.     Py_INCREF(Lorcon2Exception);
  600.     PyModule_AddObject(m, "Lorcon2Exception", Lorcon2Exception);
  601.  
  602.     /* Lorcon2 Context Object */
  603.     Py_INCREF(&PyLorcon2_ContextType);
  604.     PyLorcon2_ContextType.tp_getattro = PyObject_GenericGetAttr;
  605.     PyLorcon2_ContextType.tp_setattro = PyObject_GenericSetAttr;
  606.     PyLorcon2_ContextType.tp_alloc  = PyType_GenericAlloc;
  607.     PyLorcon2_ContextType.tp_new = PyType_GenericNew;
  608.     PyLorcon2_ContextType.tp_free = PyObject_Del;
  609.     PyModule_AddObject(m, "Context", (PyObject*)&PyLorcon2_ContextType);
  610.  
  611.     /* Lorcon2 Multicap Object */
  612.     Py_INCREF(&PyLorcon2_MultiType);
  613.     PyLorcon2_MultiType.tp_getattro = PyObject_GenericGetAttr;
  614.     PyLorcon2_MultiType.tp_setattro = PyObject_GenericSetAttr;
  615.     PyLorcon2_MultiType.tp_alloc  = PyType_GenericAlloc;
  616.     PyLorcon2_MultiType.tp_new = PyType_GenericNew;
  617.     PyLorcon2_MultiType.tp_free = PyObject_Del;
  618.     PyModule_AddObject(m, "Multi", (PyObject*)&PyLorcon2_MultiType);
  619.  
  620.     /* Lorcon2 Packet Object */
  621.     Py_INCREF(&PyLorcon2_PacketType);
  622.     PyLorcon2_PacketType.tp_getattro = PyObject_GenericGetAttr;
  623.     PyLorcon2_PacketType.tp_setattro = PyObject_GenericSetAttr;
  624.     PyLorcon2_PacketType.tp_alloc  = PyType_GenericAlloc;
  625.     PyLorcon2_PacketType.tp_new = PyType_GenericNew;
  626.     PyLorcon2_PacketType.tp_free = PyObject_Del;
  627.     PyModule_AddObject(m, "Packet", (PyObject*)&PyLorcon2_PacketType);
  628.  
  629.     return m;
  630. }
  631.  
  632. static PyObject*
  633. PyLorcon2_get_version(PyObject *self, PyObject *args)
  634. {
  635.     return PyLong_FromLong(lorcon_get_version());
  636. }
  637.  
  638. static PyObject*
  639. PyLorcon2_list_drivers(PyObject *self, PyObject *args)
  640. {
  641.     PyObject *retval, *entry;
  642.     lorcon_driver_t *driver_list, *driver;
  643.    
  644.     driver = driver_list = lorcon_list_drivers();
  645.     if (!driver) {
  646.         PyErr_SetString(Lorcon2Exception, "Unable to get driver-list");
  647.         return NULL;
  648.     }
  649.  
  650.     retval = PyList_New(0);
  651.     if (!retval) {
  652.         lorcon_free_driver_list(driver_list);
  653.         return PyErr_NoMemory();
  654.     }
  655.  
  656.     while(driver) {
  657.         entry = PyTuple_New(2);
  658.  
  659.         PyTuple_SetItem(entry, 0, PyUnicode_FromString(driver->name));
  660.         PyTuple_SetItem(entry, 1, PyUnicode_FromString(driver->details));
  661.  
  662.         PyList_Append(retval, entry);
  663.         Py_DECREF(entry);
  664.  
  665.         driver = driver->next;
  666.     }
  667.  
  668.     lorcon_free_driver_list(driver_list);
  669.  
  670.     return retval;
  671. }
  672.  
  673. static PyObject*
  674. PyLorcon2_find_driver(PyObject *self, PyObject *args)
  675. {
  676.     char *name;
  677.     PyObject* retval;
  678.     lorcon_driver_t *driver;
  679.  
  680.     if (!PyArg_ParseTuple(args, "s", &name))
  681.         return NULL;
  682.  
  683.     driver = lorcon_find_driver(name);
  684.     if (!driver) {
  685.         PyErr_SetString(Lorcon2Exception, "Unable to get driver-list");
  686.         return NULL;
  687.     }
  688.  
  689.     retval = PyTuple_New(2);
  690.     if (!retval) {
  691.         lorcon_free_driver_list(driver);
  692.         return PyErr_NoMemory();
  693.     }
  694.    
  695.     PyTuple_SetItem(retval, 0, PyUnicode_FromString(driver->name));
  696.     PyTuple_SetItem(retval, 1, PyUnicode_FromString(driver->details));
  697.  
  698.     lorcon_free_driver_list(driver);
  699.  
  700.     return retval;
  701. }
  702.  
  703. static PyObject*
  704. PyLorcon2_auto_driver(PyObject *self, PyObject *args)
  705. {
  706.     char *iface;
  707.     PyObject* retval;
  708.     lorcon_driver_t *driver;
  709.  
  710.     if (!PyArg_ParseTuple(args, "s", &iface))
  711.         return NULL;
  712.  
  713.     driver = lorcon_auto_driver(iface);
  714.     if (!driver) {
  715.         PyErr_SetString(Lorcon2Exception, "Unable to get driver");
  716.         return NULL;
  717.     }
  718.  
  719.     retval = PyTuple_New(2);
  720.     if (!retval) {
  721.         lorcon_free_driver_list(driver);
  722.         return PyErr_NoMemory();
  723.     }
  724.    
  725.     PyTuple_SetItem(retval, 0, PyUnicode_FromString(driver->name));
  726.     PyTuple_SetItem(retval, 1, PyUnicode_FromString(driver->details));
  727.  
  728.     lorcon_free_driver_list(driver);
  729.  
  730.     return retval;
  731. }
  732.  
  733. static void
  734. PyLorcon2_Context_dealloc(PyLorcon2_Context *self)
  735. {
  736.     if(self->context != NULL && self->free_on_cleanup)
  737.         lorcon_free(self->context);
  738.     Py_TYPE(self)->tp_free((PyObject*)self);
  739. }
  740.  
  741. static int
  742. PyLorcon2_Context_init(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  743. {
  744.     lorcon_driver_t *driver;
  745.     static char *kwlist[] = {"iface", "driver", NULL};
  746.     char *iface = NULL, *drivername = NULL;
  747.  
  748.     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ss", kwlist, &iface, &drivername)) {
  749.         self->context = NULL;
  750.         self->monitored = 0;
  751.         return -1;
  752.     }
  753.  
  754.     /* If we didn't get an interface, make a stub context */
  755.     if (iface == NULL) {
  756.         self->context = NULL;
  757.         self->monitored = 0;
  758.         self->free_on_cleanup = 0;
  759.         return 0;
  760.     }
  761.  
  762.     if (drivername == NULL) {
  763.         driver = lorcon_auto_driver(iface);
  764.     } else {
  765.         driver = lorcon_find_driver(drivername);
  766.     }
  767.  
  768.     if (!driver) {
  769.         PyErr_SetString(Lorcon2Exception, "Unable to get driver");
  770.         return -1;
  771.     }
  772.  
  773.     self->context = lorcon_create(iface, driver);
  774.  
  775.     lorcon_free_driver_list(driver);
  776.  
  777.     if (!self->context) {
  778.         PyErr_SetString(Lorcon2Exception, "Unable to create lorcon context");
  779.         return -1;
  780.     }
  781.    
  782.     self->monitored = 0;
  783.     lorcon_set_timeout(self->context, 100);
  784.  
  785.     self->free_on_cleanup = 1;
  786.  
  787.     return 0;
  788. }
  789.  
  790. static PyObject*
  791. PyLorcon2_Context_open_inject(PyLorcon2_Context *self)
  792. {
  793.     if (lorcon_open_inject(self->context) < 0) {
  794.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  795.         return NULL;
  796.     }
  797.  
  798.     self->monitored = 1;
  799.  
  800.     Py_INCREF(Py_None);
  801.     return Py_None;
  802. }
  803.  
  804. static PyObject*
  805. PyLorcon2_Context_open_monitor(PyLorcon2_Context *self)
  806. {
  807.     if (lorcon_open_monitor(self->context) < 0) {
  808.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  809.         return NULL;
  810.     }
  811.    
  812.     self->monitored = 1;
  813.  
  814.     Py_INCREF(Py_None);
  815.     return Py_None;
  816. }
  817.  
  818. static PyObject*
  819. PyLorcon2_Context_open_injmon(PyLorcon2_Context *self)
  820. {
  821.     if (lorcon_open_injmon(self->context) < 0) {
  822.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  823.         return NULL;
  824.     }
  825.    
  826.     self->monitored = 1;
  827.  
  828.     Py_INCREF(Py_None);
  829.     return Py_None;
  830. }
  831.  
  832. static PyObject*
  833. PyLorcon2_Context_close(PyLorcon2_Context *self)
  834. {
  835.     lorcon_close(self->context);
  836.    
  837.     self->monitored = 0;
  838.  
  839.     Py_INCREF(Py_None);
  840.     return Py_None;
  841. }
  842.  
  843. static PyObject*
  844. PyLorcon2_Context_get_error(PyLorcon2_Context *self)
  845. {
  846.     return PyUnicode_FromString(lorcon_get_error(self->context));
  847. }
  848.  
  849. static PyObject*
  850. PyLorcon2_Context_get_capiface(PyLorcon2_Context *self)
  851. {
  852.     return PyUnicode_FromString(lorcon_get_capiface(self->context));
  853. }
  854.  
  855. static PyObject*
  856. PyLorcon2_Context_send_bytes(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  857. {
  858.     char *pckt_buffer;
  859.     ssize_t pckt_size, sent;
  860.     PyObject *pckt, *pckt_string;
  861.  
  862.     if (!PyArg_ParseTuple(args, "O", &pckt))
  863.         return NULL;
  864.    
  865.     if (!self->monitored) {
  866.         PyErr_SetString(PyExc_RuntimeError, "Context must be in monitor/injection-mode");
  867.         return NULL;
  868.     }
  869.  
  870.     pckt_string = PyObject_Str(pckt);
  871.     if (!pckt_string) {
  872.         PyErr_SetString(PyExc_ValueError, "Failed to get string-representation from object.");
  873.         return NULL;
  874.     }
  875.  
  876.     if (PyBytes_AsStringAndSize(pckt_string, &pckt_buffer, &pckt_size)) {
  877.         Py_DECREF(pckt_string);
  878.         return NULL;
  879.     }
  880.  
  881.     sent = lorcon_send_bytes(self->context, pckt_size, (u_char*)pckt_buffer);
  882.     if (sent < 0) {
  883.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  884.         Py_DECREF(pckt_string);
  885.         return NULL;
  886.     }
  887.    
  888.     Py_DECREF(pckt_string);
  889.    
  890.     return PyLong_FromLong(sent);
  891. }
  892.  
  893. static PyObject*
  894. PyLorcon2_Context_set_filter(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  895. {
  896.     char *filter_buffer;
  897.     ssize_t filter_size;
  898.  
  899.     PyObject *filter, *filter_string;
  900.     int ret;
  901.  
  902.     if (!PyArg_ParseTuple(args, "O", &filter))
  903.         return NULL;
  904.    
  905.     filter_string = PyObject_Str(filter);
  906.     if (!filter_string) {
  907.         PyErr_SetString(PyExc_ValueError, "Failed to get string filter");
  908.         return NULL;
  909.     }
  910.  
  911.     if (PyBytes_AsStringAndSize(filter_string, &filter_buffer, &filter_size)) {
  912.         Py_DECREF(filter_string);
  913.         return NULL;
  914.     }
  915.  
  916.     ret = lorcon_set_filter(self->context, filter_buffer);
  917.  
  918.     if (ret < 0) {
  919.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  920.         Py_DECREF(filter_string);
  921.         return NULL;
  922.     }
  923.    
  924.     Py_DECREF(filter_string);
  925.    
  926.     return PyLong_FromLong(ret);
  927. }
  928.  
  929. static PyObject*
  930. PyLorcon2_Context_set_timeout(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  931. {
  932.     static char *kwlist[] = {"timeout", NULL};
  933.     int timeout;
  934.  
  935.     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &timeout))
  936.         return NULL;
  937.  
  938.     lorcon_set_timeout(self->context, timeout);
  939.  
  940.     Py_INCREF(Py_None);
  941.     return Py_None;
  942. }
  943.  
  944. static PyObject*
  945. PyLorcon2_Context_get_timeout(PyLorcon2_Context *self)
  946. {
  947.     return PyLong_FromLong(lorcon_get_timeout(self->context));
  948. }
  949.  
  950. static PyObject*
  951. PyLorcon2_Context_set_vap(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  952. {
  953.     char *vap;
  954.     static char *kwlist[] = {"vap", NULL};
  955.  
  956.     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &vap))
  957.         return NULL;
  958.  
  959.     lorcon_set_vap(self->context, vap);
  960.  
  961.     Py_INCREF(Py_None);
  962.     return Py_None;
  963. }
  964.  
  965. static PyObject*
  966. PyLorcon2_Context_get_vap(PyLorcon2_Context *self)
  967. {
  968.     return PyUnicode_FromString(lorcon_get_vap(self->context));
  969. }
  970.  
  971. static PyObject*
  972. PyLorcon2_Context_get_driver_name(PyLorcon2_Context *self)
  973. {
  974.     return PyUnicode_FromString(lorcon_get_driver_name(self->context));
  975. }
  976.  
  977. static PyObject*
  978. PyLorcon2_Context_set_channel(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  979. {
  980.     int channel;
  981.     lorcon_channel_t complex_channel;
  982.     PyObject *complexchan, *complexchan_string;
  983.     char *complex_buffer;
  984.     ssize_t complex_size;
  985.  
  986.     if (!self->monitored) {
  987.         PyErr_SetString(PyExc_RuntimeError, "Context must be in monitor/injection-mode");
  988.         return NULL;
  989.     }
  990.  
  991.  
  992.     if (PyArg_ParseTuple(args, "i", &channel)) {
  993.         if (lorcon_set_channel(self->context, channel) != 0) {
  994.             PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  995.             return NULL;
  996.         }
  997.     } else if (PyArg_ParseTuple(args, "O", &complexchan)) {
  998.         complexchan_string = PyObject_Str(complexchan);
  999.         if (!complexchan_string) {
  1000.             PyErr_SetString(PyExc_ValueError, "Failed to get string-representation from object.");
  1001.             return NULL;
  1002.         }
  1003.  
  1004.         if (PyBytes_AsStringAndSize(complexchan_string, &complex_buffer, &complex_size)) {
  1005.             Py_DECREF(complexchan_string);
  1006.             return NULL;
  1007.         }
  1008.  
  1009.         if (lorcon_parse_ht_channel(complex_buffer, &complex_channel) != 0) {
  1010.             Py_DECREF(complexchan_string);
  1011.             PyErr_SetString(PyExc_ValueError, "Failed to get parse channel");
  1012.             return NULL;
  1013.         }
  1014.  
  1015.         if (lorcon_set_complex_channel(self->context, &complex_channel) != 0) {
  1016.             PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  1017.             Py_DECREF(complexchan_string);
  1018.             return NULL;
  1019.         }
  1020.  
  1021.         Py_DECREF(complexchan_string);
  1022.     }
  1023.  
  1024.  
  1025.     Py_INCREF(Py_None);
  1026.     return Py_None;
  1027. }
  1028.  
  1029. static PyObject*
  1030. PyLorcon2_Context_get_channel(PyLorcon2_Context *self)
  1031. {
  1032.     int channel;
  1033.  
  1034.     channel = lorcon_get_channel(self->context);
  1035.     if (channel < 0) {
  1036.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  1037.         return NULL;
  1038.     }
  1039.  
  1040.     return PyLong_FromLong(channel);
  1041. }
  1042.  
  1043. static PyObject*
  1044. PyLorcon2_Context_get_hwmac(PyLorcon2_Context *self)
  1045. {
  1046.     int r;
  1047.     uint8_t *mac;
  1048.     PyObject *ret;
  1049.  
  1050.     r = lorcon_get_hwmac(self->context, &mac);
  1051.     if (r < 0) {
  1052.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  1053.         ret = NULL;
  1054.     } else if (r == 0) {
  1055.         Py_INCREF(Py_None);
  1056.         ret = Py_None;
  1057.     } else {
  1058.         ret = Py_BuildValue("(i,i,i,i,i,i)",
  1059.                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1060.         free(mac);
  1061.     }
  1062.  
  1063.     return ret;
  1064. }
  1065.  
  1066. static PyObject*
  1067. PyLorcon2_Context_set_hwmac(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
  1068. {
  1069.     PyObject *mac_tuple;
  1070.     uint8_t mac[6];
  1071.     int i;
  1072.  
  1073.     if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &mac_tuple))
  1074.         return NULL;
  1075.  
  1076.     if (!self->monitored) {
  1077.         PyErr_SetString(PyExc_RuntimeError, "Context must be in monitor/injection-mode");
  1078.         return NULL;
  1079.     }
  1080.    
  1081.     if (PyTuple_Size(mac_tuple) != 6) {
  1082.         PyErr_SetString(PyExc_ValueError, "Parameter must be a tuple of 6 integers");
  1083.         return NULL;
  1084.     }
  1085.    
  1086.     for (i = 0; i < 6; i++) {
  1087.         mac[i] = (uint8_t)PyLong_AsLong(PyTuple_GetItem(mac_tuple, i));
  1088.         if (mac[i] == -1) {
  1089.             PyErr_SetString(PyExc_ValueError, "Tuple-entry is not convertible to integer");
  1090.             return NULL;
  1091.         }
  1092.     }
  1093.    
  1094.     if (lorcon_set_hwmac(self->context, 6, mac) < 0) {
  1095.         PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
  1096.         return NULL;
  1097.     }
  1098.  
  1099.     Py_INCREF(Py_None);
  1100.     return Py_None;
  1101. }
  1102.  
  1103. static PyObject *
  1104. PyLorcon2_Context_get_next(PyLorcon2_Context *self) {
  1105.     int ret;
  1106.     lorcon_packet_t *l_packet;
  1107.     PyObject *arg_tuple;
  1108.  
  1109.     ret = lorcon_next_ex(self->context, &l_packet);
  1110.  
  1111.     if (ret <= 0) {
  1112.         PyErr_SetString(PyExc_RuntimeError, "Could not get next packet");
  1113.         return NULL;
  1114.     }
  1115.  
  1116.     arg_tuple = PyTuple_New(0);
  1117.  
  1118.     PyObject *obj = PyObject_CallObject((PyObject *) &PyLorcon2_PacketType, arg_tuple);
  1119.     ((PyLorcon2_Packet *) obj)->packet = l_packet;
  1120.  
  1121.     Py_DECREF(arg_tuple);
  1122.  
  1123.     return obj;
  1124.  
  1125. }
  1126.  
  1127. static PyObject*
  1128. PyLorcon2_Packet_get_time_sec(PyLorcon2_Packet *self)
  1129. {
  1130.     long int timesec;
  1131.     if (self->packet == NULL) {
  1132.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1133.         return NULL;
  1134.     }
  1135.     timesec = self->packet->ts.tv_sec;
  1136.     return PyLong_FromLong(timesec);
  1137. }
  1138.  
  1139. static PyObject*
  1140. PyLorcon2_Packet_get_time_usec(PyLorcon2_Packet *self)
  1141. {
  1142.     long int timesec;
  1143.     if (self->packet == NULL) {
  1144.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1145.         return NULL;
  1146.     }
  1147.     timesec = self->packet->ts.tv_usec;
  1148.     return PyLong_FromLong(timesec);
  1149. }
  1150.  
  1151. static PyObject*
  1152. PyLorcon2_Packet_get_length(PyLorcon2_Packet *self)
  1153. {
  1154.     long int length;
  1155.     if (self->packet == NULL) {
  1156.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1157.         return NULL;
  1158.     }
  1159.     length = self->packet->length;
  1160.     return PyLong_FromLong(length);
  1161. }
  1162.  
  1163. static PyObject*
  1164. PyLorcon2_Packet_get_dot11_length(PyLorcon2_Packet *self)
  1165. {
  1166.     long int length;
  1167.     if (self->packet == NULL) {
  1168.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1169.         return NULL;
  1170.     }
  1171.     length = self->packet->length_header;
  1172.     return PyLong_FromLong(length);
  1173. }
  1174.  
  1175. static PyObject*
  1176. PyLorcon2_Packet_get_payload_length(PyLorcon2_Packet *self)
  1177. {
  1178.     long int length;
  1179.     if (self->packet == NULL) {
  1180.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1181.         return NULL;
  1182.     }
  1183.     length = self->packet->length_data;
  1184.     return PyLong_FromLong(length);
  1185. }
  1186.  
  1187. static void
  1188. PyLorcon2_Packet_dealloc(PyLorcon2_Packet *self)
  1189. {
  1190.     if(self->packet != NULL)
  1191.         lorcon_packet_free(self->packet);
  1192.  
  1193.     Py_TYPE(self)->tp_free((PyObject*)self);
  1194. }
  1195.  
  1196. static int
  1197. PyLorcon2_Packet_init(PyLorcon2_Packet *self, PyObject *args, PyObject *kwds)
  1198. {
  1199.     self->packet = NULL;
  1200.    
  1201.     return 0;
  1202. }
  1203.  
  1204. static PyObject*
  1205. PyLorcon2_Packet_get_packet(PyLorcon2_Packet *self) {
  1206.  
  1207.     if (self->packet == NULL) {
  1208.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1209.         return NULL;
  1210.     }
  1211.  
  1212.     return PyByteArray_FromStringAndSize((const char *) self->packet->packet_raw,
  1213.             self->packet->length);
  1214. }
  1215.  
  1216. static PyObject*
  1217. PyLorcon2_Packet_get_dot11(PyLorcon2_Packet *self) {
  1218.     if (self->packet == NULL) {
  1219.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1220.         return NULL;
  1221.     }
  1222.  
  1223.     return PyByteArray_FromStringAndSize((const char *) self->packet->packet_header,
  1224.             self->packet->length_header);
  1225. }
  1226.  
  1227. static PyObject*
  1228. PyLorcon2_Packet_get_data_payload(PyLorcon2_Packet *self) {
  1229.     if (self->packet == NULL) {
  1230.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1231.         return NULL;
  1232.     }
  1233.  
  1234.     return PyByteArray_FromStringAndSize((const char *) self->packet->packet_data,
  1235.             self->packet->length_data);
  1236. }
  1237.  
  1238. static PyObject *
  1239. PyLorcon2_Packet_get_interface(PyLorcon2_Packet *self) {
  1240.     PyObject *arg_tuple, *obj;
  1241.  
  1242.     if (self->packet == NULL) {
  1243.         PyErr_SetString(PyExc_RuntimeError, "Packet not built");
  1244.         return NULL;
  1245.     }
  1246.  
  1247.     /* Make a context object that doesn't free its internal structure when the
  1248.      * python object goes away by wedging it */
  1249.     arg_tuple = PyTuple_New(0);
  1250.     obj = PyObject_CallObject((PyObject *) &PyLorcon2_ContextType, arg_tuple);
  1251.     ((PyLorcon2_Context *) obj)->context = lorcon_packet_get_interface(self->packet);
  1252.     ((PyLorcon2_Context *) obj)->free_on_cleanup = 0;
  1253.  
  1254.     Py_DECREF(arg_tuple);
  1255.  
  1256.     Py_INCREF(obj);
  1257.  
  1258.     return obj;
  1259. }
  1260.  
  1261. static int PyLorcon2_Multi_init(PyLorcon2_Multi *self,
  1262.         PyObject *args, PyObject *kwds) {
  1263.  
  1264.     self->multi = lorcon_multi_create();
  1265.  
  1266.     if (self->multi == NULL) {
  1267.         PyErr_SetString(PyExc_RuntimeError, "Unable to allocate multicap");
  1268.         return -1;
  1269.     }
  1270.  
  1271.     self->cb_func = NULL;
  1272.     self->cb_aux = NULL;
  1273.     self->error_cb_func = NULL;
  1274.    
  1275.     return 1;
  1276. }
  1277.  
  1278. static void PyLorcon2_Multi_dealloc(PyLorcon2_Multi *self) {
  1279.     if (self->multi == NULL)
  1280.         return;
  1281.  
  1282.     self->multi = NULL;
  1283.  
  1284.     if (self->cb_func != NULL)
  1285.         Py_XDECREF(self->cb_func);
  1286.     if (self->cb_aux != NULL)
  1287.         Py_XDECREF(self->cb_aux);
  1288.     if (self->error_cb_func != NULL)
  1289.         Py_XDECREF(self->error_cb_func);
  1290.  
  1291.     Py_TYPE(self)->tp_free(self);
  1292. }
  1293.  
  1294. static PyObject* PyLorcon2_Multi_get_error(PyLorcon2_Multi *self) {
  1295.     if (self->multi == NULL) {
  1296.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1297.         return NULL;
  1298.     }
  1299.  
  1300.     return PyUnicode_FromString(lorcon_multi_get_error(self->multi));
  1301. }
  1302.  
  1303. void pylorcon2_multi_error_handler(lorcon_multi_t *ctx, lorcon_t *lorcon_interface,
  1304.         void *aux) {
  1305.     PyLorcon2_Multi *multi = (PyLorcon2_Multi *) aux;
  1306.     PyObject *arg_tuple, *lorcon_obj;
  1307.     PyObject *cb_arg;
  1308.     PyObject *pyresult;
  1309.  
  1310.     /* Do nothing if we don't have a callback function */
  1311.     if (multi->error_cb_func == NULL)
  1312.         return;
  1313.    
  1314.     /* Make a lorcon context object that doesn't free its internal structure when the
  1315.      * python object goes away by wedging it */
  1316.     arg_tuple = PyTuple_New(0);
  1317.     lorcon_obj = PyObject_CallObject((PyObject *) &PyLorcon2_ContextType, arg_tuple);
  1318.     ((PyLorcon2_Context *) lorcon_obj)->context = lorcon_interface;
  1319.     ((PyLorcon2_Context *) lorcon_obj)->free_on_cleanup = 0;
  1320.     Py_DECREF(arg_tuple);
  1321.     Py_INCREF(lorcon_obj);
  1322.  
  1323.     /* Call the error cb function */
  1324.     cb_arg = Py_BuildValue("(O)", lorcon_obj);
  1325.     pyresult = PyEval_CallObject(multi->error_cb_func, cb_arg);
  1326.     Py_DECREF(cb_arg);
  1327.  
  1328.     if (pyresult == NULL) {
  1329.         PyErr_Print();
  1330.         printf("*** pylorcon2.multi error callback handler error\n");
  1331.         exit(1);
  1332.     }
  1333.  
  1334.     Py_DECREF(pyresult);
  1335. }
  1336.  
  1337. static PyObject* PyLorcon2_Multi_add_interface(PyLorcon2_Multi *self,
  1338.         PyObject *args) {
  1339.  
  1340.     PyObject *intf_obj;
  1341.  
  1342.     if (self->multi == NULL) {
  1343.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1344.         return NULL;
  1345.     }
  1346.  
  1347.     if (!PyArg_ParseTuple(args, "O", &intf_obj))
  1348.         return NULL;
  1349.  
  1350.     if (!PyObject_TypeCheck(intf_obj, &PyLorcon2_ContextType)) {
  1351.         PyErr_SetString(PyExc_RuntimeError, "Expected Lorcon2.Context");
  1352.         return NULL;
  1353.     }
  1354.  
  1355.     Py_INCREF(intf_obj);
  1356.     lorcon_multi_add_interface(self->multi, ((PyLorcon2_Context *) intf_obj)->context);
  1357.     lorcon_multi_set_interface_error_handler(self->multi,
  1358.             ((PyLorcon2_Context *) intf_obj)->context,
  1359.             pylorcon2_multi_error_handler, (void *) self);
  1360.  
  1361.     Py_INCREF(Py_None);
  1362.     return Py_None;
  1363. }
  1364.  
  1365. static PyObject* PyLorcon2_Multi_del_interface(PyLorcon2_Multi *self,
  1366.         PyObject *args) {
  1367.     PyObject *intf_obj;
  1368.  
  1369.     if (self->multi == NULL) {
  1370.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1371.         return NULL;
  1372.     }
  1373.  
  1374.     if (!PyArg_ParseTuple(args, "O", &intf_obj))
  1375.         return NULL;
  1376.  
  1377.     if (!PyObject_TypeCheck(intf_obj, &PyLorcon2_ContextType)) {
  1378.         PyErr_SetString(PyExc_RuntimeError, "Expected Lorcon2.Context");
  1379.         return NULL;
  1380.     }
  1381.  
  1382.     lorcon_multi_del_interface(self->multi,
  1383.             ((PyLorcon2_Context *) intf_obj)->context, 0);
  1384.     Py_DECREF(intf_obj);
  1385.  
  1386.     Py_INCREF(Py_None);
  1387.     return Py_None;
  1388. }
  1389.  
  1390. static PyObject* PyLorcon2_Multi_get_interfaces(PyLorcon2_Multi *self) {
  1391.     PyObject *retlist;
  1392.     PyObject *stringobj;
  1393.     lorcon_multi_interface_t *interface = NULL;
  1394.     lorcon_t *lorcon;
  1395.  
  1396.     if (self->multi == NULL) {
  1397.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1398.         return NULL;
  1399.     }
  1400.  
  1401.     retlist = PyList_New(0);
  1402.  
  1403.     while ((interface = lorcon_multi_get_next_interface(self->multi, interface))) {
  1404.         lorcon = lorcon_multi_interface_get_lorcon(interface);
  1405.         stringobj = PyUnicode_FromString(lorcon_get_capiface(lorcon));
  1406.         PyList_Append(retlist, stringobj);
  1407.         Py_DECREF(stringobj);
  1408.     }
  1409.  
  1410.     return retlist;
  1411. }
  1412.  
  1413. void pylorcon2_multi_handler(lorcon_t *ctx, lorcon_packet_t *pkt, u_char *aux) {
  1414.     PyLorcon2_Multi *multi = (PyLorcon2_Multi *) aux;
  1415.     PyObject *pypacket, *cb_arg, *packet_tuple_arg;
  1416.     PyObject *pyresult;
  1417.    
  1418.     packet_tuple_arg = PyTuple_New(0);
  1419.     pypacket = PyObject_CallObject((PyObject *) &PyLorcon2_PacketType,
  1420.             packet_tuple_arg);
  1421.     ((PyLorcon2_Packet *) pypacket)->packet = pkt;
  1422.     Py_DECREF(packet_tuple_arg);
  1423.  
  1424.     cb_arg = Py_BuildValue("(O)", pypacket);
  1425.     pyresult = PyEval_CallObject(multi->cb_func, cb_arg);
  1426.     Py_DECREF(cb_arg);
  1427.  
  1428.     if (pyresult == NULL) {
  1429.         PyErr_Print();
  1430.         printf("*** pylorcon2.multi callback handler error\n");
  1431.         exit(1);
  1432.     }
  1433.  
  1434.     Py_DECREF(pyresult);
  1435. }
  1436.  
  1437. static PyObject* PyLorcon2_Multi_loop(PyLorcon2_Multi *self, PyObject *args) {
  1438.     PyObject *callback;
  1439.     int num, ret;
  1440.    
  1441.     if (self->multi == NULL) {
  1442.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1443.         return NULL;
  1444.     }
  1445.  
  1446.     if (!PyArg_ParseTuple(args, "iO", &num, &callback))
  1447.         return NULL;
  1448.  
  1449.     if (!PyCallable_Check(callback)) {
  1450.         PyErr_SetString(PyExc_TypeError, "parameter must be callable");
  1451.         return NULL;
  1452.     }
  1453.  
  1454.     Py_XINCREF(callback);
  1455.  
  1456.     if (self->cb_func != NULL)
  1457.         Py_XDECREF(self->cb_func);
  1458.  
  1459.     self->cb_func = callback;
  1460.  
  1461.     ret = lorcon_multi_loop(self->multi, num, pylorcon2_multi_handler, (void *) self);
  1462.  
  1463.     return PyLong_FromLong(ret);
  1464. }
  1465.  
  1466. static PyObject* PyLorcon2_Multi_get_multi_ptr(PyLorcon2_Multi *self) {
  1467.     PyObject *ptrcapsule;
  1468.    
  1469.     if (self->multi == NULL) {
  1470.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1471.         return NULL;
  1472.     }
  1473.  
  1474.     ptrcapsule = PyCapsule_New((void *) self->multi, "MULTI", NULL);
  1475.  
  1476.     return ptrcapsule;
  1477. }
  1478.  
  1479. static PyObject *PyLorcon2_Multi_set_interface_error_cb(PyLorcon2_Multi *self,
  1480.         PyObject *args) {
  1481.     PyObject *cb_obj;
  1482.  
  1483.     if (self->multi == NULL) {
  1484.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1485.         return NULL;
  1486.     }
  1487.  
  1488.     if (!PyArg_ParseTuple(args, "O", &cb_obj))
  1489.         return NULL;
  1490.  
  1491.     if (!PyCallable_Check(cb_obj)) {
  1492.         PyErr_SetString(PyExc_TypeError, "parameter must be callable");
  1493.         return NULL;
  1494.     }
  1495.  
  1496.     Py_XINCREF(cb_obj);
  1497.  
  1498.     if (self->cb_func != NULL)
  1499.         Py_XDECREF(self->error_cb_func);
  1500.  
  1501.     self->error_cb_func = cb_obj;
  1502.  
  1503.     Py_INCREF(Py_None);
  1504.     return Py_None;
  1505. }
  1506.  
  1507. static PyObject *PyLorcon2_Multi_remove_interface_error_cb(PyLorcon2_Multi *self,
  1508.         PyObject *args) {
  1509.  
  1510.     if (self->multi == NULL) {
  1511.         PyErr_SetString(PyExc_RuntimeError, "Multicap not allocated");
  1512.         return NULL;
  1513.     }
  1514.  
  1515.     if (self->cb_func != NULL)
  1516.         Py_XDECREF(self->error_cb_func);
  1517.  
  1518.     self->error_cb_func = NULL;
  1519.  
  1520.     Py_INCREF(Py_None);
  1521.     return Py_None;
  1522. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top