62efcd3e975cb092fc44203e2a0d574a447080b6
[samba.git] / source4 / torture / nbt / wins.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT WINS server testing
5
6    Copyright (C) Andrew Tridgell 2005
7    
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    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/nbt/libnbt.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
26
27 #define CHECK_VALUE(v, correct) do { \
28         if ((v) != (correct)) { \
29                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
30                        __location__, #v, v, correct); \
31                 ret = False; \
32         }} while (0)
33
34 #define CHECK_STRING(v, correct) do { \
35         if (StrCaseCmp(v, correct) != 0) { \
36                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
37                        __location__, #v, v, correct); \
38                 ret = False; \
39         }} while (0)
40
41
42 /*
43   test operations against a WINS server
44 */
45 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
46                               const char *address)
47 {
48         struct nbt_name_register_wins io;
49         struct nbt_name_query query;
50         struct nbt_name_refresh_wins refresh;
51         struct nbt_name_release release;
52         NTSTATUS status;
53         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
54         BOOL ret = True;
55         const char *myaddress = talloc_strdup(mem_ctx, iface_n_ip(0));
56         const char *tname = talloc_asprintf(mem_ctx, "_TORTURE-%5u", 
57                                           (unsigned)(random() % (100000)));
58
59         /* we do the listen here to ensure the WINS server receives the packets from
60            the right IP */
61         socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
62
63         printf("Testing name registration to WINS with name '%s' at %s\n", tname, myaddress);
64
65         io.in.name.name = tname;
66         io.in.name.type = NBT_NAME_CLIENT;
67         io.in.name.scope = NULL;
68         io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
69         io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
70         io.in.nb_flags = NBT_NODE_H;
71         io.in.ttl = 300000;
72         
73         status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
74         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
75                 printf("No response from %s for name register\n", address);
76                 return False;
77         }
78         if (!NT_STATUS_IS_OK(status)) {
79                 printf("Bad response from %s for name register - %s\n",
80                        address, nt_errstr(status));
81                 return False;
82         }
83         
84         CHECK_STRING(io.out.wins_server, address);
85         CHECK_VALUE(io.out.rcode, 0);
86
87         printf("query the name to make sure its there\n");
88         query.in.name = io.in.name;
89         query.in.dest_addr = address;
90         query.in.broadcast = False;
91         query.in.wins_lookup = True;
92         query.in.timeout = 3;
93         query.in.retries = 0;
94
95         status = nbt_name_query(nbtsock, mem_ctx, &query);
96         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
97                 printf("No response from %s for name query\n", address);
98                 return False;
99         }
100         if (!NT_STATUS_IS_OK(status)) {
101                 printf("Bad response from %s for name query - %s\n",
102                        address, nt_errstr(status));
103                 return False;
104         }
105         
106         CHECK_STRING(query.out.name.name, tname);
107         CHECK_VALUE(query.out.name.type, NBT_NAME_CLIENT);
108         CHECK_VALUE(query.out.num_addrs, 1);
109         CHECK_STRING(query.out.reply_addrs[0], myaddress);
110
111         printf("refresh the name\n");
112         refresh.in.name.name = tname;
113         refresh.in.name.type = NBT_NAME_CLIENT;
114         refresh.in.name.scope = NULL;
115         refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
116         refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
117         refresh.in.nb_flags = NBT_NODE_H;
118         refresh.in.ttl = 12345;
119         
120         status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
121         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
122                 printf("No response from %s for name refresh\n", address);
123                 return False;
124         }
125         if (!NT_STATUS_IS_OK(status)) {
126                 printf("Bad response from %s for name refresh - %s\n",
127                        address, nt_errstr(status));
128                 return False;
129         }
130         
131         CHECK_STRING(io.out.wins_server, address);
132         CHECK_VALUE(io.out.rcode, 0);
133
134         printf("release the name\n");
135         release.in.name = io.in.name;
136         release.in.dest_addr = address;
137         release.in.address = myaddress;
138         release.in.nb_flags = NBT_NODE_H;
139         release.in.broadcast = False;
140         release.in.timeout = 3;
141         release.in.retries = 0;
142
143         status = nbt_name_release(nbtsock, mem_ctx, &release);
144         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
145                 printf("No response from %s for name release\n", address);
146                 return False;
147         }
148         if (!NT_STATUS_IS_OK(status)) {
149                 printf("Bad response from %s for name query - %s\n",
150                        address, nt_errstr(status));
151                 return False;
152         }
153         
154         CHECK_STRING(release.out.name.name, tname);
155         CHECK_VALUE(release.out.name.type, NBT_NAME_CLIENT);
156         CHECK_VALUE(release.out.rcode, 0);
157
158         printf("release again\n");
159         status = nbt_name_release(nbtsock, mem_ctx, &release);
160         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
161                 printf("No response from %s for name release\n", address);
162                 return False;
163         }
164         if (!NT_STATUS_IS_OK(status)) {
165                 printf("Bad response from %s for name query - %s\n",
166                        address, nt_errstr(status));
167                 return False;
168         }
169         
170         CHECK_STRING(release.out.name.name, tname);
171         CHECK_VALUE(release.out.name.type, NBT_NAME_CLIENT);
172         CHECK_VALUE(release.out.rcode, 0);
173
174
175         printf("query the name to make sure its gone\n");
176         status = nbt_name_query(nbtsock, mem_ctx, &query);
177         if (NT_STATUS_IS_OK(status)) {
178                 printf("ERROR: Name query success after release\n");
179                 return False;
180         }
181         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
182                 printf("Incorrect response to name query - %s\n", nt_errstr(status));
183                 return False;
184         }
185         
186         return ret;
187 }
188
189
190 /*
191   test WINS operations
192 */
193 BOOL torture_nbt_wins(void)
194 {
195         const char *address;
196         struct nbt_name name;
197         TALLOC_CTX *mem_ctx = talloc_new(NULL);
198         NTSTATUS status;
199         BOOL ret = True;
200         
201         name.name = lp_parm_string(-1, "torture", "host");
202         name.type = NBT_NAME_SERVER;
203         name.scope = NULL;
204
205         /* do an initial name resolution to find its IP */
206         status = resolve_name(&name, mem_ctx, &address);
207         if (!NT_STATUS_IS_OK(status)) {
208                 printf("Failed to resolve %s - %s\n",
209                        name.name, nt_errstr(status));
210                 talloc_free(mem_ctx);
211                 return False;
212         }
213
214         ret &= nbt_test_wins(mem_ctx, &name, address);
215
216         talloc_free(mem_ctx);
217
218         return ret;
219 }