r12608: Remove some unused #include lines.
[bbaumbach/samba-autobuild/.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
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
52         socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
53
54         printf("Testing name defense to name registration\n");
55
56         io.in.name = *name;
57         io.in.dest_addr = address;
58         io.in.address = myaddress;
59         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
60         io.in.register_demand = False;
61         io.in.broadcast = True;
62         io.in.multi_homed = False;
63         io.in.ttl = 1234;
64         io.in.timeout = 3;
65         io.in.retries = 0;
66         
67         status = nbt_name_register(nbtsock, mem_ctx, &io);
68         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
69                 printf("No response from %s for name register\n", address);
70                 return False;
71         }
72         if (!NT_STATUS_IS_OK(status)) {
73                 printf("Bad response from %s for name register - %s\n",
74                        address, nt_errstr(status));
75                 return False;
76         }
77         
78         CHECK_STRING(io.out.name.name, name->name);
79         CHECK_VALUE(io.out.name.type, name->type);
80         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
81
82         /* check a register demand */
83         io.in.address = myaddress;
84         io.in.register_demand = True;
85
86         status = nbt_name_register(nbtsock, mem_ctx, &io);
87         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
88                 printf("No response from %s for name register\n", address);
89                 return False;
90         }
91         if (!NT_STATUS_IS_OK(status)) {
92                 printf("Bad response from %s for name register - %s\n",
93                        address, nt_errstr(status));
94                 return False;
95         }
96         
97         CHECK_STRING(io.out.name.name, name->name);
98         CHECK_VALUE(io.out.name.type, name->type);
99         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
100
101         return ret;
102 }
103
104
105 /*
106   test that a server responds correctly to attempted name refresh requests
107 */
108 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
109                             const char *address)
110 {
111         struct nbt_name_refresh io;
112         NTSTATUS status;
113         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
114         BOOL ret = True;
115         const char *myaddress = iface_best_ip(address);
116
117         socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
118
119         printf("Testing name defense to name refresh\n");
120
121         io.in.name = *name;
122         io.in.dest_addr = address;
123         io.in.address = myaddress;
124         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
125         io.in.broadcast = False;
126         io.in.ttl = 1234;
127         io.in.timeout = 3;
128         io.in.retries = 0;
129         
130         status = nbt_name_refresh(nbtsock, mem_ctx, &io);
131         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
132                 printf("No response from %s for name refresh\n", address);
133                 return False;
134         }
135         if (!NT_STATUS_IS_OK(status)) {
136                 printf("Bad response from %s for name refresh - %s\n",
137                        address, nt_errstr(status));
138                 return False;
139         }
140         
141         CHECK_STRING(io.out.name.name, name->name);
142         CHECK_VALUE(io.out.name.type, name->type);
143         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
144
145         return ret;
146 }
147
148
149
150 /*
151   test name registration to a server
152 */
153 BOOL torture_nbt_register(void)
154 {
155         const char *address;
156         struct nbt_name name;
157         TALLOC_CTX *mem_ctx = talloc_new(NULL);
158         NTSTATUS status;
159         BOOL ret = True;
160         
161         make_nbt_name_server(&name, strupper_talloc(mem_ctx, lp_parm_string(-1, "torture", "host")));
162
163         /* do an initial name resolution to find its IP */
164         status = resolve_name(&name, mem_ctx, &address, NULL);
165         if (!NT_STATUS_IS_OK(status)) {
166                 printf("Failed to resolve %s - %s\n",
167                        name.name, nt_errstr(status));
168                 talloc_free(mem_ctx);
169                 return False;
170         }
171
172         ret &= nbt_register_own(mem_ctx, &name, address);
173         ret &= nbt_refresh_own(mem_ctx, &name, address);
174
175         talloc_free(mem_ctx);
176
177         return ret;
178 }