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