r7732: Implementation of very basic lookup function (to be used in more
[jra/samba/.git] / source4 / libnet / libnet_lookup.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   a composite function for name resolving
23 */
24
25 #include "includes.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "lib/events/events.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/composite/monitor.h"
30 #include "libnet/composite.h"
31 #include "librpc/gen_ndr/ndr_nbt.h"
32
33
34 struct lookup_state {
35         struct composite_context *resolve_ctx;
36         struct nbt_name hostname;
37         char address[16];
38 };
39
40
41 struct composite_context *libnet_Lookup_send(struct libnet_lookup *io)
42 {
43         struct composite_context *c;
44         struct lookup_state *s;
45
46         if (!io) return NULL;
47
48         /* allocate context and state structures */
49         c = talloc_zero(NULL, struct composite_context);
50         if (c == NULL) goto failed;
51
52         s = talloc_zero(c, struct lookup_state);
53         if (s == NULL) goto failed;
54         
55         /* prepare event context */
56         c->event_ctx = event_context_init(c);
57         if (c->event_ctx == NULL) goto failed;
58
59         /* parameters */
60         s->hostname.name   = talloc_strdup(s, io->in.hostname);
61         s->hostname.type   = io->in.type;
62         s->hostname.scope  = NULL;
63
64         c->private  = s;
65         c->state    = SMBCLI_REQUEST_SEND;
66
67         /* send resolve request */
68         s->resolve_ctx = resolve_name_send(&s->hostname, c->event_ctx, io->in.methods);
69
70         return c;
71
72 failed:
73         talloc_free(c);
74         return NULL;
75 }
76
77
78 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
79                             struct libnet_lookup *io)
80 {
81         NTSTATUS status;
82         struct lookup_state *s;
83
84         s = talloc_get_type(c->private, struct lookup_state);
85
86         status = resolve_name_recv(s->resolve_ctx, mem_ctx, io->out.address);
87         return status;
88 }
89
90
91 NTSTATUS libnet_Lookup(TALLOC_CTX *mem_ctx, struct libnet_lookup *io)
92 {
93         struct composite_context *c = libnet_Lookup_send(io);
94         return libnet_Lookup_recv(c, mem_ctx, io);
95 }