discover.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*****************************************************************************
  2. * __________________ _________ _____ _____ .__ ._.
  3. * \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. * | | \| ` \/ / Y \ / | | \ ___/ \|
  6. * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. * \/ \/ \/ \/ )/ \/ \/ \/
  8. *
  9. * This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
  10. *
  11. * Author: Julien 'Lta' BALLET <contact@lta.io>
  12. *
  13. * liBDSM is released under LGPLv2.1 (or later) and is also available
  14. * under a commercial license.
  15. *****************************************************************************
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU Lesser General Public License as published by
  18. * the Free Software Foundation; either version 2.1 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  29. *****************************************************************************/
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #if !defined( _WIN32 )
  35. # include <arpa/inet.h>
  36. #else
  37. # include <winsock2.h>
  38. #endif
  39. #include "bdsm.h"
  40. static void print_entry(const char *what, void *p_opaque,
  41. netbios_ns_entry *entry)
  42. {
  43. struct in_addr addr;
  44. addr.s_addr = netbios_ns_entry_ip(entry);
  45. printf("%s(%p): Ip: %s, name: %s/%s<%x>\n",
  46. what,
  47. p_opaque,
  48. inet_ntoa(addr),
  49. netbios_ns_entry_group(entry),
  50. netbios_ns_entry_name(entry),
  51. netbios_ns_entry_type(entry));
  52. }
  53. static void on_entry_added(void *p_opaque, netbios_ns_entry *entry)
  54. {
  55. print_entry("added", p_opaque, entry);
  56. }
  57. static void on_entry_removed(void *p_opaque, netbios_ns_entry *entry)
  58. {
  59. print_entry("removed", p_opaque, entry);
  60. }
  61. int main()
  62. {
  63. netbios_ns *ns;
  64. netbios_ns_discover_callbacks callbacks;
  65. ns = netbios_ns_new();
  66. callbacks.p_opaque = (void*)0x42;
  67. callbacks.pf_on_entry_added = on_entry_added;
  68. callbacks.pf_on_entry_removed = on_entry_removed;
  69. printf("Discovering...\nPress Enter to quit\n");
  70. if (netbios_ns_discover_start(ns,
  71. 4, // broadcast every 4 seconds
  72. &callbacks))
  73. {
  74. fprintf(stderr, "Error while discovering local network\n");
  75. exit(42);
  76. }
  77. getchar();
  78. netbios_ns_discover_stop(ns);
  79. netbios_ns_destroy(ns);
  80. return 0;
  81. }