Index: trunk/contrib/python/ldns_dnssec.i
===================================================================
--- trunk/contrib/python/ldns_dnssec.i	(revision 3699)
+++ trunk/contrib/python/ldns_dnssec.i	(working copy)
@@ -247,6 +247,29 @@
 
  return ldns_dnssec_zone_sign(zone, new_rrs, key_list, ldns_dnssec_default_replace_signatures, NULL);
 }
+
+ldns_status ldns_dnssec_zone_add_rr_(ldns_dnssec_zone *zone, ldns_rr *rr)
+{
+  ldns_rr *new_rr;
+  ldns_status status;
+
+  new_rr = ldns_rr_clone(rr);
+
+  /*
+   * A clone of the RR is created to be stored in the DNSSEC zone.
+   * The Python engine frees a RR object as soon it's reference count
+   * reaches zero. The code must avoid double freeing or accessing of freed
+   * memory.
+   */
+
+  status = ldns_dnssec_zone_add_rr(zone, new_rr);
+
+  if (status != LDNS_STATUS_OK) {
+    ldns_rr_free(new_rr);
+  }
+
+  return status;
+}
 %}
 
 %extend ldns_dnssec_zone {
@@ -413,7 +436,7 @@
                    The RR to add
                :returns: (ldns_status) LDNS_STATUS_OK on success, an error code otherwise
             """
-            return _ldns.ldns_dnssec_zone_add_rr(self,rr)
+            return _ldns.ldns_dnssec_zone_add_rr_(self,rr)
             #parameters: ldns_dnssec_zone *,ldns_rr *,
             #retvals: ldns_status
 
Index: trunk/contrib/python/ldns.i
===================================================================
--- trunk/contrib/python/ldns.i	(revision 3699)
+++ trunk/contrib/python/ldns.i	(working copy)
@@ -192,8 +192,53 @@
    return tuple;
  }
 
+ PyObject* ldns_rr_new_frm_fp_(FILE *fp, uint32_t default_ttl,  ldns_rdf* origin,  ldns_rdf* prev) 
+ //returns tuple (status, ldns_rr, ttl, origin, prev)
+ {
+   uint32_t defttl = default_ttl;
+   uint32_t *p_defttl = &defttl;
+   if (defttl == 0) p_defttl = 0;
+
+   /*  origin and prev have to be cloned in order to decouple the data
+    *  from the python wrapper
+    */
+   if (origin != NULL)
+       origin = ldns_rdf_clone(origin);
+   if (prev != NULL)
+       prev = ldns_rdf_clone(prev);
+
+   ldns_rdf *p_origin = origin;
+   ldns_rdf **pp_origin = &p_origin;
+   //if (p_origin == 0) pp_origin = 0;
+
+   ldns_rdf *p_prev = prev;
+   ldns_rdf **pp_prev = &p_prev;
+   //if (p_prev == 0) pp_prev = 0;
+
+   ldns_rr *p_rr = 0;
+   ldns_rr **pp_rr = &p_rr;
+
+   ldns_status st = ldns_rr_new_frm_fp(pp_rr, fp, p_defttl, pp_origin, pp_prev);
+
+   PyObject* tuple;
+   tuple = PyTuple_New(5);
+   int idx = 0;
+   PyTuple_SetItem(tuple, idx, SWIG_From_int(st)); 
+   idx++;
+   PyTuple_SetItem(tuple, idx, (st == LDNS_STATUS_OK) ? 
+                             SWIG_NewPointerObj(SWIG_as_voidptr(p_rr), SWIGTYPE_p_ldns_struct_rr, SWIG_POINTER_OWN |  0 ) : 
+		   (Py_INCREF(Py_None), Py_None));
+   idx++;
+   PyTuple_SetItem(tuple, idx, SWIG_From_int(defttl));
+   idx++;
+   PyTuple_SetItem(tuple, idx, SWIG_NewPointerObj(SWIG_as_voidptr(p_origin), SWIGTYPE_p_ldns_struct_rdf, SWIG_POINTER_OWN |  0 ));
+   idx++;
+   PyTuple_SetItem(tuple, idx, SWIG_NewPointerObj(SWIG_as_voidptr(p_prev), SWIGTYPE_p_ldns_struct_rdf, SWIG_POINTER_OWN |  0 ));
+   return tuple;
+ }
+
  PyObject* ldns_rr_new_frm_fp_l_(FILE *fp, uint32_t default_ttl,  ldns_rdf* origin,  ldns_rdf* prev) 
- //returns tuple (status, ldns_rr, [line if ret_linenr], ttl, origin, prev)
+ //returns tuple (status, ldns_rr, line, ttl, origin, prev)
  {
    int linenr = 0;
    int *p_linenr = &linenr;
Index: trunk/contrib/python/ldns_rr.i
===================================================================
--- trunk/contrib/python/ldns_rr.i	(revision 3699)
+++ trunk/contrib/python/ldns_rr.i	(working copy)
@@ -467,7 +467,7 @@
 
                   * prev - (ldns_rdf) None or updated value of prev parameter
             """
-            res = _ldns.ldns_rr_new_frm_fp_l_(file, default_ttl, origin, prev, 0)
+            res = _ldns.ldns_rr_new_frm_fp_(file, default_ttl, origin, prev)
             if res[0] != LDNS_STATUS_OK:
                 if (raiseException): raise Exception("Can't create RR, error: %d" % res[0])
                 return None
@@ -493,7 +493,7 @@
 
                   * prev - (ldns_rdf) None or updated value of prev parameter
             """
-            res = _ldns.ldns_rr_new_frm_fp_l_(file, default_ttl, origin, prev, 1)
+            res = _ldns.ldns_rr_new_frm_fp_l_(file, default_ttl, origin, prev)
             if res[0] != LDNS_STATUS_OK:
                 if (raiseException): raise Exception("Can't create RR, error: %d" % res[0])
                 return None
Index: trunk/contrib/python/Makefile
===================================================================
--- trunk/contrib/python/Makefile	(revision 3699)
+++ trunk/contrib/python/Makefile	(working copy)
@@ -45,16 +45,16 @@
 	$(MAKE) -C ../..
 
 clean:
-	rm -rdf examples/ldns
+	rm -rf examples/ldns
 	rm -f _ldns.so ldns_wrapper.o
 	$(MAKE) -C ../.. clean
 
 testenv: ../../.libs/libldns.so.1 _ldns.so
-	rm -rdf examples/ldns
-	cd examples && mkdir ldns && ln -s ../../ldns.py ldns/__init__.py && ln -s ../../_ldns.so ldns/_ldns.so && ln -s ../../../../.libs/libldns.so.1 ldns/libldns.so.1 && ls -la
+	rm -rf examples/ldns
+	cd examples && mkdir ldns && ln -s ../../ldns.py ldns/__init__.py && ln -s ../../../../.libs/_ldns.so ldns/_ldns.so && ln -s ../../../../.libs/libldns.so.1 ldns/libldns.so.1 && ls -la
 	@echo "Run a script by typing ./script_name.py"
 	cd examples && LD_LIBRARY_PATH=ldns bash
-	rm -rdf examples/ldns
+	rm -rf examples/ldns
 
 doc: ../../.libs/ldns.so.1 _ldns.so
 	$(MAKE) -C docs html
@@ -63,5 +63,4 @@
 swig: ldns.i
 	swig -python -py3 -o ldns_wrapper.c -I../.. ldns.i
 	gcc -c ldns_wrapper.c -O9 -fPIC -I../.. -I../../ldns -I/usr/include/python3.1 -I. -o ldns_wrapper.o
-	ld -shared ldns_wrapper.o -L../../.libs -lldns -o _ldns.so 
-
+	ld -shared ldns_wrapper.o -L../../.libs -lldns -o _ldns.so
Index: trunk/contrib/python/examples/python3/ldns-signzone.py
===================================================================
--- trunk/contrib/python/examples/python3/ldns-signzone.py	(revision 0)
+++ trunk/contrib/python/examples/python3/ldns-signzone.py	(revision 0)
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+# This example shows how to sign a given zone file with private key
+
+import ldns
+import sys, os, time
+
+#private key TAG which identifies the private key 
+#use ldns-keygen.py in order to obtain private key
+keytag = 30761
+
+# Read zone file
+#-------------------------------------------------------------
+
+zone = ldns.ldns_zone.new_frm_fp(open("zone.txt","r"), None, 0, ldns.LDNS_RR_CLASS_IN)
+soa = zone.soa()
+origin = soa.owner()
+
+# Prepare keys
+#-------------------------------------------------------------
+
+#Read private key from file
+keyfile = open("key-%s-%d.private" % (origin, keytag), "r");
+key = ldns.ldns_key.new_frm_fp(keyfile)
+
+#Read public key from file
+pubfname = "key-%s-%d.key" % (origin, keytag)
+pubkey = None
+if os.path.isfile(pubfname):
+   pubkeyfile = open(pubfname, "r");
+   pubkey,_,_,_ = ldns.ldns_rr.new_frm_fp(pubkeyfile)
+
+if not pubkey:
+   #Create new public key
+   pubkey = key.key_to_rr()
+
+#Set key expiration
+key.set_expiration(int(time.time()) + 365*60*60*24) #365 days
+
+#Set key owner (important step)
+key.set_pubkey_owner(origin)
+
+#Insert DNSKEY RR
+zone.push_rr(pubkey)
+
+# Sign zone
+#-------------------------------------------------------------
+
+#Create keylist and push private key
+keys = ldns.ldns_key_list()
+keys.push_key(key)
+
+#Add SOA
+signed_zone = ldns.ldns_dnssec_zone()
+signed_zone.add_rr(soa)
+
+#Add RRs
+for rr in zone.rrs().rrs():
+   print("RR:", str(rr), end=" ")
+   signed_zone.add_rr(rr)
+
+added_rrs = ldns.ldns_rr_list()
+status = signed_zone.sign(added_rrs, keys)
+if (status == ldns.LDNS_STATUS_OK):
+   signed_zone.print_to_file(open("zone_signed.txt","w"))
+

Property changes on: trunk/contrib/python/examples/python3/ldns-signzone.py
___________________________________________________________________
Added: svn:executable
   + *

