/source3/nmbd/nmbd_browserdb.c

https://gitlab.com/miztake/samba · C · 180 lines · 69 code · 24 blank · 87 comment · 8 complexity · 97469d9cc73fb269bf30321d4c9ac52f MD5 · raw file

  1. /*
  2. Unix SMB/CIFS implementation.
  3. NBT netbios routines and daemon - version 2
  4. Copyright (C) Andrew Tridgell 1994-1998
  5. Copyright (C) Luke Kenneth Casson Leighton 1994-1998
  6. Copyright (C) Jeremy Allison 1994-1998
  7. Copyright (C) Christopher R. Hertel 1998
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* -------------------------------------------------------------------------- **
  20. * Modified July 1998 by CRH.
  21. * I converted this module to use the canned doubly-linked lists. I also
  22. * added comments above the functions where possible.
  23. */
  24. #include "includes.h"
  25. #include "nmbd/nmbd.h"
  26. #include "lib/util/string_wrappers.h"
  27. /* -------------------------------------------------------------------------- **
  28. * Variables...
  29. *
  30. * lmb_browserlist - This is our local master browser list.
  31. */
  32. struct browse_cache_record *lmb_browserlist;
  33. /* -------------------------------------------------------------------------- **
  34. * Functions...
  35. */
  36. /* ************************************************************************** **
  37. * Remove and free a browser list entry.
  38. *
  39. * Input: browc - A pointer to the entry to be removed from the list and
  40. * freed.
  41. * Output: none.
  42. *
  43. * ************************************************************************** **
  44. */
  45. static void remove_lmb_browser_entry( struct browse_cache_record *browc )
  46. {
  47. DLIST_REMOVE(lmb_browserlist, browc);
  48. SAFE_FREE(browc);
  49. }
  50. /* ************************************************************************** **
  51. * Update a browser death time.
  52. *
  53. * Input: browc - Pointer to the entry to be updated.
  54. * Output: none.
  55. *
  56. * ************************************************************************** **
  57. */
  58. void update_browser_death_time( struct browse_cache_record *browc )
  59. {
  60. /* Allow the new lmb to miss an announce period before we remove it. */
  61. browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
  62. }
  63. /* ************************************************************************** **
  64. * Create a browser entry and add it to the local master browser list.
  65. *
  66. * Input: work_name
  67. * browser_name
  68. * ip
  69. *
  70. * Output: Pointer to the new entry, or NULL if malloc() failed.
  71. *
  72. * ************************************************************************** **
  73. */
  74. struct browse_cache_record *create_browser_in_lmb_cache( const char *work_name,
  75. const char *browser_name,
  76. struct in_addr ip )
  77. {
  78. struct browse_cache_record *browc;
  79. time_t now = time( NULL );
  80. browc = SMB_MALLOC_P(struct browse_cache_record);
  81. if( NULL == browc ) {
  82. DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
  83. return( NULL );
  84. }
  85. memset( (char *)browc, '\0', sizeof( *browc ) );
  86. /* For a new lmb entry we want to sync with it after one minute. This
  87. will allow it time to send out a local announce and build its
  88. browse list.
  89. */
  90. browc->sync_time = now + 60;
  91. /* Allow the new lmb to miss an announce period before we remove it. */
  92. browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
  93. unstrcpy( browc->lmb_name, browser_name);
  94. unstrcpy( browc->work_group, work_name);
  95. if (!strupper_m( browc->lmb_name )) {
  96. SAFE_FREE(browc);
  97. return NULL;
  98. }
  99. if (!strupper_m( browc->work_group )) {
  100. SAFE_FREE(browc);
  101. return NULL;
  102. }
  103. browc->ip = ip;
  104. DLIST_ADD_END(lmb_browserlist, browc);
  105. DEBUG(3, ("nmbd_browserdb:create_browser_in_lmb_cache()\n"));
  106. DEBUGADD(3, (" Added lmb cache entry for workgroup %s name %s IP %s "
  107. "ttl %d\n", browc->work_group, browc->lmb_name,
  108. inet_ntoa(ip), (int)browc->death_time));
  109. return( browc );
  110. }
  111. /* ************************************************************************** **
  112. * Find a browser entry in the local master browser list.
  113. *
  114. * Input: browser_name - The name for which to search.
  115. *
  116. * Output: A pointer to the matching entry, or NULL if no match was found.
  117. *
  118. * ************************************************************************** **
  119. */
  120. struct browse_cache_record *find_browser_in_lmb_cache( const char *browser_name )
  121. {
  122. struct browse_cache_record *browc;
  123. for( browc = lmb_browserlist; browc; browc = browc->next ) {
  124. if( strequal( browser_name, browc->lmb_name ) ) {
  125. break;
  126. }
  127. }
  128. return browc;
  129. }
  130. /* ************************************************************************** **
  131. * Expire timed out browsers in the browserlist.
  132. *
  133. * Input: t - Expiration time. Entries with death times less than this
  134. * value will be removed from the list.
  135. * Output: none.
  136. *
  137. * ************************************************************************** **
  138. */
  139. void expire_lmb_browsers( time_t t )
  140. {
  141. struct browse_cache_record *browc;
  142. struct browse_cache_record *nextbrowc;
  143. for( browc = lmb_browserlist; browc; browc = nextbrowc) {
  144. nextbrowc = browc->next;
  145. if( browc->death_time < t ) {
  146. DEBUG(3, ("nmbd_browserdb:expire_lmb_browsers()\n"));
  147. DEBUGADD(3, (" Removing timed out lmb entry %s\n",
  148. browc->lmb_name));
  149. remove_lmb_browser_entry( browc );
  150. }
  151. }
  152. }