discover.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include "bdsm.h"
  26. static void print_entry(const char *what, void *p_opaque,
  27. netbios_ns_entry *entry)
  28. {
  29. struct in_addr addr;
  30. addr.s_addr = netbios_ns_entry_ip(entry);
  31. printf("%s(%p): Ip: %s, name: %s/%s<%x>\n",
  32. what,
  33. p_opaque,
  34. inet_ntoa(addr),
  35. netbios_ns_entry_group(entry),
  36. netbios_ns_entry_name(entry),
  37. netbios_ns_entry_type(entry));
  38. }
  39. static void on_entry_added(void *p_opaque, netbios_ns_entry *entry)
  40. {
  41. print_entry("added", p_opaque, entry);
  42. }
  43. static void on_entry_removed(void *p_opaque, netbios_ns_entry *entry)
  44. {
  45. print_entry("removed", p_opaque, entry);
  46. }
  47. int main()
  48. {
  49. netbios_ns *ns;
  50. netbios_ns_discover_callbacks callbacks;
  51. ns = netbios_ns_new();
  52. callbacks.p_opaque = (void*)0x42;
  53. callbacks.pf_on_entry_added = on_entry_added;
  54. callbacks.pf_on_entry_removed = on_entry_removed;
  55. printf("Discovering...\nPress Enter to quit\n");
  56. if (!netbios_ns_discover_start(ns,
  57. 4, // broadcast every 4 seconds
  58. &callbacks))
  59. {
  60. fprintf(stderr, "Error while discovering local network\n");
  61. exit(42);
  62. }
  63. getchar();
  64. netbios_ns_discover_stop(ns);
  65. return (0);
  66. }