- added check against statistics
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.h
index 326aff22d1383873273d2d37cefa04c01ee49b4b..44c10fc5203571b8f083fb3f97fb8cf28ae5529c 100644 (file)
@@ -4,10 +4,14 @@
 #include "gns.h"
 #include "gnunet_dht_service.h"
 
-#define DHT_OPERATION_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
+#define DHT_OPERATION_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
+#define GNUNET_GNS_DEFAULT_LOOKUP_TIMEOUT \
+  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
 #define DHT_GNS_REPLICATION_LEVEL 5
 
+#define GNUNET_GNS_MAX_PARALLEL_LOOKUPS 500
+
 /*
  * DLL to hold the authority chain
  * we had to pass in the resolution process
@@ -31,14 +35,17 @@ struct AuthorityChain
 /* handle to a resolution process */
 struct ResolverHandle;
 
+/**
+ * continuation called when cleanup of resolver finishes
+ */
+typedef void (*ResolverCleanupContinuation) (void);
 
 /**
- * processor for a resultion result
+ * processor for a record lookup result
  *
  * @param cls the closure
- * @param rh the resolution handle
  * @param rd_count number of results
- * @pram rd resukt data
+ * @param rd result data
  */
 typedef void (*RecordLookupProcessor) (void *cls,
                                   uint32_t rd_count,
@@ -63,7 +70,7 @@ typedef void (*ShortenResultProcessor) (void *cls, const char* name);
 typedef void (*GetAuthorityResultProcessor) (void *cls, const char* name);
 
 /**
- * processor for a resultion result
+ * processor for a resolution result
  *
  * @param cls the closure
  * @param rh the resolution handle
@@ -77,18 +84,27 @@ typedef void (*ResolutionResultProcessor) (void *cls,
 
 
 /**
- * Resoltion status indicator
- * EXISTS: the name to lookup exists
- * EXPIRED: the name in the record expired
+ * Resolution status indicator
+ * RSL_RECORD_EXISTS: the name to lookup exists
+ * RSL_RECORD_EXPIRED: the name in the record expired
+ * RSL_TIMED_OUT: resolution timed out
  */
 enum ResolutionStatus
 {
-  EXISTS = 1,
-  EXPIRED = 2
+  RSL_RECORD_EXISTS = 1,
+  RSL_RECORD_EXPIRED = 2,
+  RSL_TIMED_OUT = 4
 };
 
 /**
  * Handle to a currenty pending resolution
+ * a ResolverHandle is passed to, for example
+ * resolve_record_ns to resolve a record in the namestore.
+ * On result (positive or negative) the ResolutionResultProcessor
+ * is called.
+ * If a timeout is set timeout_cont will be called.
+ * If no timeout is set (ie timeout forever) then background resolutions
+ * might be triggered.
  */
 struct ResolverHandle
 {
@@ -104,17 +120,20 @@ struct ResolverHandle
   /* the name of the authoritative zone to query */
   char authority_name[MAX_DNS_LABEL_LENGTH];
 
-  /**
-   * we have an authority in namestore that
-   * may be able to resolve
-   */
-  int authority_found;
-
   /* a handle for dht lookups. should be NULL if no lookups are in progress */
   struct GNUNET_DHT_GetHandle *get_handle;
 
-  /* timeout task for dht lookups */
-  GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
+  /* timeout set for this lookup task */
+  struct GNUNET_TIME_Relative timeout;
+
+  /* timeout task for the lookup */
+  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+
+  /* continuation to call on timeout */
+  GNUNET_SCHEDULER_Task timeout_cont;
+
+  /* closure for timeout cont */
+  void* timeout_cont_cls;
 
   /* called when resolution phase finishes */
   ResolutionResultProcessor proc;
@@ -127,12 +146,27 @@ struct ResolverHandle
 
   /* DLL to store the authority chain */
   struct AuthorityChain *authority_chain_tail;
-  
+
   /* status of the resolution result */
   enum ResolutionStatus status;
 
+  /**
+   * private key of an/our authoritative zone
+   * can be NULL but automatical PKEY import will not work
+   */
   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
 
+  /**
+   * the heap node associated with this lookup, null if timeout is set
+   * used for DHT background lookups.
+   */
+  struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
+
+  /**
+   * Id for resolution process
+   */
+  unsigned long long id;
+
 };
 
 
@@ -161,14 +195,11 @@ struct RecordLookupHandle
  */
 struct NameShortenHandle
 {
-
-
   /* Method to call on shorten result */
   ShortenResultProcessor proc;
 
   /* closure to pass to proc */
   void* proc_cls;
-
 };
 
 /**
@@ -176,7 +207,6 @@ struct NameShortenHandle
  */
 struct GetNameAuthorityHandle
 {
-  
   /* the name to look up authority for */
   char name[MAX_DNS_NAME_LENGTH];
   
@@ -185,7 +215,6 @@ struct GetNameAuthorityHandle
 
   /* closure to pass to proc */
   void* proc_cls;
-
 };
 
 /**
@@ -211,29 +240,46 @@ struct GetPseuAuthorityHandle
   /* a handle for dht lookups. should be NULL if no lookups are in progress */
   struct GNUNET_DHT_GetHandle *get_handle;
 
-  /* timeout task for dht lookups */
-  GNUNET_SCHEDULER_TaskIdentifier dht_timeout;
+  /* timeout task for lookup */
+  GNUNET_SCHEDULER_TaskIdentifier timeout;
 };
 
 /**
  * Initialize the resolver
+ * MUST be called before other gns_resolver_* methods
  *
  * @param nh handle to the namestore
  * @param dh handle to the dht
+ * @param lz the local zone
+ * @param max_bg_queries maximum amount of background queries
+ * @param ignore_pending ignore records that still require user confirmation
+ *        on lookup
  * @returns GNUNET_OK on success
  */
 int
 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
