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