Bläddra i källkod

Replaces the too complex iterator API by _count() and _at() functions

Julien 'Lta' BALLET 11 år sedan
förälder
incheckning
2cfd1b3d19
4 ändrade filer med 80 tillägg och 62 borttagningar
  1. 8 9
      bin/discover.c
  2. 18 24
      include/bdsm/netbios_ns.h
  3. 9 11
      src/netbios_ns.c
  4. 45 18
      src/netbios_ns_entry.c

+ 8 - 9
bin/discover.c

@@ -29,8 +29,8 @@
 
 int main(int ac, char **av)
 {
-  netbios_ns_t      *ns;
-  netbios_ns_iter_t iter;
+  netbios_ns_t        *ns;
+  netbios_ns_entry_t  *entry;
 
   ns = netbios_ns_new();
 
@@ -40,18 +40,17 @@ int main(int ac, char **av)
     exit(42);
   }
 
-  iter = netbios_ns_get_entries(ns);
-  while (iter != NULL)
+  for (int i = 0; i < netbios_ns_entry_count(ns); i++)
   {
     struct in_addr addr;
 
-    addr.s_addr = netbios_ns_iter_ip(iter);
+    entry       = netbios_ns_entry_at(ns, i);
+    addr.s_addr = netbios_ns_entry_ip(entry);
+
     printf("Ip: %s, name: %s<%x> \n",
       inet_ntoa(addr),
-      netbios_ns_iter_name(iter),
-      netbios_ns_iter_type(iter));
-
-    iter = netbios_ns_iter_next(iter);
+      netbios_ns_entry_name(entry),
+      netbios_ns_entry_type(entry));
   }
 
   return (0);

+ 18 - 24
include/bdsm/netbios_ns.h

@@ -32,7 +32,6 @@
  */
 
 /**
- * @internal
  * @brief Represents an correspondance between an IP address and a Netbios name.
  *
  * @details Consider it as an opaque data structure whose internal layout might
@@ -47,35 +46,20 @@ typedef struct                netbios_ns_entry_s
 }                             netbios_ns_entry_t;
 
 /**
- * @struct netbios_ns_iter_t
- * @brief An iterator over name service cached entries.
- * @details Can be compared againt NULL to test for validity
- */
-typedef netbios_ns_entry_t    *netbios_ns_iter_t;
-
-/**
- * @brief Iterates over netbios name service entries
- *
- * @param[in] iter An iterator opaque object.
- * @return An interator referencing the next item in the item set, or NULL.
- */
-netbios_ns_iter_t   netbios_ns_iter_next(netbios_ns_iter_t iter);
-
-/**
  * @brief Get the name of the entry referenced by the iterator iter.
  * @details The pointer points to an area of memory owned by the netbios name
  * service
  *
  * @return A null-terminated ASCII string representing the name of a netbios machine.
  */
-const char          *netbios_ns_iter_name(netbios_ns_iter_t iter);
+const char          *netbios_ns_entry_name(netbios_ns_entry_t *entry);
 
 /**
  * @brief Return the IP address of the correspondance referenced by the iterator
  *
  * @return The ip address of this entry, in network byte order.
  */
-uint32_t            netbios_ns_iter_ip(netbios_ns_iter_t iter);
+uint32_t            netbios_ns_entry_ip(netbios_ns_entry_t *entry);
 
 /**
  * @brief Return the type of record
@@ -84,7 +68,7 @@ uint32_t            netbios_ns_iter_ip(netbios_ns_iter_t iter);
  * 0 for workstation, etc.) or a value < 0 if the iterator is invalid or an
  * error occured.
  */
-char                netbios_ns_iter_type(netbios_ns_iter_t iter);
+char                netbios_ns_entry_type(netbios_ns_entry_t *entry);
 
 /**
  * @brief The netbios name service object.
@@ -141,15 +125,25 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type);
 int           netbios_ns_discover(netbios_ns_t *ns);
 
 /**
- * @brief List all know machines
- * @details Returns an iterator for the list of known machine of this name
- * service object. You might want to call discover before-hand if you don't want
+ * @brief Get the list of entries (know machine) for this name service object
+ * @details You might want to call discover before-hand if you don't want
  * the lit to be empty
  *
+ * @return The list of entries in the name service.
+ */
+int             netbios_ns_entry_count(netbios_ns_t *ns);
+
+/**
+ * @brief Get the entry at a certain position in the entry list
+ * @details You might want to call discover before-hand if you don't want
+ * the lit to be empty. The entry list contains all the record known to the
+ * name service (including resolved, reverse resolved and discovered) since the
+ * creation of the name service object or the last call to clear
+ *
  * @param ns The nameservice object.
- * @return An iterator for the list of known machine
+ * @return A pointer to a opaque netbios_ns_entry_t structure
  */
