.cvsignore: Added make_smbcodepage
[jra/samba/.git] / source3 / namedbresp.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios library routines
5    Copyright (C) Andrew Tridgell 1994-1997
6    
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 2 of the License, or
10    (at your option) any later version.
11    
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    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21    Module name: namedbresp.c
22
23 */
24
25 #include "includes.h"
26
27 extern int ClientNMB;
28
29 extern struct subnet_record *subnetlist;
30
31 extern int DEBUGLEVEL;
32
33 extern pstring scope;
34 extern pstring myname;
35 extern struct in_addr ipzero;
36
37 int num_response_packets = 0;
38
39 /***************************************************************************
40   add an expected response record into the list
41   **************************************************************************/
42 void add_response_record(struct subnet_record *d,
43                                 struct response_record *n)
44 {
45   struct response_record *n2;
46
47   if (!d) return;
48
49   num_response_packets++; /* count of total number of packets still around */
50
51   DEBUG(4,("adding response record id:%d num_records:%d\n",
52                    n->response_id, num_response_packets));
53
54   if (!d->responselist)
55     {
56       d->responselist = n;
57       n->prev = NULL;
58       n->next = NULL;
59       return;
60     }
61   
62   for (n2 = d->responselist; n2->next; n2 = n2->next) ;
63   
64   n2->next = n;
65   n->next = NULL;
66   n->prev = n2;
67 }
68
69
70 /***************************************************************************
71   remove an expected response record from the list
72   **************************************************************************/
73 void remove_response_record(struct subnet_record *d,
74                                 struct response_record *n)
75 {
76         if (!d) return;
77
78         if (n->prev) n->prev->next = n->next;
79         if (n->next) n->next->prev = n->prev;
80
81         if (d->responselist == n) d->responselist = n->next; 
82
83         free(n);
84
85         num_response_packets--; /* count of total number of packets still around */
86 }
87
88
89 /****************************************************************************
90   create a name query response record
91   **************************************************************************/
92 struct response_record *make_response_queue_record(enum state_type state,
93                                 int id,uint16 fd,
94                                 int quest_type, char *name,int type, int nb_flags, time_t ttl,
95                                 int server_type, char *my_name, char *my_comment,
96                                 BOOL bcast,BOOL recurse,
97                                 struct in_addr send_ip, struct in_addr reply_to_ip,
98                                 int reply_id)
99 {
100   struct response_record *n;
101         
102   if (!name || !name[0]) return NULL;
103         
104   if (!(n = (struct response_record *)malloc(sizeof(*n)))) 
105     return(NULL);
106
107   bzero((char *)n, sizeof(*n));
108
109   n->response_id = id;
110   n->state = state;
111   n->fd = fd;
112   n->quest_type = quest_type;
113   make_nmb_name(&n->name, name, type, scope);
114   n->nb_flags = nb_flags;
115   n->ttl = ttl;
116   n->server_type = server_type;
117   n->bcast = bcast;
118   n->recurse = recurse;
119   n->send_ip = send_ip;
120   n->reply_to_ip = reply_to_ip;
121   n->reply_id = reply_id;
122   if(my_name)
123     StrnCpy(n->my_name, my_name, sizeof(n->my_name)-1);
124   else
125     *n->my_name = 0;
126   if(my_comment)
127     StrnCpy(n->my_comment, my_comment, sizeof(n->my_comment)-1);
128   else
129     *n->my_comment = 0;
130   n->repeat_interval = 1; /* XXXX should be in ms */
131   n->repeat_count = 3; /* 3 retries */
132   n->repeat_time = time(NULL) + n->repeat_interval; /* initial retry time */
133
134   n->num_msgs = 0;
135
136   return n;
137 }
138
139
140 /****************************************************************************
141   find a response in a subnet's name query response list. 
142   **************************************************************************/
143 struct response_record *find_response_record(struct subnet_record **d,
144                                 uint16 id)
145 {  
146   struct response_record *n;
147
148   if (!d) return NULL;
149
150   for ((*d) = FIRST_SUBNET; (*d); (*d) = NEXT_SUBNET_INCLUDING_WINS(*d))
151   {
152     for (n = (*d)->responselist; n; n = n->next)
153     {
154       if (n->response_id == id) {
155          DEBUG(4, ("found response record on %s: %d\n",
156                                         inet_ntoa((*d)->bcast_ip), id));
157          return n;
158       }
159     }
160   }
161
162   *d = NULL;
163
164   return NULL;
165 }
166
167