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