-                  struct GNUNET_DHT_Handle *dh);
+                  struct GNUNET_DHT_Handle *dh,
+                  struct GNUNET_CRYPTO_ShortHashCode lz,
+                  unsigned long long max_bg_queries,
+                  int ignore_pending);
+
+/**
+ * Cleanup resolver: Terminate pending lookups
+ * 
+ * @param cont continuation to call when finished
+ */
+void
+gns_resolver_cleanup(ResolverCleanupContinuation cont);
 
 /**
  * Lookup of a record in a specific zone
- * calls lookup result processor on result
+ * calls RecordLookupProcessor on result or timeout
  *
  * @param zone the root zone
  * @param record_type the record type to look up
  * @param name the name to look up
  * @param key optional private key for authority caching
+ * @param timeout timeout for the resolution
  * @param proc the processor to call
  * @param cls the closure to pass to proc
  */
@@ -242,14 +288,29 @@ gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
                            uint32_t record_type,
                            const char* name,
                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
+                           struct GNUNET_TIME_Relative timeout,
                            RecordLookupProcessor proc,
                            void* cls);
 
+/**
+ * Shortens a name if possible. If the shortening fails
+ * name will be returned as shortened string. Else
+ * a shorter version of the name will be returned.
+ * There is no guarantee that the shortened name will
+ * actually be canonical/short etc.
+ *
+ * @param zone the zone to perform the operation in
+ * @param name name to shorten
+ * @param key optional private key for background lookups and PSEU import
+ * @param proc the processor to call on shorten result
+ * @param proc_cls the closure to pass to proc
+ */
 void
 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
                           const char* name,
+                          struct GNUNET_CRYPTO_RsaPrivateKey *key,
                           ShortenResultProcessor proc,
-                          void* cls);
+                          void* proc_cls);
 
 /**
  * Tries to resolve the authority for name
@@ -258,13 +319,13 @@ gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
  * @param zone the root zone to look up for
  * @param name the name to lookup up
  * @param proc the processor to call when finished
- * @param cls the closure to pass to the processor
+ * @param proc_cls the closure to pass to the processor
  */
 void
 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
                            const char* name,
                            GetAuthorityResultProcessor proc,
-                           void* cls);
+                           void* proc_cls);
 
 /**
  * Generic function to check for TLDs