netbios_ns.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //---------------------------------------------------------------------------
  2. // __________________ _________ _____ _____ .__ ._.
  3. // \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. // | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. // | | \| ` \/ / Y \ / | | \ ___/ \|
  6. // |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. // \/ \/ \/ \/ )/ \/ \/ \/
  8. //
  9. // This file is part of libdsm. Copyright © 2014 VideoLabs SAS
  10. //
  11. // Author: Julien 'Lta' BALLET <contact@lta.io>
  12. //
  13. // This program is free software. It comes without any warranty, to the extent
  14. // permitted by applicable law. You can redistribute it and/or modify it under
  15. // the terms of the Do What The Fuck You Want To Public License, Version 2, as
  16. // published by Sam Hocevar. See the COPYING file for more details.
  17. //----------------------------------------------------------------------------
  18. #ifndef __BDSM_NETBIOS_NS_H_
  19. #define __BDSM_NETBIOS_NS_H_
  20. #include "bdsm/netbios_defs.h"
  21. #include "bdsm/netbios_query.h"
  22. #include <sys/socket.h>
  23. #include <netinet/ip.h>
  24. #include <netinet/udp.h>
  25. /**
  26. * @file netbios_ns.h
  27. * @brief Netbios name service
  28. */
  29. /**
  30. * @brief Represents an correspondance between an IP address and a Netbios name.
  31. *
  32. * @details Consider it as an opaque data structure whose internal layout might
  33. * change at any time, please use the provided accessors functions
  34. */
  35. typedef struct netbios_ns_entry_s
  36. {
  37. struct netbios_ns_entry_s *next;
  38. struct in_addr address;
  39. char name[NETBIOS_NAME_LENGTH + 1];
  40. char type;
  41. } netbios_ns_entry;
  42. /**
  43. * @brief Get the name of the entry referenced by the iterator iter.
  44. * @details The pointer points to an area of memory owned by the netbios name
  45. * service
  46. *
  47. * @return A null-terminated ASCII string representing the name of a netbios machine.
  48. */
  49. const char *netbios_ns_entry_name(netbios_ns_entry *entry);
  50. /**
  51. * @brief Return the IP address of the correspondance referenced by the iterator
  52. *
  53. * @return The ip address of this entry, in network byte order.
  54. */
  55. uint32_t netbios_ns_entry_ip(netbios_ns_entry *entry);
  56. /**
  57. * @brief Return the type of record
  58. *
  59. * @return The type of netbios record (.ie 0x20 for FileServer,
  60. * 0 for workstation, etc.) or a value < 0 if the iterator is invalid or an
  61. * error occured.
  62. */
  63. char netbios_ns_entry_type(netbios_ns_entry *entry);
  64. /**
  65. * @brief The netbios name service object.
  66. *
  67. * @details Holds all the necessary data structure to perform resolution and
  68. * discovery, and to stores the results. Consider it as an opaque data
  69. * structure, use the related functions to interact with it.
  70. */
  71. typedef struct
  72. {
  73. int socket;
  74. struct sockaddr_in addr;
  75. uint16_t last_trn_id; // Last transaction id used;
  76. netbios_ns_entry *entries; // NS entries cache, mainly used by discover()
  77. } netbios_ns;
  78. /**
  79. * @brief Allocate and initialize the Netbios name service client object.
  80. * @return A newly allocated netbios_ns ready for querying.
  81. * Deallocate with netbios_ns_destroy().
  82. */
  83. netbios_ns *netbios_ns_new();
  84. /**
  85. * @brief Destroy the netbios name service object
  86. * @param[in] ns A pointer on the netbios_ns to destroy and deallocate
  87. */
  88. void netbios_ns_destroy(netbios_ns *ns);
  89. /**
  90. * @brief Resolve a Netbios name
  91. * @details This function tries to resolves the given NetBIOS name with the
  92. * given type on the LAN, using broadcast queries. No WINS server is called.
  93. *
  94. * @param ns the netbios name service object.
  95. * @param name the null-terminated ASCII netbios name to resolve. If it's
  96. * longer than 15 chars, it'll be truncated.
  97. * @param type The type of the name to look for. @see netbios_defs.h
  98. * @param[out] addr The IP address in network byte order of the machine if found.
  99. * @return the ipv4 address in network byte-order or 0 if it wasn't successfull.
  100. */
  101. int netbios_ns_resolve(netbios_ns *ns, const char *name,
  102. char type, uint32_t *addr);
  103. /**
  104. * @brief Try to discover all the Netbios/SMB speaking machine on the LAN.
  105. * @details This functions sends a message to '*' Netbios name, and waits for
  106. * the machine on the LAN to answer. It then performs a reverse lookup on all
  107. * the ip he received packet from. It stores the results inside of the name
  108. * service, allowing you to list them
  109. *
  110. *
  111. * @param ns The name service object.
  112. * @return It returns 0 in case of error.
  113. */
  114. int netbios_ns_discover(netbios_ns *ns);
  115. /**
  116. * @brief Get the list of entries (know machine) for this name service object
  117. * @details You might want to call discover before-hand if you don't want
  118. * the lit to be empty
  119. *
  120. * @return The list of entries in the name service.
  121. */
  122. int netbios_ns_entry_count(netbios_ns *ns);
  123. /**
  124. * @brief Get the entry at a certain position in the entry list
  125. * @details You might want to call discover before-hand if you don't want
  126. * the lit to be empty. The entry list contains all the record known to the
  127. * name service (including resolved, reverse resolved and discovered) since the
  128. * creation of the name service object or the last call to clear
  129. *
  130. * @param ns The nameservice object.
  131. * @param pos The index/position of the item to access in the list. Must be <
  132. * netbios_ns_entry_count(ns) or the pointer returned will be NULL.
  133. * @return A pointer to a opaque netbios_ns_entry structure
  134. */
  135. netbios_ns_entry *netbios_ns_entry_at(netbios_ns *ns, int pos);
  136. /**
  137. * @brief Perform an inverse netbios lookup (get name from ip)
  138. * @details This function does a NBSTAT and stores all the returned entry in
  139. * the internal list of entries. It returns one of the name found. (Normally
  140. * the <20> or <0> name)
  141. *
  142. * @param ns The name service object.
  143. * @param ip The ip address in network byte order.
  144. *
  145. * @return A null-terminated ASCII string containing the NETBIOS name. You don't
  146. * own the it (it'll be freed when destroying/clearing the name service)
  147. */
  148. const char *netbios_ns_inverse(netbios_ns *ns, uint32_t ip);
  149. /**
  150. * @brief Clear all the existing entries from the name service
  151. *
  152. * @param ns The nameservice object
  153. */
  154. void netbios_ns_clear(netbios_ns *ns);
  155. /**
  156. * @internal
  157. * @brief Add an entry to the name service list.
  158. * @details You can provide a name and/or an ip
  159. *
  160. * @param ns The name service object.
  161. * @param name The ASCII name of the entry or NULL
  162. * @param type the <X> type for this entry or -1
  163. * @param ip The IP address in network byte order (or 0)
  164. * @return The added entry
  165. */
  166. netbios_ns_entry *netbios_ns_entry_add(netbios_ns *ns, const char *name,
  167. char type, uint32_t ip);
  168. /**
  169. * @internal
  170. * @brief Find an entry in
  171. * @details [long description]
  172. *
  173. * @param ns [description]
  174. * @param by_name [description]
  175. * @param ip [description]
  176. * @return [description]
  177. */
  178. netbios_ns_entry *netbios_ns_entry_find(netbios_ns *ns, const char *by_name,
  179. uint32_t ip);
  180. #endif