netbios_ns.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_t;
  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_t *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_t *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_t *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. int socket;
  73. struct sockaddr_in addr;
  74. uint16_t last_trn_id; // Last transaction id used;
  75. netbios_ns_entry_t *entries; // Only used during discovery;
  76. } netbios_ns_t;
  77. /**
  78. * @brief Allocate and initialize the Netbios name service client object.
  79. * @return A newly allocated netbios_ns_t ready for querying.
  80. * Deallocate with netbios_ns_destroy().
  81. */
  82. netbios_ns_t *netbios_ns_new();
  83. /**
  84. * @brief Destroy the netbios name service object
  85. * @param[in] ns A pointer on the netbios_ns_t to destroy and deallocate
  86. */
  87. void netbios_ns_destroy(netbios_ns_t *ns);
  88. /**
  89. * @brief Resolve a Netbios name
  90. * @details This function tries to resolves the given NetBIOS name with the
  91. * given type on the LAN, using broadcast queries. No WINS server is called.
  92. *
  93. * @param ns the netbios name service object.
  94. * @param name the null-terminated ASCII netbios name to resolve. If it's
  95. * longer than 15 chars, it'll be truncated.
  96. * @param type The type of the name to look for. @see netbios_defs.h
  97. * @return the ipv4 address in network byte-order or 0 if it wasn't successfull.
  98. */
  99. uint32_t netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type);
  100. /**
  101. * @brief Try to discover all the Netbios/SMB speaking machine on the LAN.
  102. * @details This functions sends a message to '*' Netbios name, and waits for
  103. * the machine on the LAN to answer. It then performs a reverse lookup on all
  104. * the ip he received packet from. It stores the results inside of the name
  105. * service, allowing you to list them
  106. *
  107. *
  108. * @param ns The name service object.
  109. * @return It returns 0 in case of error.
  110. */
  111. int netbios_ns_discover(netbios_ns_t *ns);
  112. /**
  113. * @brief Get the list of entries (know machine) for this name service object
  114. * @details You might want to call discover before-hand if you don't want
  115. * the lit to be empty
  116. *
  117. * @return The list of entries in the name service.
  118. */
  119. int netbios_ns_entry_count(netbios_ns_t *ns);
  120. /**
  121. * @brief Get the entry at a certain position in the entry list
  122. * @details You might want to call discover before-hand if you don't want
  123. * the lit to be empty. The entry list contains all the record known to the
  124. * name service (including resolved, reverse resolved and discovered) since the
  125. * creation of the name service object or the last call to clear
  126. *
  127. * @param ns The nameservice object.
  128. * @return A pointer to a opaque netbios_ns_entry_t structure
  129. */
  130. netbios_ns_entry_t *netbios_ns_entry_at(netbios_ns_t *ns, int pos);
  131. /**
  132. * @brief Perform an inverse netbios lookup (get name from ip)
  133. * @details This function does a NBSTAT and stores all the returned entry in
  134. * the internal list of entries. It returns one of the name found. (Normally
  135. * the <20> or <0> name)
  136. *
  137. * @param ns The name service object.
  138. * @param ip The ip address in network byte order.
  139. *
  140. * @return A null-terminated ASCII string containing the NETBIOS name. You don't
  141. * own the it (it'll be freed when destroying/clearing the name service)
  142. */
  143. const char *netbios_ns_inverse(netbios_ns_t *ns, uint32_t ip);
  144. /**
  145. * @brief Clear all the existing entries from the name service
  146. *
  147. * @param ns The nameservice object
  148. */
  149. void netbios_ns_clear(netbios_ns_t *ns);
  150. /**
  151. * @internal
  152. * @brief Add an entry to the name service list.
  153. * @details You can provide a name and/or an ip
  154. *
  155. * @param ns The name service object.
  156. * @param name The ASCII name of the entry or NULL
  157. * @param type the <X> type for this entry or -1
  158. * @param ip The IP address in network byte order (or 0)
  159. * @return The added entry
  160. */
  161. netbios_ns_entry_t *netbios_ns_entry_add(netbios_ns_t *ns, const char *name,
  162. char type, uint32_t ip);
  163. /**
  164. * @internal
  165. * @brief Find an entry in
  166. * @details [long description]
  167. *
  168. * @param ns [description]
  169. * @param by_name [description]
  170. * @param ip [description]
  171. * @return [description]
  172. */
  173. netbios_ns_entry_t *netbios_ns_entry_find(netbios_ns_t *ns, const char *by_name,
  174. uint32_t ip);
  175. #endif