r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[jelmer/samba4-debian.git] / source / libcli / dgram / dgramsocket.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    low level socket handling for nbt dgram requests (UDP138)
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/events/events.h"
25 #include "dlinklist.h"
26 #include "libcli/dgram/libdgram.h"
27 #include "lib/socket/socket.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29
30
31 /*
32   handle recv events on a nbt dgram socket
33 */
34 static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
35 {
36         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
37         NTSTATUS status;
38         struct socket_address *src;
39         DATA_BLOB blob;
40         size_t nread, dsize;
41         struct nbt_dgram_packet *packet;
42         const char *mailslot_name;
43
44         status = socket_pending(dgmsock->sock, &dsize);
45         if (!NT_STATUS_IS_OK(status)) {
46                 talloc_free(tmp_ctx);
47                 return;
48         }
49
50         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
51         if (blob.data == NULL) {
52                 talloc_free(tmp_ctx);
53                 return;
54         }
55
56         status = socket_recvfrom(dgmsock->sock, blob.data, blob.length, &nread, 0,
57                                  tmp_ctx, &src);
58         if (!NT_STATUS_IS_OK(status)) {
59                 talloc_free(tmp_ctx);
60                 return;
61         }
62         blob.length = nread;
63
64         DEBUG(2,("Received dgram packet of length %d from %s:%d\n", 
65                  (int)blob.length, src->addr, src->port));
66
67         packet = talloc(tmp_ctx, struct nbt_dgram_packet);
68         if (packet == NULL) {
69                 talloc_free(tmp_ctx);
70                 return;
71         }
72
73         /* parse the request */
74         status = ndr_pull_struct_blob(&blob, packet, packet, 
75                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
76         if (!NT_STATUS_IS_OK(status)) {
77                 DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
78                          nt_errstr(status)));
79                 talloc_free(tmp_ctx);
80                 return;
81         }
82
83         /* if this is a mailslot message, then see if we can dispatch it to a handler */
84         mailslot_name = dgram_mailslot_name(packet);
85         if (mailslot_name) {
86                 struct dgram_mailslot_handler *dgmslot;
87                 dgmslot = dgram_mailslot_find(dgmsock, mailslot_name);
88                 if (dgmslot) {
89                         dgmslot->handler(dgmslot, packet, src);
90                 } else {
91                         DEBUG(2,("No mailslot handler for '%s'\n", mailslot_name));
92                 }
93         } else {
94                 /* dispatch if there is a general handler */
95                 if (dgmsock->incoming.handler) {
96                         dgmsock->incoming.handler(dgmsock, packet, src);
97                 }
98         }
99
100         talloc_free(tmp_ctx);
101 }
102
103
104 /*
105   handle send events on a nbt dgram socket
106 */
107 static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
108 {
109         struct nbt_dgram_request *req;
110         NTSTATUS status;
111
112         while ((req = dgmsock->send_queue)) {
113                 size_t len;
114                 
115                 len = req->encoded.length;
116                 status = socket_sendto(dgmsock->sock, &req->encoded, &len, 0, 
117                                        req->dest);
118                 if (NT_STATUS_IS_ERR(status)) {
119                         DEBUG(3,("Failed to send datagram of length %u to %s:%d: %s\n",
120                                  (unsigned)req->encoded.length, req->dest->addr, req->dest->port, 
121                                  nt_errstr(status)));
122                         DLIST_REMOVE(dgmsock->send_queue, req);
123                         talloc_free(req);
124                         continue;
125                 }
126
127                 if (!NT_STATUS_IS_OK(status)) return;
128
129                 DLIST_REMOVE(dgmsock->send_queue, req);
130                 talloc_free(req);
131         }
132
133         EVENT_FD_NOT_WRITEABLE(dgmsock->fde);
134         return;
135 }
136
137
138 /*
139   handle fd events on a nbt_dgram_socket
140 */
141 static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
142                                uint16_t flags, void *private)
143 {
144         struct nbt_dgram_socket *dgmsock = talloc_get_type(private, 
145                                                            struct nbt_dgram_socket);
146         if (flags & EVENT_FD_WRITE) {
147                 dgm_socket_send(dgmsock);
148         } 
149         if (flags & EVENT_FD_READ) {
150                 dgm_socket_recv(dgmsock);
151         }
152 }
153
154 /*
155   initialise a nbt_dgram_socket. The event_ctx is optional, if provided
156   then operations will use that event context
157 */
158 struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx, 
159                                               struct event_context *event_ctx)
160 {
161         struct nbt_dgram_socket *dgmsock;
162         NTSTATUS status;
163
164         dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
165         if (dgmsock == NULL) goto failed;
166
167         if (event_ctx == NULL) {
168                 dgmsock->event_ctx = event_context_init(dgmsock);
169         } else {
170                 dgmsock->event_ctx = talloc_reference(dgmsock, event_ctx);
171         }
172         if (dgmsock->event_ctx == NULL) goto failed;
173
174         status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
175         if (!NT_STATUS_IS_OK(status)) goto failed;
176
177         socket_set_option(dgmsock->sock, "SO_BROADCAST", "1");
178
179         talloc_steal(dgmsock, dgmsock->sock);
180
181         dgmsock->fde = event_add_fd(dgmsock->event_ctx, dgmsock, 
182                                     socket_get_fd(dgmsock->sock), 0,
183                                     dgm_socket_handler, dgmsock);
184
185         dgmsock->send_queue = NULL;
186         dgmsock->incoming.handler = NULL;
187         dgmsock->mailslot_handlers = NULL;
188         
189         return dgmsock;
190
191 failed:
192         talloc_free(dgmsock);
193         return NULL;
194 }
195
196
197 /*
198   setup a handler for generic incoming requests
199 */
200 NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
201                                     void (*handler)(struct nbt_dgram_socket *, 
202                                                     struct nbt_dgram_packet *, 
203                                                     struct socket_address *),
204                                     void *private)
205 {
206         dgmsock->incoming.handler = handler;
207         dgmsock->incoming.private = private;
208         EVENT_FD_READABLE(dgmsock->fde);
209         return NT_STATUS_OK;
210 }
211
212
213 /*
214   queue a datagram for send
215 */
216 NTSTATUS nbt_dgram_send(struct nbt_dgram_socket *dgmsock,
217                         struct nbt_dgram_packet *packet,
218                         struct socket_address *dest)
219 {
220         struct nbt_dgram_request *req;
221         NTSTATUS status = NT_STATUS_NO_MEMORY;
222
223         req = talloc(dgmsock, struct nbt_dgram_request);
224         if (req == NULL) goto failed;
225
226         req->dest = dest;
227         if (talloc_reference(req, dest) == NULL) goto failed;
228
229         status = ndr_push_struct_blob(&req->encoded, req, packet, 
230                                       (ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
231         if (!NT_STATUS_IS_OK(status)) goto failed;
232
233         DLIST_ADD_END(dgmsock->send_queue, req, struct nbt_dgram_request *);
234
235         EVENT_FD_WRITEABLE(dgmsock->fde);
236
237         return NT_STATUS_OK;
238
239 failed:
240         talloc_free(req);
241         return status;
242 }