r6246: stop waiting when we get a reply
[bbaumbach/samba-autobuild/.git] / source4 / torture / nbt / dgram.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT dgram 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 "libcli/dgram/libdgram.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "lib/socket/socket.h"
28 #include "lib/events/events.h"
29
30 #define TEST_NAME "TORTURE_TEST"
31
32 /*
33   reply handler for netlogon request
34 */
35 static void netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
36                              struct nbt_dgram_packet *packet, 
37                              const char *src_address, int src_port)
38 {
39         NTSTATUS status;
40         struct nbt_netlogon_packet netlogon;
41         int *replies = dgmslot->private;
42
43         printf("netlogon reply from %s:%d\n", src_address, src_port);
44
45         status = dgram_mailslot_netlogon_parse(dgmslot, dgmslot, packet, &netlogon);
46         if (!NT_STATUS_IS_OK(status)) {
47                 printf("Failed to parse netlogon packet from %s:%d\n",
48                        src_address, src_port);
49                 return;
50         }
51
52         NDR_PRINT_DEBUG(nbt_netlogon_packet, &netlogon);
53
54         (*replies)++;
55 }
56
57
58 /* test UDP/138 netlogon requests */
59 static BOOL nbt_test_netlogon(TALLOC_CTX *mem_ctx, 
60                               struct nbt_name name, const char *address)
61 {
62         struct dgram_mailslot_handler *dgmslot;
63         struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(mem_ctx, NULL);
64         const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
65         struct nbt_netlogon_packet logon;
66         struct nbt_name myname;
67         NTSTATUS status;
68         struct timeval tv = timeval_current();
69         int replies = 0;
70
71         /* try receiving replies on port 138 first, which will only
72            work if we are root and smbd/nmbd are not running - fall
73            back to listening on any port, which means replies from
74            some windows versions won't be seen */
75         status = socket_listen(dgmsock->sock, myaddress, lp_dgram_port(), 0, 0);
76         if (!NT_STATUS_IS_OK(status)) {
77                 socket_listen(dgmsock->sock, myaddress, 0, 0, 0);
78         }
79
80         /* setup a temporary mailslot listener for replies */
81         dgmslot = dgram_mailslot_temp(dgmsock, "\\MAILSLOT\\NET\\GETDC", 
82                                       netlogon_handler, &replies);
83         
84
85         ZERO_STRUCT(logon);
86         logon.command = NETLOGON_QUERY_FOR_PDC;
87         logon.req.pdc.computer_name = TEST_NAME;
88         logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
89         logon.req.pdc.unicode_name  = TEST_NAME;
90         logon.req.pdc.nt_version    = 1;
91         logon.req.pdc.lmnt_token    = 0xFFFF;
92         logon.req.pdc.lm20_token    = 0xFFFF;
93
94         myname.name = TEST_NAME;
95         myname.type = NBT_NAME_CLIENT;
96         myname.scope = NULL;
97
98         status = dgram_mailslot_netlogon_send(dgmsock, &name, address, &myname, &logon);
99         if (!NT_STATUS_IS_OK(status)) {
100                 printf("Failed to send netlogon request - %s\n", nt_errstr(status));
101                 goto failed;
102         }
103
104
105         while (timeval_elapsed(&tv) < 5 && replies == 0) {
106                 event_loop_once(dgmsock->event_ctx);
107         }
108
109         talloc_free(dgmsock);
110         return True;
111
112 failed:
113         talloc_free(dgmsock);
114         return False;
115 }
116
117
118 /*
119   test nbt dgram operations
120 */
121 BOOL torture_nbt_dgram(void)
122 {
123         const char *address;
124         struct nbt_name name;
125         TALLOC_CTX *mem_ctx = talloc_new(NULL);
126         NTSTATUS status;
127         BOOL ret = True;
128         
129         name.name = lp_parm_string(-1, "torture", "host");
130         name.type = NBT_NAME_PDC;
131         name.scope = NULL;
132
133         /* do an initial name resolution to find its IP */
134         status = resolve_name(&name, mem_ctx, &address);
135         if (!NT_STATUS_IS_OK(status)) {
136                 printf("Failed to resolve %s - %s\n",
137                        name.name, nt_errstr(status));
138                 talloc_free(mem_ctx);
139                 return False;
140         }
141
142         ret &= nbt_test_netlogon(mem_ctx, name, address);
143
144         talloc_free(mem_ctx);
145
146         return ret;
147 }