Ver código fonte

netbios_ns: add the group of the fileserver in entries

Thomas Guillem 10 anos atrás
pai
commit
87126870eb
5 arquivos alterados com 57 adições e 17 exclusões
  1. 9 0
      include/bdsm/netbios_ns.h
  2. 2 0
      src/netbios_defs.h
  3. 20 6
      src/netbios_ns.c
  4. 2 1
      src/netbios_ns.h
  5. 24 10
      src/netbios_ns_entry.c

+ 9 - 0
include/bdsm/netbios_ns.h

@@ -39,6 +39,15 @@ typedef struct netbios_ns netbios_ns;
 const char          *netbios_ns_entry_name(netbios_ns_entry *entry);
 const char          *netbios_ns_entry_name(netbios_ns_entry *entry);
 
 
 /**
 /**
+ * @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 group of a netbios machine.
+ */
+const char          *netbios_ns_entry_group(netbios_ns_entry *entry);
+
+/**
  * @brief Return the IP address of the correspondance referenced by the iterator
  * @brief Return the IP address of the correspondance referenced by the iterator
  *
  *
  * @return The ip address of this entry, in network byte order.
  * @return The ip address of this entry, in network byte order.

+ 2 - 0
src/netbios_defs.h

@@ -29,6 +29,8 @@
 
 
 #define NETBIOS_NAME_LENGTH   15
 #define NETBIOS_NAME_LENGTH   15
 
 
+#define NETBIOS_NAME_FLAG_GROUP (1 << 15)
+
 // http://ubiqx.org/cifs/rfc-draft/rfc1001.html#s17.2
 // http://ubiqx.org/cifs/rfc-draft/rfc1001.html#s17.2
 #define NETBIOS_WILDCARD      { 32, 'C', 'K', 'A', 'A', 'A', 'A', 'A', 'A',    \
 #define NETBIOS_WILDCARD      { 32, 'C', 'K', 'A', 'A', 'A', 'A', 'A', 'A',    \
     'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', \
     'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', \

+ 20 - 6
src/netbios_ns.c

@@ -398,9 +398,8 @@ const char        *netbios_ns_inverse(netbios_ns *ns, uint32_t ip)
     netbios_query_packet  *p = (netbios_query_packet *)recv_buffer;
     netbios_query_packet  *p = (netbios_query_packet *)recv_buffer;
     uint8_t                 name_count;
     uint8_t                 name_count;
     uint8_t                 name_idx;
     uint8_t                 name_idx;
-    char                    *names;
-    char                    *current_name;
-    char                    current_type;
+    const char              *names;
+    const char              *group = NULL;
     netbios_ns_entry        *entry = NULL;
     netbios_ns_entry        *entry = NULL;
 
 
     BDSM_dbg("Queried name length: %u\n", p->payload[0]);
     BDSM_dbg("Queried name length: %u\n", p->payload[0]);
@@ -408,14 +407,29 @@ const char        *netbios_ns_inverse(netbios_ns *ns, uint32_t ip)
     BDSM_dbg("Number of names: %hhu\n", name_count);
     BDSM_dbg("Number of names: %hhu\n", name_count);
     names = p->payload + p->payload[0] + 13;
     names = p->payload + p->payload[0] + 13;
 
 
+    // first search for a group in the name list
     for (name_idx = 0; name_idx < name_count; name_idx++)
     for (name_idx = 0; name_idx < name_count; name_idx++)
     {
     {
-        current_name = names + name_idx * 18;
-        current_type = current_name[15];
+        const char *current_name = names + name_idx * 18;
+        uint16_t current_flags = (current_name[16] << 8) | current_name[17];
+        if (current_flags & NETBIOS_NAME_FLAG_GROUP) {
+            group = current_name;
+            BDSM_dbg("Found group: %s\n", group);
+            break;
+        }
+    }
+    // then search for file servers
+    for (name_idx = 0; name_idx < name_count; name_idx++)
+    {
+        const char *current_name = names + name_idx * 18;
+        char current_type = current_name[15];
+        uint16_t current_flags = (current_name[16] << 8) | current_name[17];
 
 
+        if (current_flags & NETBIOS_NAME_FLAG_GROUP)
+            continue;
         BDSM_dbg("Found name : %s (type == 0x%x)\n", current_name, current_type);
         BDSM_dbg("Found name : %s (type == 0x%x)\n", current_name, current_type);
         if (current_type == NETBIOS_FILESERVER)
         if (current_type == NETBIOS_FILESERVER)
-            entry = netbios_ns_entry_add(ns, current_name, current_type, ip);
+            entry = netbios_ns_entry_add(ns, current_name, group, current_type, ip);
     }
     }
 
 
     return (entry->name);
     return (entry->name);

+ 2 - 1
src/netbios_ns.h

@@ -44,6 +44,7 @@ struct netbios_ns_entry
     netbios_ns_entry              *next;
     netbios_ns_entry              *next;
     struct in_addr                address;
     struct in_addr                address;
     char                          name[NETBIOS_NAME_LENGTH + 1];
     char                          name[NETBIOS_NAME_LENGTH + 1];
+    char                          group[NETBIOS_NAME_LENGTH + 1];
     char                          type;
     char                          type;
 };
 };
 
 
@@ -68,7 +69,7 @@ struct netbios_ns
  * @return The added entry
  * @return The added entry
  */
  */
 netbios_ns_entry    *netbios_ns_entry_add(netbios_ns *ns, const char *name,
 netbios_ns_entry    *netbios_ns_entry_add(netbios_ns *ns, const char *name,
-        char type, uint32_t ip);
+        const char *group, char type, uint32_t ip);
 /**
 /**
  * @internal
  * @internal
  * @brief Find an entry in
  * @brief Find an entry in

+ 24 - 10
src/netbios_ns_entry.c

@@ -31,6 +31,14 @@ const char          *netbios_ns_entry_name(netbios_ns_entry *entry)
         return (NULL);
         return (NULL);
 }
 }
 
 
+const char          *netbios_ns_entry_group(netbios_ns_entry *entry)
+{
+    if (entry != NULL)
+        return (entry->group);
+    else
+        return (NULL);
+}
+
 uint32_t            netbios_ns_entry_ip(netbios_ns_entry *entry)
 uint32_t            netbios_ns_entry_ip(netbios_ns_entry *entry)
 {
 {
     if (entry != NULL)
     if (entry != NULL)
@@ -61,7 +69,20 @@ void                netbios_ns_clear(netbios_ns *ns)
     }
     }
 }
 }
 
 
+static void netbios_ns_copy_name(char *dest, const char *src)
+{
+    memcpy(dest, src, NETBIOS_NAME_LENGTH);
+    dest[NETBIOS_NAME_LENGTH] = 0;
+
+    for (int i = 1; i < NETBIOS_NAME_LENGTH; i++ )
+      if (dest[NETBIOS_NAME_LENGTH - i] == ' ')
+        dest[NETBIOS_NAME_LENGTH - i] = 0;
+      else
+        break;
+}
+
 netbios_ns_entry *netbios_ns_entry_add(netbios_ns *ns, const char *name,
 netbios_ns_entry *netbios_ns_entry_add(netbios_ns *ns, const char *name,
+                                       const char *group,
                                        char type, uint32_t ip)
                                        char type, uint32_t ip)
 {
 {
     netbios_ns_entry  *entry;
     netbios_ns_entry  *entry;
@@ -71,16 +92,9 @@ netbios_ns_entry *netbios_ns_entry_add(netbios_ns *ns, const char *name,
         return NULL;
         return NULL;
 
 
     if (name != NULL)
     if (name != NULL)
-    {
-        memcpy(entry->name, name, NETBIOS_NAME_LENGTH);
-        entry->name[NETBIOS_NAME_LENGTH] = 0;
-
-        for (int i = 1; i < NETBIOS_NAME_LENGTH; i++ )
-          if (entry->name[NETBIOS_NAME_LENGTH - i] == ' ')
-            entry->name[NETBIOS_NAME_LENGTH - i] = 0;
-          else
-            break;
-    }
+        netbios_ns_copy_name(entry->name, name);
+    if (group != NULL)
+        netbios_ns_copy_name(entry->group, group);
 
 
     entry->type           = type;
     entry->type           = type;
     entry->address.s_addr = ip;
     entry->address.s_addr = ip;