r6223: added a bit more datagram infrastructure and the beginnings of a test
[ira/wip.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         printf("netlogon reply from %s:%d\n", src_address, src_port);
40 }
41
42 /* test UDP/138 netlogon requests */
43 static BOOL nbt_test_netlogon(TALLOC_CTX *mem_ctx, 
44                               struct nbt_name name, const char *address)
45 {
46         struct dgram_mailslot_handler *dgmslot;
47         struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(mem_ctx, NULL);
48         const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
49         struct nbt_netlogon_packet logon;
50         struct nbt_name myname;
51         NTSTATUS status;
52         int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
53         struct timeval tv = timeval_current();
54
55         socket_listen(dgmsock->sock, myaddress, 0, 0, 0);
56
57         /* setup a temporary mailslot listener for replies */
58         dgmslot = dgram_mailslot_temp(dgmsock, "\\MAILSLOT\\NET\\GETDC", 
59                                       netlogon_handler, NULL);
60         
61
62         ZERO_STRUCT(logon);
63         logon.command = NETLOGON_QUERY_FOR_PDC;
64         logon.req.pdc.computer_name = TEST_NAME;
65         logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
66         logon.req.pdc.unicode_name  = TEST_NAME;
67         logon.req.pdc.nt_version    = 1;
68         logon.req.pdc.lmnt_token    = 0xFFFF;
69         logon.req.pdc.lm20_token    = 0xFFFF;
70
71         myname.name = TEST_NAME;
72         myname.type = NBT_NAME_CLIENT;
73         myname.scope = NULL;
74
75         status = dgram_mailslot_netlogon_send(dgmsock, &name, address, &myname, &logon);
76         if (!NT_STATUS_IS_OK(status)) {
77                 printf("Failed to send netlogon request - %s\n", nt_errstr(status));
78                 goto failed;
79         }
80
81
82         while (timeval_elapsed(&tv) < timelimit) {
83                 event_loop_once(dgmsock->event_ctx);
84         }
85
86         talloc_free(dgmsock);
87         return True;
88
89 failed:
90         talloc_free(dgmsock);
91         return False;
92 }
93
94
95 /*
96   test nbt dgram operations
97 */
98 BOOL torture_nbt_dgram(void)
99 {
100         const char *address;
101         struct nbt_name name;
102         TALLOC_CTX *mem_ctx = talloc_new(NULL);
103         NTSTATUS status;
104         BOOL ret = True;
105         
106         name.name = lp_parm_string(-1, "torture", "host");
107         name.type = NBT_NAME_PDC;
108         name.scope = NULL;
109
110         /* do an initial name resolution to find its IP */
111         status = resolve_name(&name, mem_ctx, &address);
112         if (!NT_STATUS_IS_OK(status)) {
113                 printf("Failed to resolve %s - %s\n",
114                        name.name, nt_errstr(status));
115                 talloc_free(mem_ctx);
116                 return False;
117         }
118
119         ret &= nbt_test_netlogon(mem_ctx, name, address);
120
121         talloc_free(mem_ctx);
122
123         return ret;
124 }