netbios_ns.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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; // NS entries cache, mainly used by discover()
  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. * @param[out] addr The IP address in network byte order of the machine if found.
  98. * @return the ipv4 address in network byte-order or 0 if it wasn't successfull.
  99. */
  100. int netbios_ns_resolve(netbios_ns_t *ns, const char *name,
  101. char type, uint32_t * addr);
  102. /**
  103. * @brief Try to discover all the Netbios/SMB speaking machine on the LAN.
  104. * @details This functions sends a message to '*' Netbios name, and waits for
  105. * the machine on the LAN to answer. It then performs a reverse lookup on all
  106. * the ip he received packet from. It stores the results inside of the name
  107. * service, allowing you to list them
  108. *
  109. *
  110. * @param ns The name service object.
  111. * @return It returns 0 in case of error.
  112. */
  113. int netbios_ns_discover(netbios_ns_t *ns);
  114. /**
  115. * @brief Get the list of entries (know machine) for this name service object
  116. * @details You might want to call discover before-hand if you don't want
  117. * the lit to be empty
  118. *
  119. * @return The list of entries in the name service.
  120. */
  121. int netbios_ns_entry_count(netbios_ns_t *ns);
  122. /**
  123. * @brief Get the entry at a certain position in the entry list
  124. * @details You might want to call discover before-hand if you don't want
  125. * the lit to be empty. The entry list contains all the record known to the
  126. * name service (including resolved, reverse resolved and discovered) since the
  127. * creation of the name service object or the last call to clear
  128. *
  129. * @param ns The nameservice object.
  130. * @param pos The index/position of the item to access in the list. Must be <
  131. * netbios_ns_entry_count(ns) or the pointer returned will be NULL.
  132. * @return A pointer to a opaque netbios_ns_entry_t structure
  133. */
  134. netbios_ns_entry_t *netbios_ns_entry_at(netbios_ns_t *ns, int pos);
  135. /**
  136. * @brief Perform an inverse netbios lookup (get name from ip)
  137. * @details This function does a NBSTAT and stores all the returned entry in
  138. * the internal list of entries. It returns one of the name found. (Normally
  139. * the <20> or <0> name)
  140. *
  141. * @param ns The name service object.
  142. * @param ip The ip address in network byte order.
  143. *
  144. * @return A null-terminated ASCII string containing the NETBIOS name. You don't
  145. * own the it (it'll be freed when destroying/clearing the name service)
  146. */
  147. const char *netbios_ns_inverse(netbios_ns_t *ns, uint32_t ip);
  148. /**
  149. * @brief Clear all the existing entries from the name service
  150. *
  151. * @param ns The nameservice object
  152. */
  153. void netbios_ns_clear(netbios_ns_t *ns);
  154. /**
  155. * @internal
  156. * @brief Add an entry to the name service list.
  157. * @details You can provide a name and/or an ip
  158. *
  159. * @param ns The name service object.
  160. * @param name The ASCII name of the entry or NULL
  161. * @param type the <X> type for this entry or -1
  162. * @param ip The IP address in network byte order (or 0)
  163. * @return The added entry
  164. */
  165. netbios_ns_entry_t *netbios_ns_entry_add(netbios_ns_t *ns, const char *name,
  166. char type, uint32_t ip);
  167. /**
  168. * @internal
  169. * @brief Find an entry in
  170. * @details [long description]
  171. *
  172. * @param ns [description]
  173. * @param by_name [description]
  174. * @param ip [description]
  175. * @return [description]
  176. */
  177. netbios_ns_entry_t *netbios_ns_entry_find(netbios_ns_t *ns, const char *by_name,
  178. uint32_t ip);
  179. #endif