inverse.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifdef _WIN32
  41. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  42. int inet_pton (int af, const char *src, void *dst)
  43. {
  44. unsigned char *b = dst;
  45. switch (af)
  46. {
  47. case AF_INET:
  48. return sscanf (src, "%hhu.%hhu.%hhu.%hhu",
  49. b + 0, b + 1, b + 2, b + 3) == 4;
  50. }
  51. errno = EAFNOSUPPORT;
  52. return -1;
  53. }
  54. #endif
  55. int main(int ac, char **av)
  56. {
  57. netbios_ns *ns;
  58. struct in_addr addr;
  59. const char *name;
  60. ns = netbios_ns_new();
  61. if (ac != 2)
  62. {
  63. fprintf(stderr, "%s usage: %s a.b.c.d\n", av[0], av[0]);
  64. fprintf(stderr, "Print the netbios name for this IP address\n");
  65. exit(1);
  66. }
  67. inet_pton(AF_INET, av[1], &addr);
  68. if ((name = netbios_ns_inverse(ns, addr.s_addr)) == NULL)
  69. {
  70. fprintf(stderr, "Unable to perform inverse name resolution for %s\n", av[1]);
  71. exit(42);
  72. }
  73. printf("%s\n", name);
  74. netbios_ns_destroy(ns);
  75. return 0;
  76. }