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