netbios_ns.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <stdint.h>
  21. /**
  22. * @file netbios_ns.h
  23. * @brief Netbios name service
  24. */
  25. typedef struct netbios_ns_entry netbios_ns_entry;
  26. typedef struct netbios_ns netbios_ns;
  27. /**
  28. * @brief Get the name of the entry referenced by the iterator iter.
  29. * @details The pointer points to an area of memory owned by the netbios name
  30. * service
  31. *
  32. * @return A null-terminated ASCII string representing the name of a netbios machine.
  33. */
  34. const char *netbios_ns_entry_name(netbios_ns_entry *entry);
  35. /**
  36. * @brief Get the name of the entry referenced by the iterator iter.
  37. * @details The pointer points to an area of memory owned by the netbios name
  38. * service
  39. *
  40. * @return A null-terminated ASCII string representing the group of a netbios machine.
  41. */
  42. const char *netbios_ns_entry_group(netbios_ns_entry *entry);
  43. /**
  44. * @brief Return the IP address of the correspondance referenced by the iterator
  45. *
  46. * @return The ip address of this entry, in network byte order.
  47. */
  48. uint32_t netbios_ns_entry_ip(netbios_ns_entry *entry);
  49. /**
  50. * @brief Return the type of record
  51. *
  52. * @return The type of netbios record (.ie 0x20 for FileServer,
  53. * 0 for workstation, etc.) or a value < 0 if the iterator is invalid or an
  54. * error occured.
  55. */
  56. char netbios_ns_entry_type(netbios_ns_entry *entry);
  57. /**
  58. * @brief Allocate and initialize the Netbios name service client object.
  59. * @return A newly allocated netbios_ns ready for querying.
  60. * Deallocate with netbios_ns_destroy().
  61. */
  62. netbios_ns *netbios_ns_new();
  63. /**
  64. * @brief Destroy the netbios name service object
  65. * @param[in] ns A pointer on the netbios_ns to destroy and deallocate
  66. */
  67. void netbios_ns_destroy(netbios_ns *ns);
  68. /**
  69. * @brief Resolve a Netbios name
  70. * @details This function tries to resolves the given NetBIOS name with the
  71. * given type on the LAN, using broadcast queries. No WINS server is called.
  72. *
  73. * @param ns the netbios name service object.
  74. * @param name the null-terminated ASCII netbios name to resolve. If it's
  75. * longer than 15 chars, it'll be truncated.
  76. * @param type The type of the name to look for. @see netbios_defs.h
  77. * @param[out] addr The IP address in network byte order of the machine if found.
  78. * @return a value > 0 if successfull, or 0 otherwise
  79. */
  80. int netbios_ns_resolve(netbios_ns *ns, const char *name,
  81. char type, uint32_t *addr);
  82. /**
  83. * @brief Try to discover all the Netbios/SMB speaking machine on the LAN.
  84. * @details This functions sends a message to '*' Netbios name, and waits for
  85. * the machine on the LAN to answer. It then performs a reverse lookup on all
  86. * the ip he received packet from. It stores the results inside of the name
  87. * service, allowing you to list them
  88. *
  89. *
  90. * @param ns The name service object.
  91. * @return It returns 0 in case of error.
  92. */
  93. int netbios_ns_discover(netbios_ns *ns);
  94. /**
  95. * @brief Get the list of entries (know machine) for this name service object
  96. * @details You might want to call discover before-hand if you don't want
  97. * the lit to be empty
  98. *
  99. * @return The list of entries in the name service.
  100. */
  101. int netbios_ns_entry_count(netbios_ns *ns);
  102. /**
  103. * @brief Get the entry at a certain position in the entry list
  104. * @details You might want to call discover before-hand if you don't want
  105. * the lit to be empty. The entry list contains all the record known to the
  106. * name service (including resolved, reverse resolved and discovered) since the
  107. * creation of the name service object or the last call to clear
  108. *
  109. * @param ns The nameservice object.
  110. * @param pos The index/position of the item to access in the list. Must be <
  111. * netbios_ns_entry_count(ns) or the pointer returned will be NULL.
  112. * @return A pointer to a opaque netbios_ns_entry structure
  113. */
  114. netbios_ns_entry *netbios_ns_entry_at(netbios_ns *ns, int pos);
  115. /**
  116. * @brief Perform an inverse netbios lookup (get name from ip)
  117. * @details This function does a NBSTAT and stores all the returned entry in
  118. * the internal list of entries. It returns one of the name found. (Normally
  119. * the <20> or <0> name)
  120. *
  121. * @param ns The name service object.
  122. * @param ip The ip address in network byte order.
  123. *
  124. * @return A null-terminated ASCII string containing the NETBIOS name. You don't
  125. * own the it (it'll be freed when destroying/clearing the name service)
  126. */
  127. const char *netbios_ns_inverse(netbios_ns *ns, uint32_t ip);
  128. /**
  129. * @brief Clear all the existing entries from the name service
  130. *
  131. * @param ns The nameservice object
  132. */
  133. void netbios_ns_clear(netbios_ns *ns);
  134. /**
  135. * @brief abort any pending netbios_ns_* operations
  136. *
  137. *
  138. * @param ns The name service object.
  139. */
  140. void netbios_ns_abort(netbios_ns *ns);
  141. #endif