netbios_ns_entry.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "bdsm/netbios_ns.h"
  23. const char *netbios_ns_entry_name(netbios_ns_entry *entry)
  24. {
  25. if (entry != NULL)
  26. return (entry->name);
  27. else
  28. return (NULL);
  29. }
  30. uint32_t netbios_ns_entry_ip(netbios_ns_entry *entry)
  31. {
  32. if (entry != NULL)
  33. return (entry->address.s_addr);
  34. else
  35. return (0);
  36. }
  37. char netbios_ns_entry_type(netbios_ns_entry *entry)
  38. {
  39. if (entry != NULL)
  40. return (entry->type);
  41. else
  42. return (-1);
  43. }
  44. void netbios_ns_clear(netbios_ns *ns)
  45. {
  46. netbios_ns_entry *next;
  47. assert(ns != NULL);
  48. while (ns->entries != NULL)
  49. {
  50. next = ns->entries->next;
  51. free(ns->entries);
  52. ns->entries = next;
  53. }
  54. }
  55. netbios_ns_entry *netbios_ns_entry_add(netbios_ns *ns, const char *name,
  56. char type, uint32_t ip)
  57. {
  58. netbios_ns_entry *entry;
  59. entry = malloc(sizeof(netbios_ns_entry));
  60. assert(entry != NULL);
  61. memset((void *)entry, 0, sizeof(netbios_ns_entry));
  62. if (name != NULL)
  63. {
  64. memcpy(entry->name, name, NETBIOS_NAME_LENGTH);
  65. entry->name[NETBIOS_NAME_LENGTH] = 0;
  66. for (int i = 1; i < NETBIOS_NAME_LENGTH; i++ )
  67. if (entry->name[NETBIOS_NAME_LENGTH - i] == ' ')
  68. entry->name[NETBIOS_NAME_LENGTH - i] = 0;
  69. else
  70. break;
  71. }
  72. entry->type = type;
  73. entry->address.s_addr = ip;
  74. entry->next = ns->entries;
  75. ns->entries = entry;
  76. return (ns->entries);
  77. }
  78. // Find an entry in the list. Search by name if name is not NULL,
  79. // or by ip otherwise
  80. netbios_ns_entry *netbios_ns_entry_find(netbios_ns *ns, const char *by_name,
  81. uint32_t ip)
  82. {
  83. netbios_ns_entry *found = NULL;
  84. netbios_ns_entry *iter;
  85. assert(ns != NULL);
  86. iter = ns->entries;
  87. while (iter != NULL && found == NULL)
  88. {
  89. if (by_name != NULL)
  90. {
  91. if (!strncmp(by_name, iter->name, NETBIOS_NAME_LENGTH))
  92. found = iter;
  93. }
  94. else if (iter->address.s_addr == ip)
  95. found = iter;
  96. iter = iter->next;
  97. }
  98. return (found);
  99. }
  100. int netbios_ns_entry_count(netbios_ns *ns)
  101. {
  102. netbios_ns_entry *iter;
  103. int res;
  104. assert(ns != NULL);
  105. iter = ns->entries;
  106. res = 0;
  107. while (iter != NULL)
  108. {
  109. res++;
  110. iter = iter->next;
  111. }
  112. return (res);
  113. }
  114. netbios_ns_entry *netbios_ns_entry_at(netbios_ns *ns, int pos)
  115. {
  116. netbios_ns_entry *iter = NULL;
  117. int i = 0;
  118. assert(ns != NULL);
  119. iter = ns->entries;
  120. while (i < pos && iter != NULL)
  121. {
  122. i++;
  123. iter = iter->next;
  124. }
  125. return (iter);
  126. }