-netbios_ns_iter_t netbios_ns_get_entries(netbios_ns_t *ns);
+netbios_ns_entry_t *netbios_ns_entry_at(netbios_ns_t *ns, int pos);
 
 /**
  * @brief Perform an inverse netbios lookup (get name from ip)

+ 9 - 11
src/netbios_ns.c

@@ -162,6 +162,7 @@ ssize_t           netbios_ns_recv(int sock, void *buf, size_t buf_size,
 
 uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
 {
+  netbios_ns_entry_t  *cached;
   struct timeval      timeout;
   netbios_query_t     *q;
   char                *encoded_name;
@@ -174,6 +175,10 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
 
   assert(ns != NULL);
 
+
+  if ((cached = netbios_ns_entry_find(ns, name, 0)) != NULL)
+    return (cached->address.s_addr);
+
   if ((encoded_name = netbios_name_encode(name, 0, type)) == NULL)
     return (0);
 
@@ -217,10 +222,6 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
     return (0);
 }
 
-static void   _netbios_ns_sync(netbios_ns_t *ns)
-{
-
-}
 
 int           netbios_ns_discover(netbios_ns_t *ns)
 {
@@ -290,13 +291,6 @@ int           netbios_ns_discover(netbios_ns_t *ns)
   return (1);
 }
 
-netbios_ns_iter_t netbios_ns_get_entries(netbios_ns_t *ns)
-{
-  assert(ns != NULL);
-
-  return (ns->entries);
-}
-
 // Perform inverse name resolution. Grap an IP and return the first <20> field
 // returned by the host
 const char        *netbios_ns_inverse(netbios_ns_t *ns, uint32_t ip)
@@ -304,6 +298,7 @@ const char        *netbios_ns_inverse(netbios_ns_t *ns, uint32_t ip)
   const char  broadcast_name[] = NETBIOS_WILDCARD;
   char        footer[4]        = { 0x00, 0x21, 0x00, 0x01 }; // NBSTAT/IP
 
+  netbios_ns_entry_t  *cached;
   struct timeval      timeout;
   netbios_query_t     *q;
   char                recv_buffer[512]; // Hu ?
@@ -311,6 +306,9 @@ const char        *netbios_ns_inverse(netbios_ns_t *ns, uint32_t ip)
 
   assert(ns != NULL && ip != 0);
 
+  if ((cached = netbios_ns_entry_find(ns, NULL, ip)) != NULL)
+    return (cached->name);
+
   // Prepare NBSTAT query packet
   q = netbios_query_new(34 + 4, 1, NETBIOS_OP_NAME_QUERY);
   netbios_query_append(q, broadcast_name, strlen(broadcast_name) + 1);

+ 45 - 18
src/netbios_ns_entry.c

@@ -23,34 +23,26 @@
 
 #include "bdsm/netbios_ns.h"
 
-netbios_ns_iter_t   netbios_ns_iter_next(netbios_ns_iter_t iter)
+const char          *netbios_ns_entry_name(netbios_ns_entry_t *entry)
 {
-  if (iter != NULL)
-    return (iter->next);
+  if (entry != NULL)
+    return (entry->name);
   else
     return (NULL);
 }
 
-const char          *netbios_ns_iter_name(netbios_ns_iter_t iter)
+uint32_t            netbios_ns_entry_ip(netbios_ns_entry_t *entry)
 {
-  if (iter != NULL)
-    return (iter->name);
-  else
-    return (NULL);
-}
-
-uint32_t            netbios_ns_iter_ip(netbios_ns_iter_t iter)
-{
-  if (iter != NULL)
-    return (iter->address.s_addr);
+  if (entry != NULL)
+    return (entry->address.s_addr);
   else
     return (0);
 }
 
-char                netbios_ns_iter_type(netbios_ns_iter_t iter)
+char                netbios_ns_entry_type(netbios_ns_entry_t *entry)
 {
-  if (iter != NULL)
-    return (iter->type);
+  if (entry != NULL)
+    return (entry->type);
   else
     return (-1);
 }
@@ -107,7 +99,7 @@ netbios_ns_entry_t *netbios_ns_entry_find(netbios_ns_t *ns, const char *by_name,
   {
     if (by_name != NULL)
     {
-      if (!strncmp(by_name, iter->name, NETBIOS_NAME_LENGTH + 1))
+      if (!strncmp(by_name, iter->name, NETBIOS_NAME_LENGTH))
         found = iter;
     }
     else if (iter->address.s_addr == ip)
@@ -116,3 +108,38 @@ netbios_ns_entry_t *netbios_ns_entry_find(netbios_ns_t *ns, const char *by_name,
 
   return (found);
 }
+
+int             netbios_ns_entry_count(netbios_ns_t *ns)
+{
+  netbios_ns_entry_t  *iter;
+  int                 res;
+
+  assert (ns != NULL);
+
+  iter  = ns->entries;
+  res   = 0;
+  while (iter != NULL)
+  {
+    res++;
+    iter = iter->next;
+  }
+
+  return (res);
+}
+
+netbios_ns_entry_t  *netbios_ns_entry_at(netbios_ns_t *ns, int pos)
+{
+  netbios_ns_entry_t  *iter = NULL;
+  int                 i = 0;
+
+  assert(ns != NULL);
+
+  iter = ns->entries;
+  while (i < pos && iter != NULL)
+  {
+    i++;
+    iter = iter->next;
+  }
+
+  return (iter);
+}