[Dnsmasq-discuss] Experimental python binding

Petr Menšík pemensik at redhat.com
Thu Jul 11 17:45:13 UTC 2024


Hi!

I have been spending recently much time in gdb looking into bind9 code. 
But as an excercise I have created potential base for pretty printers in 
gdb.

You may know it is possible to define custom display functions written 
in python in gdb. That helps especially when lot of members, which can 
be interpreted much simpler way. Common might be std::string displaying.

I have no time to play with extensively, but I have put together 
interesting piece for code for swig tool. That cannot process original 
dnsmasq header without much hassle, but I were able to export a lot of 
flags into python. There, using enum.Flag type, it can show nice and 
friendly text way from constants gathered. For example for F_ flag or 
SERV_ flag in struct server or crec. It could use a lot of polishing, 
but I think is a great way have code more understandable from debugger.

Just put those files into src, have swig and python development files 
installed and give it a try.

Nice example can be run from python:

from pydnsmasq import *
SERV(11)

<SERV.SERV_LITERAL_ADDRESS|SERV_USE_RESOLV|SERV_4ADDR: 11>

If you have ever been lost in flags in debugger, this should help you a 
lot! But I think common enumeration defines should be changed to enum in 
C directly. It would get understood right away.

PS: consider the same license as the project for them.

Cheers,
Petr

-- 
Petr Menšík
Software Engineer, RHEL
Red Hat, http://www.redhat.com/
PGP: DFCF908DB7C87E8E529925BC4931CA5B6C9FC5CB
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mkswig.sh
Type: application/x-shellscript
Size: 189 bytes
Desc: not available
URL: <http://lists.thekelleys.org.uk/pipermail/dnsmasq-discuss/attachments/20240711/da271fc3/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pydnsmasq.py
Type: text/x-python
Size: 1725 bytes
Desc: not available
URL: <http://lists.thekelleys.org.uk/pipermail/dnsmasq-discuss/attachments/20240711/da271fc3/attachment.py>
-------------- next part --------------
/* Copyright (c) 2024 Petr Menšík <pemensik at redhat.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; version 2 dated June, 1991, or
   (at your option) version 3 dated 29 June, 2007.
 
   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, see <http://www.gnu.org/licenses/>.
*/

%module dnsmasq
%{
#define SWIG_FILE_WITH_INIT
#include "config.h"
#undef _STDIO_H
#undef _GNU_SOURCE
// generated by grep -E '^(#|typedef)' dnsmasq.h > dnsmasq-defines.h
#include "dnsmasq-defines.h"

struct daemon { } daemon;
%}
// %include "dnsmasq.h"
%include "dnsmasq-defines.h"
%include "dns-protocol.h"
%include "dhcp-protocol.h"
%include "dhcp6-protocol.h"
%include "radv-protocol.h"
%include "metrics.h"

#if 0
/* now create python code using those values as enumerators.
def dnsmasq_make_flags(name):
   return enum.Flag(name, dict([(i, v[i]) for i in vars(dnsmasq) if i.startswith(name+'_')]))

def dnsmasq_make_enum(name):
   return enum.Enum(name, dict([(i, v[i]) for i in vars(dnsmasq) if i.startswith(name+'_')]))

ACTOPM  = dnsmasq_make_enum("ACTION")
OPT     = dnsmasq_make_enum("OPT")
EVENT   = dnsmasq_make_enum("EVENT")
TXT_STAT= dnsmasq_make_enum("TXT_STAT")
T       = dnsmasq_make_enum("T")

AH      = dnsmasq_make_flags("AH")
CONFIG  = dnsmasq_make_flags("CONFIG")
CONTEXT = dnsmasq_make_flags("CONTEXT")
DHOPT   = dnsmasq_make_flags("DHOPT")
DNSSEC_FAIL = dnsmasq_make_flags("DNSSEC_FAIL")
DUMP    = dnsmasq_make_flags("DUMP")
F       = dnsmasq_make_flags("F")
FREC    = dnsmasq_make_flags("FREC")
LEASE   = dnsmasq_make_flags("LEASE")
INAME   = dnsmasq_make_flags("INAME")
IFACE   = dnsmasq_make_flags("IFACE")
SERV    = dnsmasq_make_flags("SERV")
*/
#endif


More information about the Dnsmasq-discuss mailing list