PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/samba-3.5.6/source3/nmbd/nmbd_responserecordsdb.c

https://github.com/theuni/XBMC-deps
C | 243 lines | 144 code | 46 blank | 53 comment | 34 complexity | 1debe3946d1630f7c7cb7e90ea4c166c MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. NBT netbios library routines
  4. Copyright (C) Andrew Tridgell 1994-1998
  5. Copyright (C) Luke Kenneth Casson Leighton 1994-1998
  6. Copyright (C) Jeremy Allison 1994-1998
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "includes.h"
  19. int num_response_packets = 0;
  20. /***************************************************************************
  21. Add an expected response record into the list
  22. **************************************************************************/
  23. static void add_response_record(struct subnet_record *subrec,
  24. struct response_record *rrec)
  25. {
  26. num_response_packets++; /* count of total number of packets still around */
  27. DEBUG(4,("add_response_record: adding response record id:%hu to subnet %s. num_records:%d\n",
  28. rrec->response_id, subrec->subnet_name, num_response_packets));
  29. DLIST_ADD_END(subrec->responselist, rrec, struct response_record *);
  30. }
  31. /***************************************************************************
  32. Remove an expected response record from the list
  33. **************************************************************************/
  34. void remove_response_record(struct subnet_record *subrec,
  35. struct response_record *rrec)
  36. {
  37. /* It is possible this can be called twice,
  38. with a rrec pointer that has been freed. So
  39. before we inderect into rrec, search for it
  40. on the responselist first. Bug #3617. JRA. */
  41. struct response_record *p = NULL;
  42. for (p = subrec->responselist; p; p = p->next) {
  43. if (p == rrec) {
  44. break;
  45. }
  46. }
  47. if (p == NULL) {
  48. /* We didn't find rrec on the list. */
  49. return;
  50. }
  51. DLIST_REMOVE(subrec->responselist, rrec);
  52. if(rrec->userdata) {
  53. if(rrec->userdata->free_fn) {
  54. (*rrec->userdata->free_fn)(rrec->userdata);
  55. } else {
  56. ZERO_STRUCTP(rrec->userdata);
  57. SAFE_FREE(rrec->userdata);
  58. }
  59. }
  60. /* Ensure we can delete. */
  61. rrec->packet->locked = False;
  62. free_packet(rrec->packet);
  63. ZERO_STRUCTP(rrec);
  64. SAFE_FREE(rrec);
  65. num_response_packets--; /* count of total number of packets still around */
  66. }
  67. /****************************************************************************
  68. Create a response record for an outgoing packet.
  69. **************************************************************************/
  70. struct response_record *make_response_record( struct subnet_record *subrec,
  71. struct packet_struct *p,
  72. response_function resp_fn,
  73. timeout_response_function timeout_fn,
  74. success_function success_fn,
  75. fail_function fail_fn,
  76. struct userdata_struct *userdata)
  77. {
  78. struct response_record *rrec;
  79. struct nmb_packet *nmb = &p->packet.nmb;
  80. if (!(rrec = SMB_MALLOC_P(struct response_record))) {
  81. DEBUG(0,("make_response_queue_record: malloc fail for response_record.\n"));
  82. return NULL;
  83. }
  84. memset((char *)rrec, '\0', sizeof(*rrec));
  85. rrec->response_id = nmb->header.name_trn_id;
  86. rrec->resp_fn = resp_fn;
  87. rrec->timeout_fn = timeout_fn;
  88. rrec->success_fn = success_fn;
  89. rrec->fail_fn = fail_fn;
  90. rrec->packet = p;
  91. if(userdata) {
  92. /* Intelligent userdata. */
  93. if(userdata->copy_fn) {
  94. if((rrec->userdata = (*userdata->copy_fn)(userdata)) == NULL) {
  95. DEBUG(0,("make_response_queue_record: copy fail for userdata.\n"));
  96. ZERO_STRUCTP(rrec);
  97. SAFE_FREE(rrec);
  98. return NULL;
  99. }
  100. } else {
  101. /* Primitive userdata, do a memcpy. */
  102. if((rrec->userdata = (struct userdata_struct *)
  103. SMB_MALLOC(sizeof(struct userdata_struct)+userdata->userdata_len)) == NULL) {
  104. DEBUG(0,("make_response_queue_record: malloc fail for userdata.\n"));
  105. ZERO_STRUCTP(rrec);
  106. SAFE_FREE(rrec);
  107. return NULL;
  108. }
  109. rrec->userdata->copy_fn = userdata->copy_fn;
  110. rrec->userdata->free_fn = userdata->free_fn;
  111. rrec->userdata->userdata_len = userdata->userdata_len;
  112. memcpy(rrec->userdata->data, userdata->data, userdata->userdata_len);
  113. }
  114. } else {
  115. rrec->userdata = NULL;
  116. }
  117. rrec->num_msgs = 0;
  118. if(!nmb->header.nm_flags.bcast)
  119. rrec->repeat_interval = 5; /* 5 seconds for unicast packets. */
  120. else
  121. rrec->repeat_interval = 1; /* XXXX should be in ms */
  122. rrec->repeat_count = 3; /* 3 retries */
  123. rrec->repeat_time = time(NULL) + rrec->repeat_interval; /* initial retry time */
  124. /* This packet is not being processed. */
  125. rrec->in_expiration_processing = False;
  126. /* Lock the packet so we won't lose it while it's on the list. */
  127. p->locked = True;
  128. add_response_record(subrec, rrec);
  129. return rrec;
  130. }
  131. /****************************************************************************
  132. Find a response in a subnet's name query response list.
  133. **************************************************************************/
  134. static struct response_record *find_response_record_on_subnet(
  135. struct subnet_record *subrec, uint16 id)
  136. {
  137. struct response_record *rrec = NULL;
  138. for (rrec = subrec->responselist; rrec; rrec = rrec->next) {
  139. if (rrec->response_id == id) {
  140. DEBUG(4, ("find_response_record: found response record id = %hu on subnet %s\n",
  141. id, subrec->subnet_name));
  142. break;
  143. }
  144. }
  145. return rrec;
  146. }
  147. /****************************************************************************
  148. Find a response in any subnet's name query response list.
  149. **************************************************************************/
  150. struct response_record *find_response_record(struct subnet_record **ppsubrec,
  151. uint16 id)
  152. {
  153. struct response_record *rrec = NULL;
  154. for ((*ppsubrec) = FIRST_SUBNET; (*ppsubrec);
  155. (*ppsubrec) = NEXT_SUBNET_INCLUDING_UNICAST(*ppsubrec)) {
  156. if((rrec = find_response_record_on_subnet(*ppsubrec, id)) != NULL)
  157. return rrec;
  158. }
  159. /* There should never be response records on the remote_broadcast subnet.
  160. Sanity check to ensure this is so. */
  161. if(remote_broadcast_subnet->responselist != NULL) {
  162. DEBUG(0,("find_response_record: response record found on subnet %s. This should \
  163. never happen !\n", remote_broadcast_subnet->subnet_name));
  164. }
  165. /* Now check the WINS server subnet if it exists. */
  166. if(wins_server_subnet != NULL) {
  167. *ppsubrec = wins_server_subnet;
  168. if((rrec = find_response_record_on_subnet(*ppsubrec, id))!= NULL)
  169. return rrec;
  170. }
  171. DEBUG(3,("find_response_record: response packet id %hu received with no \
  172. matching record.\n", id));
  173. *ppsubrec = NULL;
  174. return NULL;
  175. }
  176. /****************************************************************************
  177. Check if a refresh is queued for a particular name on a particular subnet.
  178. **************************************************************************/
  179. bool is_refresh_already_queued(struct subnet_record *subrec, struct name_record *namerec)
  180. {
  181. struct response_record *rrec = NULL;
  182. for (rrec = subrec->responselist; rrec; rrec = rrec->next) {
  183. struct packet_struct *p = rrec->packet;
  184. struct nmb_packet *nmb = &p->packet.nmb;
  185. if((nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
  186. (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9)) {
  187. /* Yes it's a queued refresh - check if the name is correct. */
  188. if(nmb_name_equal(&nmb->question.question_name, &namerec->name))
  189. return True;
  190. }
  191. }
  192. return False;
  193. }