r13924: Split more prototypes out of include/proto.h + initial work on header
[samba.git] / source4 / torture / nbt / register.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT name registration 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 "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "netif/netif.h"
28
29 #define CHECK_VALUE(v, correct) do { \
30         if ((v) != (correct)) { \
31                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
32                        __location__, #v, (int)v, (int)correct); \
33                 ret = False; \
34         }} while (0)
35
36 #define CHECK_STRING(v, correct) do { \
37         if (strcasecmp_m(v, correct) != 0) { \
38                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
39                        __location__, #v, v, correct); \
40                 ret = False; \
41         }} while (0)
42
43 /*
44   test that a server responds correctly to attempted registrations of its name
45 */
46 static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
47                              const char *address)
48 {
49         struct nbt_name_register io;
50         NTSTATUS status;
51         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
52         BOOL ret = True;
53         const char *myaddress = iface_best_ip(address);
54         struct socket_address *socket_address;
55
56         socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
57                                                      myaddress, 0);
58         if (!socket_address) {
59                 return False;
60         }
61
62         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
63         if (!NT_STATUS_IS_OK(status)) {
64                 printf("socket_listen for nbt_register_own failed: %s\n", nt_errstr(status));
65                 return False;
66         }
67
68         printf("Testing name defense to name registration\n");
69
70         io.in.name = *name;
71         io.in.dest_addr = address;
72         io.in.address = myaddress;
73         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
74         io.in.register_demand = False;
75         io.in.broadcast = True;
76         io.in.multi_homed = False;
77         io.in.ttl = 1234;
78         io.in.timeout = 3;
79         io.in.retries = 0;
80         
81         status = nbt_name_register(nbtsock, mem_ctx, &io);
82         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
83                 printf("No response from %s for name register\n", address);
84                 return False;
85         }
86         if (!NT_STATUS_IS_OK(status)) {
87                 printf("Bad response from %s for name register - %s\n",
88                        address, nt_errstr(status));
89                 return False;
90         }
91         
92         CHECK_STRING(io.out.name.name, name->name);
93         CHECK_VALUE(io.out.name.type, name->type);
94         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
95
96         /* check a register demand */
97         io.in.address = myaddress;
98         io.in.register_demand = True;
99
100         status = nbt_name_register(nbtsock, mem_ctx, &io);
101         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
102                 printf("No response from %s for name register demand\n", address);
103                 return False;
104         }
105         if (!NT_STATUS_IS_OK(status)) {
106                 printf("Bad response from %s for name register demand - %s\n",
107                        address, nt_errstr(status));
108                 return False;
109         }
110         
111         CHECK_STRING(io.out.name.name, name->name);
112         CHECK_VALUE(io.out.name.type, name->type);
113         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
114
115         return ret;
116 }
117
118
119 /*
120   test that a server responds correctly to attempted name refresh requests
121 */
122 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
123                             const char *address)
124 {
125         struct nbt_name_refresh io;
126         NTSTATUS status;
127         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
128         BOOL ret = True;
129         const char *myaddress = iface_best_ip(address);
130         struct socket_address *socket_address;
131
132         socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
133                                                      myaddress, 0);
134         if (!socket_address) {
135                 return False;
136         }
137
138         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
139         if (!NT_STATUS_IS_OK(status)) {
140                 printf("socket_listen for nbt_referesh_own failed: %s\n", nt_errstr(status));
141                 return False;
142         }
143
144         printf("Testing name defense to name refresh\n");
145
146         io.in.name = *name;
147         io.in.dest_addr = address;
148         io.in.address = myaddress;
149         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
150         io.in.broadcast = False;
151         io.in.ttl = 1234;
152         io.in.timeout = 3;
153         io.in.retries = 0;
154         
155         status = nbt_name_refresh(nbtsock, mem_ctx, &io);
156         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
157                 printf("No response from %s for name refresh\n", address);
158                 return False;
159         }
160         if (!NT_STATUS_IS_OK(status)) {
161                 printf("Bad response from %s for name refresh - %s\n",
162                        address, nt_errstr(status));
163                 return False;
164         }
165         
166         CHECK_STRING(io.out.name.name, name->name);
167         CHECK_VALUE(io.out.name.type, name->type);
168         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
169
170         return ret;
171 }
172
173
174
175 /*
176   test name registration to a server
177 */
178 BOOL torture_nbt_register(void)
179 {
180         const char *address;
181         struct nbt_name name;
182         TALLOC_CTX *mem_ctx = talloc_new(NULL);
183         NTSTATUS status;
184         BOOL ret = True;
185         
186         make_nbt_name_server(&name, strupper_talloc(mem_ctx, lp_parm_string(-1, "torture", "host")));
187
188         /* do an initial name resolution to find its IP */
189         status = resolve_name(&name, mem_ctx, &address, NULL);
190         if (!NT_STATUS_IS_OK(status)) {
191                 printf("Failed to resolve %s - %s\n",
192                        name.name, nt_errstr(status));
193                 talloc_free(mem_ctx);
194                 return False;
195         }
196
197         ret &= nbt_register_own(mem_ctx, &name, address);
198         ret &= nbt_refresh_own(mem_ctx, &name, address);
199
200         talloc_free(mem_ctx);
201
202         return ret;
203 }