netbios_ns.h 5.3 KB

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