/user/samba/source/nmbd/nmbd_browserdb.c

https://github.com/rhuitl/uClinux · C · 176 lines · 64 code · 24 blank · 88 comment · 8 complexity · 79617444cded5c73d104c88e368fe73d 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 2 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, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /* -------------------------------------------------------------------------- **
  21. * Modified July 1998 by CRH.
  22. * I converted this module to use the canned doubly-linked lists. I also
  23. * added comments above the functions where possible.
  24. */
  25. #include "includes.h"
  26. /* -------------------------------------------------------------------------- **
  27. * Variables...
  28. *
  29. * lmb_browserlist - This is our local master browser list.
  30. */
  31. struct browse_cache_record *lmb_browserlist;
  32. /* -------------------------------------------------------------------------- **
  33. * Functions...
  34. */
  35. /* ************************************************************************** **
  36. * Remove and free a browser list entry.
  37. *
  38. * Input: browc - A pointer to the entry to be removed from the list and
  39. * freed.
  40. * Output: none.
  41. *
  42. * ************************************************************************** **
  43. */
  44. static void remove_lmb_browser_entry( struct browse_cache_record *browc )
  45. {
  46. DLIST_REMOVE(lmb_browserlist, browc);
  47. SAFE_FREE(browc);
  48. }
  49. /* ************************************************************************** **
  50. * Update a browser death time.
  51. *
  52. * Input: browc - Pointer to the entry to be updated.
  53. * Output: none.
  54. *
  55. * ************************************************************************** **
  56. */
  57. void update_browser_death_time( struct browse_cache_record *browc )
  58. {
  59. /* Allow the new lmb to miss an announce period before we remove it. */
  60. browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
  61. }
  62. /* ************************************************************************** **
  63. * Create a browser entry and add it to the local master browser list.
  64. *
  65. * Input: work_name
  66. * browser_name
  67. * ip
  68. *
  69. * Output: Pointer to the new entry, or NULL if malloc() failed.
  70. *
  71. * ************************************************************************** **
  72. */
  73. struct browse_cache_record *create_browser_in_lmb_cache( const char *work_name,
  74. const char *browser_name,
  75. struct in_addr ip )
  76. {
  77. struct browse_cache_record *browc;
  78. time_t now = time( NULL );
  79. browc = SMB_MALLOC_P(struct browse_cache_record);
  80. if( NULL == browc ) {
  81. DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
  82. return( NULL );
  83. }
  84. memset( (char *)browc, '\0', sizeof( *browc ) );
  85. /* For a new lmb entry we want to sync with it after one minute. This
  86. will allow it time to send out a local announce and build its
  87. browse list.
  88. */
  89. browc->sync_time = now + 60;
  90. /* Allow the new lmb to miss an announce period before we remove it. */
  91. browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
  92. unstrcpy( browc->lmb_name, browser_name);
  93. unstrcpy( browc->work_group, work_name);
  94. strupper_m( browc->lmb_name );
  95. strupper_m( browc->work_group );
  96. browc->ip = ip;
  97. DLIST_ADD_END(lmb_browserlist, browc, struct browse_cache_record *);
  98. if( DEBUGLVL( 3 ) ) {
  99. Debug1( "nmbd_browserdb:create_browser_in_lmb_cache()\n" );
  100. Debug1( " Added lmb cache entry for workgroup %s ", browc->work_group );
  101. Debug1( "name %s IP %s ", browc->lmb_name, inet_ntoa(ip) );
  102. Debug1( "ttl %d\n", (int)browc->death_time );
  103. }
  104. return( browc );
  105. }
  106. /* ************************************************************************** **
  107. * Find a browser entry in the local master browser list.
  108. *
  109. * Input: browser_name - The name for which to search.
  110. *
  111. * Output: A pointer to the matching entry, or NULL if no match was found.
  112. *
  113. * ************************************************************************** **
  114. */
  115. struct browse_cache_record *find_browser_in_lmb_cache( const char *browser_name )
  116. {
  117. struct browse_cache_record *browc;
  118. for( browc = lmb_browserlist; browc; browc = browc->next ) {
  119. if( strequal( browser_name, browc->lmb_name ) ) {
  120. break;
  121. }
  122. }
  123. return browc;
  124. }
  125. /* ************************************************************************** **
  126. * Expire timed out browsers in the browserlist.
  127. *
  128. * Input: t - Expiration time. Entries with death times less than this
  129. * value will be removed from the list.
  130. * Output: none.
  131. *
  132. * ************************************************************************** **
  133. */
  134. void expire_lmb_browsers( time_t t )
  135. {
  136. struct browse_cache_record *browc;
  137. struct browse_cache_record *nextbrowc;
  138. for( browc = lmb_browserlist; browc; browc = nextbrowc) {
  139. nextbrowc = browc->next;
  140. if( browc->death_time < t ) {
  141. if( DEBUGLVL( 3 ) ) {
  142. Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" );
  143. Debug1( " Removing timed out lmb entry %s\n", browc->lmb_name );
  144. }
  145. remove_lmb_browser_entry( browc );
  146. }
  147. }
  148